Bug 1892041 - Part 1: Update test262 features. r=spidermonkey-reviewers,dminor
[gecko.git] / dom / storage / StorageNotifierService.h
blob9568f4b259bbe6537578c300f9fc291341b9a709
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_StorageNotifierService_h
8 #define mozilla_dom_StorageNotifierService_h
10 #include "nsISupportsImpl.h"
11 #include "nsTObserverArray.h"
13 class nsIEventTarget;
14 class nsIPrincipal;
15 class nsPIDOMWindowInner;
17 namespace mozilla::dom {
19 class StorageEvent;
21 /**
22 * Enables the StorageNotifierService to check whether an observer is interested
23 * in receiving events for the given principal before calling the method, an
24 * optimization refactoring.
26 * Expected to only be implemented by nsGlobalWindowObserver or its succesor.
28 class StorageNotificationObserver {
29 public:
30 NS_INLINE_DECL_PURE_VIRTUAL_REFCOUNTING
32 virtual void ObserveStorageNotification(StorageEvent* aEvent,
33 const char16_t* aStorageType,
34 bool aPrivateBrowsing) = 0;
36 virtual bool IsPrivateBrowsing() const = 0;
38 virtual nsIPrincipal* GetEffectiveCookiePrincipal() const = 0;
40 virtual nsIPrincipal* GetEffectiveStoragePrincipal() const = 0;
42 virtual nsIEventTarget* GetEventTarget() const = 0;
45 /**
46 * A specialized version of the observer service that uses the custom
47 * StorageNotificationObserver so that principal checks can happen in this class
48 * rather than in the nsIObserver::observe method where they used to happen.
50 * The only expected consumers are nsGlobalWindowInner instances via their
51 * nsGlobalWindowObserver helper that avoids being able to use the window as an
52 * nsIObserver.
54 class StorageNotifierService final {
55 public:
56 NS_INLINE_DECL_REFCOUNTING(StorageNotifierService)
58 static StorageNotifierService* GetOrCreate();
60 static void Broadcast(StorageEvent* aEvent, const char16_t* aStorageType,
61 bool aPrivateBrowsing, bool aImmediateDispatch);
63 void Register(StorageNotificationObserver* aObserver);
65 void Unregister(StorageNotificationObserver* aObserver);
67 private:
68 StorageNotifierService();
69 ~StorageNotifierService();
71 nsTObserverArray<RefPtr<StorageNotificationObserver>> mObservers;
74 } // namespace mozilla::dom
76 #endif // mozilla_dom_StorageNotifierService_h