Bug 1142503 - don't use QueryInterface when the compiler can do the cast for us; r=ehsan

Calling QueryInterface with a statically known IID should typically not
be necessary.  In those cases where it's not, the compiler can do the
cast for us, though we have to supply the reference-counting that
QueryInterface would do.

In passing, several redundant null-checks for the result of |new T| have
been deleted.
This commit is contained in:
Nathan Froyd
2015-03-12 09:43:50 -04:00
parent 6e971c203a
commit 26a0b56c7a
13 changed files with 43 additions and 48 deletions

View File

@@ -385,9 +385,7 @@ nsComposerCommandsUpdater::Notify(nsITimer *timer)
nsresult nsresult
NS_NewComposerCommandsUpdater(nsISelectionListener** aInstancePtrResult) NS_NewComposerCommandsUpdater(nsISelectionListener** aInstancePtrResult)
{ {
nsComposerCommandsUpdater* newThang = new nsComposerCommandsUpdater; nsRefPtr<nsComposerCommandsUpdater> newThang = new nsComposerCommandsUpdater;
NS_ENSURE_TRUE(newThang, NS_ERROR_OUT_OF_MEMORY); newThang.forget(aInstancePtrResult);
return NS_OK;
return newThang->QueryInterface(NS_GET_IID(nsISelectionListener),
(void **)aInstancePtrResult);
} }

View File

@@ -254,7 +254,7 @@ nsresult NS_NewHTMLURIRefObject(nsIURIRefObject** aResult, nsIDOMNode* aNode)
*aResult = 0; *aResult = 0;
return rv; return rv;
} }
return refObject->QueryInterface(NS_GET_IID(nsIURIRefObject), refObject.forget(aResult);
(void**)aResult); return NS_OK;
} }

View File

@@ -2000,7 +2000,7 @@ nsTextServicesDocument::GetDocumentContentRootNode(nsIDOMNode **aNode)
NS_ENSURE_TRUE(bodyElement, NS_ERROR_FAILURE); NS_ENSURE_TRUE(bodyElement, NS_ERROR_FAILURE);
result = bodyElement->QueryInterface(NS_GET_IID(nsIDOMNode), (void **)aNode); bodyElement.forget(aNode);
} }
else else
{ {
@@ -2014,7 +2014,7 @@ nsTextServicesDocument::GetDocumentContentRootNode(nsIDOMNode **aNode)
NS_ENSURE_TRUE(docElement, NS_ERROR_FAILURE); NS_ENSURE_TRUE(docElement, NS_ERROR_FAILURE);
result = docElement->QueryInterface(NS_GET_IID(nsIDOMNode), (void **)aNode); docElement.forget(aNode);
} }
return result; return result;

View File

@@ -288,10 +288,10 @@ nsControllerCommandGroup::IsCommandInGroup(const char *aCommand, const char *aGr
NS_IMETHODIMP NS_IMETHODIMP
nsControllerCommandGroup::GetGroupsEnumerator(nsISimpleEnumerator **_retval) nsControllerCommandGroup::GetGroupsEnumerator(nsISimpleEnumerator **_retval)
{ {
nsGroupsEnumerator *groupsEnum = new nsGroupsEnumerator(mGroupsHash); nsRefPtr<nsGroupsEnumerator> groupsEnum = new nsGroupsEnumerator(mGroupsHash);
if (!groupsEnum) return NS_ERROR_OUT_OF_MEMORY;
return groupsEnum->QueryInterface(NS_GET_IID(nsISimpleEnumerator), (void **)_retval); groupsEnum.forget(_retval);
return NS_OK;
} }
/* nsISimpleEnumerator getEnumeratorForGroup (in DOMString aGroup); */ /* nsISimpleEnumerator getEnumeratorForGroup (in DOMString aGroup); */
@@ -301,10 +301,10 @@ nsControllerCommandGroup::GetEnumeratorForGroup(const char *aGroup, nsISimpleEnu
nsDependentCString groupKey(aGroup); nsDependentCString groupKey(aGroup);
nsTArray<nsCString> *commandList = mGroupsHash.Get(groupKey); // may be null nsTArray<nsCString> *commandList = mGroupsHash.Get(groupKey); // may be null
nsNamedGroupEnumerator *theGroupEnum = new nsNamedGroupEnumerator(commandList); nsRefPtr<nsNamedGroupEnumerator> theGroupEnum = new nsNamedGroupEnumerator(commandList);
if (!theGroupEnum) return NS_ERROR_OUT_OF_MEMORY;
return theGroupEnum->QueryInterface(NS_GET_IID(nsISimpleEnumerator), (void **)_retval); theGroupEnum.forget(_retval);
return NS_OK;
} }
#if 0 #if 0

