| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/password_manager/password_store_factory.h" | 5 #include "chrome/browser/password_manager/password_store_factory.h" |
| 6 | 6 |
| 7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
| 8 #include "base/environment.h" | 8 #include "base/environment.h" |
| 9 #include "chrome/browser/password_manager/login_database.h" | 9 #include "chrome/browser/password_manager/login_database.h" |
| 10 #include "chrome/browser/password_manager/password_store_default.h" | 10 #include "chrome/browser/password_manager/password_store_default.h" |
| 11 #include "chrome/browser/prefs/pref_service.h" | 11 #include "chrome/browser/prefs/pref_service.h" |
| 12 #include "chrome/browser/profiles/profile_dependency_manager.h" | 12 #include "chrome/browser/profiles/profile_dependency_manager.h" |
| 13 #include "chrome/common/chrome_constants.h" | 13 #include "chrome/common/chrome_constants.h" |
| 14 #include "chrome/common/chrome_switches.h" | 14 #include "chrome/common/chrome_switches.h" |
| 15 #include "chrome/common/pref_names.h" | 15 #include "chrome/common/pref_names.h" |
| 16 | 16 |
| 17 #if defined(OS_WIN) | 17 #if defined(OS_WIN) |
| 18 #include "chrome/browser/password_manager/password_store_win.h" | 18 #include "chrome/browser/password_manager/password_store_win.h" |
| 19 #elif defined(OS_MACOSX) | 19 #elif defined(OS_MACOSX) |
| 20 #include "chrome/browser/keychain_mac.h" | 20 #include "chrome/browser/keychain_mac.h" |
| 21 #include "chrome/browser/mock_keychain_mac.h" | 21 #include "chrome/browser/mock_keychain_mac.h" |
| 22 #include "chrome/browser/password_manager/password_store_mac.h" | 22 #include "chrome/browser/password_manager/password_store_mac.h" |
| 23 #elif defined(OS_CHROMEOS) | 23 #elif defined(OS_CHROMEOS) || defined(OS_ANDROID) |
| 24 // Don't do anything. We're going to use the default store. | 24 // Don't do anything. We're going to use the default store. |
| 25 #elif defined(OS_POSIX) | 25 #elif defined(OS_POSIX) |
| 26 #include "base/nix/xdg_util.h" | 26 #include "base/nix/xdg_util.h" |
| 27 #if defined(USE_GNOME_KEYRING) | 27 #if defined(USE_GNOME_KEYRING) |
| 28 #include "chrome/browser/password_manager/native_backend_gnome_x.h" | 28 #include "chrome/browser/password_manager/native_backend_gnome_x.h" |
| 29 #endif | 29 #endif |
| 30 #include "chrome/browser/password_manager/native_backend_kwallet_x.h" | 30 #include "chrome/browser/password_manager/native_backend_kwallet_x.h" |
| 31 #include "chrome/browser/password_manager/password_store_x.h" | 31 #include "chrome/browser/password_manager/password_store_x.h" |
| 32 #endif | 32 #endif |
| 33 | 33 |
| 34 #if !defined(OS_MACOSX) && !defined(OS_CHROMEOS) && defined(OS_POSIX) | 34 #if !defined(OS_MACOSX) && !defined(OS_CHROMEOS) && !defined(OS_ANDROID) && \ |
| 35 defined(OS_POSIX) |
| 35 namespace { | 36 namespace { |
| 36 | 37 |
| 37 const LocalProfileId kInvalidLocalProfileId = | 38 const LocalProfileId kInvalidLocalProfileId = |
| 38 static_cast<LocalProfileId>(0); | 39 static_cast<LocalProfileId>(0); |
| 39 | 40 |
| 40 } // namespace | 41 } // namespace |
| 41 #endif | 42 #endif |
| 42 | 43 |
| 43 PasswordStore* PasswordStoreFactory::GetForProfile( | 44 PasswordStore* PasswordStoreFactory::GetForProfile( |
| 44 Profile* profile, | 45 Profile* profile, |
| (...skipping 17 matching lines...) Expand all Loading... |
| 62 "PasswordStore", | 63 "PasswordStore", |
| 63 ProfileDependencyManager::GetInstance()) { | 64 ProfileDependencyManager::GetInstance()) { |
| 64 // TODO(erg): We must always depend on WebDB; we don't want the dependency | 65 // TODO(erg): We must always depend on WebDB; we don't want the dependency |
| 65 // graph to be different based on platform. | 66 // graph to be different based on platform. |
| 66 // | 67 // |
| 67 // DependsOn(WebDataServiceFactory::GetInstance()); | 68 // DependsOn(WebDataServiceFactory::GetInstance()); |
| 68 } | 69 } |
| 69 | 70 |
| 70 PasswordStoreFactory::~PasswordStoreFactory() {} | 71 PasswordStoreFactory::~PasswordStoreFactory() {} |
| 71 | 72 |
| 72 #if !defined(OS_MACOSX) && !defined(OS_CHROMEOS) && defined(OS_POSIX) | 73 #if !defined(OS_MACOSX) && !defined(OS_CHROMEOS) && !defined(OS_ANDROID) && \ |
| 74 defined(OS_POSIX) |
| 73 LocalProfileId PasswordStoreFactory::GetLocalProfileId( | 75 LocalProfileId PasswordStoreFactory::GetLocalProfileId( |
| 74 PrefService* prefs) const { | 76 PrefService* prefs) const { |
| 75 LocalProfileId id = prefs->GetInteger(prefs::kLocalProfileId); | 77 LocalProfileId id = prefs->GetInteger(prefs::kLocalProfileId); |
| 76 if (id == kInvalidLocalProfileId) { | 78 if (id == kInvalidLocalProfileId) { |
| 77 // Note that there are many more users than this. Thus, by design, this is | 79 // 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 | 80 // 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 | 81 // 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 | 82 // for that to occur though, so the potential results of it actually |
| 81 // happening should be considered when using this value. | 83 // happening should be considered when using this value. |
| 82 static const LocalProfileId kLocalProfileIdMask = | 84 static const LocalProfileId kLocalProfileIdMask = |
| 83 static_cast<LocalProfileId>((1 << 24) - 1); | 85 static_cast<LocalProfileId>((1 << 24) - 1); |
| 84 do { | 86 do { |
| 85 id = rand() & kLocalProfileIdMask; | 87 id = rand() & kLocalProfileIdMask; |
| 86 // TODO(mdm): scan other profiles to make sure they are not using this id? | 88 // TODO(mdm): scan other profiles to make sure they are not using this id? |
| 87 } while (id == kInvalidLocalProfileId); | 89 } while (id == kInvalidLocalProfileId); |
| 88 prefs->SetInteger(prefs::kLocalProfileId, id); | 90 prefs->SetInteger(prefs::kLocalProfileId, id); |
| 89 } | 91 } |
| 90 return id; | 92 return id; |
| 91 } | 93 } |
| 92 #endif // !defined(OS_MACOSX) && !defined(OS_CHROMEOS) && defined(OS_POSIX) | 94 #endif |
| 93 | 95 |
| 94 RefcountedProfileKeyedService* PasswordStoreFactory::BuildServiceInstanceFor( | 96 RefcountedProfileKeyedService* PasswordStoreFactory::BuildServiceInstanceFor( |
| 95 Profile* profile) const { | 97 Profile* profile) const { |
| 96 scoped_refptr<PasswordStore> ps; | 98 scoped_refptr<PasswordStore> ps; |
| 97 FilePath login_db_file_path = profile->GetPath(); | 99 FilePath login_db_file_path = profile->GetPath(); |
| 98 login_db_file_path = login_db_file_path.Append(chrome::kLoginDataFileName); | 100 login_db_file_path = login_db_file_path.Append(chrome::kLoginDataFileName); |
| 99 LoginDatabase* login_db = new LoginDatabase(); | 101 LoginDatabase* login_db = new LoginDatabase(); |
| 100 if (!login_db->Init(login_db_file_path)) { | 102 if (!login_db->Init(login_db_file_path)) { |
| 101 LOG(ERROR) << "Could not initialize login database."; | 103 LOG(ERROR) << "Could not initialize login database."; |
| 102 delete login_db; | 104 delete login_db; |
| 103 return NULL; | 105 return NULL; |
| 104 } | 106 } |
| 105 #if defined(OS_WIN) | 107 #if defined(OS_WIN) |
| 106 ps = new PasswordStoreWin( | 108 ps = new PasswordStoreWin( |
| 107 login_db, profile, | 109 login_db, profile, |
| 108 profile->GetWebDataService(Profile::IMPLICIT_ACCESS)); | 110 profile->GetWebDataService(Profile::IMPLICIT_ACCESS)); |
| 109 #elif defined(OS_MACOSX) | 111 #elif defined(OS_MACOSX) |
| 110 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kUseMockKeychain)) { | 112 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kUseMockKeychain)) { |
| 111 ps = new PasswordStoreMac(new MockKeychain(), login_db); | 113 ps = new PasswordStoreMac(new MockKeychain(), login_db); |
| 112 } else { | 114 } else { |
| 113 ps = new PasswordStoreMac(new MacKeychain(), login_db); | 115 ps = new PasswordStoreMac(new MacKeychain(), login_db); |
| 114 } | 116 } |
| 115 #elif defined(OS_CHROMEOS) | 117 #elif defined(OS_CHROMEOS) || defined(OS_ANDROID) |
| 116 // For now, we use PasswordStoreDefault. We might want to make a native | 118 // For now, we use PasswordStoreDefault. We might want to make a native |
| 117 // backend for PasswordStoreX (see below) in the future though. | 119 // backend for PasswordStoreX (see below) in the future though. |
| 118 ps = new PasswordStoreDefault( | 120 ps = new PasswordStoreDefault( |
| 119 login_db, profile, | 121 login_db, profile, profile->GetWebDataService(Profile::IMPLICIT_ACCESS)); |
| 120 profile->GetWebDataService(Profile::IMPLICIT_ACCESS)); | |
| 121 #elif defined(OS_POSIX) | 122 #elif defined(OS_POSIX) |
| 122 // On POSIX systems, we try to use the "native" password management system of | 123 // 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 // 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 // (In all cases we fall back on the basic store in case of failure.) |
| 125 base::nix::DesktopEnvironment desktop_env; | 126 base::nix::DesktopEnvironment desktop_env; |
| 126 std::string store_type = | 127 std::string store_type = |
| 127 CommandLine::ForCurrentProcess()->GetSwitchValueASCII( | 128 CommandLine::ForCurrentProcess()->GetSwitchValueASCII( |
| 128 switches::kPasswordStore); | 129 switches::kPasswordStore); |
| 129 if (store_type == "kwallet") { | 130 if (store_type == "kwallet") { |
| 130 desktop_env = base::nix::DESKTOP_ENVIRONMENT_KDE4; | 131 desktop_env = base::nix::DESKTOP_ENVIRONMENT_KDE4; |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 182 | 183 |
| 183 if (!ps || !ps->Init()) { | 184 if (!ps || !ps->Init()) { |
| 184 NOTREACHED() << "Could not initialize password manager."; | 185 NOTREACHED() << "Could not initialize password manager."; |
| 185 return NULL; | 186 return NULL; |
| 186 } | 187 } |
| 187 | 188 |
| 188 return ps.release(); | 189 return ps.release(); |
| 189 } | 190 } |
| 190 | 191 |
| 191 void PasswordStoreFactory::RegisterUserPrefs(PrefService* prefs) { | 192 void PasswordStoreFactory::RegisterUserPrefs(PrefService* prefs) { |
| 192 #if !defined(OS_MACOSX) && !defined(OS_CHROMEOS) && defined(OS_POSIX) | 193 #if !defined(OS_MACOSX) && !defined(OS_CHROMEOS) && !defined(OS_ANDROID) \ |
| 194 && defined(OS_POSIX) |
| 193 prefs->RegisterIntegerPref(prefs::kLocalProfileId, | 195 prefs->RegisterIntegerPref(prefs::kLocalProfileId, |
| 194 kInvalidLocalProfileId, | 196 kInvalidLocalProfileId, |
| 195 PrefService::UNSYNCABLE_PREF); | 197 PrefService::UNSYNCABLE_PREF); |
| 196 | 198 |
| 197 // Notice that the preprocessor conditions above are exactly those that will | 199 // Notice that the preprocessor conditions above are exactly those that will |
| 198 // result in using PasswordStoreX in CreatePasswordStore() below. | 200 // result in using PasswordStoreX in CreatePasswordStore() below. |
| 199 PasswordStoreX::RegisterUserPrefs(prefs); | 201 PasswordStoreX::RegisterUserPrefs(prefs); |
| 200 #endif | 202 #endif |
| 201 } | 203 } |
| 202 | 204 |
| 203 bool PasswordStoreFactory::ServiceRedirectedInIncognito() { | 205 bool PasswordStoreFactory::ServiceRedirectedInIncognito() { |
| 204 return true; | 206 return true; |
| 205 } | 207 } |
| 206 | 208 |
| 207 bool PasswordStoreFactory::ServiceIsNULLWhileTesting() { | 209 bool PasswordStoreFactory::ServiceIsNULLWhileTesting() { |
| 208 return true; | 210 return true; |
| 209 } | 211 } |
| OLD | NEW |