Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 1 | /* |
| 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 Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 20 | #include <set> |
| 21 | #include <unordered_set> |
| 22 | |
| 23 | #include "android-base/macros.h" |
| 24 | |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 25 | #include "Resource.h" |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 26 | #include "process/IResourceTableConsumer.h" |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 27 | #include "xml/XmlDom.h" |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 28 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 29 | namespace aapt { |
| 30 | |
| 31 | class ResourceTable; |
Adam Lesinski | e4bb9eb | 2016-02-12 22:18:51 -0800 | [diff] [blame] | 32 | class ResourceEntry; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 33 | struct ConfigDescription; |
| 34 | |
| 35 | /** |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 36 | * Defines the location in which a value exists. This determines visibility of |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 37 | * other package's private symbols. |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 38 | */ |
| 39 | struct CallSite { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 40 | ResourceNameRef resource; |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 41 | }; |
| 42 | |
| 43 | /** |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 44 | * Determines whether a versioned resource should be created. If a versioned |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 45 | * resource already exists, it takes precedence. |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 46 | */ |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 47 | bool ShouldGenerateVersionedResource(const ResourceEntry* entry, |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 48 | const ConfigDescription& config, |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 49 | const int sdk_version_to_generate); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 50 | |
Adam Lesinski | fb6312f | 2016-06-28 14:40:32 -0700 | [diff] [blame] | 51 | class AutoVersioner : public IResourceTableConsumer { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 52 | public: |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 53 | AutoVersioner() = default; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 54 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 55 | bool Consume(IAaptContext* context, ResourceTable* table) override; |
| 56 | |
| 57 | private: |
| 58 | DISALLOW_COPY_AND_ASSIGN(AutoVersioner); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 59 | }; |
| 60 | |
Adam Lesinski | fb6312f | 2016-06-28 14:40:32 -0700 | [diff] [blame] | 61 | class VersionCollapser : public IResourceTableConsumer { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 62 | public: |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 63 | VersionCollapser() = default; |
| 64 | |
| 65 | bool Consume(IAaptContext* context, ResourceTable* table) override; |
| 66 | |
| 67 | private: |
| 68 | DISALLOW_COPY_AND_ASSIGN(VersionCollapser); |
Adam Lesinski | fb6312f | 2016-06-28 14:40:32 -0700 | [diff] [blame] | 69 | }; |
| 70 | |
Adam Lesinski | 9f22204 | 2015-11-04 13:51:45 -0800 | [diff] [blame] | 71 | /** |
Alexandria Cornwall | 77788eb | 2016-09-06 15:16:49 -0700 | [diff] [blame] | 72 | * Removes duplicated key-value entries from dominated resources. |
| 73 | */ |
| 74 | class ResourceDeduper : public IResourceTableConsumer { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 75 | public: |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 76 | ResourceDeduper() = default; |
| 77 | |
| 78 | bool Consume(IAaptContext* context, ResourceTable* table) override; |
| 79 | |
| 80 | private: |
| 81 | DISALLOW_COPY_AND_ASSIGN(ResourceDeduper); |
Alexandria Cornwall | 77788eb | 2016-09-06 15:16:49 -0700 | [diff] [blame] | 82 | }; |
| 83 | |
| 84 | /** |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 85 | * 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 Lesinski | 9f22204 | 2015-11-04 13:51:45 -0800 | [diff] [blame] | 89 | * issues with new apps running on old platforms. |
| 90 | * |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 91 | * 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 Lesinski | 9f22204 | 2015-11-04 13:51:45 -0800 | [diff] [blame] | 99 | * IDs. |
| 100 | * |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 101 | * This means that on a newer Android platform, an ID previously assigned to a |
| 102 | * private attribute |
Adam Lesinski | 9f22204 | 2015-11-04 13:51:45 -0800 | [diff] [blame] | 103 | * may end up assigned to a public attribute. |
| 104 | * |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 105 | * 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 Lesinski | 9f22204 | 2015-11-04 13:51:45 -0800 | [diff] [blame] | 111 | * unexpected types. |
| 112 | * |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 113 | * By moving the private attributes to a completely different type, this ID |
| 114 | * conflict will never |
Adam Lesinski | 9f22204 | 2015-11-04 13:51:45 -0800 | [diff] [blame] | 115 | * occur. |
| 116 | */ |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 117 | class 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 | |
| 127 | class ResourceConfigValue; |
| 128 | |
| 129 | class 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 | |
| 149 | class 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 Lesinski | 9f22204 | 2015-11-04 13:51:45 -0800 | [diff] [blame] | 157 | }; |
| 158 | |
| 159 | /** |
Alexandria Cornwall | a7cc3f1 | 2016-08-16 13:33:32 -0700 | [diff] [blame] | 160 | * Removes namespace nodes and URI information from the XmlResource. |
| 161 | * |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 162 | * 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 Cornwall | a7cc3f1 | 2016-08-16 13:33:32 -0700 | [diff] [blame] | 166 | */ |
| 167 | class XmlNamespaceRemover : public IXmlResourceConsumer { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 168 | public: |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 169 | explicit XmlNamespaceRemover(bool keep_uris = false) |
| 170 | : keep_uris_(keep_uris){}; |
Alexandria Cornwall | a7cc3f1 | 2016-08-16 13:33:32 -0700 | [diff] [blame] | 171 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 172 | bool Consume(IAaptContext* context, xml::XmlResource* resource) override; |
| 173 | |
| 174 | private: |
| 175 | DISALLOW_COPY_AND_ASSIGN(XmlNamespaceRemover); |
| 176 | |
| 177 | bool keep_uris_; |
Alexandria Cornwall | a7cc3f1 | 2016-08-16 13:33:32 -0700 | [diff] [blame] | 178 | }; |
| 179 | |
| 180 | /** |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 181 | * Resolves attributes in the XmlResource and compiles string values to resource |
| 182 | * values. |
Adam Lesinski | 9f22204 | 2015-11-04 13:51:45 -0800 | [diff] [blame] | 183 | * Once an XmlResource is processed by this linker, it is ready to be flattened. |
| 184 | */ |
| 185 | class XmlReferenceLinker : public IXmlResourceConsumer { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 186 | public: |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 187 | XmlReferenceLinker() = default; |
| 188 | |
| 189 | bool Consume(IAaptContext* context, xml::XmlResource* resource) override; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 190 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 191 | /** |
| 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 Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 196 | 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 Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 202 | }; |
| 203 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 204 | } // namespace aapt |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 205 | |
| 206 | #endif /* AAPT_LINKER_LINKERS_H */ |