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

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

Issue 1210643002: Update navigator.services API to use the new services.onconnect event [2/3]. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@serviceport
Patch Set: properly close connections when a worker is stopped 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 2015 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_dispatcher_impl.h"
6
7 #include "base/trace_event/trace_event.h"
8 #include "mojo/common/common_type_converters.h"
9 #include "mojo/common/url_type_converters.h"
10 #include "third_party/WebKit/public/web/WebServiceWorkerContextProxy.h"
11
12 namespace content {
13
14 namespace {
15
16 class ConnectCallbacks : public blink::WebServicePortConnectEventCallbacks {
michaeln 2015/07/15 02:47:26 nit: maybe name this differently to make it more o
Marijn Kruisselbrink 2015/07/15 20:49:40 Good point, done.
michaeln 2015/07/16 22:23:46 thnx!
17 public:
18 ConnectCallbacks(const ServicePortDispatcher::ConnectCallback& callback)
19 : callback_(callback) {}
20
21 ~ConnectCallbacks() override {}
22
23 void onSuccess(blink::WebServicePort* port) override {
24 callback_.Run(SERVICE_PORT_CONNECT_RESULT_ACCEPT,
25 mojo::String::From<base::string16>(port->name),
26 mojo::String::From<base::string16>(port->data));
27 }
28
29 void onError() override {
30 callback_.Run(SERVICE_PORT_CONNECT_RESULT_REJECT, mojo::String(""),
31 mojo::String(""));
32 }
33
34 private:
35 ServicePortDispatcher::ConnectCallback callback_;
36 };
37
38 } // namespace
39
40 void ServicePortDispatcherImpl::Create(
41 base::WeakPtr<blink::WebServiceWorkerContextProxy> proxy,
42 mojo::InterfaceRequest<ServicePortDispatcher> request) {
43 new ServicePortDispatcherImpl(proxy, request.Pass());
44 }
45
46 ServicePortDispatcherImpl::~ServicePortDispatcherImpl() {
47 WorkerTaskRunner::Instance()->RemoveStopObserver(this);
48 }
49
50 ServicePortDispatcherImpl::ServicePortDispatcherImpl(
51 base::WeakPtr<blink::WebServiceWorkerContextProxy> proxy,
52 mojo::InterfaceRequest<ServicePortDispatcher> request)
53 : binding_(this, request.Pass()), proxy_(proxy) {
54 WorkerTaskRunner::Instance()->AddStopObserver(this);
55 }
56
57 void ServicePortDispatcherImpl::OnWorkerRunLoopStopped() {
58 delete this;
59 }
60
61 void ServicePortDispatcherImpl::Connect(const mojo::String& target_url,
62 const mojo::String& origin,
63 int32_t port_id,
64 const ConnectCallback& callback) {
65 if (!proxy_) {
66 callback.Run(SERVICE_PORT_CONNECT_RESULT_REJECT, mojo::String(""),
67 mojo::String(""));
68 return;
69 }
70 TRACE_EVENT0("ServiceWorker", "ServicePortDispatcherImpl::Connect");
71 proxy_->dispatchServicePortConnectEvent(new ConnectCallbacks(callback),
72 target_url.To<GURL>(),
73 origin.To<base::string16>(), port_id);
74 }
75
76 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698