| 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" | |
| 11 #include "grit/common_resources.h" | 9 #include "grit/common_resources.h" |
| 12 #include "ui/base/resource/resource_bundle.h" | 10 #include "ui/base/resource/resource_bundle.h" |
| 13 | 11 |
| 14 namespace extensions { | |
| 15 | |
| 16 namespace { | 12 namespace { |
| 17 | 13 |
| 18 const bool kAllowTrailingComma = false; | 14 const bool kAllowTrailingComma = false; |
| 19 | 15 |
| 20 template<class FeatureClass> | |
| 21 Feature* CreateFeature() { | |
| 22 return new FeatureClass(); | |
| 23 } | |
| 24 | |
| 25 struct Static { | 16 struct Static { |
| 26 Static() | 17 Static() |
| 27 : manifest_features( | 18 : manifest_features( |
| 28 LoadProvider("manifest", | 19 LoadProvider("manifest", IDR_EXTENSION_MANIFEST_FEATURES)), |
| 29 &CreateFeature<ManifestFeature>, | |
| 30 IDR_EXTENSION_MANIFEST_FEATURES)), | |
| 31 permission_features( | 20 permission_features( |
| 32 LoadProvider("permissions", | 21 LoadProvider("permissions", IDR_EXTENSION_PERMISSION_FEATURES)) { |
| 33 &CreateFeature<PermissionFeature>, | |
| 34 IDR_EXTENSION_PERMISSION_FEATURES)) { | |
| 35 } | 22 } |
| 36 | 23 |
| 37 scoped_ptr<SimpleFeatureProvider> manifest_features; | 24 scoped_ptr<extensions::SimpleFeatureProvider> manifest_features; |
| 38 scoped_ptr<SimpleFeatureProvider> permission_features; | 25 scoped_ptr<extensions::SimpleFeatureProvider> permission_features; |
| 39 | 26 |
| 40 private: | 27 private: |
| 41 scoped_ptr<SimpleFeatureProvider> LoadProvider( | 28 scoped_ptr<extensions::SimpleFeatureProvider> LoadProvider( |
| 42 const std::string& debug_string, | 29 const std::string& debug_string, int resource_id) { |
| 43 SimpleFeatureProvider::FeatureFactory factory, | |
| 44 int resource_id) { | |
| 45 std::string manifest_features = | 30 std::string manifest_features = |
| 46 ResourceBundle::GetSharedInstance().GetRawDataResource( | 31 ResourceBundle::GetSharedInstance().GetRawDataResource( |
| 47 resource_id).as_string(); | 32 resource_id).as_string(); |
| 48 int error_code = 0; | 33 int error_code = 0; |
| 49 std::string error_message; | 34 std::string error_message; |
| 50 Value* value = base::JSONReader::ReadAndReturnError( | 35 Value* value = base::JSONReader::ReadAndReturnError( |
| 51 manifest_features, kAllowTrailingComma, &error_code, &error_message); | 36 manifest_features, kAllowTrailingComma, &error_code, &error_message); |
| 52 CHECK(value) << "Could not load features: " << debug_string << " " | 37 CHECK(value) << "Could not load features: " << debug_string << " " |
| 53 << error_message; | 38 << error_message; |
| 54 CHECK(value->IsType(Value::TYPE_DICTIONARY)) << debug_string; | 39 CHECK(value->IsType(Value::TYPE_DICTIONARY)) << debug_string; |
| 55 scoped_ptr<DictionaryValue> dictionary_value( | 40 scoped_ptr<DictionaryValue> dictionary_value( |
| 56 static_cast<DictionaryValue*>(value)); | 41 static_cast<DictionaryValue*>(value)); |
| 57 return scoped_ptr<SimpleFeatureProvider>( | 42 return scoped_ptr<extensions::SimpleFeatureProvider>( |
| 58 new SimpleFeatureProvider(dictionary_value.Pass(), factory)); | 43 new extensions::SimpleFeatureProvider(dictionary_value.Pass())); |
| 59 } | 44 } |
| 60 }; | 45 }; |
| 61 | 46 |
| 62 base::LazyInstance<Static> g_static = LAZY_INSTANCE_INITIALIZER; | 47 base::LazyInstance<Static> g_static = LAZY_INSTANCE_INITIALIZER; |
| 63 | 48 |
| 64 } // namespace | 49 } // namespace |
| 65 | 50 |
| 66 SimpleFeatureProvider::SimpleFeatureProvider(scoped_ptr<DictionaryValue> root, | 51 namespace extensions { |
| 67 FeatureFactory factory) | 52 |
| 68 : root_(root.release()), | 53 SimpleFeatureProvider::SimpleFeatureProvider( |
| 69 factory_(factory ? factory : | 54 scoped_ptr<DictionaryValue> root) |
| 70 static_cast<FeatureFactory>(&CreateFeature<Feature>)) { | 55 : root_(root.release()) { |
| 71 } | 56 } |
| 72 | 57 |
| 73 SimpleFeatureProvider::~SimpleFeatureProvider() { | 58 SimpleFeatureProvider::~SimpleFeatureProvider() { |
| 74 } | 59 } |
| 75 | 60 |
| 76 // static | 61 // static |
| 77 SimpleFeatureProvider* SimpleFeatureProvider::GetManifestFeatures() { | 62 SimpleFeatureProvider* SimpleFeatureProvider::GetManifestFeatures() { |
| 78 return g_static.Get().manifest_features.get(); | 63 return g_static.Get().manifest_features.get(); |
| 79 } | 64 } |
| 80 | 65 |
| 81 // static | 66 // static |
| 82 SimpleFeatureProvider* SimpleFeatureProvider::GetPermissionFeatures() { | 67 SimpleFeatureProvider* SimpleFeatureProvider::GetPermissionFeatures() { |
| 83 return g_static.Get().permission_features.get(); | 68 return g_static.Get().permission_features.get(); |
| 84 } | 69 } |
| 85 | 70 |
| 86 std::set<std::string> SimpleFeatureProvider::GetAllFeatureNames() const { | 71 std::set<std::string> SimpleFeatureProvider::GetAllFeatureNames() const { |
| 87 std::set<std::string> result; | 72 std::set<std::string> result; |
| 88 for (DictionaryValue::key_iterator iter = root_->begin_keys(); | 73 for (DictionaryValue::key_iterator iter = root_->begin_keys(); |
| 89 iter != root_->end_keys(); ++iter) { | 74 iter != root_->end_keys(); ++iter) { |
| 90 result.insert(*iter); | 75 result.insert(*iter); |
| 91 } | 76 } |
| 92 return result; | 77 return result; |
| 93 } | 78 } |
| 94 | 79 |
| 95 scoped_ptr<Feature> SimpleFeatureProvider::GetFeature(const std::string& name) { | 80 scoped_ptr<Feature> SimpleFeatureProvider::GetFeature( |
| 81 const std::string& name) const { |
| 82 scoped_ptr<Feature> feature; |
| 83 |
| 96 DictionaryValue* description = NULL; | 84 DictionaryValue* description = NULL; |
| 97 if (!root_->GetDictionary(name, &description)) { | 85 if (root_->GetDictionary(name, &description)) |
| 98 LOG(ERROR) << name << ": Definition not found."; | 86 feature = Feature::Parse(description); |
| 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; |
| 99 return scoped_ptr<Feature>(); | 92 return scoped_ptr<Feature>(); |
| 100 } | 93 } |
| 101 | 94 |
| 102 scoped_ptr<Feature> feature(new Feature()); | |
| 103 feature.reset((*factory_)()); | |
| 104 feature->set_name(name); | |
| 105 feature->Parse(description); | |
| 106 | |
| 107 if (feature->extension_types()->empty()) { | 95 if (feature->extension_types()->empty()) { |
| 108 LOG(ERROR) << name << ": Simple features must specify atleast one value " | 96 LOG(ERROR) << name << ": Simple features must specify atleast one value " |
| 109 << "for extension_types."; | 97 << "for extension_types."; |
| 110 return scoped_ptr<Feature>(); | 98 return scoped_ptr<Feature>(); |
| 111 } | 99 } |
| 112 | 100 |
| 113 if (!feature->contexts()->empty()) { | 101 if (!feature->contexts()->empty()) { |
| 114 LOG(ERROR) << name << ": Simple features do not support contexts."; | 102 LOG(ERROR) << name << ": Simple features do not support contexts."; |
| 115 return scoped_ptr<Feature>(); | 103 return scoped_ptr<Feature>(); |
| 116 } | 104 } |
| 117 | 105 |
| 118 return feature.Pass(); | 106 return feature.Pass(); |
| 119 } | 107 } |
| 120 | 108 |
| 121 } // namespace | 109 } // namespace |
| OLD | NEW |