Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / common / extensions / features / base_feature_provider.cc
blob44f35f5832aa731ac87c9b33ffdbc7f726fe9642
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/common/extensions/features/base_feature_provider.h"
7 #include <stack>
9 #include "base/json/json_reader.h"
10 #include "base/lazy_instance.h"
11 #include "base/strings/string_split.h"
12 #include "base/strings/string_util.h"
13 #include "chrome/common/extensions/features/api_feature.h"
14 #include "chrome/common/extensions/features/complex_feature.h"
15 #include "chrome/common/extensions/features/manifest_feature.h"
16 #include "chrome/common/extensions/features/permission_feature.h"
17 #include "grit/common_resources.h"
18 #include "ui/base/resource/resource_bundle.h"
20 namespace extensions {
22 namespace {
24 template<class FeatureClass>
25 SimpleFeature* CreateFeature() {
26 return new FeatureClass();
29 static BaseFeatureProvider* LoadProvider(
30 const std::string& name,
31 BaseFeatureProvider::FeatureFactory factory,
32 int resource_id) {
33 const std::string& features_file =
34 ResourceBundle::GetSharedInstance().GetRawDataResource(
35 resource_id).as_string();
36 int error_code = 0;
37 std::string error_message;
38 scoped_ptr<base::Value> value(base::JSONReader::ReadAndReturnError(
39 features_file, base::JSON_PARSE_RFC,
40 &error_code, &error_message));
41 DCHECK(value) << "Could not load features: " << name << " "
42 << error_message;
43 scoped_ptr<base::DictionaryValue> value_as_dict;
44 if (value) {
45 CHECK(value->IsType(base::Value::TYPE_DICTIONARY)) << name;
46 value_as_dict.reset(static_cast<base::DictionaryValue*>(value.release()));
47 } else {
48 // http://crbug.com/176381
49 value_as_dict.reset(new base::DictionaryValue());
51 return new BaseFeatureProvider(*value_as_dict, factory);
54 struct Static {
55 Static() {
56 feature_providers["api"] = make_linked_ptr(
57 LoadProvider("api",
58 &CreateFeature<APIFeature>,
59 IDR_EXTENSION_API_FEATURES));
60 feature_providers["permission"] = make_linked_ptr(
61 LoadProvider("permission",
62 &CreateFeature<PermissionFeature>,
63 IDR_EXTENSION_PERMISSION_FEATURES));
64 feature_providers["manifest"] = make_linked_ptr(
65 LoadProvider("manifest",
66 &CreateFeature<ManifestFeature>,
67 IDR_EXTENSION_MANIFEST_FEATURES));
70 typedef std::map<std::string, linked_ptr<FeatureProvider> >
71 FeatureProviderMap;
73 FeatureProvider* GetFeatures(const std::string& name) const {
74 FeatureProviderMap::const_iterator it = feature_providers.find(name);
75 CHECK(it != feature_providers.end());
76 return it->second.get();
79 FeatureProviderMap feature_providers;
82 base::LazyInstance<Static> g_static = LAZY_INSTANCE_INITIALIZER;
84 bool ParseFeature(const base::DictionaryValue* value,
85 const std::string& name,
86 SimpleFeature* feature) {
87 feature->set_name(name);
88 std::string error = feature->Parse(value);
89 if (!error.empty())
90 LOG(ERROR) << error;
91 return error.empty();
94 } // namespace
96 BaseFeatureProvider::BaseFeatureProvider(const base::DictionaryValue& root,
97 FeatureFactory factory)
98 : factory_(factory ? factory :
99 static_cast<FeatureFactory>(&CreateFeature<SimpleFeature>)) {
100 for (base::DictionaryValue::Iterator iter(root); !iter.IsAtEnd();
101 iter.Advance()) {
102 if (iter.value().GetType() == base::Value::TYPE_DICTIONARY) {
103 linked_ptr<SimpleFeature> feature((*factory_)());
105 std::vector<std::string> split;
106 base::SplitString(iter.key(), '.', &split);
108 // Push parent features on the stack, starting with the current feature.
109 // If one of the features has "noparent" set, stop pushing features on
110 // the stack. The features will then be parsed in order, starting with
111 // the farthest parent that is either top level or has "noparent" set.
112 std::stack<std::pair<std::string, const base::DictionaryValue*> >
113 parse_stack;
114 while (!split.empty()) {
115 std::string parent_name = JoinString(split, '.');
116 split.pop_back();
117 if (root.HasKey(parent_name)) {
118 const base::DictionaryValue* parent = NULL;
119 CHECK(root.GetDictionaryWithoutPathExpansion(parent_name, &parent));
120 parse_stack.push(std::make_pair(parent_name, parent));
121 bool no_parent = false;
122 parent->GetBoolean("noparent", &no_parent);
123 if (no_parent)
124 break;
128 CHECK(!parse_stack.empty());
129 // Parse all parent features.
130 bool parse_error = false;
131 while (!parse_stack.empty()) {
132 if (!ParseFeature(parse_stack.top().second,
133 parse_stack.top().first,
134 feature.get())) {
135 parse_error = true;
136 break;
138 parse_stack.pop();
141 if (parse_error)
142 continue;
144 features_[iter.key()] = feature;
145 } else if (iter.value().GetType() == base::Value::TYPE_LIST) {
146 // This is a complex feature.
147 const base::ListValue* list =
148 static_cast<const base::ListValue*>(&iter.value());
149 CHECK_GT(list->GetSize(), 0UL);
151 scoped_ptr<ComplexFeature::FeatureList> features(
152 new ComplexFeature::FeatureList());
154 // Parse and add all SimpleFeatures from the list.
155 for (base::ListValue::const_iterator list_iter = list->begin();
156 list_iter != list->end(); ++list_iter) {
157 if ((*list_iter)->GetType() != base::Value::TYPE_DICTIONARY) {
158 LOG(ERROR) << iter.key() << ": Feature rules must be dictionaries.";
159 continue;
162 scoped_ptr<SimpleFeature> feature((*factory_)());
163 if (!ParseFeature(static_cast<const base::DictionaryValue*>(*list_iter),
164 iter.key(),
165 feature.get()))
166 continue;
168 features->push_back(feature.release());
171 linked_ptr<ComplexFeature> feature(new ComplexFeature(features.Pass()));
172 feature->set_name(iter.key());
174 features_[iter.key()] = feature;
175 } else {
176 LOG(ERROR) << iter.key() << ": Feature description must be dictionary or"
177 << " list of dictionaries.";
182 BaseFeatureProvider::~BaseFeatureProvider() {
185 // static
186 FeatureProvider* BaseFeatureProvider::GetByName(
187 const std::string& name) {
188 return g_static.Get().GetFeatures(name);
191 const std::vector<std::string>& BaseFeatureProvider::GetAllFeatureNames()
192 const {
193 if (feature_names_.empty()) {
194 for (FeatureMap::const_iterator iter = features_.begin();
195 iter != features_.end(); ++iter) {
196 feature_names_.push_back(iter->first);
198 // A std::map is sorted by its keys, so we don't need to sort feature_names_
199 // now.
201 return feature_names_;
204 Feature* BaseFeatureProvider::GetFeature(const std::string& name) const {
205 FeatureMap::const_iterator iter = features_.find(name);
206 if (iter != features_.end())
207 return iter->second.get();
208 else
209 return NULL;
212 Feature* BaseFeatureProvider::GetParent(Feature* feature) const {
213 CHECK(feature);
214 if (feature->no_parent())
215 return NULL;
217 std::vector<std::string> split;
218 base::SplitString(feature->name(), '.', &split);
219 if (split.size() < 2)
220 return NULL;
221 split.pop_back();
222 return GetFeature(JoinString(split, '.'));
225 // Children of a given API are named starting with parent.name()+".", which
226 // means they'll be contiguous in the features_ std::map.
227 std::vector<Feature*> BaseFeatureProvider::GetChildren(const Feature& parent)
228 const {
229 std::string prefix = parent.name() + ".";
230 const FeatureMap::const_iterator first_child = features_.lower_bound(prefix);
232 // All children have names before (parent.name() + ('.'+1)).
233 ++prefix[prefix.size() - 1];
234 const FeatureMap::const_iterator after_children =
235 features_.lower_bound(prefix);
237 std::vector<Feature*> result;
238 result.reserve(std::distance(first_child, after_children));
239 for (FeatureMap::const_iterator it = first_child; it != after_children;
240 ++it) {
241 result.push_back(it->second.get());
243 return result;
246 } // namespace extensions