Update V8 to version 4.6.61.
[chromium-blink-merge.git] / content / renderer / web_ui_extension.cc
blob94c33817b973247b08692422a86a9627da7298a0
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"
23 #include "url/gurl.h"
24 #include "v8/include/v8.h"
26 namespace content {
28 namespace {
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())
35 return false;
37 RenderView* render_view = RenderView::FromWebView(frame->view());
38 if (!render_view)
39 return false;
41 GURL frame_url = frame->document().url();
43 bool webui_enabled =
44 (render_view->GetEnabledBindings() & BINDINGS_POLICY_WEB_UI) &&
45 (frame_url.SchemeIs(kChromeUIScheme) ||
46 frame_url.SchemeIs(url::kDataScheme));
48 if (!webui_enabled)
49 return false;
51 *frame_ptr = frame;
52 *render_view_ptr = render_view;
53 return true;
56 } // namespace
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())
69 return;
71 v8::Context::Scope context_scope(context);
73 v8::Local<v8::Object> chrome = GetOrCreateChromeObject(isolate,
74 context->Global());
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))
81 ->GetFunction());
84 // static
85 void WebUIExtension::Send(gin::Arguments* args) {
86 blink::WebFrame* frame;
87 RenderView* render_view;
88 if (!ShouldRespondToRequest(&frame, &render_view))
89 return;
91 std::string message;
92 if (!args->GetNext(&message)) {
93 args->ThrowError();
94 return;
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());
102 } else {
103 v8::Local<v8::Object> obj;
104 if (!args->GetNext(&obj)) {
105 args->ThrowError();
106 return;
109 scoped_ptr<V8ValueConverter> converter(V8ValueConverter::create());
111 base::Value* value =
112 converter->FromV8Value(obj, frame->mainWorldScriptContext());
113 base::ListValue* list = NULL;
114 value->GetAsList(&list);
115 DCHECK(list);
116 content.reset(list);
119 // Send the message up to the browser.
120 render_view->Send(new ViewHostMsg_WebUISend(render_view->GetRoutingID(),
121 frame->document().url(),
122 message,
123 *content));
126 // static
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