1 /*
2  * Copyright (C) 2017 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 "link/XmlCompatVersioner.h"
18 
19 #include <algorithm>
20 
21 #include "util/Util.h"
22 
23 namespace aapt {
24 
CopyAttr(const xml::Attribute & src,android::StringPool * out_string_pool)25 static xml::Attribute CopyAttr(const xml::Attribute& src, android::StringPool* out_string_pool) {
26   CloningValueTransformer cloner(out_string_pool);
27   xml::Attribute dst{src.namespace_uri, src.name, src.value, src.compiled_attribute};
28   if (src.compiled_value != nullptr) {
29     dst.compiled_value = src.compiled_value->Transform(cloner);
30   }
31   return dst;
32 }
33 
34 // Returns false if the attribute is not copied because an existing attribute takes precedence
35 // (came from a rule).
CopyAttribute(const xml::Attribute & src_attr,bool generated,xml::Element * dst_el,android::StringPool * out_string_pool)36 static bool CopyAttribute(const xml::Attribute& src_attr, bool generated, xml::Element* dst_el,
37                           android::StringPool* out_string_pool) {
38   CloningValueTransformer cloner(out_string_pool);
39   xml::Attribute* dst_attr = dst_el->FindAttribute(src_attr.namespace_uri, src_attr.name);
40   if (dst_attr != nullptr) {
41     if (generated) {
42       // Generated attributes always take precedence.
43       dst_attr->value = src_attr.value;
44       dst_attr->compiled_attribute = src_attr.compiled_attribute;
45       if (src_attr.compiled_value != nullptr) {
46         dst_attr->compiled_value = src_attr.compiled_value->Transform(cloner);
47       }
48       return true;
49     }
50     return false;
51   }
52   dst_el->attributes.push_back(CopyAttr(src_attr, out_string_pool));
53   return true;
54 }
55 
ProcessRule(const xml::Element & src_el,const xml::Attribute & src_attr,const ApiVersion & src_attr_version,const IDegradeRule * rule,const util::Range<ApiVersion> & api_range,bool generated,xml::Element * dst_el,std::set<ApiVersion> * out_apis_referenced,android::StringPool * out_string_pool)56 void XmlCompatVersioner::ProcessRule(const xml::Element& src_el, const xml::Attribute& src_attr,
57                                      const ApiVersion& src_attr_version, const IDegradeRule* rule,
58                                      const util::Range<ApiVersion>& api_range, bool generated,
59                                      xml::Element* dst_el,
60                                      std::set<ApiVersion>* out_apis_referenced,
61                                      android::StringPool* out_string_pool) {
62   if (src_attr_version <= api_range.start) {
63     // The API is compatible, so don't check the rule and just copy.
64     if (!CopyAttribute(src_attr, generated, dst_el, out_string_pool)) {
65       // TODO(adamlesinski): Log a warning that an attribute was overridden?
66     }
67     return;
68   }
69 
70   if (api_range.start >= SDK_LOLLIPOP_MR1) {
71     // Since LOLLIPOP MR1, the framework can handle silently ignoring unknown public attributes,
72     // so we don't need to erase/version them.
73     // Copy.
74     if (!CopyAttribute(src_attr, generated, dst_el, out_string_pool)) {
75       // TODO(adamlesinski): Log a warning that an attribute was overridden?
76     }
77   } else {
78     // We are going to erase this attribute from this XML resource version, but check if
79     // we even need to move it anywhere. A developer may have effectively overwritten it with
80     // a similarly versioned XML resource.
81     if (src_attr_version < api_range.end) {
82       // There is room for another versioned XML resource between this XML resource and the next
83       // versioned XML resource defined by the developer.
84       out_apis_referenced->insert(std::min<ApiVersion>(src_attr_version, SDK_LOLLIPOP_MR1));
85     }
86   }
87 
88   if (rule != nullptr) {
89     for (const DegradeResult& result : rule->Degrade(src_el, src_attr, out_string_pool)) {
90       const ResourceId attr_resid = result.attr.compiled_attribute.value().id.value();
91       const ApiVersion attr_version = FindAttributeSdkLevel(attr_resid);
92 
93       auto iter = rules_->find(attr_resid);
94       ProcessRule(src_el, result.attr, attr_version,
95                   iter != rules_->end() ? iter->second.get() : nullptr, api_range,
96                   true /*generated*/, dst_el, out_apis_referenced, out_string_pool);
97     }
98   }
99 }
100 
XmlCompatVersioner(const Rules * rules)101 XmlCompatVersioner::XmlCompatVersioner(const Rules* rules) : rules_(rules) {
102 }
103 
ProcessDoc(ApiVersion target_api,ApiVersion max_api,xml::XmlResource * doc,std::set<ApiVersion> * out_apis_referenced)104 std::unique_ptr<xml::XmlResource> XmlCompatVersioner::ProcessDoc(
105     ApiVersion target_api, ApiVersion max_api, xml::XmlResource* doc,
106     std::set<ApiVersion>* out_apis_referenced) {
107   const util::Range<ApiVersion> api_range{target_api, max_api};
108 
109   std::unique_ptr<xml::XmlResource> cloned_doc = util::make_unique<xml::XmlResource>(doc->file);
110   cloned_doc->file.config.sdkVersion = static_cast<uint16_t>(target_api);
111 
112   cloned_doc->root = doc->root->CloneElement([&](const xml::Element& el, xml::Element* out_el) {
113     for (const auto& attr : el.attributes) {
114       if (!attr.compiled_attribute) {
115         // Just copy if this isn't a compiled attribute.
116         out_el->attributes.push_back(CopyAttr(attr, &cloned_doc->string_pool));
117         continue;
118       }
119 
120       const ResourceId attr_resid = attr.compiled_attribute.value().id.value();
121       const ApiVersion attr_version = FindAttributeSdkLevel(attr_resid);
122 
123       auto rule = rules_->find(attr_resid);
124       ProcessRule(el, attr, attr_version, rule != rules_->end() ? rule->second.get() : nullptr,
125                   api_range, false /*generated*/, out_el, out_apis_referenced,
126                   &cloned_doc->string_pool);
127     }
128   });
129   return cloned_doc;
130 }
131 
Process(IAaptContext * context,xml::XmlResource * doc,util::Range<ApiVersion> api_range)132 std::vector<std::unique_ptr<xml::XmlResource>> XmlCompatVersioner::Process(
133     IAaptContext* context, xml::XmlResource* doc, util::Range<ApiVersion> api_range) {
134   // Adjust the API range so that it falls after this document and after minSdkVersion.
135   api_range.start = std::max(api_range.start, context->GetMinSdkVersion());
136   api_range.start = std::max(api_range.start, static_cast<ApiVersion>(doc->file.config.sdkVersion));
137 
138   std::vector<std::unique_ptr<xml::XmlResource>> versioned_docs;
139   std::set<ApiVersion> apis_referenced;
140   versioned_docs.push_back(ProcessDoc(api_range.start, api_range.end, doc, &apis_referenced));
141 
142   // Adjust the sdkVersion of the first XML document back to its original (this only really
143   // makes a difference if the sdk version was below the minSdk to start).
144   versioned_docs.back()->file.config.sdkVersion = doc->file.config.sdkVersion;
145 
146   // Iterate from smallest to largest API version.
147   for (ApiVersion api : apis_referenced) {
148     std::set<ApiVersion> tmp;
149     versioned_docs.push_back(ProcessDoc(api, api_range.end, doc, &tmp));
150   }
151   return versioned_docs;
152 }
153 
DegradeToManyRule(std::vector<ReplacementAttr> attrs)154 DegradeToManyRule::DegradeToManyRule(std::vector<ReplacementAttr> attrs)
155     : attrs_(std::move(attrs)) {
156 }
157 
CloneIfNotNull(const std::unique_ptr<Item> & src,android::StringPool * out_string_pool)158 static inline std::unique_ptr<Item> CloneIfNotNull(const std::unique_ptr<Item>& src,
159                                                    android::StringPool* out_string_pool) {
160   if (src == nullptr) {
161     return {};
162   }
163   CloningValueTransformer cloner(out_string_pool);
164   return src->Transform(cloner);
165 }
166 
Degrade(const xml::Element & src_el,const xml::Attribute & src_attr,android::StringPool * out_string_pool) const167 std::vector<DegradeResult> DegradeToManyRule::Degrade(const xml::Element& src_el,
168                                                       const xml::Attribute& src_attr,
169                                                       android::StringPool* out_string_pool) const {
170   std::vector<DegradeResult> result;
171   result.reserve(attrs_.size());
172   for (const ReplacementAttr& attr : attrs_) {
173     result.push_back(
174         DegradeResult{xml::Attribute{xml::kSchemaAndroid, attr.name, src_attr.value,
175                                      xml::AaptAttribute{attr.attr, attr.id},
176                                      CloneIfNotNull(src_attr.compiled_value, out_string_pool)},
177                       FindAttributeSdkLevel(attr.id)});
178   }
179   return result;
180 }
181 
182 }  // namespace aapt
183