Bug 1435671 - Reduce binary size by making NS_MutateURI.Apply not be a templated method r=mayhemer

We instead add a templated method NS_MutatorMethod that returns a std::function<nsresult(nsIURIMutator*)> which Apply then calls with mMutator as an argument.
The function returned by NS_MutatorMethod performs a QueryInterface, then calls the passed method with arguments on the result.

MozReview-Commit-ID: Jjqp7gGLG1D

--HG--
extra : rebase_source : f2a17aee7bb66a7ba8652817d43b9aa7ec7ef710
This commit is contained in:
Valentin Gosu 2018-02-21 01:00:54 +01:00
parent e7811b0211
commit 55a7a824c6
18 changed files with 133 additions and 103 deletions

View File

@ -78,10 +78,12 @@ nsChromeProtocolHandler::NewURI(const nsACString &aSpec,
// by standard URLs, so there is no "outer" given to CreateInstance
nsresult rv;
nsCOMPtr<nsIURL> surl;
nsCOMPtr<nsIURI> base(aBaseURI);
rv = NS_MutateURI(new mozilla::net::nsStandardURL::Mutator())
.Apply<nsIStandardURLMutator>(&nsIStandardURLMutator::Init,
nsIStandardURL::URLTYPE_STANDARD, -1,
nsCString(aSpec), aCharset, aBaseURI, nullptr)
.Apply(NS_MutatorMethod(&nsIStandardURLMutator::Init,
nsIStandardURL::URLTYPE_STANDARD,
-1, nsCString(aSpec), aCharset,
base, nullptr))
.Finalize(surl);
if (NS_FAILED(rv)) {
return rv;

View File

@ -606,9 +606,8 @@ DragDataProducer::Produce(DataTransfer* aDataTransfer,
mimeInfo->GetPrimaryExtension(primaryExtension);
rv = NS_MutateURI(imgUrl)
.Apply<nsIURLMutator>(&nsIURLMutator::SetFileExtension,
primaryExtension,
nullptr)
.Apply(NS_MutatorMethod(&nsIURLMutator::SetFileExtension,
primaryExtension, nullptr))
.Finalize(imgUrl);
NS_ENSURE_SUCCESS(rv, rv);
}

View File

@ -656,9 +656,8 @@ static nsresult AppendImagePromise(nsITransferable* aTransferable,
mimeInfo->GetPrimaryExtension(primaryExtension);
rv = NS_MutateURI(imgUri)
.Apply<nsIURLMutator>(&nsIURLMutator::SetFileExtension,
primaryExtension,
nullptr)
.Apply(NS_MutatorMethod(&nsIURLMutator::SetFileExtension,
primaryExtension, nullptr))
.Finalize(imgUrl);
NS_ENSURE_SUCCESS(rv, rv);
}

View File

@ -900,8 +900,8 @@ nsHostObjectProtocolHandler::NewURI(const nsACString& aSpec,
nsCOMPtr<nsIURI> uri;
rv = NS_MutateURI(new nsHostObjectURI::Mutator())
.SetSpec(aSpec)
.Apply<nsIBlobURIMutator>(&nsIBlobURIMutator::SetBlobImpl, blob)
.Apply<nsIPrincipalURIMutator>(&nsIPrincipalURIMutator::SetPrincipal, principal)
.Apply(NS_MutatorMethod(&nsIBlobURIMutator::SetBlobImpl, blob))
.Apply(NS_MutatorMethod(&nsIPrincipalURIMutator::SetPrincipal, principal))
.Finalize(uri);
NS_ENSURE_SUCCESS(rv, rv);

View File

@ -647,11 +647,11 @@ URLWorker::Init(const nsAString& aURL, const Optional<nsAString>& aBase,
}
nsCOMPtr<nsIURI> uri;
rv = NS_MutateURI(new nsStandardURL::Mutator())
.Apply<nsIStandardURLMutator>(&nsIStandardURLMutator::Init,
nsIStandardURL::URLTYPE_STANDARD, -1,
NS_ConvertUTF16toUTF8(aURL),
nullptr, baseURL, nullptr)
.Finalize(uri);
.Apply(NS_MutatorMethod(&nsIStandardURLMutator::Init,
nsIStandardURL::URLTYPE_STANDARD,
-1, NS_ConvertUTF16toUTF8(aURL),
nullptr, baseURL, nullptr))
.Finalize(uri);
aRv = rv;
if (NS_SUCCEEDED(rv)) {
mStdURL = static_cast<nsStandardURL*>(uri.get());

View File

@ -2120,11 +2120,10 @@ nsWebBrowserPersist::CalculateUniqueFilename(nsIURI *aURI, nsCOMPtr<nsIURI>& aOu
}
else
{
return NS_MutateURI(url)
.Apply<nsIURLMutator>(&nsIURLMutator::SetFileName,
filename,
nullptr)
.Finalize(aOutURI);
return NS_MutateURI(url)
.Apply(NS_MutatorMethod(&nsIURLMutator::SetFileName,
filename, nullptr))
.Finalize(aOutURI);
}
}
@ -2298,11 +2297,10 @@ nsWebBrowserPersist::CalculateAndAppendFileExt(nsIURI *aURI,
}
else
{
return NS_MutateURI(url)
.Apply<nsIURLMutator>(&nsIURLMutator::SetFileName,
newFileName,
nullptr)
.Finalize(aOutURI);
return NS_MutateURI(url)
.Apply(NS_MutatorMethod(&nsIURLMutator::SetFileName,
newFileName, nullptr))
.Finalize(aOutURI);
}
}

View File

@ -97,11 +97,10 @@ nsJARURI::CreateEntryURL(const nsACString& entryFilename,
// Flatten the concatenation, just in case. See bug 128288
nsAutoCString spec(NS_BOGUS_ENTRY_SCHEME + entryFilename);
return NS_MutateURI(NS_STANDARDURLMUTATOR_CONTRACTID)
.Apply<nsIStandardURLMutator>(&nsIStandardURLMutator::Init,
nsIStandardURL::URLTYPE_NO_AUTHORITY, -1,
spec, charset, nullptr,
nullptr)
.Finalize(url);
.Apply(NS_MutatorMethod(&nsIStandardURLMutator::Init,
nsIStandardURL::URLTYPE_NO_AUTHORITY, -1,
spec, charset, nullptr, nullptr))
.Finalize(url);
}
////////////////////////////////////////////////////////////////////////////////
@ -334,11 +333,10 @@ nsJARURI::SetSpecWithBase(const nsACString &aSpec, nsIURI* aBaseURL)
nsCOMPtr<nsIURI> entry;
rv = NS_MutateURI(NS_STANDARDURLMUTATOR_CONTRACTID)
.Apply<nsIStandardURLMutator>(&nsIStandardURLMutator::Init,
nsIStandardURL::URLTYPE_NO_AUTHORITY, -1,
nsCString(aSpec), mCharsetHint.get(),
otherJAR->mJAREntry,
nullptr)
.Apply(NS_MutatorMethod(&nsIStandardURLMutator::Init,
nsIStandardURL::URLTYPE_NO_AUTHORITY,
-1, nsCString(aSpec), mCharsetHint.get(),
otherJAR->mJAREntry, nullptr))
.Finalize(entry);
if (NS_FAILED(rv)) {
return rv;
@ -716,10 +714,9 @@ nsresult
nsJARURI::SetFileNameInternal(const nsACString& fileName)
{
return NS_MutateURI(mJAREntry)
.Apply<nsIURLMutator>(&nsIURLMutator::SetFileName,
nsCString(fileName),
nullptr)
.Finalize(mJAREntry);
.Apply(NS_MutatorMethod(&nsIURLMutator::SetFileName,
nsCString(fileName), nullptr))
.Finalize(mJAREntry);
}
NS_IMETHODIMP
@ -732,10 +729,9 @@ nsresult
nsJARURI::SetFileBaseNameInternal(const nsACString& fileBaseName)
{
return NS_MutateURI(mJAREntry)
.Apply<nsIURLMutator>(&nsIURLMutator::SetFileBaseName,
nsCString(fileBaseName),
nullptr)
.Finalize(mJAREntry);
.Apply(NS_MutatorMethod(&nsIURLMutator::SetFileBaseName,
nsCString(fileBaseName), nullptr))
.Finalize(mJAREntry);
}
NS_IMETHODIMP
@ -748,10 +744,9 @@ nsresult
nsJARURI::SetFileExtensionInternal(const nsACString& fileExtension)
{
return NS_MutateURI(mJAREntry)
.Apply<nsIURLMutator>(&nsIURLMutator::SetFileExtension,
nsCString(fileExtension),
nullptr)
.Finalize(mJAREntry);
.Apply(NS_MutatorMethod(&nsIURLMutator::SetFileExtension,
nsCString(fileExtension), nullptr))
.Finalize(mJAREntry);
}
NS_IMETHODIMP

View File

@ -11,6 +11,7 @@ interface nsIURIMutator;
%{C++
#include "nsString.h"
#include "nsCOMPtr.h"
#include <functional>
#undef SetPort // XXX Windows!
@ -271,6 +272,45 @@ interface nsIURIMutator : nsIURISetters
%{C++
// This templated struct is used to extract the class type of the method
// passed to NS_MutatorMethod.
template <typename Method>
struct nsMethodTypeTraits;
template <class C, typename R, typename... As>
struct nsMethodTypeTraits<R(C::*)(As...)>
{
typedef C class_type;
};
#ifdef NS_HAVE_STDCALL
template <class C, typename R, typename... As>
struct nsMethodTypeTraits<R(__stdcall C::*)(As...)>
{
typedef C class_type;
};
#endif
// This helper returns a std::function that will be applied on the
// nsIURIMutator. The type of `Interface` will be deduced from the method type.
// aMethod will be called on the target object if it successfully QIs to
// `Interface`, and the arguments will be passed to the call.
template <typename Method, typename... Args>
const std::function<nsresult(nsIURIMutator*)>
NS_MutatorMethod(Method aMethod, Args ...aArgs)
{
// Capture arguments by value, otherwise we crash.
return [=](nsIURIMutator* aMutator) {
typedef typename nsMethodTypeTraits<Method>::class_type Interface;
nsresult rv;
nsCOMPtr<Interface> target = do_QueryInterface(aMutator, &rv);
NS_ENSURE_SUCCESS(rv, rv);
rv = (target->*aMethod)(aArgs...);
NS_ENSURE_SUCCESS(rv, rv);
return NS_OK;
};
}
// This class provides a useful helper that allows chaining of setter operations
class MOZ_STACK_CLASS NS_MutateURI
{
@ -377,7 +417,7 @@ public:
* nsCOMPtr<nsIURI> uri;
* nsresult rv = NS_MutateURI(new URIClass::Mutator())
* .SetSpec(aSpec)
* .Apply<SomeInterface>(&SomeInterface::Method, arg1, arg2)
* .Apply(NS_MutatorMethod(&SomeInterface::Method, arg1, arg2))
* .Finalize(uri);
*
* If mMutator does not implement SomeInterface, do_QueryInterface will fail
@ -385,13 +425,11 @@ public:
* If aMethod does not exist, or if there is a mismatch between argument
* types, or the number of arguments, then there will be a compile error.
*/
template <typename Interface, typename Method, typename... Args>
NS_MutateURI& Apply(Method aMethod, Args ...aArgs)
NS_MutateURI& Apply(const std::function<nsresult(nsIURIMutator*)>& aFunction)
{
NS_ENSURE_SUCCESS(mStatus, *this);
nsCOMPtr<Interface> target = do_QueryInterface(mMutator, &mStatus);
mStatus = aFunction(mMutator);
NS_ENSURE_SUCCESS(mStatus, *this);
mStatus = (target->*aMethod)(aArgs...);
return *this;
}

View File

@ -3063,7 +3063,7 @@ NS_GetSecureUpgradedURI(nsIURI* aURI, nsIURI** aUpgradedURI)
// Change the default port to 443:
nsCOMPtr<nsIStandardURL> stdURL = do_QueryInterface(aURI);
if (stdURL) {
mutator.Apply<nsIStandardURLMutator>(&nsIStandardURLMutator::SetDefaultPort, 443, nullptr);
mutator.Apply(NS_MutatorMethod(&nsIStandardURLMutator::SetDefaultPort, 443, nullptr));
} else {
// If we don't have a nsStandardURL, fall back to using GetPort/SetPort.
// XXXdholbert Is this function even called with a non-nsStandardURL arg,

View File

@ -313,10 +313,9 @@ TRRService::MaybeBootstrap(const nsACString &aPossible, nsACString &aResult)
nsCOMPtr<nsIURI> url;
nsresult rv = NS_MutateURI(NS_STANDARDURLMUTATOR_CONTRACTID)
.Apply<nsIStandardURLMutator>(&nsIStandardURLMutator::Init,
nsIStandardURL::URLTYPE_STANDARD, 443,
mPrivateURI, nullptr, nullptr,
nullptr)
.Apply(NS_MutatorMethod(&nsIStandardURLMutator::Init,
nsIStandardURL::URLTYPE_STANDARD, 443,
mPrivateURI, nullptr, nullptr, nullptr))
.Finalize(url);
if (NS_FAILED(rv)) {
LOG(("TRRService::MaybeBootstrap failed to create URI!\n"));

View File

@ -164,7 +164,7 @@ nsFileProtocolHandler::GetProtocolFlags(uint32_t *result)
NS_IMETHODIMP
nsFileProtocolHandler::NewURI(const nsACString &spec,
const char *charset,
nsIURI *baseURI,
nsIURI *aBaseURI,
nsIURI **result)
{
nsCOMPtr<nsIURI> url = new nsStandardURL(true);
@ -177,12 +177,12 @@ nsFileProtocolHandler::NewURI(const nsACString &spec,
}
#endif
nsCOMPtr<nsIURI> base(aBaseURI);
return NS_MutateURI(url)
.Apply<nsIStandardURLMutator>(&nsIStandardURLMutator::Init,
nsIStandardURL::URLTYPE_NO_AUTHORITY, -1,
buf, charset, baseURI,
nullptr)
.Finalize(result);
.Apply(NS_MutatorMethod(&nsIStandardURLMutator::Init,
nsIStandardURL::URLTYPE_NO_AUTHORITY,
-1, buf, charset, base, nullptr))
.Finalize(result);
}
NS_IMETHODIMP

View File

@ -183,13 +183,12 @@ nsFtpProtocolHandler::NewURI(const nsACString &aSpec,
if (spec.FindCharInSet(CRLF) >= 0 || spec.FindChar('\0') >= 0)
return NS_ERROR_MALFORMED_URI;
nsCOMPtr<nsIURI> url;
nsCOMPtr<nsIURI> base(aBaseURI);
return NS_MutateURI(NS_STANDARDURLMUTATOR_CONTRACTID)
.Apply<nsIStandardURLMutator>(&nsIStandardURLMutator::Init,
nsIStandardURL::URLTYPE_AUTHORITY, 21,
nsCString(aSpec), aCharset, aBaseURI,
nullptr)
.Finalize(result);
.Apply(NS_MutatorMethod(&nsIStandardURLMutator::Init,
nsIStandardURL::URLTYPE_AUTHORITY,
21, nsCString(aSpec), aCharset, base, nullptr))
.Finalize(result);
}
NS_IMETHODIMP

View File

@ -1032,12 +1032,12 @@ nsGIOProtocolHandler::NewURI(const nsACString &aSpec,
}
}
nsCOMPtr<nsIURI> base(aBaseURI);
return NS_MutateURI(NS_STANDARDURLMUTATOR_CONTRACTID)
.Apply<nsIStandardURLMutator>(&nsIStandardURLMutator::Init,
nsIStandardURL::URLTYPE_STANDARD, -1,
flatSpec, aOriginCharset, aBaseURI,
nullptr)
.Finalize(aResult);
.Apply(NS_MutatorMethod(&nsIStandardURLMutator::Init,
nsIStandardURL::URLTYPE_STANDARD,
-1, flatSpec, aOriginCharset, base, nullptr))
.Finalize(aResult);
}
NS_IMETHODIMP

View File

@ -356,14 +356,12 @@ Http2Stream::MakeOriginURL(const nsACString &scheme, const nsACString &origin,
nsCOMPtr<nsIURI> &url)
{
return NS_MutateURI(new nsStandardURL::Mutator())
.Apply<nsIStandardURLMutator>(&nsIStandardURLMutator::Init,
nsIStandardURL::URLTYPE_AUTHORITY,
scheme.EqualsLiteral("http") ?
NS_HTTP_DEFAULT_PORT :
NS_HTTPS_DEFAULT_PORT,
nsCString(origin), nullptr, nullptr,
nullptr)
.Finalize(url);
.Apply(NS_MutatorMethod(&nsIStandardURLMutator::Init,
nsIStandardURL::URLTYPE_AUTHORITY,
scheme.EqualsLiteral("http") ? NS_HTTP_DEFAULT_PORT
: NS_HTTPS_DEFAULT_PORT,
nsCString(origin), nullptr, nullptr, nullptr))
.Finalize(url);
}
void

View File

@ -136,12 +136,13 @@ NewURI(const nsACString &aSpec,
int32_t aDefaultPort,
nsIURI **aURI)
{
nsCOMPtr<nsIURI> base(aBaseURI);
return NS_MutateURI(new nsStandardURL::Mutator())
.Apply<nsIStandardURLMutator>(&nsIStandardURLMutator::Init,
nsIStandardURL::URLTYPE_AUTHORITY,
aDefaultPort, nsCString(aSpec), aCharset, aBaseURI,
nullptr)
.Finalize(aURI);
.Apply(NS_MutatorMethod(&nsIStandardURLMutator::Init,
nsIStandardURL::URLTYPE_AUTHORITY,
aDefaultPort, nsCString(aSpec), aCharset,
base, nullptr))
.Finalize(aURI);
}
#ifdef ANDROID

View File

@ -234,12 +234,12 @@ SubstitutingProtocolHandler::NewURI(const nsACString &aSpec,
if (last < src)
spec.Append(last, src-last);
nsCOMPtr<nsIURI> base(aBaseURI);
return NS_MutateURI(new SubstitutingURL::Mutator())
.Apply<nsIStandardURLMutator>(&nsIStandardURLMutator::Init,
nsIStandardURL::URLTYPE_STANDARD, -1,
spec, aCharset, aBaseURI,
nullptr)
.Finalize(result);
.Apply(NS_MutatorMethod(&nsIStandardURLMutator::Init,
nsIStandardURL::URLTYPE_STANDARD,
-1, spec, aCharset, base, nullptr))
.Finalize(result);
}
nsresult

View File

@ -306,12 +306,13 @@ BaseWebSocketChannel::NewURI(const nsACString & aSpec, const char *aOriginCharse
if (NS_FAILED(rv))
return rv;
nsCOMPtr<nsIURI> base(aBaseURI);
return NS_MutateURI(new nsStandardURL::Mutator())
.Apply<nsIStandardURLMutator>(&nsIStandardURLMutator::Init,
nsIStandardURL::URLTYPE_AUTHORITY, port,
nsCString(aSpec), aOriginCharset, aBaseURI,
nullptr)
.Finalize(_retval);
.Apply(NS_MutatorMethod(&nsIStandardURLMutator::Init,
nsIStandardURL::URLTYPE_AUTHORITY,
port, nsCString(aSpec), aOriginCharset,
base, nullptr))
.Finalize(_retval);
}
NS_IMETHODIMP

View File

@ -141,12 +141,13 @@ nsAndroidProtocolHandler::NewURI(const nsACString &aSpec,
nsIURI *aBaseURI,
nsIURI **result)
{
nsCOMPtr<nsIURI> base(aBaseURI);
return NS_MutateURI(NS_STANDARDURLMUTATOR_CONTRACTID)
.Apply<nsIStandardURLMutator>(&nsIStandardURLMutator::Init,
nsIStandardURL::URLTYPE_STANDARD, -1,
nsCString(aSpec), aCharset, aBaseURI,
nullptr)
.Finalize(result);
.Apply(NS_MutatorMethod(&nsIStandardURLMutator::Init,
nsIStandardURL::URLTYPE_STANDARD,
-1, nsCString(aSpec), aCharset,
base, nullptr))
.Finalize(result);
}
NS_IMETHODIMP