Home | History | Annotate | Download | only in service_worker
      1 // Copyright 2014 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_registration_impl.h"
      6 
      7 #include "content/child/service_worker/service_worker_dispatcher.h"
      8 #include "content/child/service_worker/service_worker_registration_handle_reference.h"
      9 #include "content/child/service_worker/web_service_worker_impl.h"
     10 #include "content/common/service_worker/service_worker_types.h"
     11 #include "third_party/WebKit/public/platform/WebServiceWorkerRegistrationProxy.h"
     12 
     13 namespace content {
     14 
     15 WebServiceWorkerRegistrationImpl::QueuedTask::QueuedTask(
     16     QueuedTaskType type,
     17     blink::WebServiceWorker* worker)
     18     : type(type),
     19       worker(worker) {
     20 }
     21 
     22 WebServiceWorkerRegistrationImpl::WebServiceWorkerRegistrationImpl(
     23     scoped_ptr<ServiceWorkerRegistrationHandleReference> handle_ref)
     24     : handle_ref_(handle_ref.Pass()),
     25       proxy_(NULL) {
     26   DCHECK(handle_ref_);
     27   DCHECK_NE(kInvalidServiceWorkerRegistrationHandleId,
     28             handle_ref_->handle_id());
     29   ServiceWorkerDispatcher* dispatcher =
     30       ServiceWorkerDispatcher::GetThreadSpecificInstance();
     31   DCHECK(dispatcher);
     32   dispatcher->AddServiceWorkerRegistration(handle_ref_->handle_id(), this);
     33 }
     34 
     35 WebServiceWorkerRegistrationImpl::~WebServiceWorkerRegistrationImpl() {
     36   ServiceWorkerDispatcher* dispatcher =
     37       ServiceWorkerDispatcher::GetThreadSpecificInstance();
     38   if (dispatcher)
     39     dispatcher->RemoveServiceWorkerRegistration(handle_ref_->handle_id());
     40   ClearQueuedTasks();
     41 }
     42 
     43 void WebServiceWorkerRegistrationImpl::SetInstalling(
     44     blink::WebServiceWorker* service_worker) {
     45   if (proxy_)
     46     proxy_->setInstalling(service_worker);
     47   else
     48     queued_tasks_.push_back(QueuedTask(INSTALLING, service_worker));
     49 }
     50 
     51 void WebServiceWorkerRegistrationImpl::SetWaiting(
     52     blink::WebServiceWorker* service_worker) {
     53   if (proxy_)
     54     proxy_->setWaiting(service_worker);
     55   else
     56     queued_tasks_.push_back(QueuedTask(WAITING, service_worker));
     57 }
     58 
     59 void WebServiceWorkerRegistrationImpl::SetActive(
     60     blink::WebServiceWorker* service_worker) {
     61   if (proxy_)
     62     proxy_->setActive(service_worker);
     63   else
     64     queued_tasks_.push_back(QueuedTask(ACTIVE, service_worker));
     65 }
     66 
     67 void WebServiceWorkerRegistrationImpl::OnUpdateFound() {
     68   if (proxy_)
     69     proxy_->dispatchUpdateFoundEvent();
     70   else
     71     queued_tasks_.push_back(QueuedTask(UPDATE_FOUND, NULL));
     72 }
     73 
     74 void WebServiceWorkerRegistrationImpl::setProxy(
     75     blink::WebServiceWorkerRegistrationProxy* proxy) {
     76   proxy_ = proxy;
     77   RunQueuedTasks();
     78 }
     79 
     80 void WebServiceWorkerRegistrationImpl::RunQueuedTasks() {
     81   DCHECK(proxy_);
     82   for (std::vector<QueuedTask>::const_iterator it = queued_tasks_.begin();
     83        it != queued_tasks_.end(); ++it) {
     84     if (it->type == INSTALLING)
     85       proxy_->setInstalling(it->worker);
     86     else if (it->type == WAITING)
     87       proxy_->setWaiting(it->worker);
     88     else if (it->type == ACTIVE)
     89       proxy_->setActive(it->worker);
     90     else if (it->type == UPDATE_FOUND)
     91       proxy_->dispatchUpdateFoundEvent();
     92   }
     93   queued_tasks_.clear();
     94 }
     95 
     96 void WebServiceWorkerRegistrationImpl::ClearQueuedTasks() {
     97   for (std::vector<QueuedTask>::const_iterator it = queued_tasks_.begin();
     98        it != queued_tasks_.end(); ++it) {
     99     // If the owner of the WebServiceWorker does not exist, delete it.
    100     if (it->worker && !it->worker->proxy())
    101       delete it->worker;
    102   }
    103   queued_tasks_.clear();
    104 }
    105 
    106 blink::WebServiceWorkerRegistrationProxy*
    107 WebServiceWorkerRegistrationImpl::proxy() {
    108   return proxy_;
    109 }
    110 
    111 blink::WebURL WebServiceWorkerRegistrationImpl::scope() const {
    112   return handle_ref_->scope();
    113 }
    114 
    115 }  // namespace content
    116