blob: 493b6b1181b9086810078d469099e8beb2e128d4 [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 Lesinski5b7337f2017-06-26 14:57:22 -070026#include "ResourceTable.h"
Adam Lesinskic744ae82017-05-17 19:28:38 -070027#include "SdkConstants.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070028#include "process/IResourceTableConsumer.h"
Adam Lesinski467f1712015-11-16 17:35:44 -080029#include "xml/XmlDom.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070030
Adam Lesinski1ab598f2015-08-14 14:26:04 -070031namespace aapt {
32
33class ResourceTable;
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -080034class ResourceEntry;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070035struct ConfigDescription;
36
37/**
Adam Lesinskicacb28f2016-10-19 12:18:14 -070038 * Defines the location in which a value exists. This determines visibility of
Adam Lesinskice5e56e2016-10-21 17:56:45 -070039 * other package's private symbols.
Adam Lesinski467f1712015-11-16 17:35:44 -080040 */
41struct CallSite {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070042 ResourceNameRef resource;
Adam Lesinski467f1712015-11-16 17:35:44 -080043};
44
Adam Lesinski5b7337f2017-06-26 14:57:22 -070045// Determines whether a versioned resource should be created. If a versioned resource already
46// exists, it takes precedence.
Adam Lesinskic744ae82017-05-17 19:28:38 -070047bool ShouldGenerateVersionedResource(const ResourceEntry* entry, const ConfigDescription& config,
48 const ApiVersion sdk_version_to_generate);
49
Adam Lesinski5b7337f2017-06-26 14:57:22 -070050// Finds the next largest ApiVersion of `config` for values defined for `entry`.
Adam Lesinskic744ae82017-05-17 19:28:38 -070051ApiVersion FindNextApiVersionForConfig(const ResourceEntry* entry, const ConfigDescription& config);
Adam Lesinski1ab598f2015-08-14 14:26:04 -070052
Adam Lesinski5b7337f2017-06-26 14:57:22 -070053// Finds the next largest ApiVersion of the config pointed to by the iterator `start`.
54ApiVersion FindNextApiVersionForConfigInSortedVector(
55 std::vector<std::unique_ptr<ResourceConfigValue>>::const_iterator start,
56 std::vector<std::unique_ptr<ResourceConfigValue>>::const_iterator end);
57
Adam Lesinskifb6312f2016-06-28 14:40:32 -070058class AutoVersioner : public IResourceTableConsumer {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070059 public:
Adam Lesinskice5e56e2016-10-21 17:56:45 -070060 AutoVersioner() = default;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070061
Adam Lesinskice5e56e2016-10-21 17:56:45 -070062 bool Consume(IAaptContext* context, ResourceTable* table) override;
63
64 private:
65 DISALLOW_COPY_AND_ASSIGN(AutoVersioner);
Adam Lesinski1ab598f2015-08-14 14:26:04 -070066};
67
Alexandria Cornwall77788eb2016-09-06 15:16:49 -070068/**
Adam Lesinskicacb28f2016-10-19 12:18:14 -070069 * If any attribute resource values are defined as public, this consumer will
70 * move all private
71 * attribute resource values to a private ^private-attr type, avoiding backwards
72 * compatibility
Adam Lesinski9f222042015-11-04 13:51:45 -080073 * issues with new apps running on old platforms.
74 *
Adam Lesinskicacb28f2016-10-19 12:18:14 -070075 * The Android platform ignores resource attributes it doesn't recognize, so an
76 * app developer can
77 * use new attributes in their layout XML files without worrying about
78 * versioning. This assumption
79 * actually breaks on older platforms. OEMs may add private attributes that are
80 * used internally.
81 * AAPT originally assigned all private attributes IDs immediately proceeding
82 * the public attributes'
Adam Lesinski9f222042015-11-04 13:51:45 -080083 * IDs.
84 *
Adam Lesinskicacb28f2016-10-19 12:18:14 -070085 * This means that on a newer Android platform, an ID previously assigned to a
86 * private attribute
Adam Lesinski9f222042015-11-04 13:51:45 -080087 * may end up assigned to a public attribute.
88 *
Adam Lesinskicacb28f2016-10-19 12:18:14 -070089 * App developers assume using the newer attribute is safe on older platforms
90 * because it will
91 * be ignored. Instead, the platform thinks the new attribute is an older,
92 * private attribute and
93 * will interpret it as such. This leads to unintended styling and exceptions
94 * thrown due to
Adam Lesinski9f222042015-11-04 13:51:45 -080095 * unexpected types.
96 *
Adam Lesinskicacb28f2016-10-19 12:18:14 -070097 * By moving the private attributes to a completely different type, this ID
98 * conflict will never
Adam Lesinski9f222042015-11-04 13:51:45 -080099 * occur.
100 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700101class PrivateAttributeMover : public IResourceTableConsumer {
102 public:
103 PrivateAttributeMover() = default;
104
105 bool Consume(IAaptContext* context, ResourceTable* table) override;
106
107 private:
108 DISALLOW_COPY_AND_ASSIGN(PrivateAttributeMover);
109};
110
111class ResourceConfigValue;
112
113class ProductFilter : public IResourceTableConsumer {
114 public:
Adam Lesinskic744ae82017-05-17 19:28:38 -0700115 using ResourceConfigValueIter = std::vector<std::unique_ptr<ResourceConfigValue>>::iterator;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700116
Adam Lesinskic744ae82017-05-17 19:28:38 -0700117 explicit ProductFilter(std::unordered_set<std::string> products) : products_(products) {
118 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700119
120 ResourceConfigValueIter SelectProductToKeep(
121 const ResourceNameRef& name, const ResourceConfigValueIter begin,
122 const ResourceConfigValueIter end, IDiagnostics* diag);
123
124 bool Consume(IAaptContext* context, ResourceTable* table) override;
125
126 private:
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700127 DISALLOW_COPY_AND_ASSIGN(ProductFilter);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700128
Adam Lesinskic744ae82017-05-17 19:28:38 -0700129 std::unordered_set<std::string> products_;
Adam Lesinski9f222042015-11-04 13:51:45 -0800130};
131
132/**
Alexandria Cornwalla7cc3f12016-08-16 13:33:32 -0700133 * Removes namespace nodes and URI information from the XmlResource.
134 *
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700135 * Once an XmlResource is processed by this consumer, it is no longer able to
136 * have its attributes
137 * parsed. As such, this XmlResource must have already been processed by
138 * XmlReferenceLinker.
Alexandria Cornwalla7cc3f12016-08-16 13:33:32 -0700139 */
140class XmlNamespaceRemover : public IXmlResourceConsumer {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700141 public:
Adam Lesinskic744ae82017-05-17 19:28:38 -0700142 explicit XmlNamespaceRemover(bool keep_uris = false) : keep_uris_(keep_uris){};
Alexandria Cornwalla7cc3f12016-08-16 13:33:32 -0700143
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700144 bool Consume(IAaptContext* context, xml::XmlResource* resource) override;
145
146 private:
147 DISALLOW_COPY_AND_ASSIGN(XmlNamespaceRemover);
148
149 bool keep_uris_;
Alexandria Cornwalla7cc3f12016-08-16 13:33:32 -0700150};
151
152/**
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700153 * Resolves attributes in the XmlResource and compiles string values to resource
154 * values.
Adam Lesinski9f222042015-11-04 13:51:45 -0800155 * Once an XmlResource is processed by this linker, it is ready to be flattened.
156 */
157class XmlReferenceLinker : public IXmlResourceConsumer {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700158 public:
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700159 XmlReferenceLinker() = default;
160
161 bool Consume(IAaptContext* context, xml::XmlResource* resource) override;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700162
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700163 private:
164 DISALLOW_COPY_AND_ASSIGN(XmlReferenceLinker);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700165};
166
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700167} // namespace aapt
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700168
169#endif /* AAPT_LINKER_LINKERS_H */