| 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/complex_feature.h" |
| 10 #include "chrome/common/extensions/features/manifest_feature.h" | 10 #include "chrome/common/extensions/features/manifest_feature.h" |
| 11 #include "chrome/common/extensions/features/permission_feature.h" | 11 #include "chrome/common/extensions/features/permission_feature.h" |
| 12 #include "grit/common_resources.h" | 12 #include "grit/common_resources.h" |
| 13 #include "ui/base/resource/resource_bundle.h" | 13 #include "ui/base/resource/resource_bundle.h" |
| 14 | 14 |
| 15 namespace extensions { | 15 namespace extensions { |
| 16 | 16 |
| 17 namespace { | 17 namespace { |
| 18 | 18 |
| 19 template<class FeatureClass> | 19 template<class FeatureClass> |
| 20 SimpleFeature* CreateFeature() { | 20 Feature* CreateFeature(Feature* feature) { |
| 21 return new FeatureClass(); | 21 return new FeatureClass(feature); |
| 22 } | 22 } |
| 23 | 23 |
| 24 struct Static { | 24 struct Static { |
| 25 Static() | 25 Static() |
| 26 : manifest_features( | 26 : manifest_features( |
| 27 LoadProvider("manifest", | 27 LoadProvider("manifest", |
| 28 &CreateFeature<ManifestFeature>, | 28 &CreateFeature<ManifestFeature>, |
| 29 IDR_EXTENSION_MANIFEST_FEATURES)), | 29 IDR_EXTENSION_MANIFEST_FEATURES)), |
| 30 permission_features( | 30 permission_features( |
| 31 LoadProvider("permissions", | 31 LoadProvider("permissions", |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 79 return true; | 79 return true; |
| 80 } | 80 } |
| 81 | 81 |
| 82 base::LazyInstance<Static> g_static = LAZY_INSTANCE_INITIALIZER; | 82 base::LazyInstance<Static> g_static = LAZY_INSTANCE_INITIALIZER; |
| 83 | 83 |
| 84 } // namespace | 84 } // namespace |
| 85 | 85 |
| 86 SimpleFeatureProvider::SimpleFeatureProvider(const DictionaryValue& root, | 86 SimpleFeatureProvider::SimpleFeatureProvider(const DictionaryValue& root, |
| 87 FeatureFactory factory) | 87 FeatureFactory factory) |
| 88 : factory_(factory ? factory : | 88 : factory_(factory ? factory : |
| 89 static_cast<FeatureFactory>(&CreateFeature<SimpleFeature>)) { | 89 static_cast<FeatureFactory>(&CreateFeature<ManifestFeature>)) { |
| 90 for (DictionaryValue::Iterator iter(root); iter.HasNext(); iter.Advance()) { | 90 for (DictionaryValue::Iterator iter(root); iter.HasNext(); iter.Advance()) { |
| 91 if (iter.value().GetType() == Value::TYPE_DICTIONARY) { | 91 if (iter.value().GetType() == Value::TYPE_DICTIONARY) { |
| 92 linked_ptr<SimpleFeature> feature((*factory_)()); | 92 scoped_ptr<SimpleFeature> simple_feature(new SimpleFeature()); |
| 93 | 93 |
| 94 if (!ParseFeature(static_cast<const DictionaryValue*>(&iter.value()), | 94 if (!ParseFeature(static_cast<const DictionaryValue*>(&iter.value()), |
| 95 iter.key(), | 95 iter.key(), |
| 96 feature.get())) | 96 simple_feature.get())) |
| 97 continue; | 97 continue; |
| 98 | 98 |
| 99 linked_ptr<Feature> feature((*factory_)(simple_feature.release())); |
| 99 features_[iter.key()] = feature; | 100 features_[iter.key()] = feature; |
| 100 } else if (iter.value().GetType() == Value::TYPE_LIST) { | 101 } else if (iter.value().GetType() == Value::TYPE_LIST) { |
| 101 // This is a complex feature. | 102 // This is a complex feature. |
| 102 const ListValue* list = static_cast<const ListValue*>(&iter.value()); | 103 const ListValue* list = static_cast<const ListValue*>(&iter.value()); |
| 103 CHECK_GT(list->GetSize(), 0UL); | 104 CHECK_GT(list->GetSize(), 0UL); |
| 104 | 105 |
| 105 scoped_ptr<ComplexFeature::FeatureList> features( | 106 scoped_ptr<ComplexFeature::FeatureList> features( |
| 106 new ComplexFeature::FeatureList()); | 107 new ComplexFeature::FeatureList()); |
| 107 | 108 |
| 108 // Parse and add all SimpleFeatures from the list. | 109 // Parse and add all SimpleFeatures from the list. |
| 109 for (ListValue::const_iterator list_iter = list->begin(); | 110 for (ListValue::const_iterator list_iter = list->begin(); |
| 110 list_iter != list->end(); ++list_iter) { | 111 list_iter != list->end(); ++list_iter) { |
| 111 if ((*list_iter)->GetType() != Value::TYPE_DICTIONARY) { | 112 if ((*list_iter)->GetType() != Value::TYPE_DICTIONARY) { |
| 112 LOG(ERROR) << iter.key() << ": Feature rules must be dictionaries."; | 113 LOG(ERROR) << iter.key() << ": Feature rules must be dictionaries."; |
| 113 continue; | 114 continue; |
| 114 } | 115 } |
| 115 | 116 |
| 116 scoped_ptr<SimpleFeature> feature((*factory_)()); | 117 scoped_ptr<SimpleFeature> feature(new SimpleFeature()); |
| 117 if (!ParseFeature(static_cast<const DictionaryValue*>(*list_iter), | 118 if (!ParseFeature(static_cast<const DictionaryValue*>(*list_iter), |
| 118 iter.key(), | 119 iter.key(), |
| 119 feature.get())) | 120 feature.get())) |
| 120 continue; | 121 continue; |
| 121 | 122 |
| 122 features->push_back(feature.release()); | 123 features->push_back((*factory_)(feature.release())); |
| 123 } | 124 } |
| 124 | 125 |
| 125 linked_ptr<ComplexFeature> feature(new ComplexFeature(features.Pass())); | 126 linked_ptr<ComplexFeature> feature(new ComplexFeature(features.Pass())); |
| 126 feature->set_name(iter.key()); | 127 feature->set_name(iter.key()); |
| 127 | 128 |
| 128 features_[iter.key()] = feature; | 129 features_[iter.key()] = feature; |
| 129 } else { | 130 } else { |
| 130 LOG(ERROR) << iter.key() << ": Feature description must be dictionary or" | 131 LOG(ERROR) << iter.key() << ": Feature description must be dictionary or" |
| 131 << " list of dictionaries."; | 132 << " list of dictionaries."; |
| 132 } | 133 } |
| (...skipping 24 matching lines...) Expand all Loading... |
| 157 | 158 |
| 158 Feature* SimpleFeatureProvider::GetFeature(const std::string& name) { | 159 Feature* SimpleFeatureProvider::GetFeature(const std::string& name) { |
| 159 FeatureMap::iterator iter = features_.find(name); | 160 FeatureMap::iterator iter = features_.find(name); |
| 160 if (iter != features_.end()) | 161 if (iter != features_.end()) |
| 161 return iter->second.get(); | 162 return iter->second.get(); |
| 162 else | 163 else |
| 163 return NULL; | 164 return NULL; |
| 164 } | 165 } |
| 165 | 166 |
| 166 } // namespace | 167 } // namespace |
| OLD | NEW |