Home | History | Annotate | Download | only in service_worker
      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 #ifndef CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_REGISTRATION_H_
      6 #define CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_REGISTRATION_H_
      7 
      8 #include "base/basictypes.h"
      9 #include "base/gtest_prod_util.h"
     10 #include "base/logging.h"
     11 #include "base/memory/ref_counted.h"
     12 #include "base/memory/scoped_ptr.h"
     13 #include "content/browser/service_worker/service_worker_version.h"
     14 #include "content/common/content_export.h"
     15 #include "content/common/service_worker/service_worker_types.h"
     16 #include "url/gurl.h"
     17 
     18 namespace content {
     19 
     20 class ServiceWorkerRegistrationInfo;
     21 class ServiceWorkerVersion;
     22 
     23 // This class represents a Service Worker registration. The scope is constant
     24 // for the life of the persistent registration. It's refcounted to facilitate
     25 // multiple controllees being associated with the same registration.
     26 class CONTENT_EXPORT ServiceWorkerRegistration
     27     : NON_EXPORTED_BASE(public base::RefCounted<ServiceWorkerRegistration>),
     28       public ServiceWorkerVersion::Listener {
     29  public:
     30   typedef base::Callback<void(ServiceWorkerStatusCode status)> StatusCallback;
     31 
     32   class Listener {
     33    public:
     34     virtual void OnVersionAttributesChanged(
     35         ServiceWorkerRegistration* registration,
     36         ChangedVersionAttributesMask changed_mask,
     37         const ServiceWorkerRegistrationInfo& info) {}
     38     virtual void OnRegistrationFailed(
     39         ServiceWorkerRegistration* registration) {}
     40     virtual void OnRegistrationFinishedUninstalling(
     41         ServiceWorkerRegistration* registration) {}
     42     virtual void OnUpdateFound(
     43         ServiceWorkerRegistration* registration) {}
     44   };
     45 
     46   ServiceWorkerRegistration(const GURL& pattern,
     47                             int64 registration_id,
     48                             base::WeakPtr<ServiceWorkerContextCore> context);
     49 
     50   int64 id() const { return registration_id_; }
     51   const GURL& pattern() const { return pattern_; }
     52 
     53   bool is_deleted() const { return is_deleted_; }
     54   void set_is_deleted(bool deleted) { is_deleted_ = deleted; }
     55 
     56   bool is_uninstalling() const { return is_uninstalling_; }
     57   bool is_uninstalled() const { return is_uninstalled_; }
     58 
     59   ServiceWorkerVersion* active_version() const {
     60     return active_version_.get();
     61   }
     62 
     63   ServiceWorkerVersion* waiting_version() const {
     64     return waiting_version_.get();
     65   }
     66 
     67   ServiceWorkerVersion* installing_version() const {
     68     return installing_version_.get();
     69   }
     70 
     71   ServiceWorkerVersion* GetNewestVersion() const;
     72 
     73   void AddListener(Listener* listener);
     74   void RemoveListener(Listener* listener);
     75   void NotifyRegistrationFailed();
     76   void NotifyUpdateFound();
     77 
     78   ServiceWorkerRegistrationInfo GetInfo();
     79 
     80   // Sets the corresposding version attribute and resets the position
     81   // (if any) left vacant (ie. by a waiting version being promoted).
     82   // Also notifies listeners via OnVersionAttributesChanged.
     83   void SetActiveVersion(ServiceWorkerVersion* version);
     84   void SetWaitingVersion(ServiceWorkerVersion* version);
     85   void SetInstallingVersion(ServiceWorkerVersion* version);
     86 
     87   // If version is the installing, waiting, active version of this
     88   // registation, the method will reset that field to NULL, and notify
     89   // listeners via OnVersionAttributesChanged.
     90   void UnsetVersion(ServiceWorkerVersion* version);
     91 
     92   // Triggers the [[Activate]] algorithm when the currently active version
     93   // has no controllees. If there are no controllees at the time the method
     94   // is called, activation is initiated immediately.
     95   void ActivateWaitingVersionWhenReady();
     96 
     97   // Triggers the [[ClearRegistration]] algorithm when the currently
     98   // active version has no controllees. Deletes this registration
     99   // from storage immediately.
    100   void ClearWhenReady();
    101 
    102   // Restores this registration in storage and cancels the pending
    103   // [[ClearRegistration]] algorithm.
    104   void AbortPendingClear(const StatusCallback& callback);
    105 
    106   // The time of the most recent update check.
    107   base::Time last_update_check() const { return last_update_check_; }
    108   void set_last_update_check(base::Time last) { last_update_check_ = last; }
    109 
    110  private:
    111   friend class base::RefCounted<ServiceWorkerRegistration>;
    112 
    113   virtual ~ServiceWorkerRegistration();
    114 
    115   void SetVersionInternal(
    116       ServiceWorkerVersion* version,
    117       scoped_refptr<ServiceWorkerVersion>* data_member,
    118       int change_flag);
    119   void UnsetVersionInternal(
    120       ServiceWorkerVersion* version,
    121       ChangedVersionAttributesMask* mask);
    122 
    123   // ServiceWorkerVersion::Listener override.
    124   virtual void OnNoControllees(ServiceWorkerVersion* version) OVERRIDE;
    125 
    126   // This method corresponds to the [[Activate]] algorithm.
    127   void ActivateWaitingVersion();
    128   void OnActivateEventFinished(
    129       ServiceWorkerVersion* activating_version,
    130       ServiceWorkerStatusCode status);
    131   void OnDeleteFinished(ServiceWorkerStatusCode status);
    132 
    133   // This method corresponds to the [[ClearRegistration]] algorithm.
    134   void Clear();
    135 
    136   void OnRestoreFinished(const StatusCallback& callback,
    137                          scoped_refptr<ServiceWorkerVersion> version,
    138                          ServiceWorkerStatusCode status);
    139 
    140   const GURL pattern_;
    141   const int64 registration_id_;
    142   bool is_deleted_;
    143   bool is_uninstalling_;
    144   bool is_uninstalled_;
    145   bool should_activate_when_ready_;
    146   base::Time last_update_check_;
    147   scoped_refptr<ServiceWorkerVersion> active_version_;
    148   scoped_refptr<ServiceWorkerVersion> waiting_version_;
    149   scoped_refptr<ServiceWorkerVersion> installing_version_;
    150   ObserverList<Listener> listeners_;
    151   base::WeakPtr<ServiceWorkerContextCore> context_;
    152 
    153   DISALLOW_COPY_AND_ASSIGN(ServiceWorkerRegistration);
    154 };
    155 
    156 }  // namespace content
    157 
    158 #endif  // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_REGISTRATION_H_
    159