1 // Copyright (c) 2012 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.
5 #include "chrome/renderer/extensions/chrome_v8_context.h"
7 #include "base/logging.h"
8 #include "base/memory/scoped_ptr.h"
9 #include "base/strings/string_split.h"
10 #include "base/values.h"
11 #include "chrome/common/extensions/api/extension_api.h"
12 #include "chrome/common/extensions/extension.h"
13 #include "chrome/common/extensions/extension_set.h"
14 #include "chrome/common/extensions/features/base_feature_provider.h"
15 #include "chrome/renderer/extensions/chrome_v8_extension.h"
16 #include "chrome/renderer/extensions/module_system.h"
17 #include "chrome/renderer/extensions/user_script_slave.h"
18 #include "content/public/renderer/render_view.h"
19 #include "content/public/renderer/v8_value_converter.h"
20 #include "third_party/WebKit/public/web/WebFrame.h"
21 #include "third_party/WebKit/public/web/WebScopedMicrotaskSuppression.h"
22 #include "third_party/WebKit/public/web/WebView.h"
23 #include "v8/include/v8.h"
25 using content::V8ValueConverter;
27 namespace extensions {
29 ChromeV8Context::ChromeV8Context(v8::Handle<v8::Context> v8_context,
30 WebKit::WebFrame* web_frame,
31 const Extension* extension,
32 Feature::Context context_type)
33 : v8_context_(v8_context),
34 web_frame_(web_frame),
35 extension_(extension),
36 context_type_(context_type),
38 isolate_(v8_context->GetIsolate()) {
39 VLOG(1) << "Created context:\n"
40 << " extension id: " << GetExtensionID() << "\n"
41 << " frame: " << web_frame_ << "\n"
42 << " context type: " << GetContextTypeDescription();
45 ChromeV8Context::~ChromeV8Context() {
46 VLOG(1) << "Destroyed context for extension\n"
47 << " extension id: " << GetExtensionID();
51 void ChromeV8Context::Invalidate() {
55 module_system_->Invalidate();
60 std::string ChromeV8Context::GetExtensionID() const {
61 return extension_.get() ? extension_->id() : std::string();
64 content::RenderView* ChromeV8Context::GetRenderView() const {
65 if (web_frame_ && web_frame_->view())
66 return content::RenderView::FromWebView(web_frame_->view());
71 GURL ChromeV8Context::GetURL() const {
73 UserScriptSlave::GetDataSourceURLForFrame(web_frame_) : GURL();
76 v8::Local<v8::Value> ChromeV8Context::CallFunction(
77 v8::Handle<v8::Function> function,
79 v8::Handle<v8::Value> argv[]) const {
80 v8::HandleScope handle_scope(isolate());
81 v8::Context::Scope scope(v8_context());
83 WebKit::WebScopedMicrotaskSuppression suppression;
85 return handle_scope.Close(v8::Undefined());
87 v8::Handle<v8::Object> global = v8_context()->Global();
89 return handle_scope.Close(function->Call(global, argc, argv));
90 return handle_scope.Close(
91 web_frame_->callFunctionEvenIfScriptDisabled(function,
97 bool ChromeV8Context::IsAnyFeatureAvailableToContext(
98 const std::string& api_name) {
99 return ExtensionAPI::GetSharedInstance()->IsAnyFeatureAvailableToContext(
103 UserScriptSlave::GetDataSourceURLForFrame(web_frame_));
106 Feature::Availability ChromeV8Context::GetAvailability(
107 const std::string& api_name) {
108 return ExtensionAPI::GetSharedInstance()->IsAvailable(api_name,
114 void ChromeV8Context::DispatchOnUnloadEvent() {
115 module_system_->CallModuleMethod("unload_event", "dispatch");
118 std::string ChromeV8Context::GetContextTypeDescription() {
119 switch (context_type_) {
120 case Feature::UNSPECIFIED_CONTEXT: return "UNSPECIFIED";
121 case Feature::BLESSED_EXTENSION_CONTEXT: return "BLESSED_EXTENSION";
122 case Feature::UNBLESSED_EXTENSION_CONTEXT: return "UNBLESSED_EXTENSION";
123 case Feature::CONTENT_SCRIPT_CONTEXT: return "CONTENT_SCRIPT";
124 case Feature::WEB_PAGE_CONTEXT: return "WEB_PAGE";
127 return std::string();
130 ChromeV8Context* ChromeV8Context::GetContext() {
134 void ChromeV8Context::OnResponseReceived(const std::string& name,
137 const base::ListValue& response,
138 const std::string& error) {
139 v8::HandleScope handle_scope(isolate());
141 scoped_ptr<V8ValueConverter> converter(V8ValueConverter::create());
142 v8::Handle<v8::Value> argv[] = {
143 v8::Integer::New(request_id),
144 v8::String::New(name.c_str()),
145 v8::Boolean::New(success),
146 converter->ToV8Value(&response, v8_context_.NewHandle(isolate())),
147 v8::String::New(error.c_str())
150 v8::Handle<v8::Value> retval = module_system_->CallModuleMethod(
151 "sendRequest", "handleResponse", arraysize(argv), argv);
153 // In debug, the js will validate the callback parameters and return a
154 // string if a validation error has occured.
155 if (DCHECK_IS_ON()) {
156 if (!retval.IsEmpty() && !retval->IsUndefined()) {
157 std::string error = *v8::String::AsciiValue(retval);
158 DCHECK(false) << error;
163 } // namespace extensions