blob: 4687d2c01d68a01020c099a5ff33c7fb6934518e [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
Adam Lesinskifb6312f2016-06-28 14:40:32 -070061class VersionCollapser : public IResourceTableConsumer {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070062 public:
Adam Lesinskice5e56e2016-10-21 17:56:45 -070063 VersionCollapser() = default;
64
65 bool Consume(IAaptContext* context, ResourceTable* table) override;
66
67 private:
68 DISALLOW_COPY_AND_ASSIGN(VersionCollapser);
Adam Lesinskifb6312f2016-06-28 14:40:32 -070069};
70
Adam Lesinski9f222042015-11-04 13:51:45 -080071/**
Alexandria Cornwall77788eb2016-09-06 15:16:49 -070072 * Removes duplicated key-value entries from dominated resources.
73 */
74class ResourceDeduper : public IResourceTableConsumer {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070075 public:
Adam Lesinskice5e56e2016-10-21 17:56:45 -070076 ResourceDeduper() = default;
77
78 bool Consume(IAaptContext* context, ResourceTable* table) override;
79
80 private:
81 DISALLOW_COPY_AND_ASSIGN(ResourceDeduper);
Alexandria Cornwall77788eb2016-09-06 15:16:49 -070082};
83
84/**
Adam Lesinskicacb28f2016-10-19 12:18:14 -070085 * If any attribute resource values are defined as public, this consumer will
86 * move all private
87 * attribute resource values to a private ^private-attr type, avoiding backwards
88 * compatibility
Adam Lesinski9f222042015-11-04 13:51:45 -080089 * issues with new apps running on old platforms.
90 *
Adam Lesinskicacb28f2016-10-19 12:18:14 -070091 * The Android platform ignores resource attributes it doesn't recognize, so an
92 * app developer can
93 * use new attributes in their layout XML files without worrying about
94 * versioning. This assumption
95 * actually breaks on older platforms. OEMs may add private attributes that are
96 * used internally.
97 * AAPT originally assigned all private attributes IDs immediately proceeding
98 * the public attributes'
Adam Lesinski9f222042015-11-04 13:51:45 -080099 * IDs.
100 *
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700101 * This means that on a newer Android platform, an ID previously assigned to a
102 * private attribute
Adam Lesinski9f222042015-11-04 13:51:45 -0800103 * may end up assigned to a public attribute.
104 *
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700105 * App developers assume using the newer attribute is safe on older platforms
106 * because it will
107 * be ignored. Instead, the platform thinks the new attribute is an older,
108 * private attribute and
109 * will interpret it as such. This leads to unintended styling and exceptions
110 * thrown due to
Adam Lesinski9f222042015-11-04 13:51:45 -0800111 * unexpected types.
112 *
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700113 * By moving the private attributes to a completely different type, this ID
114 * conflict will never
Adam Lesinski9f222042015-11-04 13:51:45 -0800115 * occur.
116 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700117class PrivateAttributeMover : public IResourceTableConsumer {
118 public:
119 PrivateAttributeMover() = default;
120
121 bool Consume(IAaptContext* context, ResourceTable* table) override;
122
123 private:
124 DISALLOW_COPY_AND_ASSIGN(PrivateAttributeMover);
125};
126
127class ResourceConfigValue;
128
129class ProductFilter : public IResourceTableConsumer {
130 public:
131 using ResourceConfigValueIter =
132 std::vector<std::unique_ptr<ResourceConfigValue>>::iterator;
133
134 explicit ProductFilter(std::unordered_set<std::string> products)
135 : products_(products) {}
136
137 ResourceConfigValueIter SelectProductToKeep(
138 const ResourceNameRef& name, const ResourceConfigValueIter begin,
139 const ResourceConfigValueIter end, IDiagnostics* diag);
140
141 bool Consume(IAaptContext* context, ResourceTable* table) override;
142
143 private:
144 std::unordered_set<std::string> products_;
145
146 DISALLOW_COPY_AND_ASSIGN(ProductFilter);
147};
148
149class XmlAutoVersioner : public IXmlResourceConsumer {
150 public:
151 XmlAutoVersioner() = default;
152
153 bool Consume(IAaptContext* context, xml::XmlResource* resource) override;
154
155 private:
156 DISALLOW_COPY_AND_ASSIGN(XmlAutoVersioner);
Adam Lesinski9f222042015-11-04 13:51:45 -0800157};
158
159/**
Alexandria Cornwalla7cc3f12016-08-16 13:33:32 -0700160 * Removes namespace nodes and URI information from the XmlResource.
161 *
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700162 * Once an XmlResource is processed by this consumer, it is no longer able to
163 * have its attributes
164 * parsed. As such, this XmlResource must have already been processed by
165 * XmlReferenceLinker.
Alexandria Cornwalla7cc3f12016-08-16 13:33:32 -0700166 */
167class XmlNamespaceRemover : public IXmlResourceConsumer {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700168 public:
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700169 explicit XmlNamespaceRemover(bool keep_uris = false)
170 : keep_uris_(keep_uris){};
Alexandria Cornwalla7cc3f12016-08-16 13:33:32 -0700171
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700172 bool Consume(IAaptContext* context, xml::XmlResource* resource) override;
173
174 private:
175 DISALLOW_COPY_AND_ASSIGN(XmlNamespaceRemover);
176
177 bool keep_uris_;
Alexandria Cornwalla7cc3f12016-08-16 13:33:32 -0700178};
179
180/**
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700181 * Resolves attributes in the XmlResource and compiles string values to resource
182 * values.
Adam Lesinski9f222042015-11-04 13:51:45 -0800183 * Once an XmlResource is processed by this linker, it is ready to be flattened.
184 */
185class XmlReferenceLinker : public IXmlResourceConsumer {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700186 public:
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700187 XmlReferenceLinker() = default;
188
189 bool Consume(IAaptContext* context, xml::XmlResource* resource) override;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700190
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700191 /**
192 * Once the XmlResource has been consumed, this returns the various SDK levels
193 * in which
194 * framework attributes used within the XML document were defined.
195 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700196 inline const std::set<int>& sdk_levels() const { return sdk_levels_found_; }
197
198 private:
199 DISALLOW_COPY_AND_ASSIGN(XmlReferenceLinker);
200
201 std::set<int> sdk_levels_found_;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700202};
203
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700204} // namespace aapt
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700205
206#endif /* AAPT_LINKER_LINKERS_H */