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

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

Issue 869133005: Post a Message from Java to JS (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: spelling fix Created 5 years, 10 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 "android_webview/renderer/aw_message_port_client.h"
6 6
7 #include "android_webview/common/aw_message_port_messages.h" 7 #include "android_webview/common/aw_message_port_messages.h"
8 #include "content/public/renderer/render_frame.h" 8 #include "content/public/renderer/render_frame.h"
9 #include "content/public/renderer/render_view.h" 9 #include "content/public/renderer/render_view.h"
10 #include "content/public/renderer/v8_value_converter.h" 10 #include "content/public/renderer/v8_value_converter.h"
(...skipping 14 matching lines...) Expand all
25 : content::RenderFrameObserver(render_frame) { 25 : content::RenderFrameObserver(render_frame) {
26 } 26 }
27 27
28 AwMessagePortClient::~AwMessagePortClient() { 28 AwMessagePortClient::~AwMessagePortClient() {
29 } 29 }
30 30
31 bool AwMessagePortClient::OnMessageReceived( 31 bool AwMessagePortClient::OnMessageReceived(
32 const IPC::Message& message) { 32 const IPC::Message& message) {
33 bool handled = true; 33 bool handled = true;
34 IPC_BEGIN_MESSAGE_MAP(AwMessagePortClient, message) 34 IPC_BEGIN_MESSAGE_MAP(AwMessagePortClient, message)
35 IPC_MESSAGE_HANDLER(AwMessagePortMsg_Message, OnPostMessage) 35 IPC_MESSAGE_HANDLER(AwMessagePortMsg_WebToAppMessage, OnWebToAppMessage)
36 IPC_MESSAGE_HANDLER(AwMessagePortMsg_AppToWebMessage, OnAppToWebMessage)
36 IPC_MESSAGE_UNHANDLED(handled = false) 37 IPC_MESSAGE_UNHANDLED(handled = false)
37 IPC_END_MESSAGE_MAP() 38 IPC_END_MESSAGE_MAP()
38 39
39 return handled; 40 return handled;
40 } 41 }
41 42
42 void AwMessagePortClient::OnPostMessage( 43 void AwMessagePortClient::OnWebToAppMessage(
43 int message_port_id, 44 int message_port_id,
44 const base::string16& message, 45 const base::string16& message,
45 const vector<int>& sent_message_port_ids) { 46 const vector<int>& sent_message_port_ids) {
46 v8::HandleScope handle_scope(blink::mainThreadIsolate()); 47 v8::HandleScope handle_scope(blink::mainThreadIsolate());
47 blink::WebFrame* main_frame = 48 blink::WebFrame* main_frame =
48 render_frame()->GetRenderView()->GetWebView()->mainFrame(); 49 render_frame()->GetRenderView()->GetWebView()->mainFrame();
49 if (main_frame == nullptr) { 50 if (main_frame == nullptr) {
50 return; 51 return;
51 } 52 }
52 v8::Local<v8::Context> context = main_frame->mainWorldScriptContext(); 53 v8::Local<v8::Context> context = main_frame->mainWorldScriptContext();
53 v8::Context::Scope context_scope(context); 54 v8::Context::Scope context_scope(context);
54 DCHECK(!context.IsEmpty()); 55 DCHECK(!context.IsEmpty());
55 WebSerializedScriptValue v = WebSerializedScriptValue::fromString(message); 56 WebSerializedScriptValue v = WebSerializedScriptValue::fromString(message);
56 v8::Handle<v8::Value> v8value = v.deserialize(); 57 v8::Handle<v8::Value> v8value = v.deserialize();
57 58
58 scoped_ptr<V8ValueConverter> converter; 59 scoped_ptr<V8ValueConverter> converter;
59 converter.reset(V8ValueConverter::create()); 60 converter.reset(V8ValueConverter::create());
60 converter->SetDateAllowed(true); 61 converter->SetDateAllowed(true);
61 converter->SetRegExpAllowed(true); 62 converter->SetRegExpAllowed(true);
62 base::ListValue result; 63 base::ListValue result;
63 result.Append(converter->FromV8Value(v8value, context)); 64 result.Append(converter->FromV8Value(v8value, context));
64 Send(new AwMessagePortHostMsg_ConvertedMessage(render_frame()->GetRoutingID(), 65 Send(new AwMessagePortHostMsg_ConvertedWebToAppMessage(
65 message_port_id, 66 render_frame()->GetRoutingID(), message_port_id,
66 result, 67 result, sent_message_port_ids));
67 sent_message_port_ids)); 68 }
69
70 void AwMessagePortClient::OnAppToWebMessage(
71 int message_port_id,
72 const base::string16& message,
73 const vector<int>& sent_message_port_ids) {
74 v8::HandleScope handle_scope(blink::mainThreadIsolate());
75 blink::WebFrame* main_frame =
76 render_frame()->GetRenderView()->GetWebView()->mainFrame();
77 if (main_frame == nullptr) {
78 return;
79 }
80 v8::Local<v8::Context> context = main_frame->mainWorldScriptContext();
81 v8::Context::Scope context_scope(context);
82 DCHECK(!context.IsEmpty());
83 scoped_ptr<V8ValueConverter> converter;
84 converter.reset(V8ValueConverter::create());
85 converter->SetDateAllowed(true);
86 converter->SetRegExpAllowed(true);
87 scoped_ptr<base::Value> value(new base::StringValue(message));
88 v8::Handle<v8::Value> result_value = converter->ToV8Value(value.get(),
89 context);
90 WebSerializedScriptValue serialized_script_value =
91 WebSerializedScriptValue::serialize(result_value);
92 base::string16 result = serialized_script_value.toString();
93 Send(new AwMessagePortHostMsg_ConvertedAppToWebMessage(
94 render_frame()->GetRoutingID(), message_port_id,
95 result, sent_message_port_ids));
68 } 96 }
69 97
70 } 98 }
OLDNEW
« no previous file with comments | « android_webview/renderer/aw_message_port_client.h ('k') | content/browser/message_port_provider.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698