OLD | NEW |
---|---|
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 struct LazyFeatureProvider : public FeatureProvider { |
not at google - send to devlin
2013/04/10 02:06:22
I don't think this needs to be a struct. The membe
cduvall
2013/04/10 02:26:12
Done.
| |
26 Static() | 26 LazyFeatureProvider(const std::string& name_, |
27 : api_features( | 27 BaseFeatureProvider::FeatureFactory factory_, |
28 LoadProvider("api", | 28 int resource_id_) |
29 &CreateFeature<APIFeature>, | 29 : name(name_), |
30 IDR_EXTENSION_API_FEATURES)), | 30 factory(factory_), |
31 manifest_features( | 31 resource_id(resource_id_) { |
32 LoadProvider("manifest", | |
33 &CreateFeature<ManifestFeature>, | |
34 IDR_EXTENSION_MANIFEST_FEATURES)), | |
35 permission_features( | |
36 LoadProvider("permissions", | |
37 &CreateFeature<PermissionFeature>, | |
38 IDR_EXTENSION_PERMISSION_FEATURES)) { | |
39 } | 32 } |
40 | 33 |
41 scoped_ptr<BaseFeatureProvider> api_features; | 34 BaseFeatureProvider* GetBaseFeatureProvider() { |
42 scoped_ptr<BaseFeatureProvider> manifest_features; | 35 if (!features) |
43 scoped_ptr<BaseFeatureProvider> permission_features; | 36 features.reset(LoadProvider(name, factory, resource_id).release()); |
not at google - send to devlin
2013/04/10 02:06:22
shouldn't need the release/reset here. features =
cduvall
2013/04/10 02:26:12
Done.
| |
37 return features.get(); | |
38 } | |
39 | |
40 virtual Feature* GetFeature(const std::string& name) { | |
41 return GetBaseFeatureProvider()->GetFeature(name); | |
42 } | |
43 | |
44 std::string name; | |
45 BaseFeatureProvider::FeatureFactory factory; | |
46 int resource_id; | |
47 scoped_ptr<BaseFeatureProvider> features; | |
44 | 48 |
45 private: | 49 private: |
46 scoped_ptr<BaseFeatureProvider> LoadProvider( | 50 scoped_ptr<BaseFeatureProvider> LoadProvider( |
47 const std::string& debug_string, | 51 const std::string& debug_string, |
48 BaseFeatureProvider::FeatureFactory factory, | 52 BaseFeatureProvider::FeatureFactory factory, |
49 int resource_id) { | 53 int resource_id) { |
50 const std::string& features_file = | 54 const std::string& features_file = |
51 ResourceBundle::GetSharedInstance().GetRawDataResource( | 55 ResourceBundle::GetSharedInstance().GetRawDataResource( |
52 resource_id).as_string(); | 56 resource_id).as_string(); |
53 int error_code = 0; | 57 int error_code = 0; |
54 std::string error_message; | 58 std::string error_message; |
55 scoped_ptr<Value> value(base::JSONReader::ReadAndReturnError( | 59 scoped_ptr<Value> value(base::JSONReader::ReadAndReturnError( |
56 features_file, base::JSON_PARSE_RFC, | 60 features_file, base::JSON_PARSE_RFC, |
57 &error_code, &error_message)); | 61 &error_code, &error_message)); |
58 DCHECK(value) << "Could not load features: " << debug_string << " " | 62 DCHECK(value) << "Could not load features: " << debug_string << " " |
59 << error_message; | 63 << error_message; |
60 scoped_ptr<DictionaryValue> value_as_dict; | 64 scoped_ptr<DictionaryValue> value_as_dict; |
61 if (value) { | 65 if (value) { |
62 CHECK(value->IsType(Value::TYPE_DICTIONARY)) << debug_string; | 66 CHECK(value->IsType(Value::TYPE_DICTIONARY)) << debug_string; |
63 value_as_dict.reset(static_cast<DictionaryValue*>(value.release())); | 67 value_as_dict.reset(static_cast<DictionaryValue*>(value.release())); |
64 } else { | 68 } else { |
65 // http://crbug.com/176381 | 69 // http://crbug.com/176381 |
66 value_as_dict.reset(new DictionaryValue()); | 70 value_as_dict.reset(new DictionaryValue()); |
67 } | 71 } |
68 return make_scoped_ptr(new BaseFeatureProvider(*value_as_dict, factory)); | 72 return make_scoped_ptr(new BaseFeatureProvider(*value_as_dict, factory)); |
69 } | 73 } |
70 }; | 74 }; |
71 | 75 |
76 struct Static { | |
77 Static() { | |
78 lazy_feature_providers["api"] = linked_ptr<LazyFeatureProvider>( | |
not at google - send to devlin
2013/04/10 02:06:22
make_linked_ptr?
cduvall
2013/04/10 02:26:12
Done.
| |
79 new LazyFeatureProvider("api", | |
80 &CreateFeature<APIFeature>, | |
81 IDR_EXTENSION_API_FEATURES)); | |
82 lazy_feature_providers["permission"] = linked_ptr<LazyFeatureProvider>( | |
83 new LazyFeatureProvider("permission", | |
84 &CreateFeature<PermissionFeature>, | |
85 IDR_EXTENSION_PERMISSION_FEATURES)); | |
86 lazy_feature_providers["manifest"] = linked_ptr<LazyFeatureProvider>( | |
87 new LazyFeatureProvider("manifest", | |
88 &CreateFeature<ManifestFeature>, | |
89 IDR_EXTENSION_MANIFEST_FEATURES)); | |
90 } | |
91 | |
92 LazyFeatureProvider* LazyGetFeatures(const std::string& name) { | |
93 CHECK(lazy_feature_providers.count(name) > 0); | |
94 return lazy_feature_providers[name].get(); | |
not at google - send to devlin
2013/04/10 02:06:22
use find, it = lazy_feature_providers.find(..); CH
cduvall
2013/04/10 02:26:12
Done.
| |
95 } | |
96 | |
97 BaseFeatureProvider* GetFeatures(const std::string& name) { | |
98 return LazyGetFeatures(name)->GetBaseFeatureProvider(); | |
99 } | |
100 | |
101 typedef std::map<std::string, linked_ptr<LazyFeatureProvider> > | |
102 LazyFeatureProviderMap; | |
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.HasNext(); iter.Advance()) { | 124 for (DictionaryValue::Iterator iter(root); iter.HasNext(); 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 Loading... | |
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 BaseFeatureProvider* BaseFeatureProvider::GetFeaturesByName( |
141 return g_static.Get().api_features.get(); | 175 const std::string& name) { |
176 return g_static.Get().GetFeatures(name); | |
142 } | 177 } |
143 | 178 |
144 // static | 179 // static |
145 BaseFeatureProvider* BaseFeatureProvider::GetManifestFeatures() { | 180 FeatureProvider* BaseFeatureProvider::LazyGetFeaturesByName( |
146 return g_static.Get().manifest_features.get(); | 181 const std::string& name) { |
147 } | 182 return g_static.Get().LazyGetFeatures(name); |
148 | |
149 // static | |
150 BaseFeatureProvider* BaseFeatureProvider::GetPermissionFeatures() { | |
151 return g_static.Get().permission_features.get(); | |
152 } | 183 } |
153 | 184 |
154 std::set<std::string> BaseFeatureProvider::GetAllFeatureNames() const { | 185 std::set<std::string> BaseFeatureProvider::GetAllFeatureNames() const { |
155 std::set<std::string> result; | 186 std::set<std::string> result; |
156 for (FeatureMap::const_iterator iter = features_.begin(); | 187 for (FeatureMap::const_iterator iter = features_.begin(); |
157 iter != features_.end(); ++iter) { | 188 iter != features_.end(); ++iter) { |
158 result.insert(iter->first); | 189 result.insert(iter->first); |
159 } | 190 } |
160 return result; | 191 return result; |
161 } | 192 } |
162 | 193 |
163 Feature* BaseFeatureProvider::GetFeature(const std::string& name) { | 194 Feature* BaseFeatureProvider::GetFeature(const std::string& name) { |
164 FeatureMap::iterator iter = features_.find(name); | 195 FeatureMap::iterator iter = features_.find(name); |
165 if (iter != features_.end()) | 196 if (iter != features_.end()) |
166 return iter->second.get(); | 197 return iter->second.get(); |
167 else | 198 else |
168 return NULL; | 199 return NULL; |
169 } | 200 } |
170 | 201 |
171 } // namespace | 202 } // namespace |
OLD | NEW |