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 | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 20 | #include "Resource.h" |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 21 | #include "process/IResourceTableConsumer.h" |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 22 | #include "xml/XmlDom.h" |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 23 | |
| 24 | #include <set> |
| 25 | |
| 26 | namespace aapt { |
| 27 | |
| 28 | class ResourceTable; |
Adam Lesinski | e4bb9eb | 2016-02-12 22:18:51 -0800 | [diff] [blame] | 29 | class ResourceEntry; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 30 | struct ConfigDescription; |
| 31 | |
| 32 | /** |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 33 | * Defines the location in which a value exists. This determines visibility of |
| 34 | * other |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 35 | * package's private symbols. |
| 36 | */ |
| 37 | struct CallSite { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 38 | ResourceNameRef resource; |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 39 | }; |
| 40 | |
| 41 | /** |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 42 | * Determines whether a versioned resource should be created. If a versioned |
| 43 | * resource already |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 44 | * exists, it takes precedence. |
| 45 | */ |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 46 | bool shouldGenerateVersionedResource(const ResourceEntry* entry, |
| 47 | const ConfigDescription& config, |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 48 | const int sdkVersionToGenerate); |
| 49 | |
Adam Lesinski | fb6312f | 2016-06-28 14:40:32 -0700 | [diff] [blame] | 50 | class AutoVersioner : public IResourceTableConsumer { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 51 | public: |
| 52 | bool consume(IAaptContext* context, ResourceTable* table) override; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 53 | }; |
| 54 | |
Adam Lesinski | fb6312f | 2016-06-28 14:40:32 -0700 | [diff] [blame] | 55 | class XmlAutoVersioner : public IXmlResourceConsumer { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 56 | public: |
| 57 | bool consume(IAaptContext* context, xml::XmlResource* resource) override; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 58 | }; |
| 59 | |
Adam Lesinski | fb6312f | 2016-06-28 14:40:32 -0700 | [diff] [blame] | 60 | class VersionCollapser : public IResourceTableConsumer { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 61 | public: |
| 62 | bool consume(IAaptContext* context, ResourceTable* table) override; |
Adam Lesinski | fb6312f | 2016-06-28 14:40:32 -0700 | [diff] [blame] | 63 | }; |
| 64 | |
Adam Lesinski | 9f22204 | 2015-11-04 13:51:45 -0800 | [diff] [blame] | 65 | /** |
Alexandria Cornwall | 77788eb | 2016-09-06 15:16:49 -0700 | [diff] [blame] | 66 | * Removes duplicated key-value entries from dominated resources. |
| 67 | */ |
| 68 | class ResourceDeduper : public IResourceTableConsumer { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 69 | public: |
| 70 | bool consume(IAaptContext* context, ResourceTable* table) override; |
Alexandria Cornwall | 77788eb | 2016-09-06 15:16:49 -0700 | [diff] [blame] | 71 | }; |
| 72 | |
| 73 | /** |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 74 | * If any attribute resource values are defined as public, this consumer will |
| 75 | * move all private |
| 76 | * attribute resource values to a private ^private-attr type, avoiding backwards |
| 77 | * compatibility |
Adam Lesinski | 9f22204 | 2015-11-04 13:51:45 -0800 | [diff] [blame] | 78 | * issues with new apps running on old platforms. |
| 79 | * |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 80 | * The Android platform ignores resource attributes it doesn't recognize, so an |
| 81 | * app developer can |
| 82 | * use new attributes in their layout XML files without worrying about |
| 83 | * versioning. This assumption |
| 84 | * actually breaks on older platforms. OEMs may add private attributes that are |
| 85 | * used internally. |
| 86 | * AAPT originally assigned all private attributes IDs immediately proceeding |
| 87 | * the public attributes' |
Adam Lesinski | 9f22204 | 2015-11-04 13:51:45 -0800 | [diff] [blame] | 88 | * IDs. |
| 89 | * |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 90 | * This means that on a newer Android platform, an ID previously assigned to a |
| 91 | * private attribute |
Adam Lesinski | 9f22204 | 2015-11-04 13:51:45 -0800 | [diff] [blame] | 92 | * may end up assigned to a public attribute. |
| 93 | * |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 94 | * App developers assume using the newer attribute is safe on older platforms |
| 95 | * because it will |
| 96 | * be ignored. Instead, the platform thinks the new attribute is an older, |
| 97 | * private attribute and |
| 98 | * will interpret it as such. This leads to unintended styling and exceptions |
| 99 | * thrown due to |
Adam Lesinski | 9f22204 | 2015-11-04 13:51:45 -0800 | [diff] [blame] | 100 | * unexpected types. |
| 101 | * |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 102 | * By moving the private attributes to a completely different type, this ID |
| 103 | * conflict will never |
Adam Lesinski | 9f22204 | 2015-11-04 13:51:45 -0800 | [diff] [blame] | 104 | * occur. |
| 105 | */ |
| 106 | struct PrivateAttributeMover : public IResourceTableConsumer { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 107 | bool consume(IAaptContext* context, ResourceTable* table) override; |
Adam Lesinski | 9f22204 | 2015-11-04 13:51:45 -0800 | [diff] [blame] | 108 | }; |
| 109 | |
| 110 | /** |
Alexandria Cornwall | a7cc3f1 | 2016-08-16 13:33:32 -0700 | [diff] [blame] | 111 | * Removes namespace nodes and URI information from the XmlResource. |
| 112 | * |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 113 | * Once an XmlResource is processed by this consumer, it is no longer able to |
| 114 | * have its attributes |
| 115 | * parsed. As such, this XmlResource must have already been processed by |
| 116 | * XmlReferenceLinker. |
Alexandria Cornwall | a7cc3f1 | 2016-08-16 13:33:32 -0700 | [diff] [blame] | 117 | */ |
| 118 | class XmlNamespaceRemover : public IXmlResourceConsumer { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 119 | private: |
| 120 | bool mKeepUris; |
Alexandria Cornwall | a7cc3f1 | 2016-08-16 13:33:32 -0700 | [diff] [blame] | 121 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 122 | public: |
| 123 | XmlNamespaceRemover(bool keepUris = false) : mKeepUris(keepUris){}; |
Alexandria Cornwall | a7cc3f1 | 2016-08-16 13:33:32 -0700 | [diff] [blame] | 124 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 125 | bool consume(IAaptContext* context, xml::XmlResource* resource) override; |
Alexandria Cornwall | a7cc3f1 | 2016-08-16 13:33:32 -0700 | [diff] [blame] | 126 | }; |
| 127 | |
| 128 | /** |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 129 | * Resolves attributes in the XmlResource and compiles string values to resource |
| 130 | * values. |
Adam Lesinski | 9f22204 | 2015-11-04 13:51:45 -0800 | [diff] [blame] | 131 | * Once an XmlResource is processed by this linker, it is ready to be flattened. |
| 132 | */ |
| 133 | class XmlReferenceLinker : public IXmlResourceConsumer { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 134 | private: |
| 135 | std::set<int> mSdkLevelsFound; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 136 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 137 | public: |
| 138 | bool consume(IAaptContext* context, xml::XmlResource* resource) override; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 139 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 140 | /** |
| 141 | * Once the XmlResource has been consumed, this returns the various SDK levels |
| 142 | * in which |
| 143 | * framework attributes used within the XML document were defined. |
| 144 | */ |
| 145 | inline const std::set<int>& getSdkLevels() const { return mSdkLevelsFound; } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 146 | }; |
| 147 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 148 | } // namespace aapt |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 149 | |
| 150 | #endif /* AAPT_LINKER_LINKERS_H */ |