| OLD | NEW |
| (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 "content/renderer/android/app_web_message_port_client.h" | |
| 6 | |
| 7 #include <memory> | |
| 8 #include <utility> | |
| 9 | |
| 10 #include "content/common/app_web_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/platform/WebString.h" | |
| 16 #include "third_party/WebKit/public/web/WebFrame.h" | |
| 17 #include "third_party/WebKit/public/web/WebKit.h" | |
| 18 #include "third_party/WebKit/public/web/WebSerializedScriptValue.h" | |
| 19 #include "third_party/WebKit/public/web/WebView.h" | |
| 20 #include "v8/include/v8.h" | |
| 21 | |
| 22 using blink::WebSerializedScriptValue; | |
| 23 using content::V8ValueConverter; | |
| 24 using std::vector; | |
| 25 | |
| 26 namespace content { | |
| 27 | |
| 28 AppWebMessagePortClient::AppWebMessagePortClient( | |
| 29 content::RenderFrame* render_frame) | |
| 30 : content::RenderFrameObserver(render_frame) {} | |
| 31 | |
| 32 AppWebMessagePortClient::~AppWebMessagePortClient() {} | |
| 33 | |
| 34 bool AppWebMessagePortClient::OnMessageReceived(const IPC::Message& message) { | |
| 35 bool handled = true; | |
| 36 IPC_BEGIN_MESSAGE_MAP(AppWebMessagePortClient, message) | |
| 37 IPC_MESSAGE_HANDLER(AppWebMessagePortMsg_WebToAppMessage, OnWebToAppMessage) | |
| 38 IPC_MESSAGE_HANDLER(AppWebMessagePortMsg_AppToWebMessage, OnAppToWebMessage) | |
| 39 IPC_MESSAGE_HANDLER(AppWebMessagePortMsg_ClosePort, OnClosePort) | |
| 40 IPC_MESSAGE_UNHANDLED(handled = false) | |
| 41 IPC_END_MESSAGE_MAP() | |
| 42 | |
| 43 return handled; | |
| 44 } | |
| 45 | |
| 46 void AppWebMessagePortClient::OnDestruct() { | |
| 47 delete this; | |
| 48 } | |
| 49 | |
| 50 void AppWebMessagePortClient::OnWebToAppMessage( | |
| 51 int message_port_id, | |
| 52 const base::string16& message, | |
| 53 const vector<int>& sent_message_port_ids) { | |
| 54 v8::HandleScope handle_scope(blink::mainThreadIsolate()); | |
| 55 blink::WebFrame* main_frame = | |
| 56 render_frame()->GetRenderView()->GetWebView()->mainFrame(); | |
| 57 if (main_frame == nullptr) { | |
| 58 return; | |
| 59 } | |
| 60 v8::Local<v8::Context> context = main_frame->mainWorldScriptContext(); | |
| 61 v8::Context::Scope context_scope(context); | |
| 62 DCHECK(!context.IsEmpty()); | |
| 63 WebSerializedScriptValue v = WebSerializedScriptValue::fromString( | |
| 64 blink::WebString::fromUTF16(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 AppWebMessagePortHostMsg_ConvertedWebToAppMessage( | |
| 78 render_frame()->GetRoutingID(), message_port_id, result, | |
| 79 sent_message_port_ids)); | |
| 80 } | |
| 81 | |
| 82 void AppWebMessagePortClient::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().utf16(); | |
| 105 Send(new AppWebMessagePortHostMsg_ConvertedAppToWebMessage( | |
| 106 render_frame()->GetRoutingID(), message_port_id, result, | |
| 107 sent_message_port_ids)); | |
| 108 } | |
| 109 | |
| 110 void AppWebMessagePortClient::OnClosePort(int message_port_id) { | |
| 111 Send(new AppWebMessagePortHostMsg_ClosePortAck(render_frame()->GetRoutingID(), | |
| 112 message_port_id)); | |
| 113 } | |
| 114 } | |
| OLD | NEW |