CacheStorage: Remove unused interfaces from CacheStorageListener
[chromium-blink-merge.git] / chromeos / network / auto_connect_handler.cc
blob9715cd1517e2a87f69998d2eeacc5f0e1e93910c
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chromeos/network/auto_connect_handler.h"
8 #include "base/bind.h"
9 #include "base/bind_helpers.h"
10 #include "base/location.h"
11 #include "base/message_loop/message_loop_proxy.h"
12 #include "base/values.h"
13 #include "chromeos/dbus/dbus_thread_manager.h"
14 #include "chromeos/dbus/shill_manager_client.h"
15 #include "chromeos/dbus/shill_service_client.h"
16 #include "chromeos/network/device_state.h"
17 #include "chromeos/network/managed_network_configuration_handler.h"
18 #include "chromeos/network/network_connection_handler.h"
19 #include "chromeos/network/network_event_log.h"
20 #include "chromeos/network/network_state.h"
21 #include "chromeos/network/network_type_pattern.h"
22 #include "dbus/object_path.h"
23 #include "third_party/cros_system_api/dbus/service_constants.h"
25 namespace chromeos {
27 AutoConnectHandler::AutoConnectHandler()
28 : client_cert_resolver_(nullptr),
29 request_best_connection_pending_(false),
30 device_policy_applied_(false),
31 user_policy_applied_(false),
32 client_certs_resolved_(false),
33 applied_autoconnect_policy_(false),
34 connect_to_best_services_after_scan_(false),
35 weak_ptr_factory_(this) {
38 AutoConnectHandler::~AutoConnectHandler() {
39 if (client_cert_resolver_)
40 client_cert_resolver_->RemoveObserver(this);
41 if (LoginState::IsInitialized())
42 LoginState::Get()->RemoveObserver(this);
43 if (network_state_handler_)
44 network_state_handler_->RemoveObserver(this, FROM_HERE);
45 if (managed_configuration_handler_)
46 managed_configuration_handler_->RemoveObserver(this);
49 void AutoConnectHandler::Init(
50 ClientCertResolver* client_cert_resolver,
51 NetworkConnectionHandler* network_connection_handler,
52 NetworkStateHandler* network_state_handler,
53 ManagedNetworkConfigurationHandler* managed_network_configuration_handler) {
54 if (LoginState::IsInitialized())
55 LoginState::Get()->AddObserver(this);
57 client_cert_resolver_ = client_cert_resolver;
58 if (client_cert_resolver_)
59 client_cert_resolver_->AddObserver(this);
61 network_connection_handler_ = network_connection_handler;
62 if (network_connection_handler_)
63 network_connection_handler_->AddObserver(this);
65 network_state_handler_ = network_state_handler;
66 if (network_state_handler_)
67 network_state_handler_->AddObserver(this, FROM_HERE);
69 managed_configuration_handler_ = managed_network_configuration_handler;
70 if (managed_configuration_handler_)
71 managed_configuration_handler_->AddObserver(this);
73 if (LoginState::IsInitialized())
74 LoggedInStateChanged();
77 void AutoConnectHandler::LoggedInStateChanged() {
78 if (!LoginState::Get()->IsUserLoggedIn())
79 return;
81 // Disconnect before connecting, to ensure that we do not disconnect a network
82 // that we just connected.
83 DisconnectIfPolicyRequires();
84 NET_LOG_DEBUG("RequestBestConnection", "User logged in");
85 RequestBestConnection();
88 void AutoConnectHandler::ConnectToNetworkRequested(
89 const std::string& /*service_path*/) {
90 // Stop any pending request to connect to the best newtork.
91 request_best_connection_pending_ = false;
94 void AutoConnectHandler::PoliciesChanged(const std::string& userhash) {
95 // Ignore user policies.
96 if (!userhash.empty())
97 return;
98 DisconnectIfPolicyRequires();
101 void AutoConnectHandler::PoliciesApplied(const std::string& userhash) {
102 if (userhash.empty())
103 device_policy_applied_ = true;
104 else
105 user_policy_applied_ = true;
107 // Request to connect to the best network only if there is at least one
108 // managed network. Otherwise only process existing requests.
109 const ManagedNetworkConfigurationHandler::GuidToPolicyMap* managed_networks =
110 managed_configuration_handler_->GetNetworkConfigsFromPolicy(userhash);
111 DCHECK(managed_networks);
112 if (!managed_networks->empty()) {
113 NET_LOG_DEBUG("RequestBestConnection", "Policy applied");
114 RequestBestConnection();
115 } else {
116 CheckBestConnection();
120 void AutoConnectHandler::ScanCompleted(const DeviceState* device) {
121 if (!connect_to_best_services_after_scan_ ||
122 device->type() != shill::kTypeWifi) {
123 return;
125 connect_to_best_services_after_scan_ = false;
126 // Request ConnectToBestServices after processing any pending DBus calls.
127 base::MessageLoopProxy::current()->PostTask(
128 FROM_HERE, base::Bind(&AutoConnectHandler::CallShillConnectToBestServices,
129 weak_ptr_factory_.GetWeakPtr()));
132 void AutoConnectHandler::ResolveRequestCompleted(
133 bool network_properties_changed) {
134 client_certs_resolved_ = true;
136 // Only request to connect to the best network if network properties were
137 // actually changed. Otherwise only process existing requests.
138 if (network_properties_changed) {
139 NET_LOG_DEBUG("RequestBestConnection",
140 "Client certificate patterns resolved");
141 RequestBestConnection();
142 } else {
143 CheckBestConnection();
147 void AutoConnectHandler::RequestBestConnection() {
148 request_best_connection_pending_ = true;
149 CheckBestConnection();
152 void AutoConnectHandler::CheckBestConnection() {
153 // Return immediately if there is currently no request pending to change to
154 // the best network.
155 if (!request_best_connection_pending_)
156 return;
158 bool policy_application_running =
159 managed_configuration_handler_->IsAnyPolicyApplicationRunning();
160 bool client_cert_resolve_task_running =
161 client_cert_resolver_->IsAnyResolveTaskRunning();
162 VLOG(2) << "device policy applied: " << device_policy_applied_
163 << "\nuser policy applied: " << user_policy_applied_
164 << "\npolicy application running: " << policy_application_running
165 << "\nclient cert patterns resolved: " << client_certs_resolved_
166 << "\nclient cert resolve task running: "
167 << client_cert_resolve_task_running;
168 if (!device_policy_applied_ || policy_application_running ||
169 client_cert_resolve_task_running) {
170 return;
173 if (LoginState::Get()->IsUserLoggedIn()) {
174 // Before changing connection after login, we wait at least for:
175 // - user policy applied at least once
176 // - client certificate patterns resolved
177 if (!user_policy_applied_ || !client_certs_resolved_)
178 return;
181 request_best_connection_pending_ = false;
183 // Trigger a ConnectToBestNetwork request after the next scan completion.
184 // Note: there is an edge case here if a scan is in progress and a hidden
185 // network has been configured since the scan started. crbug.com/433075.
186 if (connect_to_best_services_after_scan_)
187 return;
188 connect_to_best_services_after_scan_ = true;
189 if (!network_state_handler_->GetScanningByType(
190 NetworkTypePattern::Primitive(shill::kTypeWifi))) {
191 network_state_handler_->RequestScan();
195 void AutoConnectHandler::DisconnectIfPolicyRequires() {
196 if (applied_autoconnect_policy_ || !LoginState::Get()->IsUserLoggedIn())
197 return;
199 const base::DictionaryValue* global_network_config =
200 managed_configuration_handler_->GetGlobalConfigFromPolicy(
201 std::string() /* no username hash, device policy */);
203 if (!global_network_config)
204 return; // Device policy is not set, yet.
206 applied_autoconnect_policy_ = true;
208 bool only_policy_autoconnect = false;
209 global_network_config->GetBooleanWithoutPathExpansion(
210 ::onc::global_network_config::kAllowOnlyPolicyNetworksToAutoconnect,
211 &only_policy_autoconnect);
213 if (only_policy_autoconnect)
214 DisconnectFromUnmanagedSharedWiFiNetworks();
217 void AutoConnectHandler::DisconnectFromUnmanagedSharedWiFiNetworks() {
218 NET_LOG_DEBUG("DisconnectFromUnmanagedSharedWiFiNetworks", "");
220 NetworkStateHandler::NetworkStateList networks;
221 network_state_handler_->GetVisibleNetworkListByType(
222 NetworkTypePattern::Wireless(), &networks);
223 for (const NetworkState* network : networks) {
224 if (!(network->IsConnectingState() || network->IsConnectedState()))
225 break; // Connected and connecting networks are listed first.
227 if (network->IsPrivate())
228 continue;
230 const bool network_is_policy_managed =
231 !network->profile_path().empty() && !network->guid().empty() &&
232 managed_configuration_handler_->FindPolicyByGuidAndProfile(
233 network->guid(), network->profile_path());
234 if (network_is_policy_managed)
235 continue;
237 NET_LOG_EVENT("Disconnect Forced by Policy", network->path());
238 DBusThreadManager::Get()->GetShillServiceClient()->Disconnect(
239 dbus::ObjectPath(network->path()), base::Bind(&base::DoNothing),
240 base::Bind(&network_handler::ShillErrorCallbackFunction,
241 "AutoConnectHandler.Disconnect failed", network->path(),
242 network_handler::ErrorCallback()));
246 void AutoConnectHandler::CallShillConnectToBestServices() const {
247 NET_LOG_EVENT("ConnectToBestServices", "");
248 DBusThreadManager::Get()->GetShillManagerClient()->ConnectToBestServices(
249 base::Bind(&base::DoNothing),
250 base::Bind(&network_handler::ShillErrorCallbackFunction,
251 "ConnectToBestServices Failed",
252 "", network_handler::ErrorCallback()));
255 } // namespace chromeos