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
|
/* -*- Mode: c++; tab-width: 4; 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 "AndroidAlerts.h"
#include "mozilla/dom/notification/NotificationHandler.h"
#include "mozilla/java/GeckoRuntimeWrappers.h"
#include "mozilla/java/WebNotificationWrappers.h"
#include "mozilla/java/WebNotificationActionWrappers.h"
#include "nsContentUtils.h"
#include "nsIPrincipal.h"
#include "nsIScriptSecurityManager.h"
#include "nsIURI.h"
using namespace mozilla::dom::notification;
namespace mozilla {
namespace widget {
NS_IMPL_ISUPPORTS(AndroidAlerts, nsIAlertsService)
struct AndroidNotificationTuple {
// Can be null if the caller doesn't care about the result.
nsCOMPtr<nsIObserver> mObserver;
// The Gecko alert notification.
nsCOMPtr<nsIAlertNotification> mAlert;
// The Java represented form of mAlert.
mozilla::java::WebNotification::GlobalRef mNotificationRef;
};
using NotificationMap = nsTHashMap<nsStringHashKey, AndroidNotificationTuple>;
static StaticAutoPtr<NotificationMap> sNotificationMap;
NS_IMETHODIMP
AndroidAlerts::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) {
MOZ_ASSERT_UNREACHABLE("Should be implemented by nsAlertsService.");
return NS_ERROR_NOT_IMPLEMENTED;
}
NS_IMETHODIMP
AndroidAlerts::ShowAlert(nsIAlertNotification* aAlert,
nsIObserver* aAlertListener) {
// nsAlertsService disables our alerts backend if we ever return failure
// here. To keep the backend enabled, we always return NS_OK even if we
// encounter an error here.
nsresult rv;
nsAutoString imageUrl;
rv = aAlert->GetImageURL(imageUrl);
NS_ENSURE_SUCCESS(rv, NS_OK);
nsAutoString title;
rv = aAlert->GetTitle(title);
NS_ENSURE_SUCCESS(rv, NS_OK);
nsAutoString text;
rv = aAlert->GetText(text);
NS_ENSURE_SUCCESS(rv, NS_OK);
nsAutoString cookie;
rv = aAlert->GetCookie(cookie);
NS_ENSURE_SUCCESS(rv, NS_OK);
nsAutoString name;
rv = aAlert->GetName(name);
NS_ENSURE_SUCCESS(rv, NS_OK);
nsAutoString lang;
rv = aAlert->GetLang(lang);
NS_ENSURE_SUCCESS(rv, NS_OK);
nsAutoString dir;
rv = aAlert->GetDir(dir);
NS_ENSURE_SUCCESS(rv, NS_OK);
bool requireInteraction;
rv = aAlert->GetRequireInteraction(&requireInteraction);
NS_ENSURE_SUCCESS(rv, NS_OK);
nsCOMPtr<nsIURI> uri;
rv = aAlert->GetURI(getter_AddRefs(uri));
NS_ENSURE_SUCCESS(rv, NS_OK);
nsCString spec;
if (uri) {
rv = uri->GetDisplaySpec(spec);
NS_ENSURE_SUCCESS(rv, NS_OK);
}
bool silent;
rv = aAlert->GetSilent(&silent);
NS_ENSURE_SUCCESS(rv, NS_OK);
bool privateBrowsing;
rv = aAlert->GetInPrivateBrowsing(&privateBrowsing);
NS_ENSURE_SUCCESS(rv, NS_OK);
nsTArray<uint32_t> vibrate;
rv = aAlert->GetVibrate(vibrate);
NS_ENSURE_SUCCESS(rv, NS_OK);
nsTArray<RefPtr<nsIAlertAction>> nsActions;
MOZ_TRY(aAlert->GetActions(nsActions));
jni::ObjectArray::LocalRef actions =
jni::ObjectArray::New(nsActions.Length());
size_t index = 0;
for (auto& nsAction : nsActions) {
nsAutoString name;
MOZ_TRY(nsAction->GetAction(name));
nsAutoString title;
MOZ_TRY(nsAction->GetTitle(title));
java::WebNotificationAction::LocalRef action =
java::WebNotificationAction::New(name, title);
actions->SetElement(index, action);
++index;
}
nsAutoCString origin;
rv = aAlert->GetOrigin(origin);
NS_ENSURE_SUCCESS(rv, NS_OK);
if (!sNotificationMap) {
sNotificationMap = new NotificationMap();
} else if (Maybe<AndroidNotificationTuple> tuple =
sNotificationMap->Extract(name)) {
if (tuple->mObserver) {
tuple->mObserver->Observe(nullptr, "alertfinished", u"close");
}
}
java::WebNotification::LocalRef notification = notification->New(
title, name, cookie, text, imageUrl, dir, lang, requireInteraction, spec,
silent, privateBrowsing, jni::IntArray::From(vibrate), actions, origin);
AndroidNotificationTuple tuple{
.mObserver = aAlertListener,
.mAlert = aAlert,
.mNotificationRef = notification,
};
sNotificationMap->InsertOrUpdate(name, std::move(tuple));
if (java::GeckoRuntime::LocalRef runtime =
java::GeckoRuntime::GetInstance()) {
runtime->NotifyOnShow(notification);
}
return NS_OK;
}
NS_IMETHODIMP
AndroidAlerts::CloseAlert(const nsAString& aAlertName, bool aContextClosed) {
if (!sNotificationMap) {
return NS_OK;
}
Maybe<AndroidNotificationTuple> tuple = sNotificationMap->Extract(aAlertName);
if (!tuple) {
return NS_OK;
}
if (tuple->mObserver) {
// All CloseAlert implementation is expected to fire alertfinished
// synchronously. (See bug 1975432 to deduplicate this logic)
// We have to fire alertfinished here as we are closing it ourselves;
// GeckoView will only send it when it's closed from Android side.
tuple->mObserver->Observe(nullptr, "alertfinished", u"close");
}
java::GeckoRuntime::LocalRef runtime = java::GeckoRuntime::GetInstance();
if (runtime != NULL) {
runtime->NotifyOnClose(tuple->mNotificationRef);
}
return NS_OK;
}
NS_IMETHODIMP AndroidAlerts::GetHistory(nsTArray<nsString>& aResult) {
// TODO: Implement this using NotificationManager.getActiveNotifications
// https://developer.android.com/reference/android/app/NotificationManager#getActiveNotifications()
// See bug 1971394.
return NS_ERROR_NOT_IMPLEMENTED;
}
NS_IMETHODIMP AndroidAlerts::Teardown() {
sNotificationMap = nullptr;
return NS_OK;
}
NS_IMETHODIMP AndroidAlerts::PbmTeardown() { return NS_ERROR_NOT_IMPLEMENTED; }
nsresult RespondViaNotificationHandler(const nsAString& aName,
const nsACString& aTopic,
Maybe<nsString> aAction,
const nsACString& aOrigin) {
if (aTopic != "alertclickcallback"_ns) {
// NOTE(krosylight): we are not handling alertfinished as we don't want to
// open the app for each notification dismiss.
return NS_OK;
}
nsCOMPtr<nsIPrincipal> principal;
if (nsCOMPtr<nsIScriptSecurityManager> ssm =
nsContentUtils::GetSecurityManager()) {
MOZ_TRY(ssm->CreateContentPrincipalFromOrigin(aOrigin,
getter_AddRefs(principal)));
} else {
return NS_ERROR_NOT_AVAILABLE;
}
RefPtr<NotificationHandler> handler = NotificationHandler::GetSingleton();
MOZ_TRY(handler->RespondOnClick(principal, aName, aAction ? *aAction : u""_ns,
/* aAutoClosed */ !aAction, nullptr));
return NS_OK;
}
void AndroidAlerts::NotifyListener(const nsAString& aName, const char* aTopic,
Maybe<nsString> aAction,
const nsACString& aOrigin) {
nsDependentCString topic(aTopic);
if (!sNotificationMap) {
RespondViaNotificationHandler(aName, topic, aAction, aOrigin);
return;
}
Maybe<AndroidNotificationTuple> tuple = sNotificationMap->MaybeGet(aName);
if (!tuple) {
RespondViaNotificationHandler(aName, topic, aAction, aOrigin);
return;
}
if (tuple->mObserver) {
nsCOMPtr<nsIAlertAction> action;
if (aAction) {
tuple->mAlert->GetAction(*aAction, getter_AddRefs(action));
}
tuple->mObserver->Observe(action, aTopic, nullptr);
}
if ("alertfinished"_ns.Equals(topic)) {
sNotificationMap->Remove(aName);
}
}
} // namespace widget
} // namespace mozilla
|