View File

@@ -409,8 +409,8 @@ nsresult nsWebBrowserFind::GetRootNode(nsIDOMDocument* aDomDoc,
rv = htmlDoc->GetBody(getter_AddRefs(bodyElement)); rv = htmlDoc->GetBody(getter_AddRefs(bodyElement));
NS_ENSURE_SUCCESS(rv, rv); NS_ENSURE_SUCCESS(rv, rv);
NS_ENSURE_ARG_POINTER(bodyElement); NS_ENSURE_ARG_POINTER(bodyElement);
return bodyElement->QueryInterface(NS_GET_IID(nsIDOMNode), bodyElement.forget(aNode);
(void **)aNode); return NS_OK;
} }
// For non-HTML documents, the content root node will be the doc element. // For non-HTML documents, the content root node will be the doc element.
@@ -418,7 +418,8 @@ nsresult nsWebBrowserFind::GetRootNode(nsIDOMDocument* aDomDoc,
rv = aDomDoc->GetDocumentElement(getter_AddRefs(docElement)); rv = aDomDoc->GetDocumentElement(getter_AddRefs(docElement));
NS_ENSURE_SUCCESS(rv, rv); NS_ENSURE_SUCCESS(rv, rv);
NS_ENSURE_ARG_POINTER(docElement); NS_ENSURE_ARG_POINTER(docElement);
return docElement->QueryInterface(NS_GET_IID(nsIDOMNode), (void **)aNode); docElement.forget(aNode);
return NS_OK;
} }
nsresult nsWebBrowserFind::SetRangeAroundDocument(nsIDOMRange* aSearchRange, nsresult nsWebBrowserFind::SetRangeAroundDocument(nsIDOMRange* aSearchRange,

View File

@@ -651,9 +651,8 @@ nsStringBundleService::CreateExtensibleBundle(const char* aCategory,
return res; return res;
} }
res = bundle->QueryInterface(NS_GET_IID(nsIStringBundle), (void**) aResult); bundle.forget(aResult);
return NS_OK;
return res;
} }
#define GLOBAL_PROPERTIES "chrome://global/locale/global-strres.properties" #define GLOBAL_PROPERTIES "chrome://global/locale/global-strres.properties"

View File

@@ -270,17 +270,13 @@ PSMContentListener::DoContent(const nsACString & aContentType,
nsIStreamListener ** aContentHandler, nsIStreamListener ** aContentHandler,
bool * aAbortProcess) bool * aAbortProcess)
{ {
PSMContentDownloader *downLoader;
uint32_t type; uint32_t type;
type = getPSMContentType(PromiseFlatCString(aContentType).get()); type = getPSMContentType(PromiseFlatCString(aContentType).get());
PR_LOG(gPIPNSSLog, PR_LOG_DEBUG, ("PSMContentListener::DoContent\n")); PR_LOG(gPIPNSSLog, PR_LOG_DEBUG, ("PSMContentListener::DoContent\n"));
if (type != PSMContentDownloader::UNKNOWN_TYPE) { if (type != PSMContentDownloader::UNKNOWN_TYPE) {
downLoader = new PSMContentDownloader(type); nsRefPtr<PSMContentDownloader> downLoader = new PSMContentDownloader(type);
if (downLoader) { downLoader.forget(aContentHandler);
downLoader->QueryInterface(NS_GET_IID(nsIStreamListener), return NS_OK;
(void **)aContentHandler);
return NS_OK;
}
} }
return NS_ERROR_FAILURE; return NS_ERROR_FAILURE;
} }

View File

@@ -4323,8 +4323,9 @@ nsNavHistoryResult::GetRoot(nsINavHistoryContainerResultNode** aRoot)
*aRoot = nullptr; *aRoot = nullptr;
return NS_ERROR_FAILURE; return NS_ERROR_FAILURE;
} }
return mRootNode->QueryInterface(NS_GET_IID(nsINavHistoryContainerResultNode), nsRefPtr<nsNavHistoryContainerResultNode> node(mRootNode);
reinterpret_cast<void**>(aRoot)); node.forget(aRoot);
return NS_OK;
} }

View File

@@ -1110,8 +1110,9 @@ nsPrintSettings::GetPageRanges(nsTArray<int32_t> &aPages)
nsresult nsresult
nsPrintSettings::_Clone(nsIPrintSettings **_retval) nsPrintSettings::_Clone(nsIPrintSettings **_retval)
{ {
nsPrintSettings* printSettings = new nsPrintSettings(*this); nsRefPtr<nsPrintSettings> printSettings = new nsPrintSettings(*this);
return printSettings->QueryInterface(NS_GET_IID(nsIPrintSettings), (void**)_retval); // ref counts printSettings.forget(_retval);
return NS_OK;
} }
/* nsIPrintSettings clone (); */ /* nsIPrintSettings clone (); */

View File

@@ -1,4 +1,4 @@
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public /* 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 * 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/. */ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
@@ -251,8 +251,9 @@ nsImageFromClipboard ::GetEncodedImageStream (unsigned char * aClipboardData, co
if (NS_SUCCEEDED(rv)){ if (NS_SUCCEEDED(rv)){
rv = encoder->InitFromData(rgbData, 0, width, height, 3 * width /* RGB * # pixels in a row */, rv = encoder->InitFromData(rgbData, 0, width, height, 3 * width /* RGB * # pixels in a row */,
imgIEncoder::INPUT_FORMAT_RGB, EmptyString()); imgIEncoder::INPUT_FORMAT_RGB, EmptyString());
if (NS_SUCCEEDED(rv)) if (NS_SUCCEEDED(rv)) {
encoder->QueryInterface(NS_GET_IID(nsIInputStream), (void **) aInputStream); encoder.forget(aInputStream);
}
} }
} }
delete [] rgbData; delete [] rgbData;

View File

@@ -118,8 +118,9 @@ NS_IMETHODIMP nsPrintSettingsWin::SetDevMode(DEVMODEW * aDevMode)
nsresult nsresult
nsPrintSettingsWin::_Clone(nsIPrintSettings **_retval) nsPrintSettingsWin::_Clone(nsIPrintSettings **_retval)
{ {
nsPrintSettingsWin* printSettings = new nsPrintSettingsWin(*this); nsRefPtr<nsPrintSettingsWin> printSettings = new nsPrintSettingsWin(*this);
return printSettings->QueryInterface(NS_GET_IID(nsIPrintSettings), (void**)_retval); // ref counts printSettings.forget(_retval);
return NS_OK;
} }
//------------------------------------------- //-------------------------------------------

View File

@@ -228,7 +228,8 @@ nsAppFileLocationProvider::GetFile(const char* aProp, bool* aPersistent,
} }
if (localFile && NS_SUCCEEDED(rv)) { if (localFile && NS_SUCCEEDED(rv)) {
return localFile->QueryInterface(NS_GET_IID(nsIFile), (void**)aResult); localFile.forget(aResult);
return NS_OK;
} }
return rv; return rv;

View File

@@ -195,11 +195,9 @@ nsWindowMediator::GetEnumerator(const char16_t* inType, nsISimpleEnumerator** ou
NS_ENSURE_ARG_POINTER(outEnumerator); NS_ENSURE_ARG_POINTER(outEnumerator);
NS_ENSURE_STATE(mReady); NS_ENSURE_STATE(mReady);
nsAppShellWindowEnumerator *enumerator = new nsASDOMWindowEarlyToLateEnumerator(inType, *this); nsRefPtr<nsAppShellWindowEnumerator> enumerator = new nsASDOMWindowEarlyToLateEnumerator(inType, *this);
if (enumerator) enumerator.forget(outEnumerator);
return enumerator->QueryInterface(NS_GET_IID(nsISimpleEnumerator) , (void**)outEnumerator); return NS_OK;
return NS_ERROR_OUT_OF_MEMORY;
} }
NS_IMETHODIMP NS_IMETHODIMP
@@ -209,11 +207,9 @@ nsWindowMediator::GetXULWindowEnumerator(const char16_t* inType, nsISimpleEnumer
NS_ENSURE_ARG_POINTER(outEnumerator); NS_ENSURE_ARG_POINTER(outEnumerator);
NS_ENSURE_STATE(mReady); NS_ENSURE_STATE(mReady);
nsAppShellWindowEnumerator *enumerator = new nsASXULWindowEarlyToLateEnumerator(inType, *this); nsRefPtr<nsAppShellWindowEnumerator> enumerator = new nsASXULWindowEarlyToLateEnumerator(inType, *this);
if (enumerator) enumerator.forget(outEnumerator);
return enumerator->QueryInterface(NS_GET_IID(nsISimpleEnumerator) , (void**)outEnumerator); return NS_OK;
return NS_ERROR_OUT_OF_MEMORY;
} }
NS_IMETHODIMP NS_IMETHODIMP