Implement ScreenOrientationDelegate on ChromeOS
[chromium-blink-merge.git] / chromeos / network / auto_connect_handler.cc
blob37f0344fa1b9fe792178ebb6bad3999f16d913a7
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"
7 #include <string>
9 #include "base/bind.h"
10 #include "base/bind_helpers.h"
11 #include "base/values.h"
12 #include "chromeos/dbus/dbus_thread_manager.h"
13 #include "chromeos/dbus/shill_service_client.h"
14 #include "chromeos/network/managed_network_configuration_handler.h"
15 #include "chromeos/network/network_event_log.h"
16 #include "chromeos/network/network_state.h"
17 #include "dbus/object_path.h"
19 namespace chromeos {
21 AutoConnectHandler::AutoConnectHandler()
22 : client_cert_resolver_(nullptr),
23 request_best_connection_pending_(false),
24 device_policy_applied_(false),
25 user_policy_applied_(false),
26 client_certs_resolved_(false),
27 applied_autoconnect_policy_(false) {
30 AutoConnectHandler::~AutoConnectHandler() {
31 if (client_cert_resolver_)
32 client_cert_resolver_->RemoveObserver(this);
33 if (LoginState::IsInitialized())
34 LoginState::Get()->RemoveObserver(this);
35 if (managed_configuration_handler_)
36 managed_configuration_handler_->RemoveObserver(this);
39 void AutoConnectHandler::Init(
40 ClientCertResolver* client_cert_resolver,
41 NetworkConnectionHandler* network_connection_handler,
42 NetworkStateHandler* network_state_handler,
43 ManagedNetworkConfigurationHandler* managed_network_configuration_handler) {
44 if (LoginState::IsInitialized())
45 LoginState::Get()->AddObserver(this);
47 client_cert_resolver_ = client_cert_resolver;
48 if (client_cert_resolver_)
49 client_cert_resolver_->AddObserver(this);
51 network_connection_handler_ = network_connection_handler;
52 if (network_connection_handler_)
53 network_connection_handler_->AddObserver(this);
55 network_state_handler_ = network_state_handler;
57 if (managed_network_configuration_handler) {
58 managed_configuration_handler_ = managed_network_configuration_handler;
59 managed_configuration_handler_->AddObserver(this);
62 if (LoginState::IsInitialized())
63 LoggedInStateChanged();
66 void AutoConnectHandler::LoggedInStateChanged() {
67 if (!LoginState::Get()->IsUserLoggedIn())
68 return;
70 // Disconnect before connecting, to ensure that we do not disconnect a network
71 // that we just connected.
72 DisconnectIfPolicyRequires();
73 NET_LOG_DEBUG("RequestBestConnection", "User logged in");
74 RequestBestConnection();
77 void AutoConnectHandler::ConnectToNetworkRequested(
78 const std::string& /*service_path*/) {
79 // Stop any pending request to connect to the best newtork.
80 request_best_connection_pending_ = false;
83 void AutoConnectHandler::PoliciesChanged(const std::string& userhash) {
84 // Ignore user policies.
85 if (!userhash.empty())
86 return;
87 DisconnectIfPolicyRequires();
90 void AutoConnectHandler::PoliciesApplied(const std::string& userhash) {
91 if (userhash.empty())
92 device_policy_applied_ = true;
93 else
94 user_policy_applied_ = true;
96 // Request to connect to the best network only if there is at least one
97 // managed network. Otherwise only process existing requests.
98 const ManagedNetworkConfigurationHandler::GuidToPolicyMap* managed_networks =
99 managed_configuration_handler_->GetNetworkConfigsFromPolicy(userhash);
100 DCHECK(managed_networks);
101 if (!managed_networks->empty()) {
102 NET_LOG_DEBUG("RequestBestConnection", "Policy applied");
103 RequestBestConnection();
104 } else {
105 CheckBestConnection();
109 void AutoConnectHandler::ResolveRequestCompleted(
110 bool network_properties_changed) {
111 client_certs_resolved_ = true;
113 // Only request to connect to the best network if network properties were
114 // actually changed. Otherwise only process existing requests.
115 if (network_properties_changed) {
116 NET_LOG_DEBUG("RequestBestConnection",
117 "Client certificate patterns resolved");
118 RequestBestConnection();
119 } else {
120 CheckBestConnection();
124 void AutoConnectHandler::RequestBestConnection() {
125 request_best_connection_pending_ = true;
126 CheckBestConnection();
129 void AutoConnectHandler::CheckBestConnection() {
130 // Return immediately if there is currently no request pending to change to
131 // the best network.
132 if (!request_best_connection_pending_)
133 return;
135 bool policy_application_running =
136 managed_configuration_handler_->IsAnyPolicyApplicationRunning();
137 bool client_cert_resolve_task_running =
138 client_cert_resolver_->IsAnyResolveTaskRunning();
139 VLOG(2) << "device policy applied: " << device_policy_applied_
140 << "\nuser policy applied: " << user_policy_applied_
141 << "\npolicy application running: " << policy_application_running
142 << "\nclient cert patterns resolved: " << client_certs_resolved_
143 << "\nclient cert resolve task running: "
144 << client_cert_resolve_task_running;
145 if (!device_policy_applied_ || policy_application_running ||
146 client_cert_resolve_task_running) {
147 return;
150 if (LoginState::Get()->IsUserLoggedIn()) {
151 // Before changing connection after login, we wait at least for:
152 // - user policy applied at least once
153 // - client certificate patterns resolved
154 if (!user_policy_applied_ || !client_certs_resolved_)
155 return;
158 request_best_connection_pending_ = false;
159 NET_LOG_EVENT("ConnectToBestWifiNetwork", "");
160 network_state_handler_->ConnectToBestWifiNetwork();
163 void AutoConnectHandler::DisconnectIfPolicyRequires() {
164 if (applied_autoconnect_policy_ || !LoginState::Get()->IsUserLoggedIn())
165 return;
167 const base::DictionaryValue* global_network_config =
168 managed_configuration_handler_->GetGlobalConfigFromPolicy(
169 std::string() /* no username hash, device policy */);
171 if (!global_network_config)
172 return; // Device policy is not set, yet.
174 applied_autoconnect_policy_ = true;
176 bool only_policy_autoconnect = false;
177 global_network_config->GetBooleanWithoutPathExpansion(
178 ::onc::global_network_config::kAllowOnlyPolicyNetworksToAutoconnect,
179 &only_policy_autoconnect);
181 if (only_policy_autoconnect)
182 DisconnectFromUnmanagedSharedWiFiNetworks();
185 void AutoConnectHandler::DisconnectFromUnmanagedSharedWiFiNetworks() {
186 NET_LOG_DEBUG("DisconnectFromUnmanagedSharedWiFiNetworks", "");
188 NetworkStateHandler::NetworkStateList networks;
189 network_state_handler_->GetVisibleNetworkListByType(
190 NetworkTypePattern::Wireless(), &networks);
191 for (const NetworkState* network : networks) {
192 if (!(network->IsConnectingState() || network->IsConnectedState()))
193 break; // Connected and connecting networks are listed first.
195 if (network->IsPrivate())
196 continue;
198 const bool network_is_policy_managed =
199 !network->profile_path().empty() && !network->guid().empty() &&
200 managed_configuration_handler_->FindPolicyByGuidAndProfile(
201 network->guid(), network->profile_path());
202 if (network_is_policy_managed)
203 continue;
205 NET_LOG_EVENT("Disconnect Forced by Policy", network->path());
206 DBusThreadManager::Get()->GetShillServiceClient()->Disconnect(
207 dbus::ObjectPath(network->path()), base::Bind(&base::DoNothing),
208 base::Bind(&network_handler::ShillErrorCallbackFunction,
209 "AutoConnectHandler.Disconnect failed", network->path(),
210 network_handler::ErrorCallback()));
214 } // namespace chromeos