| OLD | NEW |
| 1 // Copyright 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "content/renderer/web_ui_extension.h" | 5 #include "content/renderer/web_ui_extension.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 #include <utility> | 8 #include <utility> |
| 9 | 9 |
| 10 #include "base/strings/string_util.h" | 10 #include "base/strings/string_util.h" |
| (...skipping 14 matching lines...) Expand all Loading... |
| 25 #include "third_party/WebKit/public/web/WebLocalFrame.h" | 25 #include "third_party/WebKit/public/web/WebLocalFrame.h" |
| 26 #include "third_party/WebKit/public/web/WebUserGestureIndicator.h" | 26 #include "third_party/WebKit/public/web/WebUserGestureIndicator.h" |
| 27 #include "third_party/WebKit/public/web/WebView.h" | 27 #include "third_party/WebKit/public/web/WebView.h" |
| 28 #include "url/gurl.h" | 28 #include "url/gurl.h" |
| 29 #include "v8/include/v8.h" | 29 #include "v8/include/v8.h" |
| 30 | 30 |
| 31 namespace content { | 31 namespace content { |
| 32 | 32 |
| 33 namespace { | 33 namespace { |
| 34 | 34 |
| 35 bool ShouldRespondToRequest( | 35 bool ShouldRespondToRequest(blink::WebLocalFrame** frame_ptr, |
| 36 blink::WebFrame** frame_ptr, | 36 RenderView** render_view_ptr) { |
| 37 RenderView** render_view_ptr) { | 37 blink::WebLocalFrame* frame = blink::WebLocalFrame::FrameForCurrentContext(); |
| 38 blink::WebFrame* frame = blink::WebLocalFrame::FrameForCurrentContext(); | |
| 39 if (!frame || !frame->View()) | 38 if (!frame || !frame->View()) |
| 40 return false; | 39 return false; |
| 41 | 40 |
| 42 RenderView* render_view = RenderView::FromWebView(frame->View()); | 41 RenderView* render_view = RenderView::FromWebView(frame->View()); |
| 43 if (!render_view) | 42 if (!render_view) |
| 44 return false; | 43 return false; |
| 45 | 44 |
| 46 GURL frame_url = frame->GetDocument().Url(); | 45 GURL frame_url = frame->GetDocument().Url(); |
| 47 | 46 |
| 48 RenderFrame* render_frame = RenderFrame::FromWebFrame(frame); | 47 RenderFrame* render_frame = RenderFrame::FromWebFrame(frame); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 63 } | 62 } |
| 64 | 63 |
| 65 } // namespace | 64 } // namespace |
| 66 | 65 |
| 67 // Exposes two methods: | 66 // Exposes two methods: |
| 68 // - chrome.send: Used to send messages to the browser. Requires the message | 67 // - chrome.send: Used to send messages to the browser. Requires the message |
| 69 // name as the first argument and can have an optional second argument that | 68 // name as the first argument and can have an optional second argument that |
| 70 // should be an array. | 69 // should be an array. |
| 71 // - chrome.getVariableValue: Returns value for the input variable name if such | 70 // - chrome.getVariableValue: Returns value for the input variable name if such |
| 72 // a value was set by the browser. Else will return an empty string. | 71 // a value was set by the browser. Else will return an empty string. |
| 73 void WebUIExtension::Install(blink::WebFrame* frame) { | 72 void WebUIExtension::Install(blink::WebLocalFrame* frame) { |
| 74 v8::Isolate* isolate = blink::MainThreadIsolate(); | 73 v8::Isolate* isolate = blink::MainThreadIsolate(); |
| 75 v8::HandleScope handle_scope(isolate); | 74 v8::HandleScope handle_scope(isolate); |
| 76 v8::Local<v8::Context> context = frame->MainWorldScriptContext(); | 75 v8::Local<v8::Context> context = frame->MainWorldScriptContext(); |
| 77 if (context.IsEmpty()) | 76 if (context.IsEmpty()) |
| 78 return; | 77 return; |
| 79 | 78 |
| 80 v8::Context::Scope context_scope(context); | 79 v8::Context::Scope context_scope(context); |
| 81 | 80 |
| 82 v8::Local<v8::Object> chrome = GetOrCreateChromeObject(isolate, | 81 v8::Local<v8::Object> chrome = GetOrCreateChromeObject(isolate, |
| 83 context->Global()); | 82 context->Global()); |
| 84 chrome->Set(gin::StringToSymbol(isolate, "send"), | 83 chrome->Set(gin::StringToSymbol(isolate, "send"), |
| 85 gin::CreateFunctionTemplate( | 84 gin::CreateFunctionTemplate( |
| 86 isolate, base::Bind(&WebUIExtension::Send))->GetFunction()); | 85 isolate, base::Bind(&WebUIExtension::Send))->GetFunction()); |
| 87 chrome->Set(gin::StringToSymbol(isolate, "getVariableValue"), | 86 chrome->Set(gin::StringToSymbol(isolate, "getVariableValue"), |
| 88 gin::CreateFunctionTemplate( | 87 gin::CreateFunctionTemplate( |
| 89 isolate, base::Bind(&WebUIExtension::GetVariableValue)) | 88 isolate, base::Bind(&WebUIExtension::GetVariableValue)) |
| 90 ->GetFunction()); | 89 ->GetFunction()); |
| 91 } | 90 } |
| 92 | 91 |
| 93 // static | 92 // static |
| 94 void WebUIExtension::Send(gin::Arguments* args) { | 93 void WebUIExtension::Send(gin::Arguments* args) { |
| 95 blink::WebFrame* frame; | 94 blink::WebLocalFrame* frame; |
| 96 RenderView* render_view; | 95 RenderView* render_view; |
| 97 if (!ShouldRespondToRequest(&frame, &render_view)) | 96 if (!ShouldRespondToRequest(&frame, &render_view)) |
| 98 return; | 97 return; |
| 99 | 98 |
| 100 std::string message; | 99 std::string message; |
| 101 if (!args->GetNext(&message)) { | 100 if (!args->GetNext(&message)) { |
| 102 args->ThrowError(); | 101 args->ThrowError(); |
| 103 return; | 102 return; |
| 104 } | 103 } |
| 105 | 104 |
| (...skipping 23 matching lines...) Expand all Loading... |
| 129 } | 128 } |
| 130 | 129 |
| 131 // Send the message up to the browser. | 130 // Send the message up to the browser. |
| 132 render_view->Send(new ViewHostMsg_WebUISend(render_view->GetRoutingID(), | 131 render_view->Send(new ViewHostMsg_WebUISend(render_view->GetRoutingID(), |
| 133 frame->GetDocument().Url(), | 132 frame->GetDocument().Url(), |
| 134 message, *content)); | 133 message, *content)); |
| 135 } | 134 } |
| 136 | 135 |
| 137 // static | 136 // static |
| 138 std::string WebUIExtension::GetVariableValue(const std::string& name) { | 137 std::string WebUIExtension::GetVariableValue(const std::string& name) { |
| 139 blink::WebFrame* frame; | 138 blink::WebLocalFrame* frame; |
| 140 RenderView* render_view; | 139 RenderView* render_view; |
| 141 if (!ShouldRespondToRequest(&frame, &render_view)) | 140 if (!ShouldRespondToRequest(&frame, &render_view)) |
| 142 return std::string(); | 141 return std::string(); |
| 143 | 142 |
| 144 return WebUIExtensionData::Get(render_view)->GetValue(name); | 143 return WebUIExtensionData::Get(render_view)->GetValue(name); |
| 145 } | 144 } |
| 146 | 145 |
| 147 } // namespace content | 146 } // namespace content |
| OLD | NEW |