Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / content / child / service_worker / web_service_worker_impl.cc
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.
4
5 #include "content/child/service_worker/web_service_worker_impl.h"
6
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"
14
15 using blink::WebMessagePortChannel;
16 using blink::WebMessagePortChannelArray;
17 using blink::WebMessagePortChannelClient;
18 using blink::WebString;
19
20 namespace content {
21
22 WebServiceWorkerImpl::WebServiceWorkerImpl(
23     const ServiceWorkerObjectInfo& info,
24     ThreadSafeSender* thread_safe_sender)
25     : handle_ref_(
26           ServiceWorkerHandleReference::CreateForDeleter(info,
27                                                          thread_safe_sender)),
28       state_(handle_ref_->state()),
29       thread_safe_sender_(thread_safe_sender),
30       proxy_(NULL) {
31   ServiceWorkerDispatcher* dispatcher =
32       ServiceWorkerDispatcher::GetThreadSpecificInstance();
33   DCHECK(dispatcher);
34   dispatcher->AddServiceWorker(handle_ref_->handle_id(), this);
35 }
36
37 WebServiceWorkerImpl::WebServiceWorkerImpl(
38     scoped_ptr<ServiceWorkerHandleReference> handle_ref,
39     ThreadSafeSender* thread_safe_sender)
40     : handle_ref_(handle_ref.Pass()),
41       state_(handle_ref_->state()),
42       thread_safe_sender_(thread_safe_sender),
43       proxy_(NULL) {
44   ServiceWorkerDispatcher* dispatcher =
45       ServiceWorkerDispatcher::GetThreadSpecificInstance();
46   DCHECK(dispatcher);
47   dispatcher->AddServiceWorker(handle_ref_->handle_id(), this);
48 }
49
50 WebServiceWorkerImpl::~WebServiceWorkerImpl() {
51   if (handle_ref_->handle_id() == kInvalidServiceWorkerHandleId)
52     return;
53   ServiceWorkerDispatcher* dispatcher =
54       ServiceWorkerDispatcher::GetThreadSpecificInstance();
55   if (dispatcher)
56     dispatcher->RemoveServiceWorker(handle_ref_->handle_id());
57 }
58
59 void WebServiceWorkerImpl::OnStateChanged(
60     blink::WebServiceWorkerState new_state) {
61   DCHECK(proxy_);
62   if (proxy_->isReady())
63     ChangeState(new_state);
64   else
65     queued_states_.push_back(new_state);
66 }
67
68 void WebServiceWorkerImpl::setProxy(blink::WebServiceWorkerProxy* proxy) {
69   proxy_ = proxy;
70 }
71
72 void WebServiceWorkerImpl::proxyReadyChanged() {
73   if (!proxy_->isReady())
74     return;
75   for (std::vector<blink::WebServiceWorkerState>::iterator it =
76            queued_states_.begin();
77        it != queued_states_.end();
78        ++it) {
79     ChangeState(*it);
80   }
81   queued_states_.clear();
82 }
83
84 blink::WebURL WebServiceWorkerImpl::scope() const {
85   return handle_ref_->scope();
86 }
87
88 blink::WebURL WebServiceWorkerImpl::url() const {
89   return handle_ref_->url();
90 }
91
92 blink::WebServiceWorkerState WebServiceWorkerImpl::state() const {
93   return state_;
94 }
95
96 void WebServiceWorkerImpl::postMessage(const WebString& message,
97                                        WebMessagePortChannelArray* channels) {
98   thread_safe_sender_->Send(new ServiceWorkerHostMsg_PostMessageToWorker(
99       handle_ref_->handle_id(),
100       message,
101       WebMessagePortChannelImpl::ExtractMessagePortIDs(channels)));
102 }
103
104 void WebServiceWorkerImpl::ChangeState(blink::WebServiceWorkerState new_state) {
105   DCHECK(proxy_);
106   DCHECK(proxy_->isReady());
107   state_ = new_state;
108   proxy_->dispatchStateChangeEvent();
109 }
110
111 }  // namespace content