Blink roll 25b6bd3a7a131ffe68d809546ad1a20707915cdc:3a503f41ae42e5b79cfcd2ff10e65afde...
[chromium-blink-merge.git] / content / browser / message_port_provider.cc
blobd00fdf1328617f5f9b88b0654f3de4839cfb371e
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"
16 namespace content {
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();
27 if (!rvh)
28 return;
30 ViewMsg_PostMessage_Params params;
31 params.is_data_raw_string = true;
32 params.data = data;
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();
41 if (!ports.empty()) {
42 params.message_port_ids = ports;
43 mf->UpdateMessagePortsWithNewRoutes(params.message_port_ids,
44 &params.new_routing_ids);
46 rvh->Send(new ViewMsg_PostMessageEvent(
47 web_contents->GetRenderViewHost()->GetRoutingID(),
48 params));
51 void MessagePortProvider::CreateMessageChannel(WebContents* web_contents,
52 int* port1,
53 int* port2) {
54 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
56 *port1 = 0;
57 *port2 = 0;
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