OLD | NEW |
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 #include "content/public/browser/message_port_delegate.h" |
16 | 16 |
17 namespace content { | 17 namespace content { |
18 | 18 |
19 namespace { | |
20 | |
21 void PostMessageOnIOThread(MessagePortMessageFilter* filter, | |
22 int routing_id, | |
23 ViewMsg_PostMessage_Params* params) { | |
24 if (!params->message_port_ids.empty()) { | |
25 filter->UpdateMessagePortsWithNewRoutes(params->message_port_ids, | |
26 ¶ms->new_routing_ids); | |
27 } | |
28 filter->Send(new ViewMsg_PostMessageEvent(routing_id, *params)); | |
29 } | |
30 | |
31 } // namespace | |
32 | |
33 // static | 19 // static |
34 void MessagePortProvider::PostMessageToFrame( | 20 void MessagePortProvider::PostMessageToFrame( |
35 WebContents* web_contents, | 21 WebContents* web_contents, |
36 const base::string16& source_origin, | 22 const base::string16& source_origin, |
37 const base::string16& target_origin, | 23 const base::string16& target_origin, |
38 const base::string16& data, | 24 const base::string16& data, |
39 const std::vector<int>& ports) { | 25 const std::vector<TransferredMessagePort>& ports) { |
40 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 26 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
41 | 27 |
42 ViewMsg_PostMessage_Params* params = new ViewMsg_PostMessage_Params(); | 28 ViewMsg_PostMessage_Params params; |
43 params->is_data_raw_string = true; | 29 params.is_data_raw_string = true; |
44 params->data = data; | 30 params.data = data; |
45 // Blink requires a source frame to transfer ports. This is why a | 31 // Blink requires a source frame to transfer ports. This is why a |
46 // source routing id is set here. See WebDOMMessageEvent::initMessageEvent() | 32 // source routing id is set here. See WebDOMMessageEvent::initMessageEvent() |
47 params->source_routing_id = web_contents->GetRoutingID(); | 33 params.source_routing_id = web_contents->GetRoutingID(); |
48 params->source_origin = source_origin; | 34 params.source_origin = source_origin; |
49 params->target_origin = target_origin; | 35 params.target_origin = target_origin; |
50 params->message_port_ids = ports; | 36 params.message_ports = ports; |
51 | 37 |
52 RenderProcessHostImpl* rph = | 38 RenderProcessHostImpl* rph = |
53 static_cast<RenderProcessHostImpl*>(web_contents->GetRenderProcessHost()); | 39 static_cast<RenderProcessHostImpl*>(web_contents->GetRenderProcessHost()); |
54 MessagePortMessageFilter* mf = rph->message_port_message_filter(); | |
55 BrowserThread::PostTask( | 40 BrowserThread::PostTask( |
56 BrowserThread::IO, | 41 BrowserThread::IO, FROM_HERE, |
57 FROM_HERE, | 42 base::Bind(&MessagePortMessageFilter::RouteMessageEventWithMessagePorts, |
58 base::Bind(&PostMessageOnIOThread, | 43 rph->message_port_message_filter(), |
59 make_scoped_refptr(mf), | 44 web_contents->GetRoutingID(), params)); |
60 web_contents->GetRoutingID(), | |
61 base::Owned(params))); | |
62 } | 45 } |
63 | 46 |
64 // static | 47 // static |
65 void MessagePortProvider::CreateMessageChannel(MessagePortDelegate* delegate, | 48 void MessagePortProvider::CreateMessageChannel(MessagePortDelegate* delegate, |
66 int* port1, | 49 int* port1, |
67 int* port2) { | 50 int* port2) { |
68 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 51 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
69 *port1 = 0; | 52 *port1 = 0; |
70 *port2 = 0; | 53 *port2 = 0; |
71 MessagePortService* msp = MessagePortService::GetInstance(); | 54 MessagePortService* msp = MessagePortService::GetInstance(); |
72 msp->Create(MSG_ROUTING_NONE, delegate, port1); | 55 msp->Create(MSG_ROUTING_NONE, delegate, port1); |
73 msp->Create(MSG_ROUTING_NONE, delegate, port2); | 56 msp->Create(MSG_ROUTING_NONE, delegate, port2); |
74 // Update the routing number of the message ports to be equal to the message | 57 // Update the routing number of the message ports to be equal to the message |
75 // port numbers. | 58 // port numbers. |
76 msp->UpdateMessagePort(*port1, delegate, *port1); | 59 msp->UpdateMessagePort(*port1, delegate, *port1); |
77 msp->UpdateMessagePort(*port2, delegate, *port2); | 60 msp->UpdateMessagePort(*port2, delegate, *port2); |
78 msp->Entangle(*port1, *port2); | 61 msp->Entangle(*port1, *port2); |
79 msp->Entangle(*port2, *port1); | 62 msp->Entangle(*port2, *port1); |
80 } | 63 } |
81 | 64 |
82 // static | 65 // static |
83 void MessagePortProvider::PostMessageToPort( | 66 void MessagePortProvider::PostMessageToPort( |
84 int sender_port_id, | 67 int sender_port_id, |
85 const MessagePortMessage& message, | 68 const MessagePortMessage& message, |
86 const std::vector<int>& sent_ports) { | 69 const std::vector<TransferredMessagePort>& sent_ports) { |
87 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 70 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
88 MessagePortService* msp = MessagePortService::GetInstance(); | 71 MessagePortService* msp = MessagePortService::GetInstance(); |
89 msp->PostMessage(sender_port_id, message, sent_ports); | 72 msp->PostMessage(sender_port_id, message, sent_ports); |
90 } | 73 } |
91 | 74 |
92 // static | 75 // static |
93 void MessagePortProvider::ClosePort(int message_port_id) { | 76 void MessagePortProvider::ClosePort(int message_port_id) { |
94 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 77 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
95 MessagePortService* msp = MessagePortService::GetInstance(); | 78 MessagePortService* msp = MessagePortService::GetInstance(); |
96 msp->ClosePort(message_port_id); | 79 msp->ClosePort(message_port_id); |
97 } | 80 } |
98 | 81 |
99 // static | 82 // static |
100 void MessagePortProvider::OnMessagePortDelegateClosing( | 83 void MessagePortProvider::OnMessagePortDelegateClosing( |
101 MessagePortDelegate* delegate) { | 84 MessagePortDelegate* delegate) { |
102 MessagePortService::GetInstance()->OnMessagePortDelegateClosing(delegate); | 85 MessagePortService::GetInstance()->OnMessagePortDelegateClosing(delegate); |
103 } | 86 } |
104 | 87 |
105 } // namespace content | 88 } // namespace content |
OLD | NEW |