Add internet connectivity check to network monitor.

PiperOrigin-RevId: 739495202
This commit is contained in:
Francis Tsui
2025-03-22 09:33:41 -07:00
committed by Copybara-Service
parent cf271d55b7
commit 51ef2273ca
14 changed files with 79 additions and 43 deletions
@@ -29,6 +29,7 @@ class MockNetworkMonitor : public nearby::api::NetworkMonitor {
~MockNetworkMonitor() override = default;
MOCK_METHOD(bool, IsLanConnected, (), (override));
MOCK_METHOD(bool, IsInternetConnected, (), (override));
MOCK_METHOD(ConnectionType, GetCurrentConnection, (), (override));
};
+6 -5
View File
@@ -56,11 +56,12 @@ class MockSharingPlatform : public SharingPlatform {
MOCK_METHOD(void, UpdateLoggingLevel, (), (override));
MOCK_METHOD(
std::unique_ptr<nearby::api::NetworkMonitor>, CreateNetworkMonitor,
(std::function<void(nearby::api::NetworkMonitor::ConnectionType, bool)>
callback),
(override));
MOCK_METHOD(std::unique_ptr<nearby::api::NetworkMonitor>,
CreateNetworkMonitor,
(std::function<void(nearby::api::NetworkMonitor::ConnectionType,
bool, bool)>
callback),
(override));
MOCK_METHOD(nearby::sharing::api::BluetoothAdapter&, GetBluetoothAdapter, (),
(override));
+10 -3
View File
@@ -38,21 +38,28 @@ class NetworkMonitor {
kLast = k5G
};
explicit NetworkMonitor(std::function<void(ConnectionType, bool)> callback) {
// Registers a callback for connection changes. The callback will be called
// with the current connection type, whether the device is connected to a
// LAN network and whether the device is connected to internet.
explicit NetworkMonitor(
std::function<void(ConnectionType, bool, bool)> callback) {
callback_ = std::move(callback);
}
virtual ~NetworkMonitor() = default;
// Returns true if connected to an AP (Access Point), not necessarily
// connected to the internet
// connected to the internet.
virtual bool IsLanConnected() = 0;
// Returns true if connected to internet.
virtual bool IsInternetConnected() = 0;
// Returns the type of connection used currently to access the internet
virtual ConnectionType GetCurrentConnection() = 0;
protected:
std::function<void(ConnectionType, bool)> callback_;
std::function<void(ConnectionType, bool, bool)> callback_;
};
} // namespace api
+2 -1
View File
@@ -57,7 +57,8 @@ class SharingPlatform {
virtual void UpdateLoggingLevel() = 0;
virtual std::unique_ptr<nearby::api::NetworkMonitor> CreateNetworkMonitor(
std::function<void(nearby::api::NetworkMonitor::ConnectionType, bool)>
std::function<void(nearby::api::NetworkMonitor::ConnectionType, bool,
bool)>
callback) = 0;
virtual BluetoothAdapter& GetBluetoothAdapter() = 0;
@@ -40,12 +40,16 @@ class ConnectivityManager {
virtual ~ConnectivityManager() = default;
virtual bool IsLanConnected() = 0;
virtual bool IsInternetConnected() = 0;
virtual ConnectionType GetConnectionType() = 0;
// Registers a listener for connection changes. The listener will be called
// with the current connection type, whether the device is connected to a
// LAN network and whether the device is connected to internet.
virtual void RegisterConnectionListener(
absl::string_view listener_name,
std::function<void(ConnectionType, bool)>) = 0;
std::function<void(ConnectionType, bool, bool)>) = 0;
virtual void UnregisterConnectionListener(
absl::string_view listener_name) = 0;
// Is the device a HP device with Realtek wireless module.
@@ -65,13 +65,14 @@ ConnectivityManagerImpl::ConnectivityManagerImpl(SharingPlatform& platform)
: platform_(platform) {
network_monitor_ = platform.CreateNetworkMonitor(
[this](api::NetworkMonitor::ConnectionType connection_type,
bool is_lan_connected) {
bool is_lan_connected, bool is_internet_connected) {
ConnectionType new_connection_type =
static_cast<ConnectionType>(connection_type);
VLOG(1) << ": New connection type:"
<< GetConnectionTypeString(new_connection_type);
for (auto& listener : listeners_) {
listener.second(new_connection_type, is_lan_connected);
listener.second(new_connection_type, is_lan_connected,
is_internet_connected);
}
});
}
@@ -80,6 +81,10 @@ bool ConnectivityManagerImpl::IsLanConnected() {
return network_monitor_->IsLanConnected();
}
bool ConnectivityManagerImpl::IsInternetConnected() {
return network_monitor_->IsInternetConnected();
}
bool ConnectivityManagerImpl::IsHPRealtekDevice() {
if (NearbyFlags::GetInstance().GetBoolFlag(
sharing::config_package_nearby::nearby_sharing_feature::
@@ -114,7 +119,7 @@ ConnectionType ConnectivityManagerImpl::GetConnectionType() {
void ConnectivityManagerImpl::RegisterConnectionListener(
absl::string_view listener_name,
std::function<void(ConnectionType, bool)> callback) {
std::function<void(ConnectionType, bool, bool)> callback) {
listeners_.emplace(listener_name, std::move(callback));
}
@@ -38,18 +38,20 @@ class ConnectivityManagerImpl : public ConnectivityManager {
nearby::sharing::api::SharingPlatform& platform);
bool IsLanConnected() override;
bool IsInternetConnected() override;
bool IsHPRealtekDevice() override;
ConnectionType GetConnectionType() override;
void RegisterConnectionListener(
absl::string_view listener_name,
std::function<void(ConnectionType, bool)> callback) override;
std::function<void(ConnectionType, bool, bool)> callback) override;
void UnregisterConnectionListener(absl::string_view listener_name) override;
int GetListenerCount() const;
private:
absl::flat_hash_map<std::string, std::function<void(ConnectionType, bool)>>
absl::flat_hash_map<std::string,
std::function<void(ConnectionType, bool, bool)>>
listeners_;
std::unique_ptr<api::NetworkMonitor> network_monitor_;
nearby::sharing::api::SharingPlatform& platform_;
@@ -112,12 +112,12 @@ TEST(ConnectivityManagerImpl, IsHPRealtekDevice_NotRealtekDevice) {
}
TEST(ConnectivityManagerImpl, RegisterConnectionListener) {
std::function<void(ConnectivityManager::ConnectionType, bool)> listener_1 =
[](ConnectivityManager::ConnectionType connection_type,
bool is_lan_connected) {};
std::function<void(ConnectivityManager::ConnectionType, bool)> listener_2 =
[](ConnectivityManager::ConnectionType connection_type,
bool is_lan_connected) {};
std::function<void(ConnectivityManager::ConnectionType, bool, bool)>
listener_1 = [](ConnectivityManager::ConnectionType connection_type,
bool is_lan_connected, bool is_internet_connected) {};
std::function<void(ConnectivityManager::ConnectionType, bool, bool)>
listener_2 = [](ConnectivityManager::ConnectionType connection_type,
bool is_lan_connected, bool is_internet_connected) {};
MockSharingPlatform sharing_platform;
ConnectivityManagerImpl connectivity_manager_impl(sharing_platform);
@@ -133,12 +133,12 @@ TEST(ConnectivityManagerImpl, RegisterConnectionListener) {
}
TEST(ConnectivityManagerImpl, UnregisterConnectionListener) {
std::function<void(ConnectivityManager::ConnectionType, bool)> listener_1 =
[](ConnectivityManager::ConnectionType connection_type,
bool is_lan_connected) {};
std::function<void(ConnectivityManager::ConnectionType, bool)> listener_2 =
[](ConnectivityManager::ConnectionType connection_type,
bool is_lan_connected) {};
std::function<void(ConnectivityManager::ConnectionType, bool, bool)>
listener_1 = [](ConnectivityManager::ConnectionType connection_type,
bool is_lan_connected, bool is_internet_connected) {};
std::function<void(ConnectivityManager::ConnectionType, bool, bool)>
listener_2 = [](ConnectivityManager::ConnectionType connection_type,
bool is_lan_connected, bool is_internet_connected) {};
MockSharingPlatform sharing_platform;
ConnectivityManagerImpl connectivity_manager_impl(sharing_platform);
@@ -28,6 +28,7 @@ namespace nearby {
class FakeConnectivityManager : public ConnectivityManager {
public:
bool IsLanConnected() override { return is_lan_connected_; }
bool IsInternetConnected() override { return is_internet_connected_; }
bool IsHPRealtekDevice() override { return is_hp_realtek_device_; }
void SetIsHPRealtekDevice(bool is_hp_realtek_device) {
is_hp_realtek_device_ = is_hp_realtek_device;
@@ -36,7 +37,7 @@ class FakeConnectivityManager : public ConnectivityManager {
void RegisterConnectionListener(
absl::string_view listener_name,
std::function<void(ConnectionType, bool)> callback) override {
std::function<void(ConnectionType, bool, bool)> callback) override {
listeners_.emplace(listener_name, std::move(callback));
}
void UnregisterConnectionListener(absl::string_view listener_name) override {
@@ -47,7 +48,16 @@ class FakeConnectivityManager : public ConnectivityManager {
void SetLanConnected(bool connected) {
is_lan_connected_ = connected;
for (auto& listener : listeners_) {
listener.second(connection_type_, is_lan_connected_);
listener.second(connection_type_, is_lan_connected_,
is_internet_connected_);
}
}
void SetInternetConnected(bool connected) {
is_internet_connected_ = connected;
for (auto& listener : listeners_) {
listener.second(connection_type_, is_lan_connected_,
is_internet_connected_);
}
}
@@ -55,16 +65,19 @@ class FakeConnectivityManager : public ConnectivityManager {
void SetConnectionType(ConnectionType connection_type) {
connection_type_ = connection_type;
for (auto& listener : listeners_) {
listener.second(connection_type_, is_lan_connected_);
listener.second(connection_type_, is_lan_connected_,
is_internet_connected_);
}
}
int GetListenerCount() const { return listeners_.size(); }
private:
bool is_lan_connected_ = true;
bool is_internet_connected_ = true;
bool is_hp_realtek_device_ = false;
ConnectionType connection_type_ = ConnectionType::kWifi;
absl::flat_hash_map<std::string, std::function<void(ConnectionType, bool)>>
absl::flat_hash_map<std::string,
std::function<void(ConnectionType, bool, bool)>>
listeners_;
};
@@ -14,8 +14,6 @@
#include "sharing/internal/test/fake_connectivity_manager.h"
#include <string>
#include "gtest/gtest.h"
#include "sharing/internal/public/connectivity_manager.h"
@@ -47,9 +45,9 @@ TEST(FakeConnectivityManager, TestListener) {
connection_manager.GetConnectionType();
bool is_lan_connected = false;
connection_manager.RegisterConnectionListener(
"test",
[&connection_type, &is_lan_connected](
ConnectivityManager::ConnectionType connection, bool connected) {
"test", [&connection_type, &is_lan_connected](
ConnectivityManager::ConnectionType connection,
bool connected, bool internet_connected) {
connection_type = connection;
is_lan_connected = connected;
});
+1 -3
View File
@@ -74,9 +74,7 @@ std::unique_ptr<TaskRunner> FakeContext::CreateSequencedTaskRunner() const {
std::unique_ptr<TaskRunner> FakeContext::CreateConcurrentTaskRunner(
uint32_t concurrent_count) const {
std::unique_ptr<TaskRunner> task_runner =
std::make_unique<FakeTaskRunner>(fake_clock_.get(), concurrent_count);
return task_runner;
return std::make_unique<FakeTaskRunner>(fake_clock_.get(), concurrent_count);
}
TaskRunner* FakeContext::GetTaskRunner() { return executor_.get(); }
+8 -2
View File
@@ -24,26 +24,32 @@ namespace nearby {
class FakeNetworkMonitor : public api::NetworkMonitor {
public:
explicit FakeNetworkMonitor(
std::function<void(api::NetworkMonitor::ConnectionType, bool)> callback)
std::function<void(api::NetworkMonitor::ConnectionType, bool, bool)>
callback)
: api::NetworkMonitor(callback) {}
~FakeNetworkMonitor() override { callback_ = nullptr; }
bool IsLanConnected() override { return is_lan_connected_; }
bool IsInternetConnected() override { return is_internet_connected_; }
api::NetworkMonitor::ConnectionType GetCurrentConnection() override {
return api::NetworkMonitor::ConnectionType::kWifi;
}
void SetLanConnected(bool connected) { is_lan_connected_ = connected; }
void SetInternetConnected(bool connected) {
is_internet_connected_ = connected;
}
void TestNetworkChangeToEthernet() {
callback_(api::NetworkMonitor::ConnectionType::kEthernet,
is_lan_connected_);
is_lan_connected_, is_internet_connected_);
}
private:
bool is_lan_connected_ = true;
bool is_internet_connected_ = true;
};
} // namespace nearby
+1 -1
View File
@@ -238,7 +238,7 @@ NearbySharingServiceImpl::NearbySharingServiceImpl(
context_->GetConnectivityManager()->RegisterConnectionListener(
kConnectionListenerName,
[this](nearby::ConnectivityManager::ConnectionType type,
bool is_lan_connected) {
bool is_lan_connected, bool is_internet_connected) {
OnNetworkChanged(type);
OnLanConnectedChanged(is_lan_connected);
});
@@ -81,7 +81,7 @@ NearbyShareSchedulerBase::NearbyShareSchedulerBase(
connectivity_manager_->RegisterConnectionListener(
connection_listener_name_,
[this](nearby::ConnectivityManager::ConnectionType connection_type,
bool is_lan_connected) {
bool is_lan_connected, bool is_internet_connected) {
OnConnectionChanged(connection_type);
});
}