2013-03-18 17:24:54 +04: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 "mozilla/dom/Notification.h"
|
2013-10-11 22:12:13 +04:00
|
|
|
#include "mozilla/dom/AppNotificationServiceOptionsBinding.h"
|
2014-08-21 04:56:12 +04:00
|
|
|
#include "mozilla/dom/BindingUtils.h"
|
2013-09-10 11:03:37 +04:00
|
|
|
#include "mozilla/dom/OwningNonNull.h"
|
2013-10-03 05:27:53 +04:00
|
|
|
#include "mozilla/dom/Promise.h"
|
2012-07-12 19:45:38 +04:00
|
|
|
#include "mozilla/Preferences.h"
|
2013-03-18 17:24:54 +04:00
|
|
|
#include "nsContentUtils.h"
|
|
|
|
#include "nsIAlertsService.h"
|
2013-10-03 05:27:53 +04:00
|
|
|
#include "nsIAppsService.h"
|
2013-03-18 17:24:54 +04:00
|
|
|
#include "nsIContentPermissionPrompt.h"
|
|
|
|
#include "nsIDocument.h"
|
2013-10-03 05:27:53 +04:00
|
|
|
#include "nsINotificationStorage.h"
|
2013-03-18 17:24:54 +04:00
|
|
|
#include "nsIPermissionManager.h"
|
2013-10-03 05:27:53 +04:00
|
|
|
#include "nsIUUIDGenerator.h"
|
2013-03-18 17:24:54 +04:00
|
|
|
#include "nsServiceManagerUtils.h"
|
2014-08-21 04:56:12 +04:00
|
|
|
#include "nsStructuredCloneContainer.h"
|
2013-03-18 17:24:54 +04:00
|
|
|
#include "nsToolkitCompsCID.h"
|
|
|
|
#include "nsGlobalWindow.h"
|
|
|
|
#include "nsDOMJSUtils.h"
|
2013-09-24 01:30:40 +04:00
|
|
|
#include "nsIScriptSecurityManager.h"
|
2014-08-21 04:56:12 +04:00
|
|
|
#include "nsIXPConnect.h"
|
2013-10-19 00:57:55 +04:00
|
|
|
#include "mozilla/dom/PermissionMessageUtils.h"
|
2014-10-15 18:48:04 +04:00
|
|
|
#include "mozilla/dom/Event.h"
|
2014-04-29 21:27:26 +04:00
|
|
|
#include "mozilla/Services.h"
|
2014-02-10 00:34:40 +04:00
|
|
|
#include "nsContentPermissionHelper.h"
|
2014-11-28 22:08:29 +03:00
|
|
|
#include "nsILoadContext.h"
|
2013-09-10 18:49:09 +04:00
|
|
|
#ifdef MOZ_B2G
|
|
|
|
#include "nsIDOMDesktopNotification.h"
|
|
|
|
#endif
|
2013-03-18 17:24:54 +04:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
class NotificationStorageCallback final : public nsINotificationStorageCallback
|
2013-10-03 05:27:53 +04:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
NS_DECL_CYCLE_COLLECTING_ISUPPORTS
|
|
|
|
NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(NotificationStorageCallback)
|
|
|
|
|
|
|
|
NotificationStorageCallback(const GlobalObject& aGlobal, nsPIDOMWindow* aWindow, Promise* aPromise)
|
|
|
|
: mCount(0),
|
|
|
|
mGlobal(aGlobal.Get()),
|
|
|
|
mWindow(aWindow),
|
|
|
|
mPromise(aPromise)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aWindow);
|
|
|
|
MOZ_ASSERT(aPromise);
|
2014-06-16 22:08:00 +04:00
|
|
|
JSContext* cx = aGlobal.Context();
|
2013-10-03 05:27:53 +04:00
|
|
|
JSAutoCompartment ac(cx, mGlobal);
|
2014-02-12 14:50:46 +04:00
|
|
|
mNotifications = JS_NewArrayObject(cx, 0);
|
2013-10-03 05:27:53 +04:00
|
|
|
HoldData();
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHOD Handle(const nsAString& aID,
|
|
|
|
const nsAString& aTitle,
|
|
|
|
const nsAString& aDir,
|
|
|
|
const nsAString& aLang,
|
|
|
|
const nsAString& aBody,
|
|
|
|
const nsAString& aTag,
|
|
|
|
const nsAString& aIcon,
|
2014-08-21 04:56:12 +04:00
|
|
|
const nsAString& aData,
|
2014-09-17 01:12:00 +04:00
|
|
|
const nsAString& aBehavior,
|
2015-03-21 19:28:04 +03:00
|
|
|
JSContext* aCx) override
|
2013-10-03 05:27:53 +04:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(!aID.IsEmpty());
|
|
|
|
|
2014-08-21 04:56:12 +04:00
|
|
|
RootedDictionary<NotificationOptions> options(aCx);
|
2013-10-03 05:27:53 +04:00
|
|
|
options.mDir = Notification::StringToDirection(nsString(aDir));
|
|
|
|
options.mLang = aLang;
|
|
|
|
options.mBody = aBody;
|
|
|
|
options.mTag = aTag;
|
|
|
|
options.mIcon = aIcon;
|
2014-09-17 01:12:00 +04:00
|
|
|
options.mMozbehavior.Init(aBehavior);
|
|
|
|
nsRefPtr<Notification> notification;
|
|
|
|
notification = Notification::CreateInternal(mWindow,
|
|
|
|
aID,
|
|
|
|
aTitle,
|
|
|
|
options);
|
2014-08-21 04:56:12 +04:00
|
|
|
ErrorResult rv;
|
|
|
|
notification->InitFromBase64(aCx, aData, rv);
|
|
|
|
if (rv.Failed()) {
|
2015-04-27 16:18:51 +03:00
|
|
|
return rv.StealNSResult();
|
2014-08-21 04:56:12 +04:00
|
|
|
}
|
|
|
|
|
2014-09-12 18:50:00 +04:00
|
|
|
notification->SetStoredState(true);
|
|
|
|
|
2013-10-03 05:27:53 +04:00
|
|
|
JSAutoCompartment ac(aCx, mGlobal);
|
Bug 1117172 part 3. Change the wrappercached WrapObject methods to allow passing in aGivenProto. r=peterv
The only manual changes here are to BindingUtils.h, BindingUtils.cpp,
Codegen.py, Element.cpp, IDBFileRequest.cpp, IDBObjectStore.cpp,
dom/workers/Navigator.cpp, WorkerPrivate.cpp, DeviceStorageRequestChild.cpp,
Notification.cpp, nsGlobalWindow.cpp, MessagePort.cpp, nsJSEnvironment.cpp,
Sandbox.cpp, XPCConvert.cpp, ExportHelpers.cpp, and DataStoreService.cpp. The
rest of this diff was generated by running the following commands:
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObjectInternal\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObjectInternal\((?:aCx|cx|aContext|aCtx|js))\)/\1, aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapNode\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapNode\((?:aCx|cx|aContext|aCtx|js))\)/\1, aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObject\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(Binding(?:_workers)?::Wrap\((?:aCx|cx|aContext|aCtx|js), [^,)]+)\)/\1, aGivenProto)/g'
2015-03-19 17:13:33 +03:00
|
|
|
JS::Rooted<JSObject*> element(aCx, notification->WrapObject(aCx, JS::NullPtr()));
|
2013-10-03 05:27:53 +04:00
|
|
|
NS_ENSURE_TRUE(element, NS_ERROR_FAILURE);
|
|
|
|
|
2014-04-30 13:10:33 +04:00
|
|
|
JS::Rooted<JSObject*> notifications(aCx, mNotifications);
|
|
|
|
if (!JS_DefineElement(aCx, notifications, mCount++, element, 0)) {
|
2013-10-03 05:27:53 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
NS_IMETHOD Done(JSContext* aCx) override
|
2013-10-03 05:27:53 +04:00
|
|
|
{
|
|
|
|
JSAutoCompartment ac(aCx, mGlobal);
|
2013-11-19 22:39:51 +04:00
|
|
|
JS::Rooted<JS::Value> result(aCx, JS::ObjectValue(*mNotifications));
|
2013-10-03 05:27:53 +04:00
|
|
|
mPromise->MaybeResolve(aCx, result);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2014-08-01 11:22:20 +04:00
|
|
|
virtual ~NotificationStorageCallback()
|
2013-10-03 05:27:53 +04:00
|
|
|
{
|
|
|
|
DropData();
|
|
|
|
}
|
|
|
|
|
|
|
|
void HoldData()
|
|
|
|
{
|
|
|
|
mozilla::HoldJSObjects(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DropData()
|
|
|
|
{
|
|
|
|
mGlobal = nullptr;
|
|
|
|
mNotifications = nullptr;
|
|
|
|
mozilla::DropJSObjects(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t mCount;
|
|
|
|
JS::Heap<JSObject *> mGlobal;
|
|
|
|
nsCOMPtr<nsPIDOMWindow> mWindow;
|
|
|
|
nsRefPtr<Promise> mPromise;
|
|
|
|
JS::Heap<JSObject *> mNotifications;
|
|
|
|
};
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_ADDREF(NotificationStorageCallback)
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_RELEASE(NotificationStorageCallback)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_CLASS(NotificationStorageCallback)
|
|
|
|
|
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(NotificationStorageCallback)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsINotificationStorageCallback)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsISupports)
|
|
|
|
NS_INTERFACE_MAP_END
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN(NotificationStorageCallback)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_JS_MEMBER_CALLBACK(mGlobal)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_JS_MEMBER_CALLBACK(mNotifications)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_END
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(NotificationStorageCallback)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_SCRIPT_OBJECTS
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mWindow)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mPromise)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(NotificationStorageCallback)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mWindow)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mPromise)
|
|
|
|
tmp->DropData();
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
|
|
|
|
|
2013-03-18 17:24:54 +04:00
|
|
|
class NotificationPermissionRequest : public nsIContentPermissionRequest,
|
|
|
|
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 23:28:13 +04:00
|
|
|
mPermission(NotificationPermission::Default),
|
2015-04-14 04:08:00 +03:00
|
|
|
mCallback(aCallback)
|
|
|
|
{
|
|
|
|
mRequester = new nsContentPermissionRequester(mWindow);
|
|
|
|
}
|
2013-03-18 17:24:54 +04:00
|
|
|
|
|
|
|
protected:
|
2014-06-23 23:56:07 +04:00
|
|
|
virtual ~NotificationPermissionRequest() {}
|
|
|
|
|
2013-03-18 17:24:54 +04:00
|
|
|
nsresult CallCallback();
|
2012-07-12 19:45:38 +04:00
|
|
|
nsresult DispatchCallback();
|
2013-03-18 17:24:54 +04:00
|
|
|
nsCOMPtr<nsIPrincipal> mPrincipal;
|
|
|
|
nsCOMPtr<nsPIDOMWindow> mWindow;
|
|
|
|
NotificationPermission mPermission;
|
|
|
|
nsRefPtr<NotificationPermissionCallback> mCallback;
|
2015-04-14 04:08:00 +03:00
|
|
|
nsCOMPtr<nsIContentPermissionRequester> mRequester;
|
2013-03-18 17:24:54 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
class NotificationObserver : public nsIObserver
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
NS_DECL_NSIOBSERVER
|
|
|
|
|
2014-09-02 02:26:43 +04:00
|
|
|
explicit NotificationObserver(Notification* aNotification)
|
2013-03-18 17:24:54 +04:00
|
|
|
: mNotification(aNotification) {}
|
|
|
|
|
2014-06-23 23:56:07 +04:00
|
|
|
protected:
|
2013-03-18 17:24:54 +04:00
|
|
|
virtual ~NotificationObserver() {}
|
|
|
|
|
|
|
|
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) {}
|
|
|
|
|
2014-06-23 23:56:07 +04:00
|
|
|
protected:
|
2013-03-18 17:24:54 +04:00
|
|
|
virtual ~NotificationTask() {}
|
|
|
|
|
|
|
|
nsRefPtr<Notification> mNotification;
|
|
|
|
NotificationAction mAction;
|
|
|
|
};
|
|
|
|
|
|
|
|
uint32_t Notification::sCount = 0;
|
|
|
|
|
2014-04-25 20:49:00 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION(NotificationPermissionRequest, mWindow)
|
2013-03-18 17:24:54 +04:00
|
|
|
|
|
|
|
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-24 02:38:00 +04:00
|
|
|
if (nsContentUtils::IsSystemPrincipal(mPrincipal)) {
|
2013-05-06 23:28:13 +04:00
|
|
|
mPermission = NotificationPermission::Granted;
|
2013-05-24 02:38:00 +04: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 19:45:38 +04: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 23:28:13 +04:00
|
|
|
mPermission = NotificationPermission::Granted;
|
2012-07-12 19:45:38 +04:00
|
|
|
} else {
|
2013-05-06 23:28:13 +04:00
|
|
|
mPermission = NotificationPermission::Denied;
|
2012-07-12 19:45:38 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-06 23:28:13 +04:00
|
|
|
if (mPermission != NotificationPermission::Default) {
|
2012-07-12 19:45:38 +04:00
|
|
|
return DispatchCallback();
|
|
|
|
}
|
|
|
|
|
2014-08-01 11:22:20 +04:00
|
|
|
return nsContentPermissionUtils::AskPermission(this, mWindow);
|
2013-03-18 17:24:54 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
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 19:40:16 +04:00
|
|
|
NS_ENSURE_ARG_POINTER(aElement);
|
|
|
|
*aElement = nullptr;
|
|
|
|
return NS_OK;
|
2013-03-18 17:24:54 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
NotificationPermissionRequest::Cancel()
|
|
|
|
{
|
2013-05-06 23:28:13 +04:00
|
|
|
mPermission = NotificationPermission::Denied;
|
2012-07-12 19:45:38 +04:00
|
|
|
return DispatchCallback();
|
2013-03-18 17:24:54 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2013-09-10 19:41:59 +04:00
|
|
|
NotificationPermissionRequest::Allow(JS::HandleValue aChoices)
|
2013-03-18 17:24:54 +04:00
|
|
|
{
|
2013-09-10 19:41:59 +04:00
|
|
|
MOZ_ASSERT(aChoices.isUndefined());
|
|
|
|
|
2013-05-06 23:28:13 +04:00
|
|
|
mPermission = NotificationPermission::Granted;
|
2012-07-12 19:45:38 +04:00
|
|
|
return DispatchCallback();
|
|
|
|
}
|
|
|
|
|
2015-04-14 04:08:00 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
NotificationPermissionRequest::GetRequester(nsIContentPermissionRequester** aRequester)
|
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(aRequester);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIContentPermissionRequester> requester = mRequester;
|
|
|
|
requester.forget(aRequester);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-07-12 19:45:38 +04:00
|
|
|
inline nsresult
|
|
|
|
NotificationPermissionRequest::DispatchCallback()
|
|
|
|
{
|
|
|
|
if (!mCallback) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-03-18 17:24:54 +04:00
|
|
|
nsCOMPtr<nsIRunnable> callbackRunnable = NS_NewRunnableMethod(this,
|
|
|
|
&NotificationPermissionRequest::CallCallback);
|
2012-07-12 19:45:38 +04:00
|
|
|
return NS_DispatchToMainThread(callbackRunnable);
|
2013-03-18 17:24:54 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
NotificationPermissionRequest::CallCallback()
|
|
|
|
{
|
|
|
|
ErrorResult rv;
|
|
|
|
mCallback->Call(mPermission, rv);
|
2015-04-27 16:18:51 +03:00
|
|
|
return rv.StealNSResult();
|
2013-03-18 17:24:54 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-02-10 00:34:40 +04:00
|
|
|
NotificationPermissionRequest::GetTypes(nsIArray** aTypes)
|
2013-03-18 17:24:54 +04:00
|
|
|
{
|
2013-09-10 19:41:59 +04:00
|
|
|
nsTArray<nsString> emptyOptions;
|
2014-08-01 11:22:20 +04:00
|
|
|
return nsContentPermissionUtils::CreatePermissionArray(NS_LITERAL_CSTRING("desktop-notification"),
|
2015-01-16 22:58:52 +03:00
|
|
|
NS_LITERAL_CSTRING("unused"),
|
|
|
|
emptyOptions,
|
|
|
|
aTypes);
|
2013-03-18 17:24:54 +04:00
|
|
|
}
|
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS(NotificationTask, nsIRunnable)
|
2013-03-18 17:24:54 +04:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
NotificationTask::Run()
|
|
|
|
{
|
|
|
|
switch (mAction) {
|
|
|
|
case eShow:
|
2013-10-03 05:27:53 +04:00
|
|
|
mNotification->ShowInternal();
|
|
|
|
break;
|
2013-03-18 17:24:54 +04:00
|
|
|
case eClose:
|
2013-10-03 05:27:53 +04:00
|
|
|
mNotification->CloseInternal();
|
|
|
|
break;
|
2013-03-18 17:24:54 +04:00
|
|
|
default:
|
2013-06-29 05:38:30 +04:00
|
|
|
MOZ_CRASH("Unexpected action for NotificationTask.");
|
2013-03-18 17:24:54 +04:00
|
|
|
}
|
2013-10-03 05:27:53 +04:00
|
|
|
return NS_OK;
|
2013-03-18 17:24:54 +04:00
|
|
|
}
|
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS(NotificationObserver, nsIObserver)
|
2013-03-18 17:24:54 +04:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
NotificationObserver::Observe(nsISupports* aSubject, const char* aTopic,
|
2014-01-04 19:02:17 +04:00
|
|
|
const char16_t* aData)
|
2013-03-18 17:24:54 +04:00
|
|
|
{
|
2014-06-04 14:06:00 +04:00
|
|
|
nsCOMPtr<nsPIDOMWindow> window = mNotification->GetOwner();
|
2014-10-15 18:48:04 +04:00
|
|
|
if (!window || !window->IsCurrentInnerWindow()) {
|
2014-06-04 14:06:00 +04:00
|
|
|
// Window has been closed, this observer is not valid anymore
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2013-03-18 17:24:54 +04:00
|
|
|
if (!strcmp("alertclickcallback", aTopic)) {
|
2014-10-15 18:48:04 +04:00
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMEvent> event;
|
|
|
|
NS_NewDOMEvent(getter_AddRefs(event), mNotification, nullptr, nullptr);
|
|
|
|
nsresult rv = event->InitEvent(NS_LITERAL_STRING("click"), false, true);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
event->SetTrusted(true);
|
|
|
|
WantsPopupControlCheck popupControlCheck(event);
|
|
|
|
bool doDefaultAction = true;
|
|
|
|
mNotification->DispatchEvent(event, &doDefaultAction);
|
|
|
|
if (doDefaultAction) {
|
|
|
|
nsIDocument* doc = window ? window->GetExtantDoc() : nullptr;
|
|
|
|
if (doc) {
|
|
|
|
// Browser UI may use DOMWebNotificationClicked to focus the tab
|
|
|
|
// from which the event was dispatched.
|
|
|
|
nsContentUtils::DispatchChromeEvent(doc, window->GetOuterWindow(),
|
|
|
|
NS_LITERAL_STRING("DOMWebNotificationClicked"),
|
|
|
|
true, true);
|
|
|
|
}
|
2014-01-13 16:56:28 +04:00
|
|
|
}
|
2013-03-18 17:24:54 +04:00
|
|
|
} else if (!strcmp("alertfinished", aTopic)) {
|
2014-09-12 18:50:00 +04:00
|
|
|
nsCOMPtr<nsINotificationStorage> notificationStorage =
|
|
|
|
do_GetService(NS_NOTIFICATION_STORAGE_CONTRACTID);
|
|
|
|
if (notificationStorage && mNotification->IsStored()) {
|
|
|
|
nsString origin;
|
|
|
|
nsresult rv = Notification::GetOrigin(mNotification->GetOwner(), origin);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
nsString id;
|
|
|
|
mNotification->GetID(id);
|
|
|
|
notificationStorage->Delete(origin, id);
|
|
|
|
}
|
|
|
|
mNotification->SetStoredState(false);
|
|
|
|
}
|
2013-03-18 17:24:54 +04:00
|
|
|
mNotification->mIsClosed = true;
|
|
|
|
mNotification->DispatchTrustedEvent(NS_LITERAL_STRING("close"));
|
|
|
|
} else if (!strcmp("alertshow", aTopic)) {
|
|
|
|
mNotification->DispatchTrustedEvent(NS_LITERAL_STRING("show"));
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-10-03 05:27:53 +04:00
|
|
|
Notification::Notification(const nsAString& aID, const nsAString& aTitle, const nsAString& aBody,
|
2013-03-18 17:24:54 +04:00
|
|
|
NotificationDirection aDir, const nsAString& aLang,
|
2014-01-07 06:53:23 +04:00
|
|
|
const nsAString& aTag, const nsAString& aIconUrl,
|
2014-09-17 01:12:00 +04:00
|
|
|
const NotificationBehavior& aBehavior, nsPIDOMWindow* aWindow)
|
2014-04-01 10:13:50 +04:00
|
|
|
: DOMEventTargetHelper(aWindow),
|
2014-01-07 06:53:23 +04:00
|
|
|
mID(aID), mTitle(aTitle), mBody(aBody), mDir(aDir), mLang(aLang),
|
2014-09-17 01:12:00 +04:00
|
|
|
mTag(aTag), mIconUrl(aIconUrl), mBehavior(aBehavior), mIsClosed(false), mIsStored(false)
|
2013-03-18 17:24:54 +04:00
|
|
|
{
|
2014-05-09 01:16:00 +04:00
|
|
|
nsAutoString alertName;
|
|
|
|
DebugOnly<nsresult> rv = GetOrigin(GetOwner(), alertName);
|
|
|
|
MOZ_ASSERT(NS_SUCCEEDED(rv), "GetOrigin should not have failed");
|
|
|
|
|
|
|
|
// Get the notification name that is unique per origin + tag/ID.
|
|
|
|
// The name of the alert is of the form origin#tag/ID.
|
2014-05-22 07:48:51 +04:00
|
|
|
alertName.Append('#');
|
2014-05-09 01:16:00 +04:00
|
|
|
if (!mTag.IsEmpty()) {
|
2014-05-22 07:48:50 +04:00
|
|
|
alertName.AppendLiteral("tag:");
|
2014-05-09 01:16:00 +04:00
|
|
|
alertName.Append(mTag);
|
|
|
|
} else {
|
2014-05-22 07:48:50 +04:00
|
|
|
alertName.AppendLiteral("notag:");
|
2014-05-09 01:16:00 +04:00
|
|
|
alertName.Append(mID);
|
|
|
|
}
|
|
|
|
|
|
|
|
mAlertName = alertName;
|
2013-03-18 17:24:54 +04:00
|
|
|
}
|
|
|
|
|
2013-10-03 05:27:53 +04:00
|
|
|
// static
|
2013-03-18 17:24:54 +04:00
|
|
|
already_AddRefed<Notification>
|
|
|
|
Notification::Constructor(const GlobalObject& aGlobal,
|
|
|
|
const nsAString& aTitle,
|
|
|
|
const NotificationOptions& aOptions,
|
|
|
|
ErrorResult& aRv)
|
|
|
|
{
|
2013-10-03 05:27:53 +04:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
nsCOMPtr<nsPIDOMWindow> window = do_QueryInterface(aGlobal.GetAsSupports());
|
|
|
|
MOZ_ASSERT(window, "Window should not be null.");
|
2014-08-21 04:56:12 +04:00
|
|
|
|
2013-10-03 05:27:53 +04:00
|
|
|
nsRefPtr<Notification> notification = CreateInternal(window,
|
|
|
|
EmptyString(),
|
|
|
|
aTitle,
|
|
|
|
aOptions);
|
|
|
|
|
2014-08-21 04:56:12 +04:00
|
|
|
// Make a structured clone of the aOptions.mData object
|
|
|
|
JS::Rooted<JS::Value> data(aGlobal.Context(), aOptions.mData);
|
|
|
|
notification->InitFromJSVal(aGlobal.Context(), data, aRv);
|
|
|
|
if (aRv.Failed()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2013-10-03 05:27:53 +04:00
|
|
|
// Queue a task to show the notification.
|
|
|
|
nsCOMPtr<nsIRunnable> showNotificationTask =
|
|
|
|
new NotificationTask(notification, NotificationTask::eShow);
|
|
|
|
NS_DispatchToCurrentThread(showNotificationTask);
|
|
|
|
|
|
|
|
// Persist the notification.
|
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsINotificationStorage> notificationStorage =
|
|
|
|
do_GetService(NS_NOTIFICATION_STORAGE_CONTRACTID, &rv);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
aRv.Throw(rv);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsString origin;
|
|
|
|
aRv = GetOrigin(window, origin);
|
|
|
|
if (aRv.Failed()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsString id;
|
|
|
|
notification->GetID(id);
|
2014-05-09 01:16:00 +04:00
|
|
|
|
|
|
|
nsString alertName;
|
|
|
|
notification->GetAlertName(alertName);
|
|
|
|
|
2014-08-21 04:56:12 +04:00
|
|
|
nsString dataString;
|
|
|
|
nsCOMPtr<nsIStructuredCloneContainer> scContainer;
|
|
|
|
scContainer = notification->GetDataCloneContainer();
|
|
|
|
if (scContainer) {
|
|
|
|
scContainer->GetDataAsBase64(dataString);
|
|
|
|
}
|
|
|
|
|
2014-09-17 01:12:00 +04:00
|
|
|
nsAutoString behavior;
|
|
|
|
if (!aOptions.mMozbehavior.ToJSON(behavior)) {
|
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2013-10-03 05:27:53 +04:00
|
|
|
aRv = notificationStorage->Put(origin,
|
|
|
|
id,
|
|
|
|
aTitle,
|
|
|
|
DirectionToString(aOptions.mDir),
|
|
|
|
aOptions.mLang,
|
|
|
|
aOptions.mBody,
|
|
|
|
aOptions.mTag,
|
2014-05-09 01:16:00 +04:00
|
|
|
aOptions.mIcon,
|
2014-08-21 04:56:12 +04:00
|
|
|
alertName,
|
2014-09-17 01:12:00 +04:00
|
|
|
dataString,
|
|
|
|
behavior);
|
2014-08-21 04:56:12 +04:00
|
|
|
|
2013-10-03 05:27:53 +04:00
|
|
|
if (aRv.Failed()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2014-09-12 18:50:00 +04:00
|
|
|
notification->SetStoredState(true);
|
|
|
|
|
2013-10-03 05:27:53 +04:00
|
|
|
return notification.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<Notification>
|
|
|
|
Notification::CreateInternal(nsPIDOMWindow* aWindow,
|
|
|
|
const nsAString& aID,
|
|
|
|
const nsAString& aTitle,
|
|
|
|
const NotificationOptions& aOptions)
|
|
|
|
{
|
|
|
|
nsString id;
|
|
|
|
if (!aID.IsEmpty()) {
|
|
|
|
id = aID;
|
2013-03-18 17:24:54 +04:00
|
|
|
} else {
|
2013-10-03 05:27:53 +04:00
|
|
|
nsCOMPtr<nsIUUIDGenerator> uuidgen =
|
|
|
|
do_GetService("@mozilla.org/uuid-generator;1");
|
|
|
|
NS_ENSURE_TRUE(uuidgen, nullptr);
|
|
|
|
nsID uuid;
|
|
|
|
nsresult rv = uuidgen->GenerateUUIDInPlace(&uuid);
|
|
|
|
NS_ENSURE_SUCCESS(rv, nullptr);
|
|
|
|
|
|
|
|
char buffer[NSID_LENGTH];
|
|
|
|
uuid.ToProvidedString(buffer);
|
|
|
|
NS_ConvertASCIItoUTF16 convertedID(buffer);
|
|
|
|
id = convertedID;
|
2013-03-18 17:24:54 +04:00
|
|
|
}
|
|
|
|
|
2013-10-03 05:27:53 +04:00
|
|
|
nsRefPtr<Notification> notification = new Notification(id,
|
|
|
|
aTitle,
|
2013-03-18 17:24:54 +04:00
|
|
|
aOptions.mBody,
|
|
|
|
aOptions.mDir,
|
|
|
|
aOptions.mLang,
|
2013-10-03 05:27:53 +04:00
|
|
|
aOptions.mTag,
|
2014-01-07 06:53:23 +04:00
|
|
|
aOptions.mIcon,
|
2014-09-17 01:12:00 +04:00
|
|
|
aOptions.mMozbehavior,
|
2014-08-21 04:56:12 +04:00
|
|
|
aWindow);
|
2013-03-18 17:24:54 +04:00
|
|
|
return notification.forget();
|
|
|
|
}
|
|
|
|
|
2014-08-21 04:56:12 +04:00
|
|
|
Notification::~Notification() {}
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_CLASS(Notification)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(Notification, DOMEventTargetHelper)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mData)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mDataObjectContainer)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(Notification, DOMEventTargetHelper)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mData)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mDataObjectContainer)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
|
|
|
|
|
|
|
|
NS_IMPL_ADDREF_INHERITED(Notification, DOMEventTargetHelper)
|
|
|
|
NS_IMPL_RELEASE_INHERITED(Notification, DOMEventTargetHelper)
|
|
|
|
|
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(Notification)
|
|
|
|
NS_INTERFACE_MAP_END_INHERITING(DOMEventTargetHelper)
|
|
|
|
|
2013-11-12 03:56:21 +04:00
|
|
|
nsIPrincipal*
|
|
|
|
Notification::GetPrincipal()
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIScriptObjectPrincipal> sop = do_QueryInterface(GetOwner());
|
|
|
|
NS_ENSURE_TRUE(sop, nullptr);
|
|
|
|
return sop->GetPrincipal();
|
|
|
|
}
|
|
|
|
|
2013-10-03 05:27:53 +04:00
|
|
|
void
|
2013-03-18 17:24:54 +04:00
|
|
|
Notification::ShowInternal()
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIAlertsService> alertService =
|
|
|
|
do_GetService(NS_ALERTSERVICE_CONTRACTID);
|
|
|
|
|
|
|
|
ErrorResult result;
|
|
|
|
if (GetPermissionInternal(GetOwner(), result) !=
|
2013-05-06 23:28:13 +04:00
|
|
|
NotificationPermission::Granted || !alertService) {
|
2013-03-18 17:24:54 +04:00
|
|
|
// We do not have permission to show a notification or alert service
|
|
|
|
// is not available.
|
2013-10-03 05:27:53 +04:00
|
|
|
DispatchTrustedEvent(NS_LITERAL_STRING("error"));
|
|
|
|
return;
|
2013-03-18 17:24:54 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
nsAutoString absoluteUrl;
|
2014-09-17 01:12:00 +04:00
|
|
|
nsAutoString soundUrl;
|
|
|
|
// Resolve image URL against document base URI.
|
|
|
|
nsIDocument* doc = GetOwner()->GetExtantDoc();
|
|
|
|
if (doc) {
|
|
|
|
nsCOMPtr<nsIURI> baseUri = doc->GetBaseURI();
|
|
|
|
if (baseUri) {
|
|
|
|
if (mIconUrl.Length() > 0) {
|
2013-10-03 05:27:53 +04:00
|
|
|
nsCOMPtr<nsIURI> srcUri;
|
|
|
|
rv = nsContentUtils::NewURIWithDocumentCharset(getter_AddRefs(srcUri),
|
|
|
|
mIconUrl, doc, baseUri);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
nsAutoCString src;
|
|
|
|
srcUri->GetSpec(src);
|
|
|
|
absoluteUrl = NS_ConvertUTF8toUTF16(src);
|
|
|
|
}
|
|
|
|
}
|
2014-09-17 01:12:00 +04:00
|
|
|
if (mBehavior.mSoundFile.Length() > 0) {
|
|
|
|
nsCOMPtr<nsIURI> srcUri;
|
|
|
|
rv = nsContentUtils::NewURIWithDocumentCharset(getter_AddRefs(srcUri),
|
|
|
|
mBehavior.mSoundFile, doc, baseUri);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
nsAutoCString src;
|
|
|
|
srcUri->GetSpec(src);
|
|
|
|
soundUrl = NS_ConvertUTF8toUTF16(src);
|
|
|
|
}
|
|
|
|
}
|
2013-03-18 17:24:54 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-10 18:49:09 +04:00
|
|
|
nsCOMPtr<nsIObserver> observer = new NotificationObserver(this);
|
|
|
|
|
2014-08-21 04:56:12 +04:00
|
|
|
// mDataObjectContainer might be uninitialized here because the notification
|
|
|
|
// was constructed with an undefined data property.
|
|
|
|
nsString dataStr;
|
|
|
|
if (mDataObjectContainer) {
|
|
|
|
mDataObjectContainer->GetDataAsBase64(dataStr);
|
|
|
|
}
|
|
|
|
|
2013-09-10 18:49:09 +04: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();
|
2013-10-03 05:27:53 +04:00
|
|
|
rv = appsService->GetManifestURLByLocalId(appId, manifestUrl);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
mozilla::AutoSafeJSContext cx;
|
2013-11-11 12:04:41 +04:00
|
|
|
JS::Rooted<JS::Value> val(cx);
|
2013-10-03 05:27:53 +04:00
|
|
|
AppNotificationServiceOptions ops;
|
|
|
|
ops.mTextClickable = true;
|
|
|
|
ops.mManifestURL = manifestUrl;
|
2014-05-09 01:16:00 +04:00
|
|
|
ops.mId = mAlertName;
|
2014-03-18 20:01:13 +04:00
|
|
|
ops.mDbId = mID;
|
2013-10-03 05:27:53 +04:00
|
|
|
ops.mDir = DirectionToString(mDir);
|
|
|
|
ops.mLang = mLang;
|
2014-01-09 19:55:44 +04:00
|
|
|
ops.mTag = mTag;
|
2014-08-21 04:56:12 +04:00
|
|
|
ops.mData = dataStr;
|
2014-09-17 01:12:00 +04:00
|
|
|
ops.mMozbehavior = mBehavior;
|
|
|
|
ops.mMozbehavior.mSoundFile = soundUrl;
|
2013-10-03 05:27:53 +04:00
|
|
|
|
2014-06-11 23:38:55 +04:00
|
|
|
if (!ToJSValue(cx, ops, &val)) {
|
2013-10-03 05:27:53 +04:00
|
|
|
NS_WARNING("Converting dict to object failed!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-09-18 18:59:00 +04:00
|
|
|
appNotifier->ShowAppNotification(absoluteUrl, mTitle, mBody,
|
2013-10-03 05:27:53 +04:00
|
|
|
observer, val);
|
|
|
|
return;
|
2013-10-11 22:12:13 +04:00
|
|
|
}
|
2013-09-10 18:49:09 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2013-03-18 17:24:54 +04: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++);
|
2014-11-28 22:08:29 +03:00
|
|
|
bool inPrivateBrowsing = false;
|
|
|
|
if (doc) {
|
|
|
|
nsCOMPtr<nsILoadContext> loadContext = doc->GetLoadContext();
|
|
|
|
inPrivateBrowsing = loadContext && loadContext->UsePrivateBrowsing();
|
|
|
|
}
|
2013-10-03 05:27:53 +04:00
|
|
|
alertService->ShowAlertNotification(absoluteUrl, mTitle, mBody, true,
|
2014-05-09 01:16:00 +04:00
|
|
|
uniqueCookie, observer, mAlertName,
|
2013-11-12 03:56:21 +04:00
|
|
|
DirectionToString(mDir), mLang,
|
2014-11-28 22:08:29 +03:00
|
|
|
dataStr, GetPrincipal(),
|
|
|
|
inPrivateBrowsing);
|
2013-03-18 17:24:54 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
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-23 09:17:08 +04:00
|
|
|
nsCOMPtr<nsPIDOMWindow> window = do_QueryInterface(aGlobal.GetAsSupports());
|
|
|
|
nsCOMPtr<nsIScriptObjectPrincipal> sop = do_QueryInterface(aGlobal.GetAsSupports());
|
2013-03-18 17:24:54 +04:00
|
|
|
if (!sop) {
|
|
|
|
aRv.Throw(NS_ERROR_UNEXPECTED);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
nsCOMPtr<nsIPrincipal> principal = sop->GetPrincipal();
|
|
|
|
|
|
|
|
NotificationPermissionCallback* permissionCallback = nullptr;
|
|
|
|
if (aCallback.WasPassed()) {
|
2013-06-19 22:48:43 +04:00
|
|
|
permissionCallback = &aCallback.Value();
|
2013-03-18 17:24:54 +04:00
|
|
|
}
|
|
|
|
nsCOMPtr<nsIRunnable> request =
|
|
|
|
new NotificationPermissionRequest(principal, window, permissionCallback);
|
|
|
|
|
|
|
|
NS_DispatchToMainThread(request);
|
|
|
|
}
|
|
|
|
|
|
|
|
NotificationPermission
|
|
|
|
Notification::GetPermission(const GlobalObject& aGlobal, ErrorResult& aRv)
|
|
|
|
{
|
2013-08-23 09:17:08 +04:00
|
|
|
return GetPermissionInternal(aGlobal.GetAsSupports(), aRv);
|
2013-03-18 17:24:54 +04: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 23:28:13 +04:00
|
|
|
return NotificationPermission::Denied;
|
2013-03-18 17:24:54 +04:00
|
|
|
}
|
|
|
|
|
2013-05-24 02:38:00 +04:00
|
|
|
nsCOMPtr<nsIPrincipal> principal = sop->GetPrincipal();
|
|
|
|
if (nsContentUtils::IsSystemPrincipal(principal)) {
|
2013-05-06 23:28:13 +04:00
|
|
|
return NotificationPermission::Granted;
|
2013-05-24 02:38:00 +04: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 19:45:38 +04: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 23:28:13 +04:00
|
|
|
return NotificationPermission::Granted;
|
2012-07-12 19:45:38 +04:00
|
|
|
} else {
|
2013-05-06 23:28:13 +04:00
|
|
|
return NotificationPermission::Denied;
|
2012-07-12 19:45:38 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-18 17:24:54 +04:00
|
|
|
uint32_t permission = nsIPermissionManager::UNKNOWN_ACTION;
|
|
|
|
|
2013-08-16 00:15:07 +04:00
|
|
|
nsCOMPtr<nsIPermissionManager> permissionManager =
|
2014-04-29 21:27:26 +04:00
|
|
|
services::GetPermissionManager();
|
2013-03-18 17:24:54 +04:00
|
|
|
|
2013-08-16 00:15:07 +04:00
|
|
|
permissionManager->TestPermissionFromPrincipal(principal,
|
|
|
|
"desktop-notification",
|
|
|
|
&permission);
|
2013-03-18 17:24:54 +04:00
|
|
|
|
|
|
|
// Convert the result to one of the enum types.
|
|
|
|
switch (permission) {
|
|
|
|
case nsIPermissionManager::ALLOW_ACTION:
|
2013-05-06 23:28:13 +04:00
|
|
|
return NotificationPermission::Granted;
|
2013-03-18 17:24:54 +04:00
|
|
|
case nsIPermissionManager::DENY_ACTION:
|
2013-05-06 23:28:13 +04:00
|
|
|
return NotificationPermission::Denied;
|
2013-03-18 17:24:54 +04:00
|
|
|
default:
|
2013-05-06 23:28:13 +04:00
|
|
|
return NotificationPermission::Default;
|
2013-03-18 17:24:54 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-03 05:27:53 +04:00
|
|
|
already_AddRefed<Promise>
|
|
|
|
Notification::Get(const GlobalObject& aGlobal,
|
|
|
|
const GetNotificationOptions& aFilter,
|
|
|
|
ErrorResult& aRv)
|
|
|
|
{
|
2014-02-26 01:34:55 +04:00
|
|
|
nsCOMPtr<nsIGlobalObject> global =
|
|
|
|
do_QueryInterface(aGlobal.GetAsSupports());
|
|
|
|
MOZ_ASSERT(global);
|
|
|
|
nsCOMPtr<nsPIDOMWindow> window = do_QueryInterface(global);
|
2013-10-03 05:27:53 +04:00
|
|
|
MOZ_ASSERT(window);
|
|
|
|
nsIDocument* doc = window->GetExtantDoc();
|
|
|
|
if (!doc) {
|
|
|
|
aRv.Throw(NS_ERROR_UNEXPECTED);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsString origin;
|
|
|
|
aRv = GetOrigin(window, origin);
|
|
|
|
if (aRv.Failed()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsINotificationStorage> notificationStorage =
|
|
|
|
do_GetService(NS_NOTIFICATION_STORAGE_CONTRACTID, &rv);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
aRv.Throw(rv);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2014-07-19 05:31:11 +04:00
|
|
|
nsRefPtr<Promise> promise = Promise::Create(global, aRv);
|
|
|
|
if (aRv.Failed()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2013-10-03 05:27:53 +04:00
|
|
|
nsCOMPtr<nsINotificationStorageCallback> callback =
|
|
|
|
new NotificationStorageCallback(aGlobal, window, promise);
|
|
|
|
nsString tag = aFilter.mTag.WasPassed() ?
|
|
|
|
aFilter.mTag.Value() :
|
|
|
|
EmptyString();
|
|
|
|
aRv = notificationStorage->Get(origin, tag, callback);
|
|
|
|
if (aRv.Failed()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return promise.forget();
|
|
|
|
}
|
|
|
|
|
2013-03-18 17:24:54 +04:00
|
|
|
JSObject*
|
Bug 1117172 part 3. Change the wrappercached WrapObject methods to allow passing in aGivenProto. r=peterv
The only manual changes here are to BindingUtils.h, BindingUtils.cpp,
Codegen.py, Element.cpp, IDBFileRequest.cpp, IDBObjectStore.cpp,
dom/workers/Navigator.cpp, WorkerPrivate.cpp, DeviceStorageRequestChild.cpp,
Notification.cpp, nsGlobalWindow.cpp, MessagePort.cpp, nsJSEnvironment.cpp,
Sandbox.cpp, XPCConvert.cpp, ExportHelpers.cpp, and DataStoreService.cpp. The
rest of this diff was generated by running the following commands:
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObjectInternal\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObjectInternal\((?:aCx|cx|aContext|aCtx|js))\)/\1, aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapNode\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapNode\((?:aCx|cx|aContext|aCtx|js))\)/\1, aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObject\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(Binding(?:_workers)?::Wrap\((?:aCx|cx|aContext|aCtx|js), [^,)]+)\)/\1, aGivenProto)/g'
2015-03-19 17:13:33 +03:00
|
|
|
Notification::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
|
2013-03-18 17:24:54 +04:00
|
|
|
{
|
Bug 1117172 part 3. Change the wrappercached WrapObject methods to allow passing in aGivenProto. r=peterv
The only manual changes here are to BindingUtils.h, BindingUtils.cpp,
Codegen.py, Element.cpp, IDBFileRequest.cpp, IDBObjectStore.cpp,
dom/workers/Navigator.cpp, WorkerPrivate.cpp, DeviceStorageRequestChild.cpp,
Notification.cpp, nsGlobalWindow.cpp, MessagePort.cpp, nsJSEnvironment.cpp,
Sandbox.cpp, XPCConvert.cpp, ExportHelpers.cpp, and DataStoreService.cpp. The
rest of this diff was generated by running the following commands:
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObjectInternal\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObjectInternal\((?:aCx|cx|aContext|aCtx|js))\)/\1, aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapNode\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapNode\((?:aCx|cx|aContext|aCtx|js))\)/\1, aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObject\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(Binding(?:_workers)?::Wrap\((?:aCx|cx|aContext|aCtx|js), [^,)]+)\)/\1, aGivenProto)/g'
2015-03-19 17:13:33 +03:00
|
|
|
return mozilla::dom::NotificationBinding::Wrap(aCx, this, aGivenProto);
|
2013-03-18 17:24:54 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Notification::Close()
|
|
|
|
{
|
|
|
|
// Queue a task to close the notification.
|
2013-12-06 03:26:04 +04:00
|
|
|
nsCOMPtr<nsIRunnable> closeNotificationTask =
|
2013-03-18 17:24:54 +04:00
|
|
|
new NotificationTask(this, NotificationTask::eClose);
|
2013-12-06 03:26:04 +04:00
|
|
|
NS_DispatchToMainThread(closeNotificationTask);
|
2013-03-18 17:24:54 +04:00
|
|
|
}
|
|
|
|
|
2013-10-03 05:27:53 +04:00
|
|
|
void
|
2013-03-18 17:24:54 +04:00
|
|
|
Notification::CloseInternal()
|
|
|
|
{
|
2014-09-12 18:50:00 +04:00
|
|
|
if (mIsStored) {
|
2013-10-03 05:27:53 +04:00
|
|
|
// Don't bail out if notification storage fails, since we still
|
|
|
|
// want to send the close event through the alert service.
|
|
|
|
nsCOMPtr<nsINotificationStorage> notificationStorage =
|
|
|
|
do_GetService(NS_NOTIFICATION_STORAGE_CONTRACTID);
|
|
|
|
if (notificationStorage) {
|
|
|
|
nsString origin;
|
2014-05-20 17:26:36 +04:00
|
|
|
nsresult rv = GetOrigin(GetOwner(), origin);
|
2013-10-03 05:27:53 +04:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
notificationStorage->Delete(origin, mID);
|
|
|
|
}
|
|
|
|
}
|
2014-09-12 18:50:00 +04:00
|
|
|
SetStoredState(false);
|
|
|
|
}
|
|
|
|
if (!mIsClosed) {
|
2013-03-18 17:24:54 +04:00
|
|
|
nsCOMPtr<nsIAlertsService> alertService =
|
|
|
|
do_GetService(NS_ALERTSERVICE_CONTRACTID);
|
|
|
|
if (alertService) {
|
2014-05-20 17:26:36 +04:00
|
|
|
alertService->CloseAlert(mAlertName, GetPrincipal());
|
2013-10-07 22:41:13 +04:00
|
|
|
}
|
2013-10-03 05:27:53 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2013-10-03 05:27:53 +04:00
|
|
|
Notification::GetOrigin(nsPIDOMWindow* aWindow, nsString& aOrigin)
|
2013-10-03 05:27:53 +04:00
|
|
|
{
|
2013-12-06 03:26:04 +04:00
|
|
|
if (!aWindow) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2013-10-03 05:27:53 +04:00
|
|
|
nsresult rv;
|
|
|
|
nsIDocument* doc = aWindow->GetExtantDoc();
|
|
|
|
NS_ENSURE_TRUE(doc, NS_ERROR_UNEXPECTED);
|
|
|
|
nsIPrincipal* principal = doc->NodePrincipal();
|
|
|
|
NS_ENSURE_TRUE(principal, NS_ERROR_UNEXPECTED);
|
2013-10-07 22:41:13 +04:00
|
|
|
|
2013-10-03 05:27:53 +04:00
|
|
|
uint16_t appStatus = principal->GetAppStatus();
|
|
|
|
uint32_t appId = principal->GetAppId();
|
2013-10-07 22:41:13 +04:00
|
|
|
|
2013-10-03 05:27:53 +04:00
|
|
|
if (appStatus == nsIPrincipal::APP_STATUS_NOT_INSTALLED ||
|
|
|
|
appId == nsIScriptSecurityManager::NO_APP_ID ||
|
|
|
|
appId == nsIScriptSecurityManager::UNKNOWN_APP_ID) {
|
|
|
|
rv = nsContentUtils::GetUTFOrigin(principal, aOrigin);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
} else {
|
|
|
|
// If we are in "app code", use manifest URL as unique origin since
|
|
|
|
// multiple apps can share the same origin but not same notifications.
|
|
|
|
nsCOMPtr<nsIAppsService> appsService =
|
|
|
|
do_GetService("@mozilla.org/AppsService;1", &rv);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
appsService->GetManifestURLByLocalId(appId, aOrigin);
|
|
|
|
}
|
2013-10-07 22:41:13 +04:00
|
|
|
|
2013-10-03 05:27:53 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-08-21 04:56:12 +04:00
|
|
|
nsIStructuredCloneContainer* Notification::GetDataCloneContainer()
|
|
|
|
{
|
|
|
|
return mDataObjectContainer;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Notification::GetData(JSContext* aCx,
|
|
|
|
JS::MutableHandle<JS::Value> aRetval)
|
|
|
|
{
|
|
|
|
if (!mData && mDataObjectContainer) {
|
|
|
|
nsresult rv;
|
|
|
|
rv = mDataObjectContainer->DeserializeToVariant(aCx, getter_AddRefs(mData));
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
aRetval.setNull();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!mData) {
|
|
|
|
aRetval.setNull();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
VariantToJsval(aCx, mData, aRetval);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Notification::InitFromJSVal(JSContext* aCx, JS::Handle<JS::Value> aData,
|
|
|
|
ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
if (mDataObjectContainer || aData.isNull()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
mDataObjectContainer = new nsStructuredCloneContainer();
|
2015-04-16 20:22:15 +03:00
|
|
|
aRv = mDataObjectContainer->InitFromJSVal(aData, aCx);
|
2014-08-21 04:56:12 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void Notification::InitFromBase64(JSContext* aCx, const nsAString& aData,
|
|
|
|
ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
if (mDataObjectContainer || aData.IsEmpty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto container = new nsStructuredCloneContainer();
|
|
|
|
aRv = container->InitFromBase64(aData, JS_STRUCTURED_CLONE_VERSION,
|
|
|
|
aCx);
|
|
|
|
mDataObjectContainer = container;
|
|
|
|
}
|
|
|
|
|
2013-03-18 17:24:54 +04:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|
|
|
|
|