Bug 1835059 - [Cocoa] Make clipboard supports all operations on kSelectionCache type; r=mstange

There is no real usage so far, but this could make clipbord code and test more generic.

Depends on D180000

Differential Revision: https://phabricator.services.mozilla.com/D181568
This commit is contained in:
Edgar Chen 2023-06-27 14:29:03 +00:00
parent 504d1f1dbb
commit 3e0be37306
4 changed files with 64 additions and 51 deletions

View File

@ -304,12 +304,14 @@ nsresult nsClipboard::TransferableFromPasteboard(nsITransferable* aTransferable,
if (dest) CFRelease(dest);
if (source) CFRelease(source);
if (successfullyConverted)
if (successfullyConverted) {
// XXX Maybe try to fill in more types? Is there a point?
break;
else
} else {
continue;
}
}
}
return NS_OK;
@ -321,13 +323,11 @@ nsClipboard::GetNativeClipboardData(nsITransferable* aTransferable, int32_t aWhi
NS_OBJC_BEGIN_TRY_BLOCK_RETURN;
MOZ_DIAGNOSTIC_ASSERT(aTransferable);
// XXX we only support the set operation on kSelectionCache type, see bug 1835059.
MOZ_DIAGNOSTIC_ASSERT(kSelectionCache != aWhichClipboard);
MOZ_DIAGNOSTIC_ASSERT(nsIClipboard::IsClipboardTypeSupported(aWhichClipboard));
NSPasteboard* cocoaPasteboard = GetPasteboard(aWhichClipboard);
if (!cocoaPasteboard) {
return NS_ERROR_FAILURE;
if (kSelectionCache == aWhichClipboard) {
if (!sSelectionCache) {
return NS_OK;
}
// get flavor list that includes all acceptable flavors (including ones obtained through
@ -338,7 +338,25 @@ nsClipboard::GetNativeClipboardData(nsITransferable* aTransferable, int32_t aWhi
return NS_ERROR_FAILURE;
}
return nsClipboard::TransferableFromPasteboard(aTransferable, cocoaPasteboard);
for (const auto& flavor : flavors) {
nsCOMPtr<nsISupports> dataSupports;
rv = sSelectionCache->GetTransferData(flavor.get(), getter_AddRefs(dataSupports));
if (NS_SUCCEEDED(rv)) {
CLIPBOARD_LOG("%s: getting %s from cache.", __FUNCTION__, flavor.get());
aTransferable->SetTransferData(flavor.get(), dataSupports);
// XXX Maybe try to fill in more types? Is there a point?
break;
}
}
return NS_OK;
}
NSPasteboard* cocoaPasteboard = GetPasteboard(aWhichClipboard);
if (!cocoaPasteboard) {
return NS_ERROR_FAILURE;
}
return TransferableFromPasteboard(aTransferable, cocoaPasteboard);
NS_OBJC_END_TRY_BLOCK_RETURN(NS_ERROR_FAILURE);
}
@ -348,10 +366,36 @@ mozilla::Result<bool, nsresult> nsClipboard::HasNativeClipboardDataMatchingFlavo
const nsTArray<nsCString>& aFlavorList, int32_t aWhichClipboard) {
NS_OBJC_BEGIN_TRY_BLOCK_RETURN;
// XXX we only support the set operation on kSelectionCache type, see bug 1835059.
MOZ_DIAGNOSTIC_ASSERT(kSelectionCache != aWhichClipboard);
MOZ_DIAGNOSTIC_ASSERT(nsIClipboard::IsClipboardTypeSupported(aWhichClipboard));
if (kSelectionCache == aWhichClipboard) {
nsTArray<nsCString> transferableFlavors;
if (sSelectionCache &&
NS_SUCCEEDED(sSelectionCache->FlavorsTransferableCanImport(transferableFlavors))) {
if (CLIPBOARD_LOG_ENABLED()) {
CLIPBOARD_LOG(" SelectionCache types (nums %zu)\n", transferableFlavors.Length());
for (const auto& transferableFlavor : transferableFlavors) {
CLIPBOARD_LOG(" MIME %s", transferableFlavor.get());
}
}
for (const auto& transferableFlavor : transferableFlavors) {
for (const auto& flavor : aFlavorList) {
if (transferableFlavor.Equals(flavor)) {
CLIPBOARD_LOG(" has %s", flavor.get());
return true;
}
}
}
}
if (CLIPBOARD_LOG_ENABLED()) {
CLIPBOARD_LOG(" no targets at clipboard (bad match)\n");
}
return false;
}
NSPasteboard* cocoaPasteboard = GetPasteboard(aWhichClipboard);
MOZ_ASSERT(cocoaPasteboard);
if (CLIPBOARD_LOG_ENABLED()) {

View File

@ -212,15 +212,6 @@ NS_IMETHODIMP nsBaseClipboard::GetData(nsITransferable* aTransferable,
return NS_ERROR_FAILURE;
}
// XXX as of now, we only support the set operation on kSelectionCache type,
// should we also support the get operation? See also bug 1835059.
if (kSelectionCache == aWhichClipboard ||
!nsIClipboard::IsClipboardTypeSupported(aWhichClipboard)) {
CLIPBOARD_LOG("%s: clipboard %d is not supported.", __FUNCTION__,
aWhichClipboard);
return NS_ERROR_FAILURE;
}
if (mozilla::StaticPrefs::widget_clipboard_use_cached_data_enabled()) {
// If we were the last ones to put something on the navtive clipboard, then
// just use the cached transferable. Otherwise clear it because it isn't
@ -304,16 +295,6 @@ nsBaseClipboard::HasDataMatchingFlavors(const nsTArray<nsCString>& aFlavorList,
*aOutResult = false;
// XXX as of now, we only support the set operation on kSelectionCache type,
// should we also support the get operation? See also bug 1835059.
if (kSelectionCache == aWhichClipboard ||
!nsIClipboard::IsClipboardTypeSupported(aWhichClipboard)) {
CLIPBOARD_LOG("%s: clipboard %d is not supported.", __FUNCTION__,
aWhichClipboard);
// XXX should we return a error instead?
return NS_OK;
}
if (mozilla::StaticPrefs::widget_clipboard_use_cached_data_enabled()) {
if (auto* clipboardCache = GetClipboardCacheIfValid(aWhichClipboard)) {
MOZ_ASSERT(clipboardCache->GetTransferable());

View File

@ -81,27 +81,19 @@ https://bugzilla.mozilla.org/show_bug.cgi?id=948065
// Test hasDataMatchingFlavors
try {
// We only support the set operation on kSelectionCache type, see
// bug 1835059.
is(clipboard.hasDataMatchingFlavors(['text/plain'], clipboardType),
clipboardType != clipboard.kSelectionCache,
ok(clipboard.hasDataMatchingFlavors(['text/plain'], clipboardType),
`Test hasDataMatchingFlavors for clipboard type ${clipboardType}`);
} catch(e) {
ok(false, `hasDataMatchingFlavors should not throw error for clipboard type ${clipboardType}`);
}
// Test getData
let isThrow = false;
try {
is(getClipboardData('text/plain', clipboardType), str,
`Test getData for clipboard type ${clipboardType}`);
} catch(e) {
isThrow = true;
ok(false, `getData should not throw error for clipboard type ${clipboardType}`);
}
// We only support the set operation on kSelectionCache type, see
// bug 1835059.
is(isThrow, clipboardType == clipboard.kSelectionCache,
`Test if getData throw an error for clipboard type ${clipboardType}`);
});
}
});

View File

@ -46,8 +46,7 @@ function testClipboardCache(aClipboardType, aAsync) {
info(`Should not get the data from other clipboard type`);
clipboardTypes.forEach(function(otherType) {
if (otherType != clipboard.kSelectionCache &&
otherType != aClipboardType &&
if (otherType != aClipboardType &&
clipboard.isClipboardTypeSupported(otherType)) {
is(getClipboardData("text/plain", otherType), null,
`Check text/plain data on clipboard ${otherType}`);
@ -76,9 +75,7 @@ add_setup(function init() {
});
clipboardTypes.forEach(function(type) {
// Selection cache clipboard, which is available only on Cocoa widget, supports
// set operation only, see bug 1835059.
if (clipboard.kSelectionCache == type || !clipboard.isClipboardTypeSupported(type)) {
if (!clipboard.isClipboardTypeSupported(type)) {
return;
}
@ -102,8 +99,7 @@ clipboardTypes.forEach(function(type) {
// Check other clipboard types.
clipboardTypes.forEach(function(otherType) {
if (otherType != clipboard.kSelectionCache &&
otherType != type &&
if (otherType != type &&
clipboard.isClipboardTypeSupported(otherType)) {
ok(!clipboard.hasDataMatchingFlavors(["text/plain"], otherType),
`Check if there is text/plain flavor on clipboard ${otherType}`);