summaryrefslogtreecommitdiffstats
path: root/toolkit/components/alerts/nsAlertsService.cpp
blob: 8076e504f0f5f4718056bbc047611e5298813682 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode:nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "mozilla/dom/ContentChild.h"
#include "mozilla/dom/PermissionMessageUtils.h"
#include "mozilla/Preferences.h"
#include "mozilla/StaticPrefs_alerts.h"
#include "mozilla/Telemetry.h"
#include "nsXULAppAPI.h"

#include "nsAlertsService.h"

#include "nsXPCOM.h"
#include "nsPromiseFlatString.h"
#include "nsToolkitCompsCID.h"

#ifdef MOZ_PLACES
#  include "nsIFaviconService.h"
#endif  // MOZ_PLACES

#ifdef XP_WIN
#  include <shellapi.h>
#endif

using namespace mozilla;

using mozilla::dom::ContentChild;

namespace {

#ifdef MOZ_PLACES

class IconCallback final : public nsIFaviconDataCallback {
 public:
  NS_DECL_ISUPPORTS

  IconCallback(nsIAlertsService* aBackend, nsIAlertNotification* aAlert,
               nsIObserver* aAlertListener)
      : mBackend(aBackend), mAlert(aAlert), mAlertListener(aAlertListener) {}

  NS_IMETHOD
  OnComplete(nsIURI* aIconURI, uint32_t aIconSize, const uint8_t* aIconData,
             const nsACString& aMimeType, uint16_t aWidth) override {
    nsresult rv = NS_ERROR_FAILURE;
    if (aIconSize > 0) {
      nsCOMPtr<nsIAlertsIconData> alertsIconData(do_QueryInterface(mBackend));
      if (alertsIconData) {
        rv = alertsIconData->ShowAlertWithIconData(mAlert, mAlertListener,
                                                   aIconSize, aIconData);
      }
    } else if (aIconURI) {
      nsCOMPtr<nsIAlertsIconURI> alertsIconURI(do_QueryInterface(mBackend));
      if (alertsIconURI) {
        rv = alertsIconURI->ShowAlertWithIconURI(mAlert, mAlertListener,
                                                 aIconURI);
      }
    }
    if (NS_FAILED(rv)) {
      rv = mBackend->ShowAlert(mAlert, mAlertListener);
    }
    return rv;
  }

 private:
  virtual ~IconCallback() = default;

  nsCOMPtr<nsIAlertsService> mBackend;
  nsCOMPtr<nsIAlertNotification> mAlert;
  nsCOMPtr<nsIObserver> mAlertListener;
};

NS_IMPL_ISUPPORTS(IconCallback, nsIFaviconDataCallback)

#endif  // MOZ_PLACES

nsresult ShowWithIconBackend(nsIAlertsService* aBackend,
                             nsIAlertNotification* aAlert,
                             nsIObserver* aAlertListener) {
#ifdef MOZ_PLACES
  nsCOMPtr<nsIURI> uri;
  nsresult rv = aAlert->GetURI(getter_AddRefs(uri));
  if (NS_FAILED(rv) || !uri) {
    return NS_ERROR_FAILURE;
  }

  // Ensure the backend supports favicons.
  nsCOMPtr<nsIAlertsIconData> alertsIconData(do_QueryInterface(aBackend));
  nsCOMPtr<nsIAlertsIconURI> alertsIconURI;
  if (!alertsIconData) {
    alertsIconURI = do_QueryInterface(aBackend);
  }
  if (!alertsIconData && !alertsIconURI) {
    return NS_ERROR_NOT_IMPLEMENTED;
  }

  nsCOMPtr<nsIFaviconService> favicons(
      do_GetService("@mozilla.org/browser/favicon-service;1"));
  NS_ENSURE_TRUE(favicons, NS_ERROR_FAILURE);

  nsCOMPtr<nsIFaviconDataCallback> callback =
      new IconCallback(aBackend, aAlert, aAlertListener);
  if (alertsIconData) {
    return favicons->GetFaviconDataForPage(uri, callback, 0);
  }
  return favicons->GetFaviconURLForPage(uri, callback, 0);
#else
  return NS_ERROR_NOT_IMPLEMENTED;
#endif  // !MOZ_PLACES
}

nsresult ShowWithBackend(nsIAlertsService* aBackend,
                         nsIAlertNotification* aAlert,
                         nsIObserver* aAlertListener,
                         const nsAString& aPersistentData) {
  if (!aPersistentData.IsEmpty()) {
    return aBackend->ShowPersistentNotification(aPersistentData, aAlert,
                                                aAlertListener);
  }

  if (Preferences::GetBool("alerts.showFavicons")) {
    nsresult rv = ShowWithIconBackend(aBackend, aAlert, aAlertListener);
    if (NS_SUCCEEDED(rv)) {
      return rv;
    }
  }

  // If favicons are disabled, or the backend doesn't support them, show the
  // alert without one.
  return aBackend->ShowAlert(aAlert, aAlertListener);
}

}  // anonymous namespace

