Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(369)

Side by Side Diff: android_webview/renderer/aw_message_port_client.cc

Issue 831523004: Enable posting a message from JS to Android webview. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: fix component builds Created 5 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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.
4
5 #include "android_webview/renderer/aw_message_port_client.h"
6
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"
17
18 using blink::WebSerializedScriptValue;
19 using content::V8ValueConverter;
20 using std::vector;
21
22 namespace android_webview {
23
24 AwMessagePortClient::AwMessagePortClient(content::RenderFrame* render_frame)
25 : content::RenderFrameObserver(render_frame) {
26 }
27
28 AwMessagePortClient::~AwMessagePortClient() {
29 }
30
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_Message, OnPostMessage)
36 IPC_MESSAGE_UNHANDLED(handled = false)
37 IPC_END_MESSAGE_MAP()
38
39 return handled;
40 }
41
42 void AwMessagePortClient::OnPostMessage(
43 int message_port_id,
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) {
50 return;
51 }
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();
57
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(),
65 message_port_id,
66 result,
67 sent_message_port_ids));
68 }
69
70 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698