Roll src/third_party/WebKit f36d5e0:68b67cd (svn 193299:193303)
[chromium-blink-merge.git] / android_webview / renderer / aw_message_port_client.cc
blob1493db6e32c597b8af42510c94a5fde71d1d9747
1 // Copyright 2015 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 "android_webview/renderer/aw_message_port_client.h"
7 #include "android_webview/common/aw_message_port_messages.h"
8 #include "content/public/child/v8_value_converter.h"
9 #include "content/public/renderer/render_frame.h"
10 #include "content/public/renderer/render_view.h"
11 #include "ipc/ipc_message_macros.h"
12 #include "third_party/WebKit/public/web/WebFrame.h"
13 #include "third_party/WebKit/public/web/WebKit.h"
14 #include "third_party/WebKit/public/web/WebSerializedScriptValue.h"
15 #include "third_party/WebKit/public/web/WebView.h"
16 #include "v8/include/v8.h"
18 using blink::WebSerializedScriptValue;
19 using content::V8ValueConverter;
20 using std::vector;
22 namespace android_webview {
24 AwMessagePortClient::AwMessagePortClient(content::RenderFrame* render_frame)
25 : content::RenderFrameObserver(render_frame) {
28 AwMessagePortClient::~AwMessagePortClient() {
31 bool AwMessagePortClient::OnMessageReceived(
32 const IPC::Message& message) {
33 bool handled = true;
34 IPC_BEGIN_MESSAGE_MAP(AwMessagePortClient, message)
35 IPC_MESSAGE_HANDLER(AwMessagePortMsg_WebToAppMessage, OnWebToAppMessage)
36 IPC_MESSAGE_HANDLER(AwMessagePortMsg_AppToWebMessage, OnAppToWebMessage)
37 IPC_MESSAGE_HANDLER(AwMessagePortMsg_ClosePort, OnClosePort)
38 IPC_MESSAGE_UNHANDLED(handled = false)
39 IPC_END_MESSAGE_MAP()
41 return handled;
44 void AwMessagePortClient::OnWebToAppMessage(
45 int message_port_id,
46 const base::string16& message,
47 const vector<int>& sent_message_port_ids) {
48 v8::HandleScope handle_scope(blink::mainThreadIsolate());
49 blink::WebFrame* main_frame =
50 render_frame()->GetRenderView()->GetWebView()->mainFrame();
51 if (main_frame == nullptr) {
52 return;
54 v8::Local<v8::Context> context = main_frame->mainWorldScriptContext();
55 v8::Context::Scope context_scope(context);
56 DCHECK(!context.IsEmpty());
57 WebSerializedScriptValue v = WebSerializedScriptValue::fromString(message);
58 v8::Handle<v8::Value> v8value = v.deserialize();
60 scoped_ptr<V8ValueConverter> converter;
61 converter.reset(V8ValueConverter::create());
62 converter->SetDateAllowed(true);
63 converter->SetRegExpAllowed(true);
64 base::ListValue result;
65 base::Value* value = converter->FromV8Value(v8value, context);
66 if (value) {
67 result.Append(value);
70 Send(new AwMessagePortHostMsg_ConvertedWebToAppMessage(
71 render_frame()->GetRoutingID(), message_port_id, result,
72 sent_message_port_ids));
75 void AwMessagePortClient::OnAppToWebMessage(
76 int message_port_id,
77 const base::string16& message,
78 const vector<int>& sent_message_port_ids) {
79 v8::HandleScope handle_scope(blink::mainThreadIsolate());
80 blink::WebFrame* main_frame =
81 render_frame()->GetRenderView()->GetWebView()->mainFrame();
82 if (main_frame == nullptr) {
83 return;
85 v8::Local<v8::Context> context = main_frame->mainWorldScriptContext();
86 v8::Context::Scope context_scope(context);
87 DCHECK(!context.IsEmpty());
88 scoped_ptr<V8ValueConverter> converter;
89 converter.reset(V8ValueConverter::create());
90 converter->SetDateAllowed(true);
91 converter->SetRegExpAllowed(true);
92 scoped_ptr<base::Value> value(new base::StringValue(message));
93 v8::Handle<v8::Value> result_value = converter->ToV8Value(value.get(),
94 context);
95 WebSerializedScriptValue serialized_script_value =
96 WebSerializedScriptValue::serialize(result_value);
97 base::string16 result = serialized_script_value.toString();
98 Send(new AwMessagePortHostMsg_ConvertedAppToWebMessage(
99 render_frame()->GetRoutingID(), message_port_id,
100 result, sent_message_port_ids));
103 void AwMessagePortClient::OnClosePort(int message_port_id) {
104 Send(new AwMessagePortHostMsg_ClosePortAck(render_frame()->GetRoutingID(),
105 message_port_id));