Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1317)

Side by Side Diff: chrome/common/extensions/features/base_feature_provider.cc

Issue 11316164: Implement ComplexFeature to support permission features with multiple rules. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Renames and add TODO Created 8 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
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.
4
5 #include "chrome/common/extensions/features/base_feature_provider.h"
6
7 #include "base/json/json_reader.h"
8 #include "base/lazy_instance.h"
9 #include "chrome/common/extensions/features/complex_feature.h"
10 #include "chrome/common/extensions/features/manifest_feature.h"
11 #include "chrome/common/extensions/features/permission_feature.h"
12 #include "grit/common_resources.h"
13 #include "ui/base/resource/resource_bundle.h"
14
15 namespace extensions {
16
17 namespace {
18
19 template<class FeatureClass>
20 SimpleFeature* CreateFeature() {
21 return new FeatureClass();
22 }
23
24 struct Static {
25 Static()
26 : manifest_features(
27 LoadProvider("manifest",
28 &CreateFeature<ManifestFeature>,
29 IDR_EXTENSION_MANIFEST_FEATURES)),
30 permission_features(
31 LoadProvider("permissions",
32 &CreateFeature<PermissionFeature>,
33 IDR_EXTENSION_PERMISSION_FEATURES)) {
34 }
35
36 scoped_ptr<BaseFeatureProvider> manifest_features;
37 scoped_ptr<BaseFeatureProvider> permission_features;
38
39 private:
40 scoped_ptr<BaseFeatureProvider> LoadProvider(
41 const std::string& debug_string,
42 BaseFeatureProvider::FeatureFactory factory,
43 int resource_id) {
44 std::string manifest_features =
45 ResourceBundle::GetSharedInstance().GetRawDataResource(
46 resource_id).as_string();
47 int error_code = 0;
48 std::string error_message;
49 Value* value = base::JSONReader::ReadAndReturnError(
50 manifest_features, base::JSON_PARSE_RFC,
51 &error_code, &error_message);
52 CHECK(value) << "Could not load features: " << debug_string << " "
53 << error_message;
54 CHECK(value->IsType(Value::TYPE_DICTIONARY)) << debug_string;
55 scoped_ptr<DictionaryValue> dictionary_value(
56 static_cast<DictionaryValue*>(value));
57 return scoped_ptr<BaseFeatureProvider>(
58 new BaseFeatureProvider(*dictionary_value, factory));
59 }
60 };
61
62 bool ParseFeature(const DictionaryValue* value,
63 const std::string& name,
64 SimpleFeature* feature) {
65 feature->set_name(name);
66 feature->Parse(value);
67
68 if (feature->extension_types()->empty()) {
69 LOG(ERROR) << name << ": Simple features must specify at least one "
70 << "value for extension_types.";
71 return false;
72 }
73
74 if (!feature->GetContexts()->empty()) {
75 LOG(ERROR) << name << ": Simple features do not support contexts.";
76 return false;
77 }
78
79 return true;
80 }
81
82 base::LazyInstance<Static> g_static = LAZY_INSTANCE_INITIALIZER;
83
84 } // namespace
85
86 BaseFeatureProvider::BaseFeatureProvider(const DictionaryValue& root,
87 FeatureFactory factory)
88 : factory_(factory ? factory :
89 static_cast<FeatureFactory>(&CreateFeature<SimpleFeature>)) {
90 for (DictionaryValue::Iterator iter(root); iter.HasNext(); iter.Advance()) {
91 if (iter.value().GetType() == Value::TYPE_DICTIONARY) {
92 linked_ptr<SimpleFeature> feature((*factory_)());
93
94 if (!ParseFeature(static_cast<const DictionaryValue*>(&iter.value()),
95 iter.key(),
96 feature.get()))
97 continue;
98
99 features_[iter.key()] = feature;
100 } else if (iter.value().GetType() == Value::TYPE_LIST) {
101 // This is a complex feature.
102 const ListValue* list = static_cast<const ListValue*>(&iter.value());
103 CHECK_GT(list->GetSize(), 0UL);
104
105 scoped_ptr<ComplexFeature::FeatureList> features(
106 new ComplexFeature::FeatureList());
107
108 // Parse and add all SimpleFeatures from the list.
109 for (ListValue::const_iterator list_iter = list->begin();
110 list_iter != list->end(); ++list_iter) {
111 if ((*list_iter)->GetType() != Value::TYPE_DICTIONARY) {
112 LOG(ERROR) << iter.key() << ": Feature rules must be dictionaries.";
113 continue;
114 }
115
116 scoped_ptr<SimpleFeature> feature((*factory_)());
117 if (!ParseFeature(static_cast<const DictionaryValue*>(*list_iter),
118 iter.key(),
119 feature.get()))
120 continue;
121
122 features->push_back(feature.release());
123 }
124
125 linked_ptr<ComplexFeature> feature(new ComplexFeature(features.Pass()));
126 feature->set_name(iter.key());
127
128 features_[iter.key()] = feature;
129 } else {
130 LOG(ERROR) << iter.key() << ": Feature description must be dictionary or"
131 << " list of dictionaries.";
132 }
133 }
134 }
135
136 BaseFeatureProvider::~BaseFeatureProvider() {
137 }
138
139 // static
140 BaseFeatureProvider* BaseFeatureProvider::GetManifestFeatures() {
141 return g_static.Get().manifest_features.get();
142 }
143
144 // static
145 BaseFeatureProvider* BaseFeatureProvider::GetPermissionFeatures() {
146 return g_static.Get().permission_features.get();
147 }
148
149 std::set<std::string> BaseFeatureProvider::GetAllFeatureNames() const {
150 std::set<std::string> result;
151 for (FeatureMap::const_iterator iter = features_.begin();
152 iter != features_.end(); ++iter) {
153 result.insert(iter->first);
154 }
155 return result;
156 }
157
158 Feature* BaseFeatureProvider::GetFeature(const std::string& name) {
159 FeatureMap::iterator iter = features_.find(name);
160 if (iter != features_.end())
161 return iter->second.get();
162 else
163 return NULL;
164 }
165
166 } // namespace
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698