Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Migrate string encoding conversions to FML #31334

Merged
merged 2 commits into from
Feb 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions ci/licenses_golden/licenses_flutter
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,9 @@ FILE: ../../../flutter/fml/platform/win/message_loop_win.h
FILE: ../../../flutter/fml/platform/win/native_library_win.cc
FILE: ../../../flutter/fml/platform/win/paths_win.cc
FILE: ../../../flutter/fml/platform/win/posix_wrappers_win.cc
FILE: ../../../flutter/fml/platform/win/wstring_conversion.cc
FILE: ../../../flutter/fml/platform/win/wstring_conversion.h
FILE: ../../../flutter/fml/platform/win/wstring_conversion_unittests.cc
FILE: ../../../flutter/fml/posix_wrappers.h
FILE: ../../../flutter/fml/raster_thread_merger.cc
FILE: ../../../flutter/fml/raster_thread_merger.h
Expand All @@ -294,6 +296,9 @@ FILE: ../../../flutter/fml/shared_thread_merger.cc
FILE: ../../../flutter/fml/shared_thread_merger.h
FILE: ../../../flutter/fml/size.h
FILE: ../../../flutter/fml/status.h
FILE: ../../../flutter/fml/string_conversion.cc
FILE: ../../../flutter/fml/string_conversion.h
FILE: ../../../flutter/fml/string_conversion_unittests.cc
FILE: ../../../flutter/fml/synchronization/atomic_object.h
FILE: ../../../flutter/fml/synchronization/count_down_latch.cc
FILE: ../../../flutter/fml/synchronization/count_down_latch.h
Expand Down Expand Up @@ -2049,9 +2054,6 @@ FILE: ../../../flutter/shell/platform/windows/public/flutter_windows.h
FILE: ../../../flutter/shell/platform/windows/sequential_id_generator.cc
FILE: ../../../flutter/shell/platform/windows/sequential_id_generator.h
FILE: ../../../flutter/shell/platform/windows/sequential_id_generator_unittests.cc
FILE: ../../../flutter/shell/platform/windows/string_conversion.cc
FILE: ../../../flutter/shell/platform/windows/string_conversion.h
FILE: ../../../flutter/shell/platform/windows/string_conversion_unittests.cc
FILE: ../../../flutter/shell/platform/windows/system_utils.h
FILE: ../../../flutter/shell/platform/windows/system_utils_unittests.cc
FILE: ../../../flutter/shell/platform/windows/system_utils_win32.cc
Expand Down
48 changes: 43 additions & 5 deletions fml/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,11 @@ source_set("fml") {
sources += [ "backtrace_stub.cc" ]
}

public_deps = [ ":command_line" ]
public_deps = [
":build_config",
":command_line",
":string_conversion",
]

deps = [
"//third_party/abseil-cpp/absl/debugging:symbolize",
Expand Down Expand Up @@ -217,11 +221,7 @@ source_set("fml") {
"platform/win/native_library_win.cc",
"platform/win/paths_win.cc",
"platform/win/posix_wrappers_win.cc",
"platform/win/wstring_conversion.h",
]

# For wstring_conversion. See issue #50053.
defines = [ "_SILENCE_CXX17_CODECVT_HEADER_DEPRECATION_WARNING" ]
} else {
sources += [
"platform/posix/file_posix.cc",
Expand All @@ -233,6 +233,15 @@ source_set("fml") {
}
}

source_set("build_config") {
sources = [ "build_config.h" ]

public_configs = [
"//flutter:config",
"//flutter/common:flutter_config",
]
}

source_set("command_line") {
sources = [
"command_line.cc",
Expand All @@ -245,6 +254,30 @@ source_set("command_line") {
]
}

source_set("string_conversion") {
sources = [
"string_conversion.cc",
"string_conversion.h",
]

if (is_win) {
sources += [
"platform/win/wstring_conversion.cc",
"platform/win/wstring_conversion.h",
]

# TODO(cbracken): https://github.com/flutter/flutter/issues/50053
defines = [ "_SILENCE_CXX17_CODECVT_HEADER_DEPRECATION_WARNING" ]
}

deps = [ ":build_config" ]

public_configs = [
"//flutter:config",
"//flutter/common:flutter_config",
]
}

if (enable_unittests) {
test_fixtures("fml_fixtures") {
fixtures = []
Expand Down Expand Up @@ -286,6 +319,7 @@ if (enable_unittests) {
"message_loop_unittests.cc",
"paths_unittests.cc",
"raster_thread_merger_unittests.cc",
"string_conversion_unittests.cc",
"synchronization/count_down_latch_unittests.cc",
"synchronization/semaphore_unittest.cc",
"synchronization/sync_switch_unittest.cc",
Expand All @@ -307,6 +341,10 @@ if (enable_unittests) {
]
}

if (is_win) {
sources += [ "platform/win/wstring_conversion_unittests.cc" ]
}

deps = [
":fml_fixtures",
"//flutter/fml",
Expand Down
2 changes: 1 addition & 1 deletion fml/platform/win/errors_win.cc
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ std::string GetLastErrorMessage() {
std::wstringstream stream;
stream << message << " (" << last_error << ").";

return WideStringToString(stream.str());
return WideStringToUtf8(stream.str());
}

} // namespace fml
32 changes: 16 additions & 16 deletions fml/platform/win/file_win.cc
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ static std::string GetFullHandlePath(const fml::UniqueFD& handle) {
// was cached
if (result && memcmp(found.value().id.Identifier, info.FileId.Identifier,
sizeof(FILE_ID_INFO))) {
return WideStringToString(found.value().filename);
return WideStringToUtf8(found.value().filename);
} else {
fml::internal::os_win::UniqueFDTraits::RemoveCacheEntry(handle.get());
}
Expand All @@ -59,7 +59,7 @@ static std::string GetFullHandlePath(const fml::UniqueFD& handle) {
if (buffer_size == 0) {
return {};
}
return WideStringToString({buffer, buffer_size});
return WideStringToUtf8({buffer, buffer_size});
#endif
}

Expand Down Expand Up @@ -106,7 +106,7 @@ static DWORD GetShareFlags(FilePermission permission) {
}

static DWORD GetFileAttributesForUtf8Path(const char* absolute_path) {
return ::GetFileAttributes(StringToWideString(absolute_path).c_str());
return ::GetFileAttributes(Utf8ToWideString(absolute_path).c_str());
}

static DWORD GetFileAttributesForUtf8Path(const fml::UniqueFD& base_directory,
Expand Down Expand Up @@ -147,15 +147,15 @@ std::string CreateTemporaryDirectory() {
stream << temp_dir_container << "\\" << uuid_str;
auto temp_dir = stream.str();

auto dir_fd = OpenDirectory(WideStringToString(temp_dir).c_str(), true,
auto dir_fd = OpenDirectory(WideStringToUtf8(temp_dir).c_str(), true,
FilePermission::kReadWrite);
if (!dir_fd.is_valid()) {
FML_DLOG(ERROR) << "Could not get temporary directory file descriptor. "
<< GetLastErrorMessage();
return {};
}

return WideStringToString(std::move(temp_dir));
return WideStringToUtf8(std::move(temp_dir));
}

fml::UniqueFD OpenFile(const fml::UniqueFD& base_directory,
Expand All @@ -173,7 +173,7 @@ fml::UniqueFD OpenFile(const char* path,
return {};
}

auto file_name = StringToWideString({path});
auto file_name = Utf8ToWideString({path});

if (file_name.size() == 0) {
return {};
Expand Down Expand Up @@ -216,7 +216,7 @@ fml::UniqueFD OpenDirectory(const char* path,
return {};
}

auto file_name = StringToWideString({path});
auto file_name = Utf8ToWideString({path});

if (file_name.size() == 0) {
return {};
Expand Down Expand Up @@ -314,7 +314,7 @@ bool IsFile(const std::string& path) {
}

bool UnlinkDirectory(const char* path) {
if (!::RemoveDirectory(StringToWideString(path).c_str())) {
if (!::RemoveDirectory(Utf8ToWideString(path).c_str())) {
FML_DLOG(ERROR) << "Could not remove directory: '" << path << "'. "
<< GetLastErrorMessage();
return false;
Expand All @@ -324,7 +324,7 @@ bool UnlinkDirectory(const char* path) {

bool UnlinkDirectory(const fml::UniqueFD& base_directory, const char* path) {
if (!::RemoveDirectory(
StringToWideString(GetAbsolutePath(base_directory, path)).c_str())) {
Utf8ToWideString(GetAbsolutePath(base_directory, path)).c_str())) {
FML_DLOG(ERROR) << "Could not remove directory: '" << path << "'. "
<< GetLastErrorMessage();
return false;
Expand All @@ -333,7 +333,7 @@ bool UnlinkDirectory(const fml::UniqueFD& base_directory, const char* path) {
}

bool UnlinkFile(const char* path) {
if (!::DeleteFile(StringToWideString(path).c_str())) {
if (!::DeleteFile(Utf8ToWideString(path).c_str())) {
FML_DLOG(ERROR) << "Could not remove file: '" << path << "'. "
<< GetLastErrorMessage();
return false;
Expand All @@ -343,7 +343,7 @@ bool UnlinkFile(const char* path) {

bool UnlinkFile(const fml::UniqueFD& base_directory, const char* path) {
if (!::DeleteFile(
StringToWideString(GetAbsolutePath(base_directory, path)).c_str())) {
Utf8ToWideString(GetAbsolutePath(base_directory, path)).c_str())) {
FML_DLOG(ERROR) << "Could not remove file: '" << path << "'. "
<< GetLastErrorMessage();
return false;
Expand Down Expand Up @@ -436,8 +436,8 @@ bool WriteAtomically(const fml::UniqueFD& base_directory,

temp_file.reset();

if (!::MoveFile(StringToWideString(temp_file_path).c_str(),
StringToWideString(file_path).c_str())) {
if (!::MoveFile(Utf8ToWideString(temp_file_path).c_str(),
Utf8ToWideString(file_path).c_str())) {
FML_DLOG(ERROR)
<< "Could not replace temp file at correct path. File path: "
<< file_path << ". Temp file path: " << temp_file_path << " "
Expand All @@ -451,8 +451,8 @@ bool WriteAtomically(const fml::UniqueFD& base_directory,
bool VisitFiles(const fml::UniqueFD& directory, const FileVisitor& visitor) {
std::string search_pattern = GetFullHandlePath(directory) + "\\*";
WIN32_FIND_DATA find_file_data;
HANDLE find_handle = ::FindFirstFile(
StringToWideString(search_pattern).c_str(), &find_file_data);
HANDLE find_handle = ::FindFirstFile(Utf8ToWideString(search_pattern).c_str(),
&find_file_data);

if (find_handle == INVALID_HANDLE_VALUE) {
FML_DLOG(ERROR) << "Can't open the directory. Error: "
Expand All @@ -461,7 +461,7 @@ bool VisitFiles(const fml::UniqueFD& directory, const FileVisitor& visitor) {
}

do {
std::string filename = WideStringToString(find_file_data.cFileName);
std::string filename = WideStringToUtf8(find_file_data.cFileName);
if (filename != "." && filename != "..") {
if (!visitor(directory, filename)) {
::FindClose(find_handle);
Expand Down
2 changes: 1 addition & 1 deletion fml/platform/win/native_library_win.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ NativeLibrary::NativeLibrary(const char* path)
return;
}

handle_ = ::LoadLibrary(StringToWideString(path).c_str());
handle_ = ::LoadLibrary(Utf8ToWideString(path).c_str());
}

NativeLibrary::NativeLibrary(Handle handle, bool close_handle)
Expand Down
26 changes: 26 additions & 0 deletions fml/platform/win/wstring_conversion.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "flutter/fml/platform/win/wstring_conversion.h"

#include <codecvt>
#include <locale>
#include <string>

namespace fml {

using WideStringConverter =
std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>, wchar_t>;

std::string WideStringToUtf8(const std::wstring_view str) {
WideStringConverter converter;
return converter.to_bytes(str.data());
}

std::wstring Utf8ToWideString(const std::string_view str) {
WideStringConverter converter;
return converter.from_bytes(str.data());
}

} // namespace fml
18 changes: 5 additions & 13 deletions fml/platform/win/wstring_conversion.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,16 @@
#ifndef FLUTTER_FML_PLATFORM_WIN_WSTRING_CONVERSION_H_
#define FLUTTER_FML_PLATFORM_WIN_WSTRING_CONVERSION_H_

#include <codecvt>
#include <locale>
#include <string>

namespace fml {

using WideStringConvertor =
std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>, wchar_t>;
// Returns a UTF-8 encoded equivalent of a UTF-16 encoded input wide string.
std::string WideStringToUtf8(const std::wstring_view str);

inline std::wstring StringToWideString(const std::string& str) {
WideStringConvertor converter;
return converter.from_bytes(str);
}

inline std::string WideStringToString(const std::wstring& wstr) {
WideStringConvertor converter;
return converter.to_bytes(wstr);
}
// Returns a UTF-16 encoded wide string equivalent of a UTF-8 encoded input
// string.
std::wstring Utf8ToWideString(const std::string_view str);

} // namespace fml

Expand Down
37 changes: 37 additions & 0 deletions fml/platform/win/wstring_conversion_unittests.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "flutter/fml/platform/win/wstring_conversion.h"

#include "gtest/gtest.h"

namespace fml {
namespace testing {

TEST(StringConversion, Utf16ToWideStringEmpty) {
EXPECT_EQ(Utf8ToWideString(""), L"");
}

TEST(StringConversion, Utf8ToWideStringAscii) {
EXPECT_EQ(Utf8ToWideString("abc123"), L"abc123");
}

TEST(StringConversion, Utf8ToWideStringUnicode) {
EXPECT_EQ(Utf8ToWideString("\xe2\x98\x83"), L"\x2603");
}

TEST(StringConversion, WideStringToUtf8Empty) {
EXPECT_EQ(WideStringToUtf8(L""), "");
}

TEST(StringConversion, WideStringToUtf8Ascii) {
EXPECT_EQ(WideStringToUtf8(L"abc123"), "abc123");
}

TEST(StringConversion, WideStringToUtf8Unicode) {
EXPECT_EQ(WideStringToUtf8(L"\x2603"), "\xe2\x98\x83");
}

} // namespace testing
} // namespace fml
35 changes: 35 additions & 0 deletions fml/string_conversion.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "flutter/fml/string_conversion.h"

#include <codecvt>
#include <locale>
#include <string>

#include "flutter/fml/build_config.h"

#if defined(FML_OS_WIN)
// TODO(naifu): https://github.com/flutter/flutter/issues/98074
// Eliminate this workaround for a link error on Windows when the underlying
// bug is fixed.
std::locale::id std::codecvt<char16_t, char, _Mbstatet>::id;
#endif // defined(FML_OS_WIN)

namespace fml {

using Utf16StringConverter =
std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t>;

std::string Utf16ToUtf8(const std::u16string_view string) {
Utf16StringConverter converter;
return converter.to_bytes(string.data());
}

std::u16string Utf8ToUtf16(const std::string_view string) {
Utf16StringConverter converter;
return converter.from_bytes(string.data());
}

} // namespace fml
Loading