Make USB permissions work in the new permission message system
[chromium-blink-merge.git] / content / child / navigator_connect / service_port_provider.cc
blobc66a748dc1877689f65a1691e80e4a3f90533b6d
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 // We cast WebString to string16 before crossing threads.
75 // for thread-safety.
76 static_cast<base::string16>(message)));
79 void ServicePortProvider::closePort(blink::WebServicePortID port_id) {
80 GetServicePortServicePtr()->ClosePort(port_id);
83 void ServicePortProvider::PostMessage(
84 int32_t port_id,
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(
98 int port_id,
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,
109 int32_t port_id) {
110 if (result == SERVICE_PORT_CONNECT_RESULT_ACCEPT) {
111 callbacks->onSuccess(new blink::WebServicePortID(port_id));
112 } else {
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