OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 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 "extensions/common/features/json_feature_provider_source.h" |
| 6 |
| 7 #include <string> |
| 8 |
| 9 #include "base/json/json_reader.h" |
| 10 #include "base/logging.h" |
| 11 #include "ui/base/resource/resource_bundle.h" |
| 12 |
| 13 namespace extensions { |
| 14 |
| 15 JSONFeatureProviderSource::JSONFeatureProviderSource(const std::string& name) |
| 16 : name_(name) { |
| 17 } |
| 18 |
| 19 JSONFeatureProviderSource::~JSONFeatureProviderSource() { |
| 20 } |
| 21 |
| 22 void JSONFeatureProviderSource::LoadJSON(int resource_id) { |
| 23 const std::string& features_file = ResourceBundle::GetSharedInstance() |
| 24 .GetRawDataResource(resource_id) |
| 25 .as_string(); |
| 26 int error_code = 0; |
| 27 std::string error_message; |
| 28 scoped_ptr<base::Value> value(base::JSONReader::ReadAndReturnError( |
| 29 features_file, base::JSON_PARSE_RFC, &error_code, &error_message)); |
| 30 DCHECK(value) << "Could not load features: " << name_ << " " << error_message; |
| 31 |
| 32 scoped_ptr<base::DictionaryValue> value_as_dict; |
| 33 if (value) { |
| 34 CHECK(value->IsType(base::Value::TYPE_DICTIONARY)) << name_; |
| 35 value_as_dict.reset(static_cast<base::DictionaryValue*>(value.release())); |
| 36 } else { |
| 37 // There was some error loading the features file. |
| 38 // http://crbug.com/176381 |
| 39 value_as_dict.reset(new base::DictionaryValue()); |
| 40 } |
| 41 |
| 42 // Ensure there are no key collisions. |
| 43 for (base::DictionaryValue::Iterator iter(*value_as_dict); !iter.IsAtEnd(); |
| 44 iter.Advance()) { |
| 45 if (dictionary_.GetWithoutPathExpansion(iter.key(), NULL)) |
| 46 LOG(FATAL) << "Key " << iter.key() << " is defined in " << name_ |
| 47 << " JSON feature files more than once."; |
| 48 } |
| 49 |
| 50 // Merge. |
| 51 dictionary_.MergeDictionary(value_as_dict.get()); |
| 52 } |
| 53 |
| 54 } // namespace |
OLD | NEW |