Bug 1227300, Part 2 - Implement `showAlert`. r=MattN,wchen

--HG--
extra : commitid : HrMh88lKFVK
extra : rebase_source : e8a8def77f0e12152ecb48986f3c1a850d6ac9c4
This commit is contained in:
Kit Cambridge 2015-12-31 12:04:24 -07:00
Родитель 0d37a8bacb
Коммит 87582e390a
5 изменённых файлов: 84 добавлений и 54 удалений

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

@ -4432,23 +4432,24 @@ ContentParent::HasNotificationPermission(const IPC::Principal& aPrincipal)
}
bool
ContentParent::RecvShowAlertNotification(const nsString& aImageUrl, const nsString& aTitle,
const nsString& aText, const bool& aTextClickable,
const nsString& aCookie, const nsString& aName,
const nsString& aBidi, const nsString& aLang,
const nsString& aData,
const IPC::Principal& aPrincipal,
const bool& aInPrivateBrowsing)
ContentParent::RecvShowAlert(const AlertNotificationType& aAlert)
{
if (!HasNotificationPermission(aPrincipal)) {
nsCOMPtr<nsIAlertNotification> alert(dont_AddRef(aAlert));
if (NS_WARN_IF(!alert)) {
return true;
}
nsCOMPtr<nsIPrincipal> principal;
nsresult rv = alert->GetPrincipal(getter_AddRefs(principal));
if (NS_WARN_IF(NS_FAILED(rv)) ||
!HasNotificationPermission(IPC::Principal(principal))) {
return true;
}
nsCOMPtr<nsIAlertsService> sysAlerts(do_GetService(NS_ALERTSERVICE_CONTRACTID));
if (sysAlerts) {
sysAlerts->ShowAlertNotification(aImageUrl, aTitle, aText, aTextClickable,
aCookie, this, aName, aBidi, aLang,
aData, aPrincipal, aInPrivateBrowsing);
sysAlerts->ShowAlert(alert, this);
}
return true;
}

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

@ -787,13 +787,7 @@ private:
bool HasNotificationPermission(const IPC::Principal& aPrincipal);
virtual bool RecvShowAlertNotification(const nsString& aImageUrl, const nsString& aTitle,
const nsString& aText, const bool& aTextClickable,
const nsString& aCookie, const nsString& aName,
const nsString& aBidi, const nsString& aLang,
const nsString& aData,
const IPC::Principal& aPrincipal,
const bool& aInPrivateBrowsing) override;
virtual bool RecvShowAlert(const AlertNotificationType& aAlert) override;
virtual bool RecvCloseAlert(const nsString& aName,
const IPC::Principal& aPrincipal) override;

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

@ -74,6 +74,7 @@ include ProfilerTypes;
include "mozilla/dom/PContentBridgeParent.h";
using GeoPosition from "nsGeoPositionIPCSerialiser.h";
using AlertNotificationType from "mozilla/AlertNotificationIPCSerializer.h";
using struct ChromePackage from "mozilla/chrome/RegistryMessageUtils.h";
using struct SubstitutionMapping from "mozilla/chrome/RegistryMessageUtils.h";
@ -881,17 +882,7 @@ parent:
CpowEntry[] aCpows, Principal aPrincipal)
returns (StructuredCloneData[] retval);
ShowAlertNotification(nsString imageUrl,
nsString title,
nsString text,
bool textClickable,
nsString cookie,
nsString name,
nsString bidi,
nsString lang,
nsString data,
Principal principal,
bool inPrivateBrowsing);
ShowAlert(AlertNotificationType alert);
CloseAlert(nsString name, Principal principal);

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

@ -73,40 +73,66 @@ NS_IMETHODIMP nsAlertsService::ShowAlertNotification(const nsAString & aImageUrl
nsIPrincipal * aPrincipal,
bool aInPrivateBrowsing)
{
nsCOMPtr<nsIAlertNotification> alert =
do_CreateInstance(ALERT_NOTIFICATION_CONTRACTID);
NS_ENSURE_TRUE(alert, NS_ERROR_FAILURE);
nsresult rv = alert->Init(aAlertName, aImageUrl, aAlertTitle,
aAlertText, aAlertTextClickable,
aAlertCookie, aBidi, aLang, aData,
aPrincipal, aInPrivateBrowsing);
NS_ENSURE_SUCCESS(rv, rv);
return ShowAlert(alert, aAlertListener);
}
NS_IMETHODIMP nsAlertsService::ShowAlert(nsIAlertNotification * aAlert,
nsIObserver * aAlertListener)
{
NS_ENSURE_ARG(aAlert);
nsAutoString cookie;
nsresult rv = aAlert->GetCookie(cookie);
NS_ENSURE_SUCCESS(rv, rv);
if (XRE_IsContentProcess()) {
ContentChild* cpc = ContentChild::GetSingleton();
if (aAlertListener)
cpc->AddRemoteAlertObserver(PromiseFlatString(aAlertCookie), aAlertListener);
cpc->AddRemoteAlertObserver(cookie, aAlertListener);
cpc->SendShowAlertNotification(PromiseFlatString(aImageUrl),
PromiseFlatString(aAlertTitle),
PromiseFlatString(aAlertText),
aAlertTextClickable,
PromiseFlatString(aAlertCookie),
PromiseFlatString(aAlertName),
PromiseFlatString(aBidi),
PromiseFlatString(aLang),
PromiseFlatString(aData),
IPC::Principal(aPrincipal),
aInPrivateBrowsing);
cpc->SendShowAlert(aAlert);
return NS_OK;
}
nsAutoString imageUrl;
rv = aAlert->GetImageURL(imageUrl);
NS_ENSURE_SUCCESS(rv, rv);
nsAutoString title;
rv = aAlert->GetTitle(title);
NS_ENSURE_SUCCESS(rv, rv);
nsAutoString text;
rv = aAlert->GetText(text);
NS_ENSURE_SUCCESS(rv, rv);
nsAutoString name;
rv = aAlert->GetName(name);
NS_ENSURE_SUCCESS(rv, rv);
nsCOMPtr<nsIPrincipal> principal;
rv = aAlert->GetPrincipal(getter_AddRefs(principal));
NS_ENSURE_SUCCESS(rv, rv);
#ifdef MOZ_WIDGET_ANDROID
mozilla::AndroidBridge::Bridge()->ShowAlertNotification(aImageUrl, aAlertTitle, aAlertText, aAlertCookie,
aAlertListener, aAlertName, aPrincipal);
mozilla::AndroidBridge::Bridge()->ShowAlertNotification(imageUrl, title, text, cookie,
aAlertListener, name, principal);
return NS_OK;
#else
// Check if there is an optional service that handles system-level notifications
nsCOMPtr<nsIAlertsService> sysAlerts(do_GetService(NS_SYSTEMALERTSERVICE_CONTRACTID));
nsresult rv;
if (sysAlerts) {
rv = sysAlerts->ShowAlertNotification(aImageUrl, aAlertTitle, aAlertText, aAlertTextClickable,
aAlertCookie, aAlertListener, aAlertName,
aBidi, aLang, aData,
IPC::Principal(aPrincipal),
aInPrivateBrowsing);
rv = sysAlerts->ShowAlert(aAlert, aAlertListener);
if (NS_SUCCEEDED(rv))
return NS_OK;
}
@ -114,14 +140,30 @@ NS_IMETHODIMP nsAlertsService::ShowAlertNotification(const nsAString & aImageUrl
if (!ShouldShowAlert()) {
// Do not display the alert. Instead call alertfinished and get out.
if (aAlertListener)
aAlertListener->Observe(nullptr, "alertfinished", PromiseFlatString(aAlertCookie).get());
aAlertListener->Observe(nullptr, "alertfinished", cookie.get());
return NS_OK;
}
bool textClickable;
rv = aAlert->GetTextClickable(&textClickable);
NS_ENSURE_SUCCESS(rv, rv);
nsAutoString bidi;
rv = aAlert->GetDir(bidi);
NS_ENSURE_SUCCESS(rv, rv);
nsAutoString lang;
rv = aAlert->GetLang(lang);
NS_ENSURE_SUCCESS(rv, rv);
bool inPrivateBrowsing;
rv = aAlert->GetInPrivateBrowsing(&inPrivateBrowsing);
NS_ENSURE_SUCCESS(rv, rv);
// Use XUL notifications as a fallback if above methods have failed.
rv = mXULAlerts.ShowAlertNotification(aImageUrl, aAlertTitle, aAlertText, aAlertTextClickable,
aAlertCookie, aAlertListener, aAlertName,
aBidi, aLang, aPrincipal, aInPrivateBrowsing);
rv = mXULAlerts.ShowAlertNotification(imageUrl, title, text, textClickable,
cookie, aAlertListener, name,
bidi, lang, principal, inPrivateBrowsing);
return rv;
#endif // !MOZ_WIDGET_ANDROID
}

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

@ -93,9 +93,11 @@ interface nsIAlertNotification : nsISupports
readonly attribute boolean inPrivateBrowsing;
};
[scriptable, uuid(9d0284bf-db40-42da-8f0d-c2769dbde7aa)]
[scriptable, uuid(f7a36392-d98b-4141-a7d7-4e46642684e3)]
interface nsIAlertsService : nsISupports
{
void showAlert(in nsIAlertNotification alert,
[optional] in nsIObserver alertListener);
/**
* Displays a sliding notification window.
*