Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/common/extensions/features/complex_feature_provider.h" | |
| 6 | |
| 7 #include "base/json/json_reader.h" | |
| 8 #include "base/lazy_instance.h" | |
| 9 #include "chrome/common/extensions/features/complex_feature.h" | |
| 10 #include "chrome/common/extensions/features/permission_feature.h" | |
| 11 #include "chrome/common/extensions/features/simple_feature_provider.h" | |
| 12 #include "grit/common_resources.h" | |
| 13 #include "ui/base/resource/resource_bundle.h" | |
| 14 | |
| 15 | |
| 16 namespace extensions { | |
| 17 | |
| 18 namespace { | |
| 19 | |
| 20 struct Static { | |
| 21 Static() | |
| 22 : permission_features( | |
| 23 LoadProvider("permissions", | |
| 24 IDR_EXTENSION_PERMISSION_FEATURES)) { | |
| 25 } | |
| 26 | |
| 27 scoped_ptr<ComplexFeatureProvider> permission_features; | |
| 28 | |
| 29 private: | |
| 30 scoped_ptr<ComplexFeatureProvider> LoadProvider( | |
| 31 const std::string& debug_string, int resource_id) { | |
| 32 std::string manifest_features = | |
| 33 ResourceBundle::GetSharedInstance().GetRawDataResource( | |
| 34 resource_id).as_string(); | |
| 35 int error_code = 0; | |
| 36 std::string error_message; | |
| 37 Value* value = base::JSONReader::ReadAndReturnError( | |
| 38 manifest_features, base::JSON_PARSE_RFC, | |
| 39 &error_code, &error_message); | |
| 40 CHECK(value) << "Could not load features: " << debug_string << " " | |
| 41 << error_message; | |
| 42 CHECK(value->IsType(Value::TYPE_DICTIONARY)) << debug_string; | |
| 43 scoped_ptr<DictionaryValue> dictionary_value( | |
| 44 static_cast<DictionaryValue*>(value)); | |
| 45 return scoped_ptr<ComplexFeatureProvider>( | |
|
not at google - send to devlin
2012/12/12 17:42:41
you can use make_scoped_ptr here and avoid templat
justinlin
2012/12/14 12:26:26
Done.
| |
| 46 new ComplexFeatureProvider(dictionary_value.get())); | |
| 47 } | |
| 48 }; | |
| 49 | |
| 50 base::LazyInstance<Static> g_static = LAZY_INSTANCE_INITIALIZER; | |
| 51 | |
| 52 } // namespace | |
| 53 | |
| 54 ComplexFeatureProvider::ComplexFeatureProvider(DictionaryValue* root) { | |
| 55 for (DictionaryValue::Iterator iter(*root); iter.HasNext(); iter.Advance()) { | |
| 56 if (iter.value().GetType() != Value::TYPE_LIST) { | |
|
not at google - send to devlin
2012/12/12 17:42:41
It think it'd be simpler to have a single point fo
justinlin
2012/12/14 12:26:26
Done.
| |
| 57 continue; | |
| 58 } | |
| 59 | |
| 60 linked_ptr<ComplexFeature> feature(new ComplexFeature()); | |
|
not at google - send to devlin
2012/12/12 17:42:41
delay construction of this and collect simplefeatu
justinlin
2012/12/14 12:26:26
Done.
| |
| 61 feature->set_name(iter.key()); | |
| 62 | |
| 63 const ListValue* list = static_cast<const ListValue*>(&iter.value()); | |
| 64 if (list->GetSize() == 0) | |
| 65 continue; | |
|
not at google - send to devlin
2012/12/12 17:42:41
I'm not sure it makes sense to have an empty list.
justinlin
2012/12/14 12:26:26
This is from "file input" though. Do we want to cr
not at google - send to devlin
2012/12/14 19:10:42
Those files being malformed implies a bug in Chrom
justinlin
2012/12/14 21:07:52
Done. OK, added CHECK. If users aren't supposed to
| |
| 66 | |
| 67 // Parse and add all SimpleFeatures from the list. | |
| 68 for (ListValue::const_iterator list_iter = list->begin(); | |
| 69 list_iter != list->end(); ++list_iter) { | |
| 70 if ((*list_iter)->GetType() != Value::TYPE_DICTIONARY) { | |
| 71 LOG(ERROR) << iter.key() << ": Feature rules must be dictionaries."; | |
| 72 continue; | |
| 73 } | |
| 74 | |
| 75 scoped_ptr<PermissionFeature> simple_feature(new PermissionFeature()); | |
| 76 simple_feature->set_name(iter.key()); | |
| 77 simple_feature->Parse(static_cast<const DictionaryValue*>(*list_iter)); | |
| 78 | |
| 79 if (simple_feature->extension_types()->empty()) { | |
| 80 LOG(ERROR) << iter.key() << ": Simple features must specify at least " | |
| 81 << "one value for extension_types."; | |
| 82 continue; | |
|
not at google - send to devlin
2012/12/12 17:42:41
does SimpleFeature not do this validation itself?
justinlin
2012/12/14 12:26:26
Done.
| |
| 83 } | |
| 84 | |
| 85 if (!simple_feature->contexts()->empty()) { | |
| 86 LOG(ERROR) << iter.key() << ": Simple features do not support " | |
| 87 << "contexts."; | |
| 88 continue; | |
| 89 } | |
|
not at google - send to devlin
2012/12/12 17:42:41
why is that? Btw make this DCHECK so that we notic
justinlin
2012/12/14 12:26:26
Hmm.. I'm not sure if I want to attempt to move th
| |
| 90 | |
| 91 feature->AddSimpleFeature(simple_feature.release()); | |
| 92 } | |
| 93 | |
| 94 features_[iter.key()] = feature; | |
| 95 } | |
| 96 } | |
| 97 | |
| 98 ComplexFeatureProvider::~ComplexFeatureProvider() {} | |
| 99 | |
| 100 // static | |
| 101 ComplexFeatureProvider* ComplexFeatureProvider::GetPermissionFeatures() { | |
| 102 return g_static.Get().permission_features.get(); | |
| 103 } | |
| 104 | |
| 105 std::set<std::string> ComplexFeatureProvider::GetAllFeatureNames() const { | |
| 106 std::set<std::string> result; | |
| 107 for (FeatureMap::const_iterator iter = features_.begin(); | |
| 108 iter != features_.end(); ++iter) { | |
| 109 result.insert(iter->first); | |
| 110 } | |
| 111 | |
| 112 // Append all the SimpleFeatures as well. | |
| 113 std::set<std::string> simple_result = | |
| 114 SimpleFeatureProvider::GetPermissionFeatures()->GetAllFeatureNames(); | |
| 115 result.insert(simple_result.begin(), simple_result.end()); | |
| 116 | |
| 117 return result; | |
| 118 } | |
| 119 | |
| 120 Feature* ComplexFeatureProvider::GetFeature(const std::string& name) { | |
| 121 FeatureMap::iterator iter = features_.find(name); | |
| 122 if (iter != features_.end()) | |
| 123 return iter->second.get(); | |
| 124 else | |
| 125 return SimpleFeatureProvider::GetPermissionFeatures()->GetFeature(name); | |
|
not at google - send to devlin
2012/12/12 17:42:41
See comment about about not spreading load
justinlin
2012/12/14 12:26:26
Done.
| |
| 126 } | |
| 127 | |
| 128 } // namespace | |
| OLD | NEW |