sgurun | 4780212 | 2014-11-19 20:16:57 | [diff] [blame] | 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. |
| 4 | |
| 5 | #include "content/public/browser/message_port_provider.h" |
| 6 | |
| 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" |
sgurun | d2a43060 | 2015-01-23 20:54:05 | [diff] [blame] | 15 | #include "content/public/browser/message_port_delegate.h" |
sgurun | 4780212 | 2014-11-19 20:16:57 | [diff] [blame] | 16 | |
| 17 | namespace content { |
| 18 | |
sgurun | d2a43060 | 2015-01-23 20:54:05 | [diff] [blame] | 19 | // static |
sgurun | 4780212 | 2014-11-19 20:16:57 | [diff] [blame] | 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, |
mek | 5b679c9 | 2015-02-28 02:38:06 | [diff] [blame] | 25 | const std::vector<TransferredMessagePort>& ports) { |
sgurun | d2a43060 | 2015-01-23 20:54:05 | [diff] [blame] | 26 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
sgurun | 4780212 | 2014-11-19 20:16:57 | [diff] [blame] | 27 | |
mek | 5b679c9 | 2015-02-28 02:38:06 | [diff] [blame] | 28 | ViewMsg_PostMessage_Params params; |
| 29 | params.is_data_raw_string = true; |
| 30 | params.data = data; |
sgurun | d2a43060 | 2015-01-23 20:54:05 | [diff] [blame] | 31 | // Blink requires a source frame to transfer ports. This is why a |
| 32 | // source routing id is set here. See WebDOMMessageEvent::initMessageEvent() |
mek | 5b679c9 | 2015-02-28 02:38:06 | [diff] [blame] | 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; |
sgurun | 4780212 | 2014-11-19 20:16:57 | [diff] [blame] | 37 | |
sgurun | 4780212 | 2014-11-19 20:16:57 | [diff] [blame] | 38 | RenderProcessHostImpl* rph = |
sgurun | d2a43060 | 2015-01-23 20:54:05 | [diff] [blame] | 39 | static_cast<RenderProcessHostImpl*>(web_contents->GetRenderProcessHost()); |
sgurun | d2a43060 | 2015-01-23 20:54:05 | [diff] [blame] | 40 | BrowserThread::PostTask( |
mek | 5b679c9 | 2015-02-28 02:38:06 | [diff] [blame] | 41 | BrowserThread::IO, FROM_HERE, |
| 42 | base::Bind(&MessagePortMessageFilter::RouteMessageEventWithMessagePorts, |
| 43 | rph->message_port_message_filter(), |
| 44 | web_contents->GetRoutingID(), params)); |
sgurun | 4780212 | 2014-11-19 20:16:57 | [diff] [blame] | 45 | } |
| 46 | |
sgurun | d2a43060 | 2015-01-23 20:54:05 | [diff] [blame] | 47 | // static |
| 48 | void MessagePortProvider::CreateMessageChannel(MessagePortDelegate* delegate, |
sgurun | 4780212 | 2014-11-19 20:16:57 | [diff] [blame] | 49 | int* port1, |
| 50 | int* port2) { |
sgurun | d2a43060 | 2015-01-23 20:54:05 | [diff] [blame] | 51 | DCHECK_CURRENTLY_ON(BrowserThread::IO); |
sgurun | 4780212 | 2014-11-19 20:16:57 | [diff] [blame] | 52 | *port1 = 0; |
| 53 | *port2 = 0; |
sgurun | 4780212 | 2014-11-19 20:16:57 | [diff] [blame] | 54 | MessagePortService* msp = MessagePortService::GetInstance(); |
sgurun | d2a43060 | 2015-01-23 20:54:05 | [diff] [blame] | 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 |
| 58 | // port numbers. |
| 59 | msp->UpdateMessagePort(*port1, delegate, *port1); |
| 60 | msp->UpdateMessagePort(*port2, delegate, *port2); |
sgurun | 77dabb2b | 2014-12-02 20:50:02 | [diff] [blame] | 61 | msp->Entangle(*port1, *port2); |
| 62 | msp->Entangle(*port2, *port1); |
sgurun | 4780212 | 2014-11-19 20:16:57 | [diff] [blame] | 63 | } |
| 64 | |
sgurun | d2a43060 | 2015-01-23 20:54:05 | [diff] [blame] | 65 | // static |
sgurun | 92afec5 | 2015-01-29 23:08:00 | [diff] [blame] | 66 | void MessagePortProvider::PostMessageToPort( |
| 67 | int sender_port_id, |
mek | 09ac29cee | 2015-02-27 19:12:39 | [diff] [blame] | 68 | const MessagePortMessage& message, |
mek | 5b679c9 | 2015-02-28 02:38:06 | [diff] [blame] | 69 | const std::vector<TransferredMessagePort>& sent_ports) { |
sgurun | 92afec5 | 2015-01-29 23:08:00 | [diff] [blame] | 70 | DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 71 | MessagePortService* msp = MessagePortService::GetInstance(); |
mek | 09ac29cee | 2015-02-27 19:12:39 | [diff] [blame] | 72 | msp->PostMessage(sender_port_id, message, sent_ports); |
sgurun | 92afec5 | 2015-01-29 23:08:00 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | // static |
sgurun | 1f47db6e | 2015-02-26 18:50:05 | [diff] [blame] | 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); |
| 80 | } |
| 81 | |
| 82 | // static |
sgurun | 7981504 | 2015-03-10 19:27:47 | [diff] [blame] | 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); |
| 87 | } |
| 88 | |
| 89 | // static |
| 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); |
| 94 | } |
| 95 | |
| 96 | // static |
sgurun | d2a43060 | 2015-01-23 20:54:05 | [diff] [blame] | 97 | void MessagePortProvider::OnMessagePortDelegateClosing( |
| 98 | MessagePortDelegate* delegate) { |
| 99 | MessagePortService::GetInstance()->OnMessagePortDelegateClosing(delegate); |
| 100 | } |
| 101 | |
sgurun | 758671bd | 2015-03-12 01:11:47 | [diff] [blame] | 102 | // static |
| 103 | void MessagePortProvider::UpdateMessagePort(int message_port_id, |
| 104 | MessagePortDelegate* delegate) { |
| 105 | DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 106 | MessagePortService::GetInstance()->UpdateMessagePort(message_port_id, |
| 107 | delegate, |
| 108 | message_port_id); |
| 109 | } |
| 110 | |
sgurun | 4780212 | 2014-11-19 20:16:57 | [diff] [blame] | 111 | } // namespace content |