1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
ash / components / arc / net / arc_wifi_host_impl.cc [blame]
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ash/components/arc/net/arc_wifi_host_impl.h"
#include "ash/components/arc/arc_browser_context_keyed_service_factory_base.h"
#include "ash/components/arc/net/arc_net_utils.h"
#include "ash/components/arc/session/arc_bridge_service.h"
#include "base/memory/singleton.h"
#include "chromeos/ash/components/network/network_event_log.h"
#include "chromeos/ash/components/network/network_handler.h"
#include "chromeos/ash/components/network/network_state_handler.h"
#include "chromeos/ash/components/network/network_type_pattern.h"
#include "chromeos/ash/components/network/onc/network_onc_utils.h"
#include "chromeos/ash/components/network/technology_state_controller.h"
namespace {
constexpr int kGetScanResultsListLimit = 100;
ash::NetworkStateHandler* GetStateHandler() {
return ash::NetworkHandler::Get()->network_state_handler();
}
ash::TechnologyStateController* GetTechnologyStateController() {
return ash::NetworkHandler::Get()->technology_state_controller();
}
} // namespace
namespace arc {
namespace {
// Singleton factory for ArcWifiHostImpl.
class ArcWifiHostImplFactory
: public internal::ArcBrowserContextKeyedServiceFactoryBase<
ArcWifiHostImpl,
ArcWifiHostImplFactory> {
public:
// Factory name used by ArcBrowserContextKeyedServiceFactoryBase.
static constexpr const char* kName = "ArcWifiHostImplFactory";
static ArcWifiHostImplFactory* GetInstance() {
return base::Singleton<ArcWifiHostImplFactory>::get();
}
private:
friend base::DefaultSingletonTraits<ArcWifiHostImplFactory>;
ArcWifiHostImplFactory() = default;
~ArcWifiHostImplFactory() override = default;
};
} // namespace
// static
ArcWifiHostImpl* ArcWifiHostImpl::GetForBrowserContext(
content::BrowserContext* context) {
return ArcWifiHostImplFactory::GetForBrowserContext(context);
}
// static
ArcWifiHostImpl* ArcWifiHostImpl::GetForBrowserContextForTesting(
content::BrowserContext* context) {
return ArcWifiHostImplFactory::GetForBrowserContextForTesting(context);
}
ArcWifiHostImpl::ArcWifiHostImpl(content::BrowserContext* context,
ArcBridgeService* bridge_service)
: arc_bridge_service_(bridge_service) {
arc_bridge_service_->arc_wifi()->SetHost(this);
arc_bridge_service_->arc_wifi()->AddObserver(this);
}
ArcWifiHostImpl::~ArcWifiHostImpl() {
DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
arc_bridge_service_->arc_wifi()->RemoveObserver(this);
arc_bridge_service_->arc_wifi()->SetHost(nullptr);
}
// static
void ArcWifiHostImpl::EnsureFactoryBuilt() {
ArcWifiHostImplFactory::GetInstance();
}
void ArcWifiHostImpl::GetWifiEnabledState(
GetWifiEnabledStateCallback callback) {
bool is_enabled =
GetStateHandler()->IsTechnologyEnabled(ash::NetworkTypePattern::WiFi());
std::move(callback).Run(is_enabled);
}
void ArcWifiHostImpl::SetWifiEnabledState(
bool is_enabled,
SetWifiEnabledStateCallback callback) {
DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
auto state =
GetStateHandler()->GetTechnologyState(ash::NetworkTypePattern::WiFi());
// WiFi can't be enabled or disabled in these states.
switch (state) {
case ash::NetworkStateHandler::TECHNOLOGY_PROHIBITED:
case ash::NetworkStateHandler::TECHNOLOGY_UNINITIALIZED:
case ash::NetworkStateHandler::TECHNOLOGY_UNAVAILABLE:
// If WiFi is in above state, it is already disabled. This is a noop.
if (!is_enabled) {
std::move(callback).Run(true);
return;
}
NET_LOG(ERROR) << __func__ << ": failed due to WiFi state: " << state;
std::move(callback).Run(false);
return;
default:
break;
}
NET_LOG(USER) << __func__ << ": " << is_enabled;
GetTechnologyStateController()->SetTechnologiesEnabled(
ash::NetworkTypePattern::WiFi(), is_enabled,
ash::network_handler::ErrorCallback());
std::move(callback).Run(true);
}
void ArcWifiHostImpl::StartScan() {
GetStateHandler()->RequestScan(ash::NetworkTypePattern::WiFi());
}
void ArcWifiHostImpl::ScanCompleted(const ash::DeviceState* /*unused*/) {
auto* arc_wifi_instance = ARC_GET_INSTANCE_FOR_METHOD(
arc_bridge_service_->arc_wifi(), ScanCompleted);
if (!arc_wifi_instance) {
return;
}
arc_wifi_instance->ScanCompleted();
}
void ArcWifiHostImpl::GetScanResults(GetScanResultsCallback callback) {
ash::NetworkTypePattern network_pattern =
ash::onc::NetworkTypePatternFromOncType(onc::network_type::kWiFi);
ash::NetworkStateHandler::NetworkStateList network_states;
GetStateHandler()->GetNetworkListByType(
network_pattern, /*configured_only=*/false, /*visible_only=*/true,
kGetScanResultsListLimit, &network_states);
std::move(callback).Run(net_utils::TranslateScanResults(network_states));
}
void ArcWifiHostImpl::OnConnectionReady() {
DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
if (ash::NetworkHandler::IsInitialized()) {
GetStateHandler()->AddObserver(this, FROM_HERE);
}
}
void ArcWifiHostImpl::OnConnectionClosed() {
GetStateHandler()->RemoveObserver(this, FROM_HERE);
}
void ArcWifiHostImpl::OnShuttingDown() {
GetStateHandler()->RemoveObserver(this, FROM_HERE);
}
void ArcWifiHostImpl::DeviceListChanged() {
auto* arc_wifi_instance = ARC_GET_INSTANCE_FOR_METHOD(
arc_bridge_service_->arc_wifi(), WifiEnabledStateChanged);
if (!arc_wifi_instance) {
return;
}
bool is_enabled =
GetStateHandler()->IsTechnologyEnabled(ash::NetworkTypePattern::WiFi());
arc_wifi_instance->WifiEnabledStateChanged(is_enabled);
}
} // namespace arc