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/renderer/render_frame.h"
9 #include "content/public/renderer/render_view.h"
10 #include "content/public/renderer/v8_value_converter.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
;
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
) {
34 IPC_BEGIN_MESSAGE_MAP(AwMessagePortClient
, message
)
35 IPC_MESSAGE_HANDLER(AwMessagePortMsg_Message
, OnPostMessage
)
36 IPC_MESSAGE_UNHANDLED(handled
= false)
42 void AwMessagePortClient::OnPostMessage(
44 const base::string16
& message
,
45 const vector
<int>& sent_message_port_ids
) {
46 v8::HandleScope
handle_scope(blink::mainThreadIsolate());
47 blink::WebFrame
* main_frame
=
48 render_frame()->GetRenderView()->GetWebView()->mainFrame();
49 if (main_frame
== nullptr) {
52 v8::Local
<v8::Context
> context
= main_frame
->mainWorldScriptContext();
53 v8::Context::Scope
context_scope(context
);
54 DCHECK(!context
.IsEmpty());
55 WebSerializedScriptValue v
= WebSerializedScriptValue::fromString(message
);
56 v8::Handle
<v8::Value
> v8value
= v
.deserialize();
58 scoped_ptr
<V8ValueConverter
> converter
;
59 converter
.reset(V8ValueConverter::create());
60 converter
->SetDateAllowed(true);
61 converter
->SetRegExpAllowed(true);
62 base::ListValue result
;
63 result
.Append(converter
->FromV8Value(v8value
, context
));
64 Send(new AwMessagePortHostMsg_ConvertedMessage(render_frame()->GetRoutingID(),
67 sent_message_port_ids
));