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