From fbaf0bb26397aa498eb9156f06d5a6fe34dd7dd8 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Fri, 19 Apr 2024 03:14:29 +0200 Subject: Merging upstream version 125.0.1. Signed-off-by: Daniel Baumann --- widget/nsBaseClipboard.cpp | 142 +++++++++++++++++++++++++++++++++------------ 1 file changed, 105 insertions(+), 37 deletions(-) (limited to 'widget/nsBaseClipboard.cpp') diff --git a/widget/nsBaseClipboard.cpp b/widget/nsBaseClipboard.cpp index 40af1de388..333d154e70 100644 --- a/widget/nsBaseClipboard.cpp +++ b/widget/nsBaseClipboard.cpp @@ -812,56 +812,124 @@ NS_IMETHODIMP nsBaseClipboard::AsyncGetData( return NS_OK; } -void nsBaseClipboard::AsyncGetDataInternal( +already_AddRefed +nsBaseClipboard::MaybeCreateGetRequestFromClipboardCache( const nsTArray& aFlavorList, int32_t aClipboardType, - mozilla::dom::WindowContext* aRequestingWindowContext, - nsIAsyncClipboardGetCallback* aCallback) { - MOZ_ASSERT(nsIClipboard::IsClipboardTypeSupported(aClipboardType)); + mozilla::dom::WindowContext* aRequestingWindowContext) { + MOZ_DIAGNOSTIC_ASSERT(nsIClipboard::IsClipboardTypeSupported(aClipboardType)); - if (mozilla::StaticPrefs::widget_clipboard_use_cached_data_enabled()) { - // If we were the last ones to put something on the native clipboard, then - // just use the cached transferable. Otherwise clear it because it isn't - // relevant any more. - if (auto* clipboardCache = GetClipboardCacheIfValid(aClipboardType)) { - nsITransferable* cachedTransferable = clipboardCache->GetTransferable(); - MOZ_ASSERT(cachedTransferable); - - nsTArray transferableFlavors; - if (NS_SUCCEEDED(cachedTransferable->FlavorsTransferableCanExport( - transferableFlavors))) { - nsTArray results; - for (const auto& transferableFlavor : transferableFlavors) { - for (const auto& flavor : aFlavorList) { - // XXX We need special check for image as we always put the - // image as "native" on the clipboard. - if (transferableFlavor.Equals(flavor) || - (transferableFlavor.Equals(kNativeImageMime) && - nsContentUtils::IsFlavorImage(flavor))) { - MOZ_CLIPBOARD_LOG(" has %s", flavor.get()); - results.AppendElement(flavor); - } - } - } + if (!mozilla::StaticPrefs::widget_clipboard_use_cached_data_enabled()) { + return nullptr; + } + + // If we were the last ones to put something on the native clipboard, then + // just use the cached transferable. Otherwise clear it because it isn't + // relevant any more. + ClipboardCache* clipboardCache = GetClipboardCacheIfValid(aClipboardType); + if (!clipboardCache) { + return nullptr; + } + + nsITransferable* cachedTransferable = clipboardCache->GetTransferable(); + MOZ_ASSERT(cachedTransferable); + + nsTArray transferableFlavors; + if (NS_FAILED(cachedTransferable->FlavorsTransferableCanExport( + transferableFlavors))) { + return nullptr; + } - // XXX Do we need to check system clipboard for the flavors that cannot - // be found in cache? - auto asyncGetClipboardData = mozilla::MakeRefPtr( - aClipboardType, clipboardCache->GetSequenceNumber(), - std::move(results), true, this, aRequestingWindowContext); - aCallback->OnSuccess(asyncGetClipboardData); - return; + nsTArray results; + for (const auto& flavor : aFlavorList) { + for (const auto& transferableFlavor : transferableFlavors) { + // XXX We need special check for image as we always put the + // image as "native" on the clipboard. + if (transferableFlavor.Equals(flavor) || + (transferableFlavor.Equals(kNativeImageMime) && + nsContentUtils::IsFlavorImage(flavor))) { + MOZ_CLIPBOARD_LOG(" has %s", flavor.get()); + results.AppendElement(flavor); } } + } - // At this point we can't satisfy the request from cache data so let's look - // for things other people put on the system clipboard. + // XXX Do we need to check system clipboard for the flavors that cannot + // be found in cache? + return mozilla::MakeAndAddRef( + aClipboardType, clipboardCache->GetSequenceNumber(), std::move(results), + true /* aFromCache */, this, aRequestingWindowContext); +} + +void nsBaseClipboard::AsyncGetDataInternal( + const nsTArray& aFlavorList, int32_t aClipboardType, + mozilla::dom::WindowContext* aRequestingWindowContext, + nsIAsyncClipboardGetCallback* aCallback) { + MOZ_ASSERT(nsIClipboard::IsClipboardTypeSupported(aClipboardType)); + + if (nsCOMPtr asyncGetClipboardData = + MaybeCreateGetRequestFromClipboardCache(aFlavorList, aClipboardType, + aRequestingWindowContext)) { + aCallback->OnSuccess(asyncGetClipboardData); + return; } + // At this point we can't satisfy the request from cache data so let's + // look for things other people put on the system clipboard. MaybeRetryGetAvailableFlavors(aFlavorList, aClipboardType, aCallback, kGetAvailableFlavorsRetryCount, aRequestingWindowContext); } +NS_IMETHODIMP nsBaseClipboard::GetDataSnapshotSync( + const nsTArray& aFlavorList, int32_t aWhichClipboard, + mozilla::dom::WindowContext* aRequestingWindowContext, + nsIAsyncGetClipboardData** _retval) { + MOZ_CLIPBOARD_LOG("%s: clipboard=%d", __FUNCTION__, aWhichClipboard); + + *_retval = nullptr; + + if (aFlavorList.IsEmpty()) { + return NS_ERROR_INVALID_ARG; + } + + if (!nsIClipboard::IsClipboardTypeSupported(aWhichClipboard)) { + MOZ_CLIPBOARD_LOG("%s: clipboard %d is not supported.", __FUNCTION__, + aWhichClipboard); + return NS_ERROR_FAILURE; + } + + if (nsCOMPtr asyncGetClipboardData = + MaybeCreateGetRequestFromClipboardCache(aFlavorList, aWhichClipboard, + aRequestingWindowContext)) { + asyncGetClipboardData.forget(_retval); + return NS_OK; + } + + auto sequenceNumberOrError = + GetNativeClipboardSequenceNumber(aWhichClipboard); + if (sequenceNumberOrError.isErr()) { + MOZ_CLIPBOARD_LOG("%s: unable to get sequence number for clipboard %d.", + __FUNCTION__, aWhichClipboard); + return sequenceNumberOrError.unwrapErr(); + } + + nsTArray results; + for (const auto& flavor : aFlavorList) { + auto resultOrError = HasNativeClipboardDataMatchingFlavors( + AutoTArray{flavor}, aWhichClipboard); + if (resultOrError.isOk() && resultOrError.unwrap()) { + results.AppendElement(flavor); + } + } + + *_retval = + mozilla::MakeAndAddRef( + aWhichClipboard, sequenceNumberOrError.unwrap(), std::move(results), + false /* aFromCache */, this, aRequestingWindowContext) + .take(); + return NS_OK; +} + NS_IMETHODIMP nsBaseClipboard::EmptyClipboard(int32_t aWhichClipboard) { MOZ_CLIPBOARD_LOG("%s: clipboard=%d", __FUNCTION__, aWhichClipboard); -- cgit v1.2.3