From 1b91588bd3d50d8ec8cbbffddecf8dfec032646f Mon Sep 17 00:00:00 2001 From: Francis Tsui Date: Fri, 26 Jan 2024 15:18:16 -0800 Subject: [PATCH] Use filesystem::path internal UTF8 conversion. PiperOrigin-RevId: 601880589 --- sharing/BUILD | 1 - .../internal/base/utf_string_conversions.h | 2 - sharing/nearby_connections_service.cc | 55 +++++-------------- sharing/nearby_sharing_service_extension.cc | 12 ++-- 4 files changed, 20 insertions(+), 50 deletions(-) diff --git a/sharing/BUILD b/sharing/BUILD index 2c79f0f8..d1e95310 100644 --- a/sharing/BUILD +++ b/sharing/BUILD @@ -150,7 +150,6 @@ cc_library( "//sharing/flags:nearby_sharing_feature_flags_cpp_consts_generated", "//sharing/internal/api:platform", "//sharing/internal/base", - "//sharing/internal/base:utf_utils", "//sharing/internal/public:logging", "//sharing/internal/public:nearby_context", "//sharing/internal/public:types", diff --git a/sharing/internal/base/utf_string_conversions.h b/sharing/internal/base/utf_string_conversions.h index 6c9cedd9..545953d8 100644 --- a/sharing/internal/base/utf_string_conversions.h +++ b/sharing/internal/base/utf_string_conversions.h @@ -18,8 +18,6 @@ #if defined(GITHUB_BUILD) // Stub out string conversion functions for github builds. namespace nearby::utils { -std::string WideToUtf8(std::wstring_view wide) { return std::string(); } -std::wstring Utf8ToWide(std::string_view utf8) { return std::wstring(); } bool IsStringUtf8(std::string_view str) { return true; } diff --git a/sharing/nearby_connections_service.cc b/sharing/nearby_connections_service.cc index f56f6ce2..db36c897 100644 --- a/sharing/nearby_connections_service.cc +++ b/sharing/nearby_connections_service.cc @@ -24,7 +24,9 @@ #include #include -#include "sharing/internal/base/utf_string_conversions.h" +#include "internal/platform/file.h" +#include "sharing/common/compatible_u8_string.h" +#include "sharing/internal/public/logging.h" #include "sharing/nearby_connections_types.h" namespace nearby { @@ -43,26 +45,12 @@ Payload ConvertToPayload(NcPayload payload) { std::vector(data.begin(), data.end())); } case NcPayloadType::kFile: { - std::filesystem::path file_path; - std::string parent_folder; - // Initialize path with UTF8 cause crash on Windows with configured - // locale. - try { - file_path = payload.AsFile()->GetFilePath(); - if (!std::filesystem::exists(file_path)) { - file_path = utils::Utf8ToWide(payload.AsFile()->GetFilePath()); - parent_folder = payload.GetParentFolder(); - } - } catch (std::exception exception) { - file_path = utils::Utf8ToWide(payload.AsFile()->GetFilePath()); - parent_folder = payload.GetParentFolder(); - } catch (...) { - file_path = utils::Utf8ToWide(payload.AsFile()->GetFilePath()); - parent_folder = payload.GetParentFolder(); - } + std::filesystem::path file_path = + std::filesystem::u8path(payload.AsFile()->GetFilePath()); + std::string parent_folder = payload.GetParentFolder(); + NL_VLOG(1) << __func__ << ": Payload file_path=" << file_path + << ", parent_folder = " << parent_folder; return Payload(payload.GetId(), InputFile(file_path), parent_folder); - NEARBY_LOGS(VERBOSE) << __func__ << ": Payload file_path=" << file_path - << ", parent_folder = " << parent_folder; } default: return Payload(); @@ -72,28 +60,15 @@ Payload ConvertToPayload(NcPayload payload) { NcPayload ConvertToServicePayload(Payload payload) { switch (payload.content.type) { case PayloadContent::Type::kFile: { - // On Windows, a crash may happen when access string() of path if it is - // using wchar. Apply UTF8 to avoid the cross-platform issues. - std::string file_path; - std::string file_name; - std::string parent_folder; int64_t file_size = payload.content.file_payload.size; - try { - file_path = - utils::WideToUtf8(payload.content.file_payload.file.path.wstring()); - file_name = utils::WideToUtf8( - payload.content.file_payload.file.path.filename().wstring()); - } catch (std::exception e) { - file_path = payload.content.file_payload.file.path.string(); - file_name = payload.content.file_payload.file.path.filename().string(); - } catch (...) { - file_path = payload.content.file_payload.file.path.string(); - file_name = payload.content.file_payload.file.path.filename().string(); - } - parent_folder = payload.content.file_payload.parent_folder; + std::string file_path = GetCompatibleU8String( + payload.content.file_payload.file.path.u8string()); + std::string file_name = GetCompatibleU8String( + payload.content.file_payload.file.path.filename().u8string()); + std::string parent_folder = payload.content.file_payload.parent_folder; std::replace(parent_folder.begin(), parent_folder.end(), '\\', '/'); - NEARBY_LOGS(VERBOSE) << __func__ << ": NC Payload file_path=" << file_path - << ", parent_folder = " << parent_folder; + NL_VLOG(1) << __func__ << ": NC Payload file_path=" << file_path + << ", parent_folder = " << parent_folder; nearby::InputFile input_file(file_path, file_size); NcPayload nc_payload(payload.id, parent_folder, file_name, std::move(input_file)); diff --git a/sharing/nearby_sharing_service_extension.cc b/sharing/nearby_sharing_service_extension.cc index 2a809131..09d3650f 100644 --- a/sharing/nearby_sharing_service_extension.cc +++ b/sharing/nearby_sharing_service_extension.cc @@ -22,7 +22,6 @@ #include "absl/synchronization/notification.h" #include "internal/network/url.h" #include "sharing/file_attachment.h" -#include "sharing/internal/base/utf_string_conversions.h" #include "sharing/internal/public/logging.h" #include "sharing/nearby_sharing_service.h" #include "sharing/proto/wire_format.pb.h" @@ -107,8 +106,7 @@ NearbySharingService::StatusCodes NearbySharingServiceExtension::Open( NearbySharingService::StatusCodes status_codes = StatusCodes::kOk; absl::Notification notification; context_->GetShell().Open( - std::filesystem::path( - utils::Utf8ToWide(settings_->GetCustomSavePath())), + std::filesystem::u8path(settings_->GetCustomSavePath()), [&status_codes, ¬ification](absl::Status status) { if (!status.ok()) { NL_LOG(ERROR) @@ -129,10 +127,10 @@ NearbySharingService::StatusCodes NearbySharingServiceExtension::Open( if (file_attachment.file_path().has_value()) { file_path = *file_attachment.file_path(); } else { - file_path = std::filesystem::path( - utils::Utf8ToWide(settings_->GetCustomSavePath())) / - // NOLINTNEXTLINE cannot build without the new string creation - utils::Utf8ToWide(std::string(file_attachment.file_name())); + file_path = + std::filesystem::u8path(settings_->GetCustomSavePath()) / + // NOLINTNEXTLINE cannot build without the new string creation + std::filesystem::u8path(std::string(file_attachment.file_name())); } NearbySharingService::StatusCodes status_codes = StatusCodes::kOk;