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 nsDataHashtable
<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
);
90 if (aPersistentData
.IsEmpty() && aAlertListener
) {
92 sListenerMap
= new ListenerMap();
94 // This will remove any observers already registered for this name.
95 sListenerMap
->Put(name
, aAlertListener
);
98 java::WebNotification::LocalRef notification
= notification
->New(
99 title
, name
, cookie
, text
, imageUrl
, dir
, lang
, requireInteraction
, spec
);
100 java::GeckoRuntime::LocalRef runtime
= java::GeckoRuntime::GetInstance();
101 if (runtime
!= NULL
) {
102 runtime
->NotifyOnShow(notification
);
104 mNotificationsMap
.Put(name
, notification
);
110 AndroidAlerts::CloseAlert(const nsAString
& aAlertName
,
111 nsIPrincipal
* aPrincipal
) {
112 java::WebNotification::LocalRef notification
=
113 mNotificationsMap
.Get(aAlertName
);
118 java::GeckoRuntime::LocalRef runtime
= java::GeckoRuntime::GetInstance();
119 if (runtime
!= NULL
) {
120 runtime
->NotifyOnClose(notification
);
122 mNotificationsMap
.Remove(aAlertName
);
127 void AndroidAlerts::NotifyListener(const nsAString
& aName
, const char* aTopic
,
128 const char16_t
* aCookie
) {
133 nsCOMPtr
<nsIObserver
> listener
= sListenerMap
->Get(aName
);
138 listener
->Observe(nullptr, aTopic
, aCookie
);
140 if ("alertfinished"_ns
.Equals(aTopic
)) {
141 sListenerMap
->Remove(aName
);
142 mNotificationsMap
.Remove(aName
);
146 } // namespace widget
147 } // namespace mozilla