Bug 1768758 - Add Windows-specific function getW() for getting wchar_t r=xpcom-reviewers,win-reviewers,gstoll,nika

`char16_t` and `wchar_t` are functionally the same on Windows. But logging statements do not handle `char16_t` very well, resulting in the need to static cast to `wchar_t`. For more info, see [[ https://bugzilla.mozilla.org/show_bug.cgi?id=1768758 | bug task ]]:

This diff introduces a Windows-specific function for getting strings as `wchar_t` and replaces previous workaround usages.

Differential Revision: https://phabricator.services.mozilla.com/D215869
This commit is contained in:
whale 2024-07-10 22:10:56 +00:00
Родитель 40750d5e8d
Коммит a860887be7
9 изменённых файлов: 29 добавлений и 26 удалений

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

@ -453,7 +453,7 @@ static void AddCachedDirRule(sandbox::TargetPolicy* aPolicy,
if (sandbox::SBOX_ALL_OK != result) {
NS_ERROR("Failed to add file policy rule.");
LOG_E("Failed (ResultCode %d) to add %d access to: %S", result, aAccess,
static_cast<const wchar_t*>(rulePath.get()));
rulePath.getW());
}
}

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

@ -246,7 +246,7 @@ nsresult ContentAnalysis::CreateContentAnalysisClient(
if (orgName) {
auto dependentOrgName = nsDependentString(orgName.get());
LOGD("Content analysis client signed with organization name \"%S\"",
static_cast<const wchar_t*>(dependentOrgName.get()));
dependentOrgName.getW());
signatureMatches = aClientSignatureSetting.Equals(dependentOrgName);
} else {
LOGD("Content analysis client has no signature");

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

@ -18,7 +18,7 @@ static void BuildClassName(const char* aProgram, const char* aProfile,
# if defined XP_WIN
nsString pfn = mozilla::widget::WinUtils::GetPackageFamilyName();
if (!pfn.IsEmpty()) {
aClassName.AppendPrintf("_%S", static_cast<const wchar_t*>(pfn.get()));
aClassName.AppendPrintf("_%S", pfn.getW());
}
# endif
aClassName.AppendPrintf("_%s_RemoteWindow", aProfile);

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

@ -42,14 +42,12 @@ class ModuleLoadCounter final {
for (size_t i = 0; i < N; ++i) {
auto entry = mCounters.Lookup(aNames[i]);
if (!entry) {
wprintf(L"%s is not registered.\n",
static_cast<const wchar_t*>(aNames[i].get()));
wprintf(L"%s is not registered.\n", aNames[i].getW());
result = false;
} else if (*entry != aCounts[i]) {
// We can return false, but let's print out all unmet modules
// which may be helpful to investigate test failures.
wprintf(L"%s:%4d\n", static_cast<const wchar_t*>(aNames[i].get()),
*entry);
wprintf(L"%s:%4d\n", aNames[i].getW(), *entry);
result = false;
}
}
@ -253,7 +251,7 @@ class UntrustedModulesFixture : public TelemetryTestFixture {
EXPECT_TRUE(matchResult.isBoolean() && matchResult.toBoolean());
if (!matchResult.isBoolean() || !matchResult.toBoolean()) {
// If match failed, print out the actual JSON kindly.
wprintf(L"JSON: %s\n", static_cast<const wchar_t*>(json.get()));
wprintf(L"JSON: %s\n", json.getW());
wprintf(L"RE: %s\n", aPattern);
}
wprintf(L"ValidateJSValue bottom\n");

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

@ -454,9 +454,8 @@ nsOSHelperAppService::GetMIMEInfoFromOS(const nsACString& aMIMEType,
bool haveMeaningfulMimeType =
!aMIMEType.IsEmpty() &&
!aMIMEType.LowerCaseEqualsLiteral(APPLICATION_OCTET_STREAM);
LOG("Extension lookup on '%S' with mimetype '%s'%s\n",
static_cast<const wchar_t*>(fileExtension.get()), flatType.get(),
haveMeaningfulMimeType ? " (treated as meaningful)" : "");
LOG("Extension lookup on '%S' with mimetype '%s'%s\n", fileExtension.getW(),
flatType.get(), haveMeaningfulMimeType ? " (treated as meaningful)" : "");
RefPtr<nsMIMEInfoWin> mi;
@ -488,13 +487,12 @@ nsOSHelperAppService::GetMIMEInfoFromOS(const nsACString& aMIMEType,
usedMimeTypeExtensionForLookup = true;
fileExtension = extensionFromMimeType;
LOG("Now using '%s' mimetype's default file extension '%S' for lookup\n",
flatType.get(), static_cast<const wchar_t*>(fileExtension.get()));
flatType.get(), fileExtension.getW());
}
// If we have an extension, use it for lookup:
mi = GetByExtension(fileExtension, flatType.get());
LOG("Extension lookup on '%S' found: 0x%p\n",
static_cast<const wchar_t*>(fileExtension.get()), mi.get());
LOG("Extension lookup on '%S' found: 0x%p\n", fileExtension.getW(), mi.get());
if (mi) {
bool hasDefault = false;
@ -505,8 +503,7 @@ nsOSHelperAppService::GetMIMEInfoFromOS(const nsACString& aMIMEType,
RefPtr<nsMIMEInfoWin> miFromMimeType =
GetByExtension(extensionFromMimeType, flatType.get());
LOG("Mime-based ext. lookup for '%S' found 0x%p\n",
static_cast<const wchar_t*>(extensionFromMimeType.get()),
miFromMimeType.get());
extensionFromMimeType.getW(), miFromMimeType.get());
if (miFromMimeType) {
nsAutoString desc;
miFromMimeType->GetDefaultDescription(desc);
@ -522,7 +519,7 @@ nsOSHelperAppService::GetMIMEInfoFromOS(const nsACString& aMIMEType,
if (!extensionFromMimeType.IsEmpty() && !usedMimeTypeExtensionForLookup) {
mi = GetByExtension(extensionFromMimeType, flatType.get());
LOG("Mime-based ext. lookup for '%S' found 0x%p\n",
static_cast<const wchar_t*>(extensionFromMimeType.get()), mi.get());
extensionFromMimeType.getW(), mi.get());
}
if (mi) {
mi.forget(aMIMEInfo);

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

@ -107,8 +107,7 @@ GfxInfo::GetCleartypeParameters(nsAString& aCleartypeParams) {
ClearTypeParameterInfo& params = clearTypeParams[d];
if (displayNames) {
outStr.AppendPrintf(
"%S [ ", static_cast<const wchar_t*>(params.displayName.get()));
outStr.AppendPrintf("%S [ ", params.displayName.getW());
}
if (params.gamma >= 0) {

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

@ -82,8 +82,7 @@ void OSKTabTipManager::ShowOnScreenKeyboard() {
if (FAILED(hres) || !path) {
return;
}
commonProgramFilesPath =
static_cast<const wchar_t*>(nsDependentString(path).get());
commonProgramFilesPath = nsDependentString(path).getW();
::CoTaskMemFree(path);
}
wstrpath.replace(commonProgramFilesOffset,

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

@ -73,8 +73,7 @@ static nsReturnRef<nsHGLOBAL> CreateGlobalDevModeAndInit(
const nsString& aPrintName, nsIPrintSettings* aPS) {
nsHPRINTER hPrinter = nullptr;
// const cast kludge for silly Win32 api's
LPWSTR printName =
const_cast<wchar_t*>(static_cast<const wchar_t*>(aPrintName.get()));
LPWSTR printName = const_cast<wchar_t*>(aPrintName.getW());
BOOL status = ::OpenPrinterW(printName, &hPrinter, nullptr);
if (!status) {
return nsReturnRef<nsHGLOBAL>();
@ -165,9 +164,8 @@ nsresult NativeShowPrintDialog(HWND aHWnd, bool aHaveSelection,
GetDefaultPrinterNameFromGlobalPrinters(printerName);
} else {
HANDLE hPrinter = nullptr;
if (!::OpenPrinterW(const_cast<wchar_t*>(
static_cast<const wchar_t*>(printerName.get())),
&hPrinter, nullptr)) {
if (!::OpenPrinterW(const_cast<wchar_t*>(printerName.getW()), &hPrinter,
nullptr)) {
// If the last used printer is not found, we should use default printer.
GetDefaultPrinterNameFromGlobalPrinters(printerName);
} else {

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

@ -166,6 +166,18 @@ class nsTString : public nsTSubstring<T> {
return this->mData;
}
#ifdef XP_WIN
/**
* Returns the string as a wchar_t
*/
template <typename U = T>
typename std::enable_if<std::is_same<U, char16_t>::value,
const wchar_t*>::type
getW() const {
return reinterpret_cast<const wchar_t*>(this->mData);
}
#endif
/**
* returns character at specified index.
*