Bug 1842773 - Part 13: Remove TypedArrayObject::{length,byteLength}Value. r=sfink
[gecko.git] / widget / android / AndroidAlerts.cpp
blob456dc08290be963ae72dc15240da4e47b8221e78
1 /* -*- Mode: c++; tab-width: 4; 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/. */
6 #include "AndroidAlerts.h"
7 #include "mozilla/java/GeckoRuntimeWrappers.h"
8 #include "mozilla/java/WebNotificationWrappers.h"
9 #include "nsIPrincipal.h"
10 #include "nsIURI.h"
12 namespace mozilla {
13 namespace widget {
15 NS_IMPL_ISUPPORTS(AndroidAlerts, nsIAlertsService)
17 StaticAutoPtr<AndroidAlerts::ListenerMap> AndroidAlerts::sListenerMap;
18 nsTHashMap<nsStringHashKey, java::WebNotification::GlobalRef>
19 AndroidAlerts::mNotificationsMap;
21 NS_IMETHODIMP
22 AndroidAlerts::ShowAlertNotification(
23 const nsAString& aImageUrl, const nsAString& aAlertTitle,
24 const nsAString& aAlertText, bool aAlertTextClickable,
25 const nsAString& aAlertCookie, nsIObserver* aAlertListener,
26 const nsAString& aAlertName, const nsAString& aBidi, const nsAString& aLang,
27 const nsAString& aData, nsIPrincipal* aPrincipal, bool aInPrivateBrowsing,
28 bool aRequireInteraction) {
29 MOZ_ASSERT_UNREACHABLE("Should be implemented by nsAlertsService.");
30 return NS_ERROR_NOT_IMPLEMENTED;
33 NS_IMETHODIMP
34 AndroidAlerts::ShowAlert(nsIAlertNotification* aAlert,
35 nsIObserver* aAlertListener) {
36 return ShowPersistentNotification(u""_ns, aAlert, aAlertListener);
39 NS_IMETHODIMP
40 AndroidAlerts::ShowPersistentNotification(const nsAString& aPersistentData,
41 nsIAlertNotification* aAlert,
42 nsIObserver* aAlertListener) {
43 // nsAlertsService disables our alerts backend if we ever return failure
44 // here. To keep the backend enabled, we always return NS_OK even if we
45 // encounter an error here.
46 nsresult rv;
48 nsAutoString imageUrl;
49 rv = aAlert->GetImageURL(imageUrl);
50 NS_ENSURE_SUCCESS(rv, NS_OK);
52 nsAutoString title;
53 rv = aAlert->GetTitle(title);
54 NS_ENSURE_SUCCESS(rv, NS_OK);
56 nsAutoString text;
57 rv = aAlert->GetText(text);
58 NS_ENSURE_SUCCESS(rv, NS_OK);
60 nsAutoString cookie;
61 rv = aAlert->GetCookie(cookie);
62 NS_ENSURE_SUCCESS(rv, NS_OK);
64 nsAutoString name;
65 rv = aAlert->GetName(name);
66 NS_ENSURE_SUCCESS(rv, NS_OK);
68 nsAutoString lang;
69 rv = aAlert->GetLang(lang);
70 NS_ENSURE_SUCCESS(rv, NS_OK);
72 nsAutoString dir;
73 rv = aAlert->GetDir(dir);
74 NS_ENSURE_SUCCESS(rv, NS_OK);
76 bool requireInteraction;
77 rv = aAlert->GetRequireInteraction(&requireInteraction);
78 NS_ENSURE_SUCCESS(rv, NS_OK);
80 nsCOMPtr<nsIURI> uri;
81 rv = aAlert->GetURI(getter_AddRefs(uri));
82 NS_ENSURE_SUCCESS(rv, NS_OK);
84 nsCString spec;
85 if (uri) {
86 rv = uri->GetDisplaySpec(spec);
87 NS_ENSURE_SUCCESS(rv, NS_OK);
90 bool silent;
91 rv = aAlert->GetSilent(&silent);
92 NS_ENSURE_SUCCESS(rv, NS_OK);
94 bool privateBrowsing;
95 rv = aAlert->GetInPrivateBrowsing(&privateBrowsing);
96 NS_ENSURE_SUCCESS(rv, NS_OK);
98 nsTArray<uint32_t> vibrate;
99 rv = aAlert->GetVibrate(vibrate);
100 NS_ENSURE_SUCCESS(rv, NS_OK);
102 if (aPersistentData.IsEmpty() && aAlertListener) {
103 if (!sListenerMap) {
104 sListenerMap = new ListenerMap();
106 // This will remove any observers already registered for this name.
107 sListenerMap->InsertOrUpdate(name, aAlertListener);
110 java::WebNotification::LocalRef notification = notification->New(
111 title, name, cookie, text, imageUrl, dir, lang, requireInteraction, spec,
112 silent, privateBrowsing, jni::IntArray::From(vibrate));
113 java::GeckoRuntime::LocalRef runtime = java::GeckoRuntime::GetInstance();
114 if (runtime != NULL) {
115 runtime->NotifyOnShow(notification);
117 mNotificationsMap.InsertOrUpdate(name, notification);
119 return NS_OK;
122 NS_IMETHODIMP
123 AndroidAlerts::CloseAlert(const nsAString& aAlertName, bool aContextClosed) {
124 java::WebNotification::LocalRef notification =
125 mNotificationsMap.Get(aAlertName);
126 if (!notification) {
127 return NS_OK;
130 java::GeckoRuntime::LocalRef runtime = java::GeckoRuntime::GetInstance();
131 if (runtime != NULL) {
132 runtime->NotifyOnClose(notification);
134 mNotificationsMap.Remove(aAlertName);
136 return NS_OK;
139 void AndroidAlerts::NotifyListener(const nsAString& aName, const char* aTopic,
140 const char16_t* aCookie) {
141 if (!sListenerMap) {
142 return;
145 nsCOMPtr<nsIObserver> listener = sListenerMap->Get(aName);
146 if (!listener) {
147 return;
150 listener->Observe(nullptr, aTopic, aCookie);
152 if ("alertfinished"_ns.Equals(aTopic)) {
153 sListenerMap->Remove(aName);
154 mNotificationsMap.Remove(aName);
158 } // namespace widget
159 } // namespace mozilla