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. draft
authorJimmy Wang <jimmyw22@gmail.com>
Wed, 03 Aug 2016 19:08:33 -0400
changeset 401296 ae408eb774ffdabc07ed711e146d99679e835554
parent 401295 5e75a99fe0a173d99b30dc9936928216eb301a39
child 401297 845456afe9b4e04bffdb585b0e9307f3ebbf7ecc
push id26424
push userjimmyw22@gmail.com
push dateTue, 16 Aug 2016 21:05:59 +0000
bugs1261299
milestone51.0a1
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
layout/generic/nsSelection.cpp
--- 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) {