Bug 1156110 - Make nsView::mWindow an nsCOMPtr; r=roc
authorEhsan Akhgari <ehsan@mozilla.com>
Sun, 19 Apr 2015 14:59:51 -0400
changeset 269777 2a8c4b9e813f63ffbc84481ecec1be86f92161f0
parent 269776 b7b0568adaf69529623323ff82e8154cf843cc39
child 269778 f681620cad02cb3d53273a26dea180f9e7c77071
push id4830
push userjlund@mozilla.com
push dateMon, 29 Jun 2015 20:18:48 +0000
treeherdermozilla-beta@4c2175bb0420 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersroc
bugs1156110
milestone40.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1156110 - Make nsView::mWindow an nsCOMPtr; r=roc
view/nsView.cpp
view/nsView.h
--- a/view/nsView.cpp
+++ b/view/nsView.cpp
@@ -129,17 +129,17 @@ void nsView::DestroyWidget()
       mWindow->SetWidgetListener(nullptr);
 
       nsCOMPtr<nsIRunnable> widgetDestroyer =
         new DestroyWidgetRunnable(mWindow);
 
       NS_DispatchToMainThread(widgetDestroyer);
     }
 
-    NS_RELEASE(mWindow);
+    mWindow = nullptr;
   }
 }
 
 nsView* nsView::GetViewFor(nsIWidget* aWidget)
 {
   NS_PRECONDITION(nullptr != aWidget, "null widget ptr");
 
   nsIWidgetListener* listener = aWidget->GetWidgetListener();
@@ -228,17 +228,17 @@ nsIntRect nsView::CalcWidgetBounds(nsWin
 
   // Compute widget bounds in device pixels
   nsIntRect newBounds = viewBounds.ToNearestPixels(p2a);
 
 #ifdef XP_MACOSX
   // cocoa rounds widget coordinates to the nearest global "display pixel"
   // integer value. So we avoid fractional display pixel values by rounding
   // to the nearest value that won't yield a fractional display pixel.
-  nsIWidget* widget = parentWidget ? parentWidget : mWindow;
+  nsIWidget* widget = parentWidget ? parentWidget : mWindow.get();
   uint32_t round;
   if (aType == eWindowType_popup && widget &&
       ((round = widget->RoundsWidgetCoordinatesTo()) > 1)) {
     nsIntSize pixelRoundedSize = newBounds.Size();
     // round the top left and bottom right to the nearest round pixel
     newBounds.x = NSToIntRoundUp(NSAppUnitsToDoublePixels(viewBounds.x, p2a) / round) * round;
     newBounds.y = NSToIntRoundUp(NSAppUnitsToDoublePixels(viewBounds.y, p2a) / round) * round;
     newBounds.width =
@@ -570,17 +570,17 @@ nsresult nsView::CreateWidget(nsWidgetIn
     GetParent() ? GetParent()->GetNearestWidget(nullptr) : nullptr;
   if (!parentWidget) {
     NS_ERROR("nsView::CreateWidget without suitable parent widget??");
     return NS_ERROR_FAILURE;
   }
 
   // XXX: using aForceUseIWidgetParent=true to preserve previous
   // semantics.  It's not clear that it's actually needed.
-  mWindow = parentWidget->CreateChild(trect, aWidgetInitData, true).take();
+  mWindow = parentWidget->CreateChild(trect, aWidgetInitData, true);
   if (!mWindow) {
     return NS_ERROR_FAILURE;
   }
  
   InitializeWindow(aEnableDragDrop, aResetVisibility);
 
   return NS_OK;
 }
@@ -596,18 +596,17 @@ nsresult nsView::CreateWidgetForParent(n
              "Use CreateWidgetForPopup");
   MOZ_ASSERT(aParentWidget, "Parent widget required");
 
   DefaultWidgetInitData defaultInitData;
   aWidgetInitData = aWidgetInitData ? aWidgetInitData : &defaultInitData;
 
   nsIntRect trect = CalcWidgetBounds(aWidgetInitData->mWindowType);
 
-  mWindow =
-    aParentWidget->CreateChild(trect, aWidgetInitData).take();
+  mWindow = aParentWidget->CreateChild(trect, aWidgetInitData);
   if (!mWindow) {
     return NS_ERROR_FAILURE;
   }
 
   InitializeWindow(aEnableDragDrop, aResetVisibility);
 
   return NS_OK;
 }
@@ -626,28 +625,28 @@ nsresult nsView::CreateWidgetForPopup(ns
 
   // XXX/cjones: having these two separate creation cases seems ... um
   // ... unnecessary, but it's the way the old code did it.  Please
   // unify them by first finding a suitable parent nsIWidget, then
   // getting rid of aForceUseIWidgetParent.
   if (aParentWidget) {
     // XXX: using aForceUseIWidgetParent=true to preserve previous
     // semantics.  It's not clear that it's actually needed.
-    mWindow = aParentWidget->CreateChild(trect, aWidgetInitData, true).take();
+    mWindow = aParentWidget->CreateChild(trect, aWidgetInitData, true);
   }
   else {
     nsIWidget* nearestParent = GetParent() ? GetParent()->GetNearestWidget(nullptr)
                                            : nullptr;
     if (!nearestParent) {
       // Without a parent, we can't make a popup.  This can happen
       // when printing
       return NS_ERROR_FAILURE;
     }
 
-    mWindow = nearestParent->CreateChild(trect, aWidgetInitData).take();
+    mWindow = nearestParent->CreateChild(trect, aWidgetInitData);
   }
   if (!mWindow) {
     return NS_ERROR_FAILURE;
   }
 
   InitializeWindow(aEnableDragDrop, aResetVisibility);
 
   return NS_OK;
@@ -690,17 +689,16 @@ nsresult nsView::AttachToTopLevelWidget(
 
   // Note, the previous device context will be released. Detaching
   // will not restore the old one.
   nsresult rv = aWidget->AttachViewToTopLevel(!nsIWidget::UsePuppetWidgets());
   if (NS_FAILED(rv))
     return rv;
 
   mWindow = aWidget;
-  NS_ADDREF(mWindow);
 
   mWindow->SetAttachedWidgetListener(this);
   mWindow->EnableDragDrop(true);
   mWidgetIsTopLevel = true;
 
   // Refresh the view bounds
   CalcWidgetBounds(mWindow->WindowType());
 
@@ -709,17 +707,17 @@ nsresult nsView::AttachToTopLevelWidget(
 
 // Detach this view from an attached widget. 
 nsresult nsView::DetachFromTopLevelWidget()
 {
   NS_PRECONDITION(mWidgetIsTopLevel, "Not attached currently!");
   NS_PRECONDITION(mWindow, "null mWindow for DetachFromTopLevelWidget!");
 
   mWindow->SetAttachedWidgetListener(nullptr);
-  NS_RELEASE(mWindow);
+  mWindow = nullptr;
 
   mWidgetIsTopLevel = false;
   
   return NS_OK;
 }
 
 void nsView::SetZIndex(bool aAuto, int32_t aZIndex)
 {
@@ -734,17 +732,17 @@ void nsView::SetZIndex(bool aAuto, int32
 
 void nsView::AssertNoWindow()
 {
   // XXX: it would be nice to make this a strong assert
   if (MOZ_UNLIKELY(mWindow)) {
     NS_ERROR("We already have a window for this view? BAD");
     mWindow->SetWidgetListener(nullptr);
     mWindow->Destroy();
-    NS_RELEASE(mWindow);
+    mWindow = nullptr;
   }
 }
 
 //
 // internal window creation functions
 //
 void nsView::AttachWidgetEventHandler(nsIWidget* aWidget)
 {
@@ -770,18 +768,18 @@ void nsView::List(FILE* out, int32_t aIn
   fprintf(out, "%p ", (void*)this);
   if (nullptr != mWindow) {
     nscoord p2a = mViewManager->AppUnitsPerDevPixel();
     nsIntRect rect;
     mWindow->GetClientBounds(rect);
     nsRect windowBounds = rect.ToAppUnits(p2a);
     mWindow->GetBounds(rect);
     nsRect nonclientBounds = rect.ToAppUnits(p2a);
-    nsrefcnt widgetRefCnt = mWindow->AddRef() - 1;
-    mWindow->Release();
+    nsrefcnt widgetRefCnt = mWindow.get()->AddRef() - 1;
+    mWindow.get()->Release();
     int32_t Z = mWindow->GetZIndex();
     fprintf(out, "(widget=%p[%" PRIuPTR "] z=%d pos={%d,%d,%d,%d}) ",
             (void*)mWindow, widgetRefCnt, Z,
             nonclientBounds.x, nonclientBounds.y,
             windowBounds.width, windowBounds.height);
   }
   nsRect brect = GetBounds();
   fprintf(out, "{%d,%d,%d,%d}",
--- a/view/nsView.h
+++ b/view/nsView.h
@@ -6,16 +6,17 @@
 #ifndef nsView_h__
 #define nsView_h__
 
 #include "nsCoord.h"
 #include "nsRect.h"
 #include "nsPoint.h"
 #include "nsRegion.h"
 #include "nsCRT.h"
+#include "nsCOMPtr.h"
 #include "nsWidgetInitData.h" // for nsWindowType
 #include "nsIWidgetListener.h"
 #include "mozilla/EventForwards.h"
 
 class nsViewManager;
 class nsIWidget;
 class nsIFrame;
 
@@ -441,17 +442,17 @@ private:
   // Update the cached RootViewManager for all view manager descendents,
   // If the hierarchy is being removed, aViewManagerParent points to the view
   // manager for the hierarchy's old parent, and will have its mouse grab
   // released if it points to any view in this view hierarchy.
   void InvalidateHierarchy(nsViewManager *aViewManagerParent);
 
   nsViewManager    *mViewManager;
   nsView           *mParent;
-  nsIWidget        *mWindow;
+  nsCOMPtr<nsIWidget> mWindow;
   nsView           *mNextSibling;
   nsView           *mFirstChild;
   nsIFrame         *mFrame;
   nsRegion         *mDirtyRegion;
   int32_t           mZIndex;
   nsViewVisibility  mVis;
   // position relative our parent view origin but in our appunits
   nscoord           mPosX, mPosY;