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;
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),
29 ServiceWorkerDispatcher* dispatcher =
30 ServiceWorkerDispatcher::GetThreadSpecificInstance();
32 dispatcher->AddServiceWorker(handle_ref_->handle_id(), this);
35 WebServiceWorkerImpl::~WebServiceWorkerImpl() {
36 if (handle_ref_->handle_id() == kInvalidServiceWorkerHandleId)
38 ServiceWorkerDispatcher* dispatcher =
39 ServiceWorkerDispatcher::GetThreadSpecificInstance();
41 dispatcher->RemoveServiceWorker(handle_ref_->handle_id());
44 void WebServiceWorkerImpl::OnStateChanged(
45 blink::WebServiceWorkerState new_state) {
46 if (proxy_ && proxy_->isReady())
47 CommitState(new_state);
49 queued_states_.push_back(new_state);
52 void WebServiceWorkerImpl::setProxy(blink::WebServiceWorkerProxy* proxy) {
56 blink::WebServiceWorkerProxy* WebServiceWorkerImpl::proxy() {
60 void WebServiceWorkerImpl::proxyReadyChanged() {
61 if (!proxy_->isReady())
63 for (std::vector<blink::WebServiceWorkerState>::iterator it =
64 queued_states_.begin();
65 it != queued_states_.end();
69 queued_states_.clear();
72 blink::WebURL WebServiceWorkerImpl::scope() const {
73 return handle_ref_->scope();
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 thread_safe_sender_->Send(new ServiceWorkerHostMsg_PostMessageToWorker(
87 handle_ref_->handle_id(),
89 WebMessagePortChannelImpl::ExtractMessagePortIDs(channels)));
92 void WebServiceWorkerImpl::CommitState(blink::WebServiceWorkerState new_state) {
94 DCHECK(proxy_->isReady());
96 proxy_->dispatchStateChangeEvent();
99 } // namespace content