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

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

Issue 2375133002: Move MessagePort implementation from android_webview to content (Closed)
Patch Set: rsesek nits and git cl format Created 4 years, 2 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
« no previous file with comments | « android_webview/renderer/aw_message_port_client.h ('k') | content/browser/BUILD.gn » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 <memory>
8 #include <utility>
9
10 #include "android_webview/common/aw_message_port_messages.h"
11 #include "content/public/child/v8_value_converter.h"
12 #include "content/public/renderer/render_frame.h"
13 #include "content/public/renderer/render_view.h"
14 #include "ipc/ipc_message_macros.h"
15 #include "third_party/WebKit/public/web/WebFrame.h"
16 #include "third_party/WebKit/public/web/WebKit.h"
17 #include "third_party/WebKit/public/web/WebSerializedScriptValue.h"
18 #include "third_party/WebKit/public/web/WebView.h"
19 #include "v8/include/v8.h"
20
21 using blink::WebSerializedScriptValue;
22 using content::V8ValueConverter;
23 using std::vector;
24
25 namespace android_webview {
26
27 AwMessagePortClient::AwMessagePortClient(content::RenderFrame* render_frame)
28 : content::RenderFrameObserver(render_frame) {
29 }
30
31 AwMessagePortClient::~AwMessagePortClient() {
32 }
33
34 bool AwMessagePortClient::OnMessageReceived(
35 const IPC::Message& message) {
36 bool handled = true;
37 IPC_BEGIN_MESSAGE_MAP(AwMessagePortClient, message)
38 IPC_MESSAGE_HANDLER(AwMessagePortMsg_WebToAppMessage, OnWebToAppMessage)
39 IPC_MESSAGE_HANDLER(AwMessagePortMsg_AppToWebMessage, OnAppToWebMessage)
40 IPC_MESSAGE_HANDLER(AwMessagePortMsg_ClosePort, OnClosePort)
41 IPC_MESSAGE_UNHANDLED(handled = false)
42 IPC_END_MESSAGE_MAP()
43
44 return handled;
45 }
46
47 void AwMessagePortClient::OnDestruct() {
48 delete this;
49 }
50
51 void AwMessagePortClient::OnWebToAppMessage(
52 int message_port_id,
53 const base::string16& message,
54 const vector<int>& sent_message_port_ids) {
55 v8::HandleScope handle_scope(blink::mainThreadIsolate());
56 blink::WebFrame* main_frame =
57 render_frame()->GetRenderView()->GetWebView()->mainFrame();
58 if (main_frame == nullptr) {
59 return;
60 }
61 v8::Local<v8::Context> context = main_frame->mainWorldScriptContext();
62 v8::Context::Scope context_scope(context);
63 DCHECK(!context.IsEmpty());
64 WebSerializedScriptValue v = WebSerializedScriptValue::fromString(message);
65 v8::Local<v8::Value> v8value = v.deserialize();
66
67 std::unique_ptr<V8ValueConverter> converter;
68 converter.reset(V8ValueConverter::create());
69 converter->SetDateAllowed(true);
70 converter->SetRegExpAllowed(true);
71 base::ListValue result;
72 std::unique_ptr<base::Value> value = converter->FromV8Value(v8value, context);
73 if (value) {
74 result.Append(std::move(value));
75 }
76
77 Send(new AwMessagePortHostMsg_ConvertedWebToAppMessage(
78 render_frame()->GetRoutingID(), message_port_id, result,
79 sent_message_port_ids));
80 }
81
82 void AwMessagePortClient::OnAppToWebMessage(
83 int message_port_id,
84 const base::string16& message,
85 const vector<int>& sent_message_port_ids) {
86 v8::HandleScope handle_scope(blink::mainThreadIsolate());
87 blink::WebFrame* main_frame =
88 render_frame()->GetRenderView()->GetWebView()->mainFrame();
89 if (main_frame == nullptr) {
90 return;
91 }
92 v8::Local<v8::Context> context = main_frame->mainWorldScriptContext();
93 v8::Context::Scope context_scope(context);
94 DCHECK(!context.IsEmpty());
95 std::unique_ptr<V8ValueConverter> converter;
96 converter.reset(V8ValueConverter::create());
97 converter->SetDateAllowed(true);
98 converter->SetRegExpAllowed(true);
99 std::unique_ptr<base::Value> value(new base::StringValue(message));
100 v8::Local<v8::Value> result_value =
101 converter->ToV8Value(value.get(), context);
102 WebSerializedScriptValue serialized_script_value =
103 WebSerializedScriptValue::serialize(result_value);
104 base::string16 result = serialized_script_value.toString();
105 Send(new AwMessagePortHostMsg_ConvertedAppToWebMessage(
106 render_frame()->GetRoutingID(), message_port_id,
107 result, sent_message_port_ids));
108 }
109
110 void AwMessagePortClient::OnClosePort(int message_port_id) {
111 Send(new AwMessagePortHostMsg_ClosePortAck(render_frame()->GetRoutingID(),
112 message_port_id));
113 }
114
115 }
OLDNEW
« no previous file with comments | « android_webview/renderer/aw_message_port_client.h ('k') | content/browser/BUILD.gn » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698