Bug 1676529 [wpt PR 26467] - [LayoutNG] Find sibling spanners using the child iterato...
[gecko.git] / widget / android / AndroidAlerts.cpp
blobb42bbd1ddcf69ee8e6c2907a29926c4db48d8bbe
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 nsDataHashtable<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 if (aPersistentData.IsEmpty() && aAlertListener) {
91 if (!sListenerMap) {
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);
106 return NS_OK;
109 NS_IMETHODIMP
110 AndroidAlerts::CloseAlert(const nsAString& aAlertName,
111 nsIPrincipal* aPrincipal) {
112 java::WebNotification::LocalRef notification =
113 mNotificationsMap.Get(aAlertName);
114 if (!notification) {
115 return NS_OK;
118 java::GeckoRuntime::LocalRef runtime = java::GeckoRuntime::GetInstance();
119 if (runtime != NULL) {
120 runtime->NotifyOnClose(notification);
122 mNotificationsMap.Remove(aAlertName);
124 return NS_OK;
127 void AndroidAlerts::NotifyListener(const nsAString& aName, const char* aTopic,
128 const char16_t* aCookie) {
129 if (!sListenerMap) {
130 return;
133 nsCOMPtr<nsIObserver> listener = sListenerMap->Get(aName);
134 if (!listener) {
135 return;
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