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