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"
18 void MessagePortProvider::PostMessageToFrame(
19 WebContents
* web_contents
,
20 const base::string16
& source_origin
,
21 const base::string16
& target_origin
,
22 const base::string16
& data
,
23 const std::vector
<int>& ports
) {
24 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO
));
26 RenderViewHost
* rvh
= web_contents
->GetRenderViewHost();
30 ViewMsg_PostMessage_Params params
;
31 params
.is_data_raw_string
= true;
33 params
.source_routing_id
= web_contents
->GetRoutingID();
34 params
.source_origin
= source_origin
;
35 params
.target_origin
= target_origin
;
36 RenderProcessHostImpl
* rph
=
37 static_cast<RenderProcessHostImpl
*>(
38 web_contents
->GetRenderProcessHost());
39 MessagePortMessageFilter
* mf
= rph
->message_port_message_filter();
42 params
.message_port_ids
= ports
;
43 mf
->UpdateMessagePortsWithNewRoutes(params
.message_port_ids
,
44 ¶ms
.new_routing_ids
);
46 rvh
->Send(new ViewMsg_PostMessageEvent(
47 web_contents
->GetRenderViewHost()->GetRoutingID(),
51 void MessagePortProvider::CreateMessageChannel(WebContents
* web_contents
,
54 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO
));
59 RenderProcessHostImpl
* rph
=
60 static_cast<RenderProcessHostImpl
*>(
61 web_contents
->GetRenderProcessHost());
62 MessagePortMessageFilter
* mf
= rph
->message_port_message_filter();
63 MessagePortService
* msp
= MessagePortService::GetInstance();
64 msp
->Create(mf
->GetNextRoutingID(), mf
, port1
);
65 msp
->Create(mf
->GetNextRoutingID(), mf
, port2
);
68 } // namespace content