Line data Source code
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 : #include "nsComponentManagerUtils.h"
7 : #include "nsXULAppAPI.h"
8 : #include "nsSystemAlertsService.h"
9 : #include "nsAlertsIconListener.h"
10 : #include "nsAutoPtr.h"
11 :
12 0 : NS_IMPL_ADDREF(nsSystemAlertsService)
13 0 : NS_IMPL_RELEASE(nsSystemAlertsService)
14 :
15 0 : NS_INTERFACE_MAP_BEGIN(nsSystemAlertsService)
16 0 : NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIAlertsService)
17 0 : NS_INTERFACE_MAP_ENTRY(nsIAlertsService)
18 0 : NS_INTERFACE_MAP_END_THREADSAFE
19 :
20 : nsSystemAlertsService::nsSystemAlertsService()
21 : = default;
22 :
23 : nsSystemAlertsService::~nsSystemAlertsService()
24 : = default;
25 :
26 : nsresult
27 0 : nsSystemAlertsService::Init()
28 : {
29 0 : return NS_OK;
30 : }
31 :
32 0 : NS_IMETHODIMP nsSystemAlertsService::ShowAlertNotification(const nsAString & aImageUrl, const nsAString & aAlertTitle,
33 : const nsAString & aAlertText, bool aAlertTextClickable,
34 : const nsAString & aAlertCookie,
35 : nsIObserver * aAlertListener,
36 : const nsAString & aAlertName,
37 : const nsAString & aBidi,
38 : const nsAString & aLang,
39 : const nsAString & aData,
40 : nsIPrincipal * aPrincipal,
41 : bool aInPrivateBrowsing,
42 : bool aRequireInteraction)
43 : {
44 : nsCOMPtr<nsIAlertNotification> alert =
45 0 : do_CreateInstance(ALERT_NOTIFICATION_CONTRACTID);
46 0 : NS_ENSURE_TRUE(alert, NS_ERROR_FAILURE);
47 0 : nsresult rv = alert->Init(aAlertName, aImageUrl, aAlertTitle,
48 : aAlertText, aAlertTextClickable,
49 : aAlertCookie, aBidi, aLang, aData,
50 : aPrincipal, aInPrivateBrowsing,
51 0 : aRequireInteraction);
52 0 : NS_ENSURE_SUCCESS(rv, rv);
53 0 : return ShowAlert(alert, aAlertListener);
54 : }
55 :
56 0 : NS_IMETHODIMP nsSystemAlertsService::ShowPersistentNotification(const nsAString& aPersistentData,
57 : nsIAlertNotification* aAlert,
58 : nsIObserver* aAlertListener)
59 : {
60 0 : return ShowAlert(aAlert, aAlertListener);
61 : }
62 :
63 0 : NS_IMETHODIMP nsSystemAlertsService::ShowAlert(nsIAlertNotification* aAlert,
64 : nsIObserver* aAlertListener)
65 : {
66 0 : NS_ENSURE_ARG(aAlert);
67 :
68 0 : nsAutoString alertName;
69 0 : nsresult rv = aAlert->GetName(alertName);
70 0 : NS_ENSURE_SUCCESS(rv, rv);
71 :
72 : RefPtr<nsAlertsIconListener> alertListener =
73 0 : new nsAlertsIconListener(this, alertName);
74 0 : if (!alertListener)
75 0 : return NS_ERROR_OUT_OF_MEMORY;
76 :
77 0 : AddListener(alertName, alertListener);
78 0 : return alertListener->InitAlertAsync(aAlert, aAlertListener);
79 : }
80 :
81 0 : NS_IMETHODIMP nsSystemAlertsService::CloseAlert(const nsAString& aAlertName,
82 : nsIPrincipal* aPrincipal)
83 : {
84 0 : RefPtr<nsAlertsIconListener> listener = mActiveListeners.Get(aAlertName);
85 0 : if (!listener) {
86 0 : return NS_OK;
87 : }
88 0 : mActiveListeners.Remove(aAlertName);
89 0 : return listener->Close();
90 : }
91 :
92 0 : bool nsSystemAlertsService::IsActiveListener(const nsAString& aAlertName,
93 : nsAlertsIconListener* aListener)
94 : {
95 0 : return mActiveListeners.Get(aAlertName) == aListener;
96 : }
97 :
98 0 : void nsSystemAlertsService::AddListener(const nsAString& aAlertName,
99 : nsAlertsIconListener* aListener)
100 : {
101 0 : RefPtr<nsAlertsIconListener> oldListener = mActiveListeners.Get(aAlertName);
102 0 : mActiveListeners.Put(aAlertName, aListener);
103 0 : if (oldListener) {
104 : // If an alert with this name already exists, close it.
105 0 : oldListener->Close();
106 : }
107 0 : }
108 :
109 0 : void nsSystemAlertsService::RemoveListener(const nsAString& aAlertName,
110 : nsAlertsIconListener* aListener)
111 : {
112 0 : if (IsActiveListener(aAlertName, aListener)) {
113 : // The alert may have been replaced; only remove it from the active
114 : // listeners map if it's the same.
115 0 : mActiveListeners.Remove(aAlertName);
116 : }
117 0 : }
|