1 // Copyright 2014 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 "extensions/renderer/script_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 "content/public/renderer/render_view.h"
12 #include "content/public/renderer/v8_value_converter.h"
13 #include "extensions/common/extension.h"
14 #include "extensions/common/extension_api.h"
15 #include "extensions/common/extension_urls.h"
16 #include "extensions/common/features/base_feature_provider.h"
17 #include "third_party/WebKit/public/web/WebDataSource.h"
18 #include "third_party/WebKit/public/web/WebFrame.h"
19 #include "third_party/WebKit/public/web/WebScopedMicrotaskSuppression.h"
20 #include "third_party/WebKit/public/web/WebView.h"
21 #include "v8/include/v8.h"
23 using content::V8ValueConverter
;
25 namespace extensions
{
27 ScriptContext::ScriptContext(const v8::Handle
<v8::Context
>& v8_context
,
28 blink::WebFrame
* web_frame
,
29 const Extension
* extension
,
30 Feature::Context context_type
)
31 : v8_context_(v8_context
),
32 web_frame_(web_frame
),
33 extension_(extension
),
34 context_type_(context_type
),
36 isolate_(v8_context
->GetIsolate()) {
37 VLOG(1) << "Created context:\n"
38 << " extension id: " << GetExtensionID() << "\n"
39 << " frame: " << web_frame_
<< "\n"
40 << " context type: " << GetContextTypeDescription();
43 ScriptContext::~ScriptContext() {
44 VLOG(1) << "Destroyed context for extension\n"
45 << " extension id: " << GetExtensionID();
49 void ScriptContext::Invalidate() {
53 module_system_
->Invalidate();
58 std::string
ScriptContext::GetExtensionID() const {
59 return extension_
.get() ? extension_
->id() : std::string();
62 content::RenderView
* ScriptContext::GetRenderView() const {
63 if (web_frame_
&& web_frame_
->view())
64 return content::RenderView::FromWebView(web_frame_
->view());
69 v8::Local
<v8::Value
> ScriptContext::CallFunction(
70 v8::Handle
<v8::Function
> function
,
72 v8::Handle
<v8::Value
> argv
[]) const {
73 v8::EscapableHandleScope
handle_scope(isolate());
74 v8::Context::Scope
scope(v8_context());
76 blink::WebScopedMicrotaskSuppression suppression
;
78 return handle_scope
.Escape(
79 v8::Local
<v8::Primitive
>(v8::Undefined(isolate())));
82 v8::Handle
<v8::Object
> global
= v8_context()->Global();
84 return handle_scope
.Escape(function
->Call(global
, argc
, argv
));
85 return handle_scope
.Escape(
86 v8::Local
<v8::Value
>(web_frame_
->callFunctionEvenIfScriptDisabled(
87 function
, global
, argc
, argv
)));
90 Feature::Availability
ScriptContext::GetAvailability(
91 const std::string
& api_name
) {
92 // Hack: Hosted apps should have the availability of messaging APIs based on
93 // the URL of the page (which might have access depending on some extension
94 // with externally_connectable), not whether the app has access to messaging
96 const Extension
* extension
= extension_
.get();
97 if (extension
&& extension
->is_hosted_app() &&
98 (api_name
== "runtime.connect" || api_name
== "runtime.sendMessage")) {
101 return ExtensionAPI::GetSharedInstance()->IsAvailable(
102 api_name
, extension
, context_type_
, GetURL());
105 void ScriptContext::DispatchEvent(const char* event_name
,
106 v8::Handle
<v8::Array
> args
) const {
107 v8::HandleScope
handle_scope(isolate());
108 v8::Context::Scope
context_scope(v8_context());
110 v8::Handle
<v8::Value
> argv
[] = {
111 v8::String::NewFromUtf8(isolate(), event_name
), args
};
112 module_system_
->CallModuleMethod(
113 kEventBindings
, "dispatchEvent", arraysize(argv
), argv
);
116 void ScriptContext::DispatchOnUnloadEvent() {
117 module_system_
->CallModuleMethod("unload_event", "dispatch");
120 std::string
ScriptContext::GetContextTypeDescription() {
121 switch (context_type_
) {
122 case Feature::UNSPECIFIED_CONTEXT
:
123 return "UNSPECIFIED";
124 case Feature::BLESSED_EXTENSION_CONTEXT
:
125 return "BLESSED_EXTENSION";
126 case Feature::UNBLESSED_EXTENSION_CONTEXT
:
127 return "UNBLESSED_EXTENSION";
128 case Feature::CONTENT_SCRIPT_CONTEXT
:
129 return "CONTENT_SCRIPT";
130 case Feature::WEB_PAGE_CONTEXT
:
132 case Feature::BLESSED_WEB_PAGE_CONTEXT
:
133 return "BLESSED_WEB_PAGE";
136 return std::string();
139 GURL
ScriptContext::GetURL() const {
140 return web_frame() ? GetDataSourceURLForFrame(web_frame()) : GURL();
143 bool ScriptContext::IsAnyFeatureAvailableToContext(const Feature
& api
) {
144 return ExtensionAPI::GetSharedInstance()->IsAnyFeatureAvailableToContext(
145 api
, extension(), context_type(), GetDataSourceURLForFrame(web_frame()));
149 GURL
ScriptContext::GetDataSourceURLForFrame(const blink::WebFrame
* frame
) {
150 // Normally we would use frame->document().url() to determine the document's
151 // URL, but to decide whether to inject a content script, we use the URL from
152 // the data source. This "quirk" helps prevents content scripts from
153 // inadvertently adding DOM elements to the compose iframe in Gmail because
154 // the compose iframe's dataSource URL is about:blank, but the document URL
155 // changes to match the parent document after Gmail document.writes into
156 // it to create the editor.
157 // http://code.google.com/p/chromium/issues/detail?id=86742
158 blink::WebDataSource
* data_source
= frame
->provisionalDataSource()
159 ? frame
->provisionalDataSource()
160 : frame
->dataSource();
162 return GURL(data_source
->request().url());
165 ScriptContext
* ScriptContext::GetContext() { return this; }
167 void ScriptContext::OnResponseReceived(const std::string
& name
,
170 const base::ListValue
& response
,
171 const std::string
& error
) {
172 v8::HandleScope
handle_scope(isolate());
174 scoped_ptr
<V8ValueConverter
> converter(V8ValueConverter::create());
175 v8::Handle
<v8::Value
> argv
[] = {
176 v8::Integer::New(isolate(), request_id
),
177 v8::String::NewFromUtf8(isolate(), name
.c_str()),
178 v8::Boolean::New(isolate(), success
),
179 converter
->ToV8Value(&response
, v8_context_
.NewHandle(isolate())),
180 v8::String::NewFromUtf8(isolate(), error
.c_str())};
182 v8::Handle
<v8::Value
> retval
= module_system()->CallModuleMethod(
183 "sendRequest", "handleResponse", arraysize(argv
), argv
);
185 // In debug, the js will validate the callback parameters and return a
186 // string if a validation error has occured.
187 DCHECK(retval
.IsEmpty() || retval
->IsUndefined())
188 << *v8::String::Utf8Value(retval
);
191 } // namespace extensions