Create nearby::FilePath class to replace use of std::filesystem::path.

- Replace filesystem::path in DeviceInfo.

PiperOrigin-RevId: 761224912
This commit is contained in:
Francis Tsui
2025-05-20 14:12:07 -07:00
committed by Copybara-Service
parent 502bb5d9c8
commit 151ba03d0f
34 changed files with 509 additions and 196 deletions
+1
View File
@@ -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",
+4
View File
@@ -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.
+35 -1
View File
@@ -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",
+32
View File
@@ -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 <string>
namespace nearby {
#if defined(__cpp_lib_char8_t)
inline std::string GetCompatibleU8String(std::u8string str) {
return reinterpret_cast<const char*>(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_
+48
View File
@@ -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 <filesystem> // NOLINT
#include <string>
#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
+68
View File
@@ -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 <filesystem> // NOLINT
#include <string>
#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_
+92
View File
@@ -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
+5 -4
View File
@@ -19,6 +19,7 @@
#include <optional>
#include <system_error> // 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<std::filesystem::path> GetTemporaryDirectory() {
std::optional<FilePath> 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,
+4 -2
View File
@@ -19,6 +19,8 @@
#include <filesystem> // NOLINT(build/c++17)
#include <optional>
#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<std::filesystem::path> GetTemporaryDirectory();
std::optional<FilePath> 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.
+1
View File
@@ -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",
+6 -6
View File
@@ -16,12 +16,12 @@
#define PLATFORM_PUBLIC_DEVICE_INFO_H_
#include <cstddef>
#include <filesystem> // NOLINT
#include <functional>
#include <optional>
#include <string>
#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<size_t> GetAvailableDiskSpaceInBytes(
const std::filesystem::path& path) const = 0;
const FilePath& path) const = 0;
virtual bool IsScreenLocked() const = 0;
virtual void RegisterScreenLockedListener(
+11 -13
View File
@@ -21,6 +21,7 @@
#include <string>
#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<std::filesystem::path> path =
device_info_impl_->GetDownloadPath();
FilePath DeviceInfoImpl::GetDownloadPath() const {
std::optional<FilePath> 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<std::filesystem::path> path =
device_info_impl_->GetLocalAppDataPath();
FilePath DeviceInfoImpl::GetAppDataPath() const {
std::optional<FilePath> 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<std::filesystem::path> path =
device_info_impl_->GetTemporaryPath();
FilePath DeviceInfoImpl::GetTemporaryPath() const {
std::optional<FilePath> 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<std::filesystem::path> path = device_info_impl_->GetLogPath();
FilePath DeviceInfoImpl::GetLogPath() const {
std::optional<FilePath> path = device_info_impl_->GetLogPath();
return path.value_or(GetTemporaryPath());
}
std::optional<size_t> 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;
}
+6 -6
View File
@@ -16,13 +16,13 @@
#define PLATFORM_PUBLIC_DEVICE_INFO_IMPL_H_
#include <cstddef>
#include <filesystem> // NOLINT
#include <functional>
#include <memory>
#include <optional>
#include <string>
#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<size_t> GetAvailableDiskSpaceInBytes(
const std::filesystem::path& path) const override;
const FilePath& path) const override;
bool IsScreenLocked() const override;
void RegisterScreenLockedListener(
+1
View File
@@ -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
@@ -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",
@@ -15,12 +15,12 @@
#ifndef PLATFORM_IMPL_APPLE_DEVICE_INFO_H_
#define PLATFORM_IMPL_APPLE_DEVICE_INFO_H_
#include <filesystem> // NOLINT(build/c++17)
#include <functional>
#include <optional>
#include <string>
#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<std::filesystem::path> GetDownloadPath() const override;
std::optional<FilePath> GetDownloadPath() const override;
std::optional<std::filesystem::path> GetLocalAppDataPath() const override;
std::optional<FilePath> GetLocalAppDataPath() const override;
std::optional<std::filesystem::path> GetCommonAppDataPath() const override;
std::optional<FilePath> GetCommonAppDataPath() const override;
std::optional<std::filesystem::path> GetTemporaryPath() const override;
std::optional<FilePath> GetTemporaryPath() const override;
std::optional<std::filesystem::path> GetLogPath() const override;
std::optional<FilePath> GetLogPath() const override;
std::optional<std::filesystem::path> GetCrashDumpPath() const override;
std::optional<FilePath> GetCrashDumpPath() const override;
bool IsScreenLocked() const override;
@@ -19,13 +19,13 @@
#import <UIKit/UIKit.h>
#endif
#include <filesystem> // NOLINT(build/c++17)
#include <functional>
#include <optional>
#include <string>
#include <utility>
#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<std::filesystem::path> DeviceInfo::GetDownloadPath() const {
std::optional<FilePath> DeviceInfo::GetDownloadPath() const {
NSFileManager *manager = [NSFileManager defaultManager];
NSError *error = nil;
@@ -92,10 +92,10 @@ std::optional<std::filesystem::path> DeviceInfo::GetDownloadPath() const {
return std::nullopt;
}
return std::filesystem::path([downloadsURL.path cString]);
return FilePath(absl::string_view([downloadsURL.path cString]));
}
std::optional<std::filesystem::path> DeviceInfo::GetLocalAppDataPath() const {
std::optional<FilePath> DeviceInfo::GetLocalAppDataPath() const {
NSFileManager *manager = [NSFileManager defaultManager];
NSError *error = nil;
@@ -109,18 +109,18 @@ std::optional<std::filesystem::path> DeviceInfo::GetLocalAppDataPath() const {
return std::nullopt;
}
return std::filesystem::path([applicationSupportURL.path cString]);
return FilePath(absl::string_view([applicationSupportURL.path cString]));
}
std::optional<std::filesystem::path> DeviceInfo::GetCommonAppDataPath() const {
std::optional<FilePath> DeviceInfo::GetCommonAppDataPath() const {
return GetLocalAppDataPath();
}
std::optional<std::filesystem::path> DeviceInfo::GetTemporaryPath() const {
return std::filesystem::path([NSTemporaryDirectory() cString]);
std::optional<FilePath> DeviceInfo::GetTemporaryPath() const {
return FilePath(absl::string_view([NSTemporaryDirectory() cString]));
}
std::optional<std::filesystem::path> DeviceInfo::GetLogPath() const {
std::optional<FilePath> DeviceInfo::GetLogPath() const {
NSFileManager *manager = [NSFileManager defaultManager];
NSError *error = nil;
@@ -139,10 +139,10 @@ std::optional<std::filesystem::path> 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<std::filesystem::path> DeviceInfo::GetCrashDumpPath() const {
std::optional<FilePath> DeviceInfo::GetCrashDumpPath() const {
NSFileManager *manager = [NSFileManager defaultManager];
NSError *error = nil;
@@ -161,7 +161,7 @@ std::optional<std::filesystem::path> 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; }
@@ -15,12 +15,12 @@
#ifndef PLATFORM_API_DEVICE_INFO_H_
#define PLATFORM_API_DEVICE_INFO_H_
#include <filesystem> // NOLINT
#include <functional>
#include <optional>
#include <string>
#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<std::filesystem::path> GetDownloadPath() const = 0;
virtual std::optional<std::filesystem::path> GetLocalAppDataPath() const = 0;
virtual std::optional<std::filesystem::path> GetCommonAppDataPath() const = 0;
virtual std::optional<std::filesystem::path> GetTemporaryPath() const = 0;
virtual std::optional<std::filesystem::path> GetLogPath() const = 0;
virtual std::optional<std::filesystem::path> GetCrashDumpPath() const = 0;
virtual std::optional<FilePath> GetDownloadPath() const = 0;
virtual std::optional<FilePath> GetLocalAppDataPath() const = 0;
virtual std::optional<FilePath> GetCommonAppDataPath() const = 0;
virtual std::optional<FilePath> GetTemporaryPath() const = 0;
virtual std::optional<FilePath> GetLogPath() const = 0;
virtual std::optional<FilePath> GetCrashDumpPath() const = 0;
// Monitor screen status
virtual bool IsScreenLocked() const = 0;
@@ -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",
@@ -16,7 +16,6 @@
#define PLATFORM_IMPL_G3_DEVICE_INFO_H_
#include <cstdlib>
#include <filesystem> // NOLINT
#include <functional>
#include <optional>
#include <string>
@@ -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<std::filesystem::path> GetDownloadPath() const override {
return std::filesystem::temp_directory_path();
std::optional<FilePath> GetDownloadPath() const override {
return nearby::sharing::GetTemporaryDirectory();
}
std::optional<std::filesystem::path> GetLocalAppDataPath() const override {
std::optional<FilePath> 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<std::filesystem::path> GetCommonAppDataPath() const override {
return std::filesystem::temp_directory_path();
std::optional<FilePath> GetCommonAppDataPath() const override {
return nearby::sharing::GetTemporaryDirectory();
}
std::optional<std::filesystem::path> GetTemporaryPath() const override {
return std::filesystem::temp_directory_path();
std::optional<FilePath> GetTemporaryPath() const override {
return nearby::sharing::GetTemporaryDirectory();
}
std::optional<std::filesystem::path> GetLogPath() const override {
return std::filesystem::temp_directory_path();
std::optional<FilePath> GetLogPath() const override {
return nearby::sharing::GetTemporaryDirectory();
}
std::optional<std::filesystem::path> GetCrashDumpPath() const override {
return std::filesystem::temp_directory_path();
std::optional<FilePath> GetCrashDumpPath() const override {
return nearby::sharing::GetTemporaryDirectory();
}
bool IsScreenLocked() const override { return false; }
@@ -14,18 +14,23 @@
#include "internal/platform/implementation/g3/preferences_manager.h"
#include <filesystem> // NOLINT(build/c++17)
#include <cstdint>
#include <memory>
#include <optional>
#include <ostream>
#include <string>
#include <vector>
#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<g3::DeviceInfo>();
std::optional<std::filesystem::path> path =
device_info->GetLocalAppDataPath();
if (!path.has_value()) {
path = std::filesystem::temp_directory_path();
}
auto device_info = std::make_unique<DeviceInfoImpl>();
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<PreferencesRepository>(full_path.string());
std::make_unique<PreferencesRepository>(path.ToString());
value_ = preferences_repository_->LoadPreferences();
}
@@ -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",
@@ -18,7 +18,6 @@
#include <windows.h>
#include <wtsapi32.h>
#include <filesystem> // NOLINT
#include <functional>
#include <optional>
#include <string>
@@ -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<T>;
template <typename T>
using IAsyncOperation = winrt::Windows::Foundation::IAsyncOperation<T>;
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<std::string> DeviceInfo::GetOsDeviceName() const {
@@ -88,65 +88,64 @@ api::DeviceInfo::OsType DeviceInfo::GetOsType() const {
return api::DeviceInfo::OsType::kWindows;
}
std::optional<std::filesystem::path> DeviceInfo::GetDownloadPath() const {
std::optional<FilePath> 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<std::filesystem::path> DeviceInfo::GetLocalAppDataPath() const {
std::optional<FilePath> 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<std::filesystem::path> DeviceInfo::GetCommonAppDataPath() const {
std::optional<FilePath> 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<std::filesystem::path> DeviceInfo::GetTemporaryPath() const {
std::optional<FilePath> DeviceInfo::GetTemporaryPath() const {
return nearby::sharing::GetTemporaryDirectory();
}
std::optional<std::filesystem::path> DeviceInfo::GetLogPath() const {
auto prefix_path = GetLocalAppDataPath();
std::optional<FilePath> DeviceInfo::GetLogPath() const {
std::optional<FilePath> 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<std::filesystem::path> DeviceInfo::GetCrashDumpPath() const {
auto prefix_path = GetLocalAppDataPath();
std::optional<FilePath> DeviceInfo::GetCrashDumpPath() const {
std::optional<FilePath> 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;
}
@@ -15,7 +15,6 @@
#ifndef PLATFORM_IMPL_WINDOWS_DEVICE_INFO_H_
#define PLATFORM_IMPL_WINDOWS_DEVICE_INFO_H_
#include <filesystem> // NOLINT
#include <functional>
#include <optional>
#include <string>
@@ -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<std::filesystem::path> GetDownloadPath() const override;
std::optional<std::filesystem::path> GetLocalAppDataPath() const override;
std::optional<std::filesystem::path> GetCommonAppDataPath() const override;
std::optional<std::filesystem::path> GetTemporaryPath() const override;
std::optional<std::filesystem::path> GetLogPath() const override;
std::optional<std::filesystem::path> GetCrashDumpPath() const override;
std::optional<FilePath> GetDownloadPath() const override;
std::optional<FilePath> GetLocalAppDataPath() const override;
std::optional<FilePath> GetCommonAppDataPath() const override;
std::optional<FilePath> GetTemporaryPath() const override;
std::optional<FilePath> GetLogPath() const override;
std::optional<FilePath> GetCrashDumpPath() const override;
bool IsScreenLocked() const override;
void RegisterScreenLockedListener(
@@ -15,7 +15,6 @@
#include "internal/platform/implementation/windows/preferences_manager.h"
#include <cstdint>
#include <filesystem> // NOLINT(build/c++17)
#include <memory>
#include <optional>
#include <ostream>
@@ -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<std::filesystem::path> path =
std::optional<FilePath> 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<PreferencesRepository>(full_path.string());
std::make_unique<PreferencesRepository>(path->ToString());
value_ = preferences_repository_->LoadPreferences();
}
@@ -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<std::filesystem::path> app_data_path =
std::optional<FilePath> 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<std::filesystem::path> app_data_path =
std::optional<FilePath> 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<std::filesystem::path> app_data_path =
std::optional<FilePath> 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<std::filesystem::path> app_data_path =
std::optional<FilePath> 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<json> 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
+4
View File
@@ -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",
+20 -15
View File
@@ -16,7 +16,7 @@
#define THIRD_PARTY_NEARBY_INTERNAL_TEST_FAKE_DEVICE_INFO_H_
#include <cstddef>
#include <filesystem> // NOLINT
#include <cstdint>
#include <functional>
#include <limits>
#include <optional>
@@ -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<size_t> 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<std::wstring, size_t> available_space_map_;
absl::flat_hash_map<std::string,
std::function<void(api::DeviceInfo::ScreenStatus)>>
+20 -17
View File
@@ -14,17 +14,23 @@
#include "internal/test/fake_device_info.h"
#include <array>
#include <filesystem>
#include <functional>
#include <optional>
#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<FilePath> 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);
+5 -2
View File
@@ -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",
],
)
+4 -4
View File
@@ -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<bool>(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);
+10 -4
View File
@@ -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<FilePath> 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());
+1 -1
View File
@@ -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; }
+2 -1
View File
@@ -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<size_t> available_storage =
device_info.GetAvailableDiskSpaceInBytes(file_path);
device_info.GetAvailableDiskSpaceInBytes(FilePath::FromPath(file_path));
if (!available_storage.has_value()) {
return false;