From 62bd52a17f424c4299cd8b66e5f5b2e260f579d4 Mon Sep 17 00:00:00 2001 From: Andrew McCreight Date: Tue, 20 Aug 2019 22:53:49 +0000 Subject: [PATCH] Bug 1575343, part 1 - Avoid a gratuitous string copy by calling the nsAString overload of GetCallingLocation. r=smaug The nsAString overload of GetCallingLocation directly converts the original source file name string into an nsAString. A number of callers that want the source file name in an nsAString are calling the nsACString overload of GetCallingLocation, then calling NS_ConvertUTF8toUTF16. This results in an extra intermediate copy of the original string data. Differential Revision: https://phabricator.services.mozilla.com/D42727 --HG-- extra : moz-landing-system : lando --- dom/base/nsContentUtils.cpp | 11 +++++------ dom/bindings/BindingUtils.cpp | 5 ++--- dom/media/webaudio/WebAudioUtils.cpp | 8 ++++---- dom/security/featurepolicy/FeaturePolicyUtils.cpp | 8 ++++---- dom/security/nsCSPContext.cpp | 10 +++++----- 5 files changed, 20 insertions(+), 22 deletions(-) diff --git a/dom/base/nsContentUtils.cpp b/dom/base/nsContentUtils.cpp index 2f4bdced37106..d2967dfe84dac 100644 --- a/dom/base/nsContentUtils.cpp +++ b/dom/base/nsContentUtils.cpp @@ -3724,7 +3724,7 @@ nsresult nsContentUtils::ReportToConsoleByWindowID( NS_ENSURE_SUCCESS(rv, rv); } - nsAutoCString spec; + nsAutoString spec; if (!aLineNumber && aLocationMode == eUSE_CALLING_LOCATION) { JSContext* cx = GetCurrentJSContext(); if (cx) { @@ -3737,11 +3737,10 @@ nsresult nsContentUtils::ReportToConsoleByWindowID( NS_ENSURE_SUCCESS(rv, rv); if (!spec.IsEmpty()) { - rv = - errorObject->InitWithWindowID(aErrorText, - NS_ConvertUTF8toUTF16(spec), // file name - aSourceLine, aLineNumber, aColumnNumber, - aErrorFlags, aCategory, aInnerWindowID); + rv = errorObject->InitWithWindowID(aErrorText, + spec, // file name + aSourceLine, aLineNumber, aColumnNumber, + aErrorFlags, aCategory, aInnerWindowID); } else { rv = errorObject->InitWithSourceURI(aErrorText, aURI, aSourceLine, aLineNumber, aColumnNumber, aErrorFlags, diff --git a/dom/bindings/BindingUtils.cpp b/dom/bindings/BindingUtils.cpp index 1974b1785daaa..b05f2daf92e25 100644 --- a/dom/bindings/BindingUtils.cpp +++ b/dom/bindings/BindingUtils.cpp @@ -4003,7 +4003,7 @@ void DeprecationWarning(const GlobalObject& aGlobal, if (window && window->GetExtantDoc()) { window->GetExtantDoc()->WarnOnceAbout(aOperation); - nsAutoCString fileName; + nsAutoString fileName; Nullable lineNumber; Nullable columnNumber; uint32_t line = 0; @@ -4014,8 +4014,7 @@ void DeprecationWarning(const GlobalObject& aGlobal, columnNumber.SetValue(column); } - MaybeReportDeprecation(window, aOperation, - NS_ConvertUTF8toUTF16(fileName), lineNumber, + MaybeReportDeprecation(window, aOperation, fileName, lineNumber, columnNumber); } diff --git a/dom/media/webaudio/WebAudioUtils.cpp b/dom/media/webaudio/WebAudioUtils.cpp index 3107d0d64bf6c..4ededeb145285 100644 --- a/dom/media/webaudio/WebAudioUtils.cpp +++ b/dom/media/webaudio/WebAudioUtils.cpp @@ -110,7 +110,7 @@ void WebAudioUtils::LogToDeveloperConsole(uint64_t aWindowID, return; } - nsAutoCString spec; + nsAutoString spec; uint32_t aLineNumber, aColumnNumber; JSContext* cx = nsContentUtils::GetCurrentJSContext(); if (cx) { @@ -134,9 +134,9 @@ void WebAudioUtils::LogToDeveloperConsole(uint64_t aWindowID, return; } - errorObject->InitWithWindowID( - result, NS_ConvertUTF8toUTF16(spec), EmptyString(), aLineNumber, - aColumnNumber, nsIScriptError::warningFlag, "Web Audio", aWindowID); + errorObject->InitWithWindowID(result, spec, EmptyString(), aLineNumber, + aColumnNumber, nsIScriptError::warningFlag, + "Web Audio", aWindowID); console->LogMessage(errorObject); } diff --git a/dom/security/featurepolicy/FeaturePolicyUtils.cpp b/dom/security/featurepolicy/FeaturePolicyUtils.cpp index 20a0dfa147ec6..6b4e94f7a06e2 100644 --- a/dom/security/featurepolicy/FeaturePolicyUtils.cpp +++ b/dom/security/featurepolicy/FeaturePolicyUtils.cpp @@ -136,7 +136,7 @@ void FeaturePolicyUtils::ReportViolation(Document* aDocument, return; } - nsAutoCString fileName; + nsAutoString fileName; Nullable lineNumber; Nullable columnNumber; uint32_t line = 0; @@ -152,9 +152,9 @@ void FeaturePolicyUtils::ReportViolation(Document* aDocument, } RefPtr body = - new FeaturePolicyViolationReportBody( - window, aFeatureName, NS_ConvertUTF8toUTF16(fileName), lineNumber, - columnNumber, NS_LITERAL_STRING("enforce")); + new FeaturePolicyViolationReportBody(window, aFeatureName, fileName, + lineNumber, columnNumber, + NS_LITERAL_STRING("enforce")); ReportingUtils::Report(window, nsGkAtoms::featurePolicyViolation, NS_LITERAL_STRING("default"), diff --git a/dom/security/nsCSPContext.cpp b/dom/security/nsCSPContext.cpp index 33ff6887c20e5..0df8d69010be6 100644 --- a/dom/security/nsCSPContext.cpp +++ b/dom/security/nsCSPContext.cpp @@ -213,7 +213,7 @@ bool nsCSPContext::permitsInternal( if (!aIsPreload && aSendViolationReports) { uint32_t lineNumber = 0; uint32_t columnNumber = 0; - nsAutoCString spec; + nsAutoString spec; JSContext* cx = nsContentUtils::GetCurrentJSContext(); if (cx) { nsJSUtils::GetCallingLocation(cx, spec, &lineNumber, &columnNumber); @@ -229,10 +229,10 @@ bool nsCSPContext::permitsInternal( null */ violatedDirective, p, /* policy index */ EmptyString(), /* no observer subject */ - NS_ConvertUTF8toUTF16(spec), /* source file */ - EmptyString(), /* no script sample */ - lineNumber, /* line number */ - columnNumber); /* column number */ + spec, /* source file */ + EmptyString(), /* no script sample */ + lineNumber, /* line number */ + columnNumber); /* column number */ } } }