Bug 1894039 - Part 1: Use BindingUtils functions for enums r=asuth

This unfortunately adds some ASCII-to-UTF16 conversion because:

1. Enums use nsLiteralCString internally
2. nsAlertsService expects nsString. (macOS and Windows expects UTF16 while libnotify expects UTF8)
3. ServiceWorker events currently expect nsString. Fixing this requires touching quite some files and I filed bug 1894240 for that.

Differential Revision: https://phabricator.services.mozilla.com/D208920
This commit is contained in:
Kagami Sascha Rosylight 2024-05-14 14:13:28 +00:00
Родитель 71253003e0
Коммит b8f9ad47b9
3 изменённых файлов: 29 добавлений и 44 удалений

Просмотреть файл

@ -68,7 +68,7 @@ interface nsINotificationStorage : nsISupports
void put(in AString origin,
in AString id,
in AString title,
in AString dir,
in ACString dir,
in AString lang,
in AString body,
in AString tag,

Просмотреть файл

@ -808,7 +808,8 @@ Notification::ConstructFromFields(
MOZ_ASSERT(aGlobal);
RootedDictionary<NotificationOptions> options(RootingCx());
options.mDir = Notification::StringToDirection(nsString(aDir));
options.mDir = StringToEnum<NotificationDirection>(aDir).valueOr(
NotificationDirection::Auto);
options.mLang = aLang;
options.mBody = aBody;
options.mTag = aTag;
@ -853,9 +854,9 @@ nsresult Notification::PersistNotification() {
return NS_ERROR_FAILURE;
}
rv = notificationStorage->Put(origin, id, mTitle, DirectionToString(mDir),
mLang, mBody, mTag, mIconUrl, alertName,
mDataAsBase64, behavior, mScope);
rv = notificationStorage->Put(origin, id, mTitle, GetEnumString(mDir), mLang,
mBody, mTag, mIconUrl, alertName, mDataAsBase64,
behavior, mScope);
if (NS_FAILED(rv)) {
return rv;
@ -1012,9 +1013,10 @@ class ServiceWorkerNotificationObserver final : public nsIObserver {
ServiceWorkerNotificationObserver(
const nsAString& aScope, nsIPrincipal* aPrincipal, const nsAString& aID,
const nsAString& aTitle, const nsAString& aDir, const nsAString& aLang,
const nsAString& aBody, const nsAString& aTag, const nsAString& aIcon,
const nsAString& aData, const nsAString& aBehavior)
const nsAString& aTitle, NotificationDirection aDir,
const nsAString& aLang, const nsAString& aBody, const nsAString& aTag,
const nsAString& aIcon, const nsAString& aData,
const nsAString& aBehavior)
: mScope(aScope),
mID(aID),
mPrincipal(aPrincipal),
@ -1037,7 +1039,7 @@ class ServiceWorkerNotificationObserver final : public nsIObserver {
const nsString mID;
nsCOMPtr<nsIPrincipal> mPrincipal;
const nsString mTitle;
const nsString mDir;
const NotificationDirection mDir;
const nsString mLang;
const nsString mBody;
const nsString mTag;
@ -1242,14 +1244,16 @@ ServiceWorkerNotificationObserver::Observe(nsISupports* aSubject,
}
rv = swm->SendNotificationClickEvent(
originSuffix, NS_ConvertUTF16toUTF8(mScope), mID, mTitle, mDir, mLang,
mBody, mTag, mIcon, mData, mBehavior);
originSuffix, NS_ConvertUTF16toUTF8(mScope), mID, mTitle,
NS_ConvertASCIItoUTF16(GetEnumString(mDir)), mLang, mBody, mTag,
mIcon, mData, mBehavior);
Unused << NS_WARN_IF(NS_FAILED(rv));
} else {
auto* cc = ContentChild::GetSingleton();
NotificationEventData data(originSuffix, NS_ConvertUTF16toUTF8(mScope),
mID, mTitle, mDir, mLang, mBody, mTag, mIcon,
mData, mBehavior);
mID, mTitle,
NS_ConvertASCIItoUTF16(GetEnumString(mDir)),
mLang, mBody, mTag, mIcon, mData, mBehavior);
Unused << cc->SendNotificationEvent(u"click"_ns, data);
}
return NS_OK;
@ -1277,14 +1281,16 @@ ServiceWorkerNotificationObserver::Observe(nsISupports* aSubject,
}
rv = swm->SendNotificationCloseEvent(
originSuffix, NS_ConvertUTF16toUTF8(mScope), mID, mTitle, mDir, mLang,
mBody, mTag, mIcon, mData, mBehavior);
originSuffix, NS_ConvertUTF16toUTF8(mScope), mID, mTitle,
NS_ConvertASCIItoUTF16(GetEnumString(mDir)), mLang, mBody, mTag,
mIcon, mData, mBehavior);
Unused << NS_WARN_IF(NS_FAILED(rv));
} else {
auto* cc = ContentChild::GetSingleton();
NotificationEventData data(originSuffix, NS_ConvertUTF16toUTF8(mScope),
mID, mTitle, mDir, mLang, mBody, mTag, mIcon,
mData, mBehavior);
mID, mTitle,
NS_ConvertASCIItoUTF16(GetEnumString(mDir)),
mLang, mBody, mTag, mIcon, mData, mBehavior);
Unused << cc->SendNotificationEvent(u"close"_ns, data);
}
return NS_OK;
@ -1417,8 +1423,8 @@ void Notification::ShowInternal() {
behavior.Truncate();
}
observer = new ServiceWorkerNotificationObserver(
mScope, GetPrincipal(), mID, mTitle, DirectionToString(mDir), mLang,
mBody, mTag, iconUrl, mDataAsBase64, behavior);
mScope, GetPrincipal(), mID, mTitle, mDir, mLang, mBody, mTag, iconUrl,
mDataAsBase64, behavior);
}
MOZ_ASSERT(observer);
nsCOMPtr<nsIObserver> alertObserver =
@ -1441,10 +1447,10 @@ void Notification::ShowInternal() {
do_CreateInstance(ALERT_NOTIFICATION_CONTRACTID);
NS_ENSURE_TRUE_VOID(alert);
nsIPrincipal* principal = GetPrincipal();
rv =
alert->Init(alertName, iconUrl, mTitle, mBody, true, uniqueCookie,
DirectionToString(mDir), mLang, mDataAsBase64, GetPrincipal(),
inPrivateBrowsing, requireInteraction, mSilent, mVibrate);
rv = alert->Init(alertName, iconUrl, mTitle, mBody, true, uniqueCookie,
NS_ConvertASCIItoUTF16(GetEnumString(mDir)), mLang,
mDataAsBase64, GetPrincipal(), inPrivateBrowsing,
requireInteraction, mSilent, mVibrate);
NS_ENSURE_SUCCESS_VOID(rv);
if (isPersistent) {

Просмотреть файл

@ -254,27 +254,6 @@ class Notification : public DOMEventTargetHelper,
static NotificationPermission GetPermissionInternal(
nsPIDOMWindowInner* aWindow, ErrorResult& rv);
static const nsString DirectionToString(NotificationDirection aDirection) {
switch (aDirection) {
case NotificationDirection::Ltr:
return u"ltr"_ns;
case NotificationDirection::Rtl:
return u"rtl"_ns;
default:
return u"auto"_ns;
}
}
static NotificationDirection StringToDirection(const nsAString& aDirection) {
if (aDirection.EqualsLiteral("ltr")) {
return NotificationDirection::Ltr;
}
if (aDirection.EqualsLiteral("rtl")) {
return NotificationDirection::Rtl;
}
return NotificationDirection::Auto;
}
static nsresult GetOrigin(nsIPrincipal* aPrincipal, nsString& aOrigin);
void GetAlertName(nsAString& aRetval) {