Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(243)

Side by Side Diff: content/child/navigator_connect/service_port_provider.cc

Issue 1192713004: Update client side navigator.connect API to use ServicePortCollection [2/3] (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: address comments Created 5 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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/child/navigator_connect/service_port_provider.h"
6
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/WebServi cePortProviderClient.h"
18
19 namespace content {
20
21 namespace {
22
23 void ConnectToServiceOnMainThread(
24 mojo::InterfaceRequest<ServicePortService> ptr) {
25 ChildThreadImpl::current()->service_registry()->ConnectToRemoteService(
26 ptr.Pass());
27 }
28
29 } // namespace
30
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();
37 }
38
39 void ServicePortProvider::destroy() {
40 Release();
41 }
42
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)));
54 }
55
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.
62
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));
75 }
76
77 void ServicePortProvider::closePort(blink::WebServicePortID port_id) {
78 GetServicePortServicePtr()->ClosePort(port_id);
79 }
80
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_));
90 }
91
92 ServicePortProvider::~ServicePortProvider() {
93 }
94
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));
102 }
103
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();
112 }
113 }
114
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)));
121
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());
126 }
127 return service_port_service_;
128 }
129
130 } // namespace content
OLDNEW
« no previous file with comments | « content/child/navigator_connect/service_port_provider.h ('k') | content/child/webmessageportchannel_impl.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698