Bug 1261299 - Only update nsClipboard::sSelectionCache in nsSelection.cpp when the selection listener is on the same process as the focused window where the selection occured or became active.
MozReview-Commit-ID: KARKdc8G0nI
--- a/layout/generic/nsSelection.cpp
+++ b/layout/generic/nsSelection.cpp
@@ -79,16 +79,17 @@ static NS_DEFINE_CID(kFrameTraversalCID,
#include "mozilla/dom/SelectionBinding.h"
#include "mozilla/AsyncEventDispatcher.h"
#include "mozilla/Telemetry.h"
#include "mozilla/layers/ScrollInputMethods.h"
#include "nsViewManager.h"
#include "nsIEditor.h"
#include "nsIHTMLEditor.h"
+#include "nsFocusManager.h"
using namespace mozilla;
using namespace mozilla::dom;
using mozilla::layers::ScrollInputMethod;
//#define DEBUG_TABLE 1
static bool IsValidSelectionPoint(nsFrameSelection *aFrameSel, nsINode *aNode);
@@ -1971,17 +1972,20 @@ nsFrameSelection::RepaintSelection(Selec
return NS_ERROR_INVALID_ARG;
if (!mDomSelections[index])
return NS_ERROR_NULL_POINTER;
NS_ENSURE_STATE(mShell);
// On macOS, update the selection cache to the new active selection
// aka the current selection.
#ifdef XP_MACOSX
- if (aSelectionType == SelectionType::eNormal) {
+ nsFocusManager* fm = nsFocusManager::GetFocusManager();
+ // Check an active window exists otherwise there cannot be a current selection
+ // and that it's a normal selection.
+ if (fm->GetActiveWindow() && aSelectionType == SelectionType::eNormal) {
UpdateSelectionCacheOnRepaintSelection(mDomSelections[index]);
}
#endif
return mDomSelections[index]->Repaint(mShell->GetPresContext());
}
nsIFrame*
nsFrameSelection::GetFrameForNodeOffset(nsIContent* aNode,
@@ -6487,16 +6491,25 @@ NS_IMPL_ISUPPORTS(nsAutoCopyListener, ns
* Set the current selection cache on the parent process in
* widget cocoa nsClipboard whenever selection changes.
*/
NS_IMETHODIMP
nsAutoCopyListener::NotifySelectionChanged(nsIDOMDocument *aDoc,
nsISelection *aSel, int16_t aReason)
{
+ if (mCachedClipboard == nsIClipboard::kSelectionCache) {
+ nsFocusManager* fm = nsFocusManager::GetFocusManager();
+ // If no active window, do nothing because a current selection changed
+ // cannot occur unless it is in the active window.
+ if (!fm->GetActiveWindow()) {
+ return NS_OK;
+ }
+ }
+
if (!(aReason & nsISelectionListener::MOUSEUP_REASON ||
aReason & nsISelectionListener::SELECTALL_REASON ||
aReason & nsISelectionListener::KEYPRESS_REASON))
return NS_OK; //dont care if we are still dragging
bool collapsed;
if (!aDoc || !aSel ||
NS_FAILED(aSel->GetIsCollapsed(&collapsed)) || collapsed) {