From a860887be7029c266bf29eee1394bbebb577ad84 Mon Sep 17 00:00:00 2001 From: whale Date: Wed, 10 Jul 2024 22:10:56 +0000 Subject: [PATCH] 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 --- .../win/src/sandboxbroker/sandboxBroker.cpp | 2 +- .../contentanalysis/ContentAnalysis.cpp | 2 +- toolkit/components/remote/RemoteUtils.h | 2 +- .../tests/gtest/TestUntrustedModules.cpp | 8 +++----- uriloader/exthandler/win/nsOSHelperAppService.cpp | 15 ++++++--------- widget/windows/GfxInfo.cpp | 3 +-- widget/windows/OSKTabTipManager.cpp | 3 +-- widget/windows/nsPrintDialogUtil.cpp | 8 +++----- xpcom/string/nsTString.h | 12 ++++++++++++ 9 files changed, 29 insertions(+), 26 deletions(-) diff --git a/security/sandbox/win/src/sandboxbroker/sandboxBroker.cpp b/security/sandbox/win/src/sandboxbroker/sandboxBroker.cpp index c1d4e4f50eb2..15938a756d93 100644 --- a/security/sandbox/win/src/sandboxbroker/sandboxBroker.cpp +++ b/security/sandbox/win/src/sandboxbroker/sandboxBroker.cpp @@ -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(rulePath.get())); + rulePath.getW()); } } diff --git a/toolkit/components/contentanalysis/ContentAnalysis.cpp b/toolkit/components/contentanalysis/ContentAnalysis.cpp index 3c4a459166ee..dfbcaefc824a 100644 --- a/toolkit/components/contentanalysis/ContentAnalysis.cpp +++ b/toolkit/components/contentanalysis/ContentAnalysis.cpp @@ -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(dependentOrgName.get())); + dependentOrgName.getW()); signatureMatches = aClientSignatureSetting.Equals(dependentOrgName); } else { LOGD("Content analysis client has no signature"); diff --git a/toolkit/components/remote/RemoteUtils.h b/toolkit/components/remote/RemoteUtils.h index 2d21293eb581..da0ad77eb44d 100644 --- a/toolkit/components/remote/RemoteUtils.h +++ b/toolkit/components/remote/RemoteUtils.h @@ -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(pfn.get())); + aClassName.AppendPrintf("_%S", pfn.getW()); } # endif aClassName.AppendPrintf("_%s_RemoteWindow", aProfile); diff --git a/toolkit/xre/dllservices/tests/gtest/TestUntrustedModules.cpp b/toolkit/xre/dllservices/tests/gtest/TestUntrustedModules.cpp index afbd96caf60c..488f314d076d 100644 --- a/toolkit/xre/dllservices/tests/gtest/TestUntrustedModules.cpp +++ b/toolkit/xre/dllservices/tests/gtest/TestUntrustedModules.cpp @@ -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(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(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(json.get())); + wprintf(L"JSON: %s\n", json.getW()); wprintf(L"RE: %s\n", aPattern); } wprintf(L"ValidateJSValue bottom\n"); diff --git a/uriloader/exthandler/win/nsOSHelperAppService.cpp b/uriloader/exthandler/win/nsOSHelperAppService.cpp index e3a2ae9873d9..31c2a6f09c63 100644 --- a/uriloader/exthandler/win/nsOSHelperAppService.cpp +++ b/uriloader/exthandler/win/nsOSHelperAppService.cpp @@ -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(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 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(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(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 miFromMimeType = GetByExtension(extensionFromMimeType, flatType.get()); LOG("Mime-based ext. lookup for '%S' found 0x%p\n", - static_cast(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(extensionFromMimeType.get()), mi.get()); + extensionFromMimeType.getW(), mi.get()); } if (mi) { mi.forget(aMIMEInfo); diff --git a/widget/windows/GfxInfo.cpp b/widget/windows/GfxInfo.cpp index 533202f964b2..e944d537741b 100644 --- a/widget/windows/GfxInfo.cpp +++ b/widget/windows/GfxInfo.cpp @@ -107,8 +107,7 @@ GfxInfo::GetCleartypeParameters(nsAString& aCleartypeParams) { ClearTypeParameterInfo& params = clearTypeParams[d]; if (displayNames) { - outStr.AppendPrintf( - "%S [ ", static_cast(params.displayName.get())); + outStr.AppendPrintf("%S [ ", params.displayName.getW()); } if (params.gamma >= 0) { diff --git a/widget/windows/OSKTabTipManager.cpp b/widget/windows/OSKTabTipManager.cpp index b7b06c08d15f..a5e8243a82e6 100644 --- a/widget/windows/OSKTabTipManager.cpp +++ b/widget/windows/OSKTabTipManager.cpp @@ -82,8 +82,7 @@ void OSKTabTipManager::ShowOnScreenKeyboard() { if (FAILED(hres) || !path) { return; } - commonProgramFilesPath = - static_cast(nsDependentString(path).get()); + commonProgramFilesPath = nsDependentString(path).getW(); ::CoTaskMemFree(path); } wstrpath.replace(commonProgramFilesOffset, diff --git a/widget/windows/nsPrintDialogUtil.cpp b/widget/windows/nsPrintDialogUtil.cpp index 43f56e9706cd..9d2033d61e39 100644 --- a/widget/windows/nsPrintDialogUtil.cpp +++ b/widget/windows/nsPrintDialogUtil.cpp @@ -73,8 +73,7 @@ static nsReturnRef CreateGlobalDevModeAndInit( const nsString& aPrintName, nsIPrintSettings* aPS) { nsHPRINTER hPrinter = nullptr; // const cast kludge for silly Win32 api's - LPWSTR printName = - const_cast(static_cast(aPrintName.get())); + LPWSTR printName = const_cast(aPrintName.getW()); BOOL status = ::OpenPrinterW(printName, &hPrinter, nullptr); if (!status) { return nsReturnRef(); @@ -165,9 +164,8 @@ nsresult NativeShowPrintDialog(HWND aHWnd, bool aHaveSelection, GetDefaultPrinterNameFromGlobalPrinters(printerName); } else { HANDLE hPrinter = nullptr; - if (!::OpenPrinterW(const_cast( - static_cast(printerName.get())), - &hPrinter, nullptr)) { + if (!::OpenPrinterW(const_cast(printerName.getW()), &hPrinter, + nullptr)) { // If the last used printer is not found, we should use default printer. GetDefaultPrinterNameFromGlobalPrinters(printerName); } else { diff --git a/xpcom/string/nsTString.h b/xpcom/string/nsTString.h index 9793f70e3bf8..88e683159975 100644 --- a/xpcom/string/nsTString.h +++ b/xpcom/string/nsTString.h @@ -166,6 +166,18 @@ class nsTString : public nsTSubstring { return this->mData; } +#ifdef XP_WIN + /** + * Returns the string as a wchar_t + */ + template + typename std::enable_if::value, + const wchar_t*>::type + getW() const { + return reinterpret_cast(this->mData); + } +#endif + /** * returns character at specified index. *