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 u"serviceworker.txt"
20 #define SERVICEWORKERREGISTRAR_VERSION "9"
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 }  // namespace mozilla
38 
39 namespace mozilla::dom {
40 
41 class ServiceWorkerRegistrar : public nsIObserver,
42                                public nsIAsyncShutdownBlocker {
43   friend class ServiceWorkerRegistrarSaveDataRunnable;
44 
45  public:
46   NS_DECL_THREADSAFE_ISUPPORTS
47   NS_DECL_NSIOBSERVER
48   NS_DECL_NSIASYNCSHUTDOWNBLOCKER
49 
50   static void Initialize();
51 
52   void Shutdown();
53 
54   void DataSaved(uint32_t aFileGeneration);
55 
56   static already_AddRefed<ServiceWorkerRegistrar> Get();
57 
58   void GetRegistrations(nsTArray<ServiceWorkerRegistrationData>& aValues);
59 
60   void RegisterServiceWorker(const ServiceWorkerRegistrationData& aData);
61   void UnregisterServiceWorker(
62       const mozilla::ipc::PrincipalInfo& aPrincipalInfo,
63       const nsACString& aScope);
64   void RemoveAll();
65 
66  protected:
67   // These methods are protected because we test this class using gTest
68   // subclassing it.
69   void LoadData();
70   nsresult SaveData(const nsTArray<ServiceWorkerRegistrationData>& aData);
71 
72   nsresult ReadData();
73   nsresult WriteData(const nsTArray<ServiceWorkerRegistrationData>& aData);
74   void DeleteData();
75 
76   void RegisterServiceWorkerInternal(
77       const ServiceWorkerRegistrationData& aData);
78 
79   ServiceWorkerRegistrar();
80   virtual ~ServiceWorkerRegistrar();
81 
82  private:
83   void ProfileStarted();
84   void ProfileStopped();
85 
86   void MaybeScheduleSaveData();
87   void ShutdownCompleted();
88   void MaybeScheduleShutdownCompleted();
89 
90   uint32_t GetNextGeneration();
91   void MaybeResetGeneration();
92 
93   nsCOMPtr<nsIAsyncShutdownClient> GetShutdownPhase() const;
94 
95   bool IsSupportedVersion(const nsACString& aVersion) const;
96 
97   mozilla::Monitor mMonitor;
98 
99  protected:
100   // protected by mMonitor.
101   nsCOMPtr<nsIFile> mProfileDir;
102   nsTArray<ServiceWorkerRegistrationData> mData;
103   bool mDataLoaded;
104 
105   // PBackground thread only
106   uint32_t mDataGeneration;
107   uint32_t mFileGeneration;
108   uint32_t mRetryCount;
109   bool mShuttingDown;
110   bool mRunnableDispatched;
111 };
112 
113 }  // namespace mozilla::dom
114 
115 #endif  // mozilla_dom_ServiceWorkerRegistrar_h
116