Bug 1842509 - Remove media.webvtt.regions.enabled pref r=alwu,webidl,smaug,peterv
[gecko.git] / dom / serviceworkers / ServiceWorkerRegistrar.h
blob20c1cc530c5cca20d95c474a83e0d3233666f3d1
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/. */
7 #ifndef mozilla_dom_ServiceWorkerRegistrar_h
8 #define mozilla_dom_ServiceWorkerRegistrar_h
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"
19 #define SERVICEWORKERREGISTRAR_FILE u"serviceworker.txt"
20 #define SERVICEWORKERREGISTRAR_VERSION "9"
21 #define SERVICEWORKERREGISTRAR_TERMINATOR "#"
22 #define SERVICEWORKERREGISTRAR_TRUE "true"
23 #define SERVICEWORKERREGISTRAR_FALSE "false"
25 class nsIFile;
27 namespace mozilla {
29 namespace ipc {
30 class PrincipalInfo;
31 } // namespace ipc
33 namespace dom {
35 class ServiceWorkerRegistrationData;
37 } // namespace mozilla
39 namespace mozilla::dom {
41 class ServiceWorkerRegistrar : public nsIObserver,
42 public nsIAsyncShutdownBlocker {
43 friend class ServiceWorkerRegistrarSaveDataRunnable;
45 public:
46 NS_DECL_THREADSAFE_ISUPPORTS
47 NS_DECL_NSIOBSERVER
48 NS_DECL_NSIASYNCSHUTDOWNBLOCKER
50 static void Initialize();
52 void Shutdown();
54 void DataSaved(uint32_t aFileGeneration);
56 static already_AddRefed<ServiceWorkerRegistrar> Get();
58 void GetRegistrations(nsTArray<ServiceWorkerRegistrationData>& aValues);
60 void RegisterServiceWorker(const ServiceWorkerRegistrationData& aData);
61 void UnregisterServiceWorker(
62 const mozilla::ipc::PrincipalInfo& aPrincipalInfo,
63 const nsACString& aScope);
64 void RemoveAll();
66 bool ReloadDataForTest();
68 protected:
69 // These methods are protected because we test this class using gTest
70 // subclassing it.
71 void LoadData();
72 nsresult SaveData(const nsTArray<ServiceWorkerRegistrationData>& aData);
74 nsresult ReadData();
75 nsresult WriteData(const nsTArray<ServiceWorkerRegistrationData>& aData);
76 void DeleteData();
78 void RegisterServiceWorkerInternal(const ServiceWorkerRegistrationData& aData)
79 MOZ_REQUIRES(mMonitor);
81 ServiceWorkerRegistrar();
82 virtual ~ServiceWorkerRegistrar();
84 private:
85 void ProfileStarted();
86 void ProfileStopped();
88 void MaybeScheduleSaveData();
89 void ShutdownCompleted();
90 void MaybeScheduleShutdownCompleted();
92 uint32_t GetNextGeneration();
93 void MaybeResetGeneration();
95 nsCOMPtr<nsIAsyncShutdownClient> GetShutdownPhase() const;
97 bool IsSupportedVersion(const nsACString& aVersion) const;
99 protected:
100 mozilla::Monitor mMonitor;
102 // protected by mMonitor.
103 nsCOMPtr<nsIFile> mProfileDir MOZ_GUARDED_BY(mMonitor);
104 // Read on mainthread, modified on background thread EXCEPT for
105 // ReloadDataForTest() AND for gtest, which modifies this on MainThread.
106 nsTArray<ServiceWorkerRegistrationData> mData MOZ_GUARDED_BY(mMonitor);
107 bool mDataLoaded MOZ_GUARDED_BY(mMonitor);
109 // PBackground thread only
110 uint32_t mDataGeneration;
111 uint32_t mFileGeneration;
112 uint32_t mRetryCount;
113 bool mShuttingDown;
114 bool mSaveDataRunnableDispatched;
117 } // namespace mozilla::dom
119 #endif // mozilla_dom_ServiceWorkerRegistrar_h