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 "url/gurl.h" 16 17 namespace content { 18 19 class ServiceWorkerRegistrationInfo; 20 class ServiceWorkerVersion; 21 22 // This class manages all persistence of service workers: 23 // - Registrations 24 // - Mapping of caches to registrations / versions 25 // 26 // This is the place where we manage simultaneous 27 // requests for the same registrations and caches, making sure that 28 // two pages that are registering the same pattern at the same time 29 // have their registrations coalesced rather than overwriting each 30 // other. 31 // 32 // This class also manages the state of the upgrade process, which 33 // includes managing which ServiceWorkerVersion is "active" vs "in 34 // waiting". 35 class CONTENT_EXPORT ServiceWorkerRegistration NON_EXPORTED_BASE(public base::RefCounted<ServiceWorkerRegistration>)36 : NON_EXPORTED_BASE(public base::RefCounted<ServiceWorkerRegistration>) { 37 public: 38 ServiceWorkerRegistration(const GURL& pattern, 39 const GURL& script_url, 40 int64 registration_id, 41 base::WeakPtr<ServiceWorkerContextCore> context); 42 43 int64 id() const { return registration_id_; } 44 const GURL& script_url() const { return script_url_; } 45 const GURL& pattern() const { return pattern_; } 46 47 ServiceWorkerVersion* active_version() const { 48 DCHECK(!is_shutdown_); 49 return active_version_.get(); 50 } 51 52 ServiceWorkerVersion* waiting_version() const { 53 DCHECK(!is_shutdown_); 54 return waiting_version_.get(); 55 } 56 57 void set_active_version(ServiceWorkerVersion* version) { 58 DCHECK(!is_shutdown_); 59 active_version_ = version; 60 } 61 62 void set_waiting_version(ServiceWorkerVersion* version) { 63 DCHECK(!is_shutdown_); 64 waiting_version_ = version; 65 } 66 67 ServiceWorkerRegistrationInfo GetInfo(); 68 69 // Returns the active version, if it is not null; otherwise, returns the 70 // waiting version. 71 ServiceWorkerVersion* GetNewestVersion(); 72 73 // The final synchronous switchover after all events have been 74 // fired, and the old "active version" is being shut down. 75 void ActivateWaitingVersion(); 76 77 private: 78 ~ServiceWorkerRegistration(); 79 friend class base::RefCounted<ServiceWorkerRegistration>; 80 81 const GURL pattern_; 82 const GURL script_url_; 83 const int64 registration_id_; 84 85 scoped_refptr<ServiceWorkerVersion> active_version_; 86 scoped_refptr<ServiceWorkerVersion> waiting_version_; 87 88 bool is_shutdown_; 89 base::WeakPtr<ServiceWorkerContextCore> context_; 90 91 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerRegistration); 92 }; 93 } // namespace content 94 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_REGISTRATION_H_ 95