Bug 1496598 - Port bug 1493226: remove needless QueryInterface calls in JS delegation. r=jorgk
authorBen Campbell <benc@thunderbird.net>
Fri, 05 Oct 2018 17:57:24 +1300
changeset 33308 93daa9a87d526f51aa749070a5c33e54142ec49a
parent 33307 f77c18acceafaa7e9fed65ab27e293cf117850a4
child 33309 f386902fa7325f359d24b88ae5751948b2cae42d
push id387
push userclokep@gmail.com
push dateMon, 10 Dec 2018 21:30:47 +0000
reviewersjorgk
bugs1496598, 1493226
Bug 1496598 - Port bug 1493226: remove needless QueryInterface calls in JS delegation. r=jorgk This changes the DELEGATE_JS helper macro, replacing QueryInterface usage with raw pointers (or implicit casts to descendent types) in cases where the object type is known at compile time. In fact, the remaining QueryInterface uses could also be removed - in all cases, we _know_ that the mCppBase is an instance of the "Super" helper class, which is always castable to the interfaces we're opening to delegation. But at the moment, mCppBase is always stored as a nsCOMPtr<> to one of the interfaces, and we can't get to the others without using a runtime QueryInterface().
mailnews/jsaccount/src/DelegateList.h
mailnews/jsaccount/src/JaAbDirectory.h
mailnews/jsaccount/src/JaCompose.h
mailnews/jsaccount/src/JaIncomingServer.h
mailnews/jsaccount/src/JaMsgFolder.h
mailnews/jsaccount/src/JaSend.h
mailnews/jsaccount/src/JaUrl.h
--- a/mailnews/jsaccount/src/DelegateList.h
+++ b/mailnews/jsaccount/src/DelegateList.h
@@ -31,23 +31,22 @@ protected:
   nsCString mPrefix; // Windows decorated method prefix.
 };
 
 } // namespace mailnews
 } // namespace mozilla
 
 /*
  * This macro is used in forwarding functions.
- *   _interface: the interface being forwarded.
  *   _jsdelegate: the name of the JS pointer that implements a particular
  *                interface.
+ *   _jsmethods:  the DelegateList object
+ *   _cppbase:    the C++ base instance (used when call not delegated to js)
  *
- * You must follow the naming convention:
- *   1) use mCppBase as the name of the C++ base class instance.
- *   2) use mMethod as the name of the DelegateList object.
  **/
 
-#define DELEGATE_JS(_interface, _jsdelegate) (\
-    _jsdelegate && mMethods && \
-    mMethods->Contains(nsLiteralCString(__FUNCTION__)) ? \
-       _jsdelegate : nsCOMPtr<_interface>(do_QueryInterface(mCppBase)))
+#define DELEGATE_JS(_jsdelegate, _jsmethods, _cppbase) (\
+    _jsdelegate && _jsmethods && \
+    _jsmethods->Contains(nsLiteralCString(__FUNCTION__)) ? \
+       _jsdelegate : (_cppbase))
+
 
 #endif
--- a/mailnews/jsaccount/src/JaAbDirectory.h
+++ b/mailnews/jsaccount/src/JaAbDirectory.h
@@ -40,27 +40,30 @@ protected:
 
 class JaCppAbDirectoryDelegator : public JaBaseCppAbDirectory,
                                   public msgIOverride
 {
 public:
   NS_DECL_ISUPPORTS_INHERITED
   NS_DECL_MSGIOVERRIDE
 
-  NS_FORWARD_NSIABDIRECTORY(DELEGATE_JS(nsIAbDirectory, mJsIAbDirectory)->)
-  NS_FORWARD_NSIABCOLLECTION(DELEGATE_JS(nsIAbCollection, mJsIAbCollection)->)
-  NS_FORWARD_NSIABITEM(DELEGATE_JS(nsIAbItem, mJsIAbItem)->)
-  NS_FORWARD_NSIINTERFACEREQUESTOR(DELEGATE_JS(nsIInterfaceRequestor, mJsIInterfaceRequestor)->)
+  // use mCppBase as a raw pointer where possible
+  NS_FORWARD_NSIABDIRECTORY(DELEGATE_JS(mJsIAbDirectory, mMethods, mCppBase)->)
+  NS_FORWARD_NSIABCOLLECTION(DELEGATE_JS(mJsIAbCollection, mMethods, (mCppBase.get()))->)
+  NS_FORWARD_NSIABITEM(DELEGATE_JS(mJsIAbItem, mMethods, (mCppBase.get()))->)
+  NS_FORWARD_NSIINTERFACEREQUESTOR(DELEGATE_JS(mJsIInterfaceRequestor, mMethods,
+    (nsCOMPtr<nsIInterfaceRequestor>(do_QueryInterface(mCppBase))))->)
 
   JaCppAbDirectoryDelegator();
 
 private:
   virtual ~JaCppAbDirectoryDelegator() {
   }
 
+  // nsIAbDirectory inherits from nsIAbCollection which inherits from nsIAbItem.
   class Super : public nsIAbDirectory,
                 public nsIInterfaceRequestor
   {
     public:
       explicit Super(JaCppAbDirectoryDelegator* aFakeThis) {mFakeThis = aFakeThis;}
       NS_DECL_ISUPPORTS
       NS_FORWARD_NSIABDIRECTORY(mFakeThis->JaBaseCppAbDirectory::)
       NS_FORWARD_NSIABCOLLECTION(mFakeThis->JaBaseCppAbDirectory::)
--- a/mailnews/jsaccount/src/JaCompose.h
+++ b/mailnews/jsaccount/src/JaCompose.h
@@ -41,19 +41,21 @@ protected:
 
 class JaCppComposeDelegator : public JaBaseCppCompose,
                                      public msgIOverride
 {
 public:
   NS_DECL_ISUPPORTS_INHERITED
   NS_DECL_MSGIOVERRIDE
 
-  NS_FORWARD_NSIMSGCOMPOSE(DELEGATE_JS(nsIMsgCompose, mJsIMsgCompose)->)
-  NS_FORWARD_NSIMSGSENDLISTENER(DELEGATE_JS(nsIMsgSendListener, mJsIMsgSendListener)->)
-  NS_FORWARD_NSIINTERFACEREQUESTOR(DELEGATE_JS(nsIInterfaceRequestor, mJsIInterfaceRequestor)->)
+  NS_FORWARD_NSIMSGCOMPOSE(DELEGATE_JS(mJsIMsgCompose, mMethods, mCppBase)->)
+  NS_FORWARD_NSIMSGSENDLISTENER(DELEGATE_JS(mJsIMsgSendListener, mMethods, mCppBase.get())->)
+  NS_FORWARD_NSIINTERFACEREQUESTOR(
+    DELEGATE_JS(mJsIInterfaceRequestor, mMethods,
+      (nsCOMPtr<nsIInterfaceRequestor>(do_QueryInterface(mCppBase))))->)
 
   JaCppComposeDelegator();
 
 private:
   virtual ~JaCppComposeDelegator() {
   }
 
   // This class will call a method on the delegator, but force the use of the
--- a/mailnews/jsaccount/src/JaIncomingServer.h
+++ b/mailnews/jsaccount/src/JaIncomingServer.h
@@ -45,18 +45,20 @@ protected:
 
 class JaCppIncomingServerDelegator : public JaBaseCppIncomingServer,
                                      public msgIOverride
 {
 public:
   NS_DECL_ISUPPORTS_INHERITED
   NS_DECL_MSGIOVERRIDE
 
-  NS_FORWARD_NSIMSGINCOMINGSERVER(DELEGATE_JS(nsIMsgIncomingServer, mJsIMsgIncomingServer)->)
-  NS_FORWARD_NSIINTERFACEREQUESTOR(DELEGATE_JS(nsIInterfaceRequestor, mJsIInterfaceRequestor)->)
+  // use mCppBase as a raw pointer where possible
+  NS_FORWARD_NSIMSGINCOMINGSERVER(DELEGATE_JS(mJsIMsgIncomingServer, mMethods, mCppBase)->)
+  NS_FORWARD_NSIINTERFACEREQUESTOR(DELEGATE_JS(mJsIInterfaceRequestor, mMethods,
+    (nsCOMPtr<nsIInterfaceRequestor>(do_QueryInterface(mCppBase))))->)
 
   JaCppIncomingServerDelegator();
 
 private:
   virtual ~JaCppIncomingServerDelegator() {
   }
 
   // This class will call a method on the delegator, but force the use of the
--- a/mailnews/jsaccount/src/JaMsgFolder.h
+++ b/mailnews/jsaccount/src/JaMsgFolder.h
@@ -59,28 +59,33 @@ class JaCppMsgFolderDelegator : public J
 {
 public:
   NS_DECL_ISUPPORTS_INHERITED
   NS_DECL_MSGIOVERRIDE
 
   // Note that we do not support override of RDF methods.
   NS_FORWARD_NSIRDFRESOURCE(JaBaseCppMsgFolder::)
   NS_FORWARD_NSIRDFNODE(JaBaseCppMsgFolder::)
-  NS_FORWARD_NSIMSGFOLDER(DELEGATE_JS(nsIMsgFolder, mJsIMsgFolder)->)
+  NS_FORWARD_NSIMSGFOLDER(
+    DELEGATE_JS(mJsIMsgFolder, mMethods, mCppBase)->)
   NS_FORWARD_NSIDBCHANGELISTENER(
-    DELEGATE_JS(nsIDBChangeListener, mJsIDBChangeListener)->)
-  NS_FORWARD_NSIURLLISTENER(DELEGATE_JS(nsIUrlListener, mJsIUrlListener)->)
+    DELEGATE_JS(mJsIDBChangeListener, mMethods,
+      (nsCOMPtr<nsIDBChangeListener>(do_QueryInterface(mCppBase))))->)
+  NS_FORWARD_NSIURLLISTENER(
+    DELEGATE_JS(mJsIUrlListener, mMethods,
+      (nsCOMPtr<nsIUrlListener>(do_QueryInterface(mCppBase))))->)
   NS_FORWARD_NSIJUNKMAILCLASSIFICATIONLISTENER(
-      DELEGATE_JS(nsIJunkMailClassificationListener,
-                  mJsIJunkMailClassificationListener)->)
+    DELEGATE_JS(mJsIJunkMailClassificationListener, mMethods,
+      (nsCOMPtr<nsIJunkMailClassificationListener>(do_QueryInterface(mCppBase))))->)
   NS_FORWARD_NSIMSGTRAITCLASSIFICATIONLISTENER(
-      DELEGATE_JS(nsIMsgTraitClassificationListener,
-                  mJsIMsgTraitClassificationListener)->)
+    DELEGATE_JS(mJsIMsgTraitClassificationListener, mMethods,
+      (nsCOMPtr<nsIMsgTraitClassificationListener>(do_QueryInterface(mCppBase))))->)
   NS_FORWARD_NSIINTERFACEREQUESTOR(
-      DELEGATE_JS(nsIInterfaceRequestor, mJsIInterfaceRequestor)->)
+    DELEGATE_JS(mJsIInterfaceRequestor, mMethods,
+      (nsCOMPtr<nsIInterfaceRequestor>(do_QueryInterface(mCppBase))))->)
 
   JaCppMsgFolderDelegator();
 
 private:
   virtual ~JaCppMsgFolderDelegator() {
   }
 
   class Super : public nsIMsgFolder,
--- a/mailnews/jsaccount/src/JaSend.h
+++ b/mailnews/jsaccount/src/JaSend.h
@@ -42,21 +42,23 @@ protected:
 
 class JaCppSendDelegator : public JaBaseCppSend,
                            public msgIOverride
 {
 public:
   NS_DECL_ISUPPORTS_INHERITED
   NS_DECL_MSGIOVERRIDE
 
-  NS_FORWARD_NSIMSGSEND(DELEGATE_JS(nsIMsgSend, mJsIMsgSend)->)
+  NS_FORWARD_NSIMSGSEND(DELEGATE_JS(mJsIMsgSend, mMethods, mCppBase)->)
   NS_FORWARD_NSIMSGOPERATIONLISTENER(
-      DELEGATE_JS(nsIMsgOperationListener, mJsIMsgOperationListener)->)
+    DELEGATE_JS(mJsIMsgOperationListener, mMethods,
+      (nsCOMPtr<nsIMsgOperationListener>(do_QueryInterface(mCppBase))))->)
   NS_FORWARD_NSIINTERFACEREQUESTOR(
-      DELEGATE_JS(nsIInterfaceRequestor, mJsIInterfaceRequestor)->)
+    DELEGATE_JS(mJsIInterfaceRequestor, mMethods,
+      (nsCOMPtr<nsIInterfaceRequestor>(do_QueryInterface(mCppBase))))->)
 
   JaCppSendDelegator();
 
 private:
   virtual ~JaCppSendDelegator() {
   }
 
   // This class will call a method on the delegator, but force the use of the
--- a/mailnews/jsaccount/src/JaUrl.h
+++ b/mailnews/jsaccount/src/JaUrl.h
@@ -71,18 +71,20 @@ protected:
 
 class JaCppUrlDelegator : public JaBaseCppUrl,
                           public msgIOverride
 {
 public:
   NS_DECL_ISUPPORTS_INHERITED
   NS_DECL_MSGIOVERRIDE
 
-  NS_FORWARD_NSIMSGMESSAGEURL(DELEGATE_JS(nsIMsgMessageUrl, mJsIMsgMessageUrl)->)
-  NS_FORWARD_NSIINTERFACEREQUESTOR(DELEGATE_JS(nsIInterfaceRequestor, mJsIInterfaceRequestor)->)
+  NS_FORWARD_NSIMSGMESSAGEURL(DELEGATE_JS(mJsIMsgMessageUrl, mMethods,
+    (nsCOMPtr<nsIMsgMessageUrl>(do_QueryInterface(mCppBase))))->)
+  NS_FORWARD_NSIINTERFACEREQUESTOR(DELEGATE_JS(mJsIInterfaceRequestor, mMethods,
+    (nsCOMPtr<nsIInterfaceRequestor>(do_QueryInterface(mCppBase))))->)
 
   JaCppUrlDelegator();
 
   class Super : public nsIMsgMailNewsUrl,
                 public nsIURIWithSpecialOrigin,
                 public nsIMsgMessageUrl,
                 public msgIJaUrl,
                 public nsIInterfaceRequestor,