diff --git a/widget/cocoa/nsClipboard.h b/widget/cocoa/nsClipboard.h index 661336526dfb..764453dab20d 100644 --- a/widget/cocoa/nsClipboard.h +++ b/widget/cocoa/nsClipboard.h @@ -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 GetNativeClipboardSequenceNumber( int32_t aWhichClipboard) override; mozilla::Result HasNativeClipboardDataMatchingFlavors( @@ -58,10 +56,6 @@ class nsClipboard : public nsBaseClipboard { virtual ~nsClipboard(); static mozilla::Maybe FindIndexOfImageFlavor(const nsTArray& aMIMETypes); - - int32_t mCachedClipboard = -1; - // Set to the native change count after any modification of the clipboard. - int32_t mChangeCount = 0; }; #endif // nsClipboard_h_ diff --git a/widget/cocoa/nsClipboard.mm b/widget/cocoa/nsClipboard.mm index d68a865b7968..c463f097f2ab 100644 --- a/widget/cocoa/nsClipboard.mm +++ b/widget/cocoa/nsClipboard.mm @@ -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); } diff --git a/widget/nsBaseClipboard.cpp b/widget/nsBaseClipboard.cpp index 984c908aa473..a99076aa72cc 100644 --- a/widget/nsBaseClipboard.cpp +++ b/widget/nsBaseClipboard.cpp @@ -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 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; diff --git a/widget/nsBaseClipboard.h b/widget/nsBaseClipboard.h index 76fff38be11c..788eaef7dea6 100644 --- a/widget/nsBaseClipboard.h +++ b/widget/nsBaseClipboard.h @@ -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& 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 GetNativeClipboardSequenceNumber( int32_t aWhichClipboard) = 0; virtual mozilla::Result HasNativeClipboardDataMatchingFlavors( const nsTArray& aFlavorList, int32_t aWhichClipboard) = 0; - bool mEmptyingForSetData = false; - private: class ClipboardCache final { public: diff --git a/widget/windows/nsClipboard.cpp b/widget/windows/nsClipboard.cpp index 4420225f1980..43beb8e8e336 100644 --- a/widget/windows/nsClipboard.cpp +++ b/widget/windows/nsClipboard.cpp @@ -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 diff --git a/widget/windows/nsClipboard.h b/widget/windows/nsClipboard.h index 57c20e370841..761516ead54c 100644 --- a/widget/windows/nsClipboard.h +++ b/widget/windows/nsClipboard.h @@ -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 GetNativeClipboardSequenceNumber( int32_t aWhichClipboard) override; mozilla::Result HasNativeClipboardDataMatchingFlavors(