StorageObserver.h (2621B)
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_StorageObserver_h 8 #define mozilla_dom_StorageObserver_h 9 10 #include "mozilla/dom/quota/CheckedUnsafePtr.h" 11 #include "nsINamed.h" 12 #include "nsIObserver.h" 13 #include "nsITimer.h" 14 #include "nsString.h" 15 #include "nsTObserverArray.h" 16 #include "nsWeakReference.h" 17 18 namespace mozilla::dom { 19 20 class StorageObserver; 21 22 // Main-thread interface implemented by legacy LocalStorageManager and current 23 // SessionStorageManager for direct consumption. Also implemented by legacy 24 // StorageDBParent and current SessionStorageObserverParent for propagation to 25 // content processes. 26 class StorageObserverSink 27 : public SupportsCheckedUnsafePtr<CheckIf<DiagnosticAssertEnabled>> { 28 public: 29 virtual ~StorageObserverSink() = default; 30 31 private: 32 friend class StorageObserver; 33 virtual nsresult Observe(const char* aTopic, 34 const nsAString& aOriginAttributesPattern, 35 const nsACString& aOriginScope) = 0; 36 }; 37 38 // Statically (through layout statics) initialized observer receiving and 39 // processing chrome clearing notifications, such as cookie deletion etc. 40 class StorageObserver : public nsIObserver, 41 public nsINamed, 42 public nsSupportsWeakReference { 43 public: 44 NS_DECL_ISUPPORTS 45 NS_DECL_NSIOBSERVER 46 NS_DECL_NSINAMED 47 48 static nsresult Init(); 49 static nsresult Shutdown(); 50 static StorageObserver* Self() { return sSelf; } 51 52 void AddSink(StorageObserverSink* aObs); 53 void RemoveSink(StorageObserverSink* aObs); 54 void Notify(const char* aTopic, 55 const nsAString& aOriginAttributesPattern = u""_ns, 56 const nsACString& aOriginScope = ""_ns); 57 58 void NoteBackgroundThread(uint32_t aPrivateBrowsingId, 59 nsIEventTarget* aBackgroundThread); 60 61 private: 62 virtual ~StorageObserver() = default; 63 64 nsresult GetOriginScope(const char16_t* aData, nsACString& aOriginScope); 65 66 static void TestingPrefChanged(const char* aPrefName, void* aClosure); 67 68 static StorageObserver* sSelf; 69 70 nsCOMPtr<nsIEventTarget> mBackgroundThread[2]; 71 72 // Weak references 73 nsTObserverArray<CheckedUnsafePtr<StorageObserverSink>> mSinks; 74 nsCOMPtr<nsITimer> mDBThreadStartDelayTimer; 75 }; 76 77 } // namespace mozilla::dom 78 79 #endif // mozilla_dom_StorageObserver_h