NS_IMPL_ISUPPORTS(nsAlertsService, nsIAlertsService, nsIAlertsDoNotDisturb)

nsAlertsService::nsAlertsService() : mBackend(nullptr) {
  mBackend = do_GetService(NS_SYSTEMALERTSERVICE_CONTRACTID);
}

nsAlertsService::~nsAlertsService() = default;

bool nsAlertsService::ShouldShowAlert() {
  bool result = true;

#ifdef XP_WIN
  if (!xpc::IsInAutomation()) {
    QUERY_USER_NOTIFICATION_STATE qstate;
    if (SUCCEEDED(SHQueryUserNotificationState(&qstate))) {
      if (qstate != QUNS_ACCEPTS_NOTIFICATIONS) {
        result = false;
      }
    }
  }
#endif

  nsCOMPtr<nsIAlertsDoNotDisturb> alertsDND(GetDNDBackend());
  if (alertsDND) {
    bool suppressForScreenSharing = false;
    nsresult rv =
        alertsDND->GetSuppressForScreenSharing(&suppressForScreenSharing);
    if (NS_SUCCEEDED(rv)) {
      result &= !suppressForScreenSharing;
    }
  }

  return result;
}

bool nsAlertsService::ShouldUseSystemBackend() {
  if (!mBackend) {
    return false;
  }
  return StaticPrefs::alerts_useSystemBackend();
}

NS_IMETHODIMP nsAlertsService::ShowAlertNotification(
    const nsAString& aImageUrl, const nsAString& aAlertTitle,
    const nsAString& aAlertText, bool aAlertTextClickable,
    const nsAString& aAlertCookie, nsIObserver* aAlertListener,
    const nsAString& aAlertName, const nsAString& aBidi, const nsAString& aLang,
    const nsAString& aData, nsIPrincipal* aPrincipal, bool aInPrivateBrowsing,
    bool aRequireInteraction) {
  nsCOMPtr<nsIAlertNotification> alert =
      do_CreateInstance(ALERT_NOTIFICATION_CONTRACTID);
  NS_ENSURE_TRUE(alert, NS_ERROR_FAILURE);
  // vibrate is unused
  nsTArray<uint32_t> vibrate;
  nsresult rv = alert->Init(aAlertName, aImageUrl, aAlertTitle, aAlertText,
                            aAlertTextClickable, aAlertCookie, aBidi, aLang,
                            aData, aPrincipal, aInPrivateBrowsing,
                            aRequireInteraction, false, vibrate);
  NS_ENSURE_SUCCESS(rv, rv);
  return ShowAlert(alert, aAlertListener);
}

NS_IMETHODIMP nsAlertsService::ShowAlert(nsIAlertNotification* aAlert,
                                         nsIObserver* aAlertListener) {
  return ShowPersistentNotification(u""_ns, aAlert, aAlertListener);
}

NS_IMETHODIMP nsAlertsService::ShowPersistentNotification(
    const nsAString& aPersistentData, nsIAlertNotification* aAlert,
    nsIObserver* aAlertListener) {
  NS_ENSURE_ARG(aAlert);

  nsAutoString cookie;
  nsresult rv = aAlert->GetCookie(cookie);
  NS_ENSURE_SUCCESS(rv, rv);

  if (XRE_IsContentProcess()) {
    ContentChild* cpc = ContentChild::GetSingleton();

    if (aAlertListener) cpc->AddRemoteAlertObserver(cookie, aAlertListener);

    cpc->SendShowAlert(aAlert);
    return NS_OK;
  }

  // Check if there is an optional service that handles system-level
  // notifications
  if (ShouldUseSystemBackend()) {
    rv = ShowWithBackend(mBackend, aAlert, aAlertListener, aPersistentData);
    if (NS_SUCCEEDED(rv)) {
      return rv;
    }
    // If the system backend failed to show the alert, clear the backend and
    // retry with XUL notifications. Future alerts will always use XUL.
    mBackend = nullptr;
  }

  if (!ShouldShowAlert()) {
    // Do not display the alert. Instead call alertfinished and get out.
    if (aAlertListener)
      aAlertListener->Observe(nullptr, "alertfinished", cookie.get());
    return NS_OK;
  }

  // Use XUL notifications as a fallback if above methods have failed.
  nsCOMPtr<nsIAlertsService> xulBackend(nsXULAlerts::GetInstance());
  NS_ENSURE_TRUE(xulBackend, NS_ERROR_FAILURE);
  return ShowWithBackend(xulBackend, aAlert, aAlertListener, aPersistentData);
}

NS_IMETHODIMP nsAlertsService::CloseAlert(const nsAString& aAlertName,
                                          bool aContextClosed) {
  if (XRE_IsContentProcess()) {
    ContentChild* cpc = ContentChild::GetSingleton();
    cpc->SendCloseAlert(nsAutoString(aAlertName), aContextClosed);
    return NS_OK;
  }

  nsresult rv;
  // Try the system notification service.
  if (ShouldUseSystemBackend()) {
    rv = mBackend->CloseAlert(aAlertName, aContextClosed);
    if (NS_WARN_IF(NS_FAILED(rv))) {
      // If the system backend failed to close the alert, fall back to XUL for
      // future alerts.
      mBackend = nullptr;
    }
  } else {
    nsCOMPtr<nsIAlertsService> xulBackend(nsXULAlerts::GetInstance());
    NS_ENSURE_TRUE(xulBackend, NS_ERROR_FAILURE);
    rv = xulBackend->CloseAlert(aAlertName, aContextClosed);
  }
  return rv;
}

// nsIAlertsDoNotDisturb
NS_IMETHODIMP nsAlertsService::GetManualDoNotDisturb(bool* aRetVal) {
#ifdef MOZ_WIDGET_ANDROID
  return NS_ERROR_NOT_IMPLEMENTED;
#else
  nsCOMPtr<nsIAlertsDoNotDisturb> alertsDND(GetDNDBackend());
  NS_ENSURE_TRUE(alertsDND, NS_ERROR_NOT_IMPLEMENTED);
  return alertsDND->GetManualDoNotDisturb(aRetVal);
#endif
}

NS_IMETHODIMP nsAlertsService::SetManualDoNotDisturb(bool aDoNotDisturb) {
#ifdef MOZ_WIDGET_ANDROID
  return NS_ERROR_NOT_IMPLEMENTED;
#else
  nsCOMPtr<nsIAlertsDoNotDisturb> alertsDND(GetDNDBackend());
  NS_ENSURE_TRUE(alertsDND, NS_ERROR_NOT_IMPLEMENTED);

  nsresult rv = alertsDND->SetManualDoNotDisturb(aDoNotDisturb);
  if (NS_SUCCEEDED(rv)) {
    Telemetry::Accumulate(Telemetry::ALERTS_SERVICE_DND_ENABLED, 1);
  }
  return rv;
#endif
}

NS_IMETHODIMP nsAlertsService::GetSuppressForScreenSharing(bool* aRetVal) {
#ifdef MOZ_WIDGET_ANDROID
  return NS_ERROR_NOT_IMPLEMENTED;
#else
  nsCOMPtr<nsIAlertsDoNotDisturb> alertsDND(GetDNDBackend());
  NS_ENSURE_TRUE(alertsDND, NS_ERROR_NOT_IMPLEMENTED);
  return alertsDND->GetSuppressForScreenSharing(aRetVal);
#endif
}

NS_IMETHODIMP nsAlertsService::SetSuppressForScreenSharing(bool aSuppress) {
#ifdef MOZ_WIDGET_ANDROID
  return NS_ERROR_NOT_IMPLEMENTED;
#else
  nsCOMPtr<nsIAlertsDoNotDisturb> alertsDND(GetDNDBackend());
  NS_ENSURE_TRUE(alertsDND, NS_ERROR_NOT_IMPLEMENTED);
  return alertsDND->SetSuppressForScreenSharing(aSuppress);
#endif
}

already_AddRefed<nsIAlertsDoNotDisturb> nsAlertsService::GetDNDBackend() {
  nsCOMPtr<nsIAlertsService> backend;
  // Try the system notification service.
  if (ShouldUseSystemBackend()) {
    backend = mBackend;
  }
  if (!backend) {
    backend = nsXULAlerts::GetInstance();
  }

  nsCOMPtr<nsIAlertsDoNotDisturb> alertsDND(do_QueryInterface(backend));
  return alertsDND.forget();
}