Source code

Revision control

Other Tools

1
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
3
/* This Source Code Form is subject to the terms of the Mozilla Public
4
* License, v. 2.0. If a copy of the MPL was not distributed with this
5
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7
#ifndef mozilla_dom_ServiceWorkerRegistrar_h
8
#define mozilla_dom_ServiceWorkerRegistrar_h
9
10
#include "mozilla/Monitor.h"
11
#include "mozilla/Telemetry.h"
12
#include "nsClassHashtable.h"
13
#include "nsIAsyncShutdown.h"
14
#include "nsIObserver.h"
15
#include "nsCOMPtr.h"
16
#include "nsString.h"
17
#include "nsTArray.h"
18
19
#define SERVICEWORKERREGISTRAR_FILE "serviceworker.txt"
20
#define SERVICEWORKERREGISTRAR_VERSION "8"
21
#define SERVICEWORKERREGISTRAR_TERMINATOR "#"
22
#define SERVICEWORKERREGISTRAR_TRUE "true"
23
#define SERVICEWORKERREGISTRAR_FALSE "false"
24
25
class nsIFile;
26
27
namespace mozilla {
28
29
namespace ipc {
30
class PrincipalInfo;
31
} // namespace ipc
32
33
namespace dom {
34
35
class ServiceWorkerRegistrationData;
36
37
class ServiceWorkerRegistrar : public nsIObserver,
38
public nsIAsyncShutdownBlocker {
39
friend class ServiceWorkerRegistrarSaveDataRunnable;
40
41
public:
42
NS_DECL_THREADSAFE_ISUPPORTS
43
NS_DECL_NSIOBSERVER
44
NS_DECL_NSIASYNCSHUTDOWNBLOCKER
45
46
static void Initialize();
47
48
void Shutdown();
49
50
void DataSaved(uint32_t aFileGeneration);
51
52
static already_AddRefed<ServiceWorkerRegistrar> Get();
53
54
void GetRegistrations(nsTArray<ServiceWorkerRegistrationData>& aValues);
55
56
void RegisterServiceWorker(const ServiceWorkerRegistrationData& aData);
57
void UnregisterServiceWorker(
58
const mozilla::ipc::PrincipalInfo& aPrincipalInfo,
59
const nsACString& aScope);
60
void RemoveAll();
61
62
protected:
63
// These methods are protected because we test this class using gTest
64
// subclassing it.
65
void LoadData();
66
nsresult SaveData(const nsTArray<ServiceWorkerRegistrationData>& aData);
67
68
nsresult ReadData();
69
nsresult WriteData(const nsTArray<ServiceWorkerRegistrationData>& aData);
70
void DeleteData();
71
72
void RegisterServiceWorkerInternal(
73
const ServiceWorkerRegistrationData& aData);
74
75
ServiceWorkerRegistrar();
76
virtual ~ServiceWorkerRegistrar();
77
78
private:
79
void ProfileStarted();
80
void ProfileStopped();
81
82
void MaybeScheduleSaveData();
83
void ShutdownCompleted();
84
void MaybeScheduleShutdownCompleted();
85
86
uint32_t GetNextGeneration();
87
void MaybeResetGeneration();
88
89
nsCOMPtr<nsIAsyncShutdownClient> GetShutdownPhase() const;
90
91
bool IsSupportedVersion(const nsACString& aVersion) const;
92
93
mozilla::Monitor mMonitor;
94
95
protected:
96
// protected by mMonitor.
97
nsCOMPtr<nsIFile> mProfileDir;
98
nsTArray<ServiceWorkerRegistrationData> mData;
99
bool mDataLoaded;
100
101
// PBackground thread only
102
uint32_t mDataGeneration;
103
uint32_t mFileGeneration;
104
uint32_t mRetryCount;
105
bool mShuttingDown;
106
bool mRunnableDispatched;
107
};
108
109
} // namespace dom
110
} // namespace mozilla
111
112
#endif // mozilla_dom_ServiceWorkerRegistrar_h