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/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; | 96 scoped_ptr<Feature> feature; |
83 | 97 |
84 DictionaryValue* description = NULL; | 98 DictionaryValue* description = NULL; |
85 if (root_->GetDictionary(name, &description)) | 99 if (root_->GetDictionary(name, &description)) { |
86 feature = Feature::Parse(description); | 100 feature.reset((*factory_)()); |
87 | 101 feature->set_name(name); |
88 if (!feature.get()) { | 102 feature->Parse(description); |
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>(); | |
93 } | 103 } |
94 | 104 |
95 if (feature->extension_types()->empty()) { | 105 if (feature->extension_types()->empty()) { |
96 LOG(ERROR) << name << ": Simple features must specify atleast one value " | 106 LOG(ERROR) << name << ": Simple features must specify atleast one value " |
97 << "for extension_types."; | 107 << "for extension_types."; |
98 return scoped_ptr<Feature>(); | 108 return scoped_ptr<Feature>(); |
99 } | 109 } |
100 | 110 |
101 if (!feature->contexts()->empty()) { | 111 if (!feature->contexts()->empty()) { |
102 LOG(ERROR) << name << ": Simple features do not support contexts."; | 112 LOG(ERROR) << name << ": Simple features do not support contexts."; |
103 return scoped_ptr<Feature>(); | 113 return scoped_ptr<Feature>(); |
104 } | 114 } |
105 | 115 |
106 return feature.Pass(); | 116 return feature.Pass(); |
107 } | 117 } |
108 | 118 |
109 } // namespace | 119 } // namespace |
OLD | NEW |