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

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

Issue 13997002: Load extensions features file separately when needed (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: fix merge conflicts Created 7 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/features/base_feature_provider.h" 5 #include "chrome/common/extensions/features/base_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/api_feature.h" 9 #include "chrome/common/extensions/features/api_feature.h"
10 #include "chrome/common/extensions/features/complex_feature.h" 10 #include "chrome/common/extensions/features/complex_feature.h"
11 #include "chrome/common/extensions/features/manifest_feature.h" 11 #include "chrome/common/extensions/features/manifest_feature.h"
12 #include "chrome/common/extensions/features/permission_feature.h" 12 #include "chrome/common/extensions/features/permission_feature.h"
13 #include "grit/common_resources.h" 13 #include "grit/common_resources.h"
14 #include "ui/base/resource/resource_bundle.h" 14 #include "ui/base/resource/resource_bundle.h"
15 15
16 namespace extensions { 16 namespace extensions {
17 17
18 namespace { 18 namespace {
19 19
20 template<class FeatureClass> 20 template<class FeatureClass>
21 SimpleFeature* CreateFeature() { 21 SimpleFeature* CreateFeature() {
22 return new FeatureClass(); 22 return new FeatureClass();
23 } 23 }
24 24
25 struct Static { 25 class LazyFeatureProvider : public FeatureProvider {
26 Static() 26 public:
27 : api_features( 27 LazyFeatureProvider(const std::string& name,
28 LoadProvider("api", 28 BaseFeatureProvider::FeatureFactory factory,
29 &CreateFeature<APIFeature>, 29 int resource_id)
30 IDR_EXTENSION_API_FEATURES)), 30 : name_(name),
31 manifest_features( 31 factory_(factory),
32 LoadProvider("manifest", 32 resource_id_(resource_id) {
33 &CreateFeature<ManifestFeature>,
34 IDR_EXTENSION_MANIFEST_FEATURES)),
35 permission_features(
36 LoadProvider("permissions",
37 &CreateFeature<PermissionFeature>,
38 IDR_EXTENSION_PERMISSION_FEATURES)) {
39 } 33 }
40 34
41 scoped_ptr<BaseFeatureProvider> api_features; 35 virtual Feature* GetFeature(const std::string& name) {
42 scoped_ptr<BaseFeatureProvider> manifest_features; 36 return GetBaseFeatureProvider()->GetFeature(name);
43 scoped_ptr<BaseFeatureProvider> permission_features; 37 }
38
39 virtual std::set<std::string> GetAllFeatureNames() {
40 return GetBaseFeatureProvider()->GetAllFeatureNames();
41 }
44 42
45 private: 43 private:
46 scoped_ptr<BaseFeatureProvider> LoadProvider( 44 BaseFeatureProvider* GetBaseFeatureProvider() {
47 const std::string& debug_string, 45 if (!features_)
48 BaseFeatureProvider::FeatureFactory factory, 46 features_ = LoadProvider();
49 int resource_id) { 47 return features_.get();
48 }
49
50 scoped_ptr<BaseFeatureProvider> LoadProvider() {
50 const std::string& features_file = 51 const std::string& features_file =
51 ResourceBundle::GetSharedInstance().GetRawDataResource( 52 ResourceBundle::GetSharedInstance().GetRawDataResource(
52 resource_id).as_string(); 53 resource_id_).as_string();
53 int error_code = 0; 54 int error_code = 0;
54 std::string error_message; 55 std::string error_message;
55 scoped_ptr<Value> value(base::JSONReader::ReadAndReturnError( 56 scoped_ptr<Value> value(base::JSONReader::ReadAndReturnError(
56 features_file, base::JSON_PARSE_RFC, 57 features_file, base::JSON_PARSE_RFC,
57 &error_code, &error_message)); 58 &error_code, &error_message));
58 DCHECK(value) << "Could not load features: " << debug_string << " " 59 DCHECK(value) << "Could not load features: " << name_ << " "
59 << error_message; 60 << error_message;
60 scoped_ptr<DictionaryValue> value_as_dict; 61 scoped_ptr<DictionaryValue> value_as_dict;
61 if (value) { 62 if (value) {
62 CHECK(value->IsType(Value::TYPE_DICTIONARY)) << debug_string; 63 CHECK(value->IsType(Value::TYPE_DICTIONARY)) << name_;
63 value_as_dict.reset(static_cast<DictionaryValue*>(value.release())); 64 value_as_dict.reset(static_cast<DictionaryValue*>(value.release()));
64 } else { 65 } else {
65 // http://crbug.com/176381 66 // http://crbug.com/176381
66 value_as_dict.reset(new DictionaryValue()); 67 value_as_dict.reset(new DictionaryValue());
67 } 68 }
68 return make_scoped_ptr(new BaseFeatureProvider(*value_as_dict, factory)); 69 return make_scoped_ptr(new BaseFeatureProvider(*value_as_dict, factory_));
69 } 70 }
71
72 std::string name_;
73 BaseFeatureProvider::FeatureFactory factory_;
74 int resource_id_;
75 scoped_ptr<BaseFeatureProvider> features_;
70 }; 76 };
71 77
78 struct Static {
79 Static() {
80 lazy_feature_providers["api"] = make_linked_ptr(
81 new LazyFeatureProvider("api",
82 &CreateFeature<APIFeature>,
83 IDR_EXTENSION_API_FEATURES));
84 lazy_feature_providers["permission"] = make_linked_ptr(
85 new LazyFeatureProvider("permission",
86 &CreateFeature<PermissionFeature>,
87 IDR_EXTENSION_PERMISSION_FEATURES));
88 lazy_feature_providers["manifest"] = make_linked_ptr(
89 new LazyFeatureProvider("manifest",
90 &CreateFeature<ManifestFeature>,
91 IDR_EXTENSION_MANIFEST_FEATURES));
92 }
93
94 typedef std::map<std::string, linked_ptr<LazyFeatureProvider> >
95 LazyFeatureProviderMap;
96
97 LazyFeatureProvider* LazyGetFeatures(const std::string& name) {
98 LazyFeatureProviderMap::iterator it = lazy_feature_providers.find(name);
99 CHECK(it != lazy_feature_providers.end());
100 return it->second.get();
101 }
102
103 LazyFeatureProviderMap lazy_feature_providers;
104 };
105
106 base::LazyInstance<Static> g_static = LAZY_INSTANCE_INITIALIZER;
107
72 bool ParseFeature(const DictionaryValue* value, 108 bool ParseFeature(const DictionaryValue* value,
73 const std::string& name, 109 const std::string& name,
74 SimpleFeature* feature) { 110 SimpleFeature* feature) {
75 feature->set_name(name); 111 feature->set_name(name);
76 std::string error = feature->Parse(value); 112 std::string error = feature->Parse(value);
77 if (!error.empty()) 113 if (!error.empty())
78 LOG(ERROR) << error; 114 LOG(ERROR) << error;
79 return error.empty(); 115 return error.empty();
80 } 116 }
81 117
82 base::LazyInstance<Static> g_static = LAZY_INSTANCE_INITIALIZER;
83
84 } // namespace 118 } // namespace
85 119
86 BaseFeatureProvider::BaseFeatureProvider(const DictionaryValue& root, 120 BaseFeatureProvider::BaseFeatureProvider(const DictionaryValue& root,
87 FeatureFactory factory) 121 FeatureFactory factory)
88 : factory_(factory ? factory : 122 : factory_(factory ? factory :
89 static_cast<FeatureFactory>(&CreateFeature<SimpleFeature>)) { 123 static_cast<FeatureFactory>(&CreateFeature<SimpleFeature>)) {
90 for (DictionaryValue::Iterator iter(root); !iter.IsAtEnd(); iter.Advance()) { 124 for (DictionaryValue::Iterator iter(root); !iter.IsAtEnd(); iter.Advance()) {
91 if (iter.value().GetType() == Value::TYPE_DICTIONARY) { 125 if (iter.value().GetType() == Value::TYPE_DICTIONARY) {
92 linked_ptr<SimpleFeature> feature((*factory_)()); 126 linked_ptr<SimpleFeature> feature((*factory_)());
93 127
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
130 LOG(ERROR) << iter.key() << ": Feature description must be dictionary or" 164 LOG(ERROR) << iter.key() << ": Feature description must be dictionary or"
131 << " list of dictionaries."; 165 << " list of dictionaries.";
132 } 166 }
133 } 167 }
134 } 168 }
135 169
136 BaseFeatureProvider::~BaseFeatureProvider() { 170 BaseFeatureProvider::~BaseFeatureProvider() {
137 } 171 }
138 172
139 // static 173 // static
140 BaseFeatureProvider* BaseFeatureProvider::GetAPIFeatures() { 174 FeatureProvider* BaseFeatureProvider::GetByName(
141 return g_static.Get().api_features.get(); 175 const std::string& name) {
176 return g_static.Get().LazyGetFeatures(name);
142 } 177 }
143 178
144 // static 179 std::set<std::string> BaseFeatureProvider::GetAllFeatureNames() {
145 BaseFeatureProvider* BaseFeatureProvider::GetManifestFeatures() {
146 return g_static.Get().manifest_features.get();
147 }
148
149 // static
150 BaseFeatureProvider* BaseFeatureProvider::GetPermissionFeatures() {
151 return g_static.Get().permission_features.get();
152 }
153
154 std::set<std::string> BaseFeatureProvider::GetAllFeatureNames() const {
155 std::set<std::string> result; 180 std::set<std::string> result;
156 for (FeatureMap::const_iterator iter = features_.begin(); 181 for (FeatureMap::const_iterator iter = features_.begin();
157 iter != features_.end(); ++iter) { 182 iter != features_.end(); ++iter) {
158 result.insert(iter->first); 183 result.insert(iter->first);
159 } 184 }
160 return result; 185 return result;
161 } 186 }
162 187
163 Feature* BaseFeatureProvider::GetFeature(const std::string& name) { 188 Feature* BaseFeatureProvider::GetFeature(const std::string& name) {
164 FeatureMap::iterator iter = features_.find(name); 189 FeatureMap::iterator iter = features_.find(name);
165 if (iter != features_.end()) 190 if (iter != features_.end())
166 return iter->second.get(); 191 return iter->second.get();
167 else 192 else
168 return NULL; 193 return NULL;
169 } 194 }
170 195
171 } // namespace 196 } // namespace
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698