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
3 // found in the LICENSE file.
5 #include "content/public/browser/message_port_provider.h"
7 #include "base/basictypes.h"
8 #include "content/browser/browser_thread_impl.h"
9 #include "content/browser/message_port_message_filter.h"
10 #include "content/browser/message_port_service.h"
11 #include "content/browser/renderer_host/render_process_host_impl.h"
12 #include "content/browser/renderer_host/render_view_host_impl.h"
13 #include "content/browser/web_contents/web_contents_impl.h"
14 #include "content/common/frame_messages.h"
15 #include "content/public/browser/message_port_delegate.h"
20 void MessagePortProvider::PostMessageToFrame(
21 WebContents
* web_contents
,
22 const base::string16
& source_origin
,
23 const base::string16
& target_origin
,
24 const base::string16
& data
,
25 const std::vector
<TransferredMessagePort
>& ports
) {
26 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
28 FrameMsg_PostMessage_Params params
;
29 params
.is_data_raw_string
= true;
31 // Blink requires a source frame to transfer ports. This is why a
32 // source routing id is set here. See WebDOMMessageEvent::initMessageEvent()
33 // TODO(alexmos, sgurun): Clean this up once crbug.com/473258 is fixed.
34 // Once message ports can work with a null source frame,
35 // source_view_routing_id can be removed, and this can just pass in
36 // MSG_ROUTING_NONE for the source frame.
37 params
.source_view_routing_id
= web_contents
->GetRoutingID();
38 params
.source_routing_id
= MSG_ROUTING_NONE
;
39 params
.source_origin
= source_origin
;
40 params
.target_origin
= target_origin
;
41 params
.message_ports
= ports
;
43 RenderProcessHostImpl
* rph
=
44 static_cast<RenderProcessHostImpl
*>(web_contents
->GetRenderProcessHost());
45 BrowserThread::PostTask(
46 BrowserThread::IO
, FROM_HERE
,
47 base::Bind(&MessagePortMessageFilter::RouteMessageEventWithMessagePorts
,
48 rph
->message_port_message_filter(),
49 web_contents
->GetMainFrame()->GetRoutingID(), params
));
53 void MessagePortProvider::CreateMessageChannel(MessagePortDelegate
* delegate
,
56 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
59 MessagePortService
* msp
= MessagePortService::GetInstance();
60 msp
->Create(MSG_ROUTING_NONE
, delegate
, port1
);
61 msp
->Create(MSG_ROUTING_NONE
, delegate
, port2
);
62 // Update the routing number of the message ports to be equal to the message
64 msp
->UpdateMessagePort(*port1
, delegate
, *port1
);
65 msp
->UpdateMessagePort(*port2
, delegate
, *port2
);
66 msp
->Entangle(*port1
, *port2
);
67 msp
->Entangle(*port2
, *port1
);
71 void MessagePortProvider::PostMessageToPort(
73 const MessagePortMessage
& message
,
74 const std::vector
<TransferredMessagePort
>& sent_ports
) {
75 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
76 MessagePortService
* msp
= MessagePortService::GetInstance();
77 msp
->PostMessage(sender_port_id
, message
, sent_ports
);
81 void MessagePortProvider::ClosePort(int message_port_id
) {
82 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
83 MessagePortService
* msp
= MessagePortService::GetInstance();
84 msp
->ClosePort(message_port_id
);
88 void MessagePortProvider::HoldMessages(int message_port_id
) {
89 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
90 MessagePortService
* msp
= MessagePortService::GetInstance();
91 msp
->HoldMessages(message_port_id
);
95 void MessagePortProvider::ReleaseMessages(int message_port_id
) {
96 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
97 MessagePortService
* msp
= MessagePortService::GetInstance();
98 msp
->ReleaseMessages(message_port_id
);
102 void MessagePortProvider::OnMessagePortDelegateClosing(
103 MessagePortDelegate
* delegate
) {
104 MessagePortService::GetInstance()->OnMessagePortDelegateClosing(delegate
);
108 void MessagePortProvider::UpdateMessagePort(int message_port_id
,
109 MessagePortDelegate
* delegate
) {
110 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
111 MessagePortService::GetInstance()->UpdateMessagePort(message_port_id
,
116 } // namespace content