Bug 1794268 - Use WindowContext to check if drag source and drop target are in the same tab; r=webidl,smaug

Differential Revision: https://phabricator.services.mozilla.com/D163981
This commit is contained in:
Edgar Chen 2022-12-08 23:37:46 +00:00
Родитель 9ee7b343e3
Коммит fa88384e7d
4 изменённых файлов: 29 добавлений и 18 удалений

Просмотреть файл

@ -247,30 +247,19 @@ ContentAreaDropListener.prototype = {
return true;
}
let sourceNode = dataTransfer.mozSourceNode;
if (!sourceNode) {
// If this is an external drag, allow drop.
let sourceWC = dataTransfer.sourceWindowContext;
if (!sourceWC) {
return true;
}
// don't allow a drop of a node from the same document onto this one
let sourceDocument = sourceNode.ownerDocument;
let eventDocument = aEvent.originalTarget.ownerDocument;
if (sourceDocument == eventDocument) {
// If drag source and drop target are in the same top window, don't allow.
let eventWC =
aEvent.originalTarget.ownerGlobal.browsingContext.currentWindowContext;
if (eventWC && sourceWC.topWindowContext == eventWC.topWindowContext) {
return false;
}
// also check for nodes in other child or sibling frames by checking
// if both have the same top window.
if (sourceDocument && eventDocument) {
if (sourceDocument.defaultView == null) {
return true;
}
let sourceRoot = sourceDocument.defaultView.top;
if (sourceRoot && sourceRoot == eventDocument.defaultView.top) {
return false;
}
}
return true;
},

Просмотреть файл

@ -42,6 +42,7 @@
#include "mozilla/dom/BindingUtils.h"
#include "mozilla/dom/OSFileSystem.h"
#include "mozilla/dom/Promise.h"
#include "mozilla/dom/WindowContext.h"
#include "mozilla/Unused.h"
#include "nsComponentManagerUtils.h"
#include "nsNetUtil.h"
@ -437,6 +438,17 @@ already_AddRefed<nsINode> DataTransfer::GetMozSourceNode() {
return sourceNode.forget();
}
already_AddRefed<WindowContext> DataTransfer::GetSourceWindowContext() {
nsCOMPtr<nsIDragSession> dragSession = nsContentUtils::GetDragSession();
if (!dragSession) {
return nullptr;
}
RefPtr<WindowContext> sourceWindowContext;
dragSession->GetSourceWindowContext(getter_AddRefs(sourceWindowContext));
return sourceWindowContext.forget();
}
already_AddRefed<DOMStringList> DataTransfer::MozTypesAt(
uint32_t aIndex, ErrorResult& aRv) const {
// Only the first item is valid for clipboard events

Просмотреть файл

@ -42,6 +42,7 @@ class FileList;
class Promise;
template <typename T>
class Optional;
class WindowContext;
#define NS_DATATRANSFER_IID \
{ \
@ -253,6 +254,8 @@ class DataTransfer final : public nsISupports, public nsWrapperCache {
already_AddRefed<nsINode> GetMozSourceNode();
already_AddRefed<WindowContext> GetSourceWindowContext();
/*
* Integer version of dropEffect, set to one of the constants in
* nsIDragService.

Просмотреть файл

@ -151,6 +151,13 @@ partial interface DataTransfer {
[UseCounter]
readonly attribute Node? mozSourceNode;
/**
* The window context that mouse was pressed over to begin the drag. For
* external drags, this will be null.
*/
[ChromeOnly]
readonly attribute WindowContext? sourceWindowContext;
/**
* The URI spec of the triggering principal. This may be different than
* sourceNode's principal when sourceNode is xul:browser and the drag is