2012-05-21 11:12:37 +00:00
|
|
|
/* 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/. */
|
2010-09-10 05:00:14 +00:00
|
|
|
|
|
|
|
#include "nsDesktopNotification.h"
|
|
|
|
|
2010-09-13 20:44:53 +00:00
|
|
|
#include "nsContentPermissionHelper.h"
|
|
|
|
#include "nsXULAppAPI.h"
|
|
|
|
|
|
|
|
#include "mozilla/dom/PBrowserChild.h"
|
|
|
|
#include "TabChild.h"
|
2011-05-25 06:31:59 +00:00
|
|
|
#include "mozilla/Preferences.h"
|
2010-09-13 20:44:53 +00:00
|
|
|
|
2011-05-25 06:31:59 +00:00
|
|
|
using namespace mozilla;
|
2010-09-13 20:44:53 +00:00
|
|
|
using namespace mozilla::dom;
|
|
|
|
|
2010-09-10 05:00:14 +00:00
|
|
|
/* ------------------------------------------------------------------------ */
|
|
|
|
/* AlertServiceObserver */
|
|
|
|
/* ------------------------------------------------------------------------ */
|
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS1(AlertServiceObserver, nsIObserver)
|
|
|
|
|
|
|
|
/* ------------------------------------------------------------------------ */
|
|
|
|
/* nsDesktopNotification */
|
|
|
|
/* ------------------------------------------------------------------------ */
|
|
|
|
|
2012-07-26 22:25:02 +00:00
|
|
|
nsresult
|
2010-09-10 05:00:14 +00:00
|
|
|
nsDOMDesktopNotification::PostDesktopNotification()
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIAlertsService> alerts = do_GetService("@mozilla.org/alerts-service;1");
|
|
|
|
if (!alerts)
|
2012-07-26 22:25:02 +00:00
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
2010-09-10 05:00:14 +00:00
|
|
|
|
|
|
|
if (!mObserver)
|
|
|
|
mObserver = new AlertServiceObserver(this);
|
|
|
|
|
2012-07-26 22:25:02 +00:00
|
|
|
return alerts->ShowAlertNotification(mIconURL, mTitle, mDescription,
|
|
|
|
true,
|
|
|
|
EmptyString(),
|
|
|
|
mObserver,
|
|
|
|
EmptyString());
|
2010-09-10 05:00:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
DOMCI_DATA(DesktopNotification, nsDOMDesktopNotification)
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_CLASS(nsDOMDesktopNotification)
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(nsDOMDesktopNotification, nsDOMEventTargetHelper)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(nsDOMDesktopNotification, nsDOMEventTargetHelper)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
|
|
|
|
|
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(nsDOMDesktopNotification)
|
|
|
|
NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIDOMDesktopNotification)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIDOMDesktopNotification)
|
|
|
|
NS_DOM_INTERFACE_MAP_ENTRY_CLASSINFO(DesktopNotification)
|
|
|
|
NS_INTERFACE_MAP_END_INHERITING(nsDOMEventTargetHelper)
|
|
|
|
|
|
|
|
NS_IMPL_ADDREF_INHERITED(nsDOMDesktopNotification, nsDOMEventTargetHelper)
|
|
|
|
NS_IMPL_RELEASE_INHERITED(nsDOMDesktopNotification, nsDOMEventTargetHelper)
|
|
|
|
|
2012-08-31 03:45:16 +00:00
|
|
|
NS_IMPL_EVENT_HANDLER(nsDOMDesktopNotification, click)
|
|
|
|
NS_IMPL_EVENT_HANDLER(nsDOMDesktopNotification, close)
|
|
|
|
|
2010-09-10 05:00:14 +00:00
|
|
|
nsDOMDesktopNotification::nsDOMDesktopNotification(const nsAString & title,
|
|
|
|
const nsAString & description,
|
|
|
|
const nsAString & iconURL,
|
|
|
|
nsPIDOMWindow *aWindow,
|
2012-07-30 14:58:26 +00:00
|
|
|
nsIPrincipal* principal)
|
2010-09-10 05:00:14 +00:00
|
|
|
: mTitle(title)
|
|
|
|
, mDescription(description)
|
|
|
|
, mIconURL(iconURL)
|
2012-07-30 14:58:26 +00:00
|
|
|
, mPrincipal(principal)
|
2011-10-17 14:59:28 +00:00
|
|
|
, mAllow(false)
|
|
|
|
, mShowHasBeenCalled(false)
|
2010-09-10 05:00:14 +00:00
|
|
|
{
|
2012-03-13 00:56:07 +00:00
|
|
|
BindToOwner(aWindow);
|
2011-09-29 06:19:26 +00:00
|
|
|
if (Preferences::GetBool("notification.disabled", false)) {
|
2011-01-19 17:38:36 +00:00
|
|
|
return;
|
2011-05-25 06:31:59 +00:00
|
|
|
}
|
2011-01-19 17:38:36 +00:00
|
|
|
|
|
|
|
// If we are in testing mode (running mochitests, for example)
|
|
|
|
// and we are suppose to allow requests, then just post an allow event.
|
2011-09-29 06:19:26 +00:00
|
|
|
if (Preferences::GetBool("notification.prompt.testing", false) &&
|
|
|
|
Preferences::GetBool("notification.prompt.testing.allow", true)) {
|
2011-10-17 14:59:28 +00:00
|
|
|
mAllow = true;
|
2011-01-19 17:38:36 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsRefPtr<nsDesktopNotificationRequest> request = new nsDesktopNotificationRequest(this);
|
|
|
|
|
|
|
|
// if we are in the content process, then remote it to the parent.
|
|
|
|
if (XRE_GetProcessType() == GeckoProcessType_Content) {
|
|
|
|
|
|
|
|
// if for some reason mOwner is null, just silently
|
|
|
|
// bail. The user will not see a notification, and that
|
|
|
|
// is fine.
|
2012-03-13 00:56:07 +00:00
|
|
|
if (!GetOwner())
|
2011-01-19 17:38:36 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
// because owner implements nsITabChild, we can assume that it is
|
|
|
|
// the one and only TabChild for this docshell.
|
2012-03-13 00:56:07 +00:00
|
|
|
TabChild* child = GetTabChildFrom(GetOwner()->GetDocShell());
|
2012-07-30 14:58:26 +00:00
|
|
|
|
2011-01-19 17:38:36 +00:00
|
|
|
// Retain a reference so the object isn't deleted without IPDL's knowledge.
|
|
|
|
// Corresponding release occurs in DeallocPContentPermissionRequest.
|
2011-09-16 20:22:44 +00:00
|
|
|
nsRefPtr<nsDesktopNotificationRequest> copy = request;
|
2011-01-19 17:38:36 +00:00
|
|
|
|
|
|
|
nsCString type = NS_LITERAL_CSTRING("desktop-notification");
|
2012-07-30 14:58:26 +00:00
|
|
|
child->SendPContentPermissionRequestConstructor(copy.forget().get(), type, IPC::Principal(mPrincipal));
|
|
|
|
|
2011-01-19 17:38:36 +00:00
|
|
|
request->Sendprompt();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// otherwise, dispatch it
|
|
|
|
NS_DispatchToMainThread(request);
|
|
|
|
|
2010-09-10 05:00:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nsDOMDesktopNotification::~nsDOMDesktopNotification()
|
|
|
|
{
|
|
|
|
if (mObserver) {
|
|
|
|
mObserver->Disconnect();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsDOMDesktopNotification::DispatchNotificationEvent(const nsString& aName)
|
|
|
|
{
|
|
|
|
if (NS_FAILED(CheckInnerWindowCorrectness())) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMEvent> event;
|
2012-07-30 14:20:58 +00:00
|
|
|
nsresult rv = NS_NewDOMEvent(getter_AddRefs(event), nullptr, nullptr);
|
2010-09-10 05:00:14 +00:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
// it doesn't bubble, and it isn't cancelable
|
2011-10-17 14:59:28 +00:00
|
|
|
rv = event->InitEvent(aName, false, false);
|
2010-09-10 05:00:14 +00:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2012-06-10 23:44:50 +00:00
|
|
|
event->SetTrusted(true);
|
2012-07-30 14:20:58 +00:00
|
|
|
DispatchDOMEvent(nullptr, event, nullptr, nullptr);
|
2010-09-10 05:00:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-26 22:25:02 +00:00
|
|
|
nsresult
|
2011-09-29 06:19:26 +00:00
|
|
|
nsDOMDesktopNotification::SetAllow(bool aAllow)
|
2011-01-19 17:38:36 +00:00
|
|
|
{
|
|
|
|
mAllow = aAllow;
|
|
|
|
|
|
|
|
// if we have called Show() already, lets go ahead and post a notification
|
|
|
|
if (mShowHasBeenCalled && aAllow)
|
2012-07-26 22:25:02 +00:00
|
|
|
return PostDesktopNotification();
|
|
|
|
|
|
|
|
return NS_OK;
|
2011-01-19 17:38:36 +00:00
|
|
|
}
|
|
|
|
|
2010-09-10 05:00:14 +00:00
|
|
|
void
|
|
|
|
nsDOMDesktopNotification::HandleAlertServiceNotification(const char *aTopic)
|
|
|
|
{
|
|
|
|
if (NS_FAILED(CheckInnerWindowCorrectness()))
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (!strcmp("alertclickcallback", aTopic)) {
|
|
|
|
DispatchNotificationEvent(NS_LITERAL_STRING("click"));
|
|
|
|
} else if (!strcmp("alertfinished", aTopic)) {
|
|
|
|
DispatchNotificationEvent(NS_LITERAL_STRING("close"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsDOMDesktopNotification::Show()
|
|
|
|
{
|
2011-10-17 14:59:28 +00:00
|
|
|
mShowHasBeenCalled = true;
|
2010-09-13 20:44:53 +00:00
|
|
|
|
2011-01-19 17:38:36 +00:00
|
|
|
if (!mAllow)
|
2010-09-13 20:44:53 +00:00
|
|
|
return NS_OK;
|
2010-09-10 05:00:14 +00:00
|
|
|
|
2012-07-26 22:25:02 +00:00
|
|
|
return PostDesktopNotification();
|
2010-09-10 05:00:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* ------------------------------------------------------------------------ */
|
|
|
|
/* nsDesktopNotificationCenter */
|
|
|
|
/* ------------------------------------------------------------------------ */
|
|
|
|
|
|
|
|
DOMCI_DATA(DesktopNotificationCenter, nsDesktopNotificationCenter)
|
|
|
|
|
|
|
|
NS_INTERFACE_MAP_BEGIN(nsDesktopNotificationCenter)
|
|
|
|
NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIDOMDesktopNotificationCenter)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIDOMDesktopNotificationCenter)
|
|
|
|
NS_DOM_INTERFACE_MAP_ENTRY_CLASSINFO(DesktopNotificationCenter)
|
|
|
|
NS_INTERFACE_MAP_END
|
|
|
|
|
|
|
|
NS_IMPL_ADDREF(nsDesktopNotificationCenter)
|
|
|
|
NS_IMPL_RELEASE(nsDesktopNotificationCenter)
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsDesktopNotificationCenter::CreateNotification(const nsAString & title,
|
|
|
|
const nsAString & description,
|
|
|
|
const nsAString & iconURL,
|
|
|
|
nsIDOMDesktopNotification **aResult)
|
|
|
|
{
|
2012-03-13 00:56:07 +00:00
|
|
|
NS_ENSURE_STATE(mOwner);
|
2010-09-10 05:00:14 +00:00
|
|
|
nsRefPtr<nsIDOMDesktopNotification> notification = new nsDOMDesktopNotification(title,
|
|
|
|
description,
|
|
|
|
iconURL,
|
|
|
|
mOwner,
|
2012-07-30 14:58:26 +00:00
|
|
|
mPrincipal);
|
2010-09-10 05:00:14 +00:00
|
|
|
notification.forget(aResult);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* ------------------------------------------------------------------------ */
|
|
|
|
/* nsDesktopNotificationRequest */
|
|
|
|
/* ------------------------------------------------------------------------ */
|
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS2(nsDesktopNotificationRequest,
|
2010-09-13 19:31:53 +00:00
|
|
|
nsIContentPermissionRequest,
|
2010-09-10 05:00:14 +00:00
|
|
|
nsIRunnable)
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-07-30 14:58:26 +00:00
|
|
|
nsDesktopNotificationRequest::GetPrincipal(nsIPrincipal * *aRequestingPrincipal)
|
2010-09-10 05:00:14 +00:00
|
|
|
{
|
|
|
|
if (!mDesktopNotification)
|
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
|
2012-07-30 14:58:26 +00:00
|
|
|
NS_IF_ADDREF(*aRequestingPrincipal = mDesktopNotification->mPrincipal);
|
2010-09-10 05:00:14 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2010-09-13 19:31:53 +00:00
|
|
|
nsDesktopNotificationRequest::GetWindow(nsIDOMWindow * *aRequestingWindow)
|
2010-09-10 05:00:14 +00:00
|
|
|
{
|
|
|
|
if (!mDesktopNotification)
|
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
|
2012-03-13 00:56:07 +00:00
|
|
|
nsCOMPtr<nsIDOMWindow> window =
|
|
|
|
do_QueryInterface(mDesktopNotification->GetOwner());
|
2010-09-10 05:00:14 +00:00
|
|
|
NS_IF_ADDREF(*aRequestingWindow = window);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2010-09-13 19:31:53 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsDesktopNotificationRequest::GetElement(nsIDOMElement * *aElement)
|
|
|
|
{
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2010-09-10 05:00:14 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsDesktopNotificationRequest::Cancel()
|
|
|
|
{
|
2012-07-26 22:25:02 +00:00
|
|
|
nsresult rv = mDesktopNotification->SetAllow(false);
|
2012-07-30 14:20:58 +00:00
|
|
|
mDesktopNotification = nullptr;
|
2012-07-26 22:25:02 +00:00
|
|
|
return rv;
|
2010-09-10 05:00:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsDesktopNotificationRequest::Allow()
|
|
|
|
{
|
2012-07-26 22:25:02 +00:00
|
|
|
nsresult rv = mDesktopNotification->SetAllow(true);
|
2012-07-30 14:20:58 +00:00
|
|
|
mDesktopNotification = nullptr;
|
2012-07-26 22:25:02 +00:00
|
|
|
return rv;
|
2010-09-10 05:00:14 +00:00
|
|
|
}
|
|
|
|
|
2010-09-13 19:31:53 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsDesktopNotificationRequest::GetType(nsACString & aType)
|
|
|
|
{
|
|
|
|
aType = "desktop-notification";
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|