diff --git a/Package.swift b/Package.swift index 4c3aa2f8..68cc3aaa 100644 --- a/Package.swift +++ b/Package.swift @@ -501,6 +501,7 @@ let package = Package( "connections/status_test.cc", "connections/payload_test.cc", "internal/base/bluetooth_address_test.cc", + "internal/base/file_path_test.cc", "internal/base/files_test.cc", "internal/crypto/ed25519_unittest.cc", "internal/crypto_cros/aead_unittest.cc", diff --git a/README.md b/README.md index bab47915..859c3483 100644 --- a/README.md +++ b/README.md @@ -14,6 +14,10 @@ A peer-to-peer networking API that allows apps to easily discover, connect to, a An extension to Nearby Connections that features an extensible identity model for authentication and restricted visibility, resource management for system health, and proximity detection through sensor fusion. +### [Nearby for Embedded Systems](embedded/) + +A lightweight implementation of Fast Pair intended for embedded systems. + ## Contributing We encourage you to contribute to Nearby! Please check out the [Contributing to Nearby guide](CONTRIBUTING.md) for guidelines about how to proceed. diff --git a/internal/base/BUILD b/internal/base/BUILD index f3f0d075..84075b65 100644 --- a/internal/base/BUILD +++ b/internal/base/BUILD @@ -54,12 +54,32 @@ cc_library( ], ) +cc_library( + name = "compatible_u8_string", + hdrs = ["compatible_u8_string.h"], + visibility = ["//visibility:public"], +) + +cc_library( + name = "file_path", + srcs = ["file_path.cc"], + hdrs = ["file_path.h"], + visibility = ["//visibility:public"], + deps = [ + ":compatible_u8_string", + "@com_google_absl//absl/strings", + ], +) + cc_library( name = "files", srcs = ["files.cc"], hdrs = ["files.h"], visibility = ["//visibility:public"], - deps = ["//internal/platform:logging"], + deps = [ + ":file_path", + "//internal/platform:logging", + ], ) cc_test( @@ -78,6 +98,20 @@ cc_test( ], ) +cc_test( + name = "file_path_test", + size = "small", + timeout = "short", + srcs = [ + "file_path_test.cc", + ], + deps = [ + ":file_path", + "@com_github_protobuf_matchers//protobuf-matchers", + "@com_google_googletest//:gtest_main", + ], +) + cc_test( name = "files_test", size = "small", diff --git a/internal/base/compatible_u8_string.h b/internal/base/compatible_u8_string.h new file mode 100644 index 00000000..cccb3a3c --- /dev/null +++ b/internal/base/compatible_u8_string.h @@ -0,0 +1,32 @@ +// Copyright 2024 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#ifndef THIRD_PARTY_NEARBY_INTERNAL_BASE_COMPATIBLE_U8_STRING_H_ +#define THIRD_PARTY_NEARBY_INTERNAL_BASE_COMPATIBLE_U8_STRING_H_ + +#include + +namespace nearby { + +#if defined(__cpp_lib_char8_t) +inline std::string GetCompatibleU8String(std::u8string str) { + return reinterpret_cast(str.c_str()); +} +#else +inline std::string GetCompatibleU8String(std::string str) { return str; } +#endif + +} // namespace nearby + +#endif // THIRD_PARTY_NEARBY_INTERNAL_BASE_COMPATIBLE_U8_STRING_H_ diff --git a/internal/base/file_path.cc b/internal/base/file_path.cc new file mode 100644 index 00000000..856db0b5 --- /dev/null +++ b/internal/base/file_path.cc @@ -0,0 +1,48 @@ +// Copyright 2025 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include "internal/base/file_path.h" + +#include // NOLINT +#include + +#include "absl/strings/string_view.h" +#include "internal/base/compatible_u8_string.h" + +namespace nearby { + +FilePath::FilePath(absl::string_view path) + : path_(std::filesystem::u8path(path.begin(), path.end())) {} + +FilePath::FilePath(std::wstring_view path) + : path_(path) {} + +std::string FilePath::ToString() const { + return GetCompatibleU8String(path_.u8string()); +} + +std::wstring FilePath::ToWideString() const { + return path_.wstring(); +} + +FilePath& FilePath::append(const FilePath& subpath) { + path_ /= subpath.path_; + return *this; +} + +FilePath FilePath::GetParentPath() const { + return FilePath(path_.parent_path().wstring()); +} + +} // namespace nearby diff --git a/internal/base/file_path.h b/internal/base/file_path.h new file mode 100644 index 00000000..f156a39f --- /dev/null +++ b/internal/base/file_path.h @@ -0,0 +1,68 @@ +// Copyright 2025 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#ifndef THIRD_PARTY_NEARBY_INTERNAL_BASE_FILE_PATH_H_ +#define THIRD_PARTY_NEARBY_INTERNAL_BASE_FILE_PATH_H_ + +#include // NOLINT +#include + +#include "absl/strings/string_view.h" + +namespace nearby { + +// A replacement for std::filesystem::path that is exception safe, and is safe +// for use with Unicode paths in Windows. +class FilePath { + public: + FilePath() = default; + FilePath(const FilePath&) = default; + FilePath& operator=(const FilePath&) = default; + FilePath(FilePath&&) = default; + FilePath& operator=(FilePath&&) = default; + + // TODO: b/418255947 - Remove after migration is complete. + static FilePath FromPath(std::filesystem::path path) { + return FilePath(path.wstring()); + } + // Creates a FilePath from a UTF-8 encoded string. + // The `path` must be a valid UTF-8 sequence, otherwise the behavior is + // undefined. + explicit FilePath(absl::string_view path); + // Creates a FilePath from a unicode string. + explicit FilePath(std::wstring_view path); + + // Returns the path as a UTF-8 encoded string. + std::string ToString() const; + // Returns the path as a unicode string. + std::wstring ToWideString() const; + + // Appends the given `subpath` to this path using a path separator.. + FilePath& append(const FilePath& subpath); + + // Returns the path of the parent directory of this path. + FilePath GetParentPath() const; + + // TODO: b/418255947 - Remove after migration is complete. + std::filesystem::path GetPath() const { return path_; } + + friend auto operator<=>(const FilePath& lhs, const FilePath& rhs) = default; + + private: + std::filesystem::path path_; +}; + +} // namespace nearby + +#endif // THIRD_PARTY_NEARBY_INTERNAL_BASE_FILE_PATH_H_ diff --git a/internal/base/file_path_test.cc b/internal/base/file_path_test.cc new file mode 100644 index 00000000..6fd1ab6a --- /dev/null +++ b/internal/base/file_path_test.cc @@ -0,0 +1,92 @@ +// Copyright 2025 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include "internal/base/file_path.h" + +#include "gtest/gtest.h" + +namespace nearby { +namespace { + +TEST(FilePathTest, FromUTF8Windows) { + FilePath path("C:\\Users\\奥巴马\\Documents"); + EXPECT_EQ(path.ToString(), "C:\\Users\\奥巴马\\Documents"); +} + +TEST(FilePathTest, FromUTF8Linux) { + FilePath path("/usr/local/home/奥巴马/Documents"); + EXPECT_EQ(path.ToString(), "/usr/local/home/奥巴马/Documents"); +} + +TEST(FilePathTest, FromAscii) { + FilePath path("/usr/local/home/bob/Documents"); + EXPECT_EQ(path.ToString(), "/usr/local/home/bob/Documents"); +} + +TEST(FilePathTest, FromUnicodeWindows) { + FilePath path(L"C:\\Users\\奥巴马\\Documents"); + EXPECT_EQ(path.ToString(), "C:\\Users\\奥巴马\\Documents"); +} + +TEST(FilePathTest, FromUnicodeLinux) { + FilePath path(L"/usr/local/home/奥巴马/Documents"); + EXPECT_EQ(path.ToString(), "/usr/local/home/奥巴马/Documents"); +} + +TEST(FilePathTest, FromUTF8WindowsToWideString) { + FilePath path("C:\\Users\\奥巴马\\Documents"); + EXPECT_EQ(path.ToWideString(), L"C:\\Users\\奥巴马\\Documents"); +} + +TEST(FilePathTest, FromUTF8LinuxToWideString) { + FilePath path("/usr/local/home/奥巴马/Documents"); + EXPECT_EQ(path.ToWideString(), L"/usr/local/home/奥巴马/Documents"); +} + +TEST(FilePathTest, FromAsciiToWideString) { + FilePath path("/usr/local/home/bob/Documents"); + EXPECT_EQ(path.ToWideString(), L"/usr/local/home/bob/Documents"); +} + +TEST(FilePathTest, FromUnicodeWindowsToWideString) { + FilePath path(L"C:\\Users\\奥巴马\\Documents"); + EXPECT_EQ(path.ToWideString(), L"C:\\Users\\奥巴马\\Documents"); +} + +TEST(FilePathTest, FromUnicodeLinuxToWideString) { + FilePath path(L"/usr/local/home/奥巴马/Documents"); + EXPECT_EQ(path.ToWideString(), L"/usr/local/home/奥巴马/Documents"); +} + +TEST(FilePathTest, AppendSuccess) { + FilePath path("/usr/local/home/奥巴马/Documents"); + FilePath sub_path("贝拉克/temp"); + + path.append(sub_path); +#if defined(_WIN32) + EXPECT_EQ(path.ToWideString(), + L"/usr/local/home/奥巴马/Documents\\贝拉克/temp"); +#else + EXPECT_EQ(path.ToWideString(), + L"/usr/local/home/奥巴马/Documents/贝拉克/temp"); +#endif +} + +TEST(FilePathTest, GetParentPathSuccess) { + FilePath path("/usr/local/home/奥巴马/Documents"); + EXPECT_EQ(path.GetParentPath().ToString(), "/usr/local/home/奥巴马"); +} + +} // namespace +} // namespace nearby diff --git a/internal/base/files.cc b/internal/base/files.cc index 70c64453..8ad9a2bf 100644 --- a/internal/base/files.cc +++ b/internal/base/files.cc @@ -19,6 +19,7 @@ #include #include // NOLINT(build/c++11) +#include "internal/base/file_path.h" #include "internal/platform/logging.h" namespace nearby::sharing { @@ -62,20 +63,20 @@ bool RemoveFile(const std::filesystem::path& path) { return std::filesystem::remove(path, error_code); } -std::optional GetTemporaryDirectory() { +std::optional GetTemporaryDirectory() { std::error_code error_code; std::filesystem::path temp_dir = std::filesystem::temp_directory_path(error_code); if (temp_dir.empty()) { return std::nullopt; } - return temp_dir; + return FilePath::FromPath(temp_dir); } -std::filesystem::path CurrentDirectory() { +FilePath CurrentDirectory() { // temp_directory_path() returns empty path on error. std::error_code error_code; - return std::filesystem::current_path(error_code); + return FilePath::FromPath(std::filesystem::current_path(error_code)); } bool Rename(const std::filesystem::path& old_path, diff --git a/internal/base/files.h b/internal/base/files.h index 40b1a976..2c75bfae 100644 --- a/internal/base/files.h +++ b/internal/base/files.h @@ -19,6 +19,8 @@ #include // NOLINT(build/c++17) #include +#include "internal/base/file_path.h" + // This file contains exception safe wrappers to access common std::filesystem // functions. namespace nearby::sharing { @@ -37,10 +39,10 @@ bool DirectoryExists(const std::filesystem::path& path); bool RemoveFile(const std::filesystem::path& path); // Returns path to a temporary directory if available. -std::optional GetTemporaryDirectory(); +std::optional GetTemporaryDirectory(); // Returns path to the current directory. On failure returns an empty path. -std::filesystem::path CurrentDirectory(); +FilePath CurrentDirectory(); // Renames the file at old_path to new_path. // Returns true on success. diff --git a/internal/platform/BUILD b/internal/platform/BUILD index f03a249d..6665ed10 100644 --- a/internal/platform/BUILD +++ b/internal/platform/BUILD @@ -272,6 +272,7 @@ cc_library( ":base", ":util", "//connections/implementation/flags:connections_flags", + "//internal/base:file_path", "//internal/base:files", "//internal/crypto_cros", "//internal/flags:nearby_flags", diff --git a/internal/platform/device_info.h b/internal/platform/device_info.h index 3cbb716f..159d2d74 100644 --- a/internal/platform/device_info.h +++ b/internal/platform/device_info.h @@ -16,12 +16,12 @@ #define PLATFORM_PUBLIC_DEVICE_INFO_H_ #include -#include // NOLINT #include #include #include #include "absl/strings/string_view.h" +#include "internal/base/file_path.h" #include "internal/platform/implementation/device_info.h" namespace nearby { @@ -35,13 +35,13 @@ class DeviceInfo { virtual api::DeviceInfo::DeviceType GetDeviceType() const = 0; virtual api::DeviceInfo::OsType GetOsType() const = 0; - virtual std::filesystem::path GetDownloadPath() const = 0; - virtual std::filesystem::path GetAppDataPath() const = 0; - virtual std::filesystem::path GetTemporaryPath() const = 0; - virtual std::filesystem::path GetLogPath() const = 0; + virtual FilePath GetDownloadPath() const = 0; + virtual FilePath GetAppDataPath() const = 0; + virtual FilePath GetTemporaryPath() const = 0; + virtual FilePath GetLogPath() const = 0; virtual std::optional GetAvailableDiskSpaceInBytes( - const std::filesystem::path& path) const = 0; + const FilePath& path) const = 0; virtual bool IsScreenLocked() const = 0; virtual void RegisterScreenLockedListener( diff --git a/internal/platform/device_info_impl.cc b/internal/platform/device_info_impl.cc index f342dec4..b0abe001 100644 --- a/internal/platform/device_info_impl.cc +++ b/internal/platform/device_info_impl.cc @@ -21,6 +21,7 @@ #include #include "absl/strings/string_view.h" #include "internal/base/files.h" +#include "internal/base/file_path.h" #include "internal/platform/implementation/device_info.h" namespace nearby { @@ -43,9 +44,8 @@ api::DeviceInfo::OsType DeviceInfoImpl::GetOsType() const { return device_info_impl_->GetOsType(); } -std::filesystem::path DeviceInfoImpl::GetDownloadPath() const { - std::optional path = - device_info_impl_->GetDownloadPath(); +FilePath DeviceInfoImpl::GetDownloadPath() const { + std::optional path = device_info_impl_->GetDownloadPath(); if (path.has_value()) { return *path; } @@ -53,9 +53,8 @@ std::filesystem::path DeviceInfoImpl::GetDownloadPath() const { nearby::sharing::CurrentDirectory()); } -std::filesystem::path DeviceInfoImpl::GetAppDataPath() const { - std::optional path = - device_info_impl_->GetLocalAppDataPath(); +FilePath DeviceInfoImpl::GetAppDataPath() const { + std::optional path = device_info_impl_->GetLocalAppDataPath(); if (path.has_value()) { return *path; } @@ -63,9 +62,8 @@ std::filesystem::path DeviceInfoImpl::GetAppDataPath() const { nearby::sharing::CurrentDirectory()); } -std::filesystem::path DeviceInfoImpl::GetTemporaryPath() const { - std::optional path = - device_info_impl_->GetTemporaryPath(); +FilePath DeviceInfoImpl::GetTemporaryPath() const { + std::optional path = device_info_impl_->GetTemporaryPath(); if (path.has_value()) { return *path; } @@ -73,16 +71,16 @@ std::filesystem::path DeviceInfoImpl::GetTemporaryPath() const { nearby::sharing::CurrentDirectory()); } -std::filesystem::path DeviceInfoImpl::GetLogPath() const { - std::optional path = device_info_impl_->GetLogPath(); +FilePath DeviceInfoImpl::GetLogPath() const { + std::optional path = device_info_impl_->GetLogPath(); return path.value_or(GetTemporaryPath()); } std::optional DeviceInfoImpl::GetAvailableDiskSpaceInBytes( - const std::filesystem::path& path) const { + const FilePath& path) const { std::error_code error_code; std::filesystem::space_info space_info = - std::filesystem::space(path, error_code); + std::filesystem::space(path.GetPath(), error_code); if (error_code.value() == 0) { return space_info.available; } diff --git a/internal/platform/device_info_impl.h b/internal/platform/device_info_impl.h index efaa734d..fc5ba6fe 100644 --- a/internal/platform/device_info_impl.h +++ b/internal/platform/device_info_impl.h @@ -16,13 +16,13 @@ #define PLATFORM_PUBLIC_DEVICE_INFO_IMPL_H_ #include -#include // NOLINT #include #include #include #include #include "absl/strings/string_view.h" +#include "internal/base/file_path.h" #include "internal/platform/device_info.h" #include "internal/platform/implementation/device_info.h" #include "internal/platform/implementation/platform.h" @@ -38,13 +38,13 @@ class DeviceInfoImpl : public DeviceInfo { api::DeviceInfo::DeviceType GetDeviceType() const override; api::DeviceInfo::OsType GetOsType() const override; - std::filesystem::path GetDownloadPath() const override; - std::filesystem::path GetAppDataPath() const override; - std::filesystem::path GetTemporaryPath() const override; - std::filesystem::path GetLogPath() const override; + FilePath GetDownloadPath() const override; + FilePath GetAppDataPath() const override; + FilePath GetTemporaryPath() const override; + FilePath GetLogPath() const override; std::optional GetAvailableDiskSpaceInBytes( - const std::filesystem::path& path) const override; + const FilePath& path) const override; bool IsScreenLocked() const override; void RegisterScreenLockedListener( diff --git a/internal/platform/implementation/BUILD b/internal/platform/implementation/BUILD index 2db346df..2450e1ca 100644 --- a/internal/platform/implementation/BUILD +++ b/internal/platform/implementation/BUILD @@ -112,6 +112,7 @@ cc_library( "//sharing:__subpackages__", ], deps = [ + "//internal/base:file_path", "//internal/crypto_cros", "//internal/platform:base", "//internal/platform/implementation/shared:crypto", # Non-chromium impl diff --git a/internal/platform/implementation/apple/BUILD b/internal/platform/implementation/apple/BUILD index a1205c78..73fea964 100644 --- a/internal/platform/implementation/apple/BUILD +++ b/internal/platform/implementation/apple/BUILD @@ -67,6 +67,7 @@ objc_library( "//third_party/apple_frameworks:Foundation", "//third_party/apple_frameworks:Network", "@nlohmann_json//:json", + "//internal/base:file_path", "//internal/platform:base", "//internal/platform/implementation:comm", "//internal/platform/implementation:platform", diff --git a/internal/platform/implementation/apple/device_info.h b/internal/platform/implementation/apple/device_info.h index de250744..ce436201 100644 --- a/internal/platform/implementation/apple/device_info.h +++ b/internal/platform/implementation/apple/device_info.h @@ -15,12 +15,12 @@ #ifndef PLATFORM_IMPL_APPLE_DEVICE_INFO_H_ #define PLATFORM_IMPL_APPLE_DEVICE_INFO_H_ -#include // NOLINT(build/c++17) #include #include #include #include "absl/strings/string_view.h" +#include "internal/base/file_path.h" #include "internal/platform/implementation/device_info.h" namespace nearby { @@ -34,17 +34,17 @@ class DeviceInfo : public api::DeviceInfo { api::DeviceInfo::OsType GetOsType() const override; - std::optional GetDownloadPath() const override; + std::optional GetDownloadPath() const override; - std::optional GetLocalAppDataPath() const override; + std::optional GetLocalAppDataPath() const override; - std::optional GetCommonAppDataPath() const override; + std::optional GetCommonAppDataPath() const override; - std::optional GetTemporaryPath() const override; + std::optional GetTemporaryPath() const override; - std::optional GetLogPath() const override; + std::optional GetLogPath() const override; - std::optional GetCrashDumpPath() const override; + std::optional GetCrashDumpPath() const override; bool IsScreenLocked() const override; diff --git a/internal/platform/implementation/apple/device_info.mm b/internal/platform/implementation/apple/device_info.mm index 0e605df7..5ea39bc5 100644 --- a/internal/platform/implementation/apple/device_info.mm +++ b/internal/platform/implementation/apple/device_info.mm @@ -19,13 +19,13 @@ #import #endif -#include // NOLINT(build/c++17) #include #include #include #include #include "absl/strings/string_view.h" +#include "internal/base/file_path.h" #include "internal/platform/implementation/device_info.h" #import "GoogleToolboxForMac/GTMLogger.h" @@ -78,7 +78,7 @@ api::DeviceInfo::OsType DeviceInfo::GetOsType() const { #endif } -std::optional DeviceInfo::GetDownloadPath() const { +std::optional DeviceInfo::GetDownloadPath() const { NSFileManager *manager = [NSFileManager defaultManager]; NSError *error = nil; @@ -92,10 +92,10 @@ std::optional DeviceInfo::GetDownloadPath() const { return std::nullopt; } - return std::filesystem::path([downloadsURL.path cString]); + return FilePath(absl::string_view([downloadsURL.path cString])); } -std::optional DeviceInfo::GetLocalAppDataPath() const { +std::optional DeviceInfo::GetLocalAppDataPath() const { NSFileManager *manager = [NSFileManager defaultManager]; NSError *error = nil; @@ -109,18 +109,18 @@ std::optional DeviceInfo::GetLocalAppDataPath() const { return std::nullopt; } - return std::filesystem::path([applicationSupportURL.path cString]); + return FilePath(absl::string_view([applicationSupportURL.path cString])); } -std::optional DeviceInfo::GetCommonAppDataPath() const { +std::optional DeviceInfo::GetCommonAppDataPath() const { return GetLocalAppDataPath(); } -std::optional DeviceInfo::GetTemporaryPath() const { - return std::filesystem::path([NSTemporaryDirectory() cString]); +std::optional DeviceInfo::GetTemporaryPath() const { + return FilePath(absl::string_view([NSTemporaryDirectory() cString])); } -std::optional DeviceInfo::GetLogPath() const { +std::optional DeviceInfo::GetLogPath() const { NSFileManager *manager = [NSFileManager defaultManager]; NSError *error = nil; @@ -139,10 +139,10 @@ std::optional DeviceInfo::GetLogPath() const { NSURL *logsURL = [applicationSupportURL URLByAppendingPathComponent:@"Google/Nearby/Sharing/Logs"]; - return std::filesystem::path([logsURL.path cString]); + return FilePath(absl::string_view([logsURL.path cString])); } -std::optional DeviceInfo::GetCrashDumpPath() const { +std::optional DeviceInfo::GetCrashDumpPath() const { NSFileManager *manager = [NSFileManager defaultManager]; NSError *error = nil; @@ -161,7 +161,7 @@ std::optional DeviceInfo::GetCrashDumpPath() const { NSURL *crashDumpsURL = [applicationSupportURL URLByAppendingPathComponent:@"Google/Nearby/Sharing/CrashDumps"]; - return std::filesystem::path([crashDumpsURL.path cString]); + return FilePath(absl::string_view([crashDumpsURL.path cString])); } bool DeviceInfo::IsScreenLocked() const { return false; } diff --git a/internal/platform/implementation/device_info.h b/internal/platform/implementation/device_info.h index 8786d6b7..19aca037 100644 --- a/internal/platform/implementation/device_info.h +++ b/internal/platform/implementation/device_info.h @@ -15,12 +15,12 @@ #ifndef PLATFORM_API_DEVICE_INFO_H_ #define PLATFORM_API_DEVICE_INFO_H_ -#include // NOLINT #include #include #include #include "absl/strings/string_view.h" +#include "internal/base/file_path.h" namespace nearby { namespace api { @@ -46,12 +46,12 @@ class DeviceInfo { virtual OsType GetOsType() const = 0; // Gets known paths of current user. - virtual std::optional GetDownloadPath() const = 0; - virtual std::optional GetLocalAppDataPath() const = 0; - virtual std::optional GetCommonAppDataPath() const = 0; - virtual std::optional GetTemporaryPath() const = 0; - virtual std::optional GetLogPath() const = 0; - virtual std::optional GetCrashDumpPath() const = 0; + virtual std::optional GetDownloadPath() const = 0; + virtual std::optional GetLocalAppDataPath() const = 0; + virtual std::optional GetCommonAppDataPath() const = 0; + virtual std::optional GetTemporaryPath() const = 0; + virtual std::optional GetLogPath() const = 0; + virtual std::optional GetCrashDumpPath() const = 0; // Monitor screen status virtual bool IsScreenLocked() const = 0; diff --git a/internal/platform/implementation/g3/BUILD b/internal/platform/implementation/g3/BUILD index f1fea3fd..7d075edf 100644 --- a/internal/platform/implementation/g3/BUILD +++ b/internal/platform/implementation/g3/BUILD @@ -36,6 +36,8 @@ cc_library( visibility = ["//visibility:private"], deps = [ ":preferences_repository", + "//internal/base:file_path", + "//internal/base:files", "//internal/platform:base", "//internal/platform:test_util", "//internal/platform:types", diff --git a/internal/platform/implementation/g3/device_info.h b/internal/platform/implementation/g3/device_info.h index 46ebe45c..c8888e49 100644 --- a/internal/platform/implementation/g3/device_info.h +++ b/internal/platform/implementation/g3/device_info.h @@ -16,7 +16,6 @@ #define PLATFORM_IMPL_G3_DEVICE_INFO_H_ #include -#include // NOLINT #include #include #include @@ -24,6 +23,8 @@ #include "absl/container/flat_hash_map.h" #include "absl/strings/string_view.h" +#include "internal/base/file_path.h" +#include "internal/base/files.h" #include "internal/platform/implementation/device_info.h" namespace nearby { @@ -43,33 +44,35 @@ class DeviceInfo : public api::DeviceInfo { return api::DeviceInfo::OsType::kChromeOs; } - std::optional GetDownloadPath() const override { - return std::filesystem::temp_directory_path(); + std::optional GetDownloadPath() const override { + return nearby::sharing::GetTemporaryDirectory(); } - std::optional GetLocalAppDataPath() const override { + std::optional GetLocalAppDataPath() const override { const char* home_dir = getenv("HOME"); if (home_dir == nullptr) { - return std::filesystem::temp_directory_path(); + return nearby::sharing::GetTemporaryDirectory(); } // Yhis matches the .NET LocalAppData directory on Linux. - return std::filesystem::path(home_dir).append(".local").append("share"); + return FilePath(home_dir) + .append(FilePath(".local")) + .append(FilePath("share")); } - std::optional GetCommonAppDataPath() const override { - return std::filesystem::temp_directory_path(); + std::optional GetCommonAppDataPath() const override { + return nearby::sharing::GetTemporaryDirectory(); } - std::optional GetTemporaryPath() const override { - return std::filesystem::temp_directory_path(); + std::optional GetTemporaryPath() const override { + return nearby::sharing::GetTemporaryDirectory(); } - std::optional GetLogPath() const override { - return std::filesystem::temp_directory_path(); + std::optional GetLogPath() const override { + return nearby::sharing::GetTemporaryDirectory(); } - std::optional GetCrashDumpPath() const override { - return std::filesystem::temp_directory_path(); + std::optional GetCrashDumpPath() const override { + return nearby::sharing::GetTemporaryDirectory(); } bool IsScreenLocked() const override { return false; } diff --git a/internal/platform/implementation/g3/preferences_manager.cc b/internal/platform/implementation/g3/preferences_manager.cc index bc6324cc..37fca236 100644 --- a/internal/platform/implementation/g3/preferences_manager.cc +++ b/internal/platform/implementation/g3/preferences_manager.cc @@ -14,18 +14,23 @@ #include "internal/platform/implementation/g3/preferences_manager.h" -#include // NOLINT(build/c++17) +#include #include -#include #include #include #include +#include "absl/strings/str_cat.h" #include "absl/strings/string_view.h" +#include "absl/synchronization/mutex.h" +#include "absl/time/time.h" +#include "absl/types/span.h" #include "nlohmann/json.hpp" #include "nlohmann/json_fwd.hpp" -#include "internal/platform/implementation/g3/device_info.h" +#include "internal/base/file_path.h" +#include "internal/platform/device_info_impl.h" #include "internal/platform/implementation/g3/preferences_repository.h" +#include "internal/platform/implementation/preferences_manager.h" #include "internal/platform/logging.h" namespace nearby { @@ -36,16 +41,12 @@ using json = ::nlohmann::json; PreferencesManager::PreferencesManager(absl::string_view file_path) : api::PreferencesManager(file_path) { - auto device_info = std::make_unique(); - std::optional path = - device_info->GetLocalAppDataPath(); - if (!path.has_value()) { - path = std::filesystem::temp_directory_path(); - } + auto device_info = std::make_unique(); + FilePath path = device_info->GetAppDataPath(); - std::filesystem::path full_path = *path / std::string(file_path); + path.append(FilePath(file_path)); preferences_repository_ = - std::make_unique(full_path.string()); + std::make_unique(path.ToString()); value_ = preferences_repository_->LoadPreferences(); } diff --git a/internal/platform/implementation/windows/BUILD b/internal/platform/implementation/windows/BUILD index 260127bd..75ce398c 100644 --- a/internal/platform/implementation/windows/BUILD +++ b/internal/platform/implementation/windows/BUILD @@ -47,6 +47,7 @@ cc_library( deps = [ ":comm", "//internal/base:bluetooth_address", + "//internal/base:file_path", "//internal/base:files", "//internal/flags:nearby_flags", "//internal/platform:base", @@ -244,6 +245,7 @@ cc_library( "//connections/implementation/mediums/ble_v2:ble_advertisement_header", "//connections/implementation/mediums/ble_v2:bloom_filter", "//internal/account", + "//internal/base:file_path", "//internal/base:files", "//internal/flags:nearby_flags", "//internal/platform:base", diff --git a/internal/platform/implementation/windows/device_info.cc b/internal/platform/implementation/windows/device_info.cc index 390dea61..48a95f4e 100644 --- a/internal/platform/implementation/windows/device_info.cc +++ b/internal/platform/implementation/windows/device_info.cc @@ -18,7 +18,6 @@ #include #include -#include // NOLINT #include #include #include @@ -26,6 +25,7 @@ #include "absl/strings/string_view.h" #include "absl/synchronization/mutex.h" #include "internal/base/files.h" +#include "internal/base/file_path.h" #include "internal/platform/implementation/device_info.h" #include "internal/platform/implementation/windows/string_utils.h" #include "internal/platform/logging.h" @@ -51,8 +51,8 @@ using IVectorView = winrt::Windows::Foundation::Collections::IVectorView; template using IAsyncOperation = winrt::Windows::Foundation::IAsyncOperation; -constexpr char logs_relative_path[] = "Google\\Nearby\\Sharing\\Logs"; -constexpr char crash_dumps_relative_path[] = +constexpr absl::string_view kLogsRelativePath = "Google\\Nearby\\Sharing\\Logs"; +constexpr absl::string_view kCrashDumpsRelativePath = "Google\\Nearby\\Sharing\\CrashDumps"; std::optional DeviceInfo::GetOsDeviceName() const { @@ -88,65 +88,64 @@ api::DeviceInfo::OsType DeviceInfo::GetOsType() const { return api::DeviceInfo::OsType::kWindows; } -std::optional DeviceInfo::GetDownloadPath() const { +std::optional DeviceInfo::GetDownloadPath() const { PWSTR path; HRESULT result = SHGetKnownFolderPath(FOLDERID_Downloads, KF_FLAG_DEFAULT, nullptr, &path); if (result == S_OK) { std::wstring download_path{path}; CoTaskMemFree(path); - return std::filesystem::path(download_path); + return FilePath(std::wstring_view(download_path)); } CoTaskMemFree(path); return std::nullopt; } -std::optional DeviceInfo::GetLocalAppDataPath() const { +std::optional DeviceInfo::GetLocalAppDataPath() const { PWSTR path; HRESULT result = SHGetKnownFolderPath(FOLDERID_LocalAppData, KF_FLAG_DEFAULT, /*hToken=*/nullptr, &path); if (result == S_OK) { std::wstring local_appdata_path{path}; CoTaskMemFree(path); - return std::filesystem::path(local_appdata_path); + return FilePath(std::wstring_view(local_appdata_path)); } CoTaskMemFree(path); return std::nullopt; } -std::optional DeviceInfo::GetCommonAppDataPath() const { +std::optional DeviceInfo::GetCommonAppDataPath() const { PWSTR path; HRESULT result = SHGetKnownFolderPath(FOLDERID_ProgramData, KF_FLAG_DEFAULT, /*hToken=*/nullptr, &path); if (result == S_OK) { std::wstring common_app_data_path{path}; CoTaskMemFree(path); - return std::filesystem::path(common_app_data_path); + return FilePath(std::wstring_view(common_app_data_path)); } CoTaskMemFree(path); return std::nullopt; } -std::optional DeviceInfo::GetTemporaryPath() const { +std::optional DeviceInfo::GetTemporaryPath() const { return nearby::sharing::GetTemporaryDirectory(); } -std::optional DeviceInfo::GetLogPath() const { - auto prefix_path = GetLocalAppDataPath(); +std::optional DeviceInfo::GetLogPath() const { + std::optional prefix_path = GetLocalAppDataPath(); if (prefix_path.has_value()) { - return std::filesystem::path(prefix_path.value() / logs_relative_path); + return prefix_path.value().append(FilePath(kLogsRelativePath)); } return std::nullopt; } -std::optional DeviceInfo::GetCrashDumpPath() const { - auto prefix_path = GetLocalAppDataPath(); +std::optional DeviceInfo::GetCrashDumpPath() const { + std::optional prefix_path = GetLocalAppDataPath(); if (prefix_path.has_value()) { - return std::filesystem::path(prefix_path.value() / - crash_dumps_relative_path); + return prefix_path.value().append(FilePath(kCrashDumpsRelativePath)); } return std::nullopt; } diff --git a/internal/platform/implementation/windows/device_info.h b/internal/platform/implementation/windows/device_info.h index 6d9de6a3..1b4cfe99 100644 --- a/internal/platform/implementation/windows/device_info.h +++ b/internal/platform/implementation/windows/device_info.h @@ -15,7 +15,6 @@ #ifndef PLATFORM_IMPL_WINDOWS_DEVICE_INFO_H_ #define PLATFORM_IMPL_WINDOWS_DEVICE_INFO_H_ -#include // NOLINT #include #include #include @@ -23,6 +22,7 @@ #include "absl/base/thread_annotations.h" #include "absl/strings/string_view.h" #include "absl/synchronization/mutex.h" +#include "internal/base/file_path.h" #include "internal/platform/implementation/device_info.h" #include "internal/platform/implementation/windows/session_manager.h" @@ -37,12 +37,12 @@ class DeviceInfo : public api::DeviceInfo { api::DeviceInfo::DeviceType GetDeviceType() const override; api::DeviceInfo::OsType GetOsType() const override; - std::optional GetDownloadPath() const override; - std::optional GetLocalAppDataPath() const override; - std::optional GetCommonAppDataPath() const override; - std::optional GetTemporaryPath() const override; - std::optional GetLogPath() const override; - std::optional GetCrashDumpPath() const override; + std::optional GetDownloadPath() const override; + std::optional GetLocalAppDataPath() const override; + std::optional GetCommonAppDataPath() const override; + std::optional GetTemporaryPath() const override; + std::optional GetLogPath() const override; + std::optional GetCrashDumpPath() const override; bool IsScreenLocked() const override; void RegisterScreenLockedListener( diff --git a/internal/platform/implementation/windows/preferences_manager.cc b/internal/platform/implementation/windows/preferences_manager.cc index 1a45c549..35b48bbd 100644 --- a/internal/platform/implementation/windows/preferences_manager.cc +++ b/internal/platform/implementation/windows/preferences_manager.cc @@ -15,7 +15,6 @@ #include "internal/platform/implementation/windows/preferences_manager.h" #include -#include // NOLINT(build/c++17) #include #include #include @@ -30,6 +29,7 @@ #include "nlohmann/json.hpp" #include "nlohmann/json_fwd.hpp" #include "internal/base/files.h" +#include "internal/base/file_path.h" #include "internal/platform/implementation/platform.h" #include "internal/platform/implementation/preferences_manager.h" #include "internal/platform/implementation/windows/preferences_repository.h" @@ -43,7 +43,7 @@ using json = ::nlohmann::json; PreferencesManager::PreferencesManager(absl::string_view file_path) : api::PreferencesManager(file_path) { - std::optional path = + std::optional path = nearby::api::ImplementationPlatform::CreateDeviceInfo() ->GetLocalAppDataPath(); if (!path.has_value()) { @@ -51,9 +51,9 @@ PreferencesManager::PreferencesManager(absl::string_view file_path) nearby::sharing::CurrentDirectory()); } - std::filesystem::path full_path = *path / std::string(file_path); + path->append(FilePath(file_path)); preferences_repository_ = - std::make_unique(full_path.string()); + std::make_unique(path->ToString()); value_ = preferences_repository_->LoadPreferences(); } diff --git a/internal/platform/implementation/windows/preferences_repository_test.cc b/internal/platform/implementation/windows/preferences_repository_test.cc index d1732de8..1533bdea 100644 --- a/internal/platform/implementation/windows/preferences_repository_test.cc +++ b/internal/platform/implementation/windows/preferences_repository_test.cc @@ -21,6 +21,7 @@ #include "gtest/gtest.h" #include "nlohmann/json.hpp" #include "nlohmann/json_fwd.hpp" +#include "internal/base/file_path.h" #include "internal/platform/implementation/device_info.h" #include "internal/platform/implementation/platform.h" @@ -41,36 +42,36 @@ TEST(PreferencesRepository, LoadWithBadPath) { } TEST(PreferencesRepository, RecoverFromBadPreferences) { - std::optional app_data_path = + std::optional app_data_path = api::ImplementationPlatform::CreateDeviceInfo()->GetLocalAppDataPath(); ASSERT_TRUE(app_data_path.has_value()); - std::filesystem::path full_path = *app_data_path / kPreferencesPath; - std::filesystem::path full_name = full_path / kPreferencesFileName; + FilePath full_path = app_data_path->append(FilePath(kPreferencesPath)); + FilePath full_name = app_data_path->append(FilePath(kPreferencesFileName)); - if (std::filesystem::exists(full_name)) { - std::filesystem::remove(full_name); + if (std::filesystem::exists(full_name.GetPath())) { + std::filesystem::remove(full_name.GetPath()); } - std::ofstream pref_file(full_name.c_str()); + std::ofstream pref_file(full_name.GetPath()); pref_file << "\"Bad top level object\""; pref_file.close(); - PreferencesRepository preferences_repository{full_path.string()}; + PreferencesRepository preferences_repository{full_path.ToString()}; EXPECT_EQ(preferences_repository.LoadPreferences(), json::object()); } TEST(PreferencesRepository, SaveAndLoadPreferences) { - std::optional app_data_path = + std::optional app_data_path = api::ImplementationPlatform::CreateDeviceInfo()->GetLocalAppDataPath(); ASSERT_TRUE(app_data_path.has_value()); - std::filesystem::path full_path = *app_data_path / kPreferencesPath; - std::filesystem::path full_name = full_path / kPreferencesFileName; + FilePath full_path = app_data_path->append(FilePath(kPreferencesPath)); + FilePath full_name = app_data_path->append(FilePath(kPreferencesFileName)); - if (std::filesystem::exists(full_name)) { - std::filesystem::remove(full_name); + if (std::filesystem::exists(full_name.GetPath())) { + std::filesystem::remove(full_name.GetPath()); } - PreferencesRepository preferences_repository{full_path.string()}; + PreferencesRepository preferences_repository{full_path.ToString()}; json data; data["key1"] = "value1"; data["key2"] = "value2"; @@ -79,32 +80,32 @@ TEST(PreferencesRepository, SaveAndLoadPreferences) { EXPECT_EQ(result.size(), 2); EXPECT_EQ(result["key1"], "value1"); EXPECT_EQ(result["key2"], "value2"); - std::filesystem::remove(full_name); + std::filesystem::remove(full_name.GetPath()); } TEST(PreferencesRepository, LoadFromBackup) { - std::optional app_data_path = + std::optional app_data_path = api::ImplementationPlatform::CreateDeviceInfo()->GetLocalAppDataPath(); ASSERT_TRUE(app_data_path.has_value()); - std::filesystem::path full_path = *app_data_path / kPreferencesPath; - std::filesystem::path full_name = full_path / kPreferencesFileName; - std::filesystem::path full_name_backup = - full_path / kPreferencesBackupFileName; + FilePath full_path = app_data_path->append(FilePath(kPreferencesPath)); + FilePath full_name = app_data_path->append(FilePath(kPreferencesFileName)); + FilePath full_name_backup = full_path; + full_name_backup.append(FilePath(kPreferencesBackupFileName)); - if (std::filesystem::exists(full_name)) { - std::filesystem::remove(full_name); + if (std::filesystem::exists(full_name.GetPath())) { + std::filesystem::remove(full_name.GetPath()); } - if (std::filesystem::exists(full_name_backup)) { - std::filesystem::remove(full_name_backup); + if (std::filesystem::exists(full_name_backup.GetPath())) { + std::filesystem::remove(full_name_backup.GetPath()); } - PreferencesRepository preferences_repository{full_path.string()}; + PreferencesRepository preferences_repository{full_path.ToString()}; json data; data["key1"] = "value1"; data["key2"] = "value2"; - std::ofstream backup_file(full_name_backup.c_str()); + std::ofstream backup_file(full_name_backup.GetPath()); backup_file << data; backup_file.close(); @@ -115,45 +116,45 @@ TEST(PreferencesRepository, LoadFromBackup) { EXPECT_TRUE(result.has_value()); EXPECT_EQ(result.value()["key1"], "value1"); EXPECT_EQ(result.value()["key2"], "value2"); - std::filesystem::remove(full_name); - EXPECT_FALSE(std::filesystem::exists(full_name_backup)); + std::filesystem::remove(full_name.GetPath()); + EXPECT_FALSE(std::filesystem::exists(full_name_backup.GetPath())); } TEST(PreferencesRepository, RecoverFromCorruption) { - std::optional app_data_path = + std::optional app_data_path = api::ImplementationPlatform::CreateDeviceInfo()->GetLocalAppDataPath(); ASSERT_TRUE(app_data_path.has_value()); - std::filesystem::path full_path = *app_data_path / kPreferencesPath; - std::filesystem::path full_name = full_path / kPreferencesFileName; - std::filesystem::path full_name_backup = - full_path / kPreferencesBackupFileName; + FilePath full_path = app_data_path->append(FilePath(kPreferencesPath)); + FilePath full_name = app_data_path->append(FilePath(kPreferencesFileName)); + FilePath full_name_backup = full_path; + full_name_backup.append(FilePath(kPreferencesBackupFileName)); - if (std::filesystem::exists(full_name)) { - std::filesystem::remove(full_name); + if (std::filesystem::exists(full_name.GetPath())) { + std::filesystem::remove(full_name.GetPath()); } - if (std::filesystem::exists(full_name_backup)) { - std::filesystem::remove(full_name_backup); + if (std::filesystem::exists(full_name_backup.GetPath())) { + std::filesystem::remove(full_name_backup.GetPath()); } - PreferencesRepository preferences_repository{full_path.string()}; + PreferencesRepository preferences_repository{full_path.ToString()}; json data; data["key1"] = "value1"; data["key2"] = "value2"; - std::ofstream preferences_file(full_name_backup.c_str()); + std::ofstream preferences_file(full_name_backup.GetPath()); preferences_file << data; preferences_file.close(); - std::ofstream backup_file(full_name.c_str()); + std::ofstream backup_file(full_name.GetPath()); backup_file << "[BAD JSON FILE]"; backup_file.close(); std::optional result = preferences_repository.LoadPreferences(); EXPECT_EQ(result.value()["key1"], "value1"); EXPECT_EQ(result.value()["key2"], "value2"); - std::filesystem::remove(full_name); - EXPECT_FALSE(std::filesystem::exists(full_name_backup)); + std::filesystem::remove(full_name.GetPath()); + EXPECT_FALSE(std::filesystem::exists(full_name_backup.GetPath())); } } // namespace diff --git a/internal/test/BUILD b/internal/test/BUILD index 97ae5eb4..f106c81a 100644 --- a/internal/test/BUILD +++ b/internal/test/BUILD @@ -57,6 +57,8 @@ cc_library( visibility = ["//visibility:public"], deps = [ "//internal/base", + "//internal/base:file_path", + "//internal/base:files", "//internal/network:types", "//internal/platform:comm", "//internal/platform:types", @@ -92,6 +94,8 @@ cc_test( shard_count = 8, deps = [ ":test", + "//internal/base:file_path", + "//internal/base:files", "//internal/network:types", "//internal/network:url", "//internal/platform:types", diff --git a/internal/test/fake_device_info.h b/internal/test/fake_device_info.h index b7349a29..34630a89 100644 --- a/internal/test/fake_device_info.h +++ b/internal/test/fake_device_info.h @@ -16,7 +16,7 @@ #define THIRD_PARTY_NEARBY_INTERNAL_TEST_FAKE_DEVICE_INFO_H_ #include -#include // NOLINT +#include #include #include #include @@ -25,6 +25,8 @@ #include "absl/container/flat_hash_map.h" #include "absl/strings/string_view.h" +#include "internal/base/file_path.h" +#include "internal/base/files.h" #include "internal/platform/device_info.h" #include "internal/platform/implementation/device_info.h" @@ -40,21 +42,21 @@ class FakeDeviceInfo : public DeviceInfo { api::DeviceInfo::OsType GetOsType() const override { return os_type_; } - std::filesystem::path GetDownloadPath() const override { + FilePath GetDownloadPath() const override { return download_path_; } - std::filesystem::path GetAppDataPath() const override { + FilePath GetAppDataPath() const override { return app_data_path_; } - std::filesystem::path GetTemporaryPath() const override { return temp_path_; } + FilePath GetTemporaryPath() const override { return temp_path_; } - std::filesystem::path GetLogPath() const override { return temp_path_; } + FilePath GetLogPath() const override { return temp_path_; } std::optional GetAvailableDiskSpaceInBytes( - const std::filesystem::path& path) const override { - std::wstring path_key = path.wstring(); + const FilePath& path) const override { + std::wstring path_key = path.ToWideString(); auto it = available_space_map_.find(path_key); if (it != available_space_map_.end()) { return it->second; @@ -92,15 +94,15 @@ class FakeDeviceInfo : public DeviceInfo { void SetOsType(api::DeviceInfo::OsType os_type) { os_type_ = os_type; } - void SetDownloadPath(std::filesystem::path path) { download_path_ = path; } + void SetDownloadPath(FilePath path) { download_path_ = path; } - void SetAppDataPath(std::filesystem::path path) { app_data_path_ = path; } + void SetAppDataPath(FilePath path) { app_data_path_ = path; } - void SetTemporaryPath(std::filesystem::path path) { temp_path_ = path; } + void SetTemporaryPath(FilePath path) { temp_path_ = path; } - void SetAvailableDiskSpaceInBytes(const std::filesystem::path& path, + void SetAvailableDiskSpaceInBytes(const FilePath& path, size_t available_bytes) { - available_space_map_.emplace(path.wstring(), available_bytes); + available_space_map_.emplace(path.ToWideString(), available_bytes); } void ResetDiskSpace() { available_space_map_.clear(); } @@ -121,9 +123,12 @@ class FakeDeviceInfo : public DeviceInfo { api::DeviceInfo::DeviceType device_type_ = api::DeviceInfo::DeviceType::kLaptop; api::DeviceInfo::OsType os_type_ = api::DeviceInfo::OsType::kWindows; - std::filesystem::path download_path_ = std::filesystem::temp_directory_path(); - std::filesystem::path app_data_path_ = std::filesystem::temp_directory_path(); - std::filesystem::path temp_path_ = std::filesystem::temp_directory_path(); + FilePath download_path_ = nearby::sharing::GetTemporaryDirectory().value_or( + nearby::sharing::CurrentDirectory()); + FilePath app_data_path_ = nearby::sharing::GetTemporaryDirectory().value_or( + nearby::sharing::CurrentDirectory()); + FilePath temp_path_ = nearby::sharing::GetTemporaryDirectory().value_or( + nearby::sharing::CurrentDirectory()); absl::flat_hash_map available_space_map_; absl::flat_hash_map> diff --git a/internal/test/fake_device_info_test.cc b/internal/test/fake_device_info_test.cc index 7c575c52..47b7f51d 100644 --- a/internal/test/fake_device_info_test.cc +++ b/internal/test/fake_device_info_test.cc @@ -14,17 +14,23 @@ #include "internal/test/fake_device_info.h" -#include -#include #include #include #include "gtest/gtest.h" +#include "internal/base/file_path.h" +#include "internal/base/files.h" #include "internal/platform/implementation/device_info.h" namespace nearby { namespace { +FilePath GetTempDir() { + std::optional temp_dir = nearby::sharing::GetTemporaryDirectory(); + EXPECT_TRUE(temp_dir.has_value()); + return temp_dir.value(); +} + TEST(FakeDeviceInfo, DeviceName) { FakeDeviceInfo device_info; device_info.SetOsDeviceName("windows"); @@ -45,36 +51,33 @@ TEST(FakeDeviceInfo, OsType) { TEST(FakeDeviceInfo, GetDownloadPath) { FakeDeviceInfo device_info; + EXPECT_EQ(device_info.GetDownloadPath(), GetTempDir()); + device_info.SetDownloadPath(GetTempDir().append(FilePath("test"))); EXPECT_EQ(device_info.GetDownloadPath(), - std::filesystem::temp_directory_path()); - device_info.SetDownloadPath(std::filesystem::temp_directory_path() / "test"); - EXPECT_EQ(device_info.GetDownloadPath(), - std::filesystem::temp_directory_path() / "test"); + GetTempDir().append(FilePath("test"))); } TEST(FakeDeviceInfo, GetAppDataPath) { FakeDeviceInfo device_info; + EXPECT_EQ(device_info.GetAppDataPath(), GetTempDir()); + device_info.SetAppDataPath(GetTempDir().append(FilePath("test"))); EXPECT_EQ(device_info.GetAppDataPath(), - std::filesystem::temp_directory_path()); - device_info.SetAppDataPath(std::filesystem::temp_directory_path() / "test"); - EXPECT_EQ(device_info.GetAppDataPath(), - std::filesystem::temp_directory_path() / "test"); + GetTempDir().append(FilePath("test"))); } TEST(FakeDeviceInfo, GetTemporaryPath) { FakeDeviceInfo device_info; + EXPECT_EQ(device_info.GetTemporaryPath(), GetTempDir()); + device_info.SetTemporaryPath(GetTempDir().append(FilePath("test"))); EXPECT_EQ(device_info.GetTemporaryPath(), - std::filesystem::temp_directory_path()); - device_info.SetTemporaryPath(std::filesystem::temp_directory_path() / "test"); - EXPECT_EQ(device_info.GetTemporaryPath(), - std::filesystem::temp_directory_path() / "test"); + GetTempDir().append(FilePath("test"))); } TEST(FakeDeviceInfo, GetAvailableDiskSpaceInBytes) { FakeDeviceInfo device_info; - device_info.SetDownloadPath("download"); - device_info.SetAppDataPath("appdata"); - device_info.SetTemporaryPath("temp"); + device_info.SetDownloadPath(FilePath("download")); + device_info.SetAppDataPath(FilePath("appdata")); + device_info.SetTemporaryPath(FilePath("temp")); device_info.SetAvailableDiskSpaceInBytes(device_info.GetDownloadPath(), 10); device_info.SetAvailableDiskSpaceInBytes(device_info.GetAppDataPath(), 100); diff --git a/sharing/BUILD b/sharing/BUILD index 6cfacea4..a87e8c3c 100644 --- a/sharing/BUILD +++ b/sharing/BUILD @@ -316,6 +316,7 @@ cc_library( "//internal/analytics:event_logger", "//internal/base", "//internal/base:bluetooth_address", + "//internal/base:file_path", "//internal/flags:nearby_flags", "//internal/network:url", "//internal/platform:base", @@ -509,6 +510,8 @@ cc_test( ":incoming_frame_reader", ":nearby_connection_impl", ":test_support", + "//internal/base:file_path", + "//internal/base:files", "//internal/platform/implementation/g3", # fixdeps: keep "//internal/test", "//sharing/proto:wire_format_cc_proto", @@ -564,6 +567,8 @@ cc_test( ":types", "//base:casts", "//internal/analytics:mock_event_logger", + "//internal/base:file_path", + "//internal/base:files", "//internal/flags:nearby_flags", "//internal/platform/implementation:signin_attempt", "//internal/platform/implementation/g3", # fixdeps: keep @@ -573,7 +578,6 @@ cc_test( "//sharing/certificates", "//sharing/certificates:test_support", "//sharing/common", - "//sharing/common:compatible_u8_string", "//sharing/common:enum", "//sharing/contacts", "//sharing/contacts:test_support", @@ -599,7 +603,6 @@ cc_test( "@com_google_absl//absl/time", "@com_google_absl//absl/types:span", "@com_google_googletest//:gtest_main", - "@com_google_protobuf//:protobuf_lite", ], ) diff --git a/sharing/nearby_sharing_service_impl.cc b/sharing/nearby_sharing_service_impl.cc index f4d2f48a..ff4c86f7 100644 --- a/sharing/nearby_sharing_service_impl.cc +++ b/sharing/nearby_sharing_service_impl.cc @@ -44,6 +44,7 @@ #include "absl/time/time.h" #include "absl/types/span.h" #include "internal/base/bluetooth_address.h" +#include "internal/base/file_path.h" #include "internal/base/observer_list.h" #include "internal/flags/nearby_flags.h" #include "internal/network/url.h" @@ -246,13 +247,12 @@ NearbySharingServiceImpl::NearbySharingServiceImpl( CHECK(analytics_recorder); is_shutting_down_ = std::make_unique(false); - std::filesystem::path path = device_info_.GetAppDataPath(); + FilePath full_database_path = + device_info_.GetAppDataPath().append(FilePath(kProfileRelativePath)); - std::filesystem::path full_database_path = - path / std::string(kProfileRelativePath); certificate_manager_ = NearbyShareCertificateManagerImpl::Factory::Create( context_, sharing_platform, local_device_data_manager_.get(), - contact_manager_.get(), full_database_path.string(), + contact_manager_.get(), full_database_path.ToString(), nearby_share_client_factory_.get()), certificate_manager_->AddObserver(this); diff --git a/sharing/nearby_sharing_service_impl_test.cc b/sharing/nearby_sharing_service_impl_test.cc index 22738e05..90253268 100644 --- a/sharing/nearby_sharing_service_impl_test.cc +++ b/sharing/nearby_sharing_service_impl_test.cc @@ -45,6 +45,8 @@ #include "absl/time/time.h" #include "absl/types/span.h" #include "internal/analytics/mock_event_logger.h" +#include "internal/base/file_path.h" +#include "internal/base/files.h" #include "internal/flags/nearby_flags.h" #include "internal/platform/implementation/signin_attempt.h" #include "internal/test/fake_account_manager.h" @@ -58,7 +60,6 @@ #include "sharing/certificates/nearby_share_certificate_manager_impl.h" #include "sharing/certificates/nearby_share_decrypted_public_certificate.h" #include "sharing/certificates/test_util.h" -#include "sharing/common/compatible_u8_string.h" #include "sharing/common/nearby_share_enums.h" #include "sharing/common/nearby_share_prefs.h" #include "sharing/constants.h" @@ -128,6 +129,12 @@ using ::testing::UnorderedElementsAre; constexpr absl::Duration kWaitTimeout = absl::Milliseconds(500); constexpr absl::Duration kTaskWaitTimeout = absl::Seconds(2); +FilePath GetTempDir() { + std::optional temp_dir = nearby::sharing::GetTemporaryDirectory(); + EXPECT_TRUE(temp_dir.has_value()); + return temp_dir.value(); +} + class MockTransferUpdateCallback : public TransferUpdateCallback { public: ~MockTransferUpdateCallback() override = default; @@ -1234,8 +1241,7 @@ class NearbySharingServiceImplTest : public testing::Test { } void SetDiskSpace(size_t size) { - fake_device_info_.SetAvailableDiskSpaceInBytes( - std::filesystem::temp_directory_path(), size); + fake_device_info_.SetAvailableDiskSpaceInBytes(GetTempDir(), size); } void ResetDiskSpace() { fake_device_info_.ResetDiskSpace(); } @@ -2669,7 +2675,7 @@ TEST_F(NearbySharingServiceImplTest, IncomingConnectionOutOfStorage) { SetDiskSpace(kFreeDiskSpace); preference_manager().SetString( prefs::kNearbySharingCustomSavePath, - GetCompatibleU8String(fake_device_info_.GetDownloadPath().u8string())); + fake_device_info_.GetDownloadPath().ToString()); fake_nearby_connections_manager_->SetRawAuthenticationToken(kEndpointId, GetToken()); diff --git a/sharing/nearby_sharing_settings.cc b/sharing/nearby_sharing_settings.cc index 7525553c..6e72a9e5 100644 --- a/sharing/nearby_sharing_settings.cc +++ b/sharing/nearby_sharing_settings.cc @@ -185,7 +185,7 @@ void NearbyShareSettings::RestoreFallbackVisibility() { std::string NearbyShareSettings::GetCustomSavePath() const { return preference_manager_.GetString( prefs::kNearbySharingCustomSavePath, - GetCompatibleU8String(device_info_.GetDownloadPath().u8string())); + device_info_.GetDownloadPath().ToString()); } bool NearbyShareSettings::IsDisabledByPolicy() const { return false; } diff --git a/sharing/nearby_sharing_util.cc b/sharing/nearby_sharing_util.cc index 550370f6..f08595fb 100644 --- a/sharing/nearby_sharing_util.cc +++ b/sharing/nearby_sharing_util.cc @@ -26,6 +26,7 @@ #include "absl/hash/hash.h" #include "absl/strings/str_cat.h" #include "absl/strings/string_view.h" +#include "internal/base/file_path.h" #include "internal/platform/device_info.h" #include "proto/sharing_enums.pb.h" #include "sharing/advertisement.h" @@ -140,7 +141,7 @@ std::string GetDeviceId( bool IsOutOfStorage(DeviceInfo& device_info, std::filesystem::path file_path, int64_t storage_required) { std::optional available_storage = - device_info.GetAvailableDiskSpaceInBytes(file_path); + device_info.GetAvailableDiskSpaceInBytes(FilePath::FromPath(file_path)); if (!available_storage.has_value()) { return false;