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

Side by Side Diff: chrome/common/extensions/simple_feature_provider.cc

Issue 9950046: Implement FeatureProvider for ExtensionAPI. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: hate hate hate Created 8 years, 8 months 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/simple_feature_provider.h" 5 #include "chrome/common/extensions/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/manifest_feature.h"
10 #include "chrome/common/extensions/permission_feature.h"
9 #include "grit/common_resources.h" 11 #include "grit/common_resources.h"
10 #include "ui/base/resource/resource_bundle.h" 12 #include "ui/base/resource/resource_bundle.h"
11 13
14 namespace extensions {
15
12 namespace { 16 namespace {
13 17
14 const bool kAllowTrailingComma = false; 18 const bool kAllowTrailingComma = false;
15 19
20 template<class FeatureClass>
21 Feature* CreateFeature() {
22 return new FeatureClass();
23 }
24
16 struct Static { 25 struct Static {
17 Static() 26 Static()
18 : manifest_features( 27 : manifest_features(
19 LoadProvider("manifest", IDR_EXTENSION_MANIFEST_FEATURES)), 28 LoadProvider("manifest",
29 &CreateFeature<ManifestFeature>,
30 IDR_EXTENSION_MANIFEST_FEATURES)),
20 permission_features( 31 permission_features(
21 LoadProvider("permissions", IDR_EXTENSION_PERMISSION_FEATURES)) { 32 LoadProvider("permissions",
33 &CreateFeature<PermissionFeature>,
34 IDR_EXTENSION_PERMISSION_FEATURES)) {
22 } 35 }
23 36
24 scoped_ptr<extensions::SimpleFeatureProvider> manifest_features; 37 scoped_ptr<SimpleFeatureProvider> manifest_features;
25 scoped_ptr<extensions::SimpleFeatureProvider> permission_features; 38 scoped_ptr<SimpleFeatureProvider> permission_features;
26 39
27 private: 40 private:
28 scoped_ptr<extensions::SimpleFeatureProvider> LoadProvider( 41 scoped_ptr<SimpleFeatureProvider> LoadProvider(
29 const std::string& debug_string, int resource_id) { 42 const std::string& debug_string,
43 SimpleFeatureProvider::FeatureFactory factory,
44 int resource_id) {
30 std::string manifest_features = 45 std::string manifest_features =
31 ResourceBundle::GetSharedInstance().GetRawDataResource( 46 ResourceBundle::GetSharedInstance().GetRawDataResource(
32 resource_id).as_string(); 47 resource_id).as_string();
33 int error_code = 0; 48 int error_code = 0;
34 std::string error_message; 49 std::string error_message;
35 Value* value = base::JSONReader::ReadAndReturnError( 50 Value* value = base::JSONReader::ReadAndReturnError(
36 manifest_features, kAllowTrailingComma, &error_code, &error_message); 51 manifest_features, kAllowTrailingComma, &error_code, &error_message);
37 CHECK(value) << "Could not load features: " << debug_string << " " 52 CHECK(value) << "Could not load features: " << debug_string << " "
38 << error_message; 53 << error_message;
39 CHECK(value->IsType(Value::TYPE_DICTIONARY)) << debug_string; 54 CHECK(value->IsType(Value::TYPE_DICTIONARY)) << debug_string;
40 scoped_ptr<DictionaryValue> dictionary_value( 55 scoped_ptr<DictionaryValue> dictionary_value(
41 static_cast<DictionaryValue*>(value)); 56 static_cast<DictionaryValue*>(value));
42 return scoped_ptr<extensions::SimpleFeatureProvider>( 57 return scoped_ptr<SimpleFeatureProvider>(
43 new extensions::SimpleFeatureProvider(dictionary_value.Pass())); 58 new SimpleFeatureProvider(dictionary_value.Pass(), factory));
44 } 59 }
45 }; 60 };
46 61
47 base::LazyInstance<Static> g_static = LAZY_INSTANCE_INITIALIZER; 62 base::LazyInstance<Static> g_static = LAZY_INSTANCE_INITIALIZER;
48 63
49 } // namespace 64 } // namespace
50 65
51 namespace extensions { 66 SimpleFeatureProvider::SimpleFeatureProvider(scoped_ptr<DictionaryValue> root,
52 67 FeatureFactory factory)
53 SimpleFeatureProvider::SimpleFeatureProvider( 68 : root_(root.release()),
54 scoped_ptr<DictionaryValue> root) 69 factory_(factory ? factory :
55 : root_(root.release()) { 70 static_cast<FeatureFactory>(&CreateFeature<Feature>)) {
56 } 71 }
57 72
58 SimpleFeatureProvider::~SimpleFeatureProvider() { 73 SimpleFeatureProvider::~SimpleFeatureProvider() {
59 } 74 }
60 75
61 // static 76 // static
62 SimpleFeatureProvider* SimpleFeatureProvider::GetManifestFeatures() { 77 SimpleFeatureProvider* SimpleFeatureProvider::GetManifestFeatures() {
63 return g_static.Get().manifest_features.get(); 78 return g_static.Get().manifest_features.get();
64 } 79 }
65 80
66 // static 81 // static
67 SimpleFeatureProvider* SimpleFeatureProvider::GetPermissionFeatures() { 82 SimpleFeatureProvider* SimpleFeatureProvider::GetPermissionFeatures() {
68 return g_static.Get().permission_features.get(); 83 return g_static.Get().permission_features.get();
69 } 84 }
70 85
71 std::set<std::string> SimpleFeatureProvider::GetAllFeatureNames() const { 86 std::set<std::string> SimpleFeatureProvider::GetAllFeatureNames() const {
72 std::set<std::string> result; 87 std::set<std::string> result;
73 for (DictionaryValue::key_iterator iter = root_->begin_keys(); 88 for (DictionaryValue::key_iterator iter = root_->begin_keys();
74 iter != root_->end_keys(); ++iter) { 89 iter != root_->end_keys(); ++iter) {
75 result.insert(*iter); 90 result.insert(*iter);
76 } 91 }
77 return result; 92 return result;
78 } 93 }
79 94
80 scoped_ptr<Feature> SimpleFeatureProvider::GetFeature( 95 scoped_ptr<Feature> SimpleFeatureProvider::GetFeature(const std::string& name) {
81 const std::string& name) const {
82 scoped_ptr<Feature> feature;
83
84 DictionaryValue* description = NULL; 96 DictionaryValue* description = NULL;
85 if (root_->GetDictionary(name, &description)) 97 if (!root_->GetDictionary(name, &description)) {
86 feature = Feature::Parse(description); 98 LOG(ERROR) << name << ": Definition not found.";
87
88 if (!feature.get()) {
89 // Have to use DLOG here because this happens in a lot of unit tests that
90 // use ancient compiled crx files with unknown keys.
91 DLOG(ERROR) << name;
92 return scoped_ptr<Feature>(); 99 return scoped_ptr<Feature>();
93 } 100 }
94 101
102 scoped_ptr<Feature> feature(new Feature());
103 feature.reset((*factory_)());
104 feature->set_name(name);
105 feature->Parse(description);
106
95 if (feature->extension_types()->empty()) { 107 if (feature->extension_types()->empty()) {
96 LOG(ERROR) << name << ": Simple features must specify atleast one value " 108 LOG(ERROR) << name << ": Simple features must specify atleast one value "
97 << "for extension_types."; 109 << "for extension_types.";
98 return scoped_ptr<Feature>(); 110 return scoped_ptr<Feature>();
99 } 111 }
100 112
101 if (!feature->contexts()->empty()) { 113 if (!feature->contexts()->empty()) {
102 LOG(ERROR) << name << ": Simple features do not support contexts."; 114 LOG(ERROR) << name << ": Simple features do not support contexts.";
103 return scoped_ptr<Feature>(); 115 return scoped_ptr<Feature>();
104 } 116 }
105 117
106 return feature.Pass(); 118 return feature.Pass();
107 } 119 }
108 120
109 } // namespace 121 } // namespace
OLDNEW
« no previous file with comments | « chrome/common/extensions/simple_feature_provider.h ('k') | chrome/common/extensions/simple_feature_provider_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698