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 "chrome/browser/sync/test/integration/wifi_credentials_helper_chromeos.h"
10 #include "base/files/file_path.h"
11 #include "base/logging.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"
19 #include "chromeos/network/network_handler.h"
20 #include "chromeos/network/network_state_handler.h"
21 #include "components/onc/onc_constants.h"
22 #include "components/wifi_sync/network_state_helper_chromeos.h"
23 #include "components/wifi_sync/wifi_credential_syncable_service_factory.h"
24 #include "content/public/browser/browser_context.h"
26 using wifi_sync::WifiCredential
;
28 using WifiCredentialSet
= wifi_sync::WifiCredential::CredentialSet
;
30 namespace wifi_credentials_helper
{
34 const char kProfilePrefix
[] = "/profile/";
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
;
43 std::string
ChromeOsUserHashForBrowserContext(
44 const content::BrowserContext
& context
) {
45 return context
.GetPath().BaseName().value();
48 // Return value is distinct per |context|, but otherwise arbitrary.
49 std::string
ShillProfilePathForBrowserContext(
50 const content::BrowserContext
& context
) {
51 return kProfilePrefix
+ ChromeOsUserHashForBrowserContext(context
);
54 ::chromeos::ShillProfileClient::TestInterface
*
55 GetShillProfileClientTestInterface() {
56 DCHECK(::chromeos::DBusThreadManager::Get()->GetShillProfileClient());
57 DCHECK(::chromeos::DBusThreadManager::Get()->GetShillProfileClient()
58 ->GetTestInterface());
59 return ::chromeos::DBusThreadManager::Get()->GetShillProfileClient()
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();
71 ::chromeos::NetworkStateHandler
* GetNetworkStateHandler() {
72 DCHECK(::chromeos::NetworkHandler::Get()->network_state_handler());
73 return ::chromeos::NetworkHandler::Get()->network_state_handler();
80 void SetUpChromeOs() {
81 wifi_sync::WifiCredentialSyncableServiceFactory::GetInstance()
82 ->set_ignore_login_state_for_test(true);
85 void SetupClientForProfileChromeOs(
86 const content::BrowserContext
* browser_context
) {
87 DCHECK(browser_context
);
88 GetShillProfileClientTestInterface()
89 ->AddProfile(ShillProfilePathForBrowserContext(*browser_context
),
90 ChromeOsUserHashForBrowserContext(*browser_context
));
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
);
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
),
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();
120 WifiCredentialSet
GetWifiCredentialsForProfileChromeOs(
121 const content::BrowserContext
* browser_context
) {
122 DCHECK(browser_context
);
123 return wifi_sync::GetWifiCredentialsForShillProfile(
124 GetNetworkStateHandler(),
125 ShillProfilePathForBrowserContext(*browser_context
));
128 } // namespace chromeos
130 } // namespace wifi_credentials_helper