diff --git a/connections/implementation/client_proxy.cc b/connections/implementation/client_proxy.cc index 760f358c..a1a5ce88 100644 --- a/connections/implementation/client_proxy.cc +++ b/connections/implementation/client_proxy.cc @@ -61,7 +61,7 @@ #include "internal/platform/cancelable_alarm.h" #include "internal/platform/cancellation_flag.h" #ifndef NEARBY_CHROMIUM -#include "internal/platform/device_info_impl.h" +#include "internal/platform/implementation/device_info.h" #endif #include "internal/platform/error_code_params.h" #include "internal/platform/error_code_recorder.h" @@ -1340,10 +1340,11 @@ void ClientProxy::InitializePreferencesManager() { void ClientProxy::InitializePreferencesManager() { LOG(INFO) << "ClientProxy [InitializePreferencesManager]: client=" << GetClientId(); - auto device_info_ = std::make_unique(); + std::unique_ptr device_info_ = + nearby::api::ImplementationPlatform::CreateDeviceInfo(); FilePath preferences_path = - device_info_->GetAppDataPath().append(FilePath(kPreferencesFilePath)); + device_info_->GetLocalAppDataPath(FilePath(kPreferencesFilePath)); if (!Files::FileExists(preferences_path)) { Files::CreateDirectories(preferences_path); diff --git a/internal/platform/BUILD b/internal/platform/BUILD index fd82b22d..77fc1061 100644 --- a/internal/platform/BUILD +++ b/internal/platform/BUILD @@ -210,7 +210,6 @@ cc_library( srcs = [ "blocking_queue_stream.cc", "clock_impl.cc", - "device_info_impl.cc", "monitored_runnable.cc", "pending_job_registry.cc", "pipe.cc", @@ -231,8 +230,6 @@ cc_library( "condition_variable.h", "count_down_latch.h", "crypto.h", - "device_info.h", - "device_info_impl.h", "direct_executor.h", "file.h", "future.h", diff --git a/internal/platform/device_info.h b/internal/platform/device_info.h deleted file mode 100644 index 159d2d74..00000000 --- a/internal/platform/device_info.h +++ /dev/null @@ -1,74 +0,0 @@ -// Copyright 2021 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 PLATFORM_PUBLIC_DEVICE_INFO_H_ -#define PLATFORM_PUBLIC_DEVICE_INFO_H_ - -#include -#include -#include -#include - -#include "absl/strings/string_view.h" -#include "internal/base/file_path.h" -#include "internal/platform/implementation/device_info.h" - -namespace nearby { - -class DeviceInfo { - public: - virtual ~DeviceInfo() = default; - - // All strings are UTF-8 encoded. - virtual std::string GetOsDeviceName() const = 0; - virtual api::DeviceInfo::DeviceType GetDeviceType() const = 0; - virtual api::DeviceInfo::OsType GetOsType() 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 FilePath& path) const = 0; - - virtual bool IsScreenLocked() const = 0; - virtual void RegisterScreenLockedListener( - absl::string_view listener_name, - std::function callback) = 0; - virtual void UnregisterScreenLockedListener( - absl::string_view listener_name) = 0; - - virtual bool PreventSleep() = 0; - virtual bool AllowSleep() = 0; - - // Returns UTF-8 encoded localized device name depending on device type. - std::string GetDeviceTypeName() const { - // TODO(b/230132370): return localized device name. - switch (GetDeviceType()) { - case api::DeviceInfo::DeviceType::kPhone: - return "Phone"; - case api::DeviceInfo::DeviceType::kTablet: - return "Tablet"; - case api::DeviceInfo::DeviceType::kLaptop: - return "PC"; - default: - return "Unknown"; - } - } -}; - -} // namespace nearby - -#endif // PLATFORM_PUBLIC_DEVICE_INFO_H_ diff --git a/internal/platform/device_info_impl.cc b/internal/platform/device_info_impl.cc deleted file mode 100644 index 91c04021..00000000 --- a/internal/platform/device_info_impl.cc +++ /dev/null @@ -1,88 +0,0 @@ -// Copyright 2021 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/platform/device_info_impl.h" - -#include -#include -#include -#include - -#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 { - -std::string DeviceInfoImpl::GetOsDeviceName() const { - std::optional device_name = device_info_impl_->GetOsDeviceName(); - if (device_name.has_value()) { - return *device_name; - } - - return "unknown"; -} - -api::DeviceInfo::DeviceType DeviceInfoImpl::GetDeviceType() const { - return device_info_impl_->GetDeviceType(); -} - -api::DeviceInfo::OsType DeviceInfoImpl::GetOsType() const { - return device_info_impl_->GetOsType(); -} - -FilePath DeviceInfoImpl::GetDownloadPath() const { - return device_info_impl_->GetDownloadPath(); -} - -FilePath DeviceInfoImpl::GetAppDataPath() const { - return device_info_impl_->GetLocalAppDataPath(FilePath()); -} - -FilePath DeviceInfoImpl::GetTemporaryPath() const { - return device_info_impl_->GetTemporaryPath(); -} - -FilePath DeviceInfoImpl::GetLogPath() const { - return device_info_impl_->GetLogPath(); -} - -std::optional DeviceInfoImpl::GetAvailableDiskSpaceInBytes( - const FilePath& path) const { - return Files::GetAvailableDiskSpaceInBytes(path); -} - -bool DeviceInfoImpl::IsScreenLocked() const { - return device_info_impl_->IsScreenLocked(); -} - -void DeviceInfoImpl::RegisterScreenLockedListener( - absl::string_view listener_name, - std::function callback) { - device_info_impl_->RegisterScreenLockedListener(listener_name, callback); -} - -void DeviceInfoImpl::UnregisterScreenLockedListener( - absl::string_view listener_name) { - device_info_impl_->UnregisterScreenLockedListener(listener_name); -} - -bool DeviceInfoImpl::PreventSleep() { - return device_info_impl_->PreventSleep(); -} - -bool DeviceInfoImpl::AllowSleep() { return device_info_impl_->AllowSleep(); } - -} // namespace nearby diff --git a/internal/platform/device_info_impl.h b/internal/platform/device_info_impl.h deleted file mode 100644 index fc5ba6fe..00000000 --- a/internal/platform/device_info_impl.h +++ /dev/null @@ -1,63 +0,0 @@ -// Copyright 2021 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 PLATFORM_PUBLIC_DEVICE_INFO_IMPL_H_ -#define PLATFORM_PUBLIC_DEVICE_INFO_IMPL_H_ - -#include -#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" - -namespace nearby { - -class DeviceInfoImpl : public DeviceInfo { - public: - DeviceInfoImpl() - : device_info_impl_(api::ImplementationPlatform::CreateDeviceInfo()) {} - - std::string GetOsDeviceName() const override; - api::DeviceInfo::DeviceType GetDeviceType() const override; - api::DeviceInfo::OsType GetOsType() const override; - - FilePath GetDownloadPath() const override; - FilePath GetAppDataPath() const override; - FilePath GetTemporaryPath() const override; - FilePath GetLogPath() const override; - - std::optional GetAvailableDiskSpaceInBytes( - const FilePath& path) const override; - - bool IsScreenLocked() const override; - void RegisterScreenLockedListener( - absl::string_view listener_name, - std::function callback) override; - void UnregisterScreenLockedListener(absl::string_view listener_name) override; - - bool PreventSleep() override; - bool AllowSleep() override; - - private: - std::unique_ptr device_info_impl_; -}; -} // namespace nearby - -#endif // PLATFORM_PUBLIC_DEVICE_INFO_IMPL_H_ diff --git a/internal/platform/implementation/BUILD b/internal/platform/implementation/BUILD index d6092802..225e78c7 100644 --- a/internal/platform/implementation/BUILD +++ b/internal/platform/implementation/BUILD @@ -54,6 +54,7 @@ cc_library( ], deps = [ "//internal/base:file_path", + "//internal/base:files", "//internal/crypto_cros", "//internal/platform:base", "//internal/platform:mac_address", @@ -151,6 +152,7 @@ cc_library( "//location/nearby/analytics/cpp:__subpackages__", "//location/nearby/apps/better_together/plugins/preferences_native:__subpackages__", "//location/nearby/cpp/sharing:__subpackages__", + "//sharing/internal/impl/common:__subpackages__", ], deps = [ ":comm", diff --git a/internal/platform/implementation/device_info.h b/internal/platform/implementation/device_info.h index 4426ef7d..89d937d6 100644 --- a/internal/platform/implementation/device_info.h +++ b/internal/platform/implementation/device_info.h @@ -15,12 +15,14 @@ #ifndef PLATFORM_API_DEVICE_INFO_H_ #define PLATFORM_API_DEVICE_INFO_H_ +#include #include #include #include #include "absl/strings/string_view.h" #include "internal/base/file_path.h" +#include "internal/base/files.h" namespace nearby { namespace api { @@ -29,6 +31,23 @@ class DeviceInfo { public: enum class ScreenStatus { kUndetermined = 0, kLocked, kUnlocked }; enum class DeviceType { kUnknown = 0, kPhone, kTablet, kLaptop }; + template + void AbslStringify(Sink& sink, DeviceType device_type) { + switch (device_type) { + case DeviceType::kUnknown: + sink.Append("Unknown"); + return; + case DeviceType::kPhone: + sink.Append("Phone"); + return; + case DeviceType::kTablet: + sink.Append("Tablet"); + return; + case DeviceType::kLaptop: + sink.Append("PC"); + return; + } + } enum class OsType { kUnknown = 0, kAndroid, @@ -51,6 +70,11 @@ class DeviceInfo { virtual FilePath GetTemporaryPath() const = 0; virtual FilePath GetLogPath() const = 0; + virtual std::optional GetAvailableDiskSpaceInBytes( + const FilePath& path) const { + return Files::GetAvailableDiskSpaceInBytes(path); + }; + // Monitor screen status virtual bool IsScreenLocked() const = 0; virtual void RegisterScreenLockedListener( diff --git a/internal/test/fake_device_info.h b/internal/test/fake_device_info.h index 22d491da..f79476f1 100644 --- a/internal/test/fake_device_info.h +++ b/internal/test/fake_device_info.h @@ -27,14 +27,15 @@ #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" namespace nearby { -class FakeDeviceInfo : public DeviceInfo { +class FakeDeviceInfo : public api::DeviceInfo { public: - std::string GetOsDeviceName() const override { return device_name_; } + std::optional GetOsDeviceName() const override { + return device_name_; + } api::DeviceInfo::DeviceType GetDeviceType() const override { return device_type_; @@ -46,8 +47,10 @@ class FakeDeviceInfo : public DeviceInfo { return download_path_; } - FilePath GetAppDataPath() const override { - return app_data_path_; + FilePath GetLocalAppDataPath(FilePath sub_path) const override { + FilePath path = app_data_path_; + path.append(sub_path); + return path; } FilePath GetTemporaryPath() const override { return temp_path_; } diff --git a/internal/test/fake_device_info_test.cc b/internal/test/fake_device_info_test.cc index 4e7c936e..5c2e8fb2 100644 --- a/internal/test/fake_device_info_test.cc +++ b/internal/test/fake_device_info_test.cc @@ -51,13 +51,16 @@ TEST(FakeDeviceInfo, GetDownloadPath) { Files::GetTemporaryDirectory().append(FilePath("test"))); } -TEST(FakeDeviceInfo, GetAppDataPath) { +TEST(FakeDeviceInfo, GetLocalAppDataPath) { FakeDeviceInfo device_info; - EXPECT_EQ(device_info.GetAppDataPath(), Files::GetTemporaryDirectory()); + EXPECT_EQ(device_info.GetLocalAppDataPath(FilePath("abc")), + Files::GetTemporaryDirectory().append(FilePath("abc"))); device_info.SetAppDataPath( Files::GetTemporaryDirectory().append(FilePath("test"))); - EXPECT_EQ(device_info.GetAppDataPath(), - Files::GetTemporaryDirectory().append(FilePath("test"))); + EXPECT_EQ(device_info.GetLocalAppDataPath(FilePath("def")), + Files::GetTemporaryDirectory() + .append(FilePath("test")) + .append(FilePath("def"))); } TEST(FakeDeviceInfo, GetTemporaryPath) { @@ -76,7 +79,8 @@ TEST(FakeDeviceInfo, GetAvailableDiskSpaceInBytes) { device_info.SetTemporaryPath(FilePath("temp")); device_info.SetAvailableDiskSpaceInBytes(device_info.GetDownloadPath(), 10); - device_info.SetAvailableDiskSpaceInBytes(device_info.GetAppDataPath(), 100); + device_info.SetAvailableDiskSpaceInBytes( + device_info.GetLocalAppDataPath(FilePath()), 100); device_info.SetAvailableDiskSpaceInBytes(device_info.GetTemporaryPath(), 1000); @@ -84,7 +88,8 @@ TEST(FakeDeviceInfo, GetAvailableDiskSpaceInBytes) { device_info.GetAvailableDiskSpaceInBytes(device_info.GetDownloadPath()), 10); EXPECT_EQ( - device_info.GetAvailableDiskSpaceInBytes(device_info.GetAppDataPath()), + device_info.GetAvailableDiskSpaceInBytes( + device_info.GetLocalAppDataPath(FilePath())), 100); EXPECT_EQ( device_info.GetAvailableDiskSpaceInBytes(device_info.GetTemporaryPath()), diff --git a/sharing/BUILD b/sharing/BUILD index 4de4b542..b5974de2 100644 --- a/sharing/BUILD +++ b/sharing/BUILD @@ -258,12 +258,10 @@ cc_library( srcs = ["nearby_connection_impl.cc"], hdrs = ["nearby_connection_impl.h"], deps = [ - ":connection_types", ":types", - "//internal/platform:types", + "//internal/platform/implementation:types", "//sharing/internal/public:logging", "@com_google_absl//absl/base:core_headers", - "@com_google_absl//absl/strings", "@com_google_absl//absl/synchronization", ], ) @@ -303,8 +301,6 @@ cc_library( hdrs = ["nearby_sharing_util.h"], deps = [ ":types", - "//internal/base:file_path", - "//internal/platform:types", "//proto:sharing_enums_cc_proto", "//sharing/certificates", "//sharing/common:enum", @@ -382,6 +378,7 @@ cc_library( "//internal/analytics:event_logger", "//internal/base", "//internal/base:file_path", + "//internal/base:files", "//internal/flags:nearby_flags", "//internal/network:url", "//internal/platform:base", diff --git a/sharing/internal/api/BUILD b/sharing/internal/api/BUILD index 1a09ee39..4087190d 100644 --- a/sharing/internal/api/BUILD +++ b/sharing/internal/api/BUILD @@ -42,6 +42,7 @@ cc_library( "//internal/base:file_path", "//internal/platform:mac_address", "//internal/platform:types", + "//internal/platform/implementation:types", "//location/nearby/sharing/lib/account:account_manager", "//location/nearby/sharing/lib/sync:sync_binding_prefs_cc_proto", "//sharing/proto:share_cc_proto", @@ -71,16 +72,10 @@ cc_library( "//internal/base:file_path", "//internal/platform:mac_address", "//internal/platform:types", + "//internal/platform/implementation:types", "//location/nearby/sharing/lib/account:account_manager", - "//sharing/analytics", - "//sharing/internal/public:logging", - "//sharing/proto:share_cc_proto", - "@com_google_absl//absl/base:core_headers", "@com_google_absl//absl/functional:any_invocable", - "@com_google_absl//absl/status", - "@com_google_absl//absl/status:statusor", "@com_google_absl//absl/strings", - "@com_google_absl//absl/synchronization", "@com_google_absl//absl/types:span", "@com_google_googletest//:gtest_for_library_testonly", ], diff --git a/sharing/internal/api/mock_sharing_platform.h b/sharing/internal/api/mock_sharing_platform.h index d9e5df2a..34dbe0cf 100644 --- a/sharing/internal/api/mock_sharing_platform.h +++ b/sharing/internal/api/mock_sharing_platform.h @@ -23,7 +23,7 @@ #include "gmock/gmock.h" #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/task_runner.h" #include "sharing/internal/api/app_info.h" #include "sharing/internal/api/bluetooth_adapter.h" @@ -72,7 +72,7 @@ class MockSharingPlatform : public SharingPlatform { MOCK_METHOD(AccountManager&, GetAccountManager, (), (override)); MOCK_METHOD(TaskRunner&, GetDefaultTaskRunner, (), (override)); - MOCK_METHOD(nearby::DeviceInfo&, GetDeviceInfo, (), (override)); + MOCK_METHOD(nearby::api::DeviceInfo&, GetDeviceInfo, (), (override)); MOCK_METHOD(std::unique_ptr, CreatePublicCertificateDatabase, (const FilePath& database_path), (override)); diff --git a/sharing/internal/api/sharing_platform.h b/sharing/internal/api/sharing_platform.h index a65ac38f..c795002e 100644 --- a/sharing/internal/api/sharing_platform.h +++ b/sharing/internal/api/sharing_platform.h @@ -22,7 +22,7 @@ #include "location/nearby/sharing/lib/account/account_manager.h" #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/task_runner.h" #include "sharing/internal/api/app_info.h" #include "sharing/internal/api/bluetooth_adapter.h" @@ -65,7 +65,7 @@ class SharingPlatform { virtual PreferenceManager& GetPreferenceManager() = 0; virtual AccountManager& GetAccountManager() = 0; virtual TaskRunner& GetDefaultTaskRunner() = 0; - virtual nearby::DeviceInfo& GetDeviceInfo() = 0; + virtual nearby::api::DeviceInfo& GetDeviceInfo() = 0; virtual std::unique_ptr CreatePublicCertificateDatabase(const FilePath& database_path) = 0; diff --git a/sharing/local_device_data/BUILD b/sharing/local_device_data/BUILD index 3dbd73cb..22930d93 100644 --- a/sharing/local_device_data/BUILD +++ b/sharing/local_device_data/BUILD @@ -30,7 +30,6 @@ cc_library( visibility = ["//visibility:public"], deps = [ "//internal/base", - "//internal/platform:types", "//internal/platform/implementation:types", "//location/nearby/sharing/lib/account:account_manager", "//sharing/common:enum", diff --git a/sharing/local_device_data/nearby_share_local_device_data_manager_impl.cc b/sharing/local_device_data/nearby_share_local_device_data_manager_impl.cc index ad53f866..e3ab98b2 100644 --- a/sharing/local_device_data/nearby_share_local_device_data_manager_impl.cc +++ b/sharing/local_device_data/nearby_share_local_device_data_manager_impl.cc @@ -23,9 +23,8 @@ #include "location/nearby/sharing/lib/account/account_manager.h" #include "absl/memory/memory.h" +#include "absl/strings/str_cat.h" #include "absl/strings/string_view.h" -#include "absl/strings/substitute.h" -#include "internal/platform/device_info.h" #include "internal/platform/implementation/device_info.h" #include "sharing/common/nearby_share_enums.h" #include "sharing/internal/api/preference_manager.h" @@ -42,8 +41,6 @@ namespace { using ::nearby::api::DeviceInfo; using ::nearby::sharing::api::PreferenceManager; -constexpr absl::string_view kDefaultDeviceName = "$0\'s $1"; - // Returns a truncated version of |name| that is |max_length| characters long. // For example, name="Reallylongname" with max_length=9 will return "Really...". // name="Reallylongname" with max_length=20 will return "Reallylongname". @@ -71,7 +68,7 @@ NearbyShareLocalDeviceDataManagerImpl::Factory* std::unique_ptr NearbyShareLocalDeviceDataManagerImpl::Factory::Create( PreferenceManager& preference_manager, - AccountManager& account_manager, nearby::DeviceInfo& device_info) { + AccountManager& account_manager, nearby::api::DeviceInfo& device_info) { if (test_factory_) { return test_factory_->CreateInstance(); } @@ -90,7 +87,7 @@ NearbyShareLocalDeviceDataManagerImpl::Factory::~Factory() = default; NearbyShareLocalDeviceDataManagerImpl::NearbyShareLocalDeviceDataManagerImpl( PreferenceManager& preference_manager, AccountManager& account_manager, - nearby::DeviceInfo& device_info) + nearby::api::DeviceInfo& device_info) : preference_manager_(preference_manager), account_manager_(account_manager), device_info_(device_info) {} @@ -147,20 +144,24 @@ std::string NearbyShareLocalDeviceDataManagerImpl::GetDefaultDeviceName() if (os_type == DeviceInfo::OsType::kMacOS || os_type == DeviceInfo::OsType::kIos || !account.has_value() || account->given_name.empty()) { - std::string device_name = device_info_.GetOsDeviceName(); + std::string device_name = + device_info_.GetOsDeviceName().value_or("unknown"); return GetTruncatedName(device_name, kNearbyShareDeviceNameMaxLength); } std::string given_name = account->given_name; - std::string device_type = device_info_.GetDeviceTypeName(); - uint64_t untruncated_length = - absl::Substitute(kDefaultDeviceName, given_name, device_type).length(); + DeviceInfo::DeviceType device_type = device_info_.GetDeviceType(); + std::string device_name = absl::StrCat(given_name, "'s ", device_type); + uint64_t untruncated_length = device_name.length(); + if (untruncated_length <= kNearbyShareDeviceNameMaxLength) { + return device_name; + } uint64_t overflow_length = untruncated_length - kNearbyShareDeviceNameMaxLength; std::string truncated_name = GetTruncatedName(given_name, given_name.length() - overflow_length); - return absl::Substitute(kDefaultDeviceName, truncated_name, device_type); + return absl::StrCat(truncated_name, "'s ", device_type); } } // namespace nearby::sharing diff --git a/sharing/local_device_data/nearby_share_local_device_data_manager_impl.h b/sharing/local_device_data/nearby_share_local_device_data_manager_impl.h index 5231babb..7df292b0 100644 --- a/sharing/local_device_data/nearby_share_local_device_data_manager_impl.h +++ b/sharing/local_device_data/nearby_share_local_device_data_manager_impl.h @@ -20,7 +20,7 @@ #include "location/nearby/sharing/lib/account/account_manager.h" #include "absl/strings/string_view.h" -#include "internal/platform/device_info.h" +#include "internal/platform/implementation/device_info.h" #include "sharing/common/nearby_share_enums.h" #include "sharing/internal/api/preference_manager.h" #include "sharing/local_device_data/nearby_share_local_device_data_manager.h" @@ -40,7 +40,7 @@ class NearbyShareLocalDeviceDataManagerImpl public: static std::unique_ptr Create( nearby::sharing::api::PreferenceManager& preference_manager, - AccountManager& account_manager, nearby::DeviceInfo& device_info); + AccountManager& account_manager, nearby::api::DeviceInfo& device_info); static void SetFactoryForTesting(Factory* test_factory); protected: @@ -61,7 +61,7 @@ class NearbyShareLocalDeviceDataManagerImpl private: NearbyShareLocalDeviceDataManagerImpl( nearby::sharing::api::PreferenceManager& preference_manager, - AccountManager& account_manager, nearby::DeviceInfo& device_info); + AccountManager& account_manager, nearby::api::DeviceInfo& device_info); DeviceNameValidationResult ValidateDeviceName(absl::string_view name); @@ -73,7 +73,7 @@ class NearbyShareLocalDeviceDataManagerImpl nearby::sharing::api::PreferenceManager& preference_manager_; AccountManager& account_manager_; - nearby::DeviceInfo& device_info_; + nearby::api::DeviceInfo& device_info_; }; } // namespace nearby::sharing diff --git a/sharing/local_device_data/nearby_share_local_device_data_manager_impl_test.cc b/sharing/local_device_data/nearby_share_local_device_data_manager_impl_test.cc index 047f7182..3aa66539 100644 --- a/sharing/local_device_data/nearby_share_local_device_data_manager_impl_test.cc +++ b/sharing/local_device_data/nearby_share_local_device_data_manager_impl_test.cc @@ -109,11 +109,11 @@ class NearbyShareLocalDeviceDataManagerImplTest } std::string GetDeviceName() const { - return fake_device_info_.GetOsDeviceName(); + return fake_device_info_.GetOsDeviceName().value_or("unknown"); } - std::string GetDeviceTypeName() const { - return fake_device_info_.GetDeviceTypeName(); + nearby::FakeDeviceInfo::DeviceType GetDeviceType() const { + return fake_device_info_.GetDeviceType(); } protected: @@ -138,7 +138,7 @@ TEST_F(NearbyShareLocalDeviceDataManagerImplTest, DefaultDeviceName) { fake_account_manager().SetAccount(account); EXPECT_EQ(absl::Substitute(kDefaultDeviceName, kFakeGivenName, - GetDeviceTypeName()), + GetDeviceType()), manager()->GetDeviceName()); // Make sure that when we use a given name that is very long we truncate @@ -152,7 +152,7 @@ TEST_F(NearbyShareLocalDeviceDataManagerImplTest, SetDeviceName) { CreateManager(); std::string expected_default_device_name = - absl::Substitute(kDefaultDeviceName, kFakeGivenName, GetDeviceTypeName()); + absl::Substitute(kDefaultDeviceName, kFakeGivenName, GetDeviceType()); EXPECT_EQ(manager()->GetDeviceName(), expected_default_device_name); EXPECT_TRUE(notifications().empty()); diff --git a/sharing/nearby_connection_impl.cc b/sharing/nearby_connection_impl.cc index 42ab97eb..eecca33d 100644 --- a/sharing/nearby_connection_impl.cc +++ b/sharing/nearby_connection_impl.cc @@ -22,12 +22,12 @@ #include #include "absl/synchronization/mutex.h" -#include "internal/platform/device_info.h" +#include "internal/platform/implementation/device_info.h" #include "sharing/internal/public/logging.h" namespace nearby::sharing { -NearbyConnectionImpl::NearbyConnectionImpl(nearby::DeviceInfo& device_info) +NearbyConnectionImpl::NearbyConnectionImpl(nearby::api::DeviceInfo& device_info) : device_info_(device_info) { if (!device_info_.PreventSleep()) { LOG(WARNING) << __func__ << ":Failed to prevent device sleep."; diff --git a/sharing/nearby_connection_impl.h b/sharing/nearby_connection_impl.h index 35725791..090bf9eb 100644 --- a/sharing/nearby_connection_impl.h +++ b/sharing/nearby_connection_impl.h @@ -23,7 +23,7 @@ #include "absl/base/thread_annotations.h" #include "absl/synchronization/mutex.h" -#include "internal/platform/device_info.h" +#include "internal/platform/implementation/device_info.h" #include "sharing/nearby_connection.h" namespace nearby::sharing { @@ -32,7 +32,7 @@ class NearbyConnectionsManager; class NearbyConnectionImpl : public NearbyConnection { public: - explicit NearbyConnectionImpl(nearby::DeviceInfo& device_info); + explicit NearbyConnectionImpl(nearby::api::DeviceInfo& device_info); ~NearbyConnectionImpl() override; // NearbyConnection: @@ -46,7 +46,7 @@ class NearbyConnectionImpl : public NearbyConnection { void WriteMessage(std::vector bytes) ABSL_LOCKS_EXCLUDED(mutex_); private: - nearby::DeviceInfo& device_info_; + nearby::api::DeviceInfo& device_info_; absl::Mutex mutex_; std::function> bytes)> read_callback_ diff --git a/sharing/nearby_connections_manager_factory.cc b/sharing/nearby_connections_manager_factory.cc index 31816390..9f0d40e3 100644 --- a/sharing/nearby_connections_manager_factory.cc +++ b/sharing/nearby_connections_manager_factory.cc @@ -17,7 +17,7 @@ #include #include "internal/analytics/event_logger.h" -#include "internal/platform/device_info.h" +#include "internal/platform/implementation/device_info.h" #include "internal/platform/task_runner.h" #include "sharing/internal/public/context.h" #include "sharing/nearby_connections_manager.h" @@ -29,7 +29,7 @@ namespace nearby::sharing { std::unique_ptr NearbyConnectionsManagerFactory::CreateConnectionsManager( nearby::TaskRunner* connections_callback_task_runner, Context* context, - nearby::DeviceInfo& device_info, + nearby::api::DeviceInfo& device_info, nearby::analytics::EventLogger* event_logger) { return std::make_unique( connections_callback_task_runner, context, diff --git a/sharing/nearby_connections_manager_factory.h b/sharing/nearby_connections_manager_factory.h index f22da1ef..bb45b9c7 100644 --- a/sharing/nearby_connections_manager_factory.h +++ b/sharing/nearby_connections_manager_factory.h @@ -18,7 +18,7 @@ #include #include "internal/analytics/event_logger.h" -#include "internal/platform/device_info.h" +#include "internal/platform/implementation/device_info.h" #include "internal/platform/task_runner.h" #include "sharing/internal/public/context.h" #include "sharing/nearby_connections_manager.h" @@ -33,7 +33,7 @@ class NearbyConnectionsManagerFactory { // that NearbySharingService is running on. static std::unique_ptr CreateConnectionsManager( nearby::TaskRunner* connections_callback_task_runner, Context* context, - nearby::DeviceInfo& device_info, + nearby::api::DeviceInfo& device_info, nearby::analytics::EventLogger* event_logger = nullptr); private: diff --git a/sharing/nearby_connections_manager_impl.cc b/sharing/nearby_connections_manager_impl.cc index 20b6e1ef..f7154f69 100644 --- a/sharing/nearby_connections_manager_impl.cc +++ b/sharing/nearby_connections_manager_impl.cc @@ -32,7 +32,7 @@ #include "absl/types/span.h" #include "internal/base/file_path.h" #include "internal/flags/nearby_flags.h" -#include "internal/platform/device_info.h" +#include "internal/platform/implementation/device_info.h" #include "internal/platform/mutex_lock.h" #include "internal/platform/task_runner.h" #include "sharing/advertisement.h" @@ -150,7 +150,8 @@ std::string PayloadStatusToString(PayloadStatus status) { NearbyConnectionsManagerImpl::NearbyConnectionsManagerImpl( TaskRunner* connections_callback_task_runner, Context* context, - ConnectivityManager& connectivity_manager, nearby::DeviceInfo& device_info, + ConnectivityManager& connectivity_manager, + nearby::api::DeviceInfo& device_info, std::unique_ptr nearby_connections_service) : connections_callback_task_runner_(connections_callback_task_runner), context_(context), diff --git a/sharing/nearby_connections_manager_impl.h b/sharing/nearby_connections_manager_impl.h index 0de7d791..bf9ce1e8 100644 --- a/sharing/nearby_connections_manager_impl.h +++ b/sharing/nearby_connections_manager_impl.h @@ -27,7 +27,7 @@ #include "absl/container/flat_hash_set.h" #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/mutex.h" #include "internal/platform/task_runner.h" #include "internal/platform/timer.h" @@ -49,7 +49,7 @@ class NearbyConnectionsManagerImpl : public NearbyConnectionsManager { explicit NearbyConnectionsManagerImpl( nearby::TaskRunner* connections_callback_task_runner, Context* context, nearby::ConnectivityManager& connectivity_manager, - nearby::DeviceInfo& device_info, + nearby::api::DeviceInfo& device_info, std::unique_ptr nearby_connections_service); ~NearbyConnectionsManagerImpl() override; NearbyConnectionsManagerImpl(const NearbyConnectionsManagerImpl&) = delete; @@ -146,7 +146,7 @@ class NearbyConnectionsManagerImpl : public NearbyConnectionsManager { nearby::TaskRunner* const connections_callback_task_runner_; Context* const context_; nearby::ConnectivityManager& connectivity_manager_; - nearby::DeviceInfo& device_info_; + nearby::api::DeviceInfo& device_info_; // Nearby Connections Manager is called from different threads and may have // multiple calls to the class from one thread. To avoid deadlock and access diff --git a/sharing/nearby_sharing_service_impl.cc b/sharing/nearby_sharing_service_impl.cc index 8e53dbef..4f0c3086 100644 --- a/sharing/nearby_sharing_service_impl.cc +++ b/sharing/nearby_sharing_service_impl.cc @@ -49,7 +49,6 @@ #include "internal/flags/nearby_flags.h" #include "internal/network/url.h" #include "internal/platform/clock.h" -#include "internal/platform/device_info.h" #include "internal/platform/implementation/device_info.h" #include "internal/platform/task_runner.h" #include "proto/sharing_enums.pb.h" @@ -281,7 +280,7 @@ NearbySharingServiceImpl::NearbySharingServiceImpl( is_shutting_down_ = std::make_unique(false); FilePath profile_path = - device_info_.GetAppDataPath().append(FilePath(kProfileRelativePath)); + device_info_.GetLocalAppDataPath(FilePath(kProfileRelativePath)); certificate_manager_ = NearbyShareCertificateManagerImpl::Factory::Create( context_, sharing_platform, local_device_data_manager_.get(), @@ -2692,15 +2691,16 @@ void NearbySharingServiceImpl::OnReceivedIntroduction( session.session_id(), session.share_target(), /*referrer_package=*/std::nullopt, session.os_type()); - if (IsOutOfStorage(device_info_, save_path, - session.attachment_container().GetStorageSize())) { + std::optional available_storage = + device_info_.GetAvailableDiskSpaceInBytes(save_path); + if (available_storage.has_value() && + *available_storage <= session.attachment_container().GetStorageSize()) { Fail(session, TransferMetadata::Status::kNotEnoughSpace); LOG(WARNING) << __func__ << ": Not enough space on the receiver. We have informed " << session.share_target().id; return; } - OnStorageCheckCompleted(session); } diff --git a/sharing/nearby_sharing_service_impl.h b/sharing/nearby_sharing_service_impl.h index 5a0a2fca..e0c04146 100644 --- a/sharing/nearby_sharing_service_impl.h +++ b/sharing/nearby_sharing_service_impl.h @@ -39,7 +39,7 @@ #include "absl/time/time.h" #include "absl/types/span.h" #include "internal/platform/clock.h" -#include "internal/platform/device_info.h" +#include "internal/platform/implementation/device_info.h" #include "internal/platform/task_runner.h" #include "proto/sharing_enums.pb.h" #include "sharing/advertisement.h" @@ -425,7 +425,7 @@ class NearbySharingServiceImpl // Used to run nearby sharing service APIs. std::unique_ptr service_thread_; Context* const context_; - nearby::DeviceInfo& device_info_; + nearby::api::DeviceInfo& device_info_; nearby::sharing::api::PreferenceManager& preference_manager_; AccountManager& account_manager_; // Used to create analytics events. diff --git a/sharing/nearby_sharing_settings.cc b/sharing/nearby_sharing_settings.cc index 9a189aed..8d894727 100644 --- a/sharing/nearby_sharing_settings.cc +++ b/sharing/nearby_sharing_settings.cc @@ -26,7 +26,7 @@ #include "absl/synchronization/mutex.h" #include "absl/time/time.h" #include "internal/platform/clock.h" -#include "internal/platform/device_info.h" +#include "internal/platform/implementation/device_info.h" #include "proto/sharing_enums.pb.h" #include "sharing/analytics/analytics_recorder.h" #include "sharing/common/nearby_share_enums.h" @@ -69,8 +69,8 @@ ShowNotificationStatus GetNotificationStatus( } // namespace NearbyShareSettings::NearbyShareSettings( - Context* context, nearby::Clock* clock, nearby::DeviceInfo& device_info, - PreferenceManager& preference_manager, + Context* context, nearby::Clock* clock, + nearby::api::DeviceInfo& device_info, PreferenceManager& preference_manager, NearbyShareLocalDeviceDataManager* local_device_data_manager, analytics::AnalyticsRecorder* analytics_recorder) : context_(context), diff --git a/sharing/nearby_sharing_settings.h b/sharing/nearby_sharing_settings.h index 09fa7016..665d8f97 100644 --- a/sharing/nearby_sharing_settings.h +++ b/sharing/nearby_sharing_settings.h @@ -28,7 +28,7 @@ #include "absl/time/time.h" #include "internal/base/observer_list.h" #include "internal/platform/clock.h" -#include "internal/platform/device_info.h" +#include "internal/platform/implementation/device_info.h" #include "proto/sharing_enums.pb.h" #include "sharing/analytics/analytics_recorder.h" #include "sharing/common/nearby_share_enums.h" @@ -138,7 +138,8 @@ class NearbyShareSettings }; NearbyShareSettings( - Context* context, nearby::Clock* clock, nearby::DeviceInfo& device_info, + Context* context, nearby::Clock* clock, + nearby::api::DeviceInfo& device_info, nearby::sharing::api::PreferenceManager& preference_manager, NearbyShareLocalDeviceDataManager* local_device_data_manager, analytics::AnalyticsRecorder* analytics_recorder = nullptr); @@ -220,7 +221,7 @@ class NearbyShareSettings mutable absl::Mutex mutex_; Context* context_; nearby::Clock* const clock_; - nearby::DeviceInfo& device_info_; + nearby::api::DeviceInfo& device_info_; nearby::sharing::api::PreferenceManager& preference_manager_; NearbyShareLocalDeviceDataManager* const local_device_data_manager_; // Used to create analytics events. diff --git a/sharing/nearby_sharing_util.cc b/sharing/nearby_sharing_util.cc index 1fed0e12..06c38de2 100644 --- a/sharing/nearby_sharing_util.cc +++ b/sharing/nearby_sharing_util.cc @@ -14,10 +14,7 @@ #include "sharing/nearby_sharing_util.h" -#include #include -#include -#include #include #include #include @@ -26,8 +23,6 @@ #include "absl/strings/escaping.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" #include "sharing/certificates/nearby_share_decrypted_public_certificate.h" @@ -115,16 +110,4 @@ std::string GetDeviceId( return std::string(endpoint_id); } -bool IsOutOfStorage(DeviceInfo& device_info, FilePath file_path, - int64_t storage_required) { - std::optional available_storage = - device_info.GetAvailableDiskSpaceInBytes(file_path); - - if (!available_storage.has_value()) { - return false; - } - - return *available_storage <= storage_required; -} - } // namespace nearby::sharing diff --git a/sharing/nearby_sharing_util.h b/sharing/nearby_sharing_util.h index bcc4c90d..d2b2df59 100644 --- a/sharing/nearby_sharing_util.h +++ b/sharing/nearby_sharing_util.h @@ -21,23 +21,13 @@ #include #include "absl/strings/string_view.h" -#include "internal/platform/device_info.h" #include "proto/sharing_enums.pb.h" -#include "internal/base/file_path.h" #include "sharing/advertisement.h" #include "sharing/certificates/nearby_share_decrypted_public_certificate.h" #include "sharing/common/nearby_share_enums.h" namespace nearby::sharing { -// Checks whether having enough disk space for required storage. -// -// device_info - Nearby Share DeviceInfo -// file_path - The path is to store sharing contents. -// storage_required - required storage space. -bool IsOutOfStorage(nearby::DeviceInfo& device_info, FilePath file_path, - int64_t storage_required); - // Decodes certificate to find MAC address encoded in it. std::optional> GetBluetoothMacAddressFromCertificate( const NearbyShareDecryptedPublicCertificate& certificate);