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

Side by Side Diff: chrome/common/extensions/features/simple_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: Review comments, cleanup 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
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 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 "chrome/common/extensions/features/simple_feature_provider.h" 5 #include "chrome/common/extensions/features/simple_feature_provider.h"
6 6
7 #include "base/json/json_reader.h" 7 #include "base/json/json_reader.h"
8 #include "base/lazy_instance.h" 8 #include "base/lazy_instance.h"
9 #include "chrome/common/extensions/features/complex_feature.h"
9 #include "chrome/common/extensions/features/manifest_feature.h" 10 #include "chrome/common/extensions/features/manifest_feature.h"
10 #include "chrome/common/extensions/features/permission_feature.h" 11 #include "chrome/common/extensions/features/permission_feature.h"
11 #include "grit/common_resources.h" 12 #include "grit/common_resources.h"
12 #include "ui/base/resource/resource_bundle.h" 13 #include "ui/base/resource/resource_bundle.h"
13 14
14 namespace extensions { 15 namespace extensions {
15 16
16 namespace { 17 namespace {
17 18
18 template<class FeatureClass> 19 template<class FeatureClass>
19 Feature* CreateFeature() { 20 SimpleFeature* CreateFeature() {
20 return new FeatureClass(); 21 return new FeatureClass();
21 } 22 }
22 23
23 struct Static { 24 struct Static {
24 Static() 25 Static()
25 : manifest_features( 26 : manifest_features(
26 LoadProvider("manifest", 27 LoadProvider("manifest",
27 &CreateFeature<ManifestFeature>, 28 &CreateFeature<ManifestFeature>,
28 IDR_EXTENSION_MANIFEST_FEATURES)), 29 IDR_EXTENSION_MANIFEST_FEATURES)),
29 permission_features( 30 permission_features(
(...skipping 17 matching lines...) Expand all
47 std::string error_message; 48 std::string error_message;
48 Value* value = base::JSONReader::ReadAndReturnError( 49 Value* value = base::JSONReader::ReadAndReturnError(
49 manifest_features, base::JSON_PARSE_RFC, 50 manifest_features, base::JSON_PARSE_RFC,
50 &error_code, &error_message); 51 &error_code, &error_message);
51 CHECK(value) << "Could not load features: " << debug_string << " " 52 CHECK(value) << "Could not load features: " << debug_string << " "
52 << error_message; 53 << error_message;
53 CHECK(value->IsType(Value::TYPE_DICTIONARY)) << debug_string; 54 CHECK(value->IsType(Value::TYPE_DICTIONARY)) << debug_string;
54 scoped_ptr<DictionaryValue> dictionary_value( 55 scoped_ptr<DictionaryValue> dictionary_value(
55 static_cast<DictionaryValue*>(value)); 56 static_cast<DictionaryValue*>(value));
56 return scoped_ptr<SimpleFeatureProvider>( 57 return scoped_ptr<SimpleFeatureProvider>(
57 new SimpleFeatureProvider(dictionary_value.get(), factory)); 58 new SimpleFeatureProvider(*dictionary_value, factory));
58 } 59 }
59 }; 60 };
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->contexts()->empty()) {
75 LOG(ERROR) << name << ": Simple features do not support contexts.";
76 return false;
not at google - send to devlin 2012/12/14 19:10:42 if these DCHECKed then this method can't fail, and
justinlin 2012/12/14 21:07:52 Done. Oops, had to undo. Tests do things like this
77 }
78
79 return true;
80 }
81
61 base::LazyInstance<Static> g_static = LAZY_INSTANCE_INITIALIZER; 82 base::LazyInstance<Static> g_static = LAZY_INSTANCE_INITIALIZER;
62 83
63 } // namespace 84 } // namespace
64 85
65 SimpleFeatureProvider::SimpleFeatureProvider(DictionaryValue* root, 86 SimpleFeatureProvider::SimpleFeatureProvider(const DictionaryValue& root,
66 FeatureFactory factory) 87 FeatureFactory factory)
67 : factory_(factory ? factory : 88 : factory_(factory ? factory :
68 static_cast<FeatureFactory>(&CreateFeature<Feature>)) { 89 static_cast<FeatureFactory>(&CreateFeature<SimpleFeature>)) {
69 for (DictionaryValue::Iterator iter(*root); iter.HasNext(); iter.Advance()) { 90 for (DictionaryValue::Iterator iter(root); iter.HasNext(); iter.Advance()) {
70 if (iter.value().GetType() != Value::TYPE_DICTIONARY) { 91 if (iter.value().GetType() == Value::TYPE_DICTIONARY) {
71 LOG(ERROR) << iter.key() << ": Feature description must be dictionary."; 92 linked_ptr<SimpleFeature> feature((*factory_)());
72 continue; 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 if (list->GetSize() == 0) {
104 LOG(ERROR) << iter.key() << ": Feature has no rules.";
105 continue;
106 }
107
108 scoped_ptr<ComplexFeature::FeatureList> features(
109 new ComplexFeature::FeatureList());
110
111 // Parse and add all SimpleFeatures from the list.
112 for (ListValue::const_iterator list_iter = list->begin();
113 list_iter != list->end(); ++list_iter) {
114 if ((*list_iter)->GetType() != Value::TYPE_DICTIONARY) {
115 LOG(ERROR) << iter.key() << ": Feature rules must be dictionaries.";
116 continue;
117 }
118
119 scoped_ptr<SimpleFeature> feature((*factory_)());
120 if (!ParseFeature(static_cast<const DictionaryValue*>(*list_iter),
121 iter.key(),
122 feature.get()))
123 continue;
124
125 features->push_back(feature.release());
126 }
127
128 linked_ptr<ComplexFeature> feature(new ComplexFeature(features.Pass()));
129 feature->set_name(iter.key());
130
131 features_[iter.key()] = feature;
132 } else {
133 LOG(ERROR) << iter.key() << ": Feature description must be dictionary or"
134 << " list of dictionaries.";
73 } 135 }
74
75 linked_ptr<Feature> feature((*factory_)());
76 feature->set_name(iter.key());
77 feature->Parse(static_cast<const DictionaryValue*>(&iter.value()));
78
79 if (feature->extension_types()->empty()) {
80 LOG(ERROR) << iter.key() << ": Simple features must specify atleast one "
81 << "value for extension_types.";
82 continue;
83 }
84
85 if (!feature->contexts()->empty()) {
86 LOG(ERROR) << iter.key() << ": Simple features do not support contexts.";
87 continue;
88 }
89
90 features_[iter.key()] = feature;
91 } 136 }
92 } 137 }
93 138
94 SimpleFeatureProvider::~SimpleFeatureProvider() { 139 SimpleFeatureProvider::~SimpleFeatureProvider() {
95 } 140 }
96 141
97 // static 142 // static
98 SimpleFeatureProvider* SimpleFeatureProvider::GetManifestFeatures() { 143 SimpleFeatureProvider* SimpleFeatureProvider::GetManifestFeatures() {
99 return g_static.Get().manifest_features.get(); 144 return g_static.Get().manifest_features.get();
100 } 145 }
(...skipping 14 matching lines...) Expand all
115 160
116 Feature* SimpleFeatureProvider::GetFeature(const std::string& name) { 161 Feature* SimpleFeatureProvider::GetFeature(const std::string& name) {
117 FeatureMap::iterator iter = features_.find(name); 162 FeatureMap::iterator iter = features_.find(name);
118 if (iter != features_.end()) 163 if (iter != features_.end())
119 return iter->second.get(); 164 return iter->second.get();
120 else 165 else
121 return NULL; 166 return NULL;
122 } 167 }
123 168
124 } // namespace 169 } // namespace
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698