Bug 1810850 - Part 4: Refactor around nsBase::ClipboardEmptyClipboard; r=cmartin,mstange

mEmptyingForSetData flag can be removed since it's used for clearing
cache data without emptying system clipboard and now we could just call
ClipboardCache::Clear().

Depends on D179999

Differential Revision: https://phabricator.services.mozilla.com/D180000
This commit is contained in:
Edgar Chen 2023-06-26 21:02:40 +00:00
parent 1d8c57b7a7
commit 5d10dd9171
6 changed files with 26 additions and 43 deletions

View File

@ -23,9 +23,6 @@ class nsClipboard : public nsBaseClipboard {
NS_DECL_ISUPPORTS_INHERITED
// nsIClipboard
NS_IMETHOD EmptyClipboard(int32_t aWhichClipboard) override;
// On macOS, cache the transferable of the current selection (chrome/content)
// in the parent process. This is needed for the services menu which
// requires synchronous access to the current selection.
@ -46,6 +43,7 @@ class nsClipboard : public nsBaseClipboard {
int32_t aWhichClipboard) override;
NS_IMETHOD GetNativeClipboardData(nsITransferable* aTransferable,
int32_t aWhichClipboard) override;
nsresult EmptyNativeClipboardData(int32_t aWhichClipboard) override;
mozilla::Result<int32_t, nsresult> GetNativeClipboardSequenceNumber(
int32_t aWhichClipboard) override;
mozilla::Result<bool, nsresult> HasNativeClipboardDataMatchingFlavors(
@ -58,10 +56,6 @@ class nsClipboard : public nsBaseClipboard {
virtual ~nsClipboard();
static mozilla::Maybe<uint32_t> FindIndexOfImageFlavor(const nsTArray<nsCString>& aMIMETypes);
int32_t mCachedClipboard = -1;
// Set to the native change count after any modification of the clipboard.
int32_t mChangeCount = 0;
};
#endif // nsClipboard_h_

View File

@ -143,9 +143,6 @@ nsClipboard::SetNativeClipboardData(nsITransferable* aTransferable, nsIClipboard
}
}
mCachedClipboard = aWhichClipboard;
mChangeCount = [cocoaPasteboard changeCount];
return NS_OK;
NS_OBJC_END_TRY_BLOCK_RETURN(NS_ERROR_FAILURE);
@ -684,25 +681,21 @@ NSString* nsClipboard::WrapHtmlForSystemPasteboard(NSString* aString) {
return wrapped;
}
NS_IMETHODIMP
nsClipboard::EmptyClipboard(int32_t aWhichClipboard) {
nsresult nsClipboard::EmptyNativeClipboardData(int32_t aWhichClipboard) {
NS_OBJC_BEGIN_TRY_BLOCK_RETURN;
if (!mEmptyingForSetData) {
if (aWhichClipboard == kSelectionCache) {
ClearSelectionCache();
} else {
if (NSPasteboard* cocoaPasteboard = GetPasteboard(aWhichClipboard)) {
[cocoaPasteboard clearContents];
}
if (mCachedClipboard == aWhichClipboard) {
mCachedClipboard = -1;
mChangeCount = 0;
}
}
MOZ_DIAGNOSTIC_ASSERT(nsIClipboard::IsClipboardTypeSupported(aWhichClipboard));
if (kSelectionCache == aWhichClipboard) {
ClearSelectionCache();
return NS_OK;
}
return nsBaseClipboard::EmptyClipboard(aWhichClipboard);
if (NSPasteboard* cocoaPasteboard = GetPasteboard(aWhichClipboard)) {
[cocoaPasteboard clearContents];
}
return NS_OK;
NS_OBJC_END_TRY_BLOCK_RETURN(NS_ERROR_FAILURE);
}

View File

@ -175,11 +175,7 @@ NS_IMETHODIMP nsBaseClipboard::SetData(nsITransferable* aTransferable,
return NS_OK;
}
mEmptyingForSetData = true;
if (NS_FAILED(EmptyClipboard(aWhichClipboard))) {
CLIPBOARD_LOG("%s: emptying clipboard failed.", __FUNCTION__);
}
mEmptyingForSetData = false;
clipboardCache->Clear();
nsresult rv = NS_ERROR_FAILURE;
if (aTransferable) {
@ -272,12 +268,16 @@ RefPtr<GenericPromise> nsBaseClipboard::AsyncGetData(
}
NS_IMETHODIMP nsBaseClipboard::EmptyClipboard(int32_t aWhichClipboard) {
CLIPBOARD_LOG("%s: clipboard=%i", __FUNCTION__, aWhichClipboard);
CLIPBOARD_LOG("%s: clipboard=%d", __FUNCTION__, aWhichClipboard);
if (!nsIClipboard::IsClipboardTypeSupported(aWhichClipboard)) {
CLIPBOARD_LOG("%s: clipboard %d is not supported.", __FUNCTION__,
aWhichClipboard);
return NS_ERROR_FAILURE;
}
EmptyNativeClipboardData(aWhichClipboard);
if (mIgnoreEmptyNotification) {
MOZ_DIAGNOSTIC_ASSERT(false, "How did we get here?");
return NS_OK;

View File

@ -107,7 +107,7 @@ class nsBaseClipboard : public ClipboardSetDataHelper {
int32_t aWhichClipboard) override final;
NS_IMETHOD GetData(nsITransferable* aTransferable,
int32_t aWhichClipboard) override final;
NS_IMETHOD EmptyClipboard(int32_t aWhichClipboard) override;
NS_IMETHOD EmptyClipboard(int32_t aWhichClipboard) override final;
NS_IMETHOD HasDataMatchingFlavors(const nsTArray<nsCString>& aFlavorList,
int32_t aWhichClipboard,
bool* aOutResult) override final;
@ -125,13 +125,12 @@ class nsBaseClipboard : public ClipboardSetDataHelper {
// Implement the native clipboard behavior.
NS_IMETHOD GetNativeClipboardData(nsITransferable* aTransferable,
int32_t aWhichClipboard) = 0;
virtual nsresult EmptyNativeClipboardData(int32_t aWhichClipboard) = 0;
virtual mozilla::Result<int32_t, nsresult> GetNativeClipboardSequenceNumber(
int32_t aWhichClipboard) = 0;
virtual mozilla::Result<bool, nsresult> HasNativeClipboardDataMatchingFlavors(
const nsTArray<nsCString>& aFlavorList, int32_t aWhichClipboard) = 0;
bool mEmptyingForSetData = false;
private:
class ClipboardCache final {
public:

View File

@ -1341,17 +1341,16 @@ nsClipboard::GetNativeClipboardData(nsITransferable* aTransferable,
return res;
}
NS_IMETHODIMP
nsClipboard::EmptyClipboard(int32_t aWhichClipboard) {
nsresult nsClipboard::EmptyNativeClipboardData(int32_t aWhichClipboard) {
MOZ_DIAGNOSTIC_ASSERT(
nsIClipboard::IsClipboardTypeSupported(aWhichClipboard));
// Some programs such as ZoneAlarm monitor clipboard usage and then open the
// clipboard to scan it. If we i) empty and then ii) set data, then the
// 'set data' can sometimes fail with access denied becacuse another program
// has the clipboard open. So to avoid this race condition for OpenClipboard
// we do not empty the clipboard when we're setting it.
if (aWhichClipboard == kGlobalClipboard && !mEmptyingForSetData) {
RepeatedlyTryOleSetClipboard(nullptr);
}
return nsBaseClipboard::EmptyClipboard(aWhichClipboard);
RepeatedlyTryOleSetClipboard(nullptr);
return NS_OK;
}
mozilla::Result<int32_t, nsresult>

View File

@ -33,9 +33,6 @@ class nsClipboard : public nsBaseClipboard, public nsIObserver {
// nsIObserver
NS_DECL_NSIOBSERVER
// nsIClipboard
NS_IMETHOD EmptyClipboard(int32_t aWhichClipboard) override;
// Internal Native Routines
enum class MightNeedToFlush : bool { No, Yes };
static nsresult CreateNativeDataObject(nsITransferable* aTransferable,
@ -79,6 +76,7 @@ class nsClipboard : public nsBaseClipboard, public nsIObserver {
int32_t aWhichClipboard) override;
NS_IMETHOD GetNativeClipboardData(nsITransferable* aTransferable,
int32_t aWhichClipboard) override;
nsresult EmptyNativeClipboardData(int32_t aWhichClipboard) override;
mozilla::Result<int32_t, nsresult> GetNativeClipboardSequenceNumber(
int32_t aWhichClipboard) override;
mozilla::Result<bool, nsresult> HasNativeClipboardDataMatchingFlavors(