Bug 1726648 - Add a helper to trim brackets from nsID r=mccr8,smaug

Differential Revision: https://phabricator.services.mozilla.com/D123163
This commit is contained in:
Kagami Sascha Rosylight 2021-08-23 10:54:11 +00:00
Родитель e9ff4d633e
Коммит eddf271c1f
16 изменённых файлов: 95 добавлений и 72 удалений

Просмотреть файл

@ -16,6 +16,7 @@
#include "mozilla/dom/Promise.h"
#include "mozilla/dom/WorkerPrivate.h"
#include "mozilla/dom/WorkerScope.h"
#include "nsIDUtils.h"
#include "nsIGlobalObject.h"
namespace mozilla::dom {
@ -74,12 +75,7 @@ void Client::GetUrl(nsAString& aUrlOut) const {
}
void Client::GetId(nsAString& aIdOut) const {
char buf[NSID_LENGTH];
mData->info().id().ToProvidedString(buf);
NS_ConvertASCIItoUTF16 uuid(buf);
// Remove {} and the null terminator
aIdOut.Assign(Substring(uuid, 1, NSID_LENGTH - 3));
aIdOut = NSID_TrimBracketsUTF16(mData->info().id());
}
ClientType Client::Type() const { return mData->info().type(); }

Просмотреть файл

@ -28,6 +28,7 @@
#include "nsContentUtils.h"
#include "nsError.h"
#include "nsIAsyncShutdown.h"
#include "nsIDUtils.h"
#include "nsIException.h" // for nsIStackFrame
#include "nsIMemoryReporter.h"
#include "nsIPrincipal.h"
@ -712,9 +713,6 @@ nsresult BlobURLProtocolHandler::GenerateURIString(nsIPrincipal* aPrincipal,
rv = uuidgen->GenerateUUIDInPlace(&id);
NS_ENSURE_SUCCESS(rv, rv);
char chars[NSID_LENGTH];
id.ToProvidedString(chars);
aUri.AssignLiteral(BLOBURI_SCHEME);
aUri.Append(':');
@ -729,7 +727,7 @@ nsresult BlobURLProtocolHandler::GenerateURIString(nsIPrincipal* aPrincipal,
aUri.Append('/');
}
aUri += Substring(chars + 1, chars + NSID_LENGTH - 2);
aUri += NSID_TrimBracketsASCII(id);
return NS_OK;
}

Просмотреть файл

@ -5,6 +5,7 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "FontTableURIProtocolHandler.h"
#include "nsIDUtils.h"
#include "nsIUUIDGenerator.h"
#include "nsNetUtil.h"
#include "nsSimpleURI.h"
@ -23,13 +24,10 @@ nsresult FontTableURIProtocolHandler::GenerateURIString(nsACString& aUri) {
rv = uuidgen->GenerateUUIDInPlace(&id);
NS_ENSURE_SUCCESS(rv, rv);
char chars[NSID_LENGTH];
id.ToProvidedString(chars);
aUri = FONTTABLEURI_SCHEME;
aUri.Append(':');
aUri += Substring(chars + 1, chars + NSID_LENGTH - 2);
aUri += NSID_TrimBracketsASCII(id);
return NS_OK;
}

Просмотреть файл

@ -8,6 +8,7 @@
#include "FileSystemRootDirectoryEntry.h"
#include "mozilla/dom/FileSystemBinding.h"
#include "nsContentUtils.h"
#include "nsIDUtils.h"
namespace mozilla::dom {
@ -33,15 +34,9 @@ already_AddRefed<FileSystem> FileSystem::Create(nsIGlobalObject* aGlobalObject)
return nullptr;
}
char chars[NSID_LENGTH];
id.ToProvidedString(chars);
NSID_TrimBracketsUTF16 name(id);
// Any fileSystem has an unique ID. We use UUID, but our generator produces
// UUID in this format '{' + UUID + '}'. We remove them with these +1 and -2.
nsAutoCString name(Substring(chars + 1, chars + NSID_LENGTH - 2));
RefPtr<FileSystem> fs =
new FileSystem(aGlobalObject, NS_ConvertUTF8toUTF16(name));
RefPtr<FileSystem> fs = new FileSystem(aGlobalObject, name);
return fs.forget();
}

Просмотреть файл

@ -14,6 +14,7 @@
#include "mozilla/dom/Promise-inl.h"
#include "mozilla/dom/WorkerPrivate.h"
#include "nsIRunnable.h"
#include "nsIDUtils.h"
namespace mozilla::dom {
@ -66,16 +67,8 @@ static bool HasStorageAccess(nsIGlobalObject* aGlobal) {
return access > StorageAccess::eDeny;
}
// XXX: This is copied from ServiceWorkerManager and should go to nsID.
static nsString GetClientId(nsIGlobalObject* aGlobal) {
nsString id;
char buf[NSID_LENGTH];
aGlobal->GetClientInfo()->Id().ToProvidedString(buf);
NS_ConvertASCIItoUTF16 uuid(buf);
// Remove {} and the null terminator
id.Assign(Substring(uuid, 1, NSID_LENGTH - 3));
return id;
return NSID_TrimBracketsUTF16(aGlobal->GetClientInfo()->Id());
}
static bool ValidateRequestArguments(const nsAString& name,

Просмотреть файл

@ -15,6 +15,7 @@
#include "mozilla/Assertions.h"
#include "mozilla/ErrorNames.h"
#include "nsContentUtils.h"
#include "nsIDUtils.h"
#include "transport/runnable_utils.h"
#include "Tracing.h"
@ -1307,8 +1308,6 @@ nsString MediaEngineWebRTCAudioCaptureSource::GetName() const {
nsCString MediaEngineWebRTCAudioCaptureSource::GetUUID() const {
nsID uuid;
char uuidBuffer[NSID_LENGTH];
nsCString asciiString;
ErrorResult rv;
rv = nsContentUtils::GenerateUUIDInPlace(uuid);
@ -1316,11 +1315,7 @@ nsCString MediaEngineWebRTCAudioCaptureSource::GetUUID() const {
return ""_ns;
}
uuid.ToProvidedString(uuidBuffer);
asciiString.AssignASCII(uuidBuffer);
// Remove {} and the null terminator
return nsCString(Substring(asciiString, 1, NSID_LENGTH - 3));
return NSID_TrimBracketsASCII(uuid);
}
nsString MediaEngineWebRTCAudioCaptureSource::GetGroupId() const {

Просмотреть файл

@ -19,6 +19,7 @@
#include "mozilla/intl/MozLocale.h"
#include "mozilla/StaticPrefs_dom.h"
#include "nsContentUtils.h"
#include "nsIDUtils.h"
#include "nsImportModule.h"
#include "nsIRegion.h"
#include "nsIScriptError.h"
@ -650,13 +651,7 @@ already_AddRefed<PaymentRequest> PaymentRequest::CreatePaymentRequest(
return nullptr;
}
// Build a string in {xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx} format
char buffer[NSID_LENGTH];
uuid.ToProvidedString(buffer);
// Remove {} and the null terminator
nsAutoString id;
id.AssignASCII(&buffer[1], NSID_LENGTH - 3);
NSID_TrimBracketsUTF16 id(uuid);
// Create payment request with generated id
RefPtr<PaymentRequest> request = new PaymentRequest(aWindow, id);

Просмотреть файл

@ -65,6 +65,7 @@
#include "nsComponentManagerUtils.h"
#include "nsContentUtils.h"
#include "nsIDUtils.h"
#include "nsNetUtil.h"
#include "nsProxyRelease.h"
#include "nsQueryObject.h"
@ -1946,14 +1947,9 @@ class ContinueDispatchFetchEventRunnable : public Runnable {
nsString clientId;
nsString resultingClientId;
nsCOMPtr<nsILoadInfo> loadInfo = channel->LoadInfo();
char buf[NSID_LENGTH];
Maybe<ClientInfo> clientInfo = loadInfo->GetClientInfo();
if (clientInfo.isSome()) {
clientInfo.ref().Id().ToProvidedString(buf);
NS_ConvertASCIItoUTF16 uuid(buf);
// Remove {} and the null terminator
clientId.Assign(Substring(uuid, 1, NSID_LENGTH - 3));
clientId = NSID_TrimBracketsUTF16(clientInfo->Id());
}
// Having an initial or reserved client are mutually exclusive events:
@ -1971,10 +1967,7 @@ class ContinueDispatchFetchEventRunnable : public Runnable {
}
if (resulting.isSome()) {
resulting.ref().Id().ToProvidedString(buf);
NS_ConvertASCIItoUTF16 uuid(buf);
resultingClientId.Assign(Substring(uuid, 1, NSID_LENGTH - 3));
resultingClientId = NSID_TrimBracketsUTF16(resulting->Id());
}
rv = mServiceWorkerPrivate->SendFetchEvent(mChannel, mLoadGroup, clientId,

Просмотреть файл

@ -62,6 +62,7 @@
#include "mozilla/WorkerTimelineMarker.h"
#include "nsCycleCollector.h"
#include "nsGlobalWindowInner.h"
#include "nsIDUtils.h"
#include "nsNetUtil.h"
#include "nsIFile.h"
#include "nsIMemoryReporter.h"
@ -916,14 +917,8 @@ nsString ComputeWorkerPrivateId() {
nsID uuid;
rv = uuidGenerator->GenerateUUIDInPlace(&uuid);
MOZ_ASSERT(NS_SUCCEEDED(rv));
char buffer[NSID_LENGTH];
uuid.ToProvidedString(buffer);
nsString id;
// Remove {} and the null terminator
id.AssignASCII(&buffer[1], NSID_LENGTH - 3);
return id;
return NSID_TrimBracketsUTF16(uuid);
}
class WorkerPrivate::EventTarget final : public nsISerialEventTarget {

Просмотреть файл

@ -148,6 +148,7 @@
#include "nsDirectoryServiceDefs.h"
#include "nsDirectoryServiceUtils.h"
#include "nsEmbedCID.h"
#include "nsIDUtils.h"
#include "nsNetUtil.h"
#include "nsReadableUtils.h"
#include "nsXPCOM.h"
@ -2798,9 +2799,6 @@ static void SubmitDowngradeTelemetry(const nsCString& aLastVersion,
rv = uuidGen->GenerateUUIDInPlace(&uuid);
NS_ENSURE_SUCCESS_VOID(rv);
char strid[NSID_LENGTH];
uuid.ToProvidedString(strid);
nsCString arch("null");
nsCOMPtr<nsIPropertyBag2> sysInfo =
do_GetService("@mozilla.org/system-info;1");
@ -2812,9 +2810,7 @@ static void SubmitDowngradeTelemetry(const nsCString& aLastVersion,
char date[sizeof "YYYY-MM-DDThh:mm:ss.000Z"];
strftime(date, sizeof date, "%FT%T.000Z", gmtime(&now));
// NSID_LENGTH includes the trailing \0 and we also want to strip off the
// surrounding braces so the length becomes NSID_LENGTH - 3.
nsDependentCSubstring pingId(strid + 1, NSID_LENGTH - 3);
NSID_TrimBracketsASCII pingId(uuid);
constexpr auto pingType = "downgrade"_ns;
int32_t pos = aLastVersion.Find("_");

Просмотреть файл

@ -14,6 +14,7 @@
#include "mozilla/WindowsVersion.h"
#include "mozilla/gfx/2D.h"
#include "nsDirectoryServiceDefs.h"
#include "nsIDUtils.h"
#include "nsIStringBundle.h"
#include "nsIURI.h"
#include "nsIUUIDGenerator.h"
@ -545,10 +546,7 @@ nsresult ToastNotificationHandler::AsyncSaveImage(imgIRequest* aRequest) {
return rv;
}
char uuidChars[NSID_LENGTH];
uuid.ToProvidedString(uuidChars);
// Remove the brackets at the beginning and ending of the generated UUID.
nsAutoCString uuidStr(Substring(uuidChars + 1, uuidChars + NSID_LENGTH - 2));
NSID_TrimBracketsASCII uuidStr(uuid);
uuidStr.AppendLiteral(".bmp");
mImageFile->AppendNative(uuidStr);

Просмотреть файл

@ -76,6 +76,7 @@ EXPORTS += [
"nsGZFileWriter.h",
"nsIClassInfoImpl.h",
"nsID.h",
"nsIDUtils.h",
"nsIInterfaceRequestorUtils.h",
"nsINIParser.h",
"nsInterfaceRequestorAgg.h",

32
xpcom/base/nsIDUtils.h Normal file
Просмотреть файл

@ -0,0 +1,32 @@
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* 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
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#ifndef XPCOM_BASE_NSIDUTILS_H_
#define XPCOM_BASE_NSIDUTILS_H_
#include "nsID.h"
#include "nsString.h"
#include "nsTString.h"
/**
* Trims the brackets around the nsID string, since it wraps its ID with
* brackets: {xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}.
*/
template <typename T>
class NSID_TrimBrackets : public nsTAutoString<T> {
public:
explicit NSID_TrimBrackets(const nsID& aID) {
nsIDToCString toCString(aID);
// The string from nsIDToCString::get() includes a null terminator.
// Thus this trims 3 characters: `{`, `}`, and the null terminator.
this->AssignASCII(toCString.get() + 1, NSID_LENGTH - 3);
}
};
using NSID_TrimBracketsUTF16 = NSID_TrimBrackets<char16_t>;
using NSID_TrimBracketsASCII = NSID_TrimBrackets<char>;
#endif // XPCOM_BASE_NSIDUTILS_H_

Просмотреть файл

@ -0,0 +1,36 @@
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* 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
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "nsID.h"
#include "nsIDUtils.h"
#include "gtest/gtest.h"
static const char* const bare_ids[] = {
"5c347b10-d55c-11d1-89b7-006008911b81",
"fc347b10-d55c-f1d1-f9b7-006008911b81",
};
TEST(nsIDUtils, NSID_TrimBracketsUTF16)
{
nsID id{};
for (const auto* idstr : bare_ids) {
ASSERT_TRUE(id.Parse(idstr));
NSID_TrimBracketsUTF16 trimmed(id);
ASSERT_TRUE(trimmed.EqualsASCII(idstr));
}
}
TEST(nsIDUtils, NSID_TrimBracketsASCII)
{
nsID id{};
for (const auto* idstr : bare_ids) {
ASSERT_TRUE(id.Parse(idstr));
NSID_TrimBracketsASCII trimmed(id);
ASSERT_TRUE(trimmed.EqualsASCII(idstr));
}
}

Просмотреть файл

@ -5,6 +5,7 @@
#include <type_traits>
#include "nsComponentManagerUtils.h"
#include "nsIThread.h"
#include "nsThreadUtils.h"
#include "mozilla/IdleTaskRunner.h"
#include "mozilla/RefCounted.h"

Просмотреть файл

@ -24,6 +24,7 @@ UNIFIED_SOURCES += [
"TestFile.cpp",
"TestGCPostBarriers.cpp",
"TestID.cpp",
"TestIDUtils.cpp",
"TestInputStreamLengthHelper.cpp",
"TestLogCommandLineHandler.cpp",
"TestLogging.cpp",