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 #include "ResourceUtils.h"
18 #include "SdkConstants.h"
19 #include "ValueVisitor.h"
20 #include "androidfw/IDiagnostics.h"
21 #include "androidfw/ResourceTypes.h"
22 #include "link/Linkers.h"
23 #include "link/ReferenceLinker.h"
24 #include "process/IResourceTableConsumer.h"
25 #include "process/SymbolTable.h"
26 #include "trace/TraceBuffer.h"
27 #include "util/Util.h"
28 #include "xml/XmlDom.h"
29 
30 namespace aapt {
31 
32 namespace {
33 
34 // Visits each xml Element and compiles the attributes within.
35 class XmlVisitor : public xml::PackageAwareVisitor {
36  public:
37   using xml::PackageAwareVisitor::Visit;
38 
XmlVisitor(const android::Source & source,android::StringPool * pool,const CallSite & callsite,IAaptContext * context,ResourceTable * table,SymbolTable * symbols)39   XmlVisitor(const android::Source& source, android::StringPool* pool, const CallSite& callsite,
40              IAaptContext* context, ResourceTable* table, SymbolTable* symbols)
41       : source_(source),
42         callsite_(callsite),
43         context_(context),
44         symbols_(symbols),
45         reference_transformer_(callsite, context, symbols, pool, table, this) {
46   }
47 
Visit(xml::Element * el)48   void Visit(xml::Element* el) override {
49     // The default Attribute allows everything except enums or flags.
50     Attribute default_attribute(android::ResTable_map::TYPE_ANY);
51     default_attribute.SetWeak(true);
52 
53     // The default orientation of gradients in android Q is different than previous android
54     // versions. Set the android:angle attribute to "0" to ensure that the default gradient
55     // orientation will remain left-to-right in android Q.
56     if (el->name == "gradient" && context_->GetMinSdkVersion() <= SDK_Q) {
57       if (!el->FindAttribute(xml::kSchemaAndroid, "angle")) {
58         el->attributes.push_back(xml::Attribute{xml::kSchemaAndroid, "angle", "0"});
59       }
60     }
61 
62     const android::Source source = source_.WithLine(el->line_number);
63     for (xml::Attribute& attr : el->attributes) {
64       // If the attribute has no namespace, interpret values as if
65       // they were assigned to the default Attribute.
66 
67       const Attribute* attribute = &default_attribute;
68 
69       if (std::optional<xml::ExtractedPackage> maybe_package =
70               xml::ExtractPackageFromNamespace(attr.namespace_uri)) {
71         // There is a valid package name for this attribute. We will look this up.
72         Reference attr_ref(
73             ResourceNameRef(maybe_package.value().package, ResourceType::kAttr, attr.name));
74         attr_ref.private_reference = maybe_package.value().private_namespace;
75 
76         std::string err_str;
77         attr.compiled_attribute =
78             ReferenceLinker::CompileXmlAttribute(attr_ref, callsite_, context_, symbols_, &err_str);
79 
80         if (!attr.compiled_attribute) {
81           android::DiagMessage error_msg(source);
82           error_msg << "attribute ";
83           ReferenceLinker::WriteAttributeName(attr_ref, callsite_, this, &error_msg);
84           error_msg << " " << err_str;
85           context_->GetDiagnostics()->Error(error_msg);
86           error_ = true;
87           continue;
88         }
89 
90         attribute = &attr.compiled_attribute.value().attribute;
91       }
92 
93       attr.compiled_value = ResourceUtils::TryParseItemForAttribute(attr.value, attribute);
94       if (attr.compiled_value) {
95         // With a compiledValue, we must resolve the reference and assign it an ID.
96         attr.compiled_value->SetSource(source);
97         attr.compiled_value = attr.compiled_value->Transform(reference_transformer_);
98       } else if ((attribute->type_mask & android::ResTable_map::TYPE_STRING) == 0) {
99         // We won't be able to encode this as a string.
100         android::DiagMessage msg(source);
101         msg << "'" << attr.value << "' is incompatible with attribute " << attr.name << " "
102             << *attribute;
103         context_->GetDiagnostics()->Error(msg);
104         error_ = true;
105       }
106     }
107 
108     // Call the super implementation.
109     xml::PackageAwareVisitor::Visit(el);
110   }
111 
HasError()112   bool HasError() {
113     return error_ || reference_transformer_.HasError();
114   }
115 
116  private:
117   DISALLOW_COPY_AND_ASSIGN(XmlVisitor);
118 
119   android::Source source_;
120   const CallSite& callsite_;
121   IAaptContext* context_;
122   SymbolTable* symbols_;
123 
124   ReferenceLinkerTransformer reference_transformer_;
125   bool error_ = false;
126 };
127 
128 }  // namespace
129 
Consume(IAaptContext * context,xml::XmlResource * resource)130 bool XmlReferenceLinker::Consume(IAaptContext* context, xml::XmlResource* resource) {
131   TRACE_NAME("XmlReferenceLinker::Consume");
132   CallSite callsite{resource->file.name.package};
133 
134   std::string out_name = resource->file.name.entry;
135   NameMangler::Unmangle(&out_name, &callsite.package);
136 
137   if (callsite.package.empty()) {
138     // Assume an empty package means that the XML file is local. This is true of AndroidManifest.xml
139     // for example.
140     callsite.package = context->GetCompilationPackage();
141   }
142 
143   XmlVisitor visitor(resource->file.source, &resource->string_pool, callsite, context, table_,
144                      context->GetExternalSymbols());
145   if (resource->root) {
146     resource->root->Accept(&visitor);
147     return !visitor.HasError();
148   }
149   return false;
150 }
151 
152 }  // namespace aapt
153