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/child/navigator_connect/service_port_provider.h"
7 #include "base/lazy_instance.h"
8 #include "base/single_thread_task_runner.h"
9 #include "base/task_runner_util.h"
10 #include "content/child/child_thread_impl.h"
11 #include "content/child/webmessageportchannel_impl.h"
12 #include "content/common/message_port_messages.h"
13 #include "content/common/service_port_type_converters.h"
14 #include "content/public/common/navigator_connect_client.h"
15 #include "mojo/common/common_type_converters.h"
16 #include "third_party/WebKit/public/platform/WebURL.h"
17 #include "third_party/WebKit/public/platform/modules/navigator_services/WebServicePortProviderClient.h"
23 void ConnectToServiceOnMainThread(
24 mojo::InterfaceRequest
<ServicePortService
> ptr
) {
25 ChildThreadImpl::current()->service_registry()->ConnectToRemoteService(
31 ServicePortProvider::ServicePortProvider(
32 blink::WebServicePortProviderClient
* client
,
33 const scoped_refptr
<base::SingleThreadTaskRunner
>& main_loop
)
34 : client_(client
), binding_(this), main_loop_(main_loop
) {
39 void ServicePortProvider::destroy() {
43 void ServicePortProvider::connect(
44 const blink::WebURL
& target_url
,
45 const blink::WebString
& origin
,
46 blink::WebServicePortConnectCallbacks
* raw_callbacks
) {
47 scoped_ptr
<blink::WebServicePortConnectCallbacks
> callbacks(raw_callbacks
);
48 // base::Unretained is safe here, as the mojo channel will be deleted (and
49 // will wipe its callbacks) before 'this' is deleted.
50 GetServicePortServicePtr()->Connect(
51 target_url
.string().utf8(), origin
.utf8(),
52 base::Bind(&ServicePortProvider::OnConnectResult
, base::Unretained(this),
53 base::Passed(&callbacks
)));
56 void ServicePortProvider::postMessage(
57 blink::WebServicePortID port_id
,
58 const blink::WebString
& message
,
59 blink::WebMessagePortChannelArray
* channels
) {
60 // TODO(mek): If necesary, handle sending messages as values for system
63 // Have to extract IDs for the transferred MessagePorts on the main thread
64 // to make sure all ports have been fully initialized. Actually sending the
65 // message can safely be done on this thread, and using mojo, since there
66 // shouldn't be any other IPCs where ordering matters.
67 scoped_ptr
<blink::WebMessagePortChannelArray
> channel_array(channels
);
68 base::PostTaskAndReplyWithResult(
69 main_loop_
.get(), FROM_HERE
,
71 &WebMessagePortChannelImpl::ExtractMessagePortIDsWithoutQueueing
,
72 base::Passed(&channel_array
)),
73 base::Bind(&ServicePortProvider::PostMessageToBrowser
, this, port_id
,
74 // We cast WebString to string16 before crossing threads.
76 static_cast<base::string16
>(message
)));
79 void ServicePortProvider::closePort(blink::WebServicePortID port_id
) {
80 GetServicePortServicePtr()->ClosePort(port_id
);
83 void ServicePortProvider::PostMessage(
85 const mojo::String
& message
,
86 mojo::Array
<MojoTransferredMessagePortPtr
> ports
,
87 mojo::Array
<int32_t> new_routing_ids
) {
88 client_
->postMessage(port_id
, message
.To
<base::string16
>(),
89 WebMessagePortChannelImpl::CreatePorts(
90 ports
.To
<std::vector
<TransferredMessagePort
>>(),
91 new_routing_ids
.To
<std::vector
<int>>(), main_loop_
));
94 ServicePortProvider::~ServicePortProvider() {
97 void ServicePortProvider::PostMessageToBrowser(
99 const base::string16
& message
,
100 const std::vector
<TransferredMessagePort
> ports
) {
101 GetServicePortServicePtr()->PostMessage(
102 port_id
, mojo::String::From(message
),
103 mojo::Array
<MojoTransferredMessagePortPtr
>::From(ports
));
106 void ServicePortProvider::OnConnectResult(
107 scoped_ptr
<blink::WebServicePortConnectCallbacks
> callbacks
,
108 ServicePortConnectResult result
,
110 if (result
== SERVICE_PORT_CONNECT_RESULT_ACCEPT
) {
111 callbacks
->onSuccess(new blink::WebServicePortID(port_id
));
113 callbacks
->onError();
117 ServicePortServicePtr
& ServicePortProvider::GetServicePortServicePtr() {
118 if (!service_port_service_
.get()) {
119 mojo::InterfaceRequest
<ServicePortService
> request
=
120 mojo::GetProxy(&service_port_service_
);
121 main_loop_
->PostTask(FROM_HERE
, base::Bind(&ConnectToServiceOnMainThread
,
122 base::Passed(&request
)));
124 // Setup channel for browser to post events back to this class.
125 ServicePortServiceClientPtr client_ptr
;
126 binding_
.Bind(GetProxy(&client_ptr
));
127 service_port_service_
->SetClient(client_ptr
.Pass());
129 return service_port_service_
;
132 } // namespace content