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();
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   void SaveData();
67 
68   nsresult ReadData();
69   nsresult WriteData();
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 ScheduleSaveData();
83   void ShutdownCompleted();
84   void MaybeScheduleShutdownCompleted();
85 
86   nsCOMPtr<nsIAsyncShutdownClient> GetShutdownPhase() const;
87 
88   bool IsSupportedVersion(const nsACString& aVersion) const;
89 
90   mozilla::Monitor mMonitor;
91 
92  protected:
93   // protected by mMonitor.
94   nsCOMPtr<nsIFile> mProfileDir;
95   nsTArray<ServiceWorkerRegistrationData> mData;
96   bool mDataLoaded;
97 
98   // PBackground thread only
99   bool mShuttingDown;
100   uint32_t mRunnableCounter;
101 };
102 
103 }  // namespace dom
104 }  // namespace mozilla
105 
106 #endif  // mozilla_dom_ServiceWorkerRegistrar_h
107