2013-03-18 06:24:54 -07: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/. */
|
|
|
|
|
|
|
|
#include "PCOMContentPermissionRequestChild.h"
|
|
|
|
#include "mozilla/dom/Notification.h"
|
2013-09-10 00:03:37 -07:00
|
|
|
#include "mozilla/dom/OwningNonNull.h"
|
2012-07-12 08:45:38 -07:00
|
|
|
#include "mozilla/Preferences.h"
|
2013-03-18 06:24:54 -07:00
|
|
|
#include "TabChild.h"
|
|
|
|
#include "nsContentUtils.h"
|
|
|
|
#include "nsDOMEvent.h"
|
|
|
|
#include "nsIAlertsService.h"
|
|
|
|
#include "nsIContentPermissionPrompt.h"
|
|
|
|
#include "nsIDocument.h"
|
|
|
|
#include "nsIPermissionManager.h"
|
|
|
|
#include "nsServiceManagerUtils.h"
|
|
|
|
#include "nsToolkitCompsCID.h"
|
|
|
|
#include "nsGlobalWindow.h"
|
|
|
|
#include "nsDOMJSUtils.h"
|
2013-09-10 07:49:09 -07:00
|
|
|
#ifdef MOZ_B2G
|
|
|
|
#include "nsIDOMDesktopNotification.h"
|
|
|
|
#include "nsIAppsService.h"
|
|
|
|
#endif
|
2013-03-18 06:24:54 -07:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
|
|
|
class NotificationPermissionRequest : public nsIContentPermissionRequest,
|
|
|
|
public PCOMContentPermissionRequestChild,
|
|
|
|
public nsIRunnable
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
NS_DECL_CYCLE_COLLECTING_ISUPPORTS
|
|
|
|
NS_DECL_NSICONTENTPERMISSIONREQUEST
|
|
|
|
NS_DECL_NSIRUNNABLE
|
|
|
|
NS_DECL_CYCLE_COLLECTION_CLASS_AMBIGUOUS(NotificationPermissionRequest,
|
|
|
|
nsIContentPermissionRequest)
|
|
|
|
|
|
|
|
NotificationPermissionRequest(nsIPrincipal* aPrincipal, nsPIDOMWindow* aWindow,
|
|
|
|
NotificationPermissionCallback* aCallback)
|
|
|
|
: mPrincipal(aPrincipal), mWindow(aWindow),
|
2013-05-06 12:28:13 -07:00
|
|
|
mPermission(NotificationPermission::Default),
|
2013-03-18 06:24:54 -07:00
|
|
|
mCallback(aCallback) {}
|
|
|
|
|
|
|
|
virtual ~NotificationPermissionRequest() {}
|
|
|
|
|
|
|
|
bool Recv__delete__(const bool& aAllow);
|
|
|
|
void IPDLRelease() { Release(); }
|
|
|
|
|
|
|
|
protected:
|
|
|
|
nsresult CallCallback();
|
2012-07-12 08:45:38 -07:00
|
|
|
nsresult DispatchCallback();
|
2013-03-18 06:24:54 -07:00
|
|
|
nsCOMPtr<nsIPrincipal> mPrincipal;
|
|
|
|
nsCOMPtr<nsPIDOMWindow> mWindow;
|
|
|
|
NotificationPermission mPermission;
|
|
|
|
nsRefPtr<NotificationPermissionCallback> mCallback;
|
|
|
|
};
|
|
|
|
|
|
|
|
class NotificationObserver : public nsIObserver
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
NS_DECL_NSIOBSERVER
|
|
|
|
|
|
|
|
NotificationObserver(Notification* aNotification)
|
|
|
|
: mNotification(aNotification) {}
|
|
|
|
|
|
|
|
virtual ~NotificationObserver() {}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
nsRefPtr<Notification> mNotification;
|
|
|
|
};
|
|
|
|
|
|
|
|
class NotificationTask : public nsIRunnable
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
NS_DECL_NSIRUNNABLE
|
|
|
|
|
|
|
|
enum NotificationAction {
|
|
|
|
eShow,
|
|
|
|
eClose
|
|
|
|
};
|
|
|
|
|
|
|
|
NotificationTask(Notification* aNotification, NotificationAction aAction)
|
|
|
|
: mNotification(aNotification), mAction(aAction) {}
|
|
|
|
|
|
|
|
virtual ~NotificationTask() {}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
nsRefPtr<Notification> mNotification;
|
|
|
|
NotificationAction mAction;
|
|
|
|
};
|
|
|
|
|
|
|
|
uint32_t Notification::sCount = 0;
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_1(NotificationPermissionRequest, mWindow)
|
|
|
|
|
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(NotificationPermissionRequest)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIContentPermissionRequest)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIRunnable)
|
|
|
|
NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIContentPermissionRequest)
|
|
|
|
NS_INTERFACE_MAP_END
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_ADDREF(NotificationPermissionRequest)
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_RELEASE(NotificationPermissionRequest)
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
NotificationPermissionRequest::Run()
|
|
|
|
{
|
2013-05-23 15:38:00 -07:00
|
|
|
if (nsContentUtils::IsSystemPrincipal(mPrincipal)) {
|
2013-05-06 12:28:13 -07:00
|
|
|
mPermission = NotificationPermission::Granted;
|
2013-05-23 15:38:00 -07:00
|
|
|
} else {
|
|
|
|
// File are automatically granted permission.
|
|
|
|
nsCOMPtr<nsIURI> uri;
|
|
|
|
mPrincipal->GetURI(getter_AddRefs(uri));
|
|
|
|
|
|
|
|
if (uri) {
|
|
|
|
bool isFile;
|
|
|
|
uri->SchemeIs("file", &isFile);
|
|
|
|
if (isFile) {
|
|
|
|
mPermission = NotificationPermission::Granted;
|
|
|
|
}
|
|
|
|
}
|
2012-07-12 08:45:38 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Grant permission if pref'ed on.
|
|
|
|
if (Preferences::GetBool("notification.prompt.testing", false)) {
|
|
|
|
if (Preferences::GetBool("notification.prompt.testing.allow", true)) {
|
2013-05-06 12:28:13 -07:00
|
|
|
mPermission = NotificationPermission::Granted;
|
2012-07-12 08:45:38 -07:00
|
|
|
} else {
|
2013-05-06 12:28:13 -07:00
|
|
|
mPermission = NotificationPermission::Denied;
|
2012-07-12 08:45:38 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-06 12:28:13 -07:00
|
|
|
if (mPermission != NotificationPermission::Default) {
|
2012-07-12 08:45:38 -07:00
|
|
|
return DispatchCallback();
|
|
|
|
}
|
|
|
|
|
2013-03-18 06:24:54 -07:00
|
|
|
if (XRE_GetProcessType() == GeckoProcessType_Content) {
|
|
|
|
// because owner implements nsITabChild, we can assume that it is
|
|
|
|
// the one and only TabChild.
|
2013-09-12 12:24:11 -07:00
|
|
|
TabChild* child = TabChild::GetFrom(mWindow->GetDocShell());
|
2013-03-18 06:24:54 -07:00
|
|
|
if (!child) {
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Retain a reference so the object isn't deleted without IPDL's knowledge.
|
|
|
|
// Corresponding release occurs in DeallocPContentPermissionRequest.
|
|
|
|
AddRef();
|
|
|
|
|
|
|
|
NS_NAMED_LITERAL_CSTRING(type, "desktop-notification");
|
|
|
|
NS_NAMED_LITERAL_CSTRING(access, "unused");
|
|
|
|
child->SendPContentPermissionRequestConstructor(this, type, access,
|
|
|
|
IPC::Principal(mPrincipal));
|
|
|
|
|
|
|
|
Sendprompt();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIContentPermissionPrompt> prompt =
|
|
|
|
do_GetService(NS_CONTENT_PERMISSION_PROMPT_CONTRACTID);
|
|
|
|
if (prompt) {
|
|
|
|
prompt->Prompt(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
NotificationPermissionRequest::GetPrincipal(nsIPrincipal** aRequestingPrincipal)
|
|
|
|
{
|
|
|
|
NS_ADDREF(*aRequestingPrincipal = mPrincipal);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
NotificationPermissionRequest::GetWindow(nsIDOMWindow** aRequestingWindow)
|
|
|
|
{
|
|
|
|
NS_ADDREF(*aRequestingWindow = mWindow);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
NotificationPermissionRequest::GetElement(nsIDOMElement** aElement)
|
|
|
|
{
|
2013-09-04 08:40:16 -07:00
|
|
|
NS_ENSURE_ARG_POINTER(aElement);
|
|
|
|
*aElement = nullptr;
|
|
|
|
return NS_OK;
|
2013-03-18 06:24:54 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
NotificationPermissionRequest::Cancel()
|
|
|
|
{
|
2013-05-06 12:28:13 -07:00
|
|
|
mPermission = NotificationPermission::Denied;
|
2012-07-12 08:45:38 -07:00
|
|
|
return DispatchCallback();
|
2013-03-18 06:24:54 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
NotificationPermissionRequest::Allow()
|
|
|
|
{
|
2013-05-06 12:28:13 -07:00
|
|
|
mPermission = NotificationPermission::Granted;
|
2012-07-12 08:45:38 -07:00
|
|
|
return DispatchCallback();
|
|
|
|
}
|
|
|
|
|
|
|
|
inline nsresult
|
|
|
|
NotificationPermissionRequest::DispatchCallback()
|
|
|
|
{
|
|
|
|
if (!mCallback) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-03-18 06:24:54 -07:00
|
|
|
nsCOMPtr<nsIRunnable> callbackRunnable = NS_NewRunnableMethod(this,
|
|
|
|
&NotificationPermissionRequest::CallCallback);
|
2012-07-12 08:45:38 -07:00
|
|
|
return NS_DispatchToMainThread(callbackRunnable);
|
2013-03-18 06:24:54 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
NotificationPermissionRequest::CallCallback()
|
|
|
|
{
|
|
|
|
ErrorResult rv;
|
|
|
|
mCallback->Call(mPermission, rv);
|
|
|
|
return rv.ErrorCode();
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
NotificationPermissionRequest::GetAccess(nsACString& aAccess)
|
|
|
|
{
|
|
|
|
aAccess.AssignLiteral("unused");
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
NotificationPermissionRequest::GetType(nsACString& aType)
|
|
|
|
{
|
|
|
|
aType.AssignLiteral("desktop-notification");
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
NotificationPermissionRequest::Recv__delete__(const bool& aAllow)
|
|
|
|
{
|
|
|
|
if (aAllow) {
|
|
|
|
(void) Allow();
|
|
|
|
} else {
|
|
|
|
(void) Cancel();
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS1(NotificationTask, nsIRunnable)
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
NotificationTask::Run()
|
|
|
|
{
|
|
|
|
switch (mAction) {
|
|
|
|
case eShow:
|
|
|
|
return mNotification->ShowInternal();
|
|
|
|
case eClose:
|
|
|
|
return mNotification->CloseInternal();
|
|
|
|
default:
|
2013-06-28 18:38:30 -07:00
|
|
|
MOZ_CRASH("Unexpected action for NotificationTask.");
|
2013-03-18 06:24:54 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS1(NotificationObserver, nsIObserver)
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
NotificationObserver::Observe(nsISupports* aSubject, const char* aTopic,
|
|
|
|
const PRUnichar* aData)
|
|
|
|
{
|
|
|
|
if (!strcmp("alertclickcallback", aTopic)) {
|
|
|
|
mNotification->DispatchTrustedEvent(NS_LITERAL_STRING("click"));
|
|
|
|
} else if (!strcmp("alertfinished", aTopic)) {
|
|
|
|
mNotification->mIsClosed = true;
|
|
|
|
mNotification->DispatchTrustedEvent(NS_LITERAL_STRING("close"));
|
|
|
|
} else if (!strcmp("alertshow", aTopic)) {
|
|
|
|
mNotification->DispatchTrustedEvent(NS_LITERAL_STRING("show"));
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
Notification::Notification(const nsAString& aTitle, const nsAString& aBody,
|
|
|
|
NotificationDirection aDir, const nsAString& aLang,
|
|
|
|
const nsAString& aTag, const nsAString& aIconUrl)
|
|
|
|
: mTitle(aTitle), mBody(aBody), mDir(aDir), mLang(aLang),
|
|
|
|
mTag(aTag), mIconUrl(aIconUrl), mIsClosed(false)
|
|
|
|
{
|
|
|
|
SetIsDOMBinding();
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<Notification>
|
|
|
|
Notification::Constructor(const GlobalObject& aGlobal,
|
|
|
|
const nsAString& aTitle,
|
|
|
|
const NotificationOptions& aOptions,
|
|
|
|
ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
nsString tag;
|
|
|
|
if (aOptions.mTag.WasPassed()) {
|
|
|
|
tag.Append(NS_LITERAL_STRING("tag:"));
|
|
|
|
tag.Append(aOptions.mTag.Value());
|
|
|
|
} else {
|
|
|
|
tag.Append(NS_LITERAL_STRING("notag:"));
|
|
|
|
tag.AppendInt(sCount++);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsRefPtr<Notification> notification = new Notification(aTitle,
|
|
|
|
aOptions.mBody,
|
|
|
|
aOptions.mDir,
|
|
|
|
aOptions.mLang,
|
|
|
|
tag,
|
|
|
|
aOptions.mIcon);
|
|
|
|
|
2013-08-22 22:17:08 -07:00
|
|
|
nsCOMPtr<nsPIDOMWindow> window = do_QueryInterface(aGlobal.GetAsSupports());
|
2013-03-18 06:24:54 -07:00
|
|
|
MOZ_ASSERT(window, "Window should not be null.");
|
|
|
|
notification->BindToOwner(window);
|
|
|
|
|
|
|
|
// Queue a task to show the notification.
|
|
|
|
nsCOMPtr<nsIRunnable> showNotificationTask =
|
|
|
|
new NotificationTask(notification, NotificationTask::eShow);
|
|
|
|
NS_DispatchToMainThread(showNotificationTask);
|
|
|
|
|
|
|
|
return notification.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
Notification::ShowInternal()
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIAlertsService> alertService =
|
|
|
|
do_GetService(NS_ALERTSERVICE_CONTRACTID);
|
|
|
|
|
|
|
|
ErrorResult result;
|
|
|
|
if (GetPermissionInternal(GetOwner(), result) !=
|
2013-05-06 12:28:13 -07:00
|
|
|
NotificationPermission::Granted || !alertService) {
|
2013-03-18 06:24:54 -07:00
|
|
|
// We do not have permission to show a notification or alert service
|
|
|
|
// is not available.
|
|
|
|
return DispatchTrustedEvent(NS_LITERAL_STRING("error"));
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
nsAutoString absoluteUrl;
|
|
|
|
if (mIconUrl.Length() > 0) {
|
|
|
|
// Resolve image URL against document base URI.
|
|
|
|
nsIDocument* doc = GetOwner()->GetExtantDoc();
|
|
|
|
NS_ENSURE_TRUE(doc, NS_ERROR_UNEXPECTED);
|
|
|
|
nsCOMPtr<nsIURI> baseUri = doc->GetBaseURI();
|
|
|
|
NS_ENSURE_TRUE(baseUri, NS_ERROR_UNEXPECTED);
|
|
|
|
nsCOMPtr<nsIURI> srcUri;
|
|
|
|
rv = nsContentUtils::NewURIWithDocumentCharset(getter_AddRefs(srcUri),
|
|
|
|
mIconUrl, doc, baseUri);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
if (srcUri) {
|
|
|
|
nsAutoCString src;
|
|
|
|
srcUri->GetSpec(src);
|
|
|
|
absoluteUrl = NS_ConvertUTF8toUTF16(src);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-10 07:49:09 -07:00
|
|
|
nsCOMPtr<nsIObserver> observer = new NotificationObserver(this);
|
|
|
|
|
2013-03-18 06:24:54 -07:00
|
|
|
nsString alertName;
|
|
|
|
rv = GetAlertName(alertName);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2013-09-10 07:49:09 -07:00
|
|
|
#ifdef MOZ_B2G
|
|
|
|
nsCOMPtr<nsIAppNotificationService> appNotifier =
|
|
|
|
do_GetService("@mozilla.org/system-alerts-service;1");
|
|
|
|
if (appNotifier) {
|
|
|
|
nsCOMPtr<nsPIDOMWindow> window = GetOwner();
|
|
|
|
uint32_t appId = (window.get())->GetDoc()->NodePrincipal()->GetAppId();
|
|
|
|
|
|
|
|
if (appId != nsIScriptSecurityManager::UNKNOWN_APP_ID) {
|
|
|
|
nsCOMPtr<nsIAppsService> appsService = do_GetService("@mozilla.org/AppsService;1");
|
|
|
|
nsString manifestUrl = EmptyString();
|
|
|
|
appsService->GetManifestURLByLocalId(appId, manifestUrl);
|
|
|
|
return appNotifier->ShowAppNotification(mIconUrl, mTitle, mBody,
|
|
|
|
true,
|
|
|
|
manifestUrl,
|
|
|
|
observer,
|
|
|
|
alertName);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2013-03-18 06:24:54 -07:00
|
|
|
// In the case of IPC, the parent process uses the cookie to map to
|
|
|
|
// nsIObserver. Thus the cookie must be unique to differentiate observers.
|
|
|
|
nsString uniqueCookie = NS_LITERAL_STRING("notification:");
|
|
|
|
uniqueCookie.AppendInt(sCount++);
|
|
|
|
return alertService->ShowAlertNotification(absoluteUrl, mTitle, mBody, true,
|
|
|
|
uniqueCookie, observer, alertName,
|
|
|
|
DirectionToString(mDir), mLang);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Notification::RequestPermission(const GlobalObject& aGlobal,
|
|
|
|
const Optional<OwningNonNull<NotificationPermissionCallback> >& aCallback,
|
|
|
|
ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
// Get principal from global to make permission request for notifications.
|
2013-08-22 22:17:08 -07:00
|
|
|
nsCOMPtr<nsPIDOMWindow> window = do_QueryInterface(aGlobal.GetAsSupports());
|
|
|
|
nsCOMPtr<nsIScriptObjectPrincipal> sop = do_QueryInterface(aGlobal.GetAsSupports());
|
2013-03-18 06:24:54 -07:00
|
|
|
if (!sop) {
|
|
|
|
aRv.Throw(NS_ERROR_UNEXPECTED);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
nsCOMPtr<nsIPrincipal> principal = sop->GetPrincipal();
|
|
|
|
|
|
|
|
NotificationPermissionCallback* permissionCallback = nullptr;
|
|
|
|
if (aCallback.WasPassed()) {
|
2013-06-19 11:48:43 -07:00
|
|
|
permissionCallback = &aCallback.Value();
|
2013-03-18 06:24:54 -07:00
|
|
|
}
|
|
|
|
nsCOMPtr<nsIRunnable> request =
|
|
|
|
new NotificationPermissionRequest(principal, window, permissionCallback);
|
|
|
|
|
|
|
|
NS_DispatchToMainThread(request);
|
|
|
|
}
|
|
|
|
|
|
|
|
NotificationPermission
|
|
|
|
Notification::GetPermission(const GlobalObject& aGlobal, ErrorResult& aRv)
|
|
|
|
{
|
2013-08-22 22:17:08 -07:00
|
|
|
return GetPermissionInternal(aGlobal.GetAsSupports(), aRv);
|
2013-03-18 06:24:54 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NotificationPermission
|
|
|
|
Notification::GetPermissionInternal(nsISupports* aGlobal, ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
// Get principal from global to check permission for notifications.
|
|
|
|
nsCOMPtr<nsIScriptObjectPrincipal> sop = do_QueryInterface(aGlobal);
|
|
|
|
if (!sop) {
|
|
|
|
aRv.Throw(NS_ERROR_UNEXPECTED);
|
2013-05-06 12:28:13 -07:00
|
|
|
return NotificationPermission::Denied;
|
2013-03-18 06:24:54 -07:00
|
|
|
}
|
|
|
|
|
2013-05-23 15:38:00 -07:00
|
|
|
nsCOMPtr<nsIPrincipal> principal = sop->GetPrincipal();
|
|
|
|
if (nsContentUtils::IsSystemPrincipal(principal)) {
|
2013-05-06 12:28:13 -07:00
|
|
|
return NotificationPermission::Granted;
|
2013-05-23 15:38:00 -07:00
|
|
|
} else {
|
|
|
|
// Allow files to show notifications by default.
|
|
|
|
nsCOMPtr<nsIURI> uri;
|
|
|
|
principal->GetURI(getter_AddRefs(uri));
|
|
|
|
if (uri) {
|
|
|
|
bool isFile;
|
|
|
|
uri->SchemeIs("file", &isFile);
|
|
|
|
if (isFile) {
|
|
|
|
return NotificationPermission::Granted;
|
|
|
|
}
|
|
|
|
}
|
2012-07-12 08:45:38 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// We also allow notifications is they are pref'ed on.
|
|
|
|
if (Preferences::GetBool("notification.prompt.testing", false)) {
|
|
|
|
if (Preferences::GetBool("notification.prompt.testing.allow", true)) {
|
2013-05-06 12:28:13 -07:00
|
|
|
return NotificationPermission::Granted;
|
2012-07-12 08:45:38 -07:00
|
|
|
} else {
|
2013-05-06 12:28:13 -07:00
|
|
|
return NotificationPermission::Denied;
|
2012-07-12 08:45:38 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-18 06:24:54 -07:00
|
|
|
uint32_t permission = nsIPermissionManager::UNKNOWN_ACTION;
|
|
|
|
|
2013-08-15 13:15:07 -07:00
|
|
|
nsCOMPtr<nsIPermissionManager> permissionManager =
|
|
|
|
do_GetService(NS_PERMISSIONMANAGER_CONTRACTID);
|
2013-03-18 06:24:54 -07:00
|
|
|
|
2013-08-15 13:15:07 -07:00
|
|
|
permissionManager->TestPermissionFromPrincipal(principal,
|
|
|
|
"desktop-notification",
|
|
|
|
&permission);
|
2013-03-18 06:24:54 -07:00
|
|
|
|
|
|
|
// Convert the result to one of the enum types.
|
|
|
|
switch (permission) {
|
|
|
|
case nsIPermissionManager::ALLOW_ACTION:
|
2013-05-06 12:28:13 -07:00
|
|
|
return NotificationPermission::Granted;
|
2013-03-18 06:24:54 -07:00
|
|
|
case nsIPermissionManager::DENY_ACTION:
|
2013-05-06 12:28:13 -07:00
|
|
|
return NotificationPermission::Denied;
|
2013-03-18 06:24:54 -07:00
|
|
|
default:
|
2013-05-06 12:28:13 -07:00
|
|
|
return NotificationPermission::Default;
|
2013-03-18 06:24:54 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
Notification::PrefEnabled()
|
|
|
|
{
|
|
|
|
return Preferences::GetBool("dom.webnotifications.enabled", false);
|
|
|
|
}
|
|
|
|
|
|
|
|
JSObject*
|
2013-04-25 09:29:54 -07:00
|
|
|
Notification::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aScope)
|
2013-03-18 06:24:54 -07:00
|
|
|
{
|
|
|
|
return mozilla::dom::NotificationBinding::Wrap(aCx, aScope, this);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Notification::Close()
|
|
|
|
{
|
|
|
|
// Queue a task to close the notification.
|
|
|
|
nsCOMPtr<nsIRunnable> showNotificationTask =
|
|
|
|
new NotificationTask(this, NotificationTask::eClose);
|
|
|
|
NS_DispatchToMainThread(showNotificationTask);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
Notification::CloseInternal()
|
|
|
|
{
|
|
|
|
if (!mIsClosed) {
|
|
|
|
nsCOMPtr<nsIAlertsService> alertService =
|
|
|
|
do_GetService(NS_ALERTSERVICE_CONTRACTID);
|
|
|
|
|
|
|
|
if (alertService) {
|
|
|
|
nsString alertName;
|
|
|
|
nsresult rv = GetAlertName(alertName);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = alertService->CloseAlert(alertName);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
Notification::GetAlertName(nsString& aAlertName)
|
|
|
|
{
|
|
|
|
// Get the notification name that is unique per origin + tag.
|
|
|
|
// The name of the alert is of the form origin#tag
|
|
|
|
|
|
|
|
nsPIDOMWindow* owner = GetOwner();
|
|
|
|
NS_ENSURE_TRUE(owner, NS_ERROR_UNEXPECTED);
|
|
|
|
|
|
|
|
nsIDocument* doc = owner->GetExtantDoc();
|
|
|
|
NS_ENSURE_TRUE(doc, NS_ERROR_UNEXPECTED);
|
|
|
|
|
|
|
|
nsresult rv = nsContentUtils::GetUTFOrigin(doc->NodePrincipal(),
|
|
|
|
aAlertName);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
aAlertName.AppendLiteral("#");
|
|
|
|
aAlertName.Append(mTag);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|
|
|
|
|