Files
palemoon27/dom/notification/DesktopNotification.cpp
T
roytam1 15db8f16bf import changes from `dev' branch of rmottola/Arctic-Fox:
- Bug 1132854 - Remove the gfx::ToIntSize conversion helper. r=Bas (0ee451e53)
- Bug 1020179 - Let PContent manage PContentPermissionRequest. r=fabrice, r=khuey (caf96b54e)
- Bug 1153023 - Convert TabParent::mChromeOffset to a LayoutDeviceIntPoint. r=billm (ba338584a)
- Bug 1139033 - Don't schedule an unnecessary repeat transaction when doing a non-progressive paint. r=nical (9c77d9318)
- Bug 1137203 - Ignore the critical displayport when a layer is subject to OMTA relative to the scrolling ancestor. r=BenWa (c9bedfb1e)
- Bug 1137203 - Cleanup to ditch the fast-path code entirely and just prevent progressive drawing in the equivalent scenarios. r=BenWa (6bc5b8813)
- Bug 1128042 - Don't round critical displayport out as it should already be tile aligned and rounding error can increase tile usage. r=botond (03e34e2c5)
- Bug 1149461 - Disable progressive drawing unless the compositor is actively scrolling a tiled layer. r=nical (7a3de28cb)
- Bug 1152838 - Correctly inflate valid regions to tile boundaries. r=mattwoodrow (7a496f645)
- Bug 1148971 - Make nsITheme::GetMinimumWidgetSize return a LayoutDeviceIntSize result instead of the unit-less nsIntSize type. r=roc (c34ddc478)
- Bug 1155621 - Make nsIntRect and nsIntPoint typedefs of mozilla::gfx::IntRect and mozilla::gfx::IntPoint. r=Bas (9901a37f6)
- Bug 1156632 - Remove unused forward class declarations - patch 5 - rdf, parser, layout and something else, r=ehsan (794739bd3)
- Bug 1156632 - Remove unused forward class declarations - patch 6 - the rest of the tree, r=ehsan (63a2c4cb4)
- Bug 1161634 - Allow synthesizing native mouse-scroll events on Linux. r=karlt (b88c1f367)
- Bug 1161634 - Enable the test_wheel_scroll on Linux as well. r=mstange (7dab62ad4)
- Bug 1144643 - Render tooltips as transparent on Gtk3. r=karlt (80f7fa312)
- Bug 1174966 part 1 - Change type of mCancelledPointerLockRequests field from uint32_t to bit field. r=smaug (ef235c33e)
- Bug 1155030 - Fix asterix/asterisk misspelling. r=ehsan (edb769304)
- Bug 1149194 - Don't use uninitialized value in ComputedTimingFunction::operator==. r=bbirtles (6bc804d45)
- Bug 1151346 - Make ActiveLayerTracker::IsOffsetOrMarginStyleAnimated respect CSS animations. r=roc (2c3f24ba0)
- Bug 1151346 - Back out the important part again because of bug 1151889. (002b0e67b)
- Bug 1122414 part 1 - Factor out a TransitionProperty method in ElementPropertyTransition; r=jwatt (ddbbdb04c)
- Bug 1122414 part 2 - Return the transitionProperty from Animation.name for CSS transitions; r=jwatt (689251b93)
- Bug 1122414 part 3 - Update DevTools tests to expect a name for transitions; r=pbrosset (f0d7e57e9)
- Bug 1149999 - 1 - Display transition names in animation-panel now that they have names; r=past (ea3d8d552)
- Bug 1120343 - 1 - Allow setting animations' currentTime by clicking/dragging the timeline; r=miker (936996d21)
- Bug 1120343 - 2 - Add rewind and fast-forward buttons to animation player widgets; r=miker (95eddc465)
- Bug 1120343 - 3 - Tests for the current time control in the animation panel; r=miker (b8a93f858)
- Bug 1110762 - Add a setCurrentTime method to the animation actor; r=past (d0dae8967)
- Bug 1123851 - 1 - Element geometry highlighter; r=bgrins (89b1a83bf)
- Bug 1123851 - 2 - Tests for the element geometry highlighter; r=bgrins (7542bcab0)
- add missing part of accessing first element from Bug 1139925 - Make the BoxModelHighlighter highlight all quads (b5c6076c1)
- Bug 1139186 - 1 - Refactor to the native anon nodes manipulation in highlighters; r=bgrins (a454aefbf)
- Bug 1139186 - 2 - Add event handling support to CanvasFrameAnonymousContentHelper; r=bgrins (a705c2716)
- Bug 1120339 - Add setPlaybackRate method to AnimationPlayerActor; r=past (efa167a19)
- Bug 1120833 - 2 - Fire events about changed animations in the AnimationsActor; r=past (4b5fddd23)
- Bug 1120833 - 1 - Remove EventEmitter usage in AnimationPlayerFront (21186e616)
2020-05-30 12:49:11 +08:00

342 lines
10 KiB
C++

/* 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/DesktopNotification.h"
#include "mozilla/dom/DesktopNotificationBinding.h"
#include "mozilla/dom/AppNotificationServiceOptionsBinding.h"
#include "mozilla/dom/ToJSValue.h"
#include "nsContentPermissionHelper.h"
#include "nsXULAppAPI.h"
#include "mozilla/dom/PBrowserChild.h"
#include "nsIDOMDesktopNotification.h"
#include "mozilla/Preferences.h"
#include "nsGlobalWindow.h"
#include "nsIAppsService.h"
#include "nsIScriptSecurityManager.h"
#include "nsServiceManagerUtils.h"
#include "PermissionMessageUtils.h"
#include "nsILoadContext.h"
namespace mozilla {
namespace dom {
/*
* Simple Request
*/
class DesktopNotificationRequest : public nsIContentPermissionRequest
, public nsRunnable
{
virtual ~DesktopNotificationRequest()
{
}
nsCOMPtr<nsIContentPermissionRequester> mRequester;
public:
NS_DECL_ISUPPORTS_INHERITED
NS_DECL_NSICONTENTPERMISSIONREQUEST
explicit DesktopNotificationRequest(DesktopNotification* aNotification)
: mDesktopNotification(aNotification)
{
mRequester = new nsContentPermissionRequester(mDesktopNotification->GetOwner());
}
NS_IMETHOD Run() override
{
nsCOMPtr<nsPIDOMWindow> window = mDesktopNotification->GetOwner();
nsContentPermissionUtils::AskPermission(this, window);
return NS_OK;
}
nsRefPtr<DesktopNotification> mDesktopNotification;
};
/* ------------------------------------------------------------------------ */
/* AlertServiceObserver */
/* ------------------------------------------------------------------------ */
NS_IMPL_ISUPPORTS(AlertServiceObserver, nsIObserver)
/* ------------------------------------------------------------------------ */
/* DesktopNotification */
/* ------------------------------------------------------------------------ */
uint32_t DesktopNotification::sCount = 0;
nsresult
DesktopNotification::PostDesktopNotification()
{
if (!mObserver) {
mObserver = new AlertServiceObserver(this);
}
#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);
mozilla::AutoSafeJSContext cx;
JS::Rooted<JS::Value> val(cx);
AppNotificationServiceOptions ops;
ops.mTextClickable = true;
ops.mManifestURL = manifestUrl;
if (!ToJSValue(cx, ops, &val)) {
return NS_ERROR_FAILURE;
}
return appNotifier->ShowAppNotification(mIconURL, mTitle, mDescription,
mObserver, val);
}
}
#endif
nsCOMPtr<nsIAlertsService> alerts = do_GetService("@mozilla.org/alerts-service;1");
if (!alerts) {
return NS_ERROR_NOT_IMPLEMENTED;
}
// Generate a unique name (which will also be used as a cookie) because
// the nsIAlertsService will coalesce notifications with the same name.
// In the case of IPC, the parent process will use the cookie to map
// to nsIObservers, thus cookies must be unique to differentiate observers.
nsString uniqueName = NS_LITERAL_STRING("desktop-notification:");
uniqueName.AppendInt(sCount++);
nsCOMPtr<nsIDocument> doc = GetOwner()->GetDoc();
nsIPrincipal* principal = doc->NodePrincipal();
nsCOMPtr<nsILoadContext> loadContext = doc->GetLoadContext();
bool inPrivateBrowsing = loadContext && loadContext->UsePrivateBrowsing();
return alerts->ShowAlertNotification(mIconURL, mTitle, mDescription,
true,
uniqueName,
mObserver,
uniqueName,
NS_LITERAL_STRING("auto"),
EmptyString(),
EmptyString(),
principal,
inPrivateBrowsing);
}
DesktopNotification::DesktopNotification(const nsAString & title,
const nsAString & description,
const nsAString & iconURL,
nsPIDOMWindow *aWindow,
nsIPrincipal* principal)
: DOMEventTargetHelper(aWindow)
, mTitle(title)
, mDescription(description)
, mIconURL(iconURL)
, mPrincipal(principal)
, mAllow(false)
, mShowHasBeenCalled(false)
{
if (Preferences::GetBool("notification.disabled", false)) {
return;
}
// If we are in testing mode (running mochitests, for example)
// and we are suppose to allow requests, then just post an allow event.
if (Preferences::GetBool("notification.prompt.testing", false) &&
Preferences::GetBool("notification.prompt.testing.allow", true)) {
mAllow = true;
}
}
void
DesktopNotification::Init()
{
nsRefPtr<DesktopNotificationRequest> request = new DesktopNotificationRequest(this);
NS_DispatchToMainThread(request);
}
DesktopNotification::~DesktopNotification()
{
if (mObserver) {
mObserver->Disconnect();
}
}
void
DesktopNotification::DispatchNotificationEvent(const nsString& aName)
{
if (NS_FAILED(CheckInnerWindowCorrectness())) {
return;
}
nsCOMPtr<nsIDOMEvent> event;
nsresult rv = NS_NewDOMEvent(getter_AddRefs(event), this, nullptr, nullptr);
if (NS_SUCCEEDED(rv)) {
// it doesn't bubble, and it isn't cancelable
rv = event->InitEvent(aName, false, false);
if (NS_SUCCEEDED(rv)) {
event->SetTrusted(true);
DispatchDOMEvent(nullptr, event, nullptr, nullptr);
}
}
}
nsresult
DesktopNotification::SetAllow(bool aAllow)
{
mAllow = aAllow;
// if we have called Show() already, lets go ahead and post a notification
if (mShowHasBeenCalled && aAllow) {
return PostDesktopNotification();
}
return NS_OK;
}
void
DesktopNotification::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"));
}
}
void
DesktopNotification::Show(ErrorResult& aRv)
{
mShowHasBeenCalled = true;
if (!mAllow) {
return;
}
aRv = PostDesktopNotification();
}
JSObject*
DesktopNotification::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
{
return DesktopNotificationBinding::Wrap(aCx, this, aGivenProto);
}
/* ------------------------------------------------------------------------ */
/* DesktopNotificationCenter */
/* ------------------------------------------------------------------------ */
NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE_0(DesktopNotificationCenter)
NS_IMPL_CYCLE_COLLECTING_ADDREF(DesktopNotificationCenter)
NS_IMPL_CYCLE_COLLECTING_RELEASE(DesktopNotificationCenter)
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(DesktopNotificationCenter)
NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
NS_INTERFACE_MAP_ENTRY(nsISupports)
NS_INTERFACE_MAP_END
already_AddRefed<DesktopNotification>
DesktopNotificationCenter::CreateNotification(const nsAString& aTitle,
const nsAString& aDescription,
const nsAString& aIconURL)
{
MOZ_ASSERT(mOwner);
nsRefPtr<DesktopNotification> notification =
new DesktopNotification(aTitle,
aDescription,
aIconURL,
mOwner,
mPrincipal);
notification->Init();
return notification.forget();
}
JSObject*
DesktopNotificationCenter::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
{
return DesktopNotificationCenterBinding::Wrap(aCx, this, aGivenProto);
}
/* ------------------------------------------------------------------------ */
/* DesktopNotificationRequest */
/* ------------------------------------------------------------------------ */
NS_IMPL_ISUPPORTS_INHERITED(DesktopNotificationRequest, nsRunnable,
nsIContentPermissionRequest)
NS_IMETHODIMP
DesktopNotificationRequest::GetPrincipal(nsIPrincipal * *aRequestingPrincipal)
{
if (!mDesktopNotification) {
return NS_ERROR_NOT_INITIALIZED;
}
NS_IF_ADDREF(*aRequestingPrincipal = mDesktopNotification->mPrincipal);
return NS_OK;
}
NS_IMETHODIMP
DesktopNotificationRequest::GetWindow(nsIDOMWindow * *aRequestingWindow)
{
if (!mDesktopNotification) {
return NS_ERROR_NOT_INITIALIZED;
}
NS_IF_ADDREF(*aRequestingWindow = mDesktopNotification->GetOwner());
return NS_OK;
}
NS_IMETHODIMP
DesktopNotificationRequest::GetElement(nsIDOMElement * *aElement)
{
NS_ENSURE_ARG_POINTER(aElement);
*aElement = nullptr;
return NS_OK;
}
NS_IMETHODIMP
DesktopNotificationRequest::Cancel()
{
nsresult rv = mDesktopNotification->SetAllow(false);
mDesktopNotification = nullptr;
return rv;
}
NS_IMETHODIMP
DesktopNotificationRequest::Allow(JS::HandleValue aChoices)
{
MOZ_ASSERT(aChoices.isUndefined());
nsresult rv = mDesktopNotification->SetAllow(true);
mDesktopNotification = nullptr;
return rv;
}
NS_IMETHODIMP
DesktopNotificationRequest::GetRequester(nsIContentPermissionRequester** aRequester)
{
NS_ENSURE_ARG_POINTER(aRequester);
nsCOMPtr<nsIContentPermissionRequester> requester = mRequester;
requester.forget(aRequester);
return NS_OK;
}
NS_IMETHODIMP
DesktopNotificationRequest::GetTypes(nsIArray** aTypes)
{
nsTArray<nsString> emptyOptions;
return nsContentPermissionUtils::CreatePermissionArray(NS_LITERAL_CSTRING("desktop-notification"),
NS_LITERAL_CSTRING("unused"),
emptyOptions,
aTypes);
}
} // namespace dom
} // namespace mozilla