| 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 "components/wifi_sync/wifi_credential_syncable_service_factory.h" | 5 #include "components/wifi_sync/wifi_credential_syncable_service_factory.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
| 9 #include "components/keyed_service/content/browser_context_dependency_manager.h" | 9 #include "components/keyed_service/content/browser_context_dependency_manager.h" |
| 10 #include "components/wifi_sync/wifi_config_delegate.h" |
| 10 #include "components/wifi_sync/wifi_credential_syncable_service.h" | 11 #include "components/wifi_sync/wifi_credential_syncable_service.h" |
| 12 #include "content/public/browser/browser_context.h" |
| 11 | 13 |
| 12 #if defined(OS_CHROMEOS) | 14 #if defined(OS_CHROMEOS) |
| 15 #include "base/files/file_path.h" |
| 13 #include "chromeos/login/login_state.h" | 16 #include "chromeos/login/login_state.h" |
| 14 #include "chromeos/network/network_handler.h" | 17 #include "chromeos/network/network_handler.h" |
| 15 #include "components/wifi_sync/wifi_config_delegate_chromeos.h" | 18 #include "components/wifi_sync/wifi_config_delegate_chromeos.h" |
| 16 #endif | 19 #endif |
| 17 | 20 |
| 18 namespace wifi_sync { | 21 namespace wifi_sync { |
| 19 | 22 |
| 20 namespace { | |
| 21 | |
| 22 scoped_ptr<WifiConfigDelegate> BuildConfigDelegate( | |
| 23 content::BrowserContext* context) { | |
| 24 #if defined(OS_CHROMEOS) | |
| 25 const chromeos::LoginState* login_state = chromeos::LoginState::Get(); | |
| 26 DCHECK(login_state->IsUserLoggedIn()); | |
| 27 DCHECK(!login_state->primary_user_hash().empty()); | |
| 28 // TODO(quiche): Verify that |context| is the primary user's context. | |
| 29 | |
| 30 // Note: NetworkHandler is a singleton that is managed by | |
| 31 // ChromeBrowserMainPartsChromeos, and destroyed after all | |
| 32 // KeyedService instances are destroyed. | |
| 33 chromeos::NetworkHandler* network_handler = chromeos::NetworkHandler::Get(); | |
| 34 return make_scoped_ptr(new WifiConfigDelegateChromeOs( | |
| 35 login_state->primary_user_hash(), | |
| 36 network_handler->managed_network_configuration_handler())); | |
| 37 #else | |
| 38 NOTREACHED(); | |
| 39 return nullptr; | |
| 40 #endif | |
| 41 } | |
| 42 | |
| 43 } // namespace | |
| 44 | |
| 45 // static | 23 // static |
| 46 WifiCredentialSyncableService* | 24 WifiCredentialSyncableService* |
| 47 WifiCredentialSyncableServiceFactory::GetForBrowserContext( | 25 WifiCredentialSyncableServiceFactory::GetForBrowserContext( |
| 48 content::BrowserContext* browser_context) { | 26 content::BrowserContext* browser_context) { |
| 49 return static_cast<WifiCredentialSyncableService*>( | 27 return static_cast<WifiCredentialSyncableService*>( |
| 50 GetInstance()->GetServiceForBrowserContext(browser_context, true)); | 28 GetInstance()->GetServiceForBrowserContext(browser_context, true)); |
| 51 } | 29 } |
| 52 | 30 |
| 53 // static | 31 // static |
| 54 WifiCredentialSyncableServiceFactory* | 32 WifiCredentialSyncableServiceFactory* |
| 55 WifiCredentialSyncableServiceFactory::GetInstance() { | 33 WifiCredentialSyncableServiceFactory::GetInstance() { |
| 56 return Singleton<WifiCredentialSyncableServiceFactory>::get(); | 34 return Singleton<WifiCredentialSyncableServiceFactory>::get(); |
| 57 } | 35 } |
| 58 | 36 |
| 37 #if defined(OS_CHROMEOS) |
| 38 void WifiCredentialSyncableServiceFactory::IgnoreLoginStateForTest() { |
| 39 use_login_state_chrome_os_ = false; |
| 40 } |
| 41 #endif |
| 42 |
| 59 // Private methods. | 43 // Private methods. |
| 60 | 44 |
| 61 WifiCredentialSyncableServiceFactory::WifiCredentialSyncableServiceFactory() | 45 WifiCredentialSyncableServiceFactory::WifiCredentialSyncableServiceFactory() |
| 62 : BrowserContextKeyedServiceFactory( | 46 : BrowserContextKeyedServiceFactory( |
| 63 "WifiCredentialSyncableService", | 47 "WifiCredentialSyncableService", |
| 64 BrowserContextDependencyManager::GetInstance()) { | 48 BrowserContextDependencyManager::GetInstance()), |
| 49 use_login_state_chrome_os_(true) { |
| 65 } | 50 } |
| 66 | 51 |
| 67 WifiCredentialSyncableServiceFactory::~WifiCredentialSyncableServiceFactory() { | 52 WifiCredentialSyncableServiceFactory::~WifiCredentialSyncableServiceFactory() { |
| 68 } | 53 } |
| 69 | 54 |
| 70 KeyedService* WifiCredentialSyncableServiceFactory::BuildServiceInstanceFor( | 55 KeyedService* WifiCredentialSyncableServiceFactory::BuildServiceInstanceFor( |
| 71 content::BrowserContext* context) const { | 56 content::BrowserContext* context) const { |
| 72 // TODO(quiche): Figure out if this behaves properly for multi-profile. | 57 // TODO(quiche): Figure out if this behaves properly for multi-profile. |
| 73 // crbug.com/430681. | 58 // crbug.com/430681. |
| 74 return new WifiCredentialSyncableService(BuildConfigDelegate(context)); | 59 #if defined(OS_CHROMEOS) |
| 60 return new WifiCredentialSyncableService( |
| 61 BuildWifiConfigDelegateChromeOs(context)); |
| 62 #else |
| 63 NOTREACHED(); |
| 64 return nullptr; |
| 65 #endif |
| 75 } | 66 } |
| 76 | 67 |
| 68 #if defined(OS_CHROMEOS) |
| 69 scoped_ptr<WifiConfigDelegate> |
| 70 WifiCredentialSyncableServiceFactory::BuildWifiConfigDelegateChromeOs( |
| 71 content::BrowserContext* context) const { |
| 72 // Note: NetworkHandler is a singleton that is managed by |
| 73 // ChromeBrowserMainPartsChromeos, and destroyed after all |
| 74 // KeyedService instances are destroyed. |
| 75 chromeos::NetworkHandler* network_handler = chromeos::NetworkHandler::Get(); |
| 76 return make_scoped_ptr(new WifiConfigDelegateChromeOs( |
| 77 GetUserHash(context), |
| 78 network_handler->managed_network_configuration_handler())); |
| 79 } |
| 80 |
| 81 std::string WifiCredentialSyncableServiceFactory::GetUserHash( |
| 82 content::BrowserContext* context) const { |
| 83 if (use_login_state_chrome_os_) { |
| 84 const chromeos::LoginState* login_state = chromeos::LoginState::Get(); |
| 85 DCHECK(login_state->IsUserLoggedIn()); |
| 86 DCHECK(!login_state->primary_user_hash().empty()); |
| 87 // TODO(quiche): Verify that |context| is the primary user's context. |
| 88 return login_state->primary_user_hash(); |
| 89 } else { |
| 90 return context->GetPath().BaseName().value(); |
| 91 } |
| 92 } |
| 93 #endif |
| 94 |
| 77 } // namespace wifi_sync | 95 } // namespace wifi_sync |
| OLD | NEW |