Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / content / child / navigator_connect / service_port_provider.cc
blob3f63b5f90ef974319c42e7914ede3f9d769710c8
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"
19 namespace content {
21 namespace {
23 void ConnectToServiceOnMainThread(
24 mojo::InterfaceRequest<ServicePortService> ptr) {
25 ChildThreadImpl::current()->service_registry()->ConnectToRemoteService(
26 ptr.Pass());
29 } // namespace
31 ServicePortProvider::ServicePortProvider(
32 blink::WebServicePortProviderClient* client,
33 const scoped_refptr<base::SingleThreadTaskRunner>& main_loop)
34 : client_(client), binding_(this), main_loop_(main_loop) {
35 DCHECK(client_);
36 AddRef();
39 void ServicePortProvider::destroy() {
40 Release();
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
61 // services.
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,
70 base::Bind(
71 &WebMessagePortChannelImpl::ExtractMessagePortIDsWithoutQueueing,
72 base::Passed(&channel_array)),
73 base::Bind(&ServicePortProvider::PostMessageToBrowser, this, port_id,
74 message));
77 void ServicePortProvider::closePort(blink::WebServicePortID port_id) {
78 GetServicePortServicePtr()->ClosePort(port_id);
81 void ServicePortProvider::PostMessage(
82 int32_t port_id,
83 const mojo::String& message,
84 mojo::Array<MojoTransferredMessagePortPtr> ports,
85 mojo::Array<int32_t> new_routing_ids) {
86 client_->postMessage(port_id, message.To<base::string16>(),
87 WebMessagePortChannelImpl::CreatePorts(
88 ports.To<std::vector<TransferredMessagePort>>(),
89 new_routing_ids.To<std::vector<int>>(), main_loop_));
92 ServicePortProvider::~ServicePortProvider() {
95 void ServicePortProvider::PostMessageToBrowser(
96 int port_id,
97 const base::string16& message,
98 const std::vector<TransferredMessagePort> ports) {
99 GetServicePortServicePtr()->PostMessage(
100 port_id, mojo::String::From(message),
101 mojo::Array<MojoTransferredMessagePortPtr>::From(ports));
104 void ServicePortProvider::OnConnectResult(
105 scoped_ptr<blink::WebServicePortConnectCallbacks> callbacks,
106 ServicePortConnectResult result,
107 int32_t port_id) {
108 if (result == SERVICE_PORT_CONNECT_RESULT_ACCEPT) {
109 callbacks->onSuccess(new blink::WebServicePortID(port_id));
110 } else {
111 callbacks->onError();
115 ServicePortServicePtr& ServicePortProvider::GetServicePortServicePtr() {
116 if (!service_port_service_.get()) {
117 mojo::InterfaceRequest<ServicePortService> request =
118 mojo::GetProxy(&service_port_service_);
119 main_loop_->PostTask(FROM_HERE, base::Bind(&ConnectToServiceOnMainThread,
120 base::Passed(&request)));
122 // Setup channel for browser to post events back to this class.
123 ServicePortServiceClientPtr client_ptr;
124 binding_.Bind(GetProxy(&client_ptr));
125 service_port_service_->SetClient(client_ptr.Pass());
127 return service_port_service_;
130 } // namespace content