1 // Copyright (c) 2012 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/credential_cache_service_factory_win.h"
7 #include "base/command_line.h"
8 #include "base/memory/singleton.h"
9 #include "base/win/windows_version.h"
10 #include "chrome/browser/profiles/profile.h"
11 #include "chrome/browser/profiles/profile_dependency_manager.h"
12 #include "chrome/browser/profiles/profile_manager.h"
13 #include "chrome/browser/signin/signin_manager_factory.h"
14 #include "chrome/browser/signin/token_service_factory.h"
15 #include "chrome/browser/sync/credential_cache_service_win.h"
16 #include "chrome/browser/sync/profile_sync_service_factory.h"
17 #include "chrome/common/chrome_paths_internal.h"
18 #include "chrome/common/chrome_switches.h"
24 CredentialCacheServiceFactory
* CredentialCacheServiceFactory::GetInstance() {
25 return Singleton
<CredentialCacheServiceFactory
>::get();
29 syncer::CredentialCacheService
* CredentialCacheServiceFactory::GetForProfile(
31 return static_cast<syncer::CredentialCacheService
*>(
32 GetInstance()->GetServiceForProfile(profile
, true));
35 CredentialCacheServiceFactory::CredentialCacheServiceFactory()
36 : ProfileKeyedServiceFactory("CredentialCacheService",
37 ProfileDependencyManager::GetInstance()) {
38 // TODO(rsimha): Uncomment this once it exists.
39 // DependsOn(PrefServiceFactory::GetInstance());
40 DependsOn(ProfileSyncServiceFactory::GetInstance());
41 DependsOn(SigninManagerFactory::GetInstance());
42 DependsOn(TokenServiceFactory::GetInstance());
45 CredentialCacheServiceFactory::~CredentialCacheServiceFactory() {
49 bool CredentialCacheServiceFactory::IsDefaultProfile(Profile
* profile
) {
51 FilePath default_user_data_dir
;
52 chrome::GetDefaultUserDataDirectory(&default_user_data_dir
);
53 return profile
->GetPath() ==
54 ProfileManager::GetDefaultProfileDir(default_user_data_dir
);
58 // TODO(rsimha): This is a test-only hook. Remove before shipping.
59 // See http://crbug.com/144280.
60 bool CredentialCacheServiceFactory::IsDefaultAlternateProfileForTest(
63 const CommandLine
* command_line
= CommandLine::ForCurrentProcess();
64 FilePath alternate_user_data_dir
;
65 chrome::GetAlternateUserDataDirectory(&alternate_user_data_dir
);
66 return profile
->GetPath() ==
67 ProfileManager::GetDefaultProfileDir(alternate_user_data_dir
);
70 bool CredentialCacheServiceFactory::ServiceIsCreatedWithProfile() const {
74 ProfileKeyedService
* CredentialCacheServiceFactory::BuildServiceInstanceFor(
75 Profile
* profile
) const {
76 // Only instantiate a CredentialCacheService object if we are running in the
77 // default profile on Windows 8, and if credential caching is enabled.
78 // TODO(rsimha): To allow for testing, start CredentialCacheService if we are
79 // either in the default profile or the default alternate profile, so that
80 // an instance of desktop chrome using the default metro directory can be
81 // used for testing. See http://crbug.com/144280.
82 const CommandLine
* command_line
= CommandLine::ForCurrentProcess();
83 if (base::win::GetVersion() >= base::win::VERSION_WIN8
&&
84 (IsDefaultProfile(profile
) ||
85 IsDefaultAlternateProfileForTest(profile
)) &&
86 command_line
->HasSwitch(switches::kEnableSyncCredentialCaching
)) {
87 return new syncer::CredentialCacheService(profile
);