Bug 1265953, part 4 - Convert nsMenuX::LoadSubMenu and nsMenuX::AddMenu to UniquePtr. r=mstange
authorJonathan Watt <jwatt@jwatt.org>
Tue, 19 Apr 2016 23:51:49 +0100
changeset 332061 b4c514efe8c0f74ad94b33f846a3a94a37138ce4
parent 332060 e3c6988a0a55d437c1a99420dc25ec64d25f992d
child 332062 e92704fae381eb6b3363a87372663c5a6466e6fd
push id6048
push userkmoir@mozilla.com
push dateMon, 06 Jun 2016 19:02:08 +0000
treeherdermozilla-beta@46d72a56c57d [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmstange
bugs1265953
milestone48.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 1265953, part 4 - Convert nsMenuX::LoadSubMenu and nsMenuX::AddMenu to UniquePtr. r=mstange
widget/cocoa/nsMenuX.h
widget/cocoa/nsMenuX.mm
--- a/widget/cocoa/nsMenuX.h
+++ b/widget/cocoa/nsMenuX.h
@@ -73,17 +73,17 @@ protected:
   void           MenuConstruct();
   nsresult       RemoveAll();
   nsresult       SetEnabled(bool aIsEnabled);
   nsresult       GetEnabled(bool* aIsEnabled);
   void           GetMenuPopupContent(nsIContent** aResult);
   bool           OnOpen();
   bool           OnClose();
   nsresult       AddMenuItem(nsMenuItemX* aMenuItem);
-  nsresult       AddMenu(nsMenuX* aMenu);
+  nsMenuX*       AddMenu(mozilla::UniquePtr<nsMenuX> aMenu);
   void           LoadMenuItem(nsIContent* inMenuItemContent);  
   void           LoadSubMenu(nsIContent* inMenuContent);
   GeckoNSMenu*   CreateMenuWithGeckoString(nsString& menuTitle);
 
   nsTArray<mozilla::UniquePtr<nsMenuObjectX>> mMenuObjectsArray;
   nsString                  mLabel;
   uint32_t                  mVisibleItemsCount; // cache
   nsMenuObjectX*            mParent; // [weak]
--- a/widget/cocoa/nsMenuX.mm
+++ b/widget/cocoa/nsMenuX.mm
@@ -230,40 +230,41 @@ nsresult nsMenuX::AddMenuItem(nsMenuItem
   [newNativeMenuItem setRepresentedObject:info];
   [info release];
 
   return NS_OK;
 
   NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
 }
 
-nsresult nsMenuX::AddMenu(nsMenuX* aMenu)
+nsMenuX* nsMenuX::AddMenu(UniquePtr<nsMenuX> aMenu)
 {
-  NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
+  NS_OBJC_BEGIN_TRY_ABORT_BLOCK_RETURN;
 
-  // Add a submenu
-  if (!aMenu)
-    return NS_ERROR_NULL_POINTER;
+  // aMenu transfers ownership to mMenuObjectsArray and becomes nullptr, so
+  // we need to keep a raw pointer to access it conveniently.
+  nsMenuX* menu = aMenu.get();
+  mMenuObjectsArray.AppendElement(Move(aMenu));
 
-  mMenuObjectsArray.AppendElement(aMenu);
-  if (nsMenuUtilsX::NodeIsHiddenOrCollapsed(aMenu->Content()))
-    return NS_OK;
+  if (nsMenuUtilsX::NodeIsHiddenOrCollapsed(menu->Content())) {
+    return menu;
+  }
+
   ++mVisibleItemsCount;
 
   // We have to add a menu item and then associate the menu with it
-  NSMenuItem* newNativeMenuItem = aMenu->NativeMenuItem();
-  if (!newNativeMenuItem)
-    return NS_ERROR_FAILURE;
-  [mNativeMenu addItem:newNativeMenuItem];
+  NSMenuItem* newNativeMenuItem = menu->NativeMenuItem();
+  if (newNativeMenuItem) {
+    [mNativeMenu addItem:newNativeMenuItem];
+    [newNativeMenuItem setSubmenu:(NSMenu*)menu->NativeData()];
+  }
 
-  [newNativeMenuItem setSubmenu:(NSMenu*)aMenu->NativeData()];
+  return menu;
 
-  return NS_OK;
-
-  NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
+  NS_OBJC_END_TRY_ABORT_BLOCK_RETURN(nullptr);
 }
 
 // Includes all items, including hidden/collapsed ones
 uint32_t nsMenuX::GetItemCount()
 {
   return mMenuObjectsArray.Length();
 }
 
@@ -533,31 +534,33 @@ void nsMenuX::LoadMenuItem(nsIContent* i
 
   // This needs to happen after the nsIMenuItem object is inserted into
   // our item array in AddMenuItem()
   menuItem->SetupIcon();
 }
 
 void nsMenuX::LoadSubMenu(nsIContent* inMenuContent)
 {
-  nsAutoPtr<nsMenuX> menu(new nsMenuX());
+  auto menu = MakeUnique<nsMenuX>();
   if (!menu)
     return;
 
   nsresult rv = menu->Create(this, mMenuGroupOwner, inMenuContent);
   if (NS_FAILED(rv))
     return;
 
-  AddMenu(menu);
+  // |menu|'s ownership is transfer to AddMenu but, if it is successfully
+  // added, we can access it via the returned raw pointer.
+  nsMenuX* menu_ptr = AddMenu(Move(menu));
 
   // This needs to happen after the nsIMenu object is inserted into
   // our item array in AddMenu()
-  menu->SetupIcon();
-
-  menu.forget();
+  if (menu_ptr) {
+    menu_ptr->SetupIcon();
+  }
 }
 
 // This menu is about to open. Returns TRUE if we should keep processing the event,
 // FALSE if the handler wants to stop the opening of the menu.
 bool nsMenuX::OnOpen()
 {
   nsEventStatus status = nsEventStatus_eIgnore;
   WidgetMouseEvent event(true, eXULPopupShowing, nullptr,