Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/toolkit/system/gnome/nsSystemAlertsService.cpp
Line
Count
Source (jump to first uncovered line)
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
NS_IMPL_ADDREF(nsSystemAlertsService)
13
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
19
20
0
nsSystemAlertsService::nsSystemAlertsService()
21
= default;
22
23
0
nsSystemAlertsService::~nsSystemAlertsService()
24
= default;
25
26
nsresult
27
nsSystemAlertsService::Init()
28
0
{
29
0
  return NS_OK;
30
0
}
31
32
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
0
{
44
0
  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
0
                            aAlertText, aAlertTextClickable,
49
0
                            aAlertCookie, aBidi, aLang, aData,
50
0
                            aPrincipal, aInPrivateBrowsing,
51
0
                            aRequireInteraction);
52
0
  NS_ENSURE_SUCCESS(rv, rv);
53
0
  return ShowAlert(alert, aAlertListener);
54
0
}
55
56
NS_IMETHODIMP nsSystemAlertsService::ShowPersistentNotification(const nsAString& aPersistentData,
57
                                                                nsIAlertNotification* aAlert,
58
                                                                nsIObserver* aAlertListener)
59
0
{
60
0
  return ShowAlert(aAlert, aAlertListener);
61
0
}
62
63
NS_IMETHODIMP nsSystemAlertsService::ShowAlert(nsIAlertNotification* aAlert,
64
                                               nsIObserver* aAlertListener)
65
0
{
66
0
  NS_ENSURE_ARG(aAlert);
67
0
68
0
  nsAutoString alertName;
69
0
  nsresult rv = aAlert->GetName(alertName);
70
0
  NS_ENSURE_SUCCESS(rv, rv);
71
0
72
0
  RefPtr<nsAlertsIconListener> alertListener =
73
0
    new nsAlertsIconListener(this, alertName);
74
0
  if (!alertListener)
75
0
    return NS_ERROR_OUT_OF_MEMORY;
76
0
77
0
  AddListener(alertName, alertListener);
78
0
  return alertListener->InitAlertAsync(aAlert, aAlertListener);
79
0
}
80
81
NS_IMETHODIMP nsSystemAlertsService::CloseAlert(const nsAString& aAlertName,
82
                                                nsIPrincipal* aPrincipal)
83
0
{
84
0
  RefPtr<nsAlertsIconListener> listener = mActiveListeners.Get(aAlertName);
85
0
  if (!listener) {
86
0
    return NS_OK;
87
0
  }
88
0
  mActiveListeners.Remove(aAlertName);
89
0
  return listener->Close();
90
0
}
91
92
bool nsSystemAlertsService::IsActiveListener(const nsAString& aAlertName,
93
                                             nsAlertsIconListener* aListener)
94
0
{
95
0
  return mActiveListeners.Get(aAlertName) == aListener;
96
0
}
97
98
void nsSystemAlertsService::AddListener(const nsAString& aAlertName,
99
                                        nsAlertsIconListener* aListener)
100
0
{
101
0
  RefPtr<nsAlertsIconListener> oldListener = mActiveListeners.Get(aAlertName);
102
0
  mActiveListeners.Put(aAlertName, aListener);
103
0
  if (oldListener) {
104
0
    // If an alert with this name already exists, close it.
105
0
    oldListener->Close();
106
0
  }
107
0
}
108
109
void nsSystemAlertsService::RemoveListener(const nsAString& aAlertName,
110
                                           nsAlertsIconListener* aListener)
111
0
{
112
0
  if (IsActiveListener(aAlertName, aListener)) {
113
0
    // The alert may have been replaced; only remove it from the active
114
0
    // listeners map if it's the same.
115
0
    mActiveListeners.Remove(aAlertName);
116
0
  }
117
0
}