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" |
| 12 #include "base/memory/scoped_ptr.h" |
| 13 #include "base/strings/stringprintf.h" |
| 14 #include "base/values.h" |
11 #include "chrome/browser/profiles/profile.h" | 15 #include "chrome/browser/profiles/profile.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 |
| 25 using wifi_sync::WifiCredential; |
17 | 26 |
18 using WifiCredentialSet = wifi_sync::WifiCredential::CredentialSet; | 27 using WifiCredentialSet = wifi_sync::WifiCredential::CredentialSet; |
19 | 28 |
20 namespace wifi_credentials_helper { | 29 namespace wifi_credentials_helper { |
21 | 30 |
22 namespace { | 31 namespace { |
23 | 32 |
24 const char kProfilePrefix[] = "/profile/"; | 33 const char kProfilePrefix[] = "/profile/"; |
25 | 34 |
| 35 void LogCreateConfigurationFailure( |
| 36 const std::string& debug_hint, |
| 37 const std::string& /* network_config_error_message */, |
| 38 scoped_ptr<base::DictionaryValue> /* network_config_error_data */) { |
| 39 LOG(FATAL) << debug_hint; |
| 40 } |
| 41 |
26 std::string ChromeOsUserHashForBrowserContext( | 42 std::string ChromeOsUserHashForBrowserContext( |
27 const content::BrowserContext& context) { | 43 const content::BrowserContext& context) { |
28 return context.GetPath().BaseName().value(); | 44 return context.GetPath().BaseName().value(); |
29 } | 45 } |
30 | 46 |
31 std::string ShillProfilePathForBrowserContext( | 47 std::string ShillProfilePathForBrowserContext( |
32 const content::BrowserContext& context) { | 48 const content::BrowserContext& context) { |
33 return kProfilePrefix + ChromeOsUserHashForBrowserContext(context); | 49 return kProfilePrefix + ChromeOsUserHashForBrowserContext(context); |
34 } | 50 } |
35 | 51 |
| 52 chromeos::ShillProfileClient::TestInterface* |
| 53 GetShillProfileClientTestInterface() { |
| 54 DCHECK(chromeos::DBusThreadManager::Get()->GetShillProfileClient()); |
| 55 DCHECK(chromeos::DBusThreadManager::Get()->GetShillProfileClient() |
| 56 ->GetTestInterface()); |
| 57 return chromeos::DBusThreadManager::Get()->GetShillProfileClient() |
| 58 ->GetTestInterface(); |
| 59 } |
| 60 |
| 61 chromeos::ManagedNetworkConfigurationHandler* |
| 62 GetManagedNetworkConfigurationHandler() { |
| 63 DCHECK(chromeos::NetworkHandler::Get() |
| 64 ->managed_network_configuration_handler()); |
| 65 return chromeos::NetworkHandler::Get() |
| 66 ->managed_network_configuration_handler(); |
| 67 } |
| 68 |
36 chromeos::NetworkStateHandler* GetNetworkStateHandler() { | 69 chromeos::NetworkStateHandler* GetNetworkStateHandler() { |
37 DCHECK(chromeos::NetworkHandler::Get()->network_state_handler()); | 70 DCHECK(chromeos::NetworkHandler::Get()->network_state_handler()); |
38 return chromeos::NetworkHandler::Get()->network_state_handler(); | 71 return chromeos::NetworkHandler::Get()->network_state_handler(); |
39 } | 72 } |
40 | 73 |
41 } // namespace | 74 } // namespace |
42 | 75 |
| 76 void SetUpChromeOs() { |
| 77 wifi_sync::WifiCredentialSyncableServiceFactory::GetInstance() |
| 78 ->IgnoreLoginStateForTest(); |
| 79 } |
| 80 |
| 81 bool SetupClientForProfileChromeOs(const Profile* profile) { |
| 82 DCHECK(profile); |
| 83 GetShillProfileClientTestInterface() |
| 84 ->AddProfile(ShillProfilePathForBrowserContext(*profile), |
| 85 ChromeOsUserHashForBrowserContext(*profile)); |
| 86 |
| 87 const base::ListValue policy_network_configs; |
| 88 const base::DictionaryValue policy_global_config; |
| 89 GetManagedNetworkConfigurationHandler() |
| 90 ->SetPolicy(onc::ONC_SOURCE_UNKNOWN, |
| 91 ChromeOsUserHashForBrowserContext(*profile), |
| 92 policy_network_configs, |
| 93 policy_global_config); |
| 94 |
| 95 return true; |
| 96 } |
| 97 |
| 98 void AddWifiCredentialToProfileChromeOs( |
| 99 Profile* profile, const WifiCredential& credential) { |
| 100 DCHECK(profile); |
| 101 scoped_ptr<base::DictionaryValue> onc_properties = |
| 102 credential.ToOncProperties(); |
| 103 if (!onc_properties) { |
| 104 LOG(FATAL) << "Failed to generate ONC properties for " |
| 105 << credential.ToString(); |
| 106 } |
| 107 GetManagedNetworkConfigurationHandler() |
| 108 ->CreateConfiguration( |
| 109 ChromeOsUserHashForBrowserContext(*profile), |
| 110 *onc_properties, |
| 111 chromeos::network_handler::StringResultCallback(), |
| 112 base::Bind(LogCreateConfigurationFailure, |
| 113 base::StringPrintf("Failed to add credential %s", |
| 114 credential.ToString().c_str()))); |
| 115 } |
| 116 |
43 WifiCredentialSet GetWifiCredentialsForProfileChromeOs(const Profile* profile) { | 117 WifiCredentialSet GetWifiCredentialsForProfileChromeOs(const Profile* profile) { |
44 DCHECK(profile); | 118 DCHECK(profile); |
45 return wifi_sync::GetWifiCredentialsForShillProfile( | 119 return wifi_sync::GetWifiCredentialsForShillProfile( |
46 GetNetworkStateHandler(), ShillProfilePathForBrowserContext(*profile)); | 120 GetNetworkStateHandler(), ShillProfilePathForBrowserContext(*profile)); |
47 } | 121 } |
48 | 122 |
49 } // namespace wifi_credentials_helper | 123 } // namespace wifi_credentials_helper |
OLD | NEW |