1 // Copyright 2013 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/service_worker/web_service_worker_impl.h"
7 #include "content/child/service_worker/service_worker_dispatcher.h"
8 #include "content/child/service_worker/service_worker_handle_reference.h"
9 #include "content/child/thread_safe_sender.h"
10 #include "content/child/webmessageportchannel_impl.h"
11 #include "content/common/service_worker/service_worker_messages.h"
12 #include "third_party/WebKit/public/platform/WebString.h"
13 #include "third_party/WebKit/public/platform/modules/serviceworker/WebServiceWorkerProxy.h"
15 using blink::WebMessagePortChannel
;
16 using blink::WebMessagePortChannelArray
;
17 using blink::WebMessagePortChannelClient
;
18 using blink::WebString
;
24 void SendPostMessageToWorkerOnMainThread(
25 ThreadSafeSender
* thread_safe_sender
,
27 const base::string16
& message
,
28 scoped_ptr
<WebMessagePortChannelArray
> channels
) {
29 thread_safe_sender
->Send(new ServiceWorkerHostMsg_PostMessageToWorker(
31 WebMessagePortChannelImpl::ExtractMessagePortIDs(channels
.Pass())));
36 WebServiceWorkerImpl::WebServiceWorkerImpl(
37 scoped_ptr
<ServiceWorkerHandleReference
> handle_ref
,
38 ThreadSafeSender
* thread_safe_sender
)
39 : handle_ref_(handle_ref
.Pass()),
40 state_(handle_ref_
->state()),
41 thread_safe_sender_(thread_safe_sender
),
43 ServiceWorkerDispatcher
* dispatcher
=
44 ServiceWorkerDispatcher::GetThreadSpecificInstance();
46 dispatcher
->AddServiceWorker(handle_ref_
->handle_id(), this);
49 WebServiceWorkerImpl::~WebServiceWorkerImpl() {
50 if (handle_ref_
->handle_id() == kInvalidServiceWorkerHandleId
)
52 ServiceWorkerDispatcher
* dispatcher
=
53 ServiceWorkerDispatcher::GetThreadSpecificInstance();
55 dispatcher
->RemoveServiceWorker(handle_ref_
->handle_id());
58 void WebServiceWorkerImpl::OnStateChanged(
59 blink::WebServiceWorkerState new_state
) {
62 // TODO(nhiroki): This is a quick fix for http://crbug.com/507110
65 proxy_
->dispatchStateChangeEvent();
68 void WebServiceWorkerImpl::setProxy(blink::WebServiceWorkerProxy
* proxy
) {
72 blink::WebServiceWorkerProxy
* WebServiceWorkerImpl::proxy() {
76 blink::WebURL
WebServiceWorkerImpl::url() const {
77 return handle_ref_
->url();
80 blink::WebServiceWorkerState
WebServiceWorkerImpl::state() const {
84 void WebServiceWorkerImpl::postMessage(const WebString
& message
,
85 WebMessagePortChannelArray
* channels
) {
86 ServiceWorkerDispatcher
* dispatcher
=
87 ServiceWorkerDispatcher::GetThreadSpecificInstance();
90 // This may send channels for MessagePorts, and all internal book-keeping
91 // messages for MessagePort (e.g. QueueMessages) are sent from main thread
92 // (with thread hopping), so we need to do the same thread hopping here not
93 // to overtake those messages.
94 dispatcher
->main_thread_task_runner()->PostTask(
95 FROM_HERE
, base::Bind(&SendPostMessageToWorkerOnMainThread
,
96 thread_safe_sender_
, handle_ref_
->handle_id(),
97 // We cast WebString to string16 before crossing
98 // threads for thread-safety.
99 static_cast<base::string16
>(message
),
100 base::Passed(make_scoped_ptr(channels
))));
103 void WebServiceWorkerImpl::terminate() {
104 thread_safe_sender_
->Send(
105 new ServiceWorkerHostMsg_TerminateWorker(handle_ref_
->handle_id()));
108 } // namespace content