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

Unified 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 side-by-side diff with in-line comments
Download patch
Index: content/child/navigator_connect/service_port_dispatcher_impl.cc
diff --git a/content/child/navigator_connect/service_port_dispatcher_impl.cc b/content/child/navigator_connect/service_port_dispatcher_impl.cc
new file mode 100644
index 0000000000000000000000000000000000000000..589e1f342d84fe58b5d828be126af9ef9cc2ef61
--- /dev/null
+++ b/content/child/navigator_connect/service_port_dispatcher_impl.cc
@@ -0,0 +1,76 @@
+// Copyright 2015 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "content/child/navigator_connect/service_port_dispatcher_impl.h"
+
+#include "base/trace_event/trace_event.h"
+#include "mojo/common/common_type_converters.h"
+#include "mojo/common/url_type_converters.h"
+#include "third_party/WebKit/public/web/WebServiceWorkerContextProxy.h"
+
+namespace content {
+
+namespace {
+
+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!
+ public:
+ ConnectCallbacks(const ServicePortDispatcher::ConnectCallback& callback)
+ : callback_(callback) {}
+
+ ~ConnectCallbacks() override {}
+
+ void onSuccess(blink::WebServicePort* port) override {
+ callback_.Run(SERVICE_PORT_CONNECT_RESULT_ACCEPT,
+ mojo::String::From<base::string16>(port->name),
+ mojo::String::From<base::string16>(port->data));
+ }
+
+ void onError() override {
+ callback_.Run(SERVICE_PORT_CONNECT_RESULT_REJECT, mojo::String(""),
+ mojo::String(""));
+ }
+
+ private:
+ ServicePortDispatcher::ConnectCallback callback_;
+};
+
+} // namespace
+
+void ServicePortDispatcherImpl::Create(
+ base::WeakPtr<blink::WebServiceWorkerContextProxy> proxy,
+ mojo::InterfaceRequest<ServicePortDispatcher> request) {
+ new ServicePortDispatcherImpl(proxy, request.Pass());
+}
+
+ServicePortDispatcherImpl::~ServicePortDispatcherImpl() {
+ WorkerTaskRunner::Instance()->RemoveStopObserver(this);
+}
+
+ServicePortDispatcherImpl::ServicePortDispatcherImpl(
+ base::WeakPtr<blink::WebServiceWorkerContextProxy> proxy,
+ mojo::InterfaceRequest<ServicePortDispatcher> request)
+ : binding_(this, request.Pass()), proxy_(proxy) {
+ WorkerTaskRunner::Instance()->AddStopObserver(this);
+}
+
+void ServicePortDispatcherImpl::OnWorkerRunLoopStopped() {
+ delete this;
+}
+
+void ServicePortDispatcherImpl::Connect(const mojo::String& target_url,
+ const mojo::String& origin,
+ int32_t port_id,
+ const ConnectCallback& callback) {
+ if (!proxy_) {
+ callback.Run(SERVICE_PORT_CONNECT_RESULT_REJECT, mojo::String(""),
+ mojo::String(""));
+ return;
+ }
+ TRACE_EVENT0("ServiceWorker", "ServicePortDispatcherImpl::Connect");
+ proxy_->dispatchServicePortConnectEvent(new ConnectCallbacks(callback),
+ target_url.To<GURL>(),
+ origin.To<base::string16>(), port_id);
+}
+
+} // namespace content

Powered by Google App Engine
This is Rietveld 408576698