1 // Copyright 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 "content/renderer/web_ui_extension.h"
7 #include "base/memory/scoped_ptr.h"
8 #include "base/values.h"
9 #include "content/common/view_messages.h"
10 #include "content/public/child/v8_value_converter.h"
11 #include "content/public/common/bindings_policy.h"
12 #include "content/public/common/url_constants.h"
13 #include "content/public/renderer/render_thread.h"
14 #include "content/public/renderer/render_view.h"
15 #include "content/renderer/chrome_object_extensions_utils.h"
16 #include "content/renderer/web_ui_extension_data.h"
17 #include "gin/arguments.h"
18 #include "gin/function_template.h"
19 #include "third_party/WebKit/public/web/WebDocument.h"
20 #include "third_party/WebKit/public/web/WebKit.h"
21 #include "third_party/WebKit/public/web/WebLocalFrame.h"
22 #include "third_party/WebKit/public/web/WebView.h"
24 #include "v8/include/v8.h"
30 bool ShouldRespondToRequest(
31 blink::WebFrame
** frame_ptr
,
32 RenderView
** render_view_ptr
) {
33 blink::WebFrame
* frame
= blink::WebLocalFrame::frameForCurrentContext();
34 if (!frame
|| !frame
->view())
37 RenderView
* render_view
= RenderView::FromWebView(frame
->view());
41 GURL frame_url
= frame
->document().url();
44 (render_view
->GetEnabledBindings() & BINDINGS_POLICY_WEB_UI
) &&
45 (frame_url
.SchemeIs(kChromeUIScheme
) ||
46 frame_url
.SchemeIs(url::kDataScheme
));
52 *render_view_ptr
= render_view
;
58 // Exposes two methods:
59 // - chrome.send: Used to send messages to the browser. Requires the message
60 // name as the first argument and can have an optional second argument that
61 // should be an array.
62 // - chrome.getVariableValue: Returns value for the input variable name if such
63 // a value was set by the browser. Else will return an empty string.
64 void WebUIExtension::Install(blink::WebFrame
* frame
) {
65 v8::Isolate
* isolate
= blink::mainThreadIsolate();
66 v8::HandleScope
handle_scope(isolate
);
67 v8::Local
<v8::Context
> context
= frame
->mainWorldScriptContext();
68 if (context
.IsEmpty())
71 v8::Context::Scope
context_scope(context
);
73 v8::Local
<v8::Object
> chrome
= GetOrCreateChromeObject(isolate
,
75 chrome
->Set(gin::StringToSymbol(isolate
, "send"),
76 gin::CreateFunctionTemplate(
77 isolate
, base::Bind(&WebUIExtension::Send
))->GetFunction());
78 chrome
->Set(gin::StringToSymbol(isolate
, "getVariableValue"),
79 gin::CreateFunctionTemplate(
80 isolate
, base::Bind(&WebUIExtension::GetVariableValue
))
85 void WebUIExtension::Send(gin::Arguments
* args
) {
86 blink::WebFrame
* frame
;
87 RenderView
* render_view
;
88 if (!ShouldRespondToRequest(&frame
, &render_view
))
92 if (!args
->GetNext(&message
)) {
97 // If they've provided an optional message parameter, convert that into a
98 // Value to send to the browser process.
99 scoped_ptr
<base::ListValue
> content
;
100 if (args
->PeekNext().IsEmpty() || args
->PeekNext()->IsUndefined()) {
101 content
.reset(new base::ListValue());
103 v8::Local
<v8::Object
> obj
;
104 if (!args
->GetNext(&obj
)) {
109 scoped_ptr
<V8ValueConverter
> converter(V8ValueConverter::create());
112 converter
->FromV8Value(obj
, frame
->mainWorldScriptContext());
113 base::ListValue
* list
= NULL
;
114 value
->GetAsList(&list
);
119 // Send the message up to the browser.
120 render_view
->Send(new ViewHostMsg_WebUISend(render_view
->GetRoutingID(),
121 frame
->document().url(),
127 std::string
WebUIExtension::GetVariableValue(const std::string
& name
) {
128 blink::WebFrame
* frame
;
129 RenderView
* render_view
;
130 if (!ShouldRespondToRequest(&frame
, &render_view
))
131 return std::string();
133 return WebUIExtensionData::Get(render_view
)->GetValue(name
);
136 } // namespace content