Bug 1226479. Change ErrorResult::ThrowTypeError/ThrowRangeError to take string references, not pointers. r=mccr8

This commit is contained in:
Boris Zbarsky 2015-11-20 13:36:46 -05:00
parent b9c0a9adb5
commit d6a774fa3d
16 changed files with 70 additions and 63 deletions

View File

@ -37,7 +37,7 @@ ArchiveReader::Constructor(const GlobalObject& aGlobal,
nsAutoCString encoding;
if (!EncodingUtils::FindEncodingForLabelNoReplacement(aOptions.mEncoding,
encoding)) {
aError.ThrowRangeError<MSG_ENCODING_NOT_SUPPORTED>(&aOptions.mEncoding);
aError.ThrowRangeError<MSG_ENCODING_NOT_SUPPORTED>(aOptions.mEncoding);
return nullptr;
}

View File

@ -78,7 +78,7 @@ URL::Constructor(nsISupports* aParent, const nsAString& aUrl,
nsresult rv = NS_NewURI(getter_AddRefs(baseUri), aBase, nullptr, nullptr,
nsContentUtils::GetIOService());
if (NS_WARN_IF(NS_FAILED(rv))) {
aRv.ThrowTypeError<MSG_INVALID_URL>(&aBase);
aRv.ThrowTypeError<MSG_INVALID_URL>(aBase);
return nullptr;
}
@ -94,7 +94,7 @@ URL::Constructor(nsISupports* aParent, const nsAString& aUrl, nsIURI* aBase,
nsresult rv = NS_NewURI(getter_AddRefs(uri), aUrl, nullptr, aBase,
nsContentUtils::GetIOService());
if (NS_WARN_IF(NS_FAILED(rv))) {
aRv.ThrowTypeError<MSG_INVALID_URL>(&aUrl);
aRv.ThrowTypeError<MSG_INVALID_URL>(aUrl);
return nullptr;
}
@ -229,8 +229,7 @@ URL::SetHref(const nsAString& aHref, ErrorResult& aRv)
nsCOMPtr<nsIURI> uri;
rv = ioService->NewURI(href, nullptr, nullptr, getter_AddRefs(uri));
if (NS_FAILED(rv)) {
nsAutoString label(aHref);
aRv.ThrowTypeError<MSG_INVALID_URL>(&label);
aRv.ThrowTypeError<MSG_INVALID_URL>(aHref);
return;
}

View File

@ -49,19 +49,19 @@ public:
const nsTArray<nsString>& aStringParams) = 0;
// A version of AddConsoleReport() that accepts the message parameters
// as variable nsString arguments. Note, the parameters must be exactly
// nsString and not another string class. All other args the same as
// AddConsoleReport().
// as variable nsString arguments (or really, any sort of const nsAString).
// All other args the same as AddConsoleReport().
template<typename... Params>
void
AddConsoleReport(uint32_t aErrorFlags, const nsACString& aCategory,
nsContentUtils::PropertiesFile aPropertiesFile,
const nsACString& aSourceFileURI, uint32_t aLineNumber,
uint32_t aColumnNumber, const nsACString& aMessageName,
Params... aParams)
Params&&... aParams)
{
nsTArray<nsString> params;
mozilla::dom::StringArrayAppender::Append(params, sizeof...(Params), aParams...);
mozilla::dom::StringArrayAppender::Append(params, sizeof...(Params),
mozilla::Forward<Params>(aParams)...);
AddConsoleReport(aErrorFlags, aCategory, aPropertiesFile, aSourceFileURI,
aLineNumber, aColumnNumber, aMessageName, params);
}

View File

@ -61,14 +61,14 @@ struct StringArrayAppender
}
template<typename... Ts>
static void Append(nsTArray<nsString>& aArgs, uint16_t aCount, const nsAString* aFirst, Ts... aOtherArgs)
static void Append(nsTArray<nsString>& aArgs, uint16_t aCount, const nsAString& aFirst, Ts&&... aOtherArgs)
{
if (aCount == 0) {
MOZ_ASSERT(false, "There should not be more string arguments provided than are required by the ErrNum.");
return;
}
aArgs.AppendElement(*aFirst);
Append(aArgs, aCount - 1, aOtherArgs...);
aArgs.AppendElement(aFirst);
Append(aArgs, aCount - 1, Forward<Ts>(aOtherArgs)...);
}
};
@ -135,15 +135,17 @@ public:
}
template<dom::ErrNum errorNumber, typename... Ts>
void ThrowTypeError(Ts... messageArgs)
void ThrowTypeError(Ts&&... messageArgs)
{
ThrowErrorWithMessage<errorNumber>(NS_ERROR_TYPE_ERR, messageArgs...);
ThrowErrorWithMessage<errorNumber>(NS_ERROR_TYPE_ERR,
Forward<Ts>(messageArgs)...);
}
template<dom::ErrNum errorNumber, typename... Ts>
void ThrowRangeError(Ts... messageArgs)
void ThrowRangeError(Ts&&... messageArgs)
{
ThrowErrorWithMessage<errorNumber>(NS_ERROR_RANGE_ERR, messageArgs...);
ThrowErrorWithMessage<errorNumber>(NS_ERROR_RANGE_ERR,
Forward<Ts>(messageArgs)...);
}
void ReportErrorWithMessage(JSContext* cx);
@ -264,7 +266,7 @@ private:
nsTArray<nsString>& CreateErrorMessageHelper(const dom::ErrNum errorNumber, nsresult errorType);
template<dom::ErrNum errorNumber, typename... Ts>
void ThrowErrorWithMessage(nsresult errorType, Ts... messageArgs)
void ThrowErrorWithMessage(nsresult errorType, Ts&&... messageArgs)
{
#if defined(DEBUG) && (defined(__clang__) || defined(__GNUC__))
static_assert(dom::ErrorFormatNumArgs[errorNumber] == sizeof...(messageArgs),
@ -275,7 +277,8 @@ private:
nsTArray<nsString>& messageArgsArray = CreateErrorMessageHelper(errorNumber, errorType);
uint16_t argCount = dom::GetErrorArgCount(errorNumber);
dom::StringArrayAppender::Append(messageArgsArray, argCount, messageArgs...);
dom::StringArrayAppender::Append(messageArgsArray, argCount,
Forward<Ts>(messageArgs)...);
#ifdef DEBUG
mUnionState = HasMessage;
#endif // DEBUG

6
dom/cache/Cache.cpp vendored
View File

@ -46,8 +46,8 @@ IsValidPutRequestURL(const nsAString& aUrl, ErrorResult& aRv)
}
if (!validScheme) {
NS_NAMED_LITERAL_STRING(label, "Request");
aRv.ThrowTypeError<MSG_INVALID_URL_SCHEME>(&label, &aUrl);
aRv.ThrowTypeError<MSG_INVALID_URL_SCHEME>(NS_LITERAL_STRING("Request"),
aUrl);
return false;
}
@ -61,7 +61,7 @@ IsValidPutRequestMethod(const Request& aRequest, ErrorResult& aRv)
aRequest.GetMethod(method);
if (!method.LowerCaseEqualsLiteral("get")) {
NS_ConvertASCIItoUTF16 label(method);
aRv.ThrowTypeError<MSG_INVALID_REQUEST_METHOD>(&label);
aRv.ThrowTypeError<MSG_INVALID_REQUEST_METHOD>(label);
return false;
}

View File

@ -162,9 +162,9 @@ TypeUtils::ToCacheRequest(CacheRequest& aOut, InternalRequest* aIn,
if (!schemeValid) {
if (aSchemeAction == TypeErrorOnInvalidScheme) {
NS_NAMED_LITERAL_STRING(label, "Request");
NS_ConvertUTF8toUTF16 urlUTF16(url);
aRv.ThrowTypeError<MSG_INVALID_URL_SCHEME>(&label, &urlUTF16);
aRv.ThrowTypeError<MSG_INVALID_URL_SCHEME>(NS_LITERAL_STRING("Request"),
urlUTF16);
return;
}
}

View File

@ -26,7 +26,7 @@ TextDecoder::Init(const nsAString& aLabel, const bool aFatal,
if (!EncodingUtils::FindEncodingForLabelNoReplacement(aLabel, encoding)) {
nsAutoString label(aLabel);
EncodingUtils::TrimSpaceCharacters(label);
aRv.ThrowRangeError<MSG_ENCODING_NOT_SUPPORTED>(&label);
aRv.ThrowRangeError<MSG_ENCODING_NOT_SUPPORTED>(label);
return;
}
InitWithEncoding(encoding, aFatal);

View File

@ -21,7 +21,7 @@ TextEncoder::Init(const nsAString& aEncoding, ErrorResult& aRv)
// If encoding is failure, or is none of utf-8, utf-16, and utf-16be,
// throw a RangeError (https://encoding.spec.whatwg.org/#dom-textencoder).
if (!EncodingUtils::FindEncodingForLabel(label, mEncoding)) {
aRv.ThrowRangeError<MSG_ENCODING_NOT_SUPPORTED>(&label);
aRv.ThrowRangeError<MSG_ENCODING_NOT_SUPPORTED>(label);
return;
}

View File

@ -180,7 +180,7 @@ InternalHeaders::IsInvalidName(const nsACString& aName, ErrorResult& aRv)
{
if (!NS_IsValidHTTPToken(aName)) {
NS_ConvertUTF8toUTF16 label(aName);
aRv.ThrowTypeError<MSG_INVALID_HEADER_NAME>(&label);
aRv.ThrowTypeError<MSG_INVALID_HEADER_NAME>(label);
return true;
}
@ -193,7 +193,7 @@ InternalHeaders::IsInvalidValue(const nsACString& aValue, ErrorResult& aRv)
{
if (!NS_IsReasonableHTTPHeaderValue(aValue)) {
NS_ConvertUTF8toUTF16 label(aValue);
aRv.ThrowTypeError<MSG_INVALID_HEADER_VALUE>(&label);
aRv.ThrowTypeError<MSG_INVALID_HEADER_VALUE>(label);
return true;
}
return false;

View File

@ -102,7 +102,7 @@ GetRequestURLFromDocument(nsIDocument* aDocument, const nsAString& aInput,
nsCOMPtr<nsIURI> resolvedURI;
aRv = NS_NewURI(getter_AddRefs(resolvedURI), aInput, nullptr, baseURI);
if (NS_WARN_IF(aRv.Failed())) {
aRv.ThrowTypeError<MSG_INVALID_URL>(&aInput);
aRv.ThrowTypeError<MSG_INVALID_URL>(aInput);
return;
}
@ -111,7 +111,7 @@ GetRequestURLFromDocument(nsIDocument* aDocument, const nsAString& aInput,
nsAutoCString credentials;
Unused << resolvedURI->GetUserPass(credentials);
if (!credentials.IsEmpty()) {
aRv.ThrowTypeError<MSG_URL_HAS_CREDENTIALS>(&aInput);
aRv.ThrowTypeError<MSG_URL_HAS_CREDENTIALS>(aInput);
return;
}
@ -141,7 +141,7 @@ GetRequestURLFromChrome(const nsAString& aInput, nsAString& aRequestURL,
nsCOMPtr<nsIURI> uri;
aRv = NS_NewURI(getter_AddRefs(uri), aInput, nullptr, nullptr);
if (NS_WARN_IF(aRv.Failed())) {
aRv.ThrowTypeError<MSG_INVALID_URL>(&aInput);
aRv.ThrowTypeError<MSG_INVALID_URL>(aInput);
return;
}
@ -150,7 +150,7 @@ GetRequestURLFromChrome(const nsAString& aInput, nsAString& aRequestURL,
nsAutoCString credentials;
Unused << uri->GetUserPass(credentials);
if (!credentials.IsEmpty()) {
aRv.ThrowTypeError<MSG_URL_HAS_CREDENTIALS>(&aInput);
aRv.ThrowTypeError<MSG_URL_HAS_CREDENTIALS>(aInput);
return;
}
@ -183,7 +183,7 @@ GetRequestURLFromWorker(const GlobalObject& aGlobal, const nsAString& aInput,
RefPtr<workers::URL> url =
workers::URL::Constructor(aGlobal, aInput, baseURL, aRv);
if (NS_WARN_IF(aRv.Failed())) {
aRv.ThrowTypeError<MSG_INVALID_URL>(&aInput);
aRv.ThrowTypeError<MSG_INVALID_URL>(aInput);
return;
}
@ -200,7 +200,7 @@ GetRequestURLFromWorker(const GlobalObject& aGlobal, const nsAString& aInput,
}
if (!username.IsEmpty() || !password.IsEmpty()) {
aRv.ThrowTypeError<MSG_URL_HAS_CREDENTIALS>(&aInput);
aRv.ThrowTypeError<MSG_URL_HAS_CREDENTIALS>(aInput);
return;
}
@ -316,7 +316,7 @@ Request::Constructor(const GlobalObject& aGlobal,
nsresult rv = FetchUtil::GetValidRequestMethod(method, outMethod);
if (NS_FAILED(rv)) {
NS_ConvertUTF8toUTF16 label(method);
aRv.ThrowTypeError<MSG_INVALID_REQUEST_METHOD>(&label);
aRv.ThrowTypeError<MSG_INVALID_REQUEST_METHOD>(label);
return nullptr;
}
@ -350,7 +350,7 @@ Request::Constructor(const GlobalObject& aGlobal,
nsAutoCString method;
request->GetMethod(method);
NS_ConvertUTF8toUTF16 label(method);
aRv.ThrowTypeError<MSG_INVALID_REQUEST_METHOD>(&label);
aRv.ThrowTypeError<MSG_INVALID_REQUEST_METHOD>(label);
return nullptr;
}

View File

@ -990,7 +990,7 @@ MediaRecorder::Constructor(const GlobalObject& aGlobal,
// Pretending that this constructor is not defined.
NS_NAMED_LITERAL_STRING(argStr, "Argument 1 of MediaRecorder.constructor");
NS_NAMED_LITERAL_STRING(typeStr, "MediaStream");
aRv.ThrowTypeError<MSG_DOES_NOT_IMPLEMENT_INTERFACE>(&argStr, &typeStr);
aRv.ThrowTypeError<MSG_DOES_NOT_IMPLEMENT_INTERFACE>(argStr, typeStr);
return nullptr;
}

View File

@ -2365,8 +2365,7 @@ Notification::ShowPersistentNotification(nsIGlobalObject *aGlobal,
if (NS_WARN_IF(NS_FAILED(loadChecker->Result()))) {
if (loadChecker->Result() == NS_ERROR_NOT_AVAILABLE) {
nsAutoString scope(aScope);
aRv.ThrowTypeError<MSG_NO_ACTIVE_WORKER>(&scope);
aRv.ThrowTypeError<MSG_NO_ACTIVE_WORKER>(aScope);
} else {
aRv.Throw(NS_ERROR_DOM_SECURITY_ERR);
}

View File

@ -162,7 +162,7 @@ ServiceWorkerContainer::Register(const nsAString& aScriptURL,
nsCOMPtr<nsIURI> scriptURI;
rv = NS_NewURI(getter_AddRefs(scriptURI), aScriptURL, nullptr, baseURI);
if (NS_WARN_IF(NS_FAILED(rv))) {
aRv.ThrowTypeError<MSG_INVALID_URL>(&aScriptURL);
aRv.ThrowTypeError<MSG_INVALID_URL>(aScriptURL);
return nullptr;
}
@ -184,7 +184,7 @@ ServiceWorkerContainer::Register(const nsAString& aScriptURL,
nsAutoCString spec;
scriptURI->GetSpec(spec);
NS_ConvertUTF8toUTF16 wSpec(spec);
aRv.ThrowTypeError<MSG_INVALID_SCOPE>(&defaultScope, &wSpec);
aRv.ThrowTypeError<MSG_INVALID_SCOPE>(defaultScope, wSpec);
return nullptr;
}
} else {
@ -195,7 +195,7 @@ ServiceWorkerContainer::Register(const nsAString& aScriptURL,
nsAutoCString spec;
baseURI->GetSpec(spec);
NS_ConvertUTF8toUTF16 wSpec(spec);
aRv.ThrowTypeError<MSG_INVALID_SCOPE>(&aOptions.mScope.Value(), &wSpec);
aRv.ThrowTypeError<MSG_INVALID_SCOPE>(aOptions.mScope.Value(), wSpec);
return nullptr;
}

View File

@ -133,10 +133,14 @@ void
AsyncLog(nsIInterceptedChannel* aInterceptedChannel,
const nsACString& aRespondWithScriptSpec,
uint32_t aRespondWithLineNumber, uint32_t aRespondWithColumnNumber,
const nsACString& aMessageName, Params... aParams)
// We have to list one explicit string so that calls with an
// nsTArray of params won't end up in here.
const nsACString& aMessageName, const nsAString& aFirstParam,
Params&&... aParams)
{
nsTArray<nsString> paramsList(sizeof...(Params));
StringArrayAppender::Append(paramsList, sizeof...(Params), aParams...);
nsTArray<nsString> paramsList(sizeof...(Params) + 1);
StringArrayAppender::Append(paramsList, sizeof...(Params) + 1,
aFirstParam, Forward<Params>(aParams)...);
AsyncLog(aInterceptedChannel, aRespondWithScriptSpec, aRespondWithLineNumber,
aRespondWithColumnNumber, aMessageName, paramsList);
}
@ -295,7 +299,7 @@ private:
if (!mRequestWasHandled) {
::AsyncLog(mInterceptedChannel, mRespondWithScriptSpec,
mRespondWithLineNumber, mRespondWithColumnNumber,
NS_LITERAL_CSTRING("InterceptionFailedWithURL"), &mRequestURL);
NS_LITERAL_CSTRING("InterceptionFailedWithURL"), mRequestURL);
CancelRequest(NS_ERROR_INTERCEPTION_FAILED);
}
}
@ -349,7 +353,7 @@ void RespondWithCopyComplete(void* aClosure, nsresult aStatus)
AsyncLog(data->mInterceptedChannel, data->mRespondWithScriptSpec,
data->mRespondWithLineNumber, data->mRespondWithColumnNumber,
NS_LITERAL_CSTRING("InterceptionFailedWithURL"),
&data->mRequestURL);
data->mRequestURL);
event = new CancelChannelRunnable(data->mInterceptedChannel,
NS_ERROR_INTERCEPTION_FAILED);
}
@ -458,21 +462,22 @@ public:
}
template<typename... Params>
void SetCancelMessage(const nsACString& aMessageName, Params... aParams)
void SetCancelMessage(const nsACString& aMessageName, Params&&... aParams)
{
MOZ_ASSERT(mOwner);
MOZ_ASSERT(mMessageName.EqualsLiteral("InterceptionFailedWithURL"));
MOZ_ASSERT(mParams.Length() == 1);
mMessageName = aMessageName;
mParams.Clear();
StringArrayAppender::Append(mParams, sizeof...(Params), aParams...);
StringArrayAppender::Append(mParams, sizeof...(Params),
Forward<Params>(aParams)...);
}
template<typename... Params>
void SetCancelMessageAndLocation(const nsACString& aSourceSpec,
uint32_t aLine, uint32_t aColumn,
const nsACString& aMessageName,
Params... aParams)
Params&&... aParams)
{
MOZ_ASSERT(mOwner);
MOZ_ASSERT(mMessageName.EqualsLiteral("InterceptionFailedWithURL"));
@ -484,7 +489,8 @@ public:
mMessageName = aMessageName;
mParams.Clear();
StringArrayAppender::Append(mParams, sizeof...(Params), aParams...);
StringArrayAppender::Append(mParams, sizeof...(Params),
Forward<Params>(aParams)...);
}
void Reset()
@ -511,7 +517,7 @@ RespondWithHandler::ResolvedCallback(JSContext* aCx, JS::Handle<JS::Value> aValu
autoCancel.SetCancelMessageAndLocation(sourceSpec, line, column,
NS_LITERAL_CSTRING("InterceptedNonResponseWithURL"),
&mRequestURL, &valueString);
mRequestURL, valueString);
return;
}
@ -526,7 +532,7 @@ RespondWithHandler::ResolvedCallback(JSContext* aCx, JS::Handle<JS::Value> aValu
autoCancel.SetCancelMessageAndLocation(sourceSpec, line, column,
NS_LITERAL_CSTRING("InterceptedNonResponseWithURL"),
&mRequestURL, &valueString);
mRequestURL, valueString);
return;
}
@ -539,7 +545,7 @@ RespondWithHandler::ResolvedCallback(JSContext* aCx, JS::Handle<JS::Value> aValu
if (response->Type() == ResponseType::Opaque &&
!worker->OpaqueInterceptionEnabled()) {
autoCancel.SetCancelMessage(
NS_LITERAL_CSTRING("OpaqueInterceptionDisabledWithURL"), &mRequestURL);
NS_LITERAL_CSTRING("OpaqueInterceptionDisabledWithURL"), mRequestURL);
return;
}
@ -552,7 +558,7 @@ RespondWithHandler::ResolvedCallback(JSContext* aCx, JS::Handle<JS::Value> aValu
if (response->Type() == ResponseType::Error) {
autoCancel.SetCancelMessage(
NS_LITERAL_CSTRING("InterceptedErrorResponseWithURL"), &mRequestURL);
NS_LITERAL_CSTRING("InterceptedErrorResponseWithURL"), mRequestURL);
return;
}
@ -565,19 +571,19 @@ RespondWithHandler::ResolvedCallback(JSContext* aCx, JS::Handle<JS::Value> aValu
autoCancel.SetCancelMessage(
NS_LITERAL_CSTRING("BadOpaqueInterceptionRequestModeWithURL"),
&mRequestURL, &modeString);
mRequestURL, modeString);
return;
}
if (!mIsNavigationRequest && response->Type() == ResponseType::Opaqueredirect) {
autoCancel.SetCancelMessage(
NS_LITERAL_CSTRING("BadOpaqueRedirectInterceptionWithURL"), &mRequestURL);
NS_LITERAL_CSTRING("BadOpaqueRedirectInterceptionWithURL"), mRequestURL);
return;
}
if (NS_WARN_IF(response->BodyUsed())) {
autoCancel.SetCancelMessage(
NS_LITERAL_CSTRING("InterceptedUsedResponseWithURL"), &mRequestURL);
NS_LITERAL_CSTRING("InterceptedUsedResponseWithURL"), mRequestURL);
return;
}
@ -650,7 +656,7 @@ RespondWithHandler::RejectedCallback(JSContext* aCx, JS::Handle<JS::Value> aValu
::AsyncLog(mInterceptedChannel, sourceSpec, line, column,
NS_LITERAL_CSTRING("InterceptionRejectedResponseWithURL"),
&mRequestURL, &valueString);
mRequestURL, valueString);
CancelRequest(NS_ERROR_INTERCEPTION_FAILED);
}
@ -738,7 +744,7 @@ FetchEvent::ReportCanceled()
::AsyncLog(mChannel.get(), mPreventDefaultScriptSpec,
mPreventDefaultLineNumber, mPreventDefaultColumnNumber,
NS_LITERAL_CSTRING("InterceptionCanceledWithURL"), &requestURL);
NS_LITERAL_CSTRING("InterceptionCanceledWithURL"), requestURL);
}
namespace {

View File

@ -1154,7 +1154,7 @@ public:
}
// Throw the type error with a generic error message.
aRv.ThrowTypeError<MSG_SW_INSTALL_ERROR>(&scriptSpec, &scope);
aRv.ThrowTypeError<MSG_SW_INSTALL_ERROR>(scriptSpec, scope);
}
for (uint32_t i = 1; i < mCallbacks.Length(); ++i) {
@ -2606,7 +2606,7 @@ ServiceWorkerManager::HandleError(JSContext* aCx,
ErrorResult rv;
NS_ConvertUTF8toUTF16 scope(aScope);
rv.ThrowTypeError<MSG_SW_SCRIPT_THREW>(&aWorkerURL, &scope);
rv.ThrowTypeError<MSG_SW_SCRIPT_THREW>(aWorkerURL, scope);
regJob->Fail(rv);
}
}

View File

@ -701,7 +701,7 @@ ServiceWorkerRegistrationMainThread::ShowNotification(JSContext* aCx,
RefPtr<workers::ServiceWorker> worker = GetActive();
if (!worker) {
aRv.ThrowTypeError<MSG_NO_ACTIVE_WORKER>(&mScope);
aRv.ThrowTypeError<MSG_NO_ACTIVE_WORKER>(mScope);
return nullptr;
}