Bug 1951225 - Part 3: Get rid of old {Async}GetNativeClipboardData() method; r=gstoll

This doesn't change behavior.

Differential Revision: https://phabricator.services.mozilla.com/D240113
This commit is contained in:
Edgar Chen
2025-05-14 21:35:26 +00:00
committed by echen@mozilla.com
parent 46b9392a2d
commit c78ecfdc0b
3 changed files with 84 additions and 63 deletions

View File

@@ -463,10 +463,26 @@ NS_IMETHODIMP nsBaseClipboard::GetData(
// 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
}
nsresult rv = GetNativeClipboardData(aTransferable, aWhichClipboard);
nsTArray<nsCString> flavors;
nsresult rv = aTransferable->FlavorsTransferableCanImport(flavors);
if (NS_FAILED(rv)) {
return rv;
return NS_ERROR_FAILURE;
}
for (const auto& flavor : flavors) {
auto dataOrError = GetNativeClipboardData(flavor, aWhichClipboard);
if (dataOrError.isErr()) {
continue;
}
if (dataOrError.inspect()) {
aTransferable->SetTransferData(flavor.get(), dataOrError.inspect());
// XXX Maybe try to fill in more types? Is there a point?
break;
}
}
if (!mozilla::contentanalysis::ContentAnalysis::
CheckClipboardContentAnalysisSync(this, aWindowContext->Canonical(),
aTransferable, aWhichClipboard)) {
@@ -850,38 +866,6 @@ nsBaseClipboard::IsClipboardTypeSupported(ClipboardType aWhichClipboard,
}
}
// TODO: This will be removed in a subsequent patch after all platforms
// implement the new version of GetNativeClipboardData().
NS_IMETHODIMP nsBaseClipboard::GetNativeClipboardData(
nsITransferable* aTransferable, ClipboardType aWhichClipboard) {
MOZ_DIAGNOSTIC_ASSERT(aTransferable);
MOZ_DIAGNOSTIC_ASSERT(
nsIClipboard::IsClipboardTypeSupported(aWhichClipboard));
// Get flavor list that includes all acceptable flavors (including ones
// obtained through conversion).
nsTArray<nsCString> flavors;
nsresult rv = aTransferable->FlavorsTransferableCanImport(flavors);
if (NS_FAILED(rv)) {
return NS_ERROR_FAILURE;
}
for (const auto& flavor : flavors) {
auto dataOrError = GetNativeClipboardData(flavor, aWhichClipboard);
if (dataOrError.isErr()) {
continue;
}
if (dataOrError.inspect()) {
aTransferable->SetTransferData(flavor.get(), dataOrError.inspect());
// XXX Maybe try to fill in more types? Is there a point?
break;
}
}
return NS_OK;
}
void nsBaseClipboard::AsyncHasNativeClipboardDataMatchingFlavors(
const nsTArray<nsCString>& aFlavorList, ClipboardType aWhichClipboard,
HasMatchingFlavorsCallback&& aCallback) {
@@ -904,14 +888,6 @@ void nsBaseClipboard::AsyncHasNativeClipboardDataMatchingFlavors(
aCallback(std::move(results));
}
// TODO: This will be removed in a subsequent patch after all platforms
// implement the new version of GetNativeClipboardData().
void nsBaseClipboard::AsyncGetNativeClipboardData(
nsITransferable* aTransferable, ClipboardType aWhichClipboard,
GetDataCallback&& aCallback) {
aCallback(GetNativeClipboardData(aTransferable, aWhichClipboard));
}
void nsBaseClipboard::AsyncGetNativeClipboardData(
const nsACString& aFlavor, ClipboardType aWhichClipboard,
GetNativeDataCallback&& aCallback) {
@@ -1042,6 +1018,10 @@ NS_IMETHODIMP nsBaseClipboard::ClipboardDataSnapshot::GetData(
return rv;
}
if (flavors.IsEmpty()) {
return NS_OK;
}
// If the requested flavor is not in the list, throw an error.
for (const auto& flavor : flavors) {
if (!mFlavors.Contains(flavor)) {
@@ -1091,8 +1071,8 @@ NS_IMETHODIMP nsBaseClipboard::ClipboardDataSnapshot::GetData(
// Since this is an async operation, we need to check if the data is still
// valid after we get the result.
mClipboard->AsyncGetNativeClipboardData(
aTransferable, mClipboardType,
GetDataInternal(
std::move(flavors), 0, aTransferable,
[callback = nsCOMPtr{aCallback}, self = RefPtr{this},
transferable = nsCOMPtr{aTransferable},
contentAnalysisCallback =
@@ -1171,9 +1151,18 @@ NS_IMETHODIMP nsBaseClipboard::ClipboardDataSnapshot::GetDataSync(
// for things other people put on the system clipboard.
}
rv = mClipboard->GetNativeClipboardData(aTransferable, mClipboardType);
if (NS_FAILED(rv)) {
return rv;
for (const auto& flavor : flavors) {
auto dataOrError =
mClipboard->GetNativeClipboardData(flavor, mClipboardType);
if (dataOrError.isErr()) {
continue;
}
if (dataOrError.inspect()) {
aTransferable->SetTransferData(flavor.get(), dataOrError.inspect());
// XXX Maybe try to fill in more types? Is there a point?
break;
}
}
bool shouldAllowContent = mozilla::contentanalysis::ContentAnalysis::
@@ -1222,6 +1211,46 @@ bool nsBaseClipboard::ClipboardDataSnapshot::IsValid() {
return true;
}
void nsBaseClipboard::ClipboardDataSnapshot::GetDataInternal(
nsTArray<nsCString>&& aTypes, nsTArray<nsCString>::index_type aIndex,
nsITransferable* aTransferable, GetDataInternalCallback&& aCallback) {
MOZ_ASSERT(aIndex < aTypes.Length());
// Since this is an async operation, we need to check if the data is still
// valid after we get the result.
nsCString type = aTypes[aIndex];
mClipboard->AsyncGetNativeClipboardData(
type, mClipboardType,
[self = RefPtr{this}, types = std::move(aTypes), index = aIndex,
transferable = nsCOMPtr{aTransferable}, callback = std::move(aCallback)](
mozilla::Result<nsCOMPtr<nsISupports>, nsresult> aResult) mutable {
MOZ_ASSERT(index < types.Length());
// `IsValid()` checks the clipboard sequence number to ensure the data
// we are requesting is still valid.
if (!self->IsValid()) {
callback(NS_ERROR_NOT_AVAILABLE);
return;
}
if (!aResult.isErr() && aResult.inspect()) {
transferable->SetTransferData(types[index].get(), aResult.inspect());
callback(NS_OK);
return;
}
// No more types to try.
if (++index >= types.Length()) {
callback(NS_OK);
return;
}
// Recursively call GetDataInternal to try the next type.
self->GetDataInternal(std::move(types), index, transferable,
std::move(callback));
});
}
NS_IMPL_ISUPPORTS(nsBaseClipboard::ClipboardPopulatedDataSnapshot,
nsIClipboardDataSnapshot)

View File

@@ -76,9 +76,6 @@ class nsBaseClipboard : public nsIClipboard {
mozilla::dom::WindowContext* aRequestingWindowContext,
nsIClipboardGetDataSnapshotCallback* aCallback);
// TODO: This will be removed in a subsequent patch after all platforms
// implement the new version of GetNativeClipboardData().
using GetDataCallback = mozilla::MoveOnlyFunction<void(nsresult)>;
using GetNativeDataCallback = mozilla::MoveOnlyFunction<void(
mozilla::Result<nsCOMPtr<nsISupports>, nsresult>)>;
using HasMatchingFlavorsCallback = mozilla::MoveOnlyFunction<void(
@@ -108,20 +105,9 @@ class nsBaseClipboard : public nsIClipboard {
// Implement the native clipboard behavior.
NS_IMETHOD SetNativeClipboardData(nsITransferable* aTransferable,
ClipboardType aWhichClipboard) = 0;
// TODO: This will be removed in a subsequent patch after all platforms
// implement the new version of GetNativeClipboardData().
NS_IMETHOD GetNativeClipboardData(nsITransferable* aTransferable,
ClipboardType aWhichClipboard);
virtual mozilla::Result<nsCOMPtr<nsISupports>, nsresult>
GetNativeClipboardData(const nsACString& aFlavor,
ClipboardType aWhichClipboard) {
return mozilla::Err(NS_ERROR_NOT_IMPLEMENTED);
}
// TODO: This will be removed in a subsequent patch after all platforms
// implement the new version of AsyncGetNativeClipboardData().
virtual void AsyncGetNativeClipboardData(nsITransferable* aTransferable,
ClipboardType aWhichClipboard,
GetDataCallback&& aCallback);
ClipboardType aWhichClipboard) = 0;
virtual void AsyncGetNativeClipboardData(const nsACString& aFlavor,
ClipboardType aWhichClipboard,
GetNativeDataCallback&& aCallback);
@@ -184,6 +170,12 @@ class nsBaseClipboard : public nsIClipboard {
virtual ~ClipboardDataSnapshot() = default;
bool IsValid();
using GetDataInternalCallback = mozilla::MoveOnlyFunction<void(nsresult)>;
void GetDataInternal(nsTArray<nsCString>&& aTypes,
nsTArray<nsCString>::index_type aIndex,
nsITransferable* aTransferable,
GetDataInternalCallback&& aCallback);
// The clipboard type defined in nsIClipboard.
const nsIClipboard::ClipboardType mClipboardType;
// The sequence number associated with the clipboard content for this

View File

@@ -165,7 +165,7 @@ interface nsIClipboard : nsISupports
/**
* Filters the flavors aTransferable can import (see
* `nsITransferable::flavorsTransferableCanImport`) and gets the data for the
* first flavor. That data is set for aTransferable.
* first available flavor. That data is set for aTransferable.
*
* @param aTransferable The transferable
* @param aWhichClipboard Specifies the clipboard to which this operation applies.