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

Side by Side Diff: content/browser/message_port_provider.cc

Issue 831523004: Enable posting a message from JS to Android webview. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: rebase Created 5 years, 11 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 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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 "content/public/browser/message_port_provider.h" 5 #include "content/public/browser/message_port_provider.h"
6 6
7 #include "base/basictypes.h" 7 #include "base/basictypes.h"
8 #include "content/browser/browser_thread_impl.h" 8 #include "content/browser/browser_thread_impl.h"
9 #include "content/browser/message_port_message_filter.h" 9 #include "content/browser/message_port_message_filter.h"
10 #include "content/browser/message_port_service.h" 10 #include "content/browser/message_port_service.h"
11 #include "content/browser/renderer_host/render_process_host_impl.h" 11 #include "content/browser/renderer_host/render_process_host_impl.h"
12 #include "content/browser/renderer_host/render_view_host_impl.h" 12 #include "content/browser/renderer_host/render_view_host_impl.h"
13 #include "content/browser/web_contents/web_contents_impl.h" 13 #include "content/browser/web_contents/web_contents_impl.h"
14 #include "content/common/view_messages.h" 14 #include "content/common/view_messages.h"
15 #include "content/public/browser/message_port_delegate.h"
15 16
16 namespace content { 17 namespace content {
17 18
19 namespace {
20
21 void PostMessageOnIOThread(MessagePortMessageFilter* filter,
mnaganov (inactive) 2015/01/13 10:37:51 Perhaps, you can now make this function a public m
sgurun-gerrit only 2015/01/14 23:53:13 Let's leave it to later in case there is a demand,
mnaganov (inactive) 2015/01/15 09:49:33 Oh, again -- I was wrongly supposing that MessageP
22 int routing_id,
23 ViewMsg_PostMessage_Params* params) {
24 if (!params->message_port_ids.empty()) {
25 filter->UpdateMessagePortsWithNewRoutes(params->message_port_ids,
26 &params->new_routing_ids);
27 }
28 filter->Send(new ViewMsg_PostMessageEvent(routing_id, *params));
29 }
30
31 } // namespace
32
33 // static
18 void MessagePortProvider::PostMessageToFrame( 34 void MessagePortProvider::PostMessageToFrame(
19 WebContents* web_contents, 35 WebContents* web_contents,
20 const base::string16& source_origin, 36 const base::string16& source_origin,
21 const base::string16& target_origin, 37 const base::string16& target_origin,
22 const base::string16& data, 38 const base::string16& data,
23 const std::vector<int>& ports) { 39 const std::vector<int>& ports) {
24 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 40 DCHECK_CURRENTLY_ON(BrowserThread::UI);
25 41
26 RenderViewHost* rvh = web_contents->GetRenderViewHost(); 42 ViewMsg_PostMessage_Params* params = new ViewMsg_PostMessage_Params();
27 if (!rvh) 43 params->is_data_raw_string = true;
28 return; 44 params->data = data;
45 // Blink requires a source frame to transfer ports. This is why a
46 // source routing id is set here. See WebDOMMessageEvent::initMessageEvent()
47 params->source_routing_id = web_contents->GetRoutingID();
48 params->source_origin = source_origin;
49 params->target_origin = target_origin;
50 params->message_port_ids = ports;
29 51
30 ViewMsg_PostMessage_Params params;
31 params.is_data_raw_string = true;
32 params.data = data;
33 params.source_routing_id = web_contents->GetRoutingID();
34 params.source_origin = source_origin;
35 params.target_origin = target_origin;
36 RenderProcessHostImpl* rph = 52 RenderProcessHostImpl* rph =
37 static_cast<RenderProcessHostImpl*>( 53 static_cast<RenderProcessHostImpl*>(web_contents->GetRenderProcessHost());
38 web_contents->GetRenderProcessHost());
39 MessagePortMessageFilter* mf = rph->message_port_message_filter(); 54 MessagePortMessageFilter* mf = rph->message_port_message_filter();
40 55 BrowserThread::PostTask(
41 if (!ports.empty()) { 56 BrowserThread::IO,
42 params.message_port_ids = ports; 57 FROM_HERE,
43 mf->UpdateMessagePortsWithNewRoutes(params.message_port_ids, 58 base::Bind(&PostMessageOnIOThread,
44 &params.new_routing_ids); 59 make_scoped_refptr(mf),
45 } 60 web_contents->GetRoutingID(),
46 rvh->Send(new ViewMsg_PostMessageEvent( 61 base::Owned(params)));
47 web_contents->GetRenderViewHost()->GetRoutingID(),
48 params));
49 } 62 }
50 63
51 void MessagePortProvider::CreateMessageChannel(WebContents* web_contents, 64 // static
65 void MessagePortProvider::CreateMessageChannel(MessagePortDelegate* delegate,
52 int* port1, 66 int* port1,
53 int* port2) { 67 int* port2) {
54 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 68 DCHECK_CURRENTLY_ON(BrowserThread::IO);
55
56 *port1 = 0; 69 *port1 = 0;
57 *port2 = 0; 70 *port2 = 0;
58
59 RenderProcessHostImpl* rph =
60 static_cast<RenderProcessHostImpl*>(
61 web_contents->GetRenderProcessHost());
62 MessagePortMessageFilter* mf = rph->message_port_message_filter();
63 MessagePortService* msp = MessagePortService::GetInstance(); 71 MessagePortService* msp = MessagePortService::GetInstance();
64 msp->Create(mf->GetNextRoutingID(), mf, port1); 72 msp->Create(MSG_ROUTING_NONE, delegate, port1);
65 msp->Create(mf->GetNextRoutingID(), mf, port2); 73 msp->Create(MSG_ROUTING_NONE, delegate, port2);
74 // Update the routing number of the message ports to be equal to the message
75 // port numbers.
76 msp->UpdateMessagePort(*port1, delegate, *port1);
77 msp->UpdateMessagePort(*port2, delegate, *port2);
66 msp->Entangle(*port1, *port2); 78 msp->Entangle(*port1, *port2);
67 msp->Entangle(*port2, *port1); 79 msp->Entangle(*port2, *port1);
68 } 80 }
69 81
82 // static
83 void MessagePortProvider::OnMessagePortDelegateClosing(
84 MessagePortDelegate* delegate) {
85 MessagePortService::GetInstance()->OnMessagePortDelegateClosing(delegate);
86 }
87
70 } // namespace content 88 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698