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/browser/service_worker/service_worker_handle.h"
      6 
      7 #include "content/browser/service_worker/service_worker_context_core.h"
      8 #include "content/browser/service_worker/service_worker_registration.h"
      9 #include "content/common/service_worker/service_worker_messages.h"
     10 #include "ipc/ipc_sender.h"
     11 
     12 namespace content {
     13 
     14 namespace {
     15 
     16 blink::WebServiceWorkerState
     17 GetWebServiceWorkerState(ServiceWorkerVersion* version) {
     18   DCHECK(version);
     19   switch (version->status()) {
     20     case ServiceWorkerVersion::NEW:
     21       if (version->running_status() == ServiceWorkerVersion::RUNNING)
     22         return blink::WebServiceWorkerStateParsed;
     23       else
     24         return blink::WebServiceWorkerStateUnknown;
     25     case ServiceWorkerVersion::INSTALLING:
     26       return blink::WebServiceWorkerStateInstalling;
     27     case ServiceWorkerVersion::INSTALLED:
     28       return blink::WebServiceWorkerStateInstalled;
     29     case ServiceWorkerVersion::ACTIVATING:
     30       return blink::WebServiceWorkerStateActivating;
     31     case ServiceWorkerVersion::ACTIVE:
     32       return blink::WebServiceWorkerStateActive;
     33     case ServiceWorkerVersion::DEACTIVATED:
     34       return blink::WebServiceWorkerStateDeactivated;
     35   }
     36   NOTREACHED() << version->status();
     37   return blink::WebServiceWorkerStateUnknown;
     38 }
     39 
     40 }  // namespace
     41 
     42 scoped_ptr<ServiceWorkerHandle> ServiceWorkerHandle::Create(
     43     base::WeakPtr<ServiceWorkerContextCore> context,
     44     IPC::Sender* sender,
     45     int thread_id,
     46     ServiceWorkerVersion* version) {
     47   if (!context || !version)
     48     return scoped_ptr<ServiceWorkerHandle>();
     49   ServiceWorkerRegistration* registration =
     50       context->GetLiveRegistration(version->registration_id());
     51   return make_scoped_ptr(
     52       new ServiceWorkerHandle(context, sender, thread_id,
     53                               registration, version));
     54 }
     55 
     56 ServiceWorkerHandle::ServiceWorkerHandle(
     57     base::WeakPtr<ServiceWorkerContextCore> context,
     58     IPC::Sender* sender,
     59     int thread_id,
     60     ServiceWorkerRegistration* registration,
     61     ServiceWorkerVersion* version)
     62     : context_(context),
     63       sender_(sender),
     64       thread_id_(thread_id),
     65       handle_id_(context.get() ? context->GetNewServiceWorkerHandleId() : -1),
     66       ref_count_(1),
     67       registration_(registration),
     68       version_(version) {
     69   version_->AddListener(this);
     70 }
     71 
     72 ServiceWorkerHandle::~ServiceWorkerHandle() {
     73   version_->RemoveListener(this);
     74   // TODO(kinuko): At this point we can discard the registration if
     75   // all documents/handles that have a reference to the registration is
     76   // closed or freed up, but could also keep it alive in cache
     77   // (e.g. in context_) for a while with some timer so that we don't
     78   // need to re-load the same registration from disk over and over.
     79 }
     80 
     81 void ServiceWorkerHandle::OnWorkerStarted(ServiceWorkerVersion* version) {
     82 }
     83 
     84 void ServiceWorkerHandle::OnWorkerStopped(ServiceWorkerVersion* version) {
     85 }
     86 
     87 void ServiceWorkerHandle::OnErrorReported(ServiceWorkerVersion* version,
     88                                           const base::string16& error_message,
     89                                           int line_number,
     90                                           int column_number,
     91                                           const GURL& source_url) {
     92 }
     93 
     94 void ServiceWorkerHandle::OnReportConsoleMessage(ServiceWorkerVersion* version,
     95                                                  int source_identifier,
     96                                                  int message_level,
     97                                                  const base::string16& message,
     98                                                  int line_number,
     99                                                  const GURL& source_url) {
    100 }
    101 
    102 void ServiceWorkerHandle::OnVersionStateChanged(ServiceWorkerVersion* version) {
    103   sender_->Send(new ServiceWorkerMsg_ServiceWorkerStateChanged(
    104       thread_id_, handle_id_, GetWebServiceWorkerState(version)));
    105 }
    106 
    107 ServiceWorkerObjectInfo ServiceWorkerHandle::GetObjectInfo() {
    108   ServiceWorkerObjectInfo info;
    109   info.handle_id = handle_id_;
    110   info.scope = registration_->pattern();
    111   info.url = registration_->script_url();
    112   info.state = GetWebServiceWorkerState(version_);
    113   return info;
    114 }
    115 
    116 void ServiceWorkerHandle::IncrementRefCount() {
    117   DCHECK_GT(ref_count_, 0);
    118   ++ref_count_;
    119 }
    120 
    121 void ServiceWorkerHandle::DecrementRefCount() {
    122   DCHECK_GE(ref_count_, 0);
    123   --ref_count_;
    124 }
    125 
    126 }  // namespace content
    127