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

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

Issue 15091002: Lazily load API schemas from resource files and convert all APIs to features (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: more comments and fixed tests Created 7 years, 6 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"
(...skipping 18 matching lines...) Expand all
29 int resource_id) 29 int resource_id)
30 : name_(name), 30 : name_(name),
31 factory_(factory), 31 factory_(factory),
32 resource_id_(resource_id) { 32 resource_id_(resource_id) {
33 } 33 }
34 34
35 virtual Feature* GetFeature(const std::string& name) OVERRIDE { 35 virtual Feature* GetFeature(const std::string& name) OVERRIDE {
36 return GetBaseFeatureProvider()->GetFeature(name); 36 return GetBaseFeatureProvider()->GetFeature(name);
37 } 37 }
38 38
39 virtual std::set<std::string> GetAllFeatureNames() OVERRIDE { 39 virtual const std::vector<std::string>& GetAllFeatureNames() OVERRIDE {
40 return GetBaseFeatureProvider()->GetAllFeatureNames(); 40 return GetBaseFeatureProvider()->GetAllFeatureNames();
41 } 41 }
42 42
43 private: 43 private:
44 BaseFeatureProvider* GetBaseFeatureProvider() { 44 BaseFeatureProvider* GetBaseFeatureProvider() {
45 if (!features_) 45 if (!features_)
46 features_ = LoadProvider(); 46 features_ = LoadProvider();
47 return features_.get(); 47 return features_.get();
48 } 48 }
49 49
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
98 LazyFeatureProviderMap::iterator it = lazy_feature_providers.find(name); 98 LazyFeatureProviderMap::iterator it = lazy_feature_providers.find(name);
99 CHECK(it != lazy_feature_providers.end()); 99 CHECK(it != lazy_feature_providers.end());
100 return it->second.get(); 100 return it->second.get();
101 } 101 }
102 102
103 LazyFeatureProviderMap lazy_feature_providers; 103 LazyFeatureProviderMap lazy_feature_providers;
104 }; 104 };
105 105
106 base::LazyInstance<Static> g_static = LAZY_INSTANCE_INITIALIZER; 106 base::LazyInstance<Static> g_static = LAZY_INSTANCE_INITIALIZER;
107 107
108 bool ParseFeature(const DictionaryValue* value, 108 bool ParseFeature(const DictionaryValue& root,
109 const DictionaryValue* value,
109 const std::string& name, 110 const std::string& name,
110 SimpleFeature* feature) { 111 SimpleFeature* feature) {
112 std::string error;
113 std::string parent_name;
114 if (value->GetString("parent", &parent_name)) {
115 const DictionaryValue* parent = NULL;
not at google - send to devlin 2013/05/30 16:38:52 bleh so sorry about that :(
cduvall 2013/06/12 01:22:19 np, it wasn't that big of a change.
116 CHECK(root.GetDictionaryWithoutPathExpansion(parent_name, &parent));
117 scoped_ptr<DictionaryValue> value_with_parent(parent->DeepCopy());
118 value_with_parent->MergeDictionary(value);
119 error = feature->Parse(value_with_parent.get());
120 } else {
121 error = feature->Parse(value);
122 }
111 feature->set_name(name); 123 feature->set_name(name);
112 std::string error = feature->Parse(value);
113 if (!error.empty()) 124 if (!error.empty())
114 LOG(ERROR) << error; 125 LOG(ERROR) << error;
115 return error.empty(); 126 return error.empty();
116 } 127 }
117 128
118 } // namespace 129 } // namespace
119 130
120 BaseFeatureProvider::BaseFeatureProvider(const DictionaryValue& root, 131 BaseFeatureProvider::BaseFeatureProvider(const DictionaryValue& root,
121 FeatureFactory factory) 132 FeatureFactory factory)
122 : factory_(factory ? factory : 133 : factory_(factory ? factory :
123 static_cast<FeatureFactory>(&CreateFeature<SimpleFeature>)) { 134 static_cast<FeatureFactory>(&CreateFeature<SimpleFeature>)) {
124 for (DictionaryValue::Iterator iter(root); !iter.IsAtEnd(); iter.Advance()) { 135 for (DictionaryValue::Iterator iter(root); !iter.IsAtEnd(); iter.Advance()) {
125 if (iter.value().GetType() == Value::TYPE_DICTIONARY) { 136 if (iter.value().GetType() == Value::TYPE_DICTIONARY) {
126 linked_ptr<SimpleFeature> feature((*factory_)()); 137 linked_ptr<SimpleFeature> feature((*factory_)());
127 138
128 if (!ParseFeature(static_cast<const DictionaryValue*>(&iter.value()), 139 if (!ParseFeature(root,
140 static_cast<const DictionaryValue*>(&iter.value()),
129 iter.key(), 141 iter.key(),
130 feature.get())) 142 feature.get())) {
131 continue; 143 continue;
144 }
132 145
133 features_[iter.key()] = feature; 146 features_[iter.key()] = feature;
134 } else if (iter.value().GetType() == Value::TYPE_LIST) { 147 } else if (iter.value().GetType() == Value::TYPE_LIST) {
135 // This is a complex feature. 148 // This is a complex feature.
136 const ListValue* list = static_cast<const ListValue*>(&iter.value()); 149 const ListValue* list = static_cast<const ListValue*>(&iter.value());
137 CHECK_GT(list->GetSize(), 0UL); 150 CHECK_GT(list->GetSize(), 0UL);
138 151
139 scoped_ptr<ComplexFeature::FeatureList> features( 152 scoped_ptr<ComplexFeature::FeatureList> features(
140 new ComplexFeature::FeatureList()); 153 new ComplexFeature::FeatureList());
141 154
142 // Parse and add all SimpleFeatures from the list. 155 // Parse and add all SimpleFeatures from the list.
143 for (ListValue::const_iterator list_iter = list->begin(); 156 for (ListValue::const_iterator list_iter = list->begin();
144 list_iter != list->end(); ++list_iter) { 157 list_iter != list->end(); ++list_iter) {
145 if ((*list_iter)->GetType() != Value::TYPE_DICTIONARY) { 158 if ((*list_iter)->GetType() != Value::TYPE_DICTIONARY) {
146 LOG(ERROR) << iter.key() << ": Feature rules must be dictionaries."; 159 LOG(ERROR) << iter.key() << ": Feature rules must be dictionaries.";
147 continue; 160 continue;
148 } 161 }
149 162
150 scoped_ptr<SimpleFeature> feature((*factory_)()); 163 scoped_ptr<SimpleFeature> feature((*factory_)());
151 if (!ParseFeature(static_cast<const DictionaryValue*>(*list_iter), 164 if (!ParseFeature(root,
165 static_cast<const DictionaryValue*>(*list_iter),
152 iter.key(), 166 iter.key(),
153 feature.get())) 167 feature.get())) {
154 continue; 168 continue;
169 }
155 170
156 features->push_back(feature.release()); 171 features->push_back(feature.release());
157 } 172 }
158 173
159 linked_ptr<ComplexFeature> feature(new ComplexFeature(features.Pass())); 174 linked_ptr<ComplexFeature> feature(new ComplexFeature(features.Pass()));
160 feature->set_name(iter.key()); 175 feature->set_name(iter.key());
161 176
162 features_[iter.key()] = feature; 177 features_[iter.key()] = feature;
163 } else { 178 } else {
164 LOG(ERROR) << iter.key() << ": Feature description must be dictionary or" 179 LOG(ERROR) << iter.key() << ": Feature description must be dictionary or"
165 << " list of dictionaries."; 180 << " list of dictionaries.";
166 } 181 }
167 } 182 }
168 } 183 }
169 184
170 BaseFeatureProvider::~BaseFeatureProvider() { 185 BaseFeatureProvider::~BaseFeatureProvider() {
171 } 186 }
172 187
173 // static 188 // static
174 FeatureProvider* BaseFeatureProvider::GetByName( 189 FeatureProvider* BaseFeatureProvider::GetByName(
175 const std::string& name) { 190 const std::string& name) {
176 return g_static.Get().LazyGetFeatures(name); 191 return g_static.Get().LazyGetFeatures(name);
177 } 192 }
178 193
179 std::set<std::string> BaseFeatureProvider::GetAllFeatureNames() { 194 const std::vector<std::string>& BaseFeatureProvider::GetAllFeatureNames() {
180 std::set<std::string> result; 195 if (feature_names_.empty()) {
181 for (FeatureMap::const_iterator iter = features_.begin(); 196 for (FeatureMap::const_iterator iter = features_.begin();
182 iter != features_.end(); ++iter) { 197 iter != features_.end(); ++iter) {
183 result.insert(iter->first); 198 feature_names_.push_back(iter->first);
199 }
184 } 200 }
185 return result; 201 return feature_names_;
186 } 202 }
187 203
188 Feature* BaseFeatureProvider::GetFeature(const std::string& name) { 204 Feature* BaseFeatureProvider::GetFeature(const std::string& name) {
189 FeatureMap::iterator iter = features_.find(name); 205 FeatureMap::iterator iter = features_.find(name);
190 if (iter != features_.end()) 206 if (iter != features_.end())
191 return iter->second.get(); 207 return iter->second.get();
192 else 208 else
193 return NULL; 209 return NULL;
194 } 210 }
195 211
196 } // namespace 212 } // namespace
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698