Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/sync/test/integration/wifi_credentials_helper_chromeos. h" | 5 #include "chrome/browser/sync/test/integration/wifi_credentials_helper_chromeos. h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/bind.h" | |
| 9 #include "base/files/file_path.h" | 10 #include "base/files/file_path.h" |
| 10 #include "base/logging.h" | 11 #include "base/logging.h" |
| 11 #include "chrome/browser/profiles/profile.h" | 12 #include "base/memory/scoped_ptr.h" |
| 13 #include "base/message_loop/message_loop.h" | |
| 14 #include "base/strings/stringprintf.h" | |
| 15 #include "base/values.h" | |
| 16 #include "chromeos/dbus/dbus_thread_manager.h" | |
| 17 #include "chromeos/dbus/shill_profile_client.h" | |
| 18 #include "chromeos/network/managed_network_configuration_handler.h" | |
| 12 #include "chromeos/network/network_handler.h" | 19 #include "chromeos/network/network_handler.h" |
| 13 #include "chromeos/network/network_state.h" | |
| 14 #include "chromeos/network/network_state_handler.h" | 20 #include "chromeos/network/network_state_handler.h" |
| 21 #include "components/onc/onc_constants.h" | |
| 15 #include "components/wifi_sync/network_state_helper_chromeos.h" | 22 #include "components/wifi_sync/network_state_helper_chromeos.h" |
| 16 #include "components/wifi_sync/wifi_security_class.h" | 23 #include "components/wifi_sync/wifi_credential_syncable_service_factory.h" |
| 24 #include "content/public/browser/browser_context.h" | |
| 25 | |
| 26 using wifi_sync::WifiCredential; | |
| 17 | 27 |
| 18 using WifiCredentialSet = wifi_sync::WifiCredential::CredentialSet; | 28 using WifiCredentialSet = wifi_sync::WifiCredential::CredentialSet; |
| 19 | 29 |
| 20 namespace wifi_credentials_helper { | 30 namespace wifi_credentials_helper { |
| 21 | 31 |
| 22 namespace { | 32 namespace { |
| 23 | 33 |
| 24 const char kProfilePrefix[] = "/profile/"; | 34 const char kProfilePrefix[] = "/profile/"; |
| 25 | 35 |
| 36 void LogCreateConfigurationFailure( | |
| 37 const std::string& debug_hint, | |
| 38 const std::string& /* network_config_error_message */, | |
| 39 scoped_ptr<base::DictionaryValue> /* network_config_error_data */) { | |
| 40 LOG(FATAL) << debug_hint; | |
| 41 } | |
| 42 | |
| 26 std::string ChromeOsUserHashForBrowserContext( | 43 std::string ChromeOsUserHashForBrowserContext( |
| 27 const content::BrowserContext& context) { | 44 const content::BrowserContext& context) { |
| 28 return context.GetPath().BaseName().value(); | 45 return context.GetPath().BaseName().value(); |
| 29 } | 46 } |
| 30 | 47 |
| 48 // Return value is distinct per |context|, but otherwise arbitrary. | |
| 31 std::string ShillProfilePathForBrowserContext( | 49 std::string ShillProfilePathForBrowserContext( |
| 32 const content::BrowserContext& context) { | 50 const content::BrowserContext& context) { |
| 33 return kProfilePrefix + ChromeOsUserHashForBrowserContext(context); | 51 return kProfilePrefix + ChromeOsUserHashForBrowserContext(context); |
| 34 } | 52 } |
| 35 | 53 |
| 36 chromeos::NetworkStateHandler* GetNetworkStateHandler() { | 54 ::chromeos::ShillProfileClient::TestInterface* |
| 37 DCHECK(chromeos::NetworkHandler::Get()->network_state_handler()); | 55 GetShillProfileClientTestInterface() { |
| 38 return chromeos::NetworkHandler::Get()->network_state_handler(); | 56 DCHECK(::chromeos::DBusThreadManager::Get()->GetShillProfileClient()); |
| 57 DCHECK(::chromeos::DBusThreadManager::Get()->GetShillProfileClient() | |
| 58 ->GetTestInterface()); | |
| 59 return ::chromeos::DBusThreadManager::Get()->GetShillProfileClient() | |
| 60 ->GetTestInterface(); | |
| 61 } | |
| 62 | |
| 63 ::chromeos::ManagedNetworkConfigurationHandler* | |
| 64 GetManagedNetworkConfigurationHandler() { | |
| 65 DCHECK(::chromeos::NetworkHandler::Get() | |
| 66 ->managed_network_configuration_handler()); | |
| 67 return ::chromeos::NetworkHandler::Get() | |
| 68 ->managed_network_configuration_handler(); | |
| 69 } | |
| 70 | |
| 71 ::chromeos::NetworkStateHandler* GetNetworkStateHandler() { | |
| 72 DCHECK(::chromeos::NetworkHandler::Get()->network_state_handler()); | |
| 73 return ::chromeos::NetworkHandler::Get()->network_state_handler(); | |
| 39 } | 74 } |
| 40 | 75 |
| 41 } // namespace | 76 } // namespace |
| 42 | 77 |
| 43 WifiCredentialSet GetWifiCredentialsForProfileChromeOs(const Profile* profile) { | 78 namespace chromeos { |
| 44 DCHECK(profile); | 79 |
| 45 return wifi_sync::GetWifiCredentialsForShillProfile( | 80 void SetUpChromeOs() { |
| 46 GetNetworkStateHandler(), ShillProfilePathForBrowserContext(*profile)); | 81 wifi_sync::WifiCredentialSyncableServiceFactory::GetInstance() |
| 82 ->IgnoreLoginStateForTest(); | |
| 47 } | 83 } |
| 48 | 84 |
| 85 void SetupClientForProfileChromeOs( | |
| 86 const content::BrowserContext* browser_context) { | |
| 87 DCHECK(browser_context); | |
| 88 GetShillProfileClientTestInterface() | |
| 89 ->AddProfile(ShillProfilePathForBrowserContext(*browser_context), | |
| 90 ChromeOsUserHashForBrowserContext(*browser_context)); | |
| 91 | |
| 92 const base::ListValue policy_network_configs; | |
| 93 const base::DictionaryValue policy_global_config; | |
| 94 GetManagedNetworkConfigurationHandler() | |
| 95 ->SetPolicy(onc::ONC_SOURCE_UNKNOWN, | |
| 96 ChromeOsUserHashForBrowserContext(*browser_context), | |
| 97 policy_network_configs, | |
| 98 policy_global_config); | |
| 99 } | |
| 100 | |
| 101 void AddWifiCredentialToProfileChromeOs( | |
| 102 const content::BrowserContext* browser_context, | |
| 103 const WifiCredential& credential) { | |
| 104 DCHECK(browser_context); | |
| 105 scoped_ptr<base::DictionaryValue> onc_properties = | |
| 106 credential.ToOncProperties(); | |
| 107 CHECK(onc_properties) << "Failed to generate ONC properties for " | |
| 108 << credential.ToString(); | |
| 109 GetManagedNetworkConfigurationHandler() | |
| 110 ->CreateConfiguration( | |
| 111 ChromeOsUserHashForBrowserContext(*browser_context), | |
| 112 *onc_properties, | |
| 113 ::chromeos::network_handler::StringResultCallback(), | |
| 114 base::Bind(LogCreateConfigurationFailure, | |
| 115 base::StringPrintf("Failed to add credential %s", | |
| 116 credential.ToString().c_str()))); | |
| 117 base::MessageLoop::current()->RunUntilIdle(); | |
|
mukesh agrawal
2015/02/02 20:36:40
Needed since CreateConfiguration is asynchronous.
| |
| 118 } | |
| 119 | |
| 120 WifiCredentialSet GetWifiCredentialsForProfileChromeOs( | |
| 121 const content::BrowserContext* browser_context) { | |
| 122 DCHECK(browser_context); | |
| 123 return wifi_sync::GetWifiCredentialsForShillProfile( | |
| 124 GetNetworkStateHandler(), | |
| 125 ShillProfilePathForBrowserContext(*browser_context)); | |
| 126 } | |
| 127 | |
| 128 } // namespace chromeos | |
| 129 | |
| 49 } // namespace wifi_credentials_helper | 130 } // namespace wifi_credentials_helper |
| OLD | NEW |