1 /*
2  * Copyright (C) 2016 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/Linkers.h"
18 
19 #include "ResourceTable.h"
20 #include "trace/TraceBuffer.h"
21 
22 namespace aapt {
23 
SelectProductToKeep(const ResourceNameRef & name,const ResourceConfigValueIter begin,const ResourceConfigValueIter end,android::IDiagnostics * diag)24 ProductFilter::ResourceConfigValueIter ProductFilter::SelectProductToKeep(
25     const ResourceNameRef& name, const ResourceConfigValueIter begin,
26     const ResourceConfigValueIter end, android::IDiagnostics* diag) {
27   ResourceConfigValueIter default_product_iter = end;
28   ResourceConfigValueIter selected_product_iter = end;
29 
30   for (ResourceConfigValueIter iter = begin; iter != end; ++iter) {
31     ResourceConfigValue* config_value = iter->get();
32     if (products_.find(config_value->product) != products_.end()) {
33       if (selected_product_iter != end) {
34         // We have two possible values for this product!
35         diag->Error(android::DiagMessage(config_value->value->GetSource())
36                     << "selection of product '" << config_value->product << "' for resource "
37                     << name << " is ambiguous");
38 
39         ResourceConfigValue* previously_selected_config_value =
40             selected_product_iter->get();
41         diag->Note(android::DiagMessage(previously_selected_config_value->value->GetSource())
42                    << "product '" << previously_selected_config_value->product
43                    << "' is also a candidate");
44         return end;
45       }
46 
47       // Select this product.
48       selected_product_iter = iter;
49     }
50 
51     if (config_value->product.empty() || config_value->product == "default") {
52       if (default_product_iter != end) {
53         // We have two possible default values.
54         diag->Error(android::DiagMessage(config_value->value->GetSource())
55                     << "multiple default products defined for resource " << name);
56 
57         ResourceConfigValue* previously_default_config_value =
58             default_product_iter->get();
59         diag->Note(android::DiagMessage(previously_default_config_value->value->GetSource())
60                    << "default product also defined here");
61         return end;
62       }
63 
64       // Mark the default.
65       default_product_iter = iter;
66     }
67   }
68 
69   if (default_product_iter == end) {
70     diag->Error(android::DiagMessage() << "no default product defined for resource " << name);
71     return end;
72   }
73 
74   if (selected_product_iter == end) {
75     selected_product_iter = default_product_iter;
76   }
77   return selected_product_iter;
78 }
79 
Consume(IAaptContext * context,ResourceTable * table)80 bool ProductFilter::Consume(IAaptContext* context, ResourceTable* table) {
81   TRACE_NAME("ProductFilter::Consume");
82   bool error = false;
83   for (auto& pkg : table->packages) {
84     for (auto& type : pkg->types) {
85       for (auto& entry : type->entries) {
86         std::vector<std::unique_ptr<ResourceConfigValue>> new_values;
87 
88         ResourceConfigValueIter iter = entry->values.begin();
89         ResourceConfigValueIter start_range_iter = iter;
90         while (iter != entry->values.end()) {
91           ++iter;
92           if (iter == entry->values.end() ||
93               (*iter)->config != (*start_range_iter)->config) {
94             // End of the array, or we saw a different config,
95             // so this must be the end of a range of products.
96             // Select the product to keep from the set of products defined.
97             ResourceNameRef name(pkg->name, type->named_type, entry->name);
98             auto value_to_keep = SelectProductToKeep(
99                 name, start_range_iter, iter, context->GetDiagnostics());
100             if (value_to_keep == iter) {
101               // An error occurred, we could not pick a product.
102               error = true;
103             } else {
104               // We selected a product to keep. Move it to the new array.
105               new_values.push_back(std::move(*value_to_keep));
106             }
107 
108             // Start the next range of products.
109             start_range_iter = iter;
110           }
111         }
112 
113         // Now move the new values in to place.
114         entry->values = std::move(new_values);
115       }
116     }
117   }
118   return !error;
119 }
120 
121 }  // namespace aapt
122