Bug 1540969 - Get rid of nsPICommandUpdater r=bzbarsky
authorMasayuki Nakano <masayuki@d-toybox.com>
Wed, 03 Apr 2019 12:53:54 +0000
changeset 467858 395ff10d7debcb20d87e09c60600240c6cc1aef9
parent 467857 42b4d2faa54345b470bba2ceeadbc97191056b20
child 467859 d1814ba5bb8e0b5716c4eb728ca3a6e9aa305fcc
push id35810
push useraciure@mozilla.com
push dateThu, 04 Apr 2019 04:33:36 +0000
treeherdermozilla-central@b72c02e34261 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbzbarsky
bugs1540969
milestone68.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 1540969 - Get rid of nsPICommandUpdater r=bzbarsky `nsPICommandUpdater` is a scriptable interface, but nobody refers it from JS, and it's implemented only by `nsCommandManager`. Therefore, we can get rid of this interface. Additionally, `nsCommandManager::Init()` is called only by `nsDocShell`, which is the only instantiater of the class, and returns error only when given window is nullptr. Therefore, we can make the constructor take the window instead. Differential Revision: https://phabricator.services.mozilla.com/D25729
docshell/base/nsDocShell.cpp
dom/commandhandler/moz.build
dom/commandhandler/nsCommandManager.cpp
dom/commandhandler/nsCommandManager.h
dom/commandhandler/nsPICommandUpdater.idl
editor/composer/ComposerCommandsUpdater.cpp
editor/composer/ComposerCommandsUpdater.h
--- a/docshell/base/nsDocShell.cpp
+++ b/docshell/base/nsDocShell.cpp
@@ -12377,24 +12377,20 @@ nsDocShell::DoCommandWithParams(const ch
     return rv;
   }
 
   return commandController->DoCommandWithParams(aCommand, aParams);
 }
 
 nsresult nsDocShell::EnsureCommandHandler() {
   if (!mCommandManager) {
-    RefPtr<nsCommandManager> commandManager = new nsCommandManager();
-    nsCOMPtr<nsPIDOMWindowOuter> domWindow = GetWindow();
-    nsresult rv = commandManager->Init(domWindow);
-    if (NS_SUCCEEDED(rv)) {
-      mCommandManager = std::move(commandManager);
-    }
-  }
-
+    if (nsCOMPtr<nsPIDOMWindowOuter> domWindow = GetWindow()) {
+      mCommandManager = new nsCommandManager(domWindow);
+    }
+  }
   return mCommandManager ? NS_OK : NS_ERROR_FAILURE;
 }
 
 // link handling
 
 class OnLinkClickEvent : public Runnable {
  public:
   OnLinkClickEvent(nsDocShell* aHandler, nsIContent* aContent, nsIURI* aURI,
--- a/dom/commandhandler/moz.build
+++ b/dom/commandhandler/moz.build
@@ -15,17 +15,16 @@ EXPORTS += [
 ]
 
 XPIDL_SOURCES += [
     'nsICommandManager.idl',
     'nsICommandParams.idl',
     'nsIControllerCommand.idl',
     'nsIControllerCommandTable.idl',
     'nsIControllerContext.idl',
-    'nsPICommandUpdater.idl',
 ]
 
 XPIDL_MODULE = 'commandhandler'
 
 UNIFIED_SOURCES += [
     'nsBaseCommandController.cpp',
     'nsCommandManager.cpp',
     'nsCommandParams.cpp',
--- a/dom/commandhandler/nsCommandManager.cpp
+++ b/dom/commandhandler/nsCommandManager.cpp
@@ -20,17 +20,20 @@
 #include "nsPIDOMWindow.h"
 #include "nsPIWindowRoot.h"
 #include "nsIFocusManager.h"
 
 #include "nsCOMArray.h"
 
 #include "nsCommandManager.h"
 
-nsCommandManager::nsCommandManager() : mWindow(nullptr) {}
+nsCommandManager::nsCommandManager(mozIDOMWindowProxy* aWindow)
+    : mWindow(aWindow) {
+  MOZ_DIAGNOSTIC_ASSERT(mWindow);
+}
 
 nsCommandManager::~nsCommandManager() {}
 
 NS_IMPL_CYCLE_COLLECTION_CLASS(nsCommandManager)
 
 NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(nsCommandManager)
   tmp->mObserversTable.Clear();
 NS_IMPL_CYCLE_COLLECTION_UNLINK_END
@@ -44,31 +47,21 @@ NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(
   }
 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
 
 NS_IMPL_CYCLE_COLLECTING_ADDREF(nsCommandManager)
 NS_IMPL_CYCLE_COLLECTING_RELEASE(nsCommandManager)
 
 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(nsCommandManager)
   NS_INTERFACE_MAP_ENTRY(nsICommandManager)
-  NS_INTERFACE_MAP_ENTRY(nsPICommandUpdater)
   NS_INTERFACE_MAP_ENTRY(nsISupportsWeakReference)
   NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsICommandManager)
 NS_INTERFACE_MAP_END
 
-NS_IMETHODIMP
-nsCommandManager::Init(mozIDOMWindowProxy* aWindow) {
-  NS_ENSURE_ARG_POINTER(aWindow);
-
-  mWindow = aWindow;  // weak ptr
-  return NS_OK;
-}
-
-NS_IMETHODIMP
-nsCommandManager::CommandStatusChanged(const char* aCommandName) {
+nsresult nsCommandManager::CommandStatusChanged(const char* aCommandName) {
   ObserverList* commandObservers;
   mObserversTable.Get(aCommandName, &commandObservers);
 
   if (commandObservers) {
     // XXX Should we worry about observers removing themselves from Observe()?
     int32_t i, numItems = commandObservers->Length();
     for (i = 0; i < numItems; ++i) {
       nsCOMPtr<nsIObserver> observer = commandObservers->ElementAt(i);
--- a/dom/commandhandler/nsCommandManager.h
+++ b/dom/commandhandler/nsCommandManager.h
@@ -7,36 +7,45 @@
 #ifndef nsCommandManager_h__
 #define nsCommandManager_h__
 
 #include "nsString.h"
 #include "nsClassHashtable.h"
 #include "nsWeakReference.h"
 
 #include "nsICommandManager.h"
-#include "nsPICommandUpdater.h"
 #include "nsCycleCollectionParticipant.h"
 
 class nsIController;
 template <class E>
 class nsCOMArray;
 
 class nsCommandManager final : public nsICommandManager,
-                               public nsPICommandUpdater,
                                public nsSupportsWeakReference {
  public:
   typedef nsTArray<nsCOMPtr<nsIObserver> > ObserverList;
 
-  nsCommandManager();
+  nsCommandManager() = delete;
+
+  /**
+   * @param aWindow     An window which is what this command manager lives on.
+   */
+  explicit nsCommandManager(mozIDOMWindowProxy* aWindow);
 
   NS_DECL_CYCLE_COLLECTING_ISUPPORTS
   NS_DECL_CYCLE_COLLECTION_CLASS_AMBIGUOUS(nsCommandManager, nsICommandManager)
 
   NS_DECL_NSICOMMANDMANAGER
-  NS_DECL_NSPICOMMANDUPDATER
+
+  /**
+   * Notify the command manager that the status of a command changed. It may
+   * have changed from enabled to disabled, or vice versa, or become toggled
+   * etc.
+   */
+  nsresult CommandStatusChanged(const char* aCommandName);
 
   bool IsCommandEnabled(const nsCString& aCommandName,
                         mozIDOMWindowProxy* aTargetWindow);
 
  protected:
   virtual ~nsCommandManager();
 
   nsresult GetControllerForCommand(const char* aCommand,
deleted file mode 100644
--- a/dom/commandhandler/nsPICommandUpdater.idl
+++ /dev/null
@@ -1,39 +0,0 @@
-/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
-/* This Source Code Form is subject to the terms of the Mozilla Public
- * License, v. 2.0. If a copy of the MPL was not distributed with this
- * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
-
-#include "nsISupports.idl"
-
-interface mozIDOMWindowProxy;
-
-/*
-	The nsPICommandUpdater interface is used by modules that implement
-	commands, to tell the command manager that commands need updating.
-	This is a private interface; embedders should not use it.
-	
-	Command-implementing modules should get one of these by a QI
-	from an nsICommandManager.
-*/
-
-[scriptable, uuid(35e474ae-8016-4c34-9644-edc11f8b0ce1)]
-interface nsPICommandUpdater : nsISupports
-{
-
-  /*
-   * Init the command updater, passing an nsIDOMWindow which 
-   * is the window that the command updater lives on.
-   * 
-   */
-  void    init(in mozIDOMWindowProxy aWindow);
-   
-  /*
-   * Notify the command manager that the status of a command
-   * changed. It may have changed from enabled to disabled,
-   * or vice versa, or become toggled etc.
-   */
-	void		commandStatusChanged(in string aCommandName);
-	
-};
-
-
--- a/editor/composer/ComposerCommandsUpdater.cpp
+++ b/editor/composer/ComposerCommandsUpdater.cpp
@@ -241,95 +241,92 @@ nsresult ComposerCommandsUpdater::Update
     mDirtyState = aNowDirty;
   }
 
   return NS_OK;
 }
 
 nsresult ComposerCommandsUpdater::UpdateCommandGroup(
     const nsAString& aCommandGroup) {
-  nsCOMPtr<nsPICommandUpdater> commandUpdater = GetCommandUpdater();
-  NS_ENSURE_TRUE(commandUpdater, NS_ERROR_FAILURE);
+  RefPtr<nsCommandManager> commandManager = GetCommandManager();
+  NS_ENSURE_TRUE(commandManager, NS_ERROR_FAILURE);
 
   if (aCommandGroup.EqualsLiteral("undo")) {
-    commandUpdater->CommandStatusChanged("cmd_undo");
-    commandUpdater->CommandStatusChanged("cmd_redo");
+    commandManager->CommandStatusChanged("cmd_undo");
+    commandManager->CommandStatusChanged("cmd_redo");
     return NS_OK;
   }
 
   if (aCommandGroup.EqualsLiteral("select") ||
       aCommandGroup.EqualsLiteral("style")) {
-    commandUpdater->CommandStatusChanged("cmd_bold");
-    commandUpdater->CommandStatusChanged("cmd_italic");
-    commandUpdater->CommandStatusChanged("cmd_underline");
-    commandUpdater->CommandStatusChanged("cmd_tt");
+    commandManager->CommandStatusChanged("cmd_bold");
+    commandManager->CommandStatusChanged("cmd_italic");
+    commandManager->CommandStatusChanged("cmd_underline");
+    commandManager->CommandStatusChanged("cmd_tt");
 
-    commandUpdater->CommandStatusChanged("cmd_strikethrough");
-    commandUpdater->CommandStatusChanged("cmd_superscript");
-    commandUpdater->CommandStatusChanged("cmd_subscript");
-    commandUpdater->CommandStatusChanged("cmd_nobreak");
+    commandManager->CommandStatusChanged("cmd_strikethrough");
+    commandManager->CommandStatusChanged("cmd_superscript");
+    commandManager->CommandStatusChanged("cmd_subscript");
+    commandManager->CommandStatusChanged("cmd_nobreak");
 
-    commandUpdater->CommandStatusChanged("cmd_em");
-    commandUpdater->CommandStatusChanged("cmd_strong");
-    commandUpdater->CommandStatusChanged("cmd_cite");
-    commandUpdater->CommandStatusChanged("cmd_abbr");
-    commandUpdater->CommandStatusChanged("cmd_acronym");
-    commandUpdater->CommandStatusChanged("cmd_code");
-    commandUpdater->CommandStatusChanged("cmd_samp");
-    commandUpdater->CommandStatusChanged("cmd_var");
+    commandManager->CommandStatusChanged("cmd_em");
+    commandManager->CommandStatusChanged("cmd_strong");
+    commandManager->CommandStatusChanged("cmd_cite");
+    commandManager->CommandStatusChanged("cmd_abbr");
+    commandManager->CommandStatusChanged("cmd_acronym");
+    commandManager->CommandStatusChanged("cmd_code");
+    commandManager->CommandStatusChanged("cmd_samp");
+    commandManager->CommandStatusChanged("cmd_var");
 
-    commandUpdater->CommandStatusChanged("cmd_increaseFont");
-    commandUpdater->CommandStatusChanged("cmd_decreaseFont");
+    commandManager->CommandStatusChanged("cmd_increaseFont");
+    commandManager->CommandStatusChanged("cmd_decreaseFont");
 
-    commandUpdater->CommandStatusChanged("cmd_paragraphState");
-    commandUpdater->CommandStatusChanged("cmd_fontFace");
-    commandUpdater->CommandStatusChanged("cmd_fontColor");
-    commandUpdater->CommandStatusChanged("cmd_backgroundColor");
-    commandUpdater->CommandStatusChanged("cmd_highlight");
+    commandManager->CommandStatusChanged("cmd_paragraphState");
+    commandManager->CommandStatusChanged("cmd_fontFace");
+    commandManager->CommandStatusChanged("cmd_fontColor");
+    commandManager->CommandStatusChanged("cmd_backgroundColor");
+    commandManager->CommandStatusChanged("cmd_highlight");
     return NS_OK;
   }
 
   if (aCommandGroup.EqualsLiteral("save")) {
     // save commands (most are not in C++)
-    commandUpdater->CommandStatusChanged("cmd_setDocumentModified");
-    commandUpdater->CommandStatusChanged("cmd_save");
+    commandManager->CommandStatusChanged("cmd_setDocumentModified");
+    commandManager->CommandStatusChanged("cmd_save");
     return NS_OK;
   }
 
   return NS_OK;
 }
 
 nsresult ComposerCommandsUpdater::UpdateOneCommand(const char* aCommand) {
-  nsCOMPtr<nsPICommandUpdater> commandUpdater = GetCommandUpdater();
-  NS_ENSURE_TRUE(commandUpdater, NS_ERROR_FAILURE);
-
-  commandUpdater->CommandStatusChanged(aCommand);
-
+  RefPtr<nsCommandManager> commandManager = GetCommandManager();
+  NS_ENSURE_TRUE(commandManager, NS_ERROR_FAILURE);
+  commandManager->CommandStatusChanged(aCommand);
   return NS_OK;
 }
 
 bool ComposerCommandsUpdater::SelectionIsCollapsed() {
   if (NS_WARN_IF(!mDOMWindow)) {
     return true;
   }
 
   RefPtr<dom::Selection> domSelection = mDOMWindow->GetSelection();
   if (NS_WARN_IF(!domSelection)) {
     return false;
   }
 
   return domSelection->IsCollapsed();
 }
 
-already_AddRefed<nsPICommandUpdater>
-ComposerCommandsUpdater::GetCommandUpdater() {
+nsCommandManager* ComposerCommandsUpdater::GetCommandManager() {
   if (NS_WARN_IF(!mDocShell)) {
     return nullptr;
   }
-  return do_AddRef(mDocShell->GetCommandManager());
+  return mDocShell->GetCommandManager();
 }
 
 NS_IMETHODIMP
 ComposerCommandsUpdater::GetName(nsACString& aName) {
   aName.AssignLiteral("ComposerCommandsUpdater");
   return NS_OK;
 }
 
--- a/editor/composer/ComposerCommandsUpdater.h
+++ b/editor/composer/ComposerCommandsUpdater.h
@@ -11,21 +11,21 @@
 #include "nsCycleCollectionParticipant.h"
 #include "nsIDocumentStateListener.h"
 #include "nsINamed.h"
 #include "nsISupportsImpl.h"         // for NS_DECL_ISUPPORTS
 #include "nsITimer.h"                // for NS_DECL_NSITIMERCALLBACK, etc
 #include "nsITransactionListener.h"  // for nsITransactionListener
 #include "nscore.h"                  // for NS_IMETHOD, nsresult, etc
 
+class nsCommandManager;
 class nsIDocShell;
 class nsITransaction;
 class nsITransactionManager;
 class nsPIDOMWindowOuter;
-class nsPICommandUpdater;
 
 namespace mozilla {
 
 class ComposerCommandsUpdater final : public nsIDocumentStateListener,
                                       public nsITransactionListener,
                                       public nsITimerCallback,
                                       public nsINamed {
  public:
@@ -64,17 +64,17 @@ class ComposerCommandsUpdater final : pu
     eStateOn = 1,
   };
 
   bool SelectionIsCollapsed();
   nsresult UpdateDirtyState(bool aNowDirty);
   nsresult UpdateOneCommand(const char* aCommand);
   nsresult UpdateCommandGroup(const nsAString& aCommandGroup);
 
-  already_AddRefed<nsPICommandUpdater> GetCommandUpdater();
+  nsCommandManager* GetCommandManager();
 
   nsresult PrimeUpdateTimer();
   void TimerCallback();
 
   nsCOMPtr<nsITimer> mUpdateTimer;
   nsCOMPtr<nsPIDOMWindowOuter> mDOMWindow;
   nsCOMPtr<nsIDocShell> mDocShell;