ServiceWorker: Send state change events via SWProviderHost
[chromium-blink-merge.git] / content / browser / service_worker / service_worker_handle.h
blob1de8155ec6f16cf19a7f0e0f36ae3b520314b8fe
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_HANDLE_H_
6 #define CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_HANDLE_H_
8 #include "base/basictypes.h"
9 #include "base/memory/ref_counted.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/memory/weak_ptr.h"
12 #include "content/browser/service_worker/service_worker_version.h"
13 #include "content/common/content_export.h"
14 #include "content/common/service_worker/service_worker_types.h"
16 namespace IPC {
17 class Sender;
20 namespace content {
22 class ServiceWorkerContextCore;
23 class ServiceWorkerRegistration;
25 // Roughly corresponds to one ServiceWorker object in the renderer process
26 // (WebServiceWorkerImpl).
27 // Has references to the corresponding ServiceWorkerVersion and
28 // ServiceWorkerRegistration (therefore they're guaranteed to be alive while
29 // this handle is around).
30 class CONTENT_EXPORT ServiceWorkerHandle
31 : NON_EXPORTED_BASE(public ServiceWorkerVersion::Listener) {
32 public:
33 // Creates a handle for a live version. The version's corresponding
34 // registration must be also alive.
35 // This may return NULL if |context|.get() or |version| is NULL.
36 static scoped_ptr<ServiceWorkerHandle> Create(
37 base::WeakPtr<ServiceWorkerContextCore> context,
38 base::WeakPtr<ServiceWorkerProviderHost> provider_host,
39 ServiceWorkerVersion* version);
41 ServiceWorkerHandle(base::WeakPtr<ServiceWorkerContextCore> context,
42 base::WeakPtr<ServiceWorkerProviderHost> provider_host,
43 ServiceWorkerRegistration* registration,
44 ServiceWorkerVersion* version);
45 ~ServiceWorkerHandle() override;
47 // ServiceWorkerVersion::Listener overrides.
48 void OnVersionStateChanged(ServiceWorkerVersion* version) override;
50 ServiceWorkerObjectInfo GetObjectInfo();
52 int handle_id() const { return handle_id_; }
53 ServiceWorkerRegistration* registration() { return registration_.get(); }
54 ServiceWorkerVersion* version() { return version_.get(); }
56 bool HasNoRefCount() const { return ref_count_ <= 0; }
57 void IncrementRefCount();
58 void DecrementRefCount();
60 private:
61 base::WeakPtr<ServiceWorkerContextCore> context_;
62 base::WeakPtr<ServiceWorkerProviderHost> provider_host_;
63 const int handle_id_;
64 int ref_count_; // Created with 1.
65 scoped_refptr<ServiceWorkerRegistration> registration_;
66 scoped_refptr<ServiceWorkerVersion> version_;
68 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerHandle);
71 } // namespace content
73 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_HANDLE_H_