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 | c744ae8 | 2017-05-17 19:28:38 -0700 | [diff] [blame] | 26 | #include "SdkConstants.h" |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 27 | #include "process/IResourceTableConsumer.h" |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 28 | #include "xml/XmlDom.h" |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 29 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 30 | namespace aapt { |
| 31 | |
| 32 | class ResourceTable; |
Adam Lesinski | e4bb9eb | 2016-02-12 22:18:51 -0800 | [diff] [blame] | 33 | class ResourceEntry; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 34 | struct ConfigDescription; |
| 35 | |
| 36 | /** |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 37 | * Defines the location in which a value exists. This determines visibility of |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 38 | * other package's private symbols. |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 39 | */ |
| 40 | struct CallSite { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 41 | ResourceNameRef resource; |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 42 | }; |
| 43 | |
Adam Lesinski | bb94f32 | 2017-07-12 07:41:55 -0700 | [diff] [blame] | 44 | /** |
| 45 | * Determines whether a versioned resource should be created. If a versioned |
| 46 | * resource already exists, it takes precedence. |
| 47 | */ |
Adam Lesinski | c744ae8 | 2017-05-17 19:28:38 -0700 | [diff] [blame] | 48 | bool ShouldGenerateVersionedResource(const ResourceEntry* entry, const ConfigDescription& config, |
| 49 | const ApiVersion sdk_version_to_generate); |
| 50 | |
Adam Lesinski | bb94f32 | 2017-07-12 07:41:55 -0700 | [diff] [blame] | 51 | // Finds the next largest ApiVersion of the config which is identical to the given config except |
| 52 | // for sdkVersion. |
Adam Lesinski | c744ae8 | 2017-05-17 19:28:38 -0700 | [diff] [blame] | 53 | ApiVersion FindNextApiVersionForConfig(const ResourceEntry* entry, const ConfigDescription& config); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 54 | |
Adam Lesinski | fb6312f | 2016-06-28 14:40:32 -0700 | [diff] [blame] | 55 | class AutoVersioner : public IResourceTableConsumer { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 56 | public: |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 57 | AutoVersioner() = default; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 58 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 59 | bool Consume(IAaptContext* context, ResourceTable* table) override; |
| 60 | |
| 61 | private: |
| 62 | DISALLOW_COPY_AND_ASSIGN(AutoVersioner); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 63 | }; |
| 64 | |
Alexandria Cornwall | 77788eb | 2016-09-06 15:16:49 -0700 | [diff] [blame] | 65 | /** |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 66 | * 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 Lesinski | 9f22204 | 2015-11-04 13:51:45 -0800 | [diff] [blame] | 70 | * issues with new apps running on old platforms. |
| 71 | * |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 72 | * 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 Lesinski | 9f22204 | 2015-11-04 13:51:45 -0800 | [diff] [blame] | 80 | * IDs. |
| 81 | * |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 82 | * This means that on a newer Android platform, an ID previously assigned to a |
| 83 | * private attribute |
Adam Lesinski | 9f22204 | 2015-11-04 13:51:45 -0800 | [diff] [blame] | 84 | * may end up assigned to a public attribute. |
| 85 | * |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 86 | * 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 Lesinski | 9f22204 | 2015-11-04 13:51:45 -0800 | [diff] [blame] | 92 | * unexpected types. |
| 93 | * |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 94 | * By moving the private attributes to a completely different type, this ID |
| 95 | * conflict will never |
Adam Lesinski | 9f22204 | 2015-11-04 13:51:45 -0800 | [diff] [blame] | 96 | * occur. |
| 97 | */ |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 98 | class 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 | |
| 108 | class ResourceConfigValue; |
| 109 | |
| 110 | class ProductFilter : public IResourceTableConsumer { |
| 111 | public: |
Adam Lesinski | c744ae8 | 2017-05-17 19:28:38 -0700 | [diff] [blame] | 112 | using ResourceConfigValueIter = std::vector<std::unique_ptr<ResourceConfigValue>>::iterator; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 113 | |
Adam Lesinski | c744ae8 | 2017-05-17 19:28:38 -0700 | [diff] [blame] | 114 | explicit ProductFilter(std::unordered_set<std::string> products) : products_(products) { |
| 115 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 116 | |
| 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 Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 124 | DISALLOW_COPY_AND_ASSIGN(ProductFilter); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 125 | |
Adam Lesinski | c744ae8 | 2017-05-17 19:28:38 -0700 | [diff] [blame] | 126 | std::unordered_set<std::string> products_; |
Adam Lesinski | 9f22204 | 2015-11-04 13:51:45 -0800 | [diff] [blame] | 127 | }; |
| 128 | |
| 129 | /** |
Alexandria Cornwall | a7cc3f1 | 2016-08-16 13:33:32 -0700 | [diff] [blame] | 130 | * Removes namespace nodes and URI information from the XmlResource. |
| 131 | * |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 132 | * 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 Cornwall | a7cc3f1 | 2016-08-16 13:33:32 -0700 | [diff] [blame] | 136 | */ |
| 137 | class XmlNamespaceRemover : public IXmlResourceConsumer { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 138 | public: |
Adam Lesinski | c744ae8 | 2017-05-17 19:28:38 -0700 | [diff] [blame] | 139 | explicit XmlNamespaceRemover(bool keep_uris = false) : keep_uris_(keep_uris){}; |
Alexandria Cornwall | a7cc3f1 | 2016-08-16 13:33:32 -0700 | [diff] [blame] | 140 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 141 | bool Consume(IAaptContext* context, xml::XmlResource* resource) override; |
| 142 | |
| 143 | private: |
| 144 | DISALLOW_COPY_AND_ASSIGN(XmlNamespaceRemover); |
| 145 | |
| 146 | bool keep_uris_; |
Alexandria Cornwall | a7cc3f1 | 2016-08-16 13:33:32 -0700 | [diff] [blame] | 147 | }; |
| 148 | |
| 149 | /** |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 150 | * Resolves attributes in the XmlResource and compiles string values to resource |
| 151 | * values. |
Adam Lesinski | 9f22204 | 2015-11-04 13:51:45 -0800 | [diff] [blame] | 152 | * Once an XmlResource is processed by this linker, it is ready to be flattened. |
| 153 | */ |
| 154 | class XmlReferenceLinker : public IXmlResourceConsumer { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 155 | public: |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 156 | XmlReferenceLinker() = default; |
| 157 | |
| 158 | bool Consume(IAaptContext* context, xml::XmlResource* resource) override; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 159 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 160 | private: |
| 161 | DISALLOW_COPY_AND_ASSIGN(XmlReferenceLinker); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 162 | }; |
| 163 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 164 | } // namespace aapt |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 165 | |
| 166 | #endif /* AAPT_LINKER_LINKERS_H */ |