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/view_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 ViewMsg_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 params
.source_routing_id
= web_contents
->GetRoutingID();
34 params
.source_origin
= source_origin
;
35 params
.target_origin
= target_origin
;
36 params
.message_ports
= ports
;
38 RenderProcessHostImpl
* rph
=
39 static_cast<RenderProcessHostImpl
*>(web_contents
->GetRenderProcessHost());
40 BrowserThread::PostTask(
41 BrowserThread::IO
, FROM_HERE
,
42 base::Bind(&MessagePortMessageFilter::RouteMessageEventWithMessagePorts
,
43 rph
->message_port_message_filter(),
44 web_contents
->GetRoutingID(), params
));
48 void MessagePortProvider::CreateMessageChannel(MessagePortDelegate
* delegate
,
51 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
54 MessagePortService
* msp
= MessagePortService::GetInstance();
55 msp
->Create(MSG_ROUTING_NONE
, delegate
, port1
);
56 msp
->Create(MSG_ROUTING_NONE
, delegate
, port2
);
57 // Update the routing number of the message ports to be equal to the message
59 msp
->UpdateMessagePort(*port1
, delegate
, *port1
);
60 msp
->UpdateMessagePort(*port2
, delegate
, *port2
);
61 msp
->Entangle(*port1
, *port2
);
62 msp
->Entangle(*port2
, *port1
);
66 void MessagePortProvider::PostMessageToPort(
68 const MessagePortMessage
& message
,
69 const std::vector
<TransferredMessagePort
>& sent_ports
) {
70 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
71 MessagePortService
* msp
= MessagePortService::GetInstance();
72 msp
->PostMessage(sender_port_id
, message
, sent_ports
);
76 void MessagePortProvider::ClosePort(int message_port_id
) {
77 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
78 MessagePortService
* msp
= MessagePortService::GetInstance();
79 msp
->ClosePort(message_port_id
);
83 void MessagePortProvider::HoldMessages(int message_port_id
) {
84 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
85 MessagePortService
* msp
= MessagePortService::GetInstance();
86 msp
->HoldMessages(message_port_id
);
90 void MessagePortProvider::ReleaseMessages(int message_port_id
) {
91 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
92 MessagePortService
* msp
= MessagePortService::GetInstance();
93 msp
->ReleaseMessages(message_port_id
);
97 void MessagePortProvider::OnMessagePortDelegateClosing(
98 MessagePortDelegate
* delegate
) {
99 MessagePortService::GetInstance()->OnMessagePortDelegateClosing(delegate
);
103 void MessagePortProvider::UpdateMessagePort(int message_port_id
,
104 MessagePortDelegate
* delegate
) {
105 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
106 MessagePortService::GetInstance()->UpdateMessagePort(message_port_id
,
111 } // namespace content