2015-05-03 22:32:37 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2012-05-21 15:12:37 +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/. */
|
2010-09-13 23:49:27 +04:00
|
|
|
|
2015-04-14 04:08:00 +03:00
|
|
|
#include <map>
|
2010-09-13 23:49:27 +04:00
|
|
|
#include "nsCOMPtr.h"
|
2012-07-30 18:58:26 +04:00
|
|
|
#include "nsIPrincipal.h"
|
2015-04-14 04:08:00 +03:00
|
|
|
#include "mozilla/dom/ContentChild.h"
|
|
|
|
#include "mozilla/dom/ContentParent.h"
|
2013-07-24 03:39:17 +04:00
|
|
|
#include "mozilla/dom/Element.h"
|
2015-05-13 08:36:00 +03:00
|
|
|
#include "mozilla/dom/Event.h"
|
2014-02-10 00:34:40 +04:00
|
|
|
#include "mozilla/dom/PContentPermission.h"
|
2019-04-18 16:43:05 +03:00
|
|
|
#include "mozilla/dom/Performance.h"
|
2014-02-10 00:34:40 +04:00
|
|
|
#include "mozilla/dom/PermissionMessageUtils.h"
|
|
|
|
#include "mozilla/dom/PContentPermissionRequestParent.h"
|
2014-07-08 08:45:23 +04:00
|
|
|
#include "mozilla/dom/ScriptSettings.h"
|
2019-04-10 01:39:01 +03:00
|
|
|
#include "mozilla/dom/BrowserChild.h"
|
2019-04-10 00:38:15 +03:00
|
|
|
#include "mozilla/dom/BrowserParent.h"
|
2019-03-20 21:05:09 +03:00
|
|
|
#include "mozilla/Attributes.h"
|
2017-02-20 22:46:39 +03:00
|
|
|
#include "mozilla/EventStateManager.h"
|
2018-11-14 08:33:25 +03:00
|
|
|
#include "mozilla/Preferences.h"
|
2016-08-23 07:09:32 +03:00
|
|
|
#include "mozilla/Unused.h"
|
2013-09-11 00:56:05 +04:00
|
|
|
#include "nsComponentManagerUtils.h"
|
2014-02-10 00:34:40 +04:00
|
|
|
#include "nsArrayUtils.h"
|
|
|
|
#include "nsIMutableArray.h"
|
|
|
|
#include "nsContentPermissionHelper.h"
|
2013-09-10 19:41:59 +04:00
|
|
|
#include "nsJSUtils.h"
|
|
|
|
#include "nsISupportsPrimitives.h"
|
2014-08-01 11:22:20 +04:00
|
|
|
#include "nsServiceManagerUtils.h"
|
2019-01-02 16:05:23 +03:00
|
|
|
#include "mozilla/dom/Document.h"
|
2018-10-31 23:39:03 +03:00
|
|
|
#include "nsIWeakReferenceUtils.h"
|
2010-09-13 23:49:27 +04:00
|
|
|
|
2015-11-02 08:53:26 +03:00
|
|
|
using mozilla::Unused; // <snicker>
|
2012-07-30 18:58:26 +04:00
|
|
|
using namespace mozilla::dom;
|
2013-12-05 18:29:07 +04:00
|
|
|
using namespace mozilla;
|
2010-09-13 23:49:27 +04:00
|
|
|
|
2015-04-14 04:08:00 +03:00
|
|
|
#define kVisibilityChange "visibilitychange"
|
|
|
|
|
2016-01-22 21:28:33 +03:00
|
|
|
class VisibilityChangeListener final : public nsIDOMEventListener {
|
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
NS_DECL_NSIDOMEVENTLISTENER
|
|
|
|
|
2016-01-30 20:05:36 +03:00
|
|
|
explicit VisibilityChangeListener(nsPIDOMWindowInner* aWindow);
|
2016-01-22 21:28:33 +03:00
|
|
|
|
|
|
|
void RemoveListener();
|
|
|
|
void SetCallback(nsIContentPermissionRequestCallback* aCallback);
|
|
|
|
already_AddRefed<nsIContentPermissionRequestCallback> GetCallback();
|
|
|
|
|
|
|
|
private:
|
|
|
|
virtual ~VisibilityChangeListener() {}
|
|
|
|
|
|
|
|
nsWeakPtr mWindow;
|
|
|
|
nsCOMPtr<nsIContentPermissionRequestCallback> mCallback;
|
|
|
|
};
|
|
|
|
|
2015-04-14 04:08:00 +03:00
|
|
|
NS_IMPL_ISUPPORTS(VisibilityChangeListener, nsIDOMEventListener)
|
|
|
|
|
2016-01-30 20:05:36 +03:00
|
|
|
VisibilityChangeListener::VisibilityChangeListener(
|
|
|
|
nsPIDOMWindowInner* aWindow) {
|
2015-04-14 04:08:00 +03:00
|
|
|
MOZ_ASSERT(aWindow);
|
|
|
|
|
|
|
|
mWindow = do_GetWeakReference(aWindow);
|
2019-01-02 16:05:23 +03:00
|
|
|
nsCOMPtr<Document> doc = aWindow->GetExtantDoc();
|
2015-04-14 04:08:00 +03:00
|
|
|
if (doc) {
|
|
|
|
doc->AddSystemEventListener(NS_LITERAL_STRING(kVisibilityChange),
|
|
|
|
/* listener */ this,
|
|
|
|
/* use capture */ true,
|
|
|
|
/* wants untrusted */ false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2018-04-20 07:49:29 +03:00
|
|
|
VisibilityChangeListener::HandleEvent(Event* aEvent) {
|
2015-04-14 04:08:00 +03:00
|
|
|
nsAutoString type;
|
|
|
|
aEvent->GetType(type);
|
|
|
|
if (!type.EqualsLiteral(kVisibilityChange)) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2019-01-02 16:05:23 +03:00
|
|
|
nsCOMPtr<Document> doc = do_QueryInterface(aEvent->GetTarget());
|
2015-04-14 04:08:00 +03:00
|
|
|
MOZ_ASSERT(doc);
|
|
|
|
|
|
|
|
if (mCallback) {
|
|
|
|
mCallback->NotifyVisibility(!doc->Hidden());
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void VisibilityChangeListener::RemoveListener() {
|
2016-01-30 20:05:36 +03:00
|
|
|
nsCOMPtr<nsPIDOMWindowInner> window = do_QueryReferent(mWindow);
|
2015-04-14 04:08:00 +03:00
|
|
|
if (!window) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-10-02 00:38:01 +03:00
|
|
|
nsCOMPtr<EventTarget> target = window->GetExtantDoc();
|
2015-04-14 04:08:00 +03:00
|
|
|
if (target) {
|
|
|
|
target->RemoveSystemEventListener(NS_LITERAL_STRING(kVisibilityChange),
|
|
|
|
/* listener */ this,
|
|
|
|
/* use capture */ true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void VisibilityChangeListener::SetCallback(
|
|
|
|
nsIContentPermissionRequestCallback* aCallback) {
|
|
|
|
mCallback = aCallback;
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<nsIContentPermissionRequestCallback>
|
|
|
|
VisibilityChangeListener::GetCallback() {
|
|
|
|
nsCOMPtr<nsIContentPermissionRequestCallback> callback = mCallback;
|
|
|
|
return callback.forget();
|
|
|
|
}
|
|
|
|
|
2014-02-10 00:34:40 +04:00
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
|
|
|
class ContentPermissionRequestParent : public PContentPermissionRequestParent {
|
|
|
|
public:
|
2019-04-18 16:43:05 +03:00
|
|
|
ContentPermissionRequestParent(
|
|
|
|
const nsTArray<PermissionRequest>& aRequests, Element* aElement,
|
2019-04-25 17:18:11 +03:00
|
|
|
nsIPrincipal* aPrincipal, nsIPrincipal* aTopLevelPrincipal,
|
|
|
|
const bool aIsHandlingUserInput,
|
2019-04-18 16:43:05 +03:00
|
|
|
const bool aUserHadInteractedWithDocument,
|
|
|
|
const DOMTimeStamp aDocumentDOMContentLoadedTimestamp);
|
2014-02-10 00:34:40 +04:00
|
|
|
virtual ~ContentPermissionRequestParent();
|
|
|
|
|
|
|
|
bool IsBeingDestroyed();
|
|
|
|
|
|
|
|
nsCOMPtr<nsIPrincipal> mPrincipal;
|
2018-11-20 04:52:47 +03:00
|
|
|
nsCOMPtr<nsIPrincipal> mTopLevelPrincipal;
|
2014-02-10 00:34:40 +04:00
|
|
|
nsCOMPtr<Element> mElement;
|
2017-02-20 22:46:39 +03:00
|
|
|
bool mIsHandlingUserInput;
|
2019-04-18 16:43:05 +03:00
|
|
|
bool mUserHadInteractedWithDocument;
|
|
|
|
DOMTimeStamp mDocumentDOMContentLoadedTimestamp;
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<nsContentPermissionRequestProxy> mProxy;
|
2014-02-10 00:34:40 +04:00
|
|
|
nsTArray<PermissionRequest> mRequests;
|
|
|
|
|
|
|
|
private:
|
2019-03-20 21:05:09 +03:00
|
|
|
// Not MOZ_CAN_RUN_SCRIPT because we can't annotate the thing we override yet.
|
|
|
|
MOZ_CAN_RUN_SCRIPT_BOUNDARY
|
2017-11-06 06:37:28 +03:00
|
|
|
virtual mozilla::ipc::IPCResult Recvprompt() override;
|
|
|
|
virtual mozilla::ipc::IPCResult RecvNotifyVisibility(
|
|
|
|
const bool& aIsVisible) override;
|
|
|
|
virtual mozilla::ipc::IPCResult RecvDestroy() override;
|
|
|
|
virtual void ActorDestroy(ActorDestroyReason why) override;
|
2014-02-10 00:34:40 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
ContentPermissionRequestParent::ContentPermissionRequestParent(
|
|
|
|
const nsTArray<PermissionRequest>& aRequests, Element* aElement,
|
2019-04-25 17:18:11 +03:00
|
|
|
nsIPrincipal* aPrincipal, nsIPrincipal* aTopLevelPrincipal,
|
2019-04-18 16:43:05 +03:00
|
|
|
const bool aIsHandlingUserInput, const bool aUserHadInteractedWithDocument,
|
|
|
|
const DOMTimeStamp aDocumentDOMContentLoadedTimestamp) {
|
2014-02-10 00:34:40 +04:00
|
|
|
MOZ_COUNT_CTOR(ContentPermissionRequestParent);
|
|
|
|
|
|
|
|
mPrincipal = aPrincipal;
|
2018-11-20 04:52:47 +03:00
|
|
|
mTopLevelPrincipal = aTopLevelPrincipal;
|
2014-02-10 00:34:40 +04:00
|
|
|
mElement = aElement;
|
|
|
|
mRequests = aRequests;
|
2017-02-20 22:46:39 +03:00
|
|
|
mIsHandlingUserInput = aIsHandlingUserInput;
|
2019-04-18 16:43:05 +03:00
|
|
|
mUserHadInteractedWithDocument = aUserHadInteractedWithDocument;
|
|
|
|
mDocumentDOMContentLoadedTimestamp = aDocumentDOMContentLoadedTimestamp;
|
2014-02-10 00:34:40 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
ContentPermissionRequestParent::~ContentPermissionRequestParent() {
|
|
|
|
MOZ_COUNT_DTOR(ContentPermissionRequestParent);
|
|
|
|
}
|
|
|
|
|
|
|
|
mozilla::ipc::IPCResult ContentPermissionRequestParent::Recvprompt() {
|
2018-06-16 17:21:46 +03:00
|
|
|
mProxy = new nsContentPermissionRequestProxy(this);
|
|
|
|
if (NS_FAILED(mProxy->Init(mRequests))) {
|
2019-03-20 21:05:09 +03:00
|
|
|
RefPtr<nsContentPermissionRequestProxy> proxy(mProxy);
|
|
|
|
proxy->Cancel();
|
2014-02-10 00:34:40 +04:00
|
|
|
}
|
2016-11-15 06:26:00 +03:00
|
|
|
return IPC_OK();
|
2014-02-10 00:34:40 +04:00
|
|
|
}
|
|
|
|
|
2015-04-14 04:08:00 +03:00
|
|
|
mozilla::ipc::IPCResult ContentPermissionRequestParent::RecvNotifyVisibility(
|
|
|
|
const bool& aIsVisible) {
|
|
|
|
if (!mProxy) {
|
2016-11-15 06:26:00 +03:00
|
|
|
return IPC_FAIL_NO_REASON(this);
|
2015-04-14 04:08:00 +03:00
|
|
|
}
|
|
|
|
mProxy->NotifyVisibility(aIsVisible);
|
2016-11-15 06:26:00 +03:00
|
|
|
return IPC_OK();
|
2015-04-14 04:08:00 +03:00
|
|
|
}
|
|
|
|
|
2015-11-09 03:55:08 +03:00
|
|
|
mozilla::ipc::IPCResult ContentPermissionRequestParent::RecvDestroy() {
|
|
|
|
Unused << PContentPermissionRequestParent::Send__delete__(this);
|
2016-11-15 06:26:00 +03:00
|
|
|
return IPC_OK();
|
2015-11-09 03:55:08 +03:00
|
|
|
}
|
|
|
|
|
2014-02-10 00:34:40 +04:00
|
|
|
void ContentPermissionRequestParent::ActorDestroy(ActorDestroyReason why) {
|
|
|
|
if (mProxy) {
|
|
|
|
mProxy->OnParentDestroyed();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ContentPermissionRequestParent::IsBeingDestroyed() {
|
2015-04-14 04:08:00 +03:00
|
|
|
// When ContentParent::MarkAsDead() is called, we are being destroyed.
|
|
|
|
// It's unsafe to send out any message now.
|
|
|
|
ContentParent* contentParent = static_cast<ContentParent*>(Manager());
|
|
|
|
return !contentParent->IsAlive();
|
2014-02-10 00:34:40 +04:00
|
|
|
}
|
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS(ContentPermissionType, nsIContentPermissionType)
|
2014-02-10 00:34:40 +04:00
|
|
|
|
|
|
|
ContentPermissionType::ContentPermissionType(
|
2013-09-10 19:41:59 +04:00
|
|
|
const nsACString& aType, const nsTArray<nsString>& aOptions) {
|
2014-02-10 00:34:40 +04:00
|
|
|
mType = aType;
|
2013-09-10 19:41:59 +04:00
|
|
|
mOptions = aOptions;
|
2014-02-10 00:34:40 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
ContentPermissionType::~ContentPermissionType() {}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
ContentPermissionType::GetType(nsACString& aType) {
|
|
|
|
aType = mType;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-09-10 19:41:59 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
ContentPermissionType::GetOptions(nsIArray** aOptions) {
|
|
|
|
NS_ENSURE_ARG_POINTER(aOptions);
|
|
|
|
|
|
|
|
*aOptions = nullptr;
|
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsIMutableArray> options =
|
|
|
|
do_CreateInstance(NS_ARRAY_CONTRACTID, &rv);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
// copy options into JS array
|
|
|
|
for (uint32_t i = 0; i < mOptions.Length(); ++i) {
|
|
|
|
nsCOMPtr<nsISupportsString> isupportsString =
|
|
|
|
do_CreateInstance(NS_SUPPORTS_STRING_CONTRACTID, &rv);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = isupportsString->SetData(mOptions[i]);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2017-10-21 17:53:02 +03:00
|
|
|
rv = options->AppendElement(isupportsString);
|
2013-09-10 19:41:59 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
|
2015-03-31 17:03:49 +03:00
|
|
|
options.forget(aOptions);
|
2013-09-10 19:41:59 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-08-01 11:22:20 +04:00
|
|
|
// nsContentPermissionUtils
|
|
|
|
|
2019-02-26 01:05:29 +03:00
|
|
|
/* static */
|
|
|
|
uint32_t nsContentPermissionUtils::ConvertPermissionRequestToArray(
|
2014-08-01 11:22:20 +04:00
|
|
|
nsTArray<PermissionRequest>& aSrcArray, nsIMutableArray* aDesArray) {
|
2014-02-10 00:34:40 +04:00
|
|
|
uint32_t len = aSrcArray.Length();
|
|
|
|
for (uint32_t i = 0; i < len; i++) {
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<ContentPermissionType> cpt =
|
2013-09-10 19:41:59 +04:00
|
|
|
new ContentPermissionType(aSrcArray[i].type(), aSrcArray[i].options());
|
2017-10-21 17:53:02 +03:00
|
|
|
aDesArray->AppendElement(cpt);
|
2014-02-10 00:34:40 +04:00
|
|
|
}
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
2019-02-26 01:05:29 +03:00
|
|
|
/* static */
|
|
|
|
uint32_t nsContentPermissionUtils::ConvertArrayToPermissionRequest(
|
2014-08-01 11:22:20 +04:00
|
|
|
nsIArray* aSrcArray, nsTArray<PermissionRequest>& aDesArray) {
|
|
|
|
uint32_t len = 0;
|
|
|
|
aSrcArray->GetLength(&len);
|
|
|
|
for (uint32_t i = 0; i < len; i++) {
|
|
|
|
nsCOMPtr<nsIContentPermissionType> cpt = do_QueryElementAt(aSrcArray, i);
|
|
|
|
nsAutoCString type;
|
|
|
|
cpt->GetType(type);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIArray> optionArray;
|
|
|
|
cpt->GetOptions(getter_AddRefs(optionArray));
|
|
|
|
uint32_t optionsLength = 0;
|
|
|
|
if (optionArray) {
|
|
|
|
optionArray->GetLength(&optionsLength);
|
|
|
|
}
|
|
|
|
nsTArray<nsString> options;
|
|
|
|
for (uint32_t j = 0; j < optionsLength; ++j) {
|
|
|
|
nsCOMPtr<nsISupportsString> isupportsString =
|
|
|
|
do_QueryElementAt(optionArray, j);
|
|
|
|
if (isupportsString) {
|
|
|
|
nsString option;
|
|
|
|
isupportsString->GetData(option);
|
|
|
|
options.AppendElement(option);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-30 10:48:55 +03:00
|
|
|
aDesArray.AppendElement(PermissionRequest(type, options));
|
2014-08-01 11:22:20 +04:00
|
|
|
}
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
2015-04-14 04:08:00 +03:00
|
|
|
static std::map<PContentPermissionRequestParent*, TabId>&
|
|
|
|
ContentPermissionRequestParentMap() {
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
static std::map<PContentPermissionRequestParent*, TabId>
|
|
|
|
sPermissionRequestParentMap;
|
|
|
|
return sPermissionRequestParentMap;
|
|
|
|
}
|
2014-08-01 11:22:20 +04:00
|
|
|
|
2015-11-09 03:55:08 +03:00
|
|
|
static std::map<PContentPermissionRequestChild*, TabId>&
|
|
|
|
ContentPermissionRequestChildMap() {
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
static std::map<PContentPermissionRequestChild*, TabId>
|
|
|
|
sPermissionRequestChildMap;
|
|
|
|
return sPermissionRequestChildMap;
|
|
|
|
}
|
|
|
|
|
2019-02-26 01:05:29 +03:00
|
|
|
/* static */
|
|
|
|
nsresult nsContentPermissionUtils::CreatePermissionArray(
|
2014-08-01 11:22:20 +04:00
|
|
|
const nsACString& aType, const nsTArray<nsString>& aOptions,
|
|
|
|
nsIArray** aTypesArray) {
|
2014-02-10 00:34:40 +04:00
|
|
|
nsCOMPtr<nsIMutableArray> types = do_CreateInstance(NS_ARRAY_CONTRACTID);
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<ContentPermissionType> permType =
|
|
|
|
new ContentPermissionType(aType, aOptions);
|
2017-10-21 17:53:02 +03:00
|
|
|
types->AppendElement(permType);
|
2014-02-10 00:34:40 +04:00
|
|
|
types.forget(aTypesArray);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2019-02-26 01:05:29 +03:00
|
|
|
/* static */
|
|
|
|
PContentPermissionRequestParent*
|
2014-08-01 11:22:20 +04:00
|
|
|
nsContentPermissionUtils::CreateContentPermissionRequestParent(
|
|
|
|
const nsTArray<PermissionRequest>& aRequests, Element* aElement,
|
2019-04-25 17:18:11 +03:00
|
|
|
nsIPrincipal* aPrincipal, nsIPrincipal* aTopLevelPrincipal,
|
2019-04-18 16:43:05 +03:00
|
|
|
const bool aIsHandlingUserInput, const bool aUserHadInteractedWithDocument,
|
|
|
|
const DOMTimeStamp aDocumentDOMContentLoadedTimestamp,
|
|
|
|
const TabId& aTabId) {
|
2015-04-14 04:08:00 +03:00
|
|
|
PContentPermissionRequestParent* parent = new ContentPermissionRequestParent(
|
2019-04-18 16:43:05 +03:00
|
|
|
aRequests, aElement, aPrincipal, aTopLevelPrincipal, aIsHandlingUserInput,
|
|
|
|
aUserHadInteractedWithDocument, aDocumentDOMContentLoadedTimestamp);
|
2015-04-14 04:08:00 +03:00
|
|
|
ContentPermissionRequestParentMap()[parent] = aTabId;
|
|
|
|
|
|
|
|
return parent;
|
2014-02-10 00:34:40 +04:00
|
|
|
}
|
|
|
|
|
2019-02-26 01:05:29 +03:00
|
|
|
/* static */
|
|
|
|
nsresult nsContentPermissionUtils::AskPermission(
|
2016-01-30 20:05:36 +03:00
|
|
|
nsIContentPermissionRequest* aRequest, nsPIDOMWindowInner* aWindow) {
|
2014-08-01 11:22:20 +04:00
|
|
|
NS_ENSURE_STATE(aWindow && aWindow->IsCurrentInnerWindow());
|
|
|
|
|
|
|
|
// for content process
|
2015-07-04 04:29:00 +03:00
|
|
|
if (XRE_IsContentProcess()) {
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<RemotePermissionRequest> req =
|
2014-08-01 11:22:20 +04:00
|
|
|
new RemotePermissionRequest(aRequest, aWindow);
|
|
|
|
|
|
|
|
MOZ_ASSERT(NS_IsMainThread()); // IPC can only be execute on main thread.
|
|
|
|
|
2019-04-10 01:39:01 +03:00
|
|
|
BrowserChild* child = BrowserChild::GetFrom(aWindow->GetDocShell());
|
2014-08-01 11:22:20 +04:00
|
|
|
NS_ENSURE_TRUE(child, NS_ERROR_FAILURE);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIArray> typeArray;
|
|
|
|
nsresult rv = aRequest->GetTypes(getter_AddRefs(typeArray));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
nsTArray<PermissionRequest> permArray;
|
|
|
|
ConvertArrayToPermissionRequest(typeArray, permArray);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIPrincipal> principal;
|
|
|
|
rv = aRequest->GetPrincipal(getter_AddRefs(principal));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2018-11-20 04:52:47 +03:00
|
|
|
nsCOMPtr<nsIPrincipal> topLevelPrincipal;
|
|
|
|
rv = aRequest->GetTopLevelPrincipal(getter_AddRefs(topLevelPrincipal));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2017-02-20 22:46:39 +03:00
|
|
|
bool isHandlingUserInput;
|
|
|
|
rv = aRequest->GetIsHandlingUserInput(&isHandlingUserInput);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2019-04-18 16:43:05 +03:00
|
|
|
bool userHadInteractedWithDocument;
|
|
|
|
rv = aRequest->GetUserHadInteractedWithDocument(
|
|
|
|
&userHadInteractedWithDocument);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
DOMTimeStamp documentDOMContentLoadedTimestamp;
|
|
|
|
rv = aRequest->GetDocumentDOMContentLoadedTimestamp(
|
|
|
|
&documentDOMContentLoadedTimestamp);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2017-08-21 12:06:05 +03:00
|
|
|
ContentChild::GetSingleton()->SetEventTargetForActor(
|
|
|
|
req, aWindow->EventTargetFor(TaskCategory::Other));
|
|
|
|
|
2014-08-01 11:22:20 +04:00
|
|
|
req->IPDLAddRef();
|
2015-04-14 04:08:00 +03:00
|
|
|
ContentChild::GetSingleton()->SendPContentPermissionRequestConstructor(
|
|
|
|
req, permArray, IPC::Principal(principal),
|
2018-11-20 04:52:47 +03:00
|
|
|
IPC::Principal(topLevelPrincipal), isHandlingUserInput,
|
2019-04-18 16:43:05 +03:00
|
|
|
userHadInteractedWithDocument, documentDOMContentLoadedTimestamp,
|
2015-04-14 04:08:00 +03:00
|
|
|
child->GetTabId());
|
2015-11-09 03:55:08 +03:00
|
|
|
ContentPermissionRequestChildMap()[req.get()] = child->GetTabId();
|
2014-08-01 11:22:20 +04:00
|
|
|
|
|
|
|
req->Sendprompt();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// for chrome process
|
|
|
|
nsCOMPtr<nsIContentPermissionPrompt> prompt =
|
|
|
|
do_GetService(NS_CONTENT_PERMISSION_PROMPT_CONTRACTID);
|
|
|
|
if (prompt) {
|
2015-07-22 22:46:45 +03:00
|
|
|
if (NS_FAILED(prompt->Prompt(aRequest))) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2014-08-01 11:22:20 +04:00
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2019-02-26 01:05:29 +03:00
|
|
|
/* static */
|
|
|
|
nsTArray<PContentPermissionRequestParent*>
|
2015-04-14 04:08:00 +03:00
|
|
|
nsContentPermissionUtils::GetContentPermissionRequestParentById(
|
|
|
|
const TabId& aTabId) {
|
|
|
|
nsTArray<PContentPermissionRequestParent*> parentArray;
|
|
|
|
for (auto& it : ContentPermissionRequestParentMap()) {
|
|
|
|
if (it.second == aTabId) {
|
|
|
|
parentArray.AppendElement(it.first);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-01 18:59:07 +03:00
|
|
|
return parentArray;
|
2015-04-14 04:08:00 +03:00
|
|
|
}
|
|
|
|
|
2019-02-26 01:05:29 +03:00
|
|
|
/* static */
|
|
|
|
void nsContentPermissionUtils::NotifyRemoveContentPermissionRequestParent(
|
2015-04-14 04:08:00 +03:00
|
|
|
PContentPermissionRequestParent* aParent) {
|
|
|
|
auto it = ContentPermissionRequestParentMap().find(aParent);
|
|
|
|
MOZ_ASSERT(it != ContentPermissionRequestParentMap().end());
|
|
|
|
|
|
|
|
ContentPermissionRequestParentMap().erase(it);
|
|
|
|
}
|
|
|
|
|
2019-02-26 01:05:29 +03:00
|
|
|
/* static */
|
|
|
|
nsTArray<PContentPermissionRequestChild*>
|
2015-11-09 03:55:08 +03:00
|
|
|
nsContentPermissionUtils::GetContentPermissionRequestChildById(
|
|
|
|
const TabId& aTabId) {
|
|
|
|
nsTArray<PContentPermissionRequestChild*> childArray;
|
|
|
|
for (auto& it : ContentPermissionRequestChildMap()) {
|
|
|
|
if (it.second == aTabId) {
|
|
|
|
childArray.AppendElement(it.first);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-01 18:59:07 +03:00
|
|
|
return childArray;
|
2015-11-09 03:55:08 +03:00
|
|
|
}
|
|
|
|
|
2019-02-26 01:05:29 +03:00
|
|
|
/* static */
|
|
|
|
void nsContentPermissionUtils::NotifyRemoveContentPermissionRequestChild(
|
2015-11-09 03:55:08 +03:00
|
|
|
PContentPermissionRequestChild* aChild) {
|
|
|
|
auto it = ContentPermissionRequestChildMap().find(aChild);
|
|
|
|
MOZ_ASSERT(it != ContentPermissionRequestChildMap().end());
|
|
|
|
|
|
|
|
ContentPermissionRequestChildMap().erase(it);
|
|
|
|
}
|
|
|
|
|
2015-04-14 04:08:00 +03:00
|
|
|
NS_IMPL_ISUPPORTS(nsContentPermissionRequester, nsIContentPermissionRequester)
|
|
|
|
|
2016-01-30 20:05:36 +03:00
|
|
|
nsContentPermissionRequester::nsContentPermissionRequester(
|
|
|
|
nsPIDOMWindowInner* aWindow)
|
2016-01-19 16:07:00 +03:00
|
|
|
: mWindow(do_GetWeakReference(aWindow)),
|
|
|
|
mListener(new VisibilityChangeListener(aWindow)) {}
|
2015-04-14 04:08:00 +03:00
|
|
|
|
|
|
|
nsContentPermissionRequester::~nsContentPermissionRequester() {
|
|
|
|
mListener->RemoveListener();
|
|
|
|
mListener = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsContentPermissionRequester::GetVisibility(
|
|
|
|
nsIContentPermissionRequestCallback* aCallback) {
|
|
|
|
NS_ENSURE_ARG_POINTER(aCallback);
|
|
|
|
|
2016-01-30 20:05:36 +03:00
|
|
|
nsCOMPtr<nsPIDOMWindowInner> window = do_QueryReferent(mWindow);
|
2016-01-19 16:07:00 +03:00
|
|
|
if (!window) {
|
2015-04-14 04:08:00 +03:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2016-01-19 16:07:00 +03:00
|
|
|
nsCOMPtr<nsIDocShell> docshell = window->GetDocShell();
|
2015-04-14 04:08:00 +03:00
|
|
|
if (!docshell) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isActive = false;
|
|
|
|
docshell->GetIsActive(&isActive);
|
|
|
|
aCallback->NotifyVisibility(isActive);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsContentPermissionRequester::SetOnVisibilityChange(
|
|
|
|
nsIContentPermissionRequestCallback* aCallback) {
|
|
|
|
mListener->SetCallback(aCallback);
|
|
|
|
|
|
|
|
if (!aCallback) {
|
|
|
|
mListener->RemoveListener();
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsContentPermissionRequester::GetOnVisibilityChange(
|
|
|
|
nsIContentPermissionRequestCallback** aCallback) {
|
|
|
|
NS_ENSURE_ARG_POINTER(aCallback);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIContentPermissionRequestCallback> callback =
|
|
|
|
mListener->GetCallback();
|
|
|
|
callback.forget(aCallback);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2018-11-20 04:52:47 +03:00
|
|
|
static nsIPrincipal* GetTopLevelPrincipal(nsPIDOMWindowInner* aWindow) {
|
|
|
|
MOZ_ASSERT(aWindow);
|
|
|
|
|
|
|
|
nsPIDOMWindowOuter* top = aWindow->GetScriptableTop();
|
|
|
|
if (!top) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsPIDOMWindowInner* inner = top->GetCurrentInnerWindow();
|
|
|
|
if (!inner) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return nsGlobalWindowInner::Cast(inner)->GetPrincipal();
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION(ContentPermissionRequestBase, mPrincipal,
|
|
|
|
mTopLevelPrincipal, mWindow)
|
2018-11-14 08:33:25 +03:00
|
|
|
|
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(ContentPermissionRequestBase)
|
|
|
|
NS_INTERFACE_MAP_ENTRY_CONCRETE(nsISupports)
|
|
|
|
NS_INTERFACE_MAP_ENTRY_CONCRETE(nsIContentPermissionRequest)
|
|
|
|
NS_INTERFACE_MAP_END
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_ADDREF(ContentPermissionRequestBase)
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_RELEASE(ContentPermissionRequestBase)
|
|
|
|
|
|
|
|
ContentPermissionRequestBase::ContentPermissionRequestBase(
|
2019-02-04 22:56:48 +03:00
|
|
|
nsIPrincipal* aPrincipal, nsPIDOMWindowInner* aWindow,
|
|
|
|
const nsACString& aPrefName, const nsACString& aType)
|
2018-11-14 08:33:25 +03:00
|
|
|
: mPrincipal(aPrincipal),
|
2018-11-20 04:52:47 +03:00
|
|
|
mTopLevelPrincipal(aWindow ? ::GetTopLevelPrincipal(aWindow) : nullptr),
|
2018-11-14 08:33:25 +03:00
|
|
|
mWindow(aWindow),
|
|
|
|
mRequester(aWindow ? new nsContentPermissionRequester(aWindow) : nullptr),
|
|
|
|
mPrefName(aPrefName),
|
|
|
|
mType(aType),
|
2019-04-18 16:43:05 +03:00
|
|
|
mIsHandlingUserInput(EventStateManager::IsHandlingUserInput()),
|
|
|
|
mUserHadInteractedWithDocument(false),
|
|
|
|
mDocumentDOMContentLoadedTimestamp(0) {
|
|
|
|
if (!aWindow) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Document* doc = aWindow->GetExtantDoc();
|
|
|
|
if (!doc) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
mUserHadInteractedWithDocument = doc->UserHasInteracted();
|
|
|
|
|
|
|
|
nsDOMNavigationTiming* navTiming = doc->GetNavigationTiming();
|
|
|
|
if (navTiming) {
|
|
|
|
mDocumentDOMContentLoadedTimestamp =
|
|
|
|
navTiming->GetDomContentLoadedEventEnd();
|
|
|
|
}
|
|
|
|
}
|
2018-11-14 08:33:25 +03:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
ContentPermissionRequestBase::GetPrincipal(
|
|
|
|
nsIPrincipal** aRequestingPrincipal) {
|
2018-12-07 21:02:56 +03:00
|
|
|
NS_IF_ADDREF(*aRequestingPrincipal = mPrincipal);
|
2018-11-14 08:33:25 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2018-11-20 04:52:47 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
ContentPermissionRequestBase::GetTopLevelPrincipal(
|
|
|
|
nsIPrincipal** aRequestingPrincipal) {
|
2018-12-07 21:02:56 +03:00
|
|
|
NS_IF_ADDREF(*aRequestingPrincipal = mTopLevelPrincipal);
|
2018-11-20 04:52:47 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2018-11-14 08:33:25 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
ContentPermissionRequestBase::GetWindow(mozIDOMWindow** aRequestingWindow) {
|
2018-12-07 21:02:56 +03:00
|
|
|
NS_IF_ADDREF(*aRequestingWindow = mWindow);
|
2018-11-14 08:33:25 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
ContentPermissionRequestBase::GetElement(Element** aElement) {
|
|
|
|
NS_ENSURE_ARG_POINTER(aElement);
|
|
|
|
*aElement = nullptr;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
ContentPermissionRequestBase::GetIsHandlingUserInput(
|
|
|
|
bool* aIsHandlingUserInput) {
|
|
|
|
*aIsHandlingUserInput = mIsHandlingUserInput;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2019-04-18 16:43:05 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
ContentPermissionRequestBase::GetUserHadInteractedWithDocument(
|
|
|
|
bool* aUserHadInteractedWithDocument) {
|
|
|
|
*aUserHadInteractedWithDocument = mUserHadInteractedWithDocument;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
ContentPermissionRequestBase::GetDocumentDOMContentLoadedTimestamp(
|
|
|
|
DOMTimeStamp* aDocumentDOMContentLoadedTimestamp) {
|
|
|
|
*aDocumentDOMContentLoadedTimestamp = mDocumentDOMContentLoadedTimestamp;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2018-11-14 08:33:25 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
ContentPermissionRequestBase::GetRequester(
|
|
|
|
nsIContentPermissionRequester** aRequester) {
|
|
|
|
NS_ENSURE_ARG_POINTER(aRequester);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIContentPermissionRequester> requester = mRequester;
|
|
|
|
requester.forget(aRequester);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
ContentPermissionRequestBase::GetTypes(nsIArray** aTypes) {
|
|
|
|
nsTArray<nsString> emptyOptions;
|
|
|
|
return nsContentPermissionUtils::CreatePermissionArray(mType, emptyOptions,
|
|
|
|
aTypes);
|
|
|
|
}
|
|
|
|
|
|
|
|
ContentPermissionRequestBase::PromptResult
|
|
|
|
ContentPermissionRequestBase::CheckPromptPrefs() {
|
|
|
|
MOZ_ASSERT(!mPrefName.IsEmpty(),
|
|
|
|
"This derived class must support checking pref types");
|
|
|
|
|
|
|
|
nsAutoCString prefName(mPrefName);
|
|
|
|
prefName.AppendLiteral(".prompt.testing");
|
|
|
|
if (Preferences::GetBool(PromiseFlatCString(prefName).get(), false)) {
|
|
|
|
prefName.AppendLiteral(".allow");
|
|
|
|
if (Preferences::GetBool(PromiseFlatCString(prefName).get(), true)) {
|
|
|
|
return PromptResult::Granted;
|
|
|
|
}
|
|
|
|
return PromptResult::Denied;
|
|
|
|
}
|
|
|
|
|
|
|
|
return PromptResult::Pending;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult ContentPermissionRequestBase::ShowPrompt(
|
|
|
|
ContentPermissionRequestBase::PromptResult& aResult) {
|
|
|
|
aResult = CheckPromptPrefs();
|
|
|
|
|
|
|
|
if (aResult != PromptResult::Pending) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
return nsContentPermissionUtils::AskPermission(this, mWindow);
|
|
|
|
}
|
|
|
|
|
|
|
|
class RequestPromptEvent : public Runnable {
|
|
|
|
public:
|
|
|
|
RequestPromptEvent(ContentPermissionRequestBase* aRequest,
|
|
|
|
nsPIDOMWindowInner* aWindow)
|
|
|
|
: mozilla::Runnable("RequestPromptEvent"),
|
|
|
|
mRequest(aRequest),
|
|
|
|
mWindow(aWindow) {}
|
|
|
|
|
|
|
|
NS_IMETHOD Run() override {
|
|
|
|
nsContentPermissionUtils::AskPermission(mRequest, mWindow);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
RefPtr<ContentPermissionRequestBase> mRequest;
|
|
|
|
nsCOMPtr<nsPIDOMWindowInner> mWindow;
|
|
|
|
};
|
|
|
|
|
|
|
|
class RequestAllowEvent : public Runnable {
|
|
|
|
public:
|
|
|
|
RequestAllowEvent(bool allow, ContentPermissionRequestBase* request)
|
|
|
|
: mozilla::Runnable("RequestAllowEvent"),
|
|
|
|
mAllow(allow),
|
|
|
|
mRequest(request) {}
|
|
|
|
|
2019-03-20 21:05:09 +03:00
|
|
|
// Not MOZ_CAN_RUN_SCRIPT because we can't annotate the thing we override yet.
|
|
|
|
MOZ_CAN_RUN_SCRIPT_BOUNDARY
|
2018-11-14 08:33:25 +03:00
|
|
|
NS_IMETHOD Run() override {
|
2019-03-20 21:05:09 +03:00
|
|
|
// MOZ_KnownLive is OK, because we never drop the ref to mRequest.
|
2018-11-14 08:33:25 +03:00
|
|
|
if (mAllow) {
|
2019-03-20 21:05:09 +03:00
|
|
|
MOZ_KnownLive(mRequest)->Allow(JS::UndefinedHandleValue);
|
2018-11-14 08:33:25 +03:00
|
|
|
} else {
|
2019-03-20 21:05:09 +03:00
|
|
|
MOZ_KnownLive(mRequest)->Cancel();
|
2018-11-14 08:33:25 +03:00
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
bool mAllow;
|
|
|
|
RefPtr<ContentPermissionRequestBase> mRequest;
|
|
|
|
};
|
|
|
|
|
|
|
|
void ContentPermissionRequestBase::RequestDelayedTask(
|
|
|
|
nsIEventTarget* aTarget,
|
|
|
|
ContentPermissionRequestBase::DelayedTaskType aType) {
|
|
|
|
nsCOMPtr<nsIRunnable> r;
|
|
|
|
switch (aType) {
|
|
|
|
case DelayedTaskType::Allow:
|
|
|
|
r = new RequestAllowEvent(true, this);
|
|
|
|
break;
|
|
|
|
case DelayedTaskType::Deny:
|
|
|
|
r = new RequestAllowEvent(false, this);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
r = new RequestPromptEvent(this, mWindow);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
aTarget->Dispatch(r.forget());
|
|
|
|
}
|
|
|
|
|
2018-11-20 17:40:42 +03:00
|
|
|
nsresult TranslateChoices(
|
|
|
|
JS::HandleValue aChoices,
|
|
|
|
const nsTArray<PermissionRequest>& aPermissionRequests,
|
|
|
|
nsTArray<PermissionChoice>& aTranslatedChoices) {
|
|
|
|
if (aChoices.isNullOrUndefined()) {
|
|
|
|
// No choice is specified.
|
|
|
|
} else if (aChoices.isObject()) {
|
|
|
|
// Iterate through all permission types.
|
|
|
|
for (uint32_t i = 0; i < aPermissionRequests.Length(); ++i) {
|
|
|
|
nsCString type = aPermissionRequests[i].type();
|
|
|
|
|
|
|
|
JS::Rooted<JSObject*> obj(RootingCx(), &aChoices.toObject());
|
2019-02-02 06:24:22 +03:00
|
|
|
// People really shouldn't be passing WindowProxy or Location
|
|
|
|
// objects for the choices here.
|
|
|
|
obj = js::CheckedUnwrapStatic(obj);
|
2018-11-20 17:40:42 +03:00
|
|
|
if (!obj) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
AutoJSAPI jsapi;
|
|
|
|
jsapi.Init();
|
|
|
|
|
|
|
|
JSContext* cx = jsapi.cx();
|
|
|
|
JSAutoRealm ar(cx, obj);
|
|
|
|
|
|
|
|
JS::Rooted<JS::Value> val(cx);
|
|
|
|
|
|
|
|
if (!JS_GetProperty(cx, obj, type.BeginReading(), &val) ||
|
|
|
|
!val.isString()) {
|
|
|
|
// no setting for the permission type, clear exception and skip it
|
|
|
|
jsapi.ClearException();
|
|
|
|
} else {
|
|
|
|
nsAutoJSString choice;
|
|
|
|
if (!choice.init(cx, val)) {
|
|
|
|
jsapi.ClearException();
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
aTranslatedChoices.AppendElement(PermissionChoice(type, choice));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
MOZ_ASSERT(false, "SelectedChoices should be undefined or an JS object");
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-02-10 00:34:40 +04:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|
|
|
|
|
2015-04-14 04:08:00 +03:00
|
|
|
NS_IMPL_ISUPPORTS(
|
|
|
|
nsContentPermissionRequestProxy::nsContentPermissionRequesterProxy,
|
|
|
|
nsIContentPermissionRequester)
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsContentPermissionRequestProxy::nsContentPermissionRequesterProxy ::
|
|
|
|
GetVisibility(nsIContentPermissionRequestCallback* aCallback) {
|
|
|
|
NS_ENSURE_ARG_POINTER(aCallback);
|
|
|
|
|
|
|
|
mGetCallback = aCallback;
|
|
|
|
mWaitGettingResult = true;
|
2015-11-02 08:53:26 +03:00
|
|
|
Unused << mParent->SendGetVisibility();
|
2015-04-14 04:08:00 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsContentPermissionRequestProxy::nsContentPermissionRequesterProxy ::
|
|
|
|
SetOnVisibilityChange(nsIContentPermissionRequestCallback* aCallback) {
|
|
|
|
mOnChangeCallback = aCallback;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsContentPermissionRequestProxy::nsContentPermissionRequesterProxy ::
|
|
|
|
GetOnVisibilityChange(nsIContentPermissionRequestCallback** aCallback) {
|
|
|
|
NS_ENSURE_ARG_POINTER(aCallback);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIContentPermissionRequestCallback> callback = mOnChangeCallback;
|
|
|
|
callback.forget(aCallback);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void nsContentPermissionRequestProxy::nsContentPermissionRequesterProxy ::
|
|
|
|
NotifyVisibilityResult(const bool& aIsVisible) {
|
|
|
|
if (mWaitGettingResult) {
|
|
|
|
MOZ_ASSERT(mGetCallback);
|
|
|
|
mWaitGettingResult = false;
|
|
|
|
mGetCallback->NotifyVisibility(aIsVisible);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mOnChangeCallback) {
|
|
|
|
mOnChangeCallback->NotifyVisibility(aIsVisible);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-16 17:21:46 +03:00
|
|
|
nsContentPermissionRequestProxy::nsContentPermissionRequestProxy(
|
|
|
|
ContentPermissionRequestParent* parent)
|
|
|
|
: mParent(parent) {
|
|
|
|
NS_ASSERTION(mParent, "null parent");
|
2010-09-13 23:49:27 +04:00
|
|
|
}
|
|
|
|
|
2018-11-20 17:40:42 +03:00
|
|
|
nsContentPermissionRequestProxy::~nsContentPermissionRequestProxy() = default;
|
2010-09-13 23:49:27 +04:00
|
|
|
|
2018-06-16 17:21:46 +03:00
|
|
|
nsresult nsContentPermissionRequestProxy::Init(
|
|
|
|
const nsTArray<PermissionRequest>& requests) {
|
2014-02-10 00:34:40 +04:00
|
|
|
mPermissionRequests = requests;
|
2015-04-14 04:08:00 +03:00
|
|
|
mRequester = new nsContentPermissionRequesterProxy(mParent);
|
2010-09-13 23:49:27 +04:00
|
|
|
|
2014-08-01 11:22:20 +04:00
|
|
|
nsCOMPtr<nsIContentPermissionPrompt> prompt =
|
|
|
|
do_GetService(NS_CONTENT_PERMISSION_PROMPT_CONTRACTID);
|
2010-09-13 23:49:27 +04:00
|
|
|
if (!prompt) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
prompt->Prompt(this);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-02-09 22:07:43 +03:00
|
|
|
void nsContentPermissionRequestProxy::OnParentDestroyed() {
|
2015-04-14 04:08:00 +03:00
|
|
|
mRequester = nullptr;
|
2012-07-30 18:20:58 +04:00
|
|
|
mParent = nullptr;
|
2011-02-09 22:07:43 +03:00
|
|
|
}
|
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS(nsContentPermissionRequestProxy, nsIContentPermissionRequest)
|
2010-09-13 23:49:27 +04:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-02-10 00:34:40 +04:00
|
|
|
nsContentPermissionRequestProxy::GetTypes(nsIArray** aTypes) {
|
|
|
|
nsCOMPtr<nsIMutableArray> types = do_CreateInstance(NS_ARRAY_CONTRACTID);
|
2014-08-01 11:22:20 +04:00
|
|
|
if (mozilla::dom::nsContentPermissionUtils::ConvertPermissionRequestToArray(
|
|
|
|
mPermissionRequests, types)) {
|
2014-02-10 00:34:40 +04:00
|
|
|
types.forget(aTypes);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
return NS_ERROR_FAILURE;
|
2012-11-14 04:06:42 +04:00
|
|
|
}
|
|
|
|
|
2010-09-13 23:49:27 +04:00
|
|
|
NS_IMETHODIMP
|
2016-01-30 20:05:36 +03:00
|
|
|
nsContentPermissionRequestProxy::GetWindow(mozIDOMWindow** aRequestingWindow) {
|
2010-09-13 23:49:27 +04:00
|
|
|
NS_ENSURE_ARG_POINTER(aRequestingWindow);
|
2012-07-30 18:20:58 +04:00
|
|
|
*aRequestingWindow = nullptr; // ipc doesn't have a window
|
2010-09-13 23:49:27 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-07-30 18:58:26 +04:00
|
|
|
nsContentPermissionRequestProxy::GetPrincipal(
|
|
|
|
nsIPrincipal** aRequestingPrincipal) {
|
|
|
|
NS_ENSURE_ARG_POINTER(aRequestingPrincipal);
|
|
|
|
if (mParent == nullptr) {
|
2010-09-13 23:49:27 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
2012-07-30 18:58:26 +04:00
|
|
|
}
|
2010-09-13 23:49:27 +04:00
|
|
|
|
2012-07-30 18:58:26 +04:00
|
|
|
NS_ADDREF(*aRequestingPrincipal = mParent->mPrincipal);
|
2010-09-13 23:49:27 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2018-11-20 04:52:47 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsContentPermissionRequestProxy::GetTopLevelPrincipal(
|
|
|
|
nsIPrincipal** aRequestingPrincipal) {
|
|
|
|
NS_ENSURE_ARG_POINTER(aRequestingPrincipal);
|
|
|
|
if (mParent == nullptr) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_ADDREF(*aRequestingPrincipal = mParent->mTopLevelPrincipal);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2010-09-13 23:49:27 +04:00
|
|
|
NS_IMETHODIMP
|
2018-04-27 06:36:10 +03:00
|
|
|
nsContentPermissionRequestProxy::GetElement(Element** aRequestingElement) {
|
2010-09-13 23:49:27 +04:00
|
|
|
NS_ENSURE_ARG_POINTER(aRequestingElement);
|
2012-07-30 18:58:26 +04:00
|
|
|
if (mParent == nullptr) {
|
2010-09-13 23:49:27 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
2012-07-30 18:58:26 +04:00
|
|
|
}
|
|
|
|
|
2018-04-27 06:36:10 +03:00
|
|
|
nsCOMPtr<Element> elem = mParent->mElement;
|
2013-07-24 03:39:17 +04:00
|
|
|
elem.forget(aRequestingElement);
|
2010-09-13 23:49:27 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2017-02-20 22:46:39 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsContentPermissionRequestProxy::GetIsHandlingUserInput(
|
|
|
|
bool* aIsHandlingUserInput) {
|
|
|
|
NS_ENSURE_ARG_POINTER(aIsHandlingUserInput);
|
|
|
|
if (mParent == nullptr) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
*aIsHandlingUserInput = mParent->mIsHandlingUserInput;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2019-04-18 16:43:05 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsContentPermissionRequestProxy::GetUserHadInteractedWithDocument(
|
|
|
|
bool* aUserHadInteractedWithDocument) {
|
|
|
|
NS_ENSURE_ARG_POINTER(aUserHadInteractedWithDocument);
|
|
|
|
if (mParent == nullptr) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
*aUserHadInteractedWithDocument = mParent->mUserHadInteractedWithDocument;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsContentPermissionRequestProxy::GetDocumentDOMContentLoadedTimestamp(
|
|
|
|
DOMTimeStamp* aDocumentDOMContentLoadedTimestamp) {
|
|
|
|
NS_ENSURE_ARG_POINTER(aDocumentDOMContentLoadedTimestamp);
|
|
|
|
if (mParent == nullptr) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
*aDocumentDOMContentLoadedTimestamp =
|
|
|
|
mParent->mDocumentDOMContentLoadedTimestamp;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2010-09-13 23:49:27 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsContentPermissionRequestProxy::Cancel() {
|
2012-07-30 18:58:26 +04:00
|
|
|
if (mParent == nullptr) {
|
2010-09-13 23:49:27 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
2012-07-30 18:58:26 +04:00
|
|
|
}
|
|
|
|
|
2013-10-17 02:25:50 +04:00
|
|
|
// Don't send out the delete message when the managing protocol (PBrowser) is
|
|
|
|
// being destroyed and PContentPermissionRequest will soon be.
|
|
|
|
if (mParent->IsBeingDestroyed()) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2013-09-10 19:41:59 +04:00
|
|
|
nsTArray<PermissionChoice> emptyChoices;
|
|
|
|
|
2015-11-09 03:55:08 +03:00
|
|
|
Unused << mParent->SendNotifyResult(false, emptyChoices);
|
2010-09-13 23:49:27 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2013-09-10 19:41:59 +04:00
|
|
|
nsContentPermissionRequestProxy::Allow(JS::HandleValue aChoices) {
|
2012-07-30 18:58:26 +04:00
|
|
|
if (mParent == nullptr) {
|
2010-09-13 23:49:27 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
2012-07-30 18:58:26 +04:00
|
|
|
}
|
2013-10-17 02:25:50 +04:00
|
|
|
|
|
|
|
// Don't send out the delete message when the managing protocol (PBrowser) is
|
|
|
|
// being destroyed and PContentPermissionRequest will soon be.
|
|
|
|
if (mParent->IsBeingDestroyed()) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2013-09-10 19:41:59 +04:00
|
|
|
nsTArray<PermissionChoice> choices;
|
2018-11-20 17:40:42 +03:00
|
|
|
nsresult rv = TranslateChoices(aChoices, mPermissionRequests, choices);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
2013-09-10 19:41:59 +04:00
|
|
|
}
|
|
|
|
|
2015-11-09 03:55:08 +03:00
|
|
|
Unused << mParent->SendNotifyResult(true, choices);
|
2010-09-13 23:49:27 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2014-07-08 08:45:23 +04:00
|
|
|
|
2015-04-14 04:08:00 +03:00
|
|
|
void nsContentPermissionRequestProxy::NotifyVisibility(const bool& aIsVisible) {
|
|
|
|
MOZ_ASSERT(mRequester);
|
|
|
|
|
|
|
|
mRequester->NotifyVisibilityResult(aIsVisible);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsContentPermissionRequestProxy::GetRequester(
|
|
|
|
nsIContentPermissionRequester** aRequester) {
|
|
|
|
NS_ENSURE_ARG_POINTER(aRequester);
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<nsContentPermissionRequesterProxy> requester = mRequester;
|
2015-04-14 04:08:00 +03:00
|
|
|
requester.forget(aRequester);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-07-08 08:45:23 +04:00
|
|
|
// RemotePermissionRequest
|
|
|
|
|
2015-04-14 04:08:00 +03:00
|
|
|
NS_IMPL_ISUPPORTS(RemotePermissionRequest, nsIContentPermissionRequestCallback);
|
2014-07-08 08:45:23 +04:00
|
|
|
|
|
|
|
RemotePermissionRequest::RemotePermissionRequest(
|
|
|
|
nsIContentPermissionRequest* aRequest, nsPIDOMWindowInner* aWindow)
|
|
|
|
: mRequest(aRequest), mWindow(aWindow), mIPCOpen(false), mDestroyed(false) {
|
2015-04-14 04:08:00 +03:00
|
|
|
mListener = new VisibilityChangeListener(mWindow);
|
|
|
|
mListener->SetCallback(this);
|
2014-07-08 08:45:23 +04:00
|
|
|
}
|
|
|
|
|
2016-01-22 21:28:33 +03:00
|
|
|
RemotePermissionRequest::~RemotePermissionRequest() {
|
|
|
|
MOZ_ASSERT(
|
|
|
|
!mIPCOpen,
|
|
|
|
"Protocol must not be open when RemotePermissionRequest is destroyed.");
|
|
|
|
}
|
|
|
|
|
2014-08-01 11:22:20 +04:00
|
|
|
void RemotePermissionRequest::DoCancel() {
|
2014-07-08 08:45:23 +04:00
|
|
|
NS_ASSERTION(mRequest, "We need a request");
|
2019-03-20 21:05:09 +03:00
|
|
|
nsCOMPtr<nsIContentPermissionRequest> request(mRequest);
|
|
|
|
request->Cancel();
|
2014-07-08 08:45:23 +04:00
|
|
|
}
|
|
|
|
|
2014-08-01 11:22:20 +04:00
|
|
|
void RemotePermissionRequest::DoAllow(JS::HandleValue aChoices) {
|
2014-07-08 08:45:23 +04:00
|
|
|
NS_ASSERTION(mRequest, "We need a request");
|
2019-03-20 21:05:09 +03:00
|
|
|
nsCOMPtr<nsIContentPermissionRequest> request(mRequest);
|
|
|
|
request->Allow(aChoices);
|
2014-07-08 08:45:23 +04:00
|
|
|
}
|
|
|
|
|
2014-08-01 11:22:20 +04:00
|
|
|
// PContentPermissionRequestChild
|
2015-11-09 03:55:08 +03:00
|
|
|
mozilla::ipc::IPCResult RemotePermissionRequest::RecvNotifyResult(
|
|
|
|
const bool& aAllow, InfallibleTArray<PermissionChoice>&& aChoices) {
|
|
|
|
Destroy();
|
2015-04-14 04:08:00 +03:00
|
|
|
|
2014-07-08 08:45:23 +04:00
|
|
|
if (aAllow && mWindow->IsCurrentInnerWindow()) {
|
2014-08-01 11:22:20 +04:00
|
|
|
// Use 'undefined' if no choice is provided.
|
|
|
|
if (aChoices.IsEmpty()) {
|
|
|
|
DoAllow(JS::UndefinedHandleValue);
|
2016-11-15 06:26:00 +03:00
|
|
|
return IPC_OK();
|
2014-08-01 11:22:20 +04:00
|
|
|
}
|
|
|
|
|
2014-07-08 08:45:23 +04:00
|
|
|
// Convert choices to a JS val if any.
|
|
|
|
// {"type1": "choice1", "type2": "choiceA"}
|
|
|
|
AutoJSAPI jsapi;
|
|
|
|
if (NS_WARN_IF(!jsapi.Init(mWindow))) {
|
2016-11-15 06:26:00 +03:00
|
|
|
return IPC_OK(); // This is not an IPC error.
|
2014-07-08 08:45:23 +04:00
|
|
|
}
|
2014-08-01 11:22:20 +04:00
|
|
|
|
2014-07-08 08:45:23 +04:00
|
|
|
JSContext* cx = jsapi.cx();
|
|
|
|
JS::Rooted<JSObject*> obj(cx);
|
2015-01-24 18:38:08 +03:00
|
|
|
obj = JS_NewPlainObject(cx);
|
2014-07-08 08:45:23 +04:00
|
|
|
for (uint32_t i = 0; i < aChoices.Length(); ++i) {
|
|
|
|
const nsString& choice = aChoices[i].choice();
|
|
|
|
const nsCString& type = aChoices[i].type();
|
|
|
|
JS::Rooted<JSString*> jChoice(
|
|
|
|
cx, JS_NewUCStringCopyN(cx, choice.get(), choice.Length()));
|
|
|
|
JS::Rooted<JS::Value> vChoice(cx, StringValue(jChoice));
|
|
|
|
if (!JS_SetProperty(cx, obj, type.get(), vChoice)) {
|
2016-11-15 06:26:00 +03:00
|
|
|
return IPC_FAIL_NO_REASON(this);
|
2014-07-08 08:45:23 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
JS::RootedValue val(cx, JS::ObjectValue(*obj));
|
2014-08-01 11:22:20 +04:00
|
|
|
DoAllow(val);
|
2014-07-08 08:45:23 +04:00
|
|
|
} else {
|
2014-08-01 11:22:20 +04:00
|
|
|
DoCancel();
|
2014-07-08 08:45:23 +04:00
|
|
|
}
|
2016-11-15 06:26:00 +03:00
|
|
|
return IPC_OK();
|
2014-07-08 08:45:23 +04:00
|
|
|
}
|
2015-04-14 04:08:00 +03:00
|
|
|
|
|
|
|
mozilla::ipc::IPCResult RemotePermissionRequest::RecvGetVisibility() {
|
|
|
|
nsCOMPtr<nsIDocShell> docshell = mWindow->GetDocShell();
|
|
|
|
if (!docshell) {
|
2016-11-15 06:26:00 +03:00
|
|
|
return IPC_FAIL_NO_REASON(this);
|
2015-04-14 04:08:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool isActive = false;
|
|
|
|
docshell->GetIsActive(&isActive);
|
2015-11-02 08:53:26 +03:00
|
|
|
Unused << SendNotifyVisibility(isActive);
|
2016-11-15 06:26:00 +03:00
|
|
|
return IPC_OK();
|
2015-04-14 04:08:00 +03:00
|
|
|
}
|
|
|
|
|
2015-11-09 03:55:08 +03:00
|
|
|
void RemotePermissionRequest::Destroy() {
|
|
|
|
if (!IPCOpen()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
Unused << this->SendDestroy();
|
|
|
|
mListener->RemoveListener();
|
|
|
|
mListener = nullptr;
|
|
|
|
mDestroyed = true;
|
|
|
|
}
|
|
|
|
|
2015-04-14 04:08:00 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
RemotePermissionRequest::NotifyVisibility(bool isVisible) {
|
2015-11-09 03:55:08 +03:00
|
|
|
if (!IPCOpen()) {
|
2015-04-14 04:08:00 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-11-02 08:53:26 +03:00
|
|
|
Unused << SendNotifyVisibility(isVisible);
|
2015-04-14 04:08:00 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|