password_store_factory.cc revision effb81e5f8246d0db0270817048dc992db66e9fb
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. 4 5#include "chrome/browser/password_manager/password_store_factory.h" 6 7#include "base/command_line.h" 8#include "base/environment.h" 9#include "base/prefs/pref_service.h" 10#include "chrome/browser/profiles/incognito_helpers.h" 11#include "chrome/browser/sync/glue/sync_start_util.h" 12#include "chrome/browser/webdata/web_data_service.h" 13#include "chrome/browser/webdata/web_data_service_factory.h" 14#include "chrome/common/chrome_constants.h" 15#include "chrome/common/chrome_switches.h" 16#include "components/keyed_service/content/browser_context_dependency_manager.h" 17#include "components/os_crypt/os_crypt_switches.h" 18#include "components/password_manager/core/browser/login_database.h" 19#include "components/password_manager/core/browser/password_store.h" 20#include "components/password_manager/core/browser/password_store_default.h" 21#include "components/password_manager/core/common/password_manager_pref_names.h" 22#include "components/user_prefs/pref_registry_syncable.h" 23#include "content/public/browser/browser_thread.h" 24 25#if defined(OS_WIN) 26#include "chrome/browser/password_manager/password_store_win.h" 27#elif defined(OS_MACOSX) 28#include "chrome/browser/password_manager/password_store_mac.h" 29#include "crypto/apple_keychain.h" 30#include "crypto/mock_apple_keychain.h" 31#elif defined(OS_CHROMEOS) || defined(OS_ANDROID) 32// Don't do anything. We're going to use the default store. 33#elif defined(USE_X11) 34#include "base/nix/xdg_util.h" 35#if defined(USE_GNOME_KEYRING) 36#include "chrome/browser/password_manager/native_backend_gnome_x.h" 37#endif 38#include "chrome/browser/password_manager/native_backend_kwallet_x.h" 39#include "chrome/browser/password_manager/password_store_x.h" 40#endif 41 42#if !defined(OS_CHROMEOS) && defined(USE_X11) 43namespace { 44 45const LocalProfileId kInvalidLocalProfileId = 46 static_cast<LocalProfileId>(0); 47 48} // namespace 49#endif 50 51PasswordStoreService::PasswordStoreService( 52 scoped_refptr<PasswordStore> password_store) 53 : password_store_(password_store) {} 54 55PasswordStoreService::~PasswordStoreService() {} 56 57scoped_refptr<PasswordStore> PasswordStoreService::GetPasswordStore() { 58 return password_store_; 59} 60 61void PasswordStoreService::Shutdown() { 62 if (password_store_) 63 password_store_->Shutdown(); 64} 65 66// static 67scoped_refptr<PasswordStore> PasswordStoreFactory::GetForProfile( 68 Profile* profile, 69 Profile::ServiceAccessType sat) { 70 if (sat == Profile::IMPLICIT_ACCESS && profile->IsOffTheRecord()) { 71 NOTREACHED() << "This profile is OffTheRecord"; 72 return NULL; 73 } 74 75 PasswordStoreFactory* factory = GetInstance(); 76 PasswordStoreService* service = static_cast<PasswordStoreService*>( 77 factory->GetServiceForBrowserContext(profile, true)); 78 if (!service) 79 return NULL; 80 return service->GetPasswordStore(); 81} 82 83// static 84PasswordStoreFactory* PasswordStoreFactory::GetInstance() { 85 return Singleton<PasswordStoreFactory>::get(); 86} 87 88PasswordStoreFactory::PasswordStoreFactory() 89 : BrowserContextKeyedServiceFactory( 90 "PasswordStore", 91 BrowserContextDependencyManager::GetInstance()) { 92 DependsOn(WebDataServiceFactory::GetInstance()); 93} 94 95PasswordStoreFactory::~PasswordStoreFactory() {} 96 97#if !defined(OS_CHROMEOS) && defined(USE_X11) 98LocalProfileId PasswordStoreFactory::GetLocalProfileId( 99 PrefService* prefs) const { 100 LocalProfileId id = prefs->GetInteger(prefs::kLocalProfileId); 101 if (id == kInvalidLocalProfileId) { 102 // Note that there are many more users than this. Thus, by design, this is 103 // not a unique id. However, it is large enough that it is very unlikely 104 // that it would be repeated twice on a single machine. It is still possible 105 // for that to occur though, so the potential results of it actually 106 // happening should be considered when using this value. 107 static const LocalProfileId kLocalProfileIdMask = 108 static_cast<LocalProfileId>((1 << 24) - 1); 109 do { 110 id = rand() & kLocalProfileIdMask; 111 // TODO(mdm): scan other profiles to make sure they are not using this id? 112 } while (id == kInvalidLocalProfileId); 113 prefs->SetInteger(prefs::kLocalProfileId, id); 114 } 115 return id; 116} 117#endif 118 119KeyedService* PasswordStoreFactory::BuildServiceInstanceFor( 120 content::BrowserContext* context) const { 121 Profile* profile = static_cast<Profile*>(context); 122 123 base::FilePath login_db_file_path = profile->GetPath(); 124 login_db_file_path = login_db_file_path.Append(chrome::kLoginDataFileName); 125 scoped_ptr<LoginDatabase> login_db(new LoginDatabase()); 126 { 127 // TODO(paivanof@gmail.com): execution of login_db->Init() should go 128 // to DB thread. http://crbug.com/138903 129 base::ThreadRestrictions::ScopedAllowIO allow_io; 130 if (!login_db->Init(login_db_file_path)) { 131 LOG(ERROR) << "Could not initialize login database."; 132 return NULL; 133 } 134 } 135 136 scoped_refptr<base::SingleThreadTaskRunner> main_thread_runner( 137 base::MessageLoopProxy::current()); 138 scoped_refptr<base::SingleThreadTaskRunner> db_thread_runner( 139 content::BrowserThread::GetMessageLoopProxyForThread( 140 content::BrowserThread::DB)); 141 142 scoped_refptr<PasswordStore> ps; 143#if defined(OS_WIN) 144 ps = new PasswordStoreWin(main_thread_runner, 145 db_thread_runner, 146 login_db.release(), 147 WebDataService::FromBrowserContext(profile)); 148#elif defined(OS_MACOSX) 149 crypto::AppleKeychain* keychain = 150 CommandLine::ForCurrentProcess()->HasSwitch( 151 os_crypt::switches::kUseMockKeychain) ? 152 new crypto::MockAppleKeychain() : new crypto::AppleKeychain(); 153 ps = new PasswordStoreMac( 154 main_thread_runner, db_thread_runner, keychain, login_db.release()); 155#elif defined(OS_CHROMEOS) || defined(OS_ANDROID) 156 // For now, we use PasswordStoreDefault. We might want to make a native 157 // backend for PasswordStoreX (see below) in the future though. 158 ps = new PasswordStoreDefault( 159 main_thread_runner, db_thread_runner, login_db.release()); 160#elif defined(USE_X11) 161 // On POSIX systems, we try to use the "native" password management system of 162 // the desktop environment currently running, allowing GNOME Keyring in XFCE. 163 // (In all cases we fall back on the basic store in case of failure.) 164 base::nix::DesktopEnvironment desktop_env; 165 std::string store_type = 166 CommandLine::ForCurrentProcess()->GetSwitchValueASCII( 167 switches::kPasswordStore); 168 if (store_type == "kwallet") { 169 desktop_env = base::nix::DESKTOP_ENVIRONMENT_KDE4; 170 } else if (store_type == "gnome") { 171 desktop_env = base::nix::DESKTOP_ENVIRONMENT_GNOME; 172 } else if (store_type == "basic") { 173 desktop_env = base::nix::DESKTOP_ENVIRONMENT_OTHER; 174 } else { 175 // Detect the store to use automatically. 176 scoped_ptr<base::Environment> env(base::Environment::Create()); 177 desktop_env = base::nix::GetDesktopEnvironment(env.get()); 178 const char* name = base::nix::GetDesktopEnvironmentName(desktop_env); 179 VLOG(1) << "Password storage detected desktop environment: " 180 << (name ? name : "(unknown)"); 181 } 182 183 PrefService* prefs = profile->GetPrefs(); 184 LocalProfileId id = GetLocalProfileId(prefs); 185 186 scoped_ptr<PasswordStoreX::NativeBackend> backend; 187 if (desktop_env == base::nix::DESKTOP_ENVIRONMENT_KDE4) { 188 // KDE3 didn't use DBus, which our KWallet store uses. 189 VLOG(1) << "Trying KWallet for password storage."; 190 backend.reset(new NativeBackendKWallet(id)); 191 if (backend->Init()) 192 VLOG(1) << "Using KWallet for password storage."; 193 else 194 backend.reset(); 195 } else if (desktop_env == base::nix::DESKTOP_ENVIRONMENT_GNOME || 196 desktop_env == base::nix::DESKTOP_ENVIRONMENT_UNITY || 197 desktop_env == base::nix::DESKTOP_ENVIRONMENT_XFCE) { 198#if defined(USE_GNOME_KEYRING) 199 VLOG(1) << "Trying GNOME keyring for password storage."; 200 backend.reset(new NativeBackendGnome(id)); 201 if (backend->Init()) 202 VLOG(1) << "Using GNOME keyring for password storage."; 203 else 204 backend.reset(); 205#endif // defined(USE_GNOME_KEYRING) 206 } 207 208 if (!backend.get()) { 209 LOG(WARNING) << "Using basic (unencrypted) store for password storage. " 210 "See http://code.google.com/p/chromium/wiki/LinuxPasswordStorage for " 211 "more information about password storage options."; 212 } 213 214 ps = new PasswordStoreX(main_thread_runner, 215 db_thread_runner, 216 login_db.release(), 217 backend.release()); 218#elif defined(USE_OZONE) 219 ps = new PasswordStoreDefault( 220 main_thread_runner, db_thread_runner, login_db.release()); 221#else 222 NOTIMPLEMENTED(); 223#endif 224 if (!ps || !ps->Init( 225 sync_start_util::GetFlareForSyncableService(profile->GetPath()))) { 226 NOTREACHED() << "Could not initialize password manager."; 227 return NULL; 228 } 229 230 return new PasswordStoreService(ps); 231} 232 233void PasswordStoreFactory::RegisterProfilePrefs( 234 user_prefs::PrefRegistrySyncable* registry) { 235#if !defined(OS_CHROMEOS) && defined(USE_X11) 236 // Notice that the preprocessor conditions above are exactly those that will 237 // result in using PasswordStoreX in BuildServiceInstanceFor(). 238 registry->RegisterIntegerPref( 239 prefs::kLocalProfileId, 240 kInvalidLocalProfileId, 241 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); 242#endif 243} 244 245content::BrowserContext* PasswordStoreFactory::GetBrowserContextToUse( 246 content::BrowserContext* context) const { 247 return chrome::GetBrowserContextRedirectedInIncognito(context); 248} 249 250bool PasswordStoreFactory::ServiceIsNULLWhileTesting() const { 251 return true; 252} 253