Bug 1686668 [wpt PR 27185] - Update wpt metadata, a=testonly
[gecko.git] / dom / storage / StorageNotifierService.h
blobe36bb08b0d57ad4522d1db07a80dd889ce78c15d
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 {
18 namespace dom {
20 class StorageEvent;
22 /**
23 * Enables the StorageNotifierService to check whether an observer is interested
24 * in receiving events for the given principal before calling the method, an
25 * optimization refactoring.
27 * Expected to only be implemented by nsGlobalWindowObserver or its succesor.
29 class StorageNotificationObserver {
30 public:
31 NS_INLINE_DECL_PURE_VIRTUAL_REFCOUNTING
33 virtual void ObserveStorageNotification(StorageEvent* aEvent,
34 const char16_t* aStorageType,
35 bool aPrivateBrowsing) = 0;
37 virtual bool IsPrivateBrowsing() const = 0;
39 virtual nsIPrincipal* GetEffectiveStoragePrincipal() const = 0;
41 virtual nsIEventTarget* GetEventTarget() const = 0;
44 /**
45 * A specialized version of the observer service that uses the custom
46 * StorageNotificationObserver so that principal checks can happen in this class
47 * rather than in the nsIObserver::observe method where they used to happen.
49 * The only expected consumers are nsGlobalWindowInner instances via their
50 * nsGlobalWindowObserver helper that avoids being able to use the window as an
51 * nsIObserver.
53 class StorageNotifierService final {
54 public:
55 NS_INLINE_DECL_REFCOUNTING(StorageNotifierService)
57 static StorageNotifierService* GetOrCreate();
59 static void Broadcast(StorageEvent* aEvent, const char16_t* aStorageType,
60 bool aPrivateBrowsing, bool aImmediateDispatch);
62 void Register(StorageNotificationObserver* aObserver);
64 void Unregister(StorageNotificationObserver* aObserver);
66 private:
67 StorageNotifierService();
68 ~StorageNotifierService();
70 nsTObserverArray<RefPtr<StorageNotificationObserver>> mObservers;
73 } // namespace dom
74 } // namespace mozilla
76 #endif // mozilla_dom_StorageNotifierService_h