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