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"
15 NS_IMPL_ISUPPORTS(AndroidAlerts
, nsIAlertsService
)
17 StaticAutoPtr
<AndroidAlerts::ListenerMap
> AndroidAlerts::sListenerMap
;
18 nsTHashMap
<nsStringHashKey
, java::WebNotification::GlobalRef
>
19 AndroidAlerts::mNotificationsMap
;
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
;
34 AndroidAlerts::ShowAlert(nsIAlertNotification
* aAlert
,
35 nsIObserver
* aAlertListener
) {
36 return ShowPersistentNotification(u
""_ns
, aAlert
, aAlertListener
);
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.
48 nsAutoString imageUrl
;
49 rv
= aAlert
->GetImageURL(imageUrl
);
50 NS_ENSURE_SUCCESS(rv
, NS_OK
);
53 rv
= aAlert
->GetTitle(title
);
54 NS_ENSURE_SUCCESS(rv
, NS_OK
);
57 rv
= aAlert
->GetText(text
);
58 NS_ENSURE_SUCCESS(rv
, NS_OK
);
61 rv
= aAlert
->GetCookie(cookie
);
62 NS_ENSURE_SUCCESS(rv
, NS_OK
);
65 rv
= aAlert
->GetName(name
);
66 NS_ENSURE_SUCCESS(rv
, NS_OK
);
69 rv
= aAlert
->GetLang(lang
);
70 NS_ENSURE_SUCCESS(rv
, NS_OK
);
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
);
81 rv
= aAlert
->GetURI(getter_AddRefs(uri
));
82 NS_ENSURE_SUCCESS(rv
, NS_OK
);
86 rv
= uri
->GetDisplaySpec(spec
);
87 NS_ENSURE_SUCCESS(rv
, NS_OK
);
91 rv
= aAlert
->GetSilent(&silent
);
92 NS_ENSURE_SUCCESS(rv
, NS_OK
);
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
) {
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
);
123 AndroidAlerts::CloseAlert(const nsAString
& aAlertName
, bool aContextClosed
) {
124 java::WebNotification::LocalRef notification
=
125 mNotificationsMap
.Get(aAlertName
);
130 java::GeckoRuntime::LocalRef runtime
= java::GeckoRuntime::GetInstance();
131 if (runtime
!= NULL
) {
132 runtime
->NotifyOnClose(notification
);
134 mNotificationsMap
.Remove(aAlertName
);
139 void AndroidAlerts::NotifyListener(const nsAString
& aName
, const char* aTopic
,
140 const char16_t
* aCookie
) {
145 nsCOMPtr
<nsIObserver
> listener
= sListenerMap
->Get(aName
);
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