Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(6562)

Unified Diff: chrome/common/extensions/api/extension_api.cc

Issue 8540012: Enable extension APIs for content scripts. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: rebase Created 9 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « chrome/common/extensions/api/extension_api.h ('k') | chrome/common/extensions/api/extension_api.json » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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..ba81b64b6dbfa534400d500397250df995434c75
--- /dev/null
+++ b/chrome/common/extensions/api/extension_api.cc
@@ -0,0 +1,95 @@
+// 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(
+ 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()));
+}
+
+ExtensionAPI::~ExtensionAPI() {
+}
+
+bool ExtensionAPI::IsPrivileged(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, '.'));
+ if (!name_space_node)
+ return true;
+
+ if (!IsChildNamePrivileged(name_space_node, "functions", name) ||
+ !IsChildNamePrivileged(name_space_node, "events", name)) {
+ return false;
+ }
+
+ return true;
+}
+
+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::IsChildNamePrivileged(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 true;
+
+ bool unprivileged = false;
+ DictionaryValue* child = FindListItem(child_list, "name", child_name);
+ if (!child || !child->GetBoolean("unprivileged", &unprivileged))
+ return true;
+
+ return !unprivileged;
+}
+
+}
« no previous file with comments | « chrome/common/extensions/api/extension_api.h ('k') | chrome/common/extensions/api/extension_api.json » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698