From 40a355a42d4a9444dc753c04c6608dade2f06a23 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Fri, 19 Apr 2024 03:13:27 +0200 Subject: Adding upstream version 125.0.1. Signed-off-by: Daniel Baumann --- dom/events/Clipboard.cpp | 84 ++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 71 insertions(+), 13 deletions(-) (limited to 'dom/events/Clipboard.cpp') diff --git a/dom/events/Clipboard.cpp b/dom/events/Clipboard.cpp index 560002dd68..b163bc816f 100644 --- a/dom/events/Clipboard.cpp +++ b/dom/events/Clipboard.cpp @@ -29,6 +29,7 @@ #include "nsArrayUtils.h" #include "nsComponentManagerUtils.h" #include "nsContentUtils.h" +#include "nsGlobalWindowInner.h" #include "nsIClipboard.h" #include "nsIInputStream.h" #include "nsIParserUtils.h" @@ -86,6 +87,16 @@ class ClipboardGetCallback : public nsIAsyncClipboardGetCallback { RefPtr mPromise; }; +static nsTArray MandatoryDataTypesAsCStrings() { + // Mandatory data types defined in + // https://w3c.github.io/clipboard-apis/#mandatory-data-types-x. The types + // should be in the same order as kNonPlainTextExternalFormats in + // DataTransfer. + return nsTArray{nsLiteralCString(kHTMLMime), + nsLiteralCString(kTextMime), + nsLiteralCString(kPNGImageMime)}; +} + class ClipboardGetCallbackForRead final : public ClipboardGetCallback { public: explicit ClipboardGetCallbackForRead(nsIGlobalObject* aGlobal, @@ -109,11 +120,15 @@ class ClipboardGetCallbackForRead final : public ClipboardGetCallback { } AutoTArray, 3> entries; - for (const auto& format : flavorList) { - auto entry = MakeRefPtr( - mGlobal, NS_ConvertUTF8toUTF16(format)); - entry->LoadDataFromSystemClipboard(aAsyncGetClipboardData); - entries.AppendElement(std::move(entry)); + // We might reuse the request from DataTransfer created for paste event, + // which could contain more types that are not in the mandatory list. + for (const auto& format : MandatoryDataTypesAsCStrings()) { + if (flavorList.Contains(format)) { + auto entry = MakeRefPtr( + mGlobal, NS_ConvertUTF8toUTF16(format)); + entry->LoadDataFromSystemClipboard(aAsyncGetClipboardData); + entries.AppendElement(std::move(entry)); + } } RefPtr p(std::move(mPromise)); @@ -214,6 +229,36 @@ NS_IMPL_ISUPPORTS(ClipboardGetCallbackForReadText, nsIAsyncClipboardGetCallback, } // namespace +void Clipboard::RequestRead(Promise& aPromise, const ReadRequestType& aType, + nsPIDOMWindowInner& aOwner, + nsIPrincipal& aSubjectPrincipal, + nsIAsyncGetClipboardData& aRequest) { +#ifdef DEBUG + bool isValid = false; + MOZ_ASSERT(NS_SUCCEEDED(aRequest.GetValid(&isValid)) && isValid); +#endif + + RefPtr callback; + switch (aType) { + case ReadRequestType::eRead: { + callback = + MakeRefPtr(aOwner.AsGlobal(), &aPromise); + break; + } + case ReadRequestType::eReadText: { + callback = MakeRefPtr(&aPromise); + break; + } + default: { + MOZ_ASSERT_UNREACHABLE("Unknown read type"); + return; + } + } + + MOZ_ASSERT(callback); + callback->OnSuccess(&aRequest); +} + void Clipboard::RequestRead(Promise* aPromise, ReadRequestType aType, nsPIDOMWindowInner* aOwner, nsIPrincipal& aPrincipal) { @@ -239,19 +284,14 @@ void Clipboard::RequestRead(Promise* aPromise, ReadRequestType aType, callback = MakeRefPtr(global, std::move(p)); rv = clipboardService->AsyncGetData( - // Mandatory data types defined in - // https://w3c.github.io/clipboard-apis/#mandatory-data-types-x - AutoTArray{nsDependentCString(kHTMLMime), - nsDependentCString(kTextMime), - nsDependentCString(kPNGImageMime)}, - nsIClipboard::kGlobalClipboard, owner->GetWindowContext(), - &aPrincipal, callback); + MandatoryDataTypesAsCStrings(), nsIClipboard::kGlobalClipboard, + owner->GetWindowContext(), &aPrincipal, callback); break; } case ReadRequestType::eReadText: { callback = MakeRefPtr(std::move(p)); rv = clipboardService->AsyncGetData( - AutoTArray{nsDependentCString(kTextMime)}, + AutoTArray{nsLiteralCString(kTextMime)}, nsIClipboard::kGlobalClipboard, owner->GetWindowContext(), &aPrincipal, callback); break; @@ -288,6 +328,24 @@ already_AddRefed Clipboard::ReadHelper(nsIPrincipal& aSubjectPrincipal, return p.forget(); } + // If a "paste" clipboard event is actively being processed, we're + // intentionally skipping permission/user-activation checks and giving the + // webpage access to the clipboard. + if (RefPtr dataTransfer = + nsGlobalWindowInner::Cast(owner)->GetCurrentPasteDataTransfer()) { + // If there is valid nsIAsyncGetClipboardData, use it directly. + if (nsCOMPtr asyncGetClipboardData = + dataTransfer->GetAsyncGetClipboardData()) { + bool isValid = false; + asyncGetClipboardData->GetValid(&isValid); + if (isValid) { + RequestRead(*p, aType, *owner, aSubjectPrincipal, + *asyncGetClipboardData); + return p.forget(); + } + } + } + if (IsTestingPrefEnabledOrHasReadPermission(aSubjectPrincipal)) { MOZ_LOG(GetClipboardLog(), LogLevel::Debug, ("%s: testing pref enabled or has read permission", __FUNCTION__)); -- cgit v1.2.3