Re-subimission of https://codereview.chromium.org/1041213003/
[chromium-blink-merge.git] / content / child / service_worker / web_service_worker_impl.cc
blob0939aaa7b6d83c6056b8f34151179307418ace4c
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/WebServiceWorkerProxy.h"
13 #include "third_party/WebKit/public/platform/WebString.h"
15 using blink::WebMessagePortChannel;
16 using blink::WebMessagePortChannelArray;
17 using blink::WebMessagePortChannelClient;
18 using blink::WebString;
20 namespace content {
22 WebServiceWorkerImpl::WebServiceWorkerImpl(
23 scoped_ptr<ServiceWorkerHandleReference> handle_ref,
24 ThreadSafeSender* thread_safe_sender)
25 : handle_ref_(handle_ref.Pass()),
26 state_(handle_ref_->state()),
27 thread_safe_sender_(thread_safe_sender),
28 proxy_(NULL) {
29 ServiceWorkerDispatcher* dispatcher =
30 ServiceWorkerDispatcher::GetThreadSpecificInstance();
31 DCHECK(dispatcher);
32 dispatcher->AddServiceWorker(handle_ref_->handle_id(), this);
35 WebServiceWorkerImpl::~WebServiceWorkerImpl() {
36 if (handle_ref_->handle_id() == kInvalidServiceWorkerHandleId)
37 return;
38 ServiceWorkerDispatcher* dispatcher =
39 ServiceWorkerDispatcher::GetThreadSpecificInstance();
40 if (dispatcher)
41 dispatcher->RemoveServiceWorker(handle_ref_->handle_id());
44 void WebServiceWorkerImpl::OnStateChanged(
45 blink::WebServiceWorkerState new_state) {
46 state_ = new_state;
47 proxy_->dispatchStateChangeEvent();
50 void WebServiceWorkerImpl::setProxy(blink::WebServiceWorkerProxy* proxy) {
51 proxy_ = proxy;
54 blink::WebServiceWorkerProxy* WebServiceWorkerImpl::proxy() {
55 return proxy_;
58 blink::WebURL WebServiceWorkerImpl::url() const {
59 return handle_ref_->url();
62 blink::WebServiceWorkerState WebServiceWorkerImpl::state() const {
63 return state_;
66 void WebServiceWorkerImpl::postMessage(const WebString& message,
67 WebMessagePortChannelArray* channels) {
68 thread_safe_sender_->Send(new ServiceWorkerHostMsg_PostMessageToWorker(
69 handle_ref_->handle_id(),
70 message,
71 WebMessagePortChannelImpl::ExtractMessagePortIDs(channels)));
74 void WebServiceWorkerImpl::terminate() {
75 thread_safe_sender_->Send(
76 new ServiceWorkerHostMsg_TerminateWorker(handle_ref_->handle_id()));
79 } // namespace content