1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode:nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3  * License, v. 2.0. If a copy of the MPL was not distributed with this
4  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 
6 #ifndef nsXULAlerts_h__
7 #define nsXULAlerts_h__
8 
9 #include "nsCycleCollectionParticipant.h"
10 #include "nsHashKeys.h"
11 #include "nsInterfaceHashtable.h"
12 
13 #include "mozIDOMWindow.h"
14 #include "nsIObserver.h"
15 
16 struct PendingAlert {
InitPendingAlert17   void Init(nsIAlertNotification* aAlert, nsIObserver* aListener) {
18     mAlert = aAlert;
19     mListener = aListener;
20   }
21   nsCOMPtr<nsIAlertNotification> mAlert;
22   nsCOMPtr<nsIObserver> mListener;
23 };
24 
25 class nsXULAlerts : public nsIAlertsService,
26                     public nsIAlertsDoNotDisturb,
27                     public nsIAlertsIconURI {
28   friend class nsXULAlertObserver;
29 
30  public:
31   NS_DECL_NSIALERTSICONURI
32   NS_DECL_NSIALERTSDONOTDISTURB
33   NS_DECL_NSIALERTSSERVICE
34   NS_DECL_ISUPPORTS
35 
36   nsXULAlerts() = default;
37 
38   static already_AddRefed<nsXULAlerts> GetInstance();
39 
40  protected:
41   virtual ~nsXULAlerts() = default;
42   void PersistentAlertFinished();
43 
44   nsInterfaceHashtable<nsStringHashKey, mozIDOMWindowProxy> mNamedWindows;
45   uint32_t mPersistentAlertCount = 0;
46   nsTArray<PendingAlert> mPendingPersistentAlerts;
47   bool mDoNotDisturb = false;
48 
49  private:
50   bool mSuppressForScreenSharing = false;
51 };
52 
53 /**
54  * This class wraps observers for alerts and watches
55  * for the "alertfinished" event in order to release
56  * the reference on the nsIDOMWindow of the XUL alert.
57  */
58 class nsXULAlertObserver : public nsIObserver {
59  public:
60   NS_DECL_CYCLE_COLLECTING_ISUPPORTS
61   NS_DECL_NSIOBSERVER
NS_DECL_CYCLE_COLLECTION_CLASS(nsXULAlertObserver)62   NS_DECL_CYCLE_COLLECTION_CLASS(nsXULAlertObserver)
63 
64   nsXULAlertObserver(nsXULAlerts* aXULAlerts, const nsAString& aAlertName,
65                      nsIObserver* aObserver, bool aIsPersistent)
66       : mXULAlerts(aXULAlerts),
67         mAlertName(aAlertName),
68         mObserver(aObserver),
69         mIsPersistent(aIsPersistent) {}
70 
SetAlertWindow(mozIDOMWindowProxy * aWindow)71   void SetAlertWindow(mozIDOMWindowProxy* aWindow) { mAlertWindow = aWindow; }
72 
73  protected:
74   virtual ~nsXULAlertObserver() = default;
75 
76   RefPtr<nsXULAlerts> mXULAlerts;
77   nsString mAlertName;
78   nsCOMPtr<mozIDOMWindowProxy> mAlertWindow;
79   nsCOMPtr<nsIObserver> mObserver;
80   bool mIsPersistent;
81 };
82 
83 #endif /* nsXULAlerts_h__ */
84