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