OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "extensions/common/features/base_feature_provider.h" | 5 #include "extensions/common/features/base_feature_provider.h" |
6 | 6 |
7 #include <stack> | 7 #include <stack> |
8 #include <utility> | 8 #include <utility> |
9 | 9 |
10 #include "base/strings/string_split.h" | 10 #include "base/strings/string_split.h" |
11 #include "base/strings/string_util.h" | 11 #include "base/strings/string_util.h" |
12 #include "extensions/common/extensions_client.h" | 12 #include "extensions/common/extensions_client.h" |
13 #include "extensions/common/features/complex_feature.h" | 13 #include "extensions/common/features/complex_feature.h" |
14 #include "extensions/common/features/simple_feature.h" | 14 #include "extensions/common/features/simple_feature.h" |
15 | 15 |
16 namespace extensions { | 16 namespace extensions { |
17 | 17 |
18 namespace { | 18 namespace { |
19 | 19 |
| 20 bool IsNocompile(const base::Value& value) { |
| 21 bool nocompile = false; |
| 22 const base::DictionaryValue* as_dict = nullptr; |
| 23 if (value.GetAsDictionary(&as_dict)) { |
| 24 as_dict->GetBoolean("nocompile", &nocompile); |
| 25 } else { |
| 26 // "nocompile" is not supported for any other feature type. |
| 27 } |
| 28 return nocompile; |
| 29 } |
| 30 |
20 bool ParseFeature(const base::DictionaryValue* value, | 31 bool ParseFeature(const base::DictionaryValue* value, |
21 const std::string& name, | 32 const std::string& name, |
22 SimpleFeature* feature) { | 33 SimpleFeature* feature) { |
23 feature->set_name(name); | 34 feature->set_name(name); |
24 std::string error = feature->Parse(value); | 35 std::string error = feature->Parse(value); |
25 if (!error.empty()) | 36 if (!error.empty()) |
26 LOG(ERROR) << error; | 37 LOG(ERROR) << error; |
27 return error.empty(); | 38 return error.empty(); |
28 } | 39 } |
29 | 40 |
30 } // namespace | 41 } // namespace |
31 | 42 |
32 BaseFeatureProvider::BaseFeatureProvider(const base::DictionaryValue& root, | 43 BaseFeatureProvider::BaseFeatureProvider(const base::DictionaryValue& root, |
33 FeatureFactory factory) | 44 FeatureFactory factory) |
34 : factory_(factory) { | 45 : factory_(factory) { |
35 for (base::DictionaryValue::Iterator iter(root); !iter.IsAtEnd(); | 46 for (base::DictionaryValue::Iterator iter(root); !iter.IsAtEnd(); |
36 iter.Advance()) { | 47 iter.Advance()) { |
| 48 if (IsNocompile(iter.value())) { |
| 49 continue; |
| 50 } |
| 51 |
37 if (iter.value().GetType() == base::Value::TYPE_DICTIONARY) { | 52 if (iter.value().GetType() == base::Value::TYPE_DICTIONARY) { |
38 linked_ptr<SimpleFeature> feature((*factory_)()); | 53 linked_ptr<SimpleFeature> feature((*factory_)()); |
39 | 54 |
40 std::vector<std::string> split; | 55 std::vector<std::string> split; |
41 base::SplitString(iter.key(), '.', &split); | 56 base::SplitString(iter.key(), '.', &split); |
42 | 57 |
43 // Push parent features on the stack, starting with the current feature. | 58 // Push parent features on the stack, starting with the current feature. |
44 // If one of the features has "noparent" set, stop pushing features on | 59 // If one of the features has "noparent" set, stop pushing features on |
45 // the stack. The features will then be parsed in order, starting with | 60 // the stack. The features will then be parsed in order, starting with |
46 // the farthest parent that is either top level or has "noparent" set. | 61 // the farthest parent that is either top level or has "noparent" set. |
47 std::stack<std::pair<std::string, const base::DictionaryValue*> > | 62 std::stack<std::pair<std::string, const base::DictionaryValue*> > |
48 parse_stack; | 63 parse_stack; |
49 while (!split.empty()) { | 64 while (!split.empty()) { |
50 std::string parent_name = JoinString(split, '.'); | 65 std::string parent_name = JoinString(split, '.'); |
51 split.pop_back(); | 66 split.pop_back(); |
52 if (root.HasKey(parent_name)) { | 67 if (root.HasKey(parent_name)) { |
53 const base::DictionaryValue* parent = NULL; | 68 const base::DictionaryValue* parent = nullptr; |
54 CHECK(root.GetDictionaryWithoutPathExpansion(parent_name, &parent)); | 69 CHECK(root.GetDictionaryWithoutPathExpansion(parent_name, &parent)); |
55 parse_stack.push(std::make_pair(parent_name, parent)); | 70 parse_stack.push(std::make_pair(parent_name, parent)); |
56 bool no_parent = false; | 71 bool no_parent = false; |
57 parent->GetBoolean("noparent", &no_parent); | 72 parent->GetBoolean("noparent", &no_parent); |
58 if (no_parent) | 73 if (no_parent) |
59 break; | 74 break; |
60 } | 75 } |
61 } | 76 } |
62 | 77 |
63 CHECK(!parse_stack.empty()); | 78 CHECK(!parse_stack.empty()); |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
128 // now. | 143 // now. |
129 } | 144 } |
130 return feature_names_; | 145 return feature_names_; |
131 } | 146 } |
132 | 147 |
133 Feature* BaseFeatureProvider::GetFeature(const std::string& name) const { | 148 Feature* BaseFeatureProvider::GetFeature(const std::string& name) const { |
134 FeatureMap::const_iterator iter = features_.find(name); | 149 FeatureMap::const_iterator iter = features_.find(name); |
135 if (iter != features_.end()) | 150 if (iter != features_.end()) |
136 return iter->second.get(); | 151 return iter->second.get(); |
137 else | 152 else |
138 return NULL; | 153 return nullptr; |
139 } | 154 } |
140 | 155 |
141 Feature* BaseFeatureProvider::GetParent(Feature* feature) const { | 156 Feature* BaseFeatureProvider::GetParent(Feature* feature) const { |
142 CHECK(feature); | 157 CHECK(feature); |
143 if (feature->no_parent()) | 158 if (feature->no_parent()) |
144 return NULL; | 159 return nullptr; |
145 | 160 |
146 std::vector<std::string> split; | 161 std::vector<std::string> split; |
147 base::SplitString(feature->name(), '.', &split); | 162 base::SplitString(feature->name(), '.', &split); |
148 if (split.size() < 2) | 163 if (split.size() < 2) |
149 return NULL; | 164 return nullptr; |
150 split.pop_back(); | 165 split.pop_back(); |
151 return GetFeature(JoinString(split, '.')); | 166 return GetFeature(JoinString(split, '.')); |
152 } | 167 } |
153 | 168 |
154 // Children of a given API are named starting with parent.name()+".", which | 169 // Children of a given API are named starting with parent.name()+".", which |
155 // means they'll be contiguous in the features_ std::map. | 170 // means they'll be contiguous in the features_ std::map. |
156 std::vector<Feature*> BaseFeatureProvider::GetChildren(const Feature& parent) | 171 std::vector<Feature*> BaseFeatureProvider::GetChildren(const Feature& parent) |
157 const { | 172 const { |
158 std::string prefix = parent.name() + "."; | 173 std::string prefix = parent.name() + "."; |
159 const FeatureMap::const_iterator first_child = features_.lower_bound(prefix); | 174 const FeatureMap::const_iterator first_child = features_.lower_bound(prefix); |
160 | 175 |
161 // All children have names before (parent.name() + ('.'+1)). | 176 // All children have names before (parent.name() + ('.'+1)). |
162 ++prefix[prefix.size() - 1]; | 177 ++prefix[prefix.size() - 1]; |
163 const FeatureMap::const_iterator after_children = | 178 const FeatureMap::const_iterator after_children = |
164 features_.lower_bound(prefix); | 179 features_.lower_bound(prefix); |
165 | 180 |
166 std::vector<Feature*> result; | 181 std::vector<Feature*> result; |
167 result.reserve(std::distance(first_child, after_children)); | 182 result.reserve(std::distance(first_child, after_children)); |
168 for (FeatureMap::const_iterator it = first_child; it != after_children; | 183 for (FeatureMap::const_iterator it = first_child; it != after_children; |
169 ++it) { | 184 ++it) { |
170 result.push_back(it->second.get()); | 185 result.push_back(it->second.get()); |
171 } | 186 } |
172 return result; | 187 return result; |
173 } | 188 } |
174 | 189 |
175 } // namespace extensions | 190 } // namespace extensions |
OLD | NEW |