Commit e5128829 authored by Jun Cai's avatar Jun Cai Committed by Commit Bot

Reland: Network Service: Migrate chromeos::assistant::NetworkProviderImpl to...

Reland: Network Service: Migrate chromeos::assistant::NetworkProviderImpl to NetworkConnectionTracker

The original CL is:
https://chromium-review.googlesource.com/c/chromium/src/+/1270950
and the Initial upload is the same as the original CL.

The reland CL fixes compile error on chromeos.

TBR=jam@chromium.org, stevenjb@chromium.org, xiaohuic@chromium.org, rmcelrath@chromium.org

Bug: 887040
Change-Id: Ic0cf9d07f7c7d6c5c59694c1414bc38b83a36456
Reviewed-on: https://chromium-review.googlesource.com/c/1277647Reviewed-by: default avatarJun Cai <juncai@chromium.org>
Reviewed-by: default avatarJoel Hockey <joelhockey@chromium.org>
Commit-Queue: Jun Cai <juncai@chromium.org>
Cr-Commit-Position: refs/heads/master@{#599042}
parent a88c577a
...@@ -176,6 +176,7 @@ ...@@ -176,6 +176,7 @@
#if BUILDFLAG(ENABLE_CROS_ASSISTANT) #if BUILDFLAG(ENABLE_CROS_ASSISTANT)
#include "chromeos/services/assistant/public/mojom/constants.mojom.h" #include "chromeos/services/assistant/public/mojom/constants.mojom.h"
#include "chromeos/services/assistant/service.h" #include "chromeos/services/assistant/service.h"
#include "content/public/browser/network_service_instance.h"
#endif #endif
#endif #endif
...@@ -1164,8 +1165,11 @@ void ProfileImpl::RegisterInProcessServices(StaticServiceMap* services) { ...@@ -1164,8 +1165,11 @@ void ProfileImpl::RegisterInProcessServices(StaticServiceMap* services) {
{ {
service_manager::EmbeddedServiceInfo info; service_manager::EmbeddedServiceInfo info;
info.factory = base::BindRepeating([] { info.factory = base::BindRepeating([] {
network::NetworkConnectionTracker* network_connection_tracker =
content::GetNetworkConnectionTracker();
return std::unique_ptr<service_manager::Service>( return std::unique_ptr<service_manager::Service>(
std::make_unique<chromeos::assistant::Service>()); std::make_unique<chromeos::assistant::Service>(
network_connection_tracker));
}); });
info.task_runner = base::CreateSingleThreadTaskRunnerWithTraits( info.task_runner = base::CreateSingleThreadTaskRunnerWithTraits(
{content::BrowserThread::UI}); {content::BrowserThread::UI});
......
...@@ -80,6 +80,8 @@ source_set("lib") { ...@@ -80,6 +80,8 @@ source_set("lib") {
"//libassistant/shared/internal_api/c:api_wrappers_entrypoint", "//libassistant/shared/internal_api/c:api_wrappers_entrypoint",
"//libassistant/shared/public", "//libassistant/shared/public",
"//libassistant/shared/public:export", "//libassistant/shared/public:export",
"//services/network/public/cpp",
"//services/network/public/mojom",
"//ui/base", "//ui/base",
] ]
......
...@@ -10,6 +10,7 @@ include_rules = [ ...@@ -10,6 +10,7 @@ include_rules = [
"+services/audio/public", "+services/audio/public",
"+services/device/public", "+services/device/public",
"+services/identity/public", "+services/identity/public",
"+services/network/public",
"+services/service_manager/public", "+services/service_manager/public",
"+ui/accessibility/ax_assistant_structure.h", "+ui/accessibility/ax_assistant_structure.h",
"+ui/accessibility/mojom", "+ui/accessibility/mojom",
......
...@@ -62,7 +62,8 @@ AssistantManagerServiceImpl::AssistantManagerServiceImpl( ...@@ -62,7 +62,8 @@ AssistantManagerServiceImpl::AssistantManagerServiceImpl(
service_manager::Connector* connector, service_manager::Connector* connector,
device::mojom::BatteryMonitorPtr battery_monitor, device::mojom::BatteryMonitorPtr battery_monitor,
Service* service, Service* service,
bool enable_hotword) bool enable_hotword,
network::NetworkConnectionTracker* network_connection_tracker)
: enable_hotword_(enable_hotword), : enable_hotword_(enable_hotword),
action_module_(std::make_unique<action::CrosActionModule>(this)), action_module_(std::make_unique<action::CrosActionModule>(this)),
main_thread_task_runner_(base::ThreadTaskRunnerHandle::Get()), main_thread_task_runner_(base::ThreadTaskRunnerHandle::Get()),
...@@ -76,7 +77,7 @@ AssistantManagerServiceImpl::AssistantManagerServiceImpl( ...@@ -76,7 +77,7 @@ AssistantManagerServiceImpl::AssistantManagerServiceImpl(
background_thread_.Start(); background_thread_.Start();
platform_api_ = std::make_unique<PlatformApiImpl>( platform_api_ = std::make_unique<PlatformApiImpl>(
connector, std::move(battery_monitor), enable_hotword, connector, std::move(battery_monitor), enable_hotword,
background_thread_.task_runner()); background_thread_.task_runner(), network_connection_tracker);
connector->BindInterface(ash::mojom::kServiceName, connector->BindInterface(ash::mojom::kServiceName,
&voice_interaction_controller_); &voice_interaction_controller_);
connector->BindInterface(ash::mojom::kServiceName, connector->BindInterface(ash::mojom::kServiceName,
......
...@@ -58,10 +58,12 @@ class AssistantManagerServiceImpl ...@@ -58,10 +58,12 @@ class AssistantManagerServiceImpl
public assistant_client::DeviceStateListener { public assistant_client::DeviceStateListener {
public: public:
// |service| owns this class and must outlive this class. // |service| owns this class and must outlive this class.
AssistantManagerServiceImpl(service_manager::Connector* connector, AssistantManagerServiceImpl(
device::mojom::BatteryMonitorPtr battery_monitor, service_manager::Connector* connector,
Service* service, device::mojom::BatteryMonitorPtr battery_monitor,
bool enable_hotword); Service* service,
bool enable_hotword,
network::NetworkConnectionTracker* network_connection_tracker);
~AssistantManagerServiceImpl() override; ~AssistantManagerServiceImpl() override;
......
...@@ -10,15 +10,26 @@ using ConnectionStatus = assistant_client::NetworkProvider::ConnectionStatus; ...@@ -10,15 +10,26 @@ using ConnectionStatus = assistant_client::NetworkProvider::ConnectionStatus;
namespace chromeos { namespace chromeos {
namespace assistant { namespace assistant {
NetworkProviderImpl::NetworkProviderImpl() NetworkProviderImpl::NetworkProviderImpl(
: connection_type_(net::NetworkChangeNotifier::GetConnectionType()) { network::NetworkConnectionTracker* network_connection_tracker)
net::NetworkChangeNotifier::AddNetworkChangeObserver(this); : network_connection_tracker_(network_connection_tracker),
weak_factory_(this) {
if (network_connection_tracker_) {
network_connection_tracker_->AddNetworkConnectionObserver(this);
network_connection_tracker_->GetConnectionType(
&connection_type_,
base::BindOnce(&NetworkProviderImpl::OnConnectionChanged,
weak_factory_.GetWeakPtr()));
}
} }
NetworkProviderImpl::~NetworkProviderImpl() = default; NetworkProviderImpl::~NetworkProviderImpl() {
if (network_connection_tracker_)
network_connection_tracker_->RemoveNetworkConnectionObserver(this);
}
void NetworkProviderImpl::OnNetworkChanged( void NetworkProviderImpl::OnConnectionChanged(
net::NetworkChangeNotifier::ConnectionType type) { network::mojom::ConnectionType type) {
connection_type_ = type; connection_type_ = type;
} }
...@@ -26,16 +37,16 @@ ConnectionStatus NetworkProviderImpl::GetConnectionStatus() { ...@@ -26,16 +37,16 @@ ConnectionStatus NetworkProviderImpl::GetConnectionStatus() {
// TODO(updowndota): Check actual internect connectivity in addition to the // TODO(updowndota): Check actual internect connectivity in addition to the
// physical connectivity. // physical connectivity.
switch (connection_type_) { switch (connection_type_) {
case net::NetworkChangeNotifier::CONNECTION_UNKNOWN: case network::mojom::ConnectionType::CONNECTION_UNKNOWN:
return ConnectionStatus::UNKNOWN; return ConnectionStatus::UNKNOWN;
case net::NetworkChangeNotifier::CONNECTION_ETHERNET: case network::mojom::ConnectionType::CONNECTION_ETHERNET:
case net::NetworkChangeNotifier::CONNECTION_WIFI: case network::mojom::ConnectionType::CONNECTION_WIFI:
case net::NetworkChangeNotifier::CONNECTION_2G: case network::mojom::ConnectionType::CONNECTION_2G:
case net::NetworkChangeNotifier::CONNECTION_3G: case network::mojom::ConnectionType::CONNECTION_3G:
case net::NetworkChangeNotifier::CONNECTION_4G: case network::mojom::ConnectionType::CONNECTION_4G:
case net::NetworkChangeNotifier::CONNECTION_BLUETOOTH: case network::mojom::ConnectionType::CONNECTION_BLUETOOTH:
return ConnectionStatus::CONNECTED; return ConnectionStatus::CONNECTED;
case net::NetworkChangeNotifier::CONNECTION_NONE: case network::mojom::ConnectionType::CONNECTION_NONE:
return ConnectionStatus::DISCONNECTED_FROM_INTERNET; return ConnectionStatus::DISCONNECTED_FROM_INTERNET;
} }
} }
......
...@@ -6,29 +6,33 @@ ...@@ -6,29 +6,33 @@
#define CHROMEOS_SERVICES_ASSISTANT_PLATFORM_NETWORK_PROVIDER_IMPL_H_ #define CHROMEOS_SERVICES_ASSISTANT_PLATFORM_NETWORK_PROVIDER_IMPL_H_
#include "base/macros.h" #include "base/macros.h"
#include "base/memory/weak_ptr.h"
#include "libassistant/shared/public/platform_net.h" #include "libassistant/shared/public/platform_net.h"
#include "net/base/network_change_notifier.h" #include "services/network/public/cpp/network_connection_tracker.h"
#include "services/network/public/mojom/network_change_manager.mojom.h"
namespace chromeos { namespace chromeos {
namespace assistant { namespace assistant {
class NetworkProviderImpl class NetworkProviderImpl
: public assistant_client::NetworkProvider, : public assistant_client::NetworkProvider,
public net::NetworkChangeNotifier::NetworkChangeObserver { public network::NetworkConnectionTracker::NetworkConnectionObserver {
public: public:
NetworkProviderImpl(); explicit NetworkProviderImpl(
network::NetworkConnectionTracker* network_connection_tracker);
~NetworkProviderImpl() override; ~NetworkProviderImpl() override;
// net::NetworkChangeNotifier overrides: // network::NetworkConnectionTracker::NetworkConnectionObserver:
void OnNetworkChanged( void OnConnectionChanged(network::mojom::ConnectionType type) override;
net::NetworkChangeNotifier::ConnectionType type) override;
// assistant_client::NetworkProvider::NetworkChangeObserver overrides: // assistant_client::NetworkProvider::NetworkChangeObserver overrides:
ConnectionStatus GetConnectionStatus() override; ConnectionStatus GetConnectionStatus() override;
assistant_client::MdnsResponder* GetMdnsResponder() override; assistant_client::MdnsResponder* GetMdnsResponder() override;
private: private:
net::NetworkChangeNotifier::ConnectionType connection_type_; network::NetworkConnectionTracker* network_connection_tracker_;
network::mojom::ConnectionType connection_type_;
base::WeakPtrFactory<NetworkProviderImpl> weak_factory_;
DISALLOW_COPY_AND_ASSIGN(NetworkProviderImpl); DISALLOW_COPY_AND_ASSIGN(NetworkProviderImpl);
}; };
......
...@@ -75,9 +75,11 @@ PlatformApiImpl::PlatformApiImpl( ...@@ -75,9 +75,11 @@ PlatformApiImpl::PlatformApiImpl(
service_manager::Connector* connector, service_manager::Connector* connector,
device::mojom::BatteryMonitorPtr battery_monitor, device::mojom::BatteryMonitorPtr battery_monitor,
bool enable_hotword, bool enable_hotword,
scoped_refptr<base::SingleThreadTaskRunner> background_task_runner) scoped_refptr<base::SingleThreadTaskRunner> background_task_runner,
network::NetworkConnectionTracker* network_connection_tracker)
: audio_input_provider_(connector, enable_hotword), : audio_input_provider_(connector, enable_hotword),
audio_output_provider_(connector, background_task_runner), audio_output_provider_(connector, background_task_runner),
network_provider_(network_connection_tracker),
system_provider_(std::move(battery_monitor)) {} system_provider_(std::move(battery_monitor)) {}
PlatformApiImpl::~PlatformApiImpl() = default; PlatformApiImpl::~PlatformApiImpl() = default;
......
...@@ -34,7 +34,8 @@ class PlatformApiImpl : public assistant_client::PlatformApi { ...@@ -34,7 +34,8 @@ class PlatformApiImpl : public assistant_client::PlatformApi {
service_manager::Connector* connector, service_manager::Connector* connector,
device::mojom::BatteryMonitorPtr battery_monitor, device::mojom::BatteryMonitorPtr battery_monitor,
bool enable_hotword, bool enable_hotword,
scoped_refptr<base::SingleThreadTaskRunner> background_task_runner); scoped_refptr<base::SingleThreadTaskRunner> background_task_runner,
network::NetworkConnectionTracker* network_connection_tracker);
~PlatformApiImpl() override; ~PlatformApiImpl() override;
// assistant_client::PlatformApi overrides // assistant_client::PlatformApi overrides
......
...@@ -52,13 +52,14 @@ constexpr base::TimeDelta kMaxTokenRefreshDelay = ...@@ -52,13 +52,14 @@ constexpr base::TimeDelta kMaxTokenRefreshDelay =
} // namespace } // namespace
Service::Service() Service::Service(network::NetworkConnectionTracker* network_connection_tracker)
: platform_binding_(this), : platform_binding_(this),
session_observer_binding_(this), session_observer_binding_(this),
token_refresh_timer_(std::make_unique<base::OneShotTimer>()), token_refresh_timer_(std::make_unique<base::OneShotTimer>()),
main_thread_task_runner_(base::ThreadTaskRunnerHandle::Get()), main_thread_task_runner_(base::ThreadTaskRunnerHandle::Get()),
power_manager_observer_(this), power_manager_observer_(this),
voice_interaction_observer_binding_(this), voice_interaction_observer_binding_(this),
network_connection_tracker_(network_connection_tracker),
weak_ptr_factory_(this) { weak_ptr_factory_(this) {
registry_.AddInterface<mojom::AssistantPlatform>(base::BindRepeating( registry_.AddInterface<mojom::AssistantPlatform>(base::BindRepeating(
&Service::BindAssistantPlatformConnection, base::Unretained(this))); &Service::BindAssistantPlatformConnection, base::Unretained(this)));
...@@ -283,7 +284,8 @@ void Service::CreateAssistantManagerService(bool enable_hotword) { ...@@ -283,7 +284,8 @@ void Service::CreateAssistantManagerService(bool enable_hotword) {
context()->connector()->BindInterface(device::mojom::kServiceName, context()->connector()->BindInterface(device::mojom::kServiceName,
mojo::MakeRequest(&battery_monitor)); mojo::MakeRequest(&battery_monitor));
assistant_manager_service_ = std::make_unique<AssistantManagerServiceImpl>( assistant_manager_service_ = std::make_unique<AssistantManagerServiceImpl>(
context()->connector(), std::move(battery_monitor), this, enable_hotword); context()->connector(), std::move(battery_monitor), this, enable_hotword,
network_connection_tracker_);
// Bind to Assistant controller in ash. // Bind to Assistant controller in ash.
context()->connector()->BindInterface(ash::mojom::kServiceName, context()->connector()->BindInterface(ash::mojom::kServiceName,
......
...@@ -33,6 +33,10 @@ namespace base { ...@@ -33,6 +33,10 @@ namespace base {
class OneShotTimer; class OneShotTimer;
} }
namespace network {
class NetworkConnectionTracker;
} // namespace network
namespace chromeos { namespace chromeos {
namespace assistant { namespace assistant {
...@@ -45,7 +49,8 @@ class Service : public service_manager::Service, ...@@ -45,7 +49,8 @@ class Service : public service_manager::Service,
public mojom::AssistantPlatform, public mojom::AssistantPlatform,
public ash::mojom::VoiceInteractionObserver { public ash::mojom::VoiceInteractionObserver {
public: public:
Service(); explicit Service(
network::NetworkConnectionTracker* network_connection_tracker);
~Service() override; ~Service() override;
mojom::Client* client() { return client_.get(); } mojom::Client* client() { return client_.get(); }
...@@ -154,6 +159,8 @@ class Service : public service_manager::Service, ...@@ -154,6 +159,8 @@ class Service : public service_manager::Service,
mojo::Binding<ash::mojom::VoiceInteractionObserver> mojo::Binding<ash::mojom::VoiceInteractionObserver>
voice_interaction_observer_binding_; voice_interaction_observer_binding_;
network::NetworkConnectionTracker* network_connection_tracker_;
base::WeakPtrFactory<Service> weak_ptr_factory_; base::WeakPtrFactory<Service> weak_ptr_factory_;
DISALLOW_COPY_AND_ASSIGN(Service); DISALLOW_COPY_AND_ASSIGN(Service);
......
...@@ -177,7 +177,8 @@ class ServiceTestClient : public service_manager::test::ServiceTestClient, ...@@ -177,7 +177,8 @@ class ServiceTestClient : public service_manager::test::ServiceTestClient,
const std::string& name, const std::string& name,
service_manager::mojom::PIDReceiverPtr pid_receiver) override { service_manager::mojom::PIDReceiverPtr pid_receiver) override {
if (name == mojom::kServiceName) { if (name == mojom::kServiceName) {
auto service = std::make_unique<chromeos::assistant::Service>(); auto service = std::make_unique<chromeos::assistant::Service>(
nullptr /* network_connection_tracker */);
auto mock_timer = std::make_unique<base::OneShotTimer>( auto mock_timer = std::make_unique<base::OneShotTimer>(
mock_task_runner_->GetMockTickClock()); mock_task_runner_->GetMockTickClock());
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment