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