Chromium Code Reviews| Index: chrome/common/extensions/api/extension_api.cc |
| diff --git a/chrome/common/extensions/api/extension_api.cc b/chrome/common/extensions/api/extension_api.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..3beb13bc7c80ae6e71c2615904617eee9019a776 |
| --- /dev/null |
| +++ b/chrome/common/extensions/api/extension_api.cc |
| @@ -0,0 +1,88 @@ |
| +// Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "chrome/common/extensions/api/extension_api.h" |
| + |
| +#include <string> |
| +#include <vector> |
| + |
| +#include "base/json/json_reader.h" |
| +#include "base/logging.h" |
| +#include "base/string_split.h" |
| +#include "base/string_util.h" |
| +#include "base/values.h" |
| +#include "grit/common_resources.h" |
| +#include "ui/base/resource/resource_bundle.h" |
| + |
| +namespace extensions { |
| + |
| +// static |
| +ExtensionAPI* ExtensionAPI::GetInstance() { |
| + return Singleton<ExtensionAPI>::get(); |
| +} |
| + |
| +ExtensionAPI::ExtensionAPI() { |
| + const bool kAllowTrailingCommas = false; |
| + std::string error_message; |
| + scoped_ptr<Value> temp_value( |
| + base::JSONReader::ReadAndReturnError( |
|
Matt Perry
2011/11/11 20:07:19
since this is used in the renderer, doesn't this m
Aaron Boodman
2011/11/11 23:12:02
In the latest patch, I've changed the place where
Matt Perry
2011/11/11 23:35:24
awesome
|
| + ResourceBundle::GetSharedInstance().GetRawDataResource( |
| + IDR_EXTENSION_API_JSON).as_string(), |
| + kAllowTrailingCommas, |
| + NULL, // error code |
| + &error_message)); |
| + CHECK(temp_value.get()) << error_message; |
| + CHECK(temp_value->IsType(base::Value::TYPE_LIST)) |
| + << "Top-level node in extension API is not dictionary"; |
| + |
| + value_.reset(static_cast<base::ListValue*>(temp_value.release())); |
| +} |
| + |
| +bool ExtensionAPI::IsFullNameUnprivileged(const std::string& full_name) const { |
| + std::vector<std::string> name_space; |
| + base::SplitString(full_name, '.', &name_space); |
| + std::string name = name_space.back(); |
| + name_space.pop_back(); |
| + |
| + base::DictionaryValue* name_space_node = |
| + FindListItem(value_.get(), "namespace", JoinString(name_space, '.')); |
| + CHECK(name_space_node); |
| + |
| + return IsChildNameUnprivileged(name_space_node, "functions", name) || |
| + IsChildNameUnprivileged(name_space_node, "events", name); |
| +} |
| + |
| +DictionaryValue* ExtensionAPI::FindListItem( |
| + base::ListValue* list, |
| + const std::string& property_name, |
| + const std::string& property_value) const { |
| + for (size_t i = 0; i < list->GetSize(); ++i) { |
| + DictionaryValue* item = NULL; |
| + CHECK(list->GetDictionary(i, &item)); |
| + std::string value; |
| + if (item->GetString(property_name, &value) && value == property_value) |
| + return item; |
| + } |
| + |
| + return NULL; |
| +} |
| + |
| +bool ExtensionAPI::IsChildNameUnprivileged( |
| + DictionaryValue* name_space_node, |
| + const std::string& child_kind, |
| + const std::string& child_name) const { |
| + ListValue* child_list = NULL; |
| + name_space_node->GetList(child_kind, &child_list); |
| + if (!child_list) |
| + return false; |
| + |
| + bool unprivileged = false; |
| + DictionaryValue* child = FindListItem(child_list, "name", child_name); |
| + if (!child || !child->GetBoolean("unprivileged", &unprivileged)) |
| + return false; |
| + |
| + return unprivileged; |
| +} |
| + |
| +} |