Bug 1450882 - part 3: Create non-virtual methods to nsCommandParams and expose its header r?ehsan draft
authorMasayuki Nakano <masayuki@d-toybox.com>
Tue, 10 Jul 2018 18:48:43 +0900
changeset 815996 cbc58ec7ce42fccae04f9681fda940e4d7c7e206
parent 815995 e80852b2c767d39b301d36ce0c8be66a71ac8bce
child 815997 056ef748448155db28be3a957538a307b06b88ab
push id115711
push usermasayuki@d-toybox.com
push dateTue, 10 Jul 2018 12:46:56 +0000
reviewersehsan
bugs1450882
milestone63.0a1
Bug 1450882 - part 3: Create non-virtual methods to nsCommandParams and expose its header r?ehsan nsCommandParams is used internally but accessed via nsICommandParams. For such internal users, nsCommandParams should have non-virtual methods to get/set values. The new method names are similar to Preferences API for consistency with it. MozReview-Commit-ID: FJwWMFzV9w
dom/commandhandler/moz.build
dom/commandhandler/nsCommandParams.cpp
dom/commandhandler/nsCommandParams.h
dom/commandhandler/nsICommandParams.idl
--- a/dom/commandhandler/moz.build
+++ b/dom/commandhandler/moz.build
@@ -2,16 +2,20 @@
 # vim: set filetype=python:
 # 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/.
 
 with Files("**"):
     BUG_COMPONENT = ("Core", "Embedding: APIs")
 
+EXPORTS += [
+    'nsCommandParams.h',
+]
+
 XPIDL_SOURCES += [
     'nsICommandManager.idl',
     'nsICommandParams.idl',
     'nsIControllerCommand.idl',
     'nsIControllerCommandTable.idl',
     'nsIControllerContext.idl',
     'nsPICommandUpdater.idl',
 ]
--- a/dom/commandhandler/nsCommandParams.cpp
+++ b/dom/commandhandler/nsCommandParams.cpp
@@ -47,170 +47,261 @@ nsCommandParams::GetValueType(const char
   return NS_ERROR_FAILURE;
 }
 
 NS_IMETHODIMP
 nsCommandParams::GetBooleanValue(const char* aName, bool* aRetVal)
 {
   NS_ENSURE_ARG_POINTER(aRetVal);
 
+  ErrorResult error;
+  *aRetVal = GetBool(aName, error);
+  return error.StealNSResult();
+}
+
+bool
+nsCommandParams::GetBool(const char* aName, ErrorResult& aRv) const
+{
+  MOZ_ASSERT(!aRv.Failed());
+
   HashEntry* foundEntry = GetNamedEntry(aName);
   if (foundEntry && foundEntry->mEntryType == eBooleanType) {
-    *aRetVal = foundEntry->mData.mBoolean;
-    return NS_OK;
+    return foundEntry->mData.mBoolean;
   }
-  *aRetVal = false;
-  return NS_ERROR_FAILURE;
+  aRv.Throw(NS_ERROR_FAILURE);
+  return false;
 }
 
 NS_IMETHODIMP
 nsCommandParams::GetLongValue(const char* aName, int32_t* aRetVal)
 {
   NS_ENSURE_ARG_POINTER(aRetVal);
 
+  ErrorResult error;
+  *aRetVal = GetInt(aName, error);
+  return error.StealNSResult();
+}
+
+int32_t
+nsCommandParams::GetInt(const char* aName, ErrorResult& aRv) const
+{
+  MOZ_ASSERT(!aRv.Failed());
+
   HashEntry* foundEntry = GetNamedEntry(aName);
   if (foundEntry && foundEntry->mEntryType == eLongType) {
-    *aRetVal = foundEntry->mData.mLong;
-    return NS_OK;
+    return foundEntry->mData.mLong;
   }
-  *aRetVal = false;
-  return NS_ERROR_FAILURE;
+  aRv.Throw(NS_ERROR_FAILURE);
+  return 0;
 }
 
 NS_IMETHODIMP
 nsCommandParams::GetDoubleValue(const char* aName, double* aRetVal)
 {
   NS_ENSURE_ARG_POINTER(aRetVal);
 
+  ErrorResult error;
+  *aRetVal = GetDouble(aName, error);
+  return error.StealNSResult();
+}
+
+double
+nsCommandParams::GetDouble(const char* aName, ErrorResult& aRv) const
+{
+  MOZ_ASSERT(!aRv.Failed());
+
   HashEntry* foundEntry = GetNamedEntry(aName);
   if (foundEntry && foundEntry->mEntryType == eDoubleType) {
-    *aRetVal = foundEntry->mData.mDouble;
-    return NS_OK;
+    return foundEntry->mData.mDouble;
   }
-  *aRetVal = 0.0;
-  return NS_ERROR_FAILURE;
+  aRv.Throw(NS_ERROR_FAILURE);
+  return 0.0;
 }
 
 NS_IMETHODIMP
 nsCommandParams::GetStringValue(const char* aName, nsAString& aRetVal)
 {
+  return GetString(aName, aRetVal);
+}
+
+nsresult
+nsCommandParams::GetString(const char* aName, nsAString& aRetVal) const
+{
   HashEntry* foundEntry = GetNamedEntry(aName);
   if (foundEntry && foundEntry->mEntryType == eWStringType) {
     NS_ASSERTION(foundEntry->mData.mString, "Null string");
     aRetVal.Assign(*foundEntry->mData.mString);
     return NS_OK;
   }
   aRetVal.Truncate();
   return NS_ERROR_FAILURE;
 }
 
 NS_IMETHODIMP
 nsCommandParams::GetCStringValue(const char* aName, nsACString& aRetVal)
 {
+  return GetCString(aName, aRetVal);
+}
+
+nsresult
+nsCommandParams::GetCString(const char* aName, nsACString& aRetVal) const
+{
   HashEntry* foundEntry = GetNamedEntry(aName);
   if (foundEntry && foundEntry->mEntryType == eStringType) {
     NS_ASSERTION(foundEntry->mData.mCString, "Null string");
     aRetVal.Assign(*foundEntry->mData.mCString);
     return NS_OK;
   }
   aRetVal.Truncate();
   return NS_ERROR_FAILURE;
 }
 
 NS_IMETHODIMP
 nsCommandParams::GetISupportsValue(const char* aName, nsISupports** aRetVal)
 {
   NS_ENSURE_ARG_POINTER(aRetVal);
 
+  ErrorResult error;
+  nsCOMPtr<nsISupports> result = GetISupports(aName, error);
+  if (result) {
+    result.forget(aRetVal);
+  } else {
+    *aRetVal = nullptr;
+  }
+  return error.StealNSResult();
+}
+
+already_AddRefed<nsISupports>
+nsCommandParams::GetISupports(const char* aName, ErrorResult& aRv) const
+{
+  MOZ_ASSERT(!aRv.Failed());
+
   HashEntry* foundEntry = GetNamedEntry(aName);
   if (foundEntry && foundEntry->mEntryType == eISupportsType) {
-    NS_IF_ADDREF(*aRetVal = foundEntry->mISupports.get());
-    return NS_OK;
+    nsCOMPtr<nsISupports> result = foundEntry->mISupports;
+    return result.forget();
   }
-  *aRetVal = nullptr;
-  return NS_ERROR_FAILURE;
+  aRv.Throw(NS_ERROR_FAILURE);
+  return nullptr;
 }
 
 NS_IMETHODIMP
 nsCommandParams::SetBooleanValue(const char* aName, bool aValue)
 {
+  return SetBool(aName, aValue);
+}
+
+nsresult
+nsCommandParams::SetBool(const char* aName, bool aValue)
+{
   HashEntry* foundEntry = GetOrMakeEntry(aName, eBooleanType);
   if (!foundEntry) {
     return NS_ERROR_OUT_OF_MEMORY;
   }
   foundEntry->mData.mBoolean = aValue;
   return NS_OK;
 }
 
 NS_IMETHODIMP
 nsCommandParams::SetLongValue(const char* aName, int32_t aValue)
 {
+  return SetInt(aName, aValue);
+}
+
+nsresult
+nsCommandParams::SetInt(const char* aName, int32_t aValue)
+{
   HashEntry* foundEntry = GetOrMakeEntry(aName, eLongType);
   if (!foundEntry) {
     return NS_ERROR_OUT_OF_MEMORY;
   }
   foundEntry->mData.mLong = aValue;
   return NS_OK;
 }
 
 NS_IMETHODIMP
 nsCommandParams::SetDoubleValue(const char* aName, double aValue)
 {
+  return SetDouble(aName, aValue);
+}
+
+nsresult
+nsCommandParams::SetDouble(const char* aName, double aValue)
+{
   HashEntry* foundEntry = GetOrMakeEntry(aName, eDoubleType);
   if (!foundEntry) {
     return NS_ERROR_OUT_OF_MEMORY;
   }
   foundEntry->mData.mDouble = aValue;
   return NS_OK;
 }
 
 NS_IMETHODIMP
 nsCommandParams::SetStringValue(const char* aName, const nsAString& aValue)
 {
+  return SetString(aName, aValue);
+}
+
+nsresult
+nsCommandParams::SetString(const char* aName, const nsAString& aValue)
+{
   HashEntry* foundEntry = GetOrMakeEntry(aName, eWStringType);
   if (!foundEntry) {
     return NS_ERROR_OUT_OF_MEMORY;
   }
   foundEntry->mData.mString = new nsString(aValue);
   return NS_OK;
 }
 
 NS_IMETHODIMP
 nsCommandParams::SetCStringValue(const char* aName, const nsACString& aValue)
 {
+  return SetCString(aName, aValue);
+}
+
+nsresult
+nsCommandParams::SetCString(const char* aName, const nsACString& aValue)
+{
   HashEntry* foundEntry = GetOrMakeEntry(aName, eStringType);
   if (!foundEntry) {
     return NS_ERROR_OUT_OF_MEMORY;
   }
   foundEntry->mData.mCString = new nsCString(aValue);
   return NS_OK;
 }
 
 NS_IMETHODIMP
 nsCommandParams::SetISupportsValue(const char* aName, nsISupports* aValue)
 {
+  return SetISupports(aName, aValue);
+}
+
+nsresult
+nsCommandParams::SetISupports(const char* aName, nsISupports* aValue)
+{
   HashEntry* foundEntry = GetOrMakeEntry(aName, eISupportsType);
   if (!foundEntry) {
     return NS_ERROR_OUT_OF_MEMORY;
   }
   foundEntry->mISupports = aValue; // addrefs
   return NS_OK;
 }
 
 NS_IMETHODIMP
 nsCommandParams::RemoveValue(const char* aName)
 {
   mValuesHash.Remove((void*)aName);
   return NS_OK;
 }
 
 nsCommandParams::HashEntry*
-nsCommandParams::GetNamedEntry(const char* aName)
+nsCommandParams::GetNamedEntry(const char* aName) const
 {
-  return static_cast<HashEntry*>(mValuesHash.Search((void*)aName));
+  return static_cast<HashEntry*>(
+           const_cast<PLDHashTable&>(mValuesHash).Search((void*)aName));
 }
 
 nsCommandParams::HashEntry*
 nsCommandParams::GetOrMakeEntry(const char* aName, uint8_t aEntryType)
 {
   auto foundEntry = static_cast<HashEntry*>(mValuesHash.Search((void*)aName));
   if (foundEntry) { // reuse existing entry
     foundEntry->Reset(aEntryType);
--- a/dom/commandhandler/nsCommandParams.h
+++ b/dom/commandhandler/nsCommandParams.h
@@ -1,30 +1,69 @@
 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
 /* 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/. */
 
-#ifndef nsCommandParams_h__
-#define nsCommandParams_h__
+#ifndef nsCommandParams_h
+#define nsCommandParams_h
 
+#include "mozilla/ErrorResult.h"
 #include "nsString.h"
 #include "nsICommandParams.h"
 #include "nsCOMPtr.h"
 #include "PLDHashTable.h"
 
 class nsCommandParams : public nsICommandParams
 {
+  typedef mozilla::ErrorResult ErrorResult;
+  typedef mozilla::IgnoredErrorResult IgnoredErrorResult;
+
 public:
   nsCommandParams();
 
   NS_DECL_ISUPPORTS
   NS_DECL_NSICOMMANDPARAMS
 
+  bool GetBool(const char* aName, ErrorResult& aRv) const;
+  inline bool GetBool(const char* aName) const
+  {
+    IgnoredErrorResult error;
+    return GetBool(aName, error);
+  }
+  int32_t GetInt(const char* aName, ErrorResult& aRv) const;
+  inline int32_t GetInt(const char* aName) const
+  {
+    IgnoredErrorResult error;
+    return GetInt(aName, error);
+  }
+  double GetDouble(const char* aName, ErrorResult& aRv) const;
+  inline double GetDouble(const char* aName) const
+  {
+    IgnoredErrorResult error;
+    return GetDouble(aName, error);
+  }
+  nsresult GetString(const char* aName, nsAString& aValue) const;
+  nsresult GetCString(const char* aName, nsACString& aValue) const;
+  already_AddRefed<nsISupports>
+  GetISupports(const char* aName, ErrorResult& aRv) const;
+  inline already_AddRefed<nsISupports> GetISupports(const char* aName) const
+  {
+    IgnoredErrorResult error;
+    return GetISupports(aName, error);
+  }
+
+  nsresult SetBool(const char* aName, bool aValue);
+  nsresult SetInt(const char* aName, int32_t aValue);
+  nsresult SetDouble(const char* aName, double aValue);
+  nsresult SetString(const char* aName, const nsAString& aValue);
+  nsresult SetCString(const char* aName, const nsACString& aValue);
+  nsresult SetISupports(const char* aName, nsISupports* aValue);
+
 protected:
   virtual ~nsCommandParams();
 
   struct HashEntry : public PLDHashEntryHdr
   {
     nsCString mEntryName;
 
     uint8_t mEntryType;
@@ -105,17 +144,17 @@ protected:
           break;
         default:
           NS_ERROR("Unknown type");
       }
       mEntryType = aNewType;
     }
   };
 
-  HashEntry* GetNamedEntry(const char* aName);
+  HashEntry* GetNamedEntry(const char* aName) const;
   HashEntry* GetOrMakeEntry(const char* aName, uint8_t aEntryType);
 
 protected:
   static PLDHashNumber HashKey(const void* aKey);
 
   static bool HashMatchEntry(const PLDHashEntryHdr* aEntry, const void* aKey);
 
   static void HashMoveEntry(PLDHashTable* aTable, const PLDHashEntryHdr* aFrom,
@@ -123,9 +162,21 @@ protected:
 
   static void HashClearEntry(PLDHashTable* aTable, PLDHashEntryHdr* aEntry);
 
   PLDHashTable mValuesHash;
 
   static const PLDHashTableOps sHashOps;
 };
 
-#endif // nsCommandParams_h__
+nsCommandParams*
+nsICommandParams::AsCommandParams()
+{
+  return static_cast<nsCommandParams*>(this);
+}
+
+const nsCommandParams*
+nsICommandParams::AsCommandParams() const
+{
+  return static_cast<const nsCommandParams*>(this);
+}
+
+#endif // nsCommandParams_h
--- a/dom/commandhandler/nsICommandParams.idl
+++ b/dom/commandhandler/nsICommandParams.idl
@@ -6,16 +6,21 @@
 #include "nsISupports.idl"
 
 /*
  * nsICommandParams is used to pass parameters to commands executed
  * via nsICommandManager, and to get command state.
  *
  */
 
+
+%{C++
+class nsCommandParams;
+%}
+
 [scriptable, builtinclass, uuid(b1fdf3c4-74e3-4f7d-a14d-2b76bcf53482)]
 interface nsICommandParams : nsISupports
 {
   /*
    * List of primitive types for parameter values.
    */
   const short eNoType                     = 0;      /* Only used for sanity checking */
   const short eBooleanType                = 1;
@@ -70,16 +75,25 @@ interface nsICommandParams : nsISupports
   void        setISupportsValue(in string name, in nsISupports value);
 
   /*
    * removeValue
    *
    * Remove the specified parameter from the list.
    */
   void        removeValue(in string name);
+
+%{C++
+  /**
+   * In order to avoid circular dependency issues, these methods are defined
+   * in nsCommandParams.h.  Consumers need to #include that header.
+   */
+  inline nsCommandParams* AsCommandParams();
+  inline const nsCommandParams* AsCommandParams() const;
+%}
 };
 
 // {f7fa4581-238e-11d5-a73c-ab64fb68f2bc}
 %{C++
 #define NS_COMMAND_PARAMS_CID { 0xf7fa4581, 0x238e, 0x11d5, { 0xa7, 0x3c, 0xab, 0x64, 0xfb, 0x68, 0xf2, 0xbc } }
 #define NS_COMMAND_PARAMS_CONTRACTID "@mozilla.org/embedcomp/command-params;1"
 %}