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 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 17 | #include "link/Linkers.h" |
| 18 | |
Adam Lesinski | 3866554 | 2016-12-28 12:25:46 -0500 | [diff] [blame] | 19 | #include "androidfw/ResourceTypes.h" |
| 20 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 21 | #include "Diagnostics.h" |
| 22 | #include "ResourceUtils.h" |
| 23 | #include "SdkConstants.h" |
Adam Lesinski | d3ffa844 | 2017-09-28 13:34:35 -0700 | [diff] [blame] | 24 | #include "ValueVisitor.h" |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 25 | #include "link/ReferenceLinker.h" |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 26 | #include "process/IResourceTableConsumer.h" |
| 27 | #include "process/SymbolTable.h" |
Fabien Sanglard | 2d34e76 | 2019-02-21 15:13:29 -0800 | [diff] [blame] | 28 | #include "trace/TraceBuffer.h" |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 29 | #include "util/Util.h" |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 30 | #include "xml/XmlDom.h" |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 31 | |
| 32 | namespace aapt { |
| 33 | |
| 34 | namespace { |
| 35 | |
Adam Lesinski | 1ef0fa9 | 2017-08-15 21:32:49 -0700 | [diff] [blame] | 36 | // Visits all references (including parents of styles, references in styles, arrays, etc) and |
| 37 | // links their symbolic name to their Resource ID, performing mangling and package aliasing |
| 38 | // as needed. |
Adam Lesinski | d3ffa844 | 2017-09-28 13:34:35 -0700 | [diff] [blame] | 39 | class ReferenceVisitor : public DescendingValueVisitor { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 40 | public: |
Adam Lesinski | d3ffa844 | 2017-09-28 13:34:35 -0700 | [diff] [blame] | 41 | using DescendingValueVisitor::Visit; |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 42 | |
Adam Lesinski | f34b6f4 | 2017-03-03 16:33:26 -0800 | [diff] [blame] | 43 | ReferenceVisitor(const CallSite& callsite, IAaptContext* context, SymbolTable* symbols, |
| 44 | xml::IPackageDeclStack* decls) |
| 45 | : callsite_(callsite), context_(context), symbols_(symbols), decls_(decls), error_(false) {} |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 46 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 47 | void Visit(Reference* ref) override { |
Adam Lesinski | f34b6f4 | 2017-03-03 16:33:26 -0800 | [diff] [blame] | 48 | if (!ReferenceLinker::LinkReference(callsite_, ref, context_, symbols_, decls_)) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 49 | error_ = true; |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 50 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 51 | } |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 52 | |
Adam Lesinski | 1ef0fa9 | 2017-08-15 21:32:49 -0700 | [diff] [blame] | 53 | bool HasError() const { |
| 54 | return error_; |
| 55 | } |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 56 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 57 | private: |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 58 | DISALLOW_COPY_AND_ASSIGN(ReferenceVisitor); |
| 59 | |
Adam Lesinski | f34b6f4 | 2017-03-03 16:33:26 -0800 | [diff] [blame] | 60 | const CallSite& callsite_; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 61 | IAaptContext* context_; |
| 62 | SymbolTable* symbols_; |
| 63 | xml::IPackageDeclStack* decls_; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 64 | bool error_; |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 65 | }; |
| 66 | |
Adam Lesinski | 1ef0fa9 | 2017-08-15 21:32:49 -0700 | [diff] [blame] | 67 | // Visits each xml Element and compiles the attributes within. |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 68 | class XmlVisitor : public xml::PackageAwareVisitor { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 69 | public: |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 70 | using xml::PackageAwareVisitor::Visit; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 71 | |
Adam Lesinski | f34b6f4 | 2017-03-03 16:33:26 -0800 | [diff] [blame] | 72 | XmlVisitor(const Source& source, const CallSite& callsite, IAaptContext* context, |
Adam Lesinski | c744ae8 | 2017-05-17 19:28:38 -0700 | [diff] [blame] | 73 | SymbolTable* symbols) |
Adam Lesinski | f34b6f4 | 2017-03-03 16:33:26 -0800 | [diff] [blame] | 74 | : source_(source), |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 75 | callsite_(callsite), |
Adam Lesinski | f34b6f4 | 2017-03-03 16:33:26 -0800 | [diff] [blame] | 76 | context_(context), |
| 77 | symbols_(symbols), |
Adam Lesinski | c744ae8 | 2017-05-17 19:28:38 -0700 | [diff] [blame] | 78 | reference_visitor_(callsite, context, symbols, this) { |
| 79 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 80 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 81 | void Visit(xml::Element* el) override { |
Adam Lesinski | 3866554 | 2016-12-28 12:25:46 -0500 | [diff] [blame] | 82 | // The default Attribute allows everything except enums or flags. |
Adam Lesinski | 73bff1e | 2017-12-08 16:06:10 -0800 | [diff] [blame] | 83 | Attribute default_attribute(android::ResTable_map::TYPE_ANY); |
| 84 | default_attribute.SetWeak(true); |
Adam Lesinski | 3866554 | 2016-12-28 12:25:46 -0500 | [diff] [blame] | 85 | |
Ryan Mitchell | 4d5833e | 2019-09-04 02:46:03 -0700 | [diff] [blame] | 86 | // The default orientation of gradients in android Q is different than previous android |
| 87 | // versions. Set the android:angle attribute to "0" to ensure that the default gradient |
| 88 | // orientation will remain left-to-right in android Q. |
| 89 | if (el->name == "gradient" && context_->GetMinSdkVersion() <= SDK_Q) { |
| 90 | if (!el->FindAttribute(xml::kSchemaAndroid, "angle")) { |
| 91 | el->attributes.push_back(xml::Attribute{xml::kSchemaAndroid, "angle", "0"}); |
| 92 | } |
| 93 | } |
| 94 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 95 | const Source source = source_.WithLine(el->line_number); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 96 | for (xml::Attribute& attr : el->attributes) { |
Adam Lesinski | 3866554 | 2016-12-28 12:25:46 -0500 | [diff] [blame] | 97 | // If the attribute has no namespace, interpret values as if |
| 98 | // they were assigned to the default Attribute. |
| 99 | |
Adam Lesinski | 73bff1e | 2017-12-08 16:06:10 -0800 | [diff] [blame] | 100 | const Attribute* attribute = &default_attribute; |
Adam Lesinski | 3866554 | 2016-12-28 12:25:46 -0500 | [diff] [blame] | 101 | |
| 102 | if (Maybe<xml::ExtractedPackage> maybe_package = |
| 103 | xml::ExtractPackageFromNamespace(attr.namespace_uri)) { |
| 104 | // There is a valid package name for this attribute. We will look this up. |
Adam Lesinski | 1ef0fa9 | 2017-08-15 21:32:49 -0700 | [diff] [blame] | 105 | Reference attr_ref( |
| 106 | ResourceNameRef(maybe_package.value().package, ResourceType::kAttr, attr.name)); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 107 | attr_ref.private_reference = maybe_package.value().private_namespace; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 108 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 109 | std::string err_str; |
Chris Warrington | 58e2fbf | 2018-07-23 14:12:20 +0000 | [diff] [blame] | 110 | attr.compiled_attribute = |
Udam Saini | b228df3 | 2019-06-18 16:50:34 -0700 | [diff] [blame] | 111 | ReferenceLinker::CompileXmlAttribute(attr_ref, callsite_, context_, symbols_, &err_str); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 112 | |
Adam Lesinski | 3866554 | 2016-12-28 12:25:46 -0500 | [diff] [blame] | 113 | if (!attr.compiled_attribute) { |
Adam Lesinski | 1ef0fa9 | 2017-08-15 21:32:49 -0700 | [diff] [blame] | 114 | DiagMessage error_msg(source); |
| 115 | error_msg << "attribute "; |
| 116 | ReferenceLinker::WriteAttributeName(attr_ref, callsite_, this, &error_msg); |
| 117 | error_msg << " " << err_str; |
| 118 | context_->GetDiagnostics()->Error(error_msg); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 119 | error_ = true; |
Adam Lesinski | 3866554 | 2016-12-28 12:25:46 -0500 | [diff] [blame] | 120 | continue; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 121 | } |
Adam Lesinski | 3866554 | 2016-12-28 12:25:46 -0500 | [diff] [blame] | 122 | |
Adam Lesinski | c744ae8 | 2017-05-17 19:28:38 -0700 | [diff] [blame] | 123 | attribute = &attr.compiled_attribute.value().attribute; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 124 | } |
| 125 | |
Adam Lesinski | 3866554 | 2016-12-28 12:25:46 -0500 | [diff] [blame] | 126 | attr.compiled_value = ResourceUtils::TryParseItemForAttribute(attr.value, attribute); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 127 | if (attr.compiled_value) { |
Adam Lesinski | c744ae8 | 2017-05-17 19:28:38 -0700 | [diff] [blame] | 128 | // With a compiledValue, we must resolve the reference and assign it an ID. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 129 | attr.compiled_value->SetSource(source); |
| 130 | attr.compiled_value->Accept(&reference_visitor_); |
Adam Lesinski | 3866554 | 2016-12-28 12:25:46 -0500 | [diff] [blame] | 131 | } else if ((attribute->type_mask & android::ResTable_map::TYPE_STRING) == 0) { |
| 132 | // We won't be able to encode this as a string. |
| 133 | DiagMessage msg(source); |
Adam Lesinski | 1ef0fa9 | 2017-08-15 21:32:49 -0700 | [diff] [blame] | 134 | msg << "'" << attr.value << "' is incompatible with attribute " << attr.name << " " |
| 135 | << *attribute; |
Adam Lesinski | 3866554 | 2016-12-28 12:25:46 -0500 | [diff] [blame] | 136 | context_->GetDiagnostics()->Error(msg); |
| 137 | error_ = true; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 138 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 139 | } |
| 140 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 141 | // Call the super implementation. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 142 | xml::PackageAwareVisitor::Visit(el); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 143 | } |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 144 | |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 145 | bool HasError() { |
| 146 | return error_ || reference_visitor_.HasError(); |
| 147 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 148 | |
| 149 | private: |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 150 | DISALLOW_COPY_AND_ASSIGN(XmlVisitor); |
| 151 | |
Adam Lesinski | f34b6f4 | 2017-03-03 16:33:26 -0800 | [diff] [blame] | 152 | Source source_; |
| 153 | const CallSite& callsite_; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 154 | IAaptContext* context_; |
| 155 | SymbolTable* symbols_; |
Adam Lesinski | f34b6f4 | 2017-03-03 16:33:26 -0800 | [diff] [blame] | 156 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 157 | ReferenceVisitor reference_visitor_; |
| 158 | bool error_ = false; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 159 | }; |
| 160 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 161 | } // namespace |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 162 | |
Adam Lesinski | 3866554 | 2016-12-28 12:25:46 -0500 | [diff] [blame] | 163 | bool XmlReferenceLinker::Consume(IAaptContext* context, xml::XmlResource* resource) { |
Fabien Sanglard | 2d34e76 | 2019-02-21 15:13:29 -0800 | [diff] [blame] | 164 | TRACE_NAME("XmlReferenceLinker::Consume"); |
Adam Lesinski | 1ef0fa9 | 2017-08-15 21:32:49 -0700 | [diff] [blame] | 165 | CallSite callsite{resource->file.name.package}; |
| 166 | |
| 167 | std::string out_name = resource->file.name.entry; |
| 168 | NameMangler::Unmangle(&out_name, &callsite.package); |
| 169 | |
| 170 | if (callsite.package.empty()) { |
| 171 | // Assume an empty package means that the XML file is local. This is true of AndroidManifest.xml |
| 172 | // for example. |
| 173 | callsite.package = context->GetCompilationPackage(); |
| 174 | } |
| 175 | |
Adam Lesinski | c744ae8 | 2017-05-17 19:28:38 -0700 | [diff] [blame] | 176 | XmlVisitor visitor(resource->file.source, callsite, context, context->GetExternalSymbols()); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 177 | if (resource->root) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 178 | resource->root->Accept(&visitor); |
| 179 | return !visitor.HasError(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 180 | } |
| 181 | return false; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 182 | } |
| 183 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 184 | } // namespace aapt |