blob: 5527f9092c87f82c1cc23d34f1ab72a29dee501b [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 Lesinskic744ae82017-05-17 19:28:38 -070026#include "SdkConstants.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070027#include "process/IResourceTableConsumer.h"
Adam Lesinski467f1712015-11-16 17:35:44 -080028#include "xml/XmlDom.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070029
Adam Lesinski1ab598f2015-08-14 14:26:04 -070030namespace aapt {
31
32class ResourceTable;
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -080033class ResourceEntry;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070034struct ConfigDescription;
35
36/**
Adam Lesinskicacb28f2016-10-19 12:18:14 -070037 * Defines the location in which a value exists. This determines visibility of
Adam Lesinskice5e56e2016-10-21 17:56:45 -070038 * other package's private symbols.
Adam Lesinski467f1712015-11-16 17:35:44 -080039 */
40struct CallSite {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070041 ResourceNameRef resource;
Adam Lesinski467f1712015-11-16 17:35:44 -080042};
43
Adam Lesinskibb94f322017-07-12 07:41:55 -070044/**
45 * Determines whether a versioned resource should be created. If a versioned
46 * resource already exists, it takes precedence.
47 */
Adam Lesinskic744ae82017-05-17 19:28:38 -070048bool ShouldGenerateVersionedResource(const ResourceEntry* entry, const ConfigDescription& config,
49 const ApiVersion sdk_version_to_generate);
50
Adam Lesinskibb94f322017-07-12 07:41:55 -070051// Finds the next largest ApiVersion of the config which is identical to the given config except
52// for sdkVersion.
Adam Lesinskic744ae82017-05-17 19:28:38 -070053ApiVersion FindNextApiVersionForConfig(const ResourceEntry* entry, const ConfigDescription& config);
Adam Lesinski1ab598f2015-08-14 14:26:04 -070054
Adam Lesinskifb6312f2016-06-28 14:40:32 -070055class AutoVersioner : public IResourceTableConsumer {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070056 public:
Adam Lesinskice5e56e2016-10-21 17:56:45 -070057 AutoVersioner() = default;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070058
Adam Lesinskice5e56e2016-10-21 17:56:45 -070059 bool Consume(IAaptContext* context, ResourceTable* table) override;
60
61 private:
62 DISALLOW_COPY_AND_ASSIGN(AutoVersioner);
Adam Lesinski1ab598f2015-08-14 14:26:04 -070063};
64
Alexandria Cornwall77788eb2016-09-06 15:16:49 -070065/**
Adam Lesinskicacb28f2016-10-19 12:18:14 -070066 * If any attribute resource values are defined as public, this consumer will
67 * move all private
68 * attribute resource values to a private ^private-attr type, avoiding backwards
69 * compatibility
Adam Lesinski9f222042015-11-04 13:51:45 -080070 * issues with new apps running on old platforms.
71 *
Adam Lesinskicacb28f2016-10-19 12:18:14 -070072 * The Android platform ignores resource attributes it doesn't recognize, so an
73 * app developer can
74 * use new attributes in their layout XML files without worrying about
75 * versioning. This assumption
76 * actually breaks on older platforms. OEMs may add private attributes that are
77 * used internally.
78 * AAPT originally assigned all private attributes IDs immediately proceeding
79 * the public attributes'
Adam Lesinski9f222042015-11-04 13:51:45 -080080 * IDs.
81 *
Adam Lesinskicacb28f2016-10-19 12:18:14 -070082 * This means that on a newer Android platform, an ID previously assigned to a
83 * private attribute
Adam Lesinski9f222042015-11-04 13:51:45 -080084 * may end up assigned to a public attribute.
85 *
Adam Lesinskicacb28f2016-10-19 12:18:14 -070086 * App developers assume using the newer attribute is safe on older platforms
87 * because it will
88 * be ignored. Instead, the platform thinks the new attribute is an older,
89 * private attribute and
90 * will interpret it as such. This leads to unintended styling and exceptions
91 * thrown due to
Adam Lesinski9f222042015-11-04 13:51:45 -080092 * unexpected types.
93 *
Adam Lesinskicacb28f2016-10-19 12:18:14 -070094 * By moving the private attributes to a completely different type, this ID
95 * conflict will never
Adam Lesinski9f222042015-11-04 13:51:45 -080096 * occur.
97 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -070098class PrivateAttributeMover : public IResourceTableConsumer {
99 public:
100 PrivateAttributeMover() = default;
101
102 bool Consume(IAaptContext* context, ResourceTable* table) override;
103
104 private:
105 DISALLOW_COPY_AND_ASSIGN(PrivateAttributeMover);
106};
107
108class ResourceConfigValue;
109
110class ProductFilter : public IResourceTableConsumer {
111 public:
Adam Lesinskic744ae82017-05-17 19:28:38 -0700112 using ResourceConfigValueIter = std::vector<std::unique_ptr<ResourceConfigValue>>::iterator;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700113
Adam Lesinskic744ae82017-05-17 19:28:38 -0700114 explicit ProductFilter(std::unordered_set<std::string> products) : products_(products) {
115 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700116
117 ResourceConfigValueIter SelectProductToKeep(
118 const ResourceNameRef& name, const ResourceConfigValueIter begin,
119 const ResourceConfigValueIter end, IDiagnostics* diag);
120
121 bool Consume(IAaptContext* context, ResourceTable* table) override;
122
123 private:
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700124 DISALLOW_COPY_AND_ASSIGN(ProductFilter);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700125
Adam Lesinskic744ae82017-05-17 19:28:38 -0700126 std::unordered_set<std::string> products_;
Adam Lesinski9f222042015-11-04 13:51:45 -0800127};
128
129/**
Alexandria Cornwalla7cc3f12016-08-16 13:33:32 -0700130 * Removes namespace nodes and URI information from the XmlResource.
131 *
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700132 * Once an XmlResource is processed by this consumer, it is no longer able to
133 * have its attributes
134 * parsed. As such, this XmlResource must have already been processed by
135 * XmlReferenceLinker.
Alexandria Cornwalla7cc3f12016-08-16 13:33:32 -0700136 */
137class XmlNamespaceRemover : public IXmlResourceConsumer {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700138 public:
Adam Lesinskic744ae82017-05-17 19:28:38 -0700139 explicit XmlNamespaceRemover(bool keep_uris = false) : keep_uris_(keep_uris){};
Alexandria Cornwalla7cc3f12016-08-16 13:33:32 -0700140
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700141 bool Consume(IAaptContext* context, xml::XmlResource* resource) override;
142
143 private:
144 DISALLOW_COPY_AND_ASSIGN(XmlNamespaceRemover);
145
146 bool keep_uris_;
Alexandria Cornwalla7cc3f12016-08-16 13:33:32 -0700147};
148
149/**
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700150 * Resolves attributes in the XmlResource and compiles string values to resource
151 * values.
Adam Lesinski9f222042015-11-04 13:51:45 -0800152 * Once an XmlResource is processed by this linker, it is ready to be flattened.
153 */
154class XmlReferenceLinker : public IXmlResourceConsumer {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700155 public:
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700156 XmlReferenceLinker() = default;
157
158 bool Consume(IAaptContext* context, xml::XmlResource* resource) override;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700159
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700160 private:
161 DISALLOW_COPY_AND_ASSIGN(XmlReferenceLinker);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700162};
163
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700164} // namespace aapt
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700165
166#endif /* AAPT_LINKER_LINKERS_H */