| 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/simple_feature_provider.h" | 5 #include "chrome/common/extensions/features/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/features/complex_feature.h" |
| 9 #include "chrome/common/extensions/features/manifest_feature.h" | 10 #include "chrome/common/extensions/features/manifest_feature.h" |
| 10 #include "chrome/common/extensions/features/permission_feature.h" | 11 #include "chrome/common/extensions/features/permission_feature.h" |
| 11 #include "grit/common_resources.h" | 12 #include "grit/common_resources.h" |
| 12 #include "ui/base/resource/resource_bundle.h" | 13 #include "ui/base/resource/resource_bundle.h" |
| 13 | 14 |
| 14 namespace extensions { | 15 namespace extensions { |
| 15 | 16 |
| 16 namespace { | 17 namespace { |
| 17 | 18 |
| 18 template<class FeatureClass> | 19 template<class FeatureClass> |
| 19 Feature* CreateFeature() { | 20 SimpleFeature* CreateFeature() { |
| 20 return new FeatureClass(); | 21 return new FeatureClass(); |
| 21 } | 22 } |
| 22 | 23 |
| 23 struct Static { | 24 struct Static { |
| 24 Static() | 25 Static() |
| 25 : manifest_features( | 26 : manifest_features( |
| 26 LoadProvider("manifest", | 27 LoadProvider("manifest", |
| 27 &CreateFeature<ManifestFeature>, | 28 &CreateFeature<ManifestFeature>, |
| 28 IDR_EXTENSION_MANIFEST_FEATURES)), | 29 IDR_EXTENSION_MANIFEST_FEATURES)), |
| 29 permission_features( | 30 permission_features( |
| (...skipping 17 matching lines...) Expand all Loading... |
| 47 std::string error_message; | 48 std::string error_message; |
| 48 Value* value = base::JSONReader::ReadAndReturnError( | 49 Value* value = base::JSONReader::ReadAndReturnError( |
| 49 manifest_features, base::JSON_PARSE_RFC, | 50 manifest_features, base::JSON_PARSE_RFC, |
| 50 &error_code, &error_message); | 51 &error_code, &error_message); |
| 51 CHECK(value) << "Could not load features: " << debug_string << " " | 52 CHECK(value) << "Could not load features: " << debug_string << " " |
| 52 << error_message; | 53 << error_message; |
| 53 CHECK(value->IsType(Value::TYPE_DICTIONARY)) << debug_string; | 54 CHECK(value->IsType(Value::TYPE_DICTIONARY)) << debug_string; |
| 54 scoped_ptr<DictionaryValue> dictionary_value( | 55 scoped_ptr<DictionaryValue> dictionary_value( |
| 55 static_cast<DictionaryValue*>(value)); | 56 static_cast<DictionaryValue*>(value)); |
| 56 return scoped_ptr<SimpleFeatureProvider>( | 57 return scoped_ptr<SimpleFeatureProvider>( |
| 57 new SimpleFeatureProvider(dictionary_value.get(), factory)); | 58 new SimpleFeatureProvider(*dictionary_value, factory)); |
| 58 } | 59 } |
| 59 }; | 60 }; |
| 60 | 61 |
| 62 bool ParseFeature(const DictionaryValue* value, |
| 63 const std::string& name, |
| 64 SimpleFeature* feature) { |
| 65 feature->set_name(name); |
| 66 feature->Parse(value); |
| 67 |
| 68 if (feature->extension_types()->empty()) { |
| 69 LOG(ERROR) << name << ": Simple features must specify at least one " |
| 70 << "value for extension_types."; |
| 71 return false; |
| 72 } |
| 73 |
| 74 if (!feature->GetContexts()->empty()) { |
| 75 LOG(ERROR) << name << ": Simple features do not support contexts."; |
| 76 return false; |
| 77 } |
| 78 |
| 79 return true; |
| 80 } |
| 81 |
| 61 base::LazyInstance<Static> g_static = LAZY_INSTANCE_INITIALIZER; | 82 base::LazyInstance<Static> g_static = LAZY_INSTANCE_INITIALIZER; |
| 62 | 83 |
| 63 } // namespace | 84 } // namespace |
| 64 | 85 |
| 65 SimpleFeatureProvider::SimpleFeatureProvider(DictionaryValue* root, | 86 SimpleFeatureProvider::SimpleFeatureProvider(const DictionaryValue& root, |
| 66 FeatureFactory factory) | 87 FeatureFactory factory) |
| 67 : factory_(factory ? factory : | 88 : factory_(factory ? factory : |
| 68 static_cast<FeatureFactory>(&CreateFeature<Feature>)) { | 89 static_cast<FeatureFactory>(&CreateFeature<SimpleFeature>)) { |
| 69 for (DictionaryValue::Iterator iter(*root); iter.HasNext(); iter.Advance()) { | 90 for (DictionaryValue::Iterator iter(root); iter.HasNext(); iter.Advance()) { |
| 70 if (iter.value().GetType() != Value::TYPE_DICTIONARY) { | 91 if (iter.value().GetType() == Value::TYPE_DICTIONARY) { |
| 71 LOG(ERROR) << iter.key() << ": Feature description must be dictionary."; | 92 linked_ptr<SimpleFeature> feature((*factory_)()); |
| 72 continue; | 93 |
| 94 if (!ParseFeature(static_cast<const DictionaryValue*>(&iter.value()), |
| 95 iter.key(), |
| 96 feature.get())) |
| 97 continue; |
| 98 |
| 99 features_[iter.key()] = feature; |
| 100 } else if (iter.value().GetType() == Value::TYPE_LIST) { |
| 101 // This is a complex feature. |
| 102 const ListValue* list = static_cast<const ListValue*>(&iter.value()); |
| 103 CHECK_GT(list->GetSize(), 0UL); |
| 104 |
| 105 scoped_ptr<ComplexFeature::FeatureList> features( |
| 106 new ComplexFeature::FeatureList()); |
| 107 |
| 108 // Parse and add all SimpleFeatures from the list. |
| 109 for (ListValue::const_iterator list_iter = list->begin(); |
| 110 list_iter != list->end(); ++list_iter) { |
| 111 if ((*list_iter)->GetType() != Value::TYPE_DICTIONARY) { |
| 112 LOG(ERROR) << iter.key() << ": Feature rules must be dictionaries."; |
| 113 continue; |
| 114 } |
| 115 |
| 116 scoped_ptr<SimpleFeature> feature((*factory_)()); |
| 117 if (!ParseFeature(static_cast<const DictionaryValue*>(*list_iter), |
| 118 iter.key(), |
| 119 feature.get())) |
| 120 continue; |
| 121 |
| 122 features->push_back(feature.release()); |
| 123 } |
| 124 |
| 125 linked_ptr<ComplexFeature> feature(new ComplexFeature(features.Pass())); |
| 126 feature->set_name(iter.key()); |
| 127 |
| 128 features_[iter.key()] = feature; |
| 129 } else { |
| 130 LOG(ERROR) << iter.key() << ": Feature description must be dictionary or" |
| 131 << " list of dictionaries."; |
| 73 } | 132 } |
| 74 | |
| 75 linked_ptr<Feature> feature((*factory_)()); | |
| 76 feature->set_name(iter.key()); | |
| 77 feature->Parse(static_cast<const DictionaryValue*>(&iter.value())); | |
| 78 | |
| 79 if (feature->extension_types()->empty()) { | |
| 80 LOG(ERROR) << iter.key() << ": Simple features must specify atleast one " | |
| 81 << "value for extension_types."; | |
| 82 continue; | |
| 83 } | |
| 84 | |
| 85 if (!feature->contexts()->empty()) { | |
| 86 LOG(ERROR) << iter.key() << ": Simple features do not support contexts."; | |
| 87 continue; | |
| 88 } | |
| 89 | |
| 90 features_[iter.key()] = feature; | |
| 91 } | 133 } |
| 92 } | 134 } |
| 93 | 135 |
| 94 SimpleFeatureProvider::~SimpleFeatureProvider() { | 136 SimpleFeatureProvider::~SimpleFeatureProvider() { |
| 95 } | 137 } |
| 96 | 138 |
| 97 // static | 139 // static |
| 98 SimpleFeatureProvider* SimpleFeatureProvider::GetManifestFeatures() { | 140 SimpleFeatureProvider* SimpleFeatureProvider::GetManifestFeatures() { |
| 99 return g_static.Get().manifest_features.get(); | 141 return g_static.Get().manifest_features.get(); |
| 100 } | 142 } |
| (...skipping 14 matching lines...) Expand all Loading... |
| 115 | 157 |
| 116 Feature* SimpleFeatureProvider::GetFeature(const std::string& name) { | 158 Feature* SimpleFeatureProvider::GetFeature(const std::string& name) { |
| 117 FeatureMap::iterator iter = features_.find(name); | 159 FeatureMap::iterator iter = features_.find(name); |
| 118 if (iter != features_.end()) | 160 if (iter != features_.end()) |
| 119 return iter->second.get(); | 161 return iter->second.get(); |
| 120 else | 162 else |
| 121 return NULL; | 163 return NULL; |
| 122 } | 164 } |
| 123 | 165 |
| 124 } // namespace | 166 } // namespace |
| OLD | NEW |