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"
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"
35 class ServiceWorkerRegistrationData
;
37 } // namespace mozilla
39 namespace mozilla::dom
{
41 class ServiceWorkerRegistrar
: public nsIObserver
,
42 public nsIAsyncShutdownBlocker
{
43 friend class ServiceWorkerRegistrarSaveDataRunnable
;
46 NS_DECL_THREADSAFE_ISUPPORTS
48 NS_DECL_NSIASYNCSHUTDOWNBLOCKER
50 static void Initialize();
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
);
66 bool ReloadDataForTest();
69 // These methods are protected because we test this class using gTest
72 nsresult
SaveData(const nsTArray
<ServiceWorkerRegistrationData
>& aData
);
75 nsresult
WriteData(const nsTArray
<ServiceWorkerRegistrationData
>& aData
);
78 void RegisterServiceWorkerInternal(const ServiceWorkerRegistrationData
& aData
)
79 MOZ_REQUIRES(mMonitor
);
81 ServiceWorkerRegistrar();
82 virtual ~ServiceWorkerRegistrar();
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;
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
;
114 bool mSaveDataRunnableDispatched
;
117 } // namespace mozilla::dom
119 #endif // mozilla_dom_ServiceWorkerRegistrar_h