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

Side by Side Diff: content/renderer/android/app_web_message_port_client.cc

Issue 2375133002: Move MessagePort implementation from android_webview to content (Closed)
Patch Set: added ifdef 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
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 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 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 "android_webview/renderer/aw_message_port_client.h" 5 #include <content/common/app_web_message_port_messages.h>
Robert Sesek 2016/10/11 15:44:34 Wrong #include type for local paths. Should be "".
Yusuf 2016/10/11 16:48:47 Done.
6 6 #include <content/renderer/android/app_web_message_port_client.h>
7 #include <memory> 7 #include <memory>
8 #include <utility> 8 #include <utility>
9 9
10 #include "android_webview/common/aw_message_port_messages.h"
11 #include "content/public/child/v8_value_converter.h" 10 #include "content/public/child/v8_value_converter.h"
12 #include "content/public/renderer/render_frame.h" 11 #include "content/public/renderer/render_frame.h"
13 #include "content/public/renderer/render_view.h" 12 #include "content/public/renderer/render_view.h"
14 #include "ipc/ipc_message_macros.h" 13 #include "ipc/ipc_message_macros.h"
15 #include "third_party/WebKit/public/web/WebFrame.h" 14 #include "third_party/WebKit/public/web/WebFrame.h"
16 #include "third_party/WebKit/public/web/WebKit.h" 15 #include "third_party/WebKit/public/web/WebKit.h"
17 #include "third_party/WebKit/public/web/WebSerializedScriptValue.h" 16 #include "third_party/WebKit/public/web/WebSerializedScriptValue.h"
18 #include "third_party/WebKit/public/web/WebView.h" 17 #include "third_party/WebKit/public/web/WebView.h"
19 #include "v8/include/v8.h" 18 #include "v8/include/v8.h"
20 19
21 using blink::WebSerializedScriptValue; 20 using blink::WebSerializedScriptValue;
22 using content::V8ValueConverter; 21 using content::V8ValueConverter;
23 using std::vector; 22 using std::vector;
24 23
25 namespace android_webview { 24 namespace content {
26 25
27 AwMessagePortClient::AwMessagePortClient(content::RenderFrame* render_frame) 26 AppWebMessagePortClient::AppWebMessagePortClient(
27 content::RenderFrame* render_frame)
28 : content::RenderFrameObserver(render_frame) { 28 : content::RenderFrameObserver(render_frame) {
29 } 29 }
30 30
31 AwMessagePortClient::~AwMessagePortClient() { 31 AppWebMessagePortClient::~AppWebMessagePortClient() {
32 } 32 }
33 33
34 bool AwMessagePortClient::OnMessageReceived( 34 bool AppWebMessagePortClient::OnMessageReceived(
35 const IPC::Message& message) { 35 const IPC::Message& message) {
36 bool handled = true; 36 bool handled = true;
37 IPC_BEGIN_MESSAGE_MAP(AwMessagePortClient, message) 37 IPC_BEGIN_MESSAGE_MAP(AppWebMessagePortClient, message)
38 IPC_MESSAGE_HANDLER(AwMessagePortMsg_WebToAppMessage, OnWebToAppMessage) 38 IPC_MESSAGE_HANDLER(AppWebMessagePortMsg_WebToAppMessage, OnWebToAppMessage)
39 IPC_MESSAGE_HANDLER(AwMessagePortMsg_AppToWebMessage, OnAppToWebMessage) 39 IPC_MESSAGE_HANDLER(AppWebMessagePortMsg_AppToWebMessage, OnAppToWebMessage)
40 IPC_MESSAGE_HANDLER(AwMessagePortMsg_ClosePort, OnClosePort) 40 IPC_MESSAGE_HANDLER(AppWebMessagePortMsg_ClosePort, OnClosePort)
41 IPC_MESSAGE_UNHANDLED(handled = false) 41 IPC_MESSAGE_UNHANDLED(handled = false)
42 IPC_END_MESSAGE_MAP() 42 IPC_END_MESSAGE_MAP()
43 43
44 return handled; 44 return handled;
45 } 45 }
46 46
47 void AwMessagePortClient::OnDestruct() { 47 void AppWebMessagePortClient::OnDestruct() {
48 delete this; 48 delete this;
49 } 49 }
50 50
51 void AwMessagePortClient::OnWebToAppMessage( 51 void AppWebMessagePortClient::OnWebToAppMessage(
52 int message_port_id, 52 int message_port_id,
53 const base::string16& message, 53 const base::string16& message,
54 const vector<int>& sent_message_port_ids) { 54 const vector<int>& sent_message_port_ids) {
55 v8::HandleScope handle_scope(blink::mainThreadIsolate()); 55 v8::HandleScope handle_scope(blink::mainThreadIsolate());
56 blink::WebFrame* main_frame = 56 blink::WebFrame* main_frame =
57 render_frame()->GetRenderView()->GetWebView()->mainFrame(); 57 render_frame()->GetRenderView()->GetWebView()->mainFrame();
58 if (main_frame == nullptr) { 58 if (main_frame == nullptr) {
59 return; 59 return;
60 } 60 }
61 v8::Local<v8::Context> context = main_frame->mainWorldScriptContext(); 61 v8::Local<v8::Context> context = main_frame->mainWorldScriptContext();
62 v8::Context::Scope context_scope(context); 62 v8::Context::Scope context_scope(context);
63 DCHECK(!context.IsEmpty()); 63 DCHECK(!context.IsEmpty());
64 WebSerializedScriptValue v = WebSerializedScriptValue::fromString(message); 64 WebSerializedScriptValue v = WebSerializedScriptValue::fromString(message);
65 v8::Local<v8::Value> v8value = v.deserialize(); 65 v8::Local<v8::Value> v8value = v.deserialize();
66 66
67 std::unique_ptr<V8ValueConverter> converter; 67 std::unique_ptr<V8ValueConverter> converter;
68 converter.reset(V8ValueConverter::create()); 68 converter.reset(V8ValueConverter::create());
69 converter->SetDateAllowed(true); 69 converter->SetDateAllowed(true);
70 converter->SetRegExpAllowed(true); 70 converter->SetRegExpAllowed(true);
71 base::ListValue result; 71 base::ListValue result;
72 std::unique_ptr<base::Value> value = converter->FromV8Value(v8value, context); 72 std::unique_ptr<base::Value> value = converter->FromV8Value(v8value, context);
73 if (value) { 73 if (value) {
74 result.Append(std::move(value)); 74 result.Append(std::move(value));
75 } 75 }
76 76
77 Send(new AwMessagePortHostMsg_ConvertedWebToAppMessage( 77 Send(new AppWebMessagePortHostMsg_ConvertedWebToAppMessage(
78 render_frame()->GetRoutingID(), message_port_id, result, 78 render_frame()->GetRoutingID(), message_port_id, result,
79 sent_message_port_ids)); 79 sent_message_port_ids));
80 } 80 }
81 81
82 void AwMessagePortClient::OnAppToWebMessage( 82 void AppWebMessagePortClient::OnAppToWebMessage(
83 int message_port_id, 83 int message_port_id,
84 const base::string16& message, 84 const base::string16& message,
85 const vector<int>& sent_message_port_ids) { 85 const vector<int>& sent_message_port_ids) {
86 v8::HandleScope handle_scope(blink::mainThreadIsolate()); 86 v8::HandleScope handle_scope(blink::mainThreadIsolate());
87 blink::WebFrame* main_frame = 87 blink::WebFrame* main_frame =
88 render_frame()->GetRenderView()->GetWebView()->mainFrame(); 88 render_frame()->GetRenderView()->GetWebView()->mainFrame();
89 if (main_frame == nullptr) { 89 if (main_frame == nullptr) {
90 return; 90 return;
91 } 91 }
92 v8::Local<v8::Context> context = main_frame->mainWorldScriptContext(); 92 v8::Local<v8::Context> context = main_frame->mainWorldScriptContext();
93 v8::Context::Scope context_scope(context); 93 v8::Context::Scope context_scope(context);
94 DCHECK(!context.IsEmpty()); 94 DCHECK(!context.IsEmpty());
95 std::unique_ptr<V8ValueConverter> converter; 95 std::unique_ptr<V8ValueConverter> converter;
96 converter.reset(V8ValueConverter::create()); 96 converter.reset(V8ValueConverter::create());
97 converter->SetDateAllowed(true); 97 converter->SetDateAllowed(true);
98 converter->SetRegExpAllowed(true); 98 converter->SetRegExpAllowed(true);
99 std::unique_ptr<base::Value> value(new base::StringValue(message)); 99 std::unique_ptr<base::Value> value(new base::StringValue(message));
100 v8::Local<v8::Value> result_value = 100 v8::Local<v8::Value> result_value =
101 converter->ToV8Value(value.get(), context); 101 converter->ToV8Value(value.get(), context);
102 WebSerializedScriptValue serialized_script_value = 102 WebSerializedScriptValue serialized_script_value =
103 WebSerializedScriptValue::serialize(result_value); 103 WebSerializedScriptValue::serialize(result_value);
104 base::string16 result = serialized_script_value.toString(); 104 base::string16 result = serialized_script_value.toString();
105 Send(new AwMessagePortHostMsg_ConvertedAppToWebMessage( 105 Send(new AppWebMessagePortHostMsg_ConvertedAppToWebMessage(
106 render_frame()->GetRoutingID(), message_port_id, 106 render_frame()->GetRoutingID(), message_port_id,
107 result, sent_message_port_ids)); 107 result, sent_message_port_ids));
108 } 108 }
109 109
110 void AwMessagePortClient::OnClosePort(int message_port_id) { 110 void AppWebMessagePortClient::OnClosePort(int message_port_id) {
111 Send(new AwMessagePortHostMsg_ClosePortAck(render_frame()->GetRoutingID(), 111 Send(new AppWebMessagePortHostMsg_ClosePortAck(render_frame()->GetRoutingID(),
112 message_port_id)); 112 message_port_id));
113 } 113 }
114 114
115 } 115 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698