15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/password_manager/password_store_factory.h"
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/command_line.h"
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/environment.h"
92a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/prefs/pref_service.h"
10116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "chrome/browser/password_manager/sync_metrics.h"
11c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "chrome/browser/profiles/incognito_helpers.h"
12a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "chrome/browser/sync/glue/sync_start_util.h"
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/webdata/web_data_service_factory.h"
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/common/chrome_constants.h"
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/common/chrome_switches.h"
16a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "components/keyed_service/content/browser_context_dependency_manager.h"
17a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "components/os_crypt/os_crypt_switches.h"
185d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "components/password_manager/core/browser/login_database.h"
195d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "components/password_manager/core/browser/password_store.h"
205d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "components/password_manager/core/browser/password_store_default.h"
21a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "components/password_manager/core/common/password_manager_pref_names.h"
22cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "components/pref_registry/pref_registry_syncable.h"
235d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "content/public/browser/browser_thread.h"
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#if defined(OS_WIN)
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/password_manager/password_store_win.h"
27116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "components/password_manager/core/browser/webdata/password_web_data_service_win.h"
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#elif defined(OS_MACOSX)
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/password_manager/password_store_mac.h"
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "crypto/apple_keychain.h"
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "crypto/mock_apple_keychain.h"
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#elif defined(OS_CHROMEOS) || defined(OS_ANDROID)
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Don't do anything. We're going to use the default store.
34c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#elif defined(USE_X11)
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/nix/xdg_util.h"
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#if defined(USE_GNOME_KEYRING)
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/password_manager/native_backend_gnome_x.h"
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/password_manager/native_backend_kwallet_x.h"
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/password_manager/password_store_x.h"
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
43c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdochusing password_manager::PasswordStore;
44c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch
45c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#if !defined(OS_CHROMEOS) && defined(USE_X11)
465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace {
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)const LocalProfileId kInvalidLocalProfileId =
495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    static_cast<LocalProfileId>(0);
505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace
525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
545d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)PasswordStoreService::PasswordStoreService(
555d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    scoped_refptr<PasswordStore> password_store)
565d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    : password_store_(password_store) {}
575d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
585d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)PasswordStoreService::~PasswordStoreService() {}
595d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
605d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)scoped_refptr<PasswordStore> PasswordStoreService::GetPasswordStore() {
615d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  return password_store_;
625d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
635d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
645d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)void PasswordStoreService::Shutdown() {
651320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  if (password_store_.get())
665d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    password_store_->Shutdown();
675d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
685d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
695d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// static
705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)scoped_refptr<PasswordStore> PasswordStoreFactory::GetForProfile(
715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    Profile* profile,
725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    Profile::ServiceAccessType sat) {
735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (sat == Profile::IMPLICIT_ACCESS && profile->IsOffTheRecord()) {
745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    NOTREACHED() << "This profile is OffTheRecord";
755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return NULL;
765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
785d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  PasswordStoreFactory* factory = GetInstance();
795d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  PasswordStoreService* service = static_cast<PasswordStoreService*>(
805d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      factory->GetServiceForBrowserContext(profile, true));
815d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (!service)
825d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    return NULL;
835d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  return service->GetPasswordStore();
845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// static
875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)PasswordStoreFactory* PasswordStoreFactory::GetInstance() {
885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return Singleton<PasswordStoreFactory>::get();
895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)PasswordStoreFactory::PasswordStoreFactory()
925d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    : BrowserContextKeyedServiceFactory(
935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        "PasswordStore",
9490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)        BrowserContextDependencyManager::GetInstance()) {
955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DependsOn(WebDataServiceFactory::GetInstance());
965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)PasswordStoreFactory::~PasswordStoreFactory() {}
995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
100c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#if !defined(OS_CHROMEOS) && defined(USE_X11)
1015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)LocalProfileId PasswordStoreFactory::GetLocalProfileId(
1025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    PrefService* prefs) const {
103c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  LocalProfileId id =
104c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch      prefs->GetInteger(password_manager::prefs::kLocalProfileId);
1055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (id == kInvalidLocalProfileId) {
1065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // Note that there are many more users than this. Thus, by design, this is
1075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // not a unique id. However, it is large enough that it is very unlikely
1085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // that it would be repeated twice on a single machine. It is still possible
1095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // for that to occur though, so the potential results of it actually
1105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // happening should be considered when using this value.
1115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    static const LocalProfileId kLocalProfileIdMask =
1125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        static_cast<LocalProfileId>((1 << 24) - 1);
1135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    do {
1145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      id = rand() & kLocalProfileIdMask;
1155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      // TODO(mdm): scan other profiles to make sure they are not using this id?
1165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    } while (id == kInvalidLocalProfileId);
117c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch    prefs->SetInteger(password_manager::prefs::kLocalProfileId, id);
1185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
1195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return id;
1205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
1225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
123a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)KeyedService* PasswordStoreFactory::BuildServiceInstanceFor(
124c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    content::BrowserContext* context) const {
125c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  Profile* profile = static_cast<Profile*>(context);
126c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  base::FilePath login_db_file_path = profile->GetPath();
1285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  login_db_file_path = login_db_file_path.Append(chrome::kLoginDataFileName);
129c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  scoped_ptr<password_manager::LoginDatabase> login_db(
130c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch      new password_manager::LoginDatabase());
1315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  {
1325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // TODO(paivanof@gmail.com): execution of login_db->Init() should go
1335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // to DB thread. http://crbug.com/138903
1345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    base::ThreadRestrictions::ScopedAllowIO allow_io;
1355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (!login_db->Init(login_db_file_path)) {
1365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      LOG(ERROR) << "Could not initialize login database.";
1375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return NULL;
1385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
1395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
1405d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1415d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  scoped_refptr<base::SingleThreadTaskRunner> main_thread_runner(
1425d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      base::MessageLoopProxy::current());
1435d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  scoped_refptr<base::SingleThreadTaskRunner> db_thread_runner(
1445d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      content::BrowserThread::GetMessageLoopProxyForThread(
1455d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)          content::BrowserThread::DB));
1465d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1475d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  scoped_refptr<PasswordStore> ps;
1485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#if defined(OS_WIN)
1495d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ps = new PasswordStoreWin(main_thread_runner,
1505d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                            db_thread_runner,
1515d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                            login_db.release(),
152116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch                            WebDataServiceFactory::GetPasswordWebDataForProfile(
153116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch                                profile, Profile::EXPLICIT_ACCESS));
1545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#elif defined(OS_MACOSX)
1555d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  crypto::AppleKeychain* keychain =
156a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      CommandLine::ForCurrentProcess()->HasSwitch(
157a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)          os_crypt::switches::kUseMockKeychain) ?
1585d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)          new crypto::MockAppleKeychain() : new crypto::AppleKeychain();
1595d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ps = new PasswordStoreMac(
1605d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      main_thread_runner, db_thread_runner, keychain, login_db.release());
1615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#elif defined(OS_CHROMEOS) || defined(OS_ANDROID)
1625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // For now, we use PasswordStoreDefault. We might want to make a native
1635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // backend for PasswordStoreX (see below) in the future though.
164c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  ps = new password_manager::PasswordStoreDefault(
1655d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      main_thread_runner, db_thread_runner, login_db.release());
166c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#elif defined(USE_X11)
1675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // On POSIX systems, we try to use the "native" password management system of
1685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // the desktop environment currently running, allowing GNOME Keyring in XFCE.
1695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // (In all cases we fall back on the basic store in case of failure.)
1705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  base::nix::DesktopEnvironment desktop_env;
1715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::string store_type =
1725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
1735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          switches::kPasswordStore);
1745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (store_type == "kwallet") {
1755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    desktop_env = base::nix::DESKTOP_ENVIRONMENT_KDE4;
1765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  } else if (store_type == "gnome") {
1775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    desktop_env = base::nix::DESKTOP_ENVIRONMENT_GNOME;
1785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  } else if (store_type == "basic") {
1795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    desktop_env = base::nix::DESKTOP_ENVIRONMENT_OTHER;
1805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  } else {
1815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // Detect the store to use automatically.
1825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    scoped_ptr<base::Environment> env(base::Environment::Create());
1835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    desktop_env = base::nix::GetDesktopEnvironment(env.get());
1845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const char* name = base::nix::GetDesktopEnvironmentName(desktop_env);
1855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    VLOG(1) << "Password storage detected desktop environment: "
1865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            << (name ? name : "(unknown)");
1875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
1885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  PrefService* prefs = profile->GetPrefs();
1905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  LocalProfileId id = GetLocalProfileId(prefs);
1915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  scoped_ptr<PasswordStoreX::NativeBackend> backend;
1935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (desktop_env == base::nix::DESKTOP_ENVIRONMENT_KDE4) {
1945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // KDE3 didn't use DBus, which our KWallet store uses.
1955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    VLOG(1) << "Trying KWallet for password storage.";
196effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    backend.reset(new NativeBackendKWallet(id));
1975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (backend->Init())
1985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      VLOG(1) << "Using KWallet for password storage.";
1995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    else
2005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      backend.reset();
2015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  } else if (desktop_env == base::nix::DESKTOP_ENVIRONMENT_GNOME ||
2025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)             desktop_env == base::nix::DESKTOP_ENVIRONMENT_UNITY ||
2035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)             desktop_env == base::nix::DESKTOP_ENVIRONMENT_XFCE) {
2045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#if defined(USE_GNOME_KEYRING)
2055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    VLOG(1) << "Trying GNOME keyring for password storage.";
206effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    backend.reset(new NativeBackendGnome(id));
2075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (backend->Init())
2085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      VLOG(1) << "Using GNOME keyring for password storage.";
2095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    else
2105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      backend.reset();
2115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif  // defined(USE_GNOME_KEYRING)
2125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
2135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (!backend.get()) {
2155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    LOG(WARNING) << "Using basic (unencrypted) store for password storage. "
2165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        "See http://code.google.com/p/chromium/wiki/LinuxPasswordStorage for "
2175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        "more information about password storage options.";
2185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
2195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2205d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ps = new PasswordStoreX(main_thread_runner,
2215d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                          db_thread_runner,
2225d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                          login_db.release(),
2235d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                          backend.release());
2245d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#elif defined(USE_OZONE)
225c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  ps = new password_manager::PasswordStoreDefault(
2265d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      main_thread_runner, db_thread_runner, login_db.release());
2275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#else
2285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  NOTIMPLEMENTED();
2295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
230116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  std::string sync_username =
231116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      password_manager_sync_metrics::GetSyncUsername(profile);
2321320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  if (!ps.get() ||
2331320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      !ps->Init(sync_start_util::GetFlareForSyncableService(profile->GetPath()),
2341320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                sync_username)) {
2355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    NOTREACHED() << "Could not initialize password manager.";
2365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return NULL;
2375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
2385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2395d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  return new PasswordStoreService(ps);
2405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2427dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdochvoid PasswordStoreFactory::RegisterProfilePrefs(
243c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    user_prefs::PrefRegistrySyncable* registry) {
244c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#if !defined(OS_CHROMEOS) && defined(USE_X11)
245effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  // Notice that the preprocessor conditions above are exactly those that will
246effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  // result in using PasswordStoreX in BuildServiceInstanceFor().
247c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  registry->RegisterIntegerPref(
248c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch      password_manager::prefs::kLocalProfileId,
249c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      kInvalidLocalProfileId,
250c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
2515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
2525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
254c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)content::BrowserContext* PasswordStoreFactory::GetBrowserContextToUse(
255c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    content::BrowserContext* context) const {
256c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  return chrome::GetBrowserContextRedirectedInIncognito(context);
2575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool PasswordStoreFactory::ServiceIsNULLWhileTesting() const {
2605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return true;
2615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
262