diff --git a/layout/generic/nsAutoCopyListener.h b/layout/generic/nsAutoCopyListener.h index 4af5c0bada5c..c580f5260082 100644 --- a/layout/generic/nsAutoCopyListener.h +++ b/layout/generic/nsAutoCopyListener.h @@ -16,16 +16,20 @@ public: NS_DECL_ISUPPORTS NS_DECL_NSISELECTIONLISTENER + explicit nsAutoCopyListener(int16_t aClipboardID) + : mCachedClipboard(aClipboardID) + {} + void Listen(nsISelectionPrivate *aSelection) { NS_ASSERTION(aSelection, "Null selection passed to Listen()"); aSelection->AddSelectionListener(this); } - static nsAutoCopyListener* GetInstance() + static nsAutoCopyListener* GetInstance(int16_t aClipboardID) { if (!sInstance) { - sInstance = new nsAutoCopyListener(); + sInstance = new nsAutoCopyListener(aClipboardID); NS_ADDREF(sInstance); } @@ -42,6 +46,7 @@ private: ~nsAutoCopyListener() {} static nsAutoCopyListener* sInstance; + int16_t mCachedClipboard; }; #endif diff --git a/layout/generic/nsSelection.cpp b/layout/generic/nsSelection.cpp index aa48b87ccba5..c7024a4d5a6f 100644 --- a/layout/generic/nsSelection.cpp +++ b/layout/generic/nsSelection.cpp @@ -549,16 +549,22 @@ nsFrameSelection::nsFrameSelection() mSelectingTableCellMode = 0; mSelectedCellIndex = 0; + nsAutoCopyListener *autoCopy = nullptr; + // On macOS, cache the current selection to send to osx service menu. +#ifdef XP_MACOSX + autoCopy = nsAutoCopyListener::GetInstance(nsIClipboard::kSelectionCache); +#endif + // Check to see if the autocopy pref is enabled // and add the autocopy listener if it is if (Preferences::GetBool("clipboard.autocopy")) { - nsAutoCopyListener *autoCopy = nsAutoCopyListener::GetInstance(); + autoCopy = nsAutoCopyListener::GetInstance(nsIClipboard::kSelectionClipboard); + } - if (autoCopy) { - int8_t index = GetIndexFromSelectionType(SelectionType::eNormal); - if (mDomSelections[index]) { - autoCopy->Listen(mDomSelections[index]); - } + if (autoCopy) { + int8_t index = GetIndexFromSelectionType(SelectionType::eNormal); + if (mDomSelections[index]) { + autoCopy->Listen(mDomSelections[index]); } } @@ -6467,6 +6473,11 @@ NS_IMPL_ISUPPORTS(nsAutoCopyListener, nsISelectionListener) * selections? * - maybe we should just never clear the X clipboard? That would make this * problem just go away, which is very tempting. + * + * On macOS, + * nsIClipboard::kSelectionCache is the flag for current selection cache. + * Set the current selection cache on the parent process in + * widget cocoa nsClipboard whenever selection changes. */ NS_IMETHODIMP @@ -6493,7 +6504,7 @@ nsAutoCopyListener::NotifySelectionChanged(nsIDOMDocument *aDoc, // call the copy code return nsCopySupport::HTMLCopy(aSel, doc, - nsIClipboard::kSelectionClipboard, false); + mCachedClipboard, false); } // SelectionChangeListener diff --git a/widget/nsIClipboard.idl b/widget/nsIClipboard.idl index c63095a54b71..aaf97a5ecded 100644 --- a/widget/nsIClipboard.idl +++ b/widget/nsIClipboard.idl @@ -17,6 +17,8 @@ interface nsIClipboard : nsISupports const long kSelectionClipboard = 0; const long kGlobalClipboard = 1; const long kFindClipboard = 2; + // Used to cache current selection on (nsClipboard) for macOS service menu. + const long kSelectionCache = 3; /** * Given a transferable, set the data on the native clipboard