LSObserver.h (2133B)
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_localstorage_LSObserver_h 8 #define mozilla_dom_localstorage_LSObserver_h 9 10 #include "mozilla/Assertions.h" 11 #include "nsISupports.h" 12 #include "nsString.h" 13 14 namespace mozilla::dom { 15 16 class LSObserverChild; 17 18 /** 19 * Effectively just a refcounted life-cycle management wrapper around 20 * LSObserverChild which exists to receive "storage" event information from 21 * other processes. (Same-process events are handled within the process, see 22 * `LSObject::OnChange`.) 23 * 24 * ## Lifecycle ## 25 * - Created by LSObject::EnsureObserver via synchronous LSRequest idiom 26 * whenever the first window's origin adds a "storage" event. Placed in the 27 * gLSObservers LSObserverHashtable for subsequent LSObject's via 28 * LSObserver::Get lookup. 29 * - The LSObserverChild directly handles "Observe" messages, shunting them 30 * directly to Storage::NotifyChange which does all the legwork of notifying 31 * windows about "storage" events. 32 * - Destroyed when refcount goes to zero due to all owning LSObjects being 33 * destroyed or having their `LSObject::DropObserver` methods invoked due to 34 * the last "storage" event listener being removed from the owning window. 35 */ 36 class LSObserver final { 37 friend class LSObject; 38 39 LSObserverChild* mActor; 40 41 const nsCString mOrigin; 42 43 public: 44 static LSObserver* Get(const nsACString& aOrigin); 45 46 NS_INLINE_DECL_REFCOUNTING(LSObserver) 47 48 void AssertIsOnOwningThread() const { NS_ASSERT_OWNINGTHREAD(LSObserver); } 49 50 void SetActor(LSObserverChild* aActor); 51 52 void ClearActor() { 53 AssertIsOnOwningThread(); 54 MOZ_ASSERT(mActor); 55 56 mActor = nullptr; 57 } 58 59 private: 60 // Only created by LSObject. 61 explicit LSObserver(const nsACString& aOrigin); 62 63 ~LSObserver(); 64 }; 65 66 } // namespace mozilla::dom 67 68 #endif // mozilla_dom_localstorage_LSObserver_h