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 "components/wifi_sync/wifi_credential_syncable_service_factory.h"
9 #include "base/logging.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "components/keyed_service/content/browser_context_dependency_manager.h"
12 #include "components/wifi_sync/wifi_config_delegate.h"
13 #include "components/wifi_sync/wifi_credential_syncable_service.h"
14 #include "content/public/browser/browser_context.h"
16 #if defined(OS_CHROMEOS)
17 #include "base/files/file_path.h"
18 #include "chromeos/login/login_state.h"
19 #include "chromeos/network/network_handler.h"
20 #include "components/wifi_sync/wifi_config_delegate_chromeos.h"
27 #if defined(OS_CHROMEOS)
28 // Returns a string identifying a ChromeOS network settings profile,
29 // by that profile's UserHash property. This value may be communicated
30 // to the ChromeOS connection manager ("Shill"), but must not be
31 // exposed to any untrusted code (e.g., via web APIs).
32 std::string
GetUserHash(content::BrowserContext
* context
,
33 bool use_login_state
) {
34 if (use_login_state
) {
35 const chromeos::LoginState
* login_state
= chromeos::LoginState::Get();
36 DCHECK(login_state
->IsUserLoggedIn());
37 DCHECK(!login_state
->primary_user_hash().empty());
38 // TODO(quiche): Verify that |context| is the primary user's context.
39 return login_state
->primary_user_hash();
41 // In WiFi credential sync tests, LoginState is not
42 // available. Instead, those tests set their Shill profiles'
43 // UserHashes based on the corresponding BrowserContexts' storage
45 return context
->GetPath().BaseName().value();
53 WifiCredentialSyncableService
*
54 WifiCredentialSyncableServiceFactory::GetForBrowserContext(
55 content::BrowserContext
* browser_context
) {
56 return static_cast<WifiCredentialSyncableService
*>(
57 GetInstance()->GetServiceForBrowserContext(browser_context
, true));
61 WifiCredentialSyncableServiceFactory
*
62 WifiCredentialSyncableServiceFactory::GetInstance() {
63 return base::Singleton
<WifiCredentialSyncableServiceFactory
>::get();
68 WifiCredentialSyncableServiceFactory::WifiCredentialSyncableServiceFactory()
69 : BrowserContextKeyedServiceFactory(
70 "WifiCredentialSyncableService",
71 BrowserContextDependencyManager::GetInstance()) {
74 WifiCredentialSyncableServiceFactory::~WifiCredentialSyncableServiceFactory() {
77 KeyedService
* WifiCredentialSyncableServiceFactory::BuildServiceInstanceFor(
78 content::BrowserContext
* context
) const {
79 // TODO(quiche): Figure out if this behaves properly for multi-profile.
81 #if defined(OS_CHROMEOS)
82 return new WifiCredentialSyncableService(
83 BuildWifiConfigDelegateChromeOs(context
));
90 #if defined(OS_CHROMEOS)
91 scoped_ptr
<WifiConfigDelegate
>
92 WifiCredentialSyncableServiceFactory::BuildWifiConfigDelegateChromeOs(
93 content::BrowserContext
* context
) const {
94 // Note: NetworkHandler is a singleton that is managed by
95 // ChromeBrowserMainPartsChromeos, and destroyed after all
96 // KeyedService instances are destroyed.
97 chromeos::NetworkHandler
* network_handler
= chromeos::NetworkHandler::Get();
98 return make_scoped_ptr(new WifiConfigDelegateChromeOs(
99 GetUserHash(context
, !ignore_login_state_for_test_
),
100 network_handler
->managed_network_configuration_handler()));
104 } // namespace wifi_sync