OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011 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/api/extension_api.h" |
| 6 |
| 7 #include <string> |
| 8 #include <vector> |
| 9 |
| 10 #include "base/json/json_reader.h" |
| 11 #include "base/logging.h" |
| 12 #include "base/string_split.h" |
| 13 #include "base/string_util.h" |
| 14 #include "base/values.h" |
| 15 #include "grit/common_resources.h" |
| 16 #include "ui/base/resource/resource_bundle.h" |
| 17 |
| 18 namespace extensions { |
| 19 |
| 20 // static |
| 21 ExtensionAPI* ExtensionAPI::GetInstance() { |
| 22 return Singleton<ExtensionAPI>::get(); |
| 23 } |
| 24 |
| 25 ExtensionAPI::ExtensionAPI() { |
| 26 const bool kAllowTrailingCommas = false; |
| 27 std::string error_message; |
| 28 scoped_ptr<Value> temp_value( |
| 29 base::JSONReader::ReadAndReturnError( |
| 30 ResourceBundle::GetSharedInstance().GetRawDataResource( |
| 31 IDR_EXTENSION_API_JSON).as_string(), |
| 32 kAllowTrailingCommas, |
| 33 NULL, // error code |
| 34 &error_message)); |
| 35 CHECK(temp_value.get()) << error_message; |
| 36 CHECK(temp_value->IsType(base::Value::TYPE_LIST)) |
| 37 << "Top-level node in extension API is not dictionary"; |
| 38 |
| 39 value_.reset(static_cast<base::ListValue*>(temp_value.release())); |
| 40 } |
| 41 |
| 42 ExtensionAPI::~ExtensionAPI() { |
| 43 } |
| 44 |
| 45 bool ExtensionAPI::IsPrivileged(const std::string& full_name) const { |
| 46 std::vector<std::string> name_space; |
| 47 base::SplitString(full_name, '.', &name_space); |
| 48 std::string name = name_space.back(); |
| 49 name_space.pop_back(); |
| 50 |
| 51 base::DictionaryValue* name_space_node = |
| 52 FindListItem(value_.get(), "namespace", JoinString(name_space, '.')); |
| 53 if (!name_space_node) |
| 54 return true; |
| 55 |
| 56 if (!IsChildNamePrivileged(name_space_node, "functions", name) || |
| 57 !IsChildNamePrivileged(name_space_node, "events", name)) { |
| 58 return false; |
| 59 } |
| 60 |
| 61 return true; |
| 62 } |
| 63 |
| 64 DictionaryValue* ExtensionAPI::FindListItem( |
| 65 base::ListValue* list, |
| 66 const std::string& property_name, |
| 67 const std::string& property_value) const { |
| 68 for (size_t i = 0; i < list->GetSize(); ++i) { |
| 69 DictionaryValue* item = NULL; |
| 70 CHECK(list->GetDictionary(i, &item)); |
| 71 std::string value; |
| 72 if (item->GetString(property_name, &value) && value == property_value) |
| 73 return item; |
| 74 } |
| 75 |
| 76 return NULL; |
| 77 } |
| 78 |
| 79 bool ExtensionAPI::IsChildNamePrivileged(DictionaryValue* name_space_node, |
| 80 const std::string& child_kind, |
| 81 const std::string& child_name) const { |
| 82 ListValue* child_list = NULL; |
| 83 name_space_node->GetList(child_kind, &child_list); |
| 84 if (!child_list) |
| 85 return true; |
| 86 |
| 87 bool unprivileged = false; |
| 88 DictionaryValue* child = FindListItem(child_list, "name", child_name); |
| 89 if (!child || !child->GetBoolean("unprivileged", &unprivileged)) |
| 90 return true; |
| 91 |
| 92 return !unprivileged; |
| 93 } |
| 94 |
| 95 } |
OLD | NEW |