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

Unified Diff: chrome/renderer/extensions/chrome_v8_extension.cc

Issue 8540012: Enable extension APIs for content scripts. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: 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
Index: chrome/renderer/extensions/chrome_v8_extension.cc
diff --git a/chrome/renderer/extensions/chrome_v8_extension.cc b/chrome/renderer/extensions/chrome_v8_extension.cc
index 274ccac46f98a63b074d3c2a96c4906784e250b8..6933c67469faef38389679e84b310f98ec6c84e5 100644
--- a/chrome/renderer/extensions/chrome_v8_extension.cc
+++ b/chrome/renderer/extensions/chrome_v8_extension.cc
@@ -10,6 +10,7 @@
#include "base/string_util.h"
#include "chrome/common/extensions/extension.h"
#include "chrome/common/extensions/extension_set.h"
+#include "chrome/common/extensions/api/extension_api.h"
#include "chrome/renderer/extensions/chrome_v8_context.h"
#include "chrome/renderer/extensions/extension_dispatcher.h"
#include "content/public/renderer/render_view.h"
@@ -18,6 +19,7 @@
#include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h"
#include "ui/base/resource/resource_bundle.h"
+using extensions::ExtensionAPI;
using WebKit::WebFrame;
using WebKit::WebView;
@@ -124,21 +126,43 @@ const Extension* ChromeV8Extension::GetExtensionForCurrentRenderView() const {
return extensions->GetByURL(url);
}
-bool ChromeV8Extension::CheckPermissionForCurrentRenderView(
+bool ChromeV8Extension::CheckCurrentContextAccessToExtensionAPI(
const std::string& function_name) const {
- const ::Extension* extension = GetExtensionForCurrentRenderView();
- if (extension &&
- extension_dispatcher_->IsExtensionActive(extension->id()) &&
- extension->HasAPIPermission(function_name))
- return true;
-
- static const char kMessage[] =
- "You do not have permission to use '%s'. Be sure to declare"
- " in your manifest what permissions you need.";
- std::string error_msg = base::StringPrintf(kMessage, function_name.c_str());
-
- v8::ThrowException(v8::Exception::Error(v8::String::New(error_msg.c_str())));
- return false;
+ ChromeV8Context* context =
+ extension_dispatcher_->v8_context_set().GetCurrent();
+ if (!context) {
+ DLOG(ERROR) << "Not in a v8::Context";
+ return false;
+ }
+
+ const ::Extension* extension = NULL;
+ if (!context->extension_id().empty()) {
+ extension =
+ extension_dispatcher_->extensions()->GetByID(context->extension_id());
+ }
+
+ if (!extension || !extension->HasAPIPermission(function_name)) {
+ static const char kMessage[] =
+ "You do not have permission to use '%s'. Be sure to declare"
+ " in your manifest what permissions you need.";
+ std::string error_msg = base::StringPrintf(kMessage, function_name.c_str());
+ v8::ThrowException(
+ v8::Exception::Error(v8::String::New(error_msg.c_str())));
+ return false;
+ }
+
+ ExtensionAPI* api = ExtensionAPI::GetInstance();
+ if (!extension_dispatcher_->IsExtensionActive(extension->id()) &&
+ !api->IsFullNameUnprivileged(function_name)) {
+ static const char kMessage[] =
+ "%s can only be used in an extension process.";
+ std::string error_msg = base::StringPrintf(kMessage, function_name.c_str());
+ v8::ThrowException(
+ v8::Exception::Error(v8::String::New(error_msg.c_str())));
+ return false;
+ }
+
+ return true;
}
v8::Handle<v8::FunctionTemplate>

Powered by Google App Engine
This is Rietveld 408576698