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.
5 #include "content/child/navigator_connect/service_port_dispatcher_impl.h"
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/modules/serviceworker/WebServiceWorkerContextProxy.h"
16 class WebConnectCallbacksImpl
17 : public blink::WebServicePortConnectEventCallbacks
{
19 WebConnectCallbacksImpl(
20 const ServicePortDispatcher::ConnectCallback
& callback
)
21 : callback_(callback
) {}
23 ~WebConnectCallbacksImpl() override
{}
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
));
31 void onError() override
{
32 callback_
.Run(SERVICE_PORT_CONNECT_RESULT_REJECT
, mojo::String(""),
37 ServicePortDispatcher::ConnectCallback callback_
;
42 void ServicePortDispatcherImpl::Create(
43 base::WeakPtr
<blink::WebServiceWorkerContextProxy
> proxy
,
44 mojo::InterfaceRequest
<ServicePortDispatcher
> request
) {
45 new ServicePortDispatcherImpl(proxy
, request
.Pass());
48 ServicePortDispatcherImpl::~ServicePortDispatcherImpl() {
49 WorkerTaskRunner::Instance()->RemoveStopObserver(this);
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);
59 void ServicePortDispatcherImpl::OnWorkerRunLoopStopped() {
63 void ServicePortDispatcherImpl::Connect(const mojo::String
& target_url
,
64 const mojo::String
& origin
,
66 const ConnectCallback
& callback
) {
68 callback
.Run(SERVICE_PORT_CONNECT_RESULT_REJECT
, mojo::String(""),
72 TRACE_EVENT0("ServiceWorker", "ServicePortDispatcherImpl::Connect");
73 proxy_
->dispatchServicePortConnectEvent(new WebConnectCallbacksImpl(callback
),
74 target_url
.To
<GURL
>(),
75 origin
.To
<base::string16
>(), port_id
);
78 } // namespace content