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 | |
Alexandria Cornwall | 77788eb | 2016-09-06 15:16:49 -0700 | [diff] [blame] | 61 | /** |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 62 | * 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 Lesinski | 9f22204 | 2015-11-04 13:51:45 -0800 | [diff] [blame] | 66 | * issues with new apps running on old platforms. |
| 67 | * |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 68 | * 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 Lesinski | 9f22204 | 2015-11-04 13:51:45 -0800 | [diff] [blame] | 76 | * IDs. |
| 77 | * |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 78 | * This means that on a newer Android platform, an ID previously assigned to a |
| 79 | * private attribute |
Adam Lesinski | 9f22204 | 2015-11-04 13:51:45 -0800 | [diff] [blame] | 80 | * may end up assigned to a public attribute. |
| 81 | * |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 82 | * 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 Lesinski | 9f22204 | 2015-11-04 13:51:45 -0800 | [diff] [blame] | 88 | * unexpected types. |
| 89 | * |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 90 | * By moving the private attributes to a completely different type, this ID |
| 91 | * conflict will never |
Adam Lesinski | 9f22204 | 2015-11-04 13:51:45 -0800 | [diff] [blame] | 92 | * occur. |
| 93 | */ |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 94 | class 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 | |
| 104 | class ResourceConfigValue; |
| 105 | |
| 106 | class 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 | |
| 126 | class 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 Lesinski | 9f22204 | 2015-11-04 13:51:45 -0800 | [diff] [blame] | 134 | }; |
| 135 | |
| 136 | /** |
Alexandria Cornwall | a7cc3f1 | 2016-08-16 13:33:32 -0700 | [diff] [blame] | 137 | * Removes namespace nodes and URI information from the XmlResource. |
| 138 | * |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 139 | * 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 Cornwall | a7cc3f1 | 2016-08-16 13:33:32 -0700 | [diff] [blame] | 143 | */ |
| 144 | class XmlNamespaceRemover : public IXmlResourceConsumer { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 145 | public: |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 146 | explicit XmlNamespaceRemover(bool keep_uris = false) |
| 147 | : keep_uris_(keep_uris){}; |
Alexandria Cornwall | a7cc3f1 | 2016-08-16 13:33:32 -0700 | [diff] [blame] | 148 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 149 | bool Consume(IAaptContext* context, xml::XmlResource* resource) override; |
| 150 | |
| 151 | private: |
| 152 | DISALLOW_COPY_AND_ASSIGN(XmlNamespaceRemover); |
| 153 | |
| 154 | bool keep_uris_; |
Alexandria Cornwall | a7cc3f1 | 2016-08-16 13:33:32 -0700 | [diff] [blame] | 155 | }; |
| 156 | |
| 157 | /** |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 158 | * Resolves attributes in the XmlResource and compiles string values to resource |
| 159 | * values. |
Adam Lesinski | 9f22204 | 2015-11-04 13:51:45 -0800 | [diff] [blame] | 160 | * Once an XmlResource is processed by this linker, it is ready to be flattened. |
| 161 | */ |
| 162 | class XmlReferenceLinker : public IXmlResourceConsumer { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 163 | public: |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 164 | XmlReferenceLinker() = default; |
| 165 | |
| 166 | bool Consume(IAaptContext* context, xml::XmlResource* resource) override; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 167 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 168 | /** |
| 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 Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 173 | 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 Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 179 | }; |
| 180 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 181 | } // namespace aapt |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 182 | |
| 183 | #endif /* AAPT_LINKER_LINKERS_H */ |