OLD | NEW |
(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 WebConnectCallbacksImpl |
| 17 : public blink::WebServicePortConnectEventCallbacks { |
| 18 public: |
| 19 WebConnectCallbacksImpl( |
| 20 const ServicePortDispatcher::ConnectCallback& callback) |
| 21 : callback_(callback) {} |
| 22 |
| 23 ~WebConnectCallbacksImpl() override {} |
| 24 |
| 25 void onSuccess(blink::WebServicePort* port) override { |
| 26 callback_.Run(SERVICE_PORT_CONNECT_RESULT_ACCEPT, |
| 27 mojo::String::From<base::string16>(port->name), |
| 28 mojo::String::From<base::string16>(port->data)); |
| 29 } |
| 30 |
| 31 void onError() override { |
| 32 callback_.Run(SERVICE_PORT_CONNECT_RESULT_REJECT, mojo::String(""), |
| 33 mojo::String("")); |
| 34 } |
| 35 |
| 36 private: |
| 37 ServicePortDispatcher::ConnectCallback callback_; |
| 38 }; |
| 39 |
| 40 } // namespace |
| 41 |
| 42 void ServicePortDispatcherImpl::Create( |
| 43 base::WeakPtr<blink::WebServiceWorkerContextProxy> proxy, |
| 44 mojo::InterfaceRequest<ServicePortDispatcher> request) { |
| 45 new ServicePortDispatcherImpl(proxy, request.Pass()); |
| 46 } |
| 47 |
| 48 ServicePortDispatcherImpl::~ServicePortDispatcherImpl() { |
| 49 WorkerTaskRunner::Instance()->RemoveStopObserver(this); |
| 50 } |
| 51 |
| 52 ServicePortDispatcherImpl::ServicePortDispatcherImpl( |
| 53 base::WeakPtr<blink::WebServiceWorkerContextProxy> proxy, |
| 54 mojo::InterfaceRequest<ServicePortDispatcher> request) |
| 55 : binding_(this, request.Pass()), proxy_(proxy) { |
| 56 WorkerTaskRunner::Instance()->AddStopObserver(this); |
| 57 } |
| 58 |
| 59 void ServicePortDispatcherImpl::OnWorkerRunLoopStopped() { |
| 60 delete this; |
| 61 } |
| 62 |
| 63 void ServicePortDispatcherImpl::Connect(const mojo::String& target_url, |
| 64 const mojo::String& origin, |
| 65 int32_t port_id, |
| 66 const ConnectCallback& callback) { |
| 67 if (!proxy_) { |
| 68 callback.Run(SERVICE_PORT_CONNECT_RESULT_REJECT, mojo::String(""), |
| 69 mojo::String("")); |
| 70 return; |
| 71 } |
| 72 TRACE_EVENT0("ServiceWorker", "ServicePortDispatcherImpl::Connect"); |
| 73 proxy_->dispatchServicePortConnectEvent(new WebConnectCallbacksImpl(callback), |
| 74 target_url.To<GURL>(), |
| 75 origin.To<base::string16>(), port_id); |
| 76 } |
| 77 |
| 78 } // namespace content |
OLD | NEW |