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.
5 #ifndef CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_REGISTRATION_H_
6 #define CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_REGISTRATION_H_
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"
19 class ServiceWorkerRegistrationInfo;
20 class ServiceWorkerVersion;
22 // This class manages all persistence of service workers:
24 // - Mapping of caches to registrations / versions
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
32 // This class also manages the state of the upgrade process, which
33 // includes managing which ServiceWorkerVersion is "active" vs "in
34 // waiting" (or "pending")
35 class CONTENT_EXPORT ServiceWorkerRegistration
36 : NON_EXPORTED_BASE(public base::RefCounted<ServiceWorkerRegistration>) {
38 ServiceWorkerRegistration(const GURL& pattern,
39 const GURL& script_url,
40 int64 registration_id,
41 base::WeakPtr<ServiceWorkerContextCore> context);
43 int64 id() const { return registration_id_; }
44 const GURL& script_url() const { return script_url_; }
45 const GURL& pattern() const { return pattern_; }
47 ServiceWorkerVersion* active_version() const {
48 DCHECK(!is_shutdown_);
49 return active_version_.get();
52 ServiceWorkerVersion* pending_version() const {
53 DCHECK(!is_shutdown_);
54 return pending_version_.get();
57 void set_active_version(ServiceWorkerVersion* version) {
58 DCHECK(!is_shutdown_);
59 active_version_ = version;
62 void set_pending_version(ServiceWorkerVersion* version) {
63 DCHECK(!is_shutdown_);
64 pending_version_ = version;
67 ServiceWorkerRegistrationInfo GetInfo();
69 // Returns the active version, if it is not null; otherwise, returns the
71 ServiceWorkerVersion* GetNewestVersion();
73 // The final synchronous switchover after all events have been
74 // fired, and the old "active version" is being shut down.
75 void ActivatePendingVersion();
78 ~ServiceWorkerRegistration();
79 friend class base::RefCounted<ServiceWorkerRegistration>;
82 const GURL script_url_;
83 const int64 registration_id_;
85 scoped_refptr<ServiceWorkerVersion> active_version_;
86 scoped_refptr<ServiceWorkerVersion> pending_version_;
89 base::WeakPtr<ServiceWorkerContextCore> context_;
91 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerRegistration);
93 } // namespace content
94 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_REGISTRATION_H_