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 "nsDataHashtable.h"
11 #include "nsHashKeys.h"
12 #include "nsInterfaceHashtable.h"
13 
14 #include "mozIDOMWindow.h"
15 #include "nsIObserver.h"
16 
17 struct PendingAlert {
InitPendingAlert18   void Init(nsIAlertNotification* aAlert, nsIObserver* aListener) {
19     mAlert = aAlert;
20     mListener = aListener;
21   }
22   nsCOMPtr<nsIAlertNotification> mAlert;
23   nsCOMPtr<nsIObserver> mListener;
24 };
25 
26 class nsXULAlerts : public nsIAlertsService,
27                     public nsIAlertsDoNotDisturb,
28                     public nsIAlertsIconURI {
29   friend class nsXULAlertObserver;
30 
31  public:
32   NS_DECL_NSIALERTSICONURI
33   NS_DECL_NSIALERTSDONOTDISTURB
34   NS_DECL_NSIALERTSSERVICE
35   NS_DECL_ISUPPORTS
36 
nsXULAlerts()37   nsXULAlerts() {}
38 
39   static already_AddRefed<nsXULAlerts> GetInstance();
40 
41  protected:
~nsXULAlerts()42   virtual ~nsXULAlerts() {}
43   void PersistentAlertFinished();
44 
45   nsInterfaceHashtable<nsStringHashKey, mozIDOMWindowProxy> mNamedWindows;
46   uint32_t mPersistentAlertCount = 0;
47   nsTArray<PendingAlert> mPendingPersistentAlerts;
48   bool mDoNotDisturb = false;
49 };
50 
51 /**
52  * This class wraps observers for alerts and watches
53  * for the "alertfinished" event in order to release
54  * the reference on the nsIDOMWindow of the XUL alert.
55  */
56 class nsXULAlertObserver : public nsIObserver {
57  public:
58   NS_DECL_CYCLE_COLLECTING_ISUPPORTS
59   NS_DECL_NSIOBSERVER
NS_DECL_CYCLE_COLLECTION_CLASS(nsXULAlertObserver)60   NS_DECL_CYCLE_COLLECTION_CLASS(nsXULAlertObserver)
61 
62   nsXULAlertObserver(nsXULAlerts* aXULAlerts, const nsAString& aAlertName,
63                      nsIObserver* aObserver, bool aIsPersistent)
64       : mXULAlerts(aXULAlerts),
65         mAlertName(aAlertName),
66         mObserver(aObserver),
67         mIsPersistent(aIsPersistent) {}
68 
SetAlertWindow(mozIDOMWindowProxy * aWindow)69   void SetAlertWindow(mozIDOMWindowProxy* aWindow) { mAlertWindow = aWindow; }
70 
71  protected:
~nsXULAlertObserver()72   virtual ~nsXULAlertObserver() {}
73 
74   RefPtr<nsXULAlerts> mXULAlerts;
75   nsString mAlertName;
76   nsCOMPtr<mozIDOMWindowProxy> mAlertWindow;
77   nsCOMPtr<nsIObserver> mObserver;
78   bool mIsPersistent;
79 };
80 
81 #endif /* nsXULAlerts_h__ */
82