Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2013 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. | |
| 4 | |
| 5 #include "chrome/browser/extensions/api/networking_private/networking_private_ch romeos.h" | |
| 6 | |
| 7 #include "base/bind.h" | |
| 8 #include "base/bind_helpers.h" | |
| 9 #include "base/callback.h" | |
| 10 #include "chrome/browser/chromeos/profiles/profile_helper.h" | |
| 11 #include "chrome/browser/extensions/api/networking_private/networking_private_ap i.h" | |
| 12 #include "chrome/common/extensions/api/networking_private.h" | |
| 13 #include "chromeos/dbus/dbus_thread_manager.h" | |
| 14 #include "chromeos/dbus/shill_manager_client.h" | |
| 15 #include "chromeos/login/login_state.h" | |
| 16 #include "chromeos/network/managed_network_configuration_handler.h" | |
| 17 #include "chromeos/network/network_connection_handler.h" | |
| 18 #include "chromeos/network/network_device_handler.h" | |
| 19 #include "chromeos/network/network_event_log.h" | |
| 20 #include "chromeos/network/network_state.h" | |
| 21 #include "chromeos/network/network_state_handler.h" | |
| 22 #include "chromeos/network/network_util.h" | |
| 23 #include "chromeos/network/onc/onc_signature.h" | |
| 24 #include "chromeos/network/onc/onc_translator.h" | |
| 25 #include "chromeos/network/onc/onc_utils.h" | |
| 26 #include "chromeos/network/portal_detector/network_portal_detector.h" | |
| 27 #include "components/onc/onc_constants.h" | |
| 28 #include "content/public/browser/browser_context.h" | |
| 29 | |
| 30 using chromeos::NetworkHandler; | |
| 31 using chromeos::NetworkTypePattern; | |
| 32 using chromeos::ShillManagerClient; | |
| 33 using extensions::NetworkingPrivateDelegate; | |
| 34 | |
| 35 namespace { | |
| 36 | |
| 37 chromeos::ShillManagerClient* GetShillManagerClient() { | |
| 38 return chromeos::DBusThreadManager::Get()->GetShillManagerClient(); | |
| 39 } | |
| 40 | |
| 41 chromeos::NetworkStateHandler* GetStateHandler() { | |
| 42 return NetworkHandler::Get()->network_state_handler(); | |
| 43 } | |
| 44 | |
| 45 chromeos::ManagedNetworkConfigurationHandler* GetManagedConfigurationHandler() { | |
| 46 return NetworkHandler::Get()->managed_network_configuration_handler(); | |
| 47 } | |
| 48 | |
| 49 ShillManagerClient::VerificationProperties ConvertVerificationProperties( | |
| 50 const base::DictionaryValue& input) { | |
| 51 ShillManagerClient::VerificationProperties output; | |
| 52 // These keys are described in networking_private.json but not defined | |
| 53 // anywhere as constants. | |
| 54 input.GetStringWithoutPathExpansion("certificate", &output.certificate); | |
|
pneubeck (no reviews)
2014/07/14 21:33:57
IMO, a layer violation
stevenjb
2014/07/15 00:33:40
Acknowledged.
| |
| 55 input.GetStringWithoutPathExpansion("publicKey", &output.public_key); | |
| 56 input.GetStringWithoutPathExpansion("nonce", &output.nonce); | |
| 57 input.GetStringWithoutPathExpansion("signedData", &output.signed_data); | |
| 58 input.GetStringWithoutPathExpansion("deviceSerial", &output.device_serial); | |
| 59 input.GetStringWithoutPathExpansion("deviceSsid", &output.device_ssid); | |
| 60 input.GetStringWithoutPathExpansion("deviceBssid", &output.device_bssid); | |
| 61 return output; | |
| 62 } | |
| 63 | |
| 64 bool GetServicePathFromGuid(const std::string& guid, | |
| 65 std::string* service_path, | |
| 66 std::string* error) { | |
| 67 const chromeos::NetworkState* network = | |
| 68 GetStateHandler()->GetNetworkStateFromGuid(guid); | |
| 69 if (!network) { | |
| 70 *error = extensions::networking_private::kErrorInvalidNetworkGuid; | |
|
pneubeck (no reviews)
2014/07/14 21:33:57
again, I think, a layer violation.
Not so, if the
stevenjb
2014/07/15 00:33:40
See meta-comment.
| |
| 71 return false; | |
| 72 } | |
| 73 *service_path = network->path(); | |
| 74 return true; | |
| 75 } | |
| 76 | |
| 77 bool GetUserIdHash(content::BrowserContext* browser_context, | |
| 78 std::string* user_hash, | |
| 79 std::string* error) { | |
| 80 std::string profile_user_hash = | |
| 81 chromeos::ProfileHelper::GetUserIdHashFromProfile( | |
|
pneubeck (no reviews)
2014/07/14 21:33:58
this dependency should be in the factory, to keep
stevenjb
2014/07/15 00:33:39
Hmm, I'm not convinced of the value in that. This
pneubeck (no reviews)
2014/07/15 09:56:28
Moving dependencies to a factory is not only helpf
| |
| 82 static_cast<Profile*>(browser_context)); | |
| 83 | |
| 84 // Currently Chrome OS only configures networks for the primary user. | |
| 85 // Configuration attempts from other browser contexts should fail. | |
| 86 // TODO(stevenjb): use an ExtensionsBrowserClient method to access | |
| 87 // ProfileHelper when moving this to src/extensions. | |
| 88 if (profile_user_hash != chromeos::LoginState::Get()->primary_user_hash()) { | |
| 89 // Disallow class requiring a user id hash from a non-primary user context | |
| 90 // to avoid complexites with the policy code. | |
|
pneubeck (no reviews)
2014/07/14 21:33:57
complications or complexities?
stevenjb
2014/07/15 00:33:39
Done.
| |
| 91 LOG(ERROR) << "networkingPrivate API call from non primary user: " | |
| 92 << profile_user_hash; | |
| 93 *error = "Error.NonPrimaryUser"; | |
| 94 return false; | |
| 95 } | |
| 96 *user_hash = profile_user_hash; | |
| 97 return true; | |
| 98 } | |
| 99 | |
| 100 void NetworkHandlerDictionaryCallback( | |
| 101 const NetworkingPrivateDelegate::DictionaryCallback& callback, | |
| 102 const std::string& service_path, | |
| 103 const base::DictionaryValue& dictionary) { | |
| 104 scoped_ptr<base::DictionaryValue> dictionary_copy(dictionary.DeepCopy()); | |
| 105 callback.Run(dictionary_copy.Pass()); | |
| 106 } | |
| 107 | |
| 108 void NetworkHandlerFailureCallback( | |
| 109 const NetworkingPrivateDelegate::FailureCallback& callback, | |
| 110 const std::string& error_name, | |
| 111 scoped_ptr<base::DictionaryValue> error_data) { | |
| 112 callback.Run(error_name); | |
| 113 } | |
| 114 | |
| 115 void ShillFailureCallback( | |
| 116 const NetworkingPrivateDelegate::FailureCallback& callback, | |
| 117 const std::string& error_name, | |
| 118 const std::string& error_message) { | |
| 119 callback.Run(error_name); | |
| 120 } | |
| 121 | |
| 122 } // namespace | |
| 123 | |
| 124 //////////////////////////////////////////////////////////////////////////////// | |
| 125 | |
| 126 namespace extensions { | |
| 127 | |
| 128 NetworkingPrivateChromeOS::NetworkingPrivateChromeOS( | |
| 129 content::BrowserContext* browser_context) | |
| 130 : browser_context_(browser_context) { | |
|
pneubeck (no reviews)
2014/07/14 21:33:58
as mentioned above, please get the user hash once
stevenjb
2014/07/15 00:33:40
Acknowledged.
| |
| 131 } | |
| 132 | |
| 133 NetworkingPrivateChromeOS::~NetworkingPrivateChromeOS() {} | |
| 134 | |
| 135 void NetworkingPrivateChromeOS::GetProperties( | |
| 136 const std::string& guid, | |
| 137 const DictionaryCallback& success_callback, | |
| 138 const FailureCallback& failure_callback) { | |
| 139 std::string service_path, error; | |
| 140 if (!GetServicePathFromGuid(guid, &service_path, &error)) { | |
| 141 failure_callback.Run(error); | |
| 142 return; | |
| 143 } | |
| 144 | |
| 145 GetManagedConfigurationHandler()->GetProperties( | |
| 146 service_path, | |
| 147 base::Bind(&NetworkHandlerDictionaryCallback, success_callback), | |
| 148 base::Bind(&NetworkHandlerFailureCallback, failure_callback)); | |
| 149 } | |
| 150 | |
| 151 void NetworkingPrivateChromeOS::GetManagedProperties( | |
| 152 const std::string& guid, | |
| 153 const DictionaryCallback& success_callback, | |
| 154 const FailureCallback& failure_callback) { | |
| 155 std::string service_path, error; | |
| 156 if (!GetServicePathFromGuid(guid, &service_path, &error)) { | |
| 157 failure_callback.Run(error); | |
| 158 return; | |
| 159 } | |
| 160 | |
| 161 std::string user_id_hash; | |
| 162 if (!GetUserIdHash(browser_context_, &user_id_hash, &error)) { | |
| 163 failure_callback.Run(error); | |
| 164 return; | |
| 165 } | |
| 166 | |
| 167 GetManagedConfigurationHandler()->GetManagedProperties( | |
| 168 user_id_hash, | |
| 169 service_path, | |
| 170 base::Bind(&NetworkHandlerDictionaryCallback, success_callback), | |
| 171 base::Bind(&NetworkHandlerFailureCallback, failure_callback)); | |
| 172 } | |
| 173 | |
| 174 void NetworkingPrivateChromeOS::GetState( | |
| 175 const std::string& guid, | |
| 176 const DictionaryCallback& success_callback, | |
| 177 const FailureCallback& failure_callback) { | |
| 178 std::string service_path, error; | |
| 179 if (!GetServicePathFromGuid(guid, &service_path, &error)) { | |
| 180 failure_callback.Run(error); | |
| 181 return; | |
| 182 } | |
| 183 | |
| 184 const chromeos::NetworkState* network_state = | |
| 185 GetStateHandler()->GetNetworkStateFromServicePath( | |
| 186 service_path, false /* configured_only */); | |
| 187 if (!network_state) { | |
| 188 failure_callback.Run(networking_private::kErrorNetworkUnavailable); | |
| 189 return; | |
| 190 } | |
| 191 | |
| 192 scoped_ptr<base::DictionaryValue> network_properties = | |
| 193 chromeos::network_util::TranslateNetworkStateToONC(network_state); | |
| 194 | |
| 195 success_callback.Run(network_properties.Pass()); | |
| 196 } | |
| 197 | |
| 198 void NetworkingPrivateChromeOS::SetProperties( | |
| 199 const std::string& guid, | |
| 200 scoped_ptr<base::DictionaryValue> properties, | |
| 201 const VoidCallback& success_callback, | |
| 202 const FailureCallback& failure_callback) { | |
| 203 std::string service_path, error; | |
| 204 if (!GetServicePathFromGuid(guid, &service_path, &error)) { | |
| 205 failure_callback.Run(error); | |
| 206 return; | |
| 207 } | |
| 208 | |
| 209 GetManagedConfigurationHandler()->SetProperties( | |
| 210 service_path, | |
| 211 *properties, | |
| 212 success_callback, | |
| 213 base::Bind(&NetworkHandlerFailureCallback, failure_callback)); | |
| 214 } | |
| 215 | |
| 216 void NetworkingPrivateChromeOS::CreateNetwork( | |
| 217 bool shared, | |
| 218 scoped_ptr<base::DictionaryValue> properties, | |
| 219 const StringCallback& success_callback, | |
| 220 const FailureCallback& failure_callback) { | |
| 221 std::string user_id_hash, error; | |
| 222 if (!shared && !GetUserIdHash(browser_context_, &user_id_hash, &error)) { | |
| 223 failure_callback.Run(error); | |
|
pneubeck (no reviews)
2014/07/14 21:33:58
optional nit:
the old comment was no so bad:
-
stevenjb
2014/07/15 00:33:39
Done.
| |
| 224 return; | |
| 225 } | |
| 226 | |
| 227 GetManagedConfigurationHandler()->CreateConfiguration( | |
| 228 user_id_hash, | |
| 229 *properties, | |
| 230 success_callback, | |
| 231 base::Bind(&NetworkHandlerFailureCallback, failure_callback)); | |
| 232 } | |
| 233 | |
| 234 void NetworkingPrivateChromeOS::GetNetworks( | |
| 235 const std::string& network_type, | |
| 236 bool configured_only, | |
| 237 bool visible_only, | |
| 238 int limit, | |
| 239 const NetworkListCallback& success_callback, | |
| 240 const FailureCallback& failure_callback) { | |
| 241 NetworkTypePattern pattern = | |
| 242 chromeos::onc::NetworkTypePatternFromOncType(network_type); | |
| 243 scoped_ptr<base::ListValue> network_properties_list = | |
| 244 chromeos::network_util::TranslateNetworkListToONC( | |
| 245 pattern, configured_only, visible_only, limit, false /* debugging */); | |
| 246 success_callback.Run(network_properties_list.Pass()); | |
| 247 } | |
| 248 | |
| 249 void NetworkingPrivateChromeOS::StartConnect( | |
| 250 const std::string& guid, | |
| 251 const VoidCallback& success_callback, | |
| 252 const FailureCallback& failure_callback) { | |
| 253 std::string service_path, error; | |
| 254 if (!GetServicePathFromGuid(guid, &service_path, &error)) { | |
| 255 failure_callback.Run(error); | |
| 256 return; | |
| 257 } | |
| 258 | |
| 259 const bool check_error_state = false; | |
| 260 NetworkHandler::Get()->network_connection_handler()->ConnectToNetwork( | |
| 261 service_path, | |
| 262 success_callback, | |
| 263 base::Bind(&NetworkHandlerFailureCallback, failure_callback), | |
| 264 check_error_state); | |
| 265 } | |
| 266 | |
| 267 void NetworkingPrivateChromeOS::StartDisconnect( | |
| 268 const std::string& guid, | |
| 269 const VoidCallback& success_callback, | |
| 270 const FailureCallback& failure_callback) { | |
| 271 std::string service_path, error; | |
| 272 if (!GetServicePathFromGuid(guid, &service_path, &error)) { | |
| 273 failure_callback.Run(error); | |
| 274 return; | |
| 275 } | |
| 276 | |
| 277 NetworkHandler::Get()->network_connection_handler()->DisconnectNetwork( | |
| 278 service_path, | |
| 279 success_callback, | |
| 280 base::Bind(&NetworkHandlerFailureCallback, failure_callback)); | |
| 281 } | |
| 282 | |
| 283 void NetworkingPrivateChromeOS::VerifyDestination( | |
| 284 scoped_ptr<base::DictionaryValue> verification_properties, | |
| 285 const BoolCallback& success_callback, | |
| 286 const FailureCallback& failure_callback) { | |
| 287 ShillManagerClient::VerificationProperties verification_property_struct = | |
| 288 ConvertVerificationProperties(*verification_properties); | |
| 289 | |
| 290 GetShillManagerClient()->VerifyDestination( | |
| 291 verification_property_struct, | |
| 292 success_callback, | |
| 293 base::Bind(&ShillFailureCallback, failure_callback)); | |
| 294 } | |
| 295 | |
| 296 void NetworkingPrivateChromeOS::VerifyAndEncryptCredentials( | |
| 297 const std::string& guid, | |
| 298 scoped_ptr<base::DictionaryValue> verification_properties, | |
| 299 const StringCallback& success_callback, | |
| 300 const FailureCallback& failure_callback) { | |
| 301 std::string service_path, error; | |
| 302 if (!GetServicePathFromGuid(guid, &service_path, &error)) { | |
| 303 failure_callback.Run(error); | |
| 304 return; | |
| 305 } | |
| 306 | |
| 307 ShillManagerClient::VerificationProperties verification_property_struct = | |
| 308 ConvertVerificationProperties(*verification_properties); | |
| 309 | |
| 310 GetShillManagerClient()->VerifyAndEncryptCredentials( | |
| 311 verification_property_struct, | |
| 312 service_path, | |
| 313 success_callback, | |
| 314 base::Bind(&ShillFailureCallback, failure_callback)); | |
| 315 } | |
| 316 | |
| 317 void NetworkingPrivateChromeOS::VerifyAndEncryptData( | |
| 318 scoped_ptr<base::DictionaryValue> verification_properties, | |
| 319 const std::string& data, | |
| 320 const StringCallback& success_callback, | |
| 321 const FailureCallback& failure_callback) { | |
| 322 ShillManagerClient::VerificationProperties verification_property_struct = | |
| 323 ConvertVerificationProperties(*verification_properties); | |
| 324 | |
| 325 GetShillManagerClient()->VerifyAndEncryptData( | |
| 326 verification_property_struct, | |
| 327 data, | |
| 328 success_callback, | |
| 329 base::Bind(&ShillFailureCallback, failure_callback)); | |
| 330 } | |
| 331 | |
| 332 void NetworkingPrivateChromeOS::SetWifiTDLSEnabledState( | |
| 333 const std::string& ip_or_mac_address, | |
| 334 bool enabled, | |
| 335 const StringCallback& success_callback, | |
| 336 const FailureCallback& failure_callback) { | |
| 337 NetworkHandler::Get()->network_device_handler()->SetWifiTDLSEnabled( | |
| 338 ip_or_mac_address, | |
| 339 enabled, | |
| 340 success_callback, | |
| 341 base::Bind(&NetworkHandlerFailureCallback, failure_callback)); | |
| 342 } | |
| 343 | |
| 344 void NetworkingPrivateChromeOS::GetWifiTDLSStatus( | |
| 345 const std::string& ip_or_mac_address, | |
| 346 const StringCallback& success_callback, | |
| 347 const FailureCallback& failure_callback) { | |
| 348 NetworkHandler::Get()->network_device_handler()->GetWifiTDLSStatus( | |
| 349 ip_or_mac_address, | |
| 350 success_callback, | |
| 351 base::Bind(&NetworkHandlerFailureCallback, failure_callback)); | |
| 352 } | |
| 353 | |
| 354 void NetworkingPrivateChromeOS::GetCaptivePortalStatus( | |
| 355 const std::string& guid, | |
| 356 const StringCallback& success_callback, | |
| 357 const FailureCallback& failure_callback) { | |
| 358 std::string service_path, error; | |
| 359 if (!GetServicePathFromGuid(guid, &service_path, &error)) { | |
| 360 failure_callback.Run(error); | |
| 361 return; | |
| 362 } | |
| 363 | |
| 364 chromeos::NetworkPortalDetector* detector = | |
| 365 chromeos::NetworkPortalDetector::Get(); | |
|
pneubeck (no reviews)
2014/07/14 21:33:57
should be passed in by the factory through the con
stevenjb
2014/07/15 00:33:40
See comment above.
| |
| 366 if (!detector) { | |
| 367 failure_callback.Run(networking_private::kErrorNotReady); | |
| 368 return; | |
| 369 } | |
| 370 | |
| 371 chromeos::NetworkPortalDetector::CaptivePortalState state = | |
| 372 detector->GetCaptivePortalState(service_path); | |
| 373 success_callback.Run( | |
| 374 chromeos::NetworkPortalDetector::CaptivePortalStatusString(state.status)); | |
| 375 } | |
| 376 | |
| 377 scoped_ptr<base::ListValue> | |
| 378 NetworkingPrivateChromeOS::GetEnabledNetworkTypes() { | |
| 379 chromeos::NetworkStateHandler* state_handler = GetStateHandler(); | |
| 380 | |
| 381 scoped_ptr<base::ListValue> network_list(new base::ListValue); | |
| 382 | |
| 383 if (state_handler->IsTechnologyEnabled(NetworkTypePattern::Ethernet())) | |
| 384 network_list->AppendString(api::networking_private::ToString( | |
| 385 api::networking_private::NETWORK_TYPE_ETHERNET)); | |
|
pneubeck (no reviews)
2014/07/14 21:33:57
this translation should be in the _api.cc
stevenjb
2014/07/15 00:33:40
Done.
| |
| 386 if (state_handler->IsTechnologyEnabled(NetworkTypePattern::WiFi())) | |
| 387 network_list->AppendString(api::networking_private::ToString( | |
| 388 api::networking_private::NETWORK_TYPE_WIFI)); | |
| 389 if (state_handler->IsTechnologyEnabled(NetworkTypePattern::Wimax())) | |
| 390 network_list->AppendString(api::networking_private::ToString( | |
| 391 api::networking_private::NETWORK_TYPE_WIMAX)); | |
| 392 if (state_handler->IsTechnologyEnabled(NetworkTypePattern::Cellular())) | |
| 393 network_list->AppendString(api::networking_private::ToString( | |
| 394 api::networking_private::NETWORK_TYPE_CELLULAR)); | |
| 395 | |
| 396 return network_list.Pass(); | |
| 397 } | |
| 398 | |
| 399 bool NetworkingPrivateChromeOS::EnableNetworkType(const std::string& type) { | |
| 400 NetworkTypePattern pattern = | |
| 401 chromeos::onc::NetworkTypePatternFromOncType(type); | |
| 402 | |
| 403 GetStateHandler()->SetTechnologyEnabled( | |
| 404 pattern, true, chromeos::network_handler::ErrorCallback()); | |
| 405 | |
| 406 return true; | |
| 407 } | |
| 408 | |
| 409 bool NetworkingPrivateChromeOS::DisableNetworkType(const std::string& type) { | |
| 410 NetworkTypePattern pattern = | |
| 411 chromeos::onc::NetworkTypePatternFromOncType(type); | |
| 412 | |
| 413 GetStateHandler()->SetTechnologyEnabled( | |
| 414 pattern, false, chromeos::network_handler::ErrorCallback()); | |
| 415 | |
| 416 return true; | |
| 417 } | |
| 418 | |
| 419 bool NetworkingPrivateChromeOS::RequestScan() { | |
| 420 GetStateHandler()->RequestScan(); | |
| 421 return true; | |
| 422 } | |
| 423 | |
| 424 } // namespace extensions | |
| OLD | NEW |