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.
5 #ifndef CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_REGISTRATION_HANDLE_H_
6 #define CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_REGISTRATION_HANDLE_H_
8 #include "base/memory/ref_counted.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/memory/weak_ptr.h"
11 #include "content/browser/service_worker/service_worker_registration.h"
12 #include "content/browser/service_worker/service_worker_version.h"
13 #include "content/common/service_worker/service_worker_types.h"
17 class ServiceWorkerContextCore
;
18 class ServiceWorkerDispatcherHost
;
19 class ServiceWorkerVersion
;
21 // Roughly corresponds to one WebServiceWorkerRegistration object in the
24 // The renderer process maintains the reference count by owning a
25 // ServiceWorkerRegistrationHandleReference for each reference it has to the
26 // registration object. ServiceWorkerRegistrationHandleReference creation and
27 // destruction sends an IPC to the browser process, which adjusts the
28 // ServiceWorkerRegistrationHandle refcount.
30 // Has a reference to the corresponding ServiceWorkerRegistration in order to
31 // ensure that the registration is alive while this handle is around.
32 class ServiceWorkerRegistrationHandle
33 : public ServiceWorkerRegistration::Listener
{
35 CONTENT_EXPORT
ServiceWorkerRegistrationHandle(
36 base::WeakPtr
<ServiceWorkerContextCore
> context
,
37 base::WeakPtr
<ServiceWorkerProviderHost
> provider_host
,
38 ServiceWorkerRegistration
* registration
);
39 virtual ~ServiceWorkerRegistrationHandle();
41 ServiceWorkerRegistrationObjectInfo
GetObjectInfo();
43 bool HasNoRefCount() const { return ref_count_
<= 0; }
44 void IncrementRefCount();
45 void DecrementRefCount();
47 int provider_id() const { return provider_id_
; }
48 int handle_id() const { return handle_id_
; }
50 ServiceWorkerRegistration
* registration() { return registration_
.get(); }
53 // ServiceWorkerRegistration::Listener overrides.
54 void OnVersionAttributesChanged(
55 ServiceWorkerRegistration
* registration
,
56 ChangedVersionAttributesMask changed_mask
,
57 const ServiceWorkerRegistrationInfo
& info
) override
;
58 void OnRegistrationFailed(ServiceWorkerRegistration
* registration
) override
;
59 void OnUpdateFound(ServiceWorkerRegistration
* registration
) override
;
61 // Sets the corresponding version field to the given version or if the given
62 // version is nullptr, clears the field.
63 void SetVersionAttributes(
64 ChangedVersionAttributesMask changed_mask
,
65 ServiceWorkerVersion
* installing_version
,
66 ServiceWorkerVersion
* waiting_version
,
67 ServiceWorkerVersion
* active_version
);
69 base::WeakPtr
<ServiceWorkerContextCore
> context_
;
70 base::WeakPtr
<ServiceWorkerProviderHost
> provider_host_
;
71 const int provider_id_
;
73 int ref_count_
; // Created with 1.
75 // This handle is the primary owner of this registration.
76 scoped_refptr
<ServiceWorkerRegistration
> registration_
;
78 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerRegistrationHandle
);
81 } // namespace content
83 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_REGISTRATION_HANDLE_H_