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

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: 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/simple_feature_provider.h"
6
7 #include "base/json/json_reader.h"
8 #include "base/lazy_instance.h"
9 #include "chrome/common/extensions/features/manifest_feature.h"
10 #include "chrome/common/extensions/features/permission_feature.h"
11 #include "grit/common_resources.h"
12 #include "ui/base/resource/resource_bundle.h"
13
14 namespace extensions {
15
16 namespace {
17
18 template<class FeatureClass>
19 Feature* CreateFeature() {
20 return new FeatureClass();
21 }
22
23 struct Static {
24 Static()
25 : manifest_features(
26 LoadProvider("manifest",
27 &CreateFeature<ManifestFeature>,
28 IDR_EXTENSION_MANIFEST_FEATURES)),
29 permission_features(
30 LoadProvider("permissions",
31 &CreateFeature<PermissionFeature>,
32 IDR_EXTENSION_PERMISSION_FEATURES)) {
33 }
34
35 scoped_ptr<SimpleFeatureProvider> manifest_features;
36 scoped_ptr<SimpleFeatureProvider> permission_features;
37
38 private:
39 scoped_ptr<SimpleFeatureProvider> LoadProvider(
40 const std::string& debug_string,
41 SimpleFeatureProvider::FeatureFactory factory,
42 int resource_id) {
43 std::string manifest_features =
44 ResourceBundle::GetSharedInstance().GetRawDataResource(
45 resource_id).as_string();
46 int error_code = 0;
47 std::string error_message;
48 Value* value = base::JSONReader::ReadAndReturnError(
49 manifest_features, base::JSON_PARSE_RFC,
50 &error_code, &error_message);
51 CHECK(value) << "Could not load features: " << debug_string << " "
52 << error_message;
53 CHECK(value->IsType(Value::TYPE_DICTIONARY)) << debug_string;
54 scoped_ptr<DictionaryValue> dictionary_value(
55 static_cast<DictionaryValue*>(value));
56 return scoped_ptr<SimpleFeatureProvider>(
57 new SimpleFeatureProvider(dictionary_value.get(), factory));
58 }
59 };
60
61 base::LazyInstance<Static> g_static = LAZY_INSTANCE_INITIALIZER;
62
63 } // namespace
64
65 SimpleFeatureProvider::SimpleFeatureProvider(DictionaryValue* root,
66 FeatureFactory factory)
67 : factory_(factory ? factory :
68 static_cast<FeatureFactory>(&CreateFeature<Feature>)) {
69 for (DictionaryValue::Iterator iter(*root); iter.HasNext(); iter.Advance()) {
70 if (iter.value().GetType() != Value::TYPE_DICTIONARY) {
71 LOG(ERROR) << iter.key() << ": Feature description must be dictionary.";
72 continue;
73 }
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 }
92 }
93
94 SimpleFeatureProvider::~SimpleFeatureProvider() {
95 }
96
97 // static
98 SimpleFeatureProvider* SimpleFeatureProvider::GetManifestFeatures() {
99 return g_static.Get().manifest_features.get();
100 }
101
102 // static
103 SimpleFeatureProvider* SimpleFeatureProvider::GetPermissionFeatures() {
104 return g_static.Get().permission_features.get();
105 }
106
107 std::set<std::string> SimpleFeatureProvider::GetAllFeatureNames() const {
108 std::set<std::string> result;
109 for (FeatureMap::const_iterator iter = features_.begin();
110 iter != features_.end(); ++iter) {
111 result.insert(iter->first);
112 }
113 return result;
114 }
115
116 Feature* SimpleFeatureProvider::GetFeature(const std::string& name) {
117 FeatureMap::iterator iter = features_.find(name);
118 if (iter != features_.end())
119 return iter->second.get();
120 else
121 return NULL;
122 }
123
124 } // namespace
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698