Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 "chrome/browser/password_manager/login_database.h" | |
| 10 #include "chrome/browser/password_manager/password_store_default.h" | |
| 11 #include "chrome/browser/prefs/pref_service.h" | |
| 12 #include "chrome/browser/profiles/profile_dependency_manager.h" | |
| 13 #include "chrome/common/chrome_constants.h" | |
| 14 #include "chrome/common/chrome_switches.h" | |
| 15 #include "chrome/common/pref_names.h" | |
| 16 | |
| 17 #if defined(OS_WIN) | |
| 18 #include "chrome/browser/password_manager/password_store_win.h" | |
| 19 #elif defined(OS_MACOSX) | |
| 20 #include "chrome/browser/keychain_mac.h" | |
| 21 #include "chrome/browser/mock_keychain_mac.h" | |
| 22 #include "chrome/browser/password_manager/password_store_mac.h" | |
| 23 #elif defined(OS_CHROMEOS) | |
| 24 // Don't do anything. We're going to use the default store. | |
| 25 #elif defined(OS_POSIX) | |
| 26 #include "base/nix/xdg_util.h" | |
| 27 #if defined(USE_GNOME_KEYRING) | |
| 28 #include "chrome/browser/password_manager/native_backend_gnome_x.h" | |
| 29 #endif | |
| 30 #include "chrome/browser/password_manager/native_backend_kwallet_x.h" | |
| 31 #include "chrome/browser/password_manager/password_store_x.h" | |
| 32 #endif | |
| 33 | |
| 34 #if !defined(OS_MACOSX) && !defined(OS_CHROMEOS) && defined(OS_POSIX) | |
| 35 namespace { | |
| 36 | |
| 37 const LocalProfileId kInvalidLocalProfileId = | |
| 38 static_cast<LocalProfileId>(0); | |
| 39 | |
| 40 } // namespace | |
| 41 #endif | |
| 42 | |
| 43 PasswordStore* PasswordStoreFactory::GetForProfile( | |
| 44 Profile* profile, | |
| 45 Profile::ServiceAccessType sat) { | |
| 46 if (sat == Profile::IMPLICIT_ACCESS && profile->IsOffTheRecord()) { | |
| 47 NOTREACHED() << "This profile is OffTheRecord"; | |
| 48 return NULL; | |
| 49 } | |
| 50 | |
| 51 return static_cast<PasswordStore*>(GetInstance()->GetBaseForProfile( | |
| 52 profile, true)); | |
| 53 } | |
| 54 | |
| 55 // static | |
| 56 PasswordStoreFactory* PasswordStoreFactory::GetInstance() { | |
| 57 return Singleton<PasswordStoreFactory>::get(); | |
| 58 } | |
| 59 | |
| 60 PasswordStoreFactory::PasswordStoreFactory() | |
| 61 : RefcountedProfileKeyedServiceFactory( | |
| 62 "PasswordStore", | |
| 63 ProfileDependencyManager::GetInstance()) { | |
| 64 // TODO(erg): We must always depend on WebDB; we don't want the dependency | |
| 65 // graph to be different based on platform. | |
| 66 // | |
| 67 // DependsOn(WebDataServiceFactory::GetInstance()); | |
|
Mike Mammarella
2012/03/10 05:11:52
Do you mean for this to be uncommented, then? Alte
Elliot Glaysher
2012/03/12 19:59:40
I mean for this to be uncommented eventually. WebD
| |
| 68 } | |
| 69 | |
| 70 PasswordStoreFactory::~PasswordStoreFactory() {} | |
| 71 | |
| 72 #if !defined(OS_MACOSX) && !defined(OS_CHROMEOS) && defined(OS_POSIX) | |
| 73 LocalProfileId PasswordStoreFactory::GetLocalProfileId( | |
| 74 PrefService* prefs) const { | |
| 75 LocalProfileId id = prefs->GetInteger(prefs::kLocalProfileId); | |
| 76 if (id == kInvalidLocalProfileId) { | |
| 77 // Note that there are many more users than this. Thus, by design, this is | |
| 78 // not a unique id. However, it is large enough that it is very unlikely | |
| 79 // that it would be repeated twice on a single machine. It is still possible | |
| 80 // for that to occur though, so the potential results of it actually | |
| 81 // happening should be considered when using this value. | |
| 82 static const LocalProfileId kLocalProfileIdMask = | |
| 83 static_cast<LocalProfileId>((1 << 24) - 1); | |
| 84 do { | |
| 85 id = rand() & kLocalProfileIdMask; | |
| 86 // TODO(mdm): scan other profiles to make sure they are not using this id? | |
| 87 } while (id == kInvalidLocalProfileId); | |
| 88 prefs->SetInteger(prefs::kLocalProfileId, id); | |
| 89 } | |
| 90 return id; | |
| 91 } | |
| 92 #endif // !defined(OS_MACOSX) && !defined(OS_CHROMEOS) && defined(OS_POSIX) | |
| 93 | |
| 94 RefcountedProfileKeyedService* PasswordStoreFactory::BuildServiceInstanceFor( | |
| 95 Profile* profile) const { | |
| 96 scoped_refptr<PasswordStore> ps; | |
| 97 FilePath login_db_file_path = profile->GetPath(); | |
| 98 login_db_file_path = login_db_file_path.Append(chrome::kLoginDataFileName); | |
| 99 LoginDatabase* login_db = new LoginDatabase(); | |
| 100 if (!login_db->Init(login_db_file_path)) { | |
| 101 LOG(ERROR) << "Could not initialize login database."; | |
| 102 delete login_db; | |
| 103 return NULL; | |
| 104 } | |
| 105 #if defined(OS_WIN) | |
| 106 ps = new PasswordStoreWin( | |
| 107 login_db, profile, | |
| 108 profile->GetWebDataService(Profile::IMPLICIT_ACCESS)); | |
| 109 #elif defined(OS_MACOSX) | |
| 110 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kUseMockKeychain)) { | |
| 111 ps = new PasswordStoreMac(new MockKeychain(), login_db); | |
| 112 } else { | |
| 113 ps = new PasswordStoreMac(new MacKeychain(), login_db); | |
| 114 } | |
| 115 #elif defined(OS_CHROMEOS) | |
| 116 // For now, we use PasswordStoreDefault. We might want to make a native | |
| 117 // backend for PasswordStoreX (see below) in the future though. | |
| 118 ps = new PasswordStoreDefault( | |
| 119 login_db, profile, | |
| 120 profile->GetWebDataService(Profile::IMPLICIT_ACCESS)); | |
| 121 #elif defined(OS_POSIX) | |
| 122 // On POSIX systems, we try to use the "native" password management system of | |
| 123 // the desktop environment currently running, allowing GNOME Keyring in XFCE. | |
| 124 // (In all cases we fall back on the basic store in case of failure.) | |
| 125 base::nix::DesktopEnvironment desktop_env; | |
| 126 std::string store_type = | |
| 127 CommandLine::ForCurrentProcess()->GetSwitchValueASCII( | |
| 128 switches::kPasswordStore); | |
| 129 if (store_type == "kwallet") { | |
| 130 desktop_env = base::nix::DESKTOP_ENVIRONMENT_KDE4; | |
| 131 } else if (store_type == "gnome") { | |
| 132 desktop_env = base::nix::DESKTOP_ENVIRONMENT_GNOME; | |
| 133 } else if (store_type == "basic") { | |
| 134 desktop_env = base::nix::DESKTOP_ENVIRONMENT_OTHER; | |
| 135 } else { | |
| 136 // Detect the store to use automatically. | |
| 137 scoped_ptr<base::Environment> env(base::Environment::Create()); | |
| 138 desktop_env = base::nix::GetDesktopEnvironment(env.get()); | |
| 139 const char* name = base::nix::GetDesktopEnvironmentName(desktop_env); | |
| 140 VLOG(1) << "Password storage detected desktop environment: " | |
| 141 << (name ? name : "(unknown)"); | |
| 142 } | |
| 143 | |
| 144 PrefService* prefs = profile->GetPrefs(); | |
| 145 LocalProfileId id = GetLocalProfileId(prefs); | |
| 146 | |
| 147 scoped_ptr<PasswordStoreX::NativeBackend> backend; | |
| 148 if (desktop_env == base::nix::DESKTOP_ENVIRONMENT_KDE4) { | |
| 149 // KDE3 didn't use DBus, which our KWallet store uses. | |
| 150 VLOG(1) << "Trying KWallet for password storage."; | |
| 151 backend.reset(new NativeBackendKWallet(id, prefs)); | |
| 152 if (backend->Init()) | |
| 153 VLOG(1) << "Using KWallet for password storage."; | |
| 154 else | |
| 155 backend.reset(); | |
| 156 } else if (desktop_env == base::nix::DESKTOP_ENVIRONMENT_GNOME || | |
| 157 desktop_env == base::nix::DESKTOP_ENVIRONMENT_XFCE) { | |
| 158 #if defined(USE_GNOME_KEYRING) | |
| 159 VLOG(1) << "Trying GNOME keyring for password storage."; | |
| 160 backend.reset(new NativeBackendGnome(id, prefs)); | |
| 161 if (backend->Init()) | |
| 162 VLOG(1) << "Using GNOME keyring for password storage."; | |
| 163 else | |
| 164 backend.reset(); | |
| 165 #endif // defined(USE_GNOME_KEYRING) | |
| 166 } | |
| 167 | |
| 168 if (!backend.get()) { | |
| 169 LOG(WARNING) << "Using basic (unencrypted) store for password storage. " | |
| 170 "See http://code.google.com/p/chromium/wiki/LinuxPasswordStorage for " | |
| 171 "more information about password storage options."; | |
| 172 } | |
| 173 | |
| 174 ps = new PasswordStoreX(login_db, profile, | |
| 175 profile->GetWebDataService(Profile::IMPLICIT_ACCESS), | |
| 176 backend.release()); | |
| 177 #else | |
| 178 NOTIMPLEMENTED(); | |
| 179 #endif | |
| 180 if (!ps) | |
| 181 delete login_db; | |
| 182 | |
| 183 if (!ps || !ps->Init()) { | |
| 184 NOTREACHED() << "Could not initialize password manager."; | |
| 185 return NULL; | |
| 186 } | |
| 187 | |
| 188 return ps.release(); | |
| 189 } | |
| 190 | |
| 191 void PasswordStoreFactory::RegisterUserPrefs(PrefService* prefs) { | |
| 192 #if !defined(OS_MACOSX) && !defined(OS_CHROMEOS) && defined(OS_POSIX) | |
| 193 prefs->RegisterIntegerPref(prefs::kLocalProfileId, | |
| 194 kInvalidLocalProfileId, | |
| 195 PrefService::UNSYNCABLE_PREF); | |
| 196 | |
| 197 // Notice that the preprocessor conditions above are exactly those that will | |
| 198 // result in using PasswordStoreX in CreatePasswordStore() below. | |
| 199 PasswordStoreX::RegisterUserPrefs(prefs); | |
| 200 #endif | |
| 201 } | |
| 202 | |
| 203 bool PasswordStoreFactory::ServiceRedirectedInIncognito() { | |
| 204 return true; | |
| 205 } | |
| 206 | |
| 207 bool PasswordStoreFactory::ServiceIsNULLWhileTesting() { | |
| 208 return true; | |
| 209 } | |
| OLD | NEW |