blob: d00fa736f28b39a5b44813565c1a3f8a7f5466ad [file] [log] [blame]
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef AAPT_LINKER_LINKERS_H
18#define AAPT_LINKER_LINKERS_H
19
Adam Lesinskice5e56e2016-10-21 17:56:45 -070020#include <set>
21#include <unordered_set>
22
23#include "android-base/macros.h"
24
Adam Lesinski467f1712015-11-16 17:35:44 -080025#include "Resource.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070026#include "process/IResourceTableConsumer.h"
Adam Lesinski467f1712015-11-16 17:35:44 -080027#include "xml/XmlDom.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070028
Adam Lesinski1ab598f2015-08-14 14:26:04 -070029namespace aapt {
30
31class ResourceTable;
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -080032class ResourceEntry;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070033struct ConfigDescription;
34
35/**
Adam Lesinskicacb28f2016-10-19 12:18:14 -070036 * Defines the location in which a value exists. This determines visibility of
Adam Lesinskice5e56e2016-10-21 17:56:45 -070037 * other package's private symbols.
Adam Lesinski467f1712015-11-16 17:35:44 -080038 */
39struct CallSite {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070040 ResourceNameRef resource;
Adam Lesinski467f1712015-11-16 17:35:44 -080041};
42
43/**
Adam Lesinskicacb28f2016-10-19 12:18:14 -070044 * Determines whether a versioned resource should be created. If a versioned
Adam Lesinskice5e56e2016-10-21 17:56:45 -070045 * resource already exists, it takes precedence.
Adam Lesinski1ab598f2015-08-14 14:26:04 -070046 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -070047bool ShouldGenerateVersionedResource(const ResourceEntry* entry,
Adam Lesinskicacb28f2016-10-19 12:18:14 -070048 const ConfigDescription& config,
Adam Lesinskice5e56e2016-10-21 17:56:45 -070049 const int sdk_version_to_generate);
Adam Lesinski1ab598f2015-08-14 14:26:04 -070050
Adam Lesinskifb6312f2016-06-28 14:40:32 -070051class AutoVersioner : public IResourceTableConsumer {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070052 public:
Adam Lesinskice5e56e2016-10-21 17:56:45 -070053 AutoVersioner() = default;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070054
Adam Lesinskice5e56e2016-10-21 17:56:45 -070055 bool Consume(IAaptContext* context, ResourceTable* table) override;
56
57 private:
58 DISALLOW_COPY_AND_ASSIGN(AutoVersioner);
Adam Lesinski1ab598f2015-08-14 14:26:04 -070059};
60
Alexandria Cornwall77788eb2016-09-06 15:16:49 -070061/**
Adam Lesinskicacb28f2016-10-19 12:18:14 -070062 * If any attribute resource values are defined as public, this consumer will
63 * move all private
64 * attribute resource values to a private ^private-attr type, avoiding backwards
65 * compatibility
Adam Lesinski9f222042015-11-04 13:51:45 -080066 * issues with new apps running on old platforms.
67 *
Adam Lesinskicacb28f2016-10-19 12:18:14 -070068 * The Android platform ignores resource attributes it doesn't recognize, so an
69 * app developer can
70 * use new attributes in their layout XML files without worrying about
71 * versioning. This assumption
72 * actually breaks on older platforms. OEMs may add private attributes that are
73 * used internally.
74 * AAPT originally assigned all private attributes IDs immediately proceeding
75 * the public attributes'
Adam Lesinski9f222042015-11-04 13:51:45 -080076 * IDs.
77 *
Adam Lesinskicacb28f2016-10-19 12:18:14 -070078 * This means that on a newer Android platform, an ID previously assigned to a
79 * private attribute
Adam Lesinski9f222042015-11-04 13:51:45 -080080 * may end up assigned to a public attribute.
81 *
Adam Lesinskicacb28f2016-10-19 12:18:14 -070082 * App developers assume using the newer attribute is safe on older platforms
83 * because it will
84 * be ignored. Instead, the platform thinks the new attribute is an older,
85 * private attribute and
86 * will interpret it as such. This leads to unintended styling and exceptions
87 * thrown due to
Adam Lesinski9f222042015-11-04 13:51:45 -080088 * unexpected types.
89 *
Adam Lesinskicacb28f2016-10-19 12:18:14 -070090 * By moving the private attributes to a completely different type, this ID
91 * conflict will never
Adam Lesinski9f222042015-11-04 13:51:45 -080092 * occur.
93 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -070094class PrivateAttributeMover : public IResourceTableConsumer {
95 public:
96 PrivateAttributeMover() = default;
97
98 bool Consume(IAaptContext* context, ResourceTable* table) override;
99
100 private:
101 DISALLOW_COPY_AND_ASSIGN(PrivateAttributeMover);
102};
103
104class ResourceConfigValue;
105
106class ProductFilter : public IResourceTableConsumer {
107 public:
108 using ResourceConfigValueIter =
109 std::vector<std::unique_ptr<ResourceConfigValue>>::iterator;
110
111 explicit ProductFilter(std::unordered_set<std::string> products)
112 : products_(products) {}
113
114 ResourceConfigValueIter SelectProductToKeep(
115 const ResourceNameRef& name, const ResourceConfigValueIter begin,
116 const ResourceConfigValueIter end, IDiagnostics* diag);
117
118 bool Consume(IAaptContext* context, ResourceTable* table) override;
119
120 private:
121 std::unordered_set<std::string> products_;
122
123 DISALLOW_COPY_AND_ASSIGN(ProductFilter);
124};
125
126class XmlAutoVersioner : public IXmlResourceConsumer {
127 public:
128 XmlAutoVersioner() = default;
129
130 bool Consume(IAaptContext* context, xml::XmlResource* resource) override;
131
132 private:
133 DISALLOW_COPY_AND_ASSIGN(XmlAutoVersioner);
Adam Lesinski9f222042015-11-04 13:51:45 -0800134};
135
136/**
Alexandria Cornwalla7cc3f12016-08-16 13:33:32 -0700137 * Removes namespace nodes and URI information from the XmlResource.
138 *
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700139 * Once an XmlResource is processed by this consumer, it is no longer able to
140 * have its attributes
141 * parsed. As such, this XmlResource must have already been processed by
142 * XmlReferenceLinker.
Alexandria Cornwalla7cc3f12016-08-16 13:33:32 -0700143 */
144class XmlNamespaceRemover : public IXmlResourceConsumer {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700145 public:
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700146 explicit XmlNamespaceRemover(bool keep_uris = false)
147 : keep_uris_(keep_uris){};
Alexandria Cornwalla7cc3f12016-08-16 13:33:32 -0700148
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700149 bool Consume(IAaptContext* context, xml::XmlResource* resource) override;
150
151 private:
152 DISALLOW_COPY_AND_ASSIGN(XmlNamespaceRemover);
153
154 bool keep_uris_;
Alexandria Cornwalla7cc3f12016-08-16 13:33:32 -0700155};
156
157/**
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700158 * Resolves attributes in the XmlResource and compiles string values to resource
159 * values.
Adam Lesinski9f222042015-11-04 13:51:45 -0800160 * Once an XmlResource is processed by this linker, it is ready to be flattened.
161 */
162class XmlReferenceLinker : public IXmlResourceConsumer {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700163 public:
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700164 XmlReferenceLinker() = default;
165
166 bool Consume(IAaptContext* context, xml::XmlResource* resource) override;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700167
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700168 /**
169 * Once the XmlResource has been consumed, this returns the various SDK levels
170 * in which
171 * framework attributes used within the XML document were defined.
172 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700173 inline const std::set<int>& sdk_levels() const { return sdk_levels_found_; }
174
175 private:
176 DISALLOW_COPY_AND_ASSIGN(XmlReferenceLinker);
177
178 std::set<int> sdk_levels_found_;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700179};
180
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700181} // namespace aapt
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700182
183#endif /* AAPT_LINKER_LINKERS_H */