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 params
.source_routing_id
= MSG_ROUTING_NONE
;
32 params
.source_origin
= source_origin
;
33 params
.target_origin
= target_origin
;
34 params
.message_ports
= ports
;
36 RenderProcessHostImpl
* rph
=
37 static_cast<RenderProcessHostImpl
*>(web_contents
->GetRenderProcessHost());
38 BrowserThread::PostTask(
39 BrowserThread::IO
, FROM_HERE
,
40 base::Bind(&MessagePortMessageFilter::RouteMessageEventWithMessagePorts
,
41 rph
->message_port_message_filter(),
42 web_contents
->GetMainFrame()->GetRoutingID(), params
));
46 void MessagePortProvider::CreateMessageChannel(MessagePortDelegate
* delegate
,
49 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
52 MessagePortService
* msp
= MessagePortService::GetInstance();
53 msp
->Create(MSG_ROUTING_NONE
, delegate
, port1
);
54 msp
->Create(MSG_ROUTING_NONE
, delegate
, port2
);
55 // Update the routing number of the message ports to be equal to the message
57 msp
->UpdateMessagePort(*port1
, delegate
, *port1
);
58 msp
->UpdateMessagePort(*port2
, delegate
, *port2
);
59 msp
->Entangle(*port1
, *port2
);
60 msp
->Entangle(*port2
, *port1
);
64 void MessagePortProvider::PostMessageToPort(
66 const MessagePortMessage
& message
,
67 const std::vector
<TransferredMessagePort
>& sent_ports
) {
68 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
69 MessagePortService
* msp
= MessagePortService::GetInstance();
70 msp
->PostMessage(sender_port_id
, message
, sent_ports
);
74 void MessagePortProvider::ClosePort(int message_port_id
) {
75 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
76 MessagePortService
* msp
= MessagePortService::GetInstance();
77 msp
->ClosePort(message_port_id
);
81 void MessagePortProvider::HoldMessages(int message_port_id
) {
82 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
83 MessagePortService
* msp
= MessagePortService::GetInstance();
84 msp
->HoldMessages(message_port_id
);
88 void MessagePortProvider::ReleaseMessages(int message_port_id
) {
89 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
90 MessagePortService
* msp
= MessagePortService::GetInstance();
91 msp
->ReleaseMessages(message_port_id
);
95 void MessagePortProvider::OnMessagePortDelegateClosing(
96 MessagePortDelegate
* delegate
) {
97 MessagePortService::GetInstance()->OnMessagePortDelegateClosing(delegate
);
101 void MessagePortProvider::UpdateMessagePort(int message_port_id
,
102 MessagePortDelegate
* delegate
) {
103 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
104 MessagePortService::GetInstance()->UpdateMessagePort(message_port_id
,
109 } // namespace content