| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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_proxy_mac.h" | 5 #include "chrome/browser/password_manager/password_store_proxy_mac.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 #include <utility> | 8 #include <utility> |
| 9 | 9 |
| 10 #include "base/metrics/histogram_macros.h" | 10 #include "base/metrics/histogram_macros.h" |
| 11 #include "chrome/browser/password_manager/password_store_mac.h" | 11 #include "chrome/browser/password_manager/password_store_mac.h" |
| 12 #include "chrome/browser/password_manager/simple_password_store_mac.h" | 12 #include "chrome/browser/password_manager/simple_password_store_mac.h" |
| 13 #include "content/public/browser/browser_thread.h" | 13 #include "content/public/browser/browser_thread.h" |
| 14 #include "crypto/apple_keychain.h" | 14 #include "crypto/apple_keychain.h" |
| 15 | 15 |
| 16 using password_manager::MigrationStatus; | 16 using password_manager::MigrationStatus; |
| 17 using password_manager::PasswordStoreChangeList; | 17 using password_manager::PasswordStoreChangeList; |
| 18 | 18 |
| 19 PasswordStoreProxyMac::PasswordStoreProxyMac( | 19 PasswordStoreProxyMac::PasswordStoreProxyMac( |
| 20 scoped_refptr<base::SingleThreadTaskRunner> main_thread_runner, | 20 scoped_refptr<base::SingleThreadTaskRunner> main_thread_runner, |
| 21 std::unique_ptr<crypto::AppleKeychain> keychain, | 21 std::unique_ptr<crypto::AppleKeychain> keychain, |
| 22 std::unique_ptr<password_manager::LoginDatabase> login_db, | 22 std::unique_ptr<password_manager::LoginDatabase> login_db, |
| 23 PrefService* prefs) | 23 PrefService* prefs) |
| 24 : PasswordStore(main_thread_runner, nullptr), | 24 : PasswordStore(main_thread_runner, nullptr), |
| 25 login_metadata_db_(std::move(login_db)) { | 25 login_metadata_db_(std::move(login_db)), |
| 26 keychain_(std::move(keychain)) { |
| 26 DCHECK(login_metadata_db_); | 27 DCHECK(login_metadata_db_); |
| 27 migration_status_.Init(password_manager::prefs::kKeychainMigrationStatus, | 28 migration_status_.Init(password_manager::prefs::kKeychainMigrationStatus, |
| 28 prefs); | 29 prefs); |
| 29 if (migration_status_.GetValue() == | 30 // The login database will be set later after initialization. |
| 30 static_cast<int>(MigrationStatus::MIGRATED)) { | 31 password_store_simple_ = |
| 31 // The login database will be set later after initialization. | 32 new SimplePasswordStoreMac(main_thread_runner, nullptr, nullptr); |
| 32 password_store_simple_ = | |
| 33 new SimplePasswordStoreMac(main_thread_runner, nullptr, nullptr); | |
| 34 } else { | |
| 35 password_store_mac_ = | |
| 36 new PasswordStoreMac(main_thread_runner, nullptr, std::move(keychain)); | |
| 37 } | |
| 38 } | 33 } |
| 39 | 34 |
| 40 PasswordStoreProxyMac::~PasswordStoreProxyMac() { | 35 PasswordStoreProxyMac::~PasswordStoreProxyMac() { |
| 41 } | 36 } |
| 42 | 37 |
| 43 bool PasswordStoreProxyMac::Init( | 38 bool PasswordStoreProxyMac::Init( |
| 44 const syncer::SyncableService::StartSyncFlare& flare) { | 39 const syncer::SyncableService::StartSyncFlare& flare) { |
| 45 // Set up a background thread. | 40 // Set up a background thread. |
| 46 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 41 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 47 thread_.reset(new base::Thread("Chrome_PasswordStore_Thread")); | 42 thread_.reset(new base::Thread("Chrome_PasswordStore_Thread")); |
| (...skipping 15 matching lines...) Expand all Loading... |
| 63 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 58 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 64 PasswordStore::ShutdownOnUIThread(); | 59 PasswordStore::ShutdownOnUIThread(); |
| 65 thread_->Stop(); | 60 thread_->Stop(); |
| 66 | 61 |
| 67 // Execute the task which are still pending. | 62 // Execute the task which are still pending. |
| 68 FlushPendingTasks(); | 63 FlushPendingTasks(); |
| 69 | 64 |
| 70 // Unsubscribe the observer, otherwise it's too late in the destructor. | 65 // Unsubscribe the observer, otherwise it's too late in the destructor. |
| 71 migration_status_.Destroy(); | 66 migration_status_.Destroy(); |
| 72 | 67 |
| 73 // After the thread has stopped it's impossible to switch from one backend to | |
| 74 // another. GetBackend() returns the correct result. | |
| 75 // The backend doesn't need the background thread as PasswordStore::Init() and | |
| 76 // other public methods were never called on it. | |
| 77 GetBackend()->ShutdownOnUIThread(); | 68 GetBackend()->ShutdownOnUIThread(); |
| 78 } | 69 } |
| 79 | 70 |
| 80 scoped_refptr<base::SingleThreadTaskRunner> | 71 scoped_refptr<base::SingleThreadTaskRunner> |
| 81 PasswordStoreProxyMac::GetBackgroundTaskRunner() { | 72 PasswordStoreProxyMac::GetBackgroundTaskRunner() { |
| 82 return thread_ ? thread_->task_runner() : nullptr; | 73 return thread_ ? thread_->task_runner() : nullptr; |
| 83 } | 74 } |
| 84 | 75 |
| 85 password_manager::PasswordStore* PasswordStoreProxyMac::GetBackend() const { | 76 password_manager::PasswordStore* PasswordStoreProxyMac::GetBackend() const { |
| 86 if (password_store_mac_) | |
| 87 return password_store_mac_.get(); | |
| 88 return password_store_simple_.get(); | 77 return password_store_simple_.get(); |
| 89 } | 78 } |
| 90 | 79 |
| 91 void PasswordStoreProxyMac::InitOnBackgroundThread(MigrationStatus status) { | 80 void PasswordStoreProxyMac::InitOnBackgroundThread(MigrationStatus status) { |
| 92 DCHECK(GetBackgroundTaskRunner()->BelongsToCurrentThread()); | 81 DCHECK(GetBackgroundTaskRunner()->BelongsToCurrentThread()); |
| 93 if (!login_metadata_db_->Init()) { | 82 if (!login_metadata_db_->Init()) { |
| 94 login_metadata_db_.reset(); | 83 login_metadata_db_.reset(); |
| 95 LOG(ERROR) << "Could not create/open login database."; | 84 LOG(ERROR) << "Could not create/open login database."; |
| 96 } | 85 } |
| 97 | 86 |
| 98 if (status == MigrationStatus::MIGRATED) { | 87 if (login_metadata_db_ && (status == MigrationStatus::NOT_STARTED || |
| 99 password_store_simple_->InitWithTaskRunner(GetBackgroundTaskRunner(), | 88 status == MigrationStatus::FAILED_ONCE || |
| 100 std::move(login_metadata_db_)); | 89 status == MigrationStatus::FAILED_TWICE)) { |
| 101 } else { | 90 // Let's try to migrate the passwords. |
| 102 password_store_mac_->set_login_metadata_db(login_metadata_db_.get()); | 91 login_metadata_db_->set_clear_password_values(true); |
| 103 password_store_mac_->InitWithTaskRunner(GetBackgroundTaskRunner()); | 92 auto import_status = |
| 104 if (login_metadata_db_ && (status == MigrationStatus::NOT_STARTED || | 93 PasswordStoreMac::ImportFromKeychain(login_metadata_db_.get(), |
| 105 status == MigrationStatus::FAILED_ONCE)) { | 94 keychain_.get()); |
| 106 // Let's try to migrate the passwords. | 95 if (import_status == PasswordStoreMac::MIGRATION_OK) { |
| 107 if (password_store_mac_->ImportFromKeychain() == | 96 status = MigrationStatus::MIGRATED; |
| 108 PasswordStoreMac::MIGRATION_OK) { | 97 } else if (import_status == PasswordStoreMac::MIGRATION_PARTIAL) { |
| 109 status = MigrationStatus::MIGRATED; | 98 status = MigrationStatus::MIGRATED_PARTIALLY; |
| 110 // Switch from |password_store_mac_| to |password_store_simple_|. | 99 } else { |
| 111 password_store_mac_->set_login_metadata_db(nullptr); | 100 login_metadata_db_.reset(); |
| 112 pending_ui_tasks_.push_back(base::Bind( | 101 } |
| 113 &PasswordStoreMac::ShutdownOnUIThread, password_store_mac_)); | 102 pending_ui_tasks_.push_back( |
| 114 password_store_mac_ = nullptr; | 103 base::Bind(&PasswordStoreProxyMac::UpdateStatusPref, this, status)); |
| 115 DCHECK(!password_store_simple_); | 104 } else if (login_metadata_db_ && status == MigrationStatus::MIGRATED) { |
| 116 password_store_simple_ = new SimplePasswordStoreMac( | 105 // Delete the migrated passwords from the keychain. |
| 117 main_thread_runner_, GetBackgroundTaskRunner(), | 106 std::vector<std::unique_ptr<autofill::PasswordForm>> forms; |
| 118 std::move(login_metadata_db_)); | 107 if (login_metadata_db_->GetAutofillableLogins(&forms)) { |
| 119 } else { | 108 PasswordStoreMac::CleanUpKeychain(keychain_.get(), forms); |
| 120 status = (status == MigrationStatus::FAILED_ONCE | 109 status = MigrationStatus::MIGRATED_DELETED; |
| 121 ? MigrationStatus::FAILED_TWICE | |
| 122 : MigrationStatus::FAILED_ONCE); | |
| 123 } | |
| 124 pending_ui_tasks_.push_back( | 110 pending_ui_tasks_.push_back( |
| 125 base::Bind(&PasswordStoreProxyMac::UpdateStatusPref, this, status)); | 111 base::Bind(&PasswordStoreProxyMac::UpdateStatusPref, this, status)); |
| 126 } | 112 } |
| 127 } | 113 } |
| 114 |
| 115 password_store_simple_->InitWithTaskRunner(GetBackgroundTaskRunner(), |
| 116 std::move(login_metadata_db_)); |
| 128 if (!pending_ui_tasks_.empty()) { | 117 if (!pending_ui_tasks_.empty()) { |
| 129 main_thread_runner_->PostTask( | 118 main_thread_runner_->PostTask( |
| 130 FROM_HERE, base::Bind(&PasswordStoreProxyMac::FlushPendingTasks, this)); | 119 FROM_HERE, base::Bind(&PasswordStoreProxyMac::FlushPendingTasks, this)); |
| 131 } | 120 } |
| 132 UMA_HISTOGRAM_ENUMERATION( | 121 UMA_HISTOGRAM_ENUMERATION( |
| 133 "PasswordManager.KeychainMigration.Status", static_cast<int>(status), | 122 "PasswordManager.KeychainMigration.Status", static_cast<int>(status), |
| 134 static_cast<int>(MigrationStatus::MIGRATION_STATUS_COUNT)); | 123 static_cast<int>(MigrationStatus::MIGRATION_STATUS_COUNT)); |
| 135 DCHECK(GetBackend()); | |
| 136 } | 124 } |
| 137 | 125 |
| 138 void PasswordStoreProxyMac::UpdateStatusPref(MigrationStatus status) { | 126 void PasswordStoreProxyMac::UpdateStatusPref(MigrationStatus status) { |
| 139 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 127 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 140 migration_status_.SetValue(static_cast<int>(status)); | 128 migration_status_.SetValue(static_cast<int>(status)); |
| 141 } | 129 } |
| 142 | 130 |
| 143 void PasswordStoreProxyMac::FlushPendingTasks() { | 131 void PasswordStoreProxyMac::FlushPendingTasks() { |
| 144 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 132 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 145 for (auto& task : pending_ui_tasks_) | 133 for (auto& task : pending_ui_tasks_) |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 223 } | 211 } |
| 224 | 212 |
| 225 void PasswordStoreProxyMac::RemoveSiteStatsImpl(const GURL& origin_domain) { | 213 void PasswordStoreProxyMac::RemoveSiteStatsImpl(const GURL& origin_domain) { |
| 226 GetBackend()->RemoveSiteStatsImpl(origin_domain); | 214 GetBackend()->RemoveSiteStatsImpl(origin_domain); |
| 227 } | 215 } |
| 228 | 216 |
| 229 std::vector<std::unique_ptr<password_manager::InteractionsStats>> | 217 std::vector<std::unique_ptr<password_manager::InteractionsStats>> |
| 230 PasswordStoreProxyMac::GetSiteStatsImpl(const GURL& origin_domain) { | 218 PasswordStoreProxyMac::GetSiteStatsImpl(const GURL& origin_domain) { |
| 231 return GetBackend()->GetSiteStatsImpl(origin_domain); | 219 return GetBackend()->GetSiteStatsImpl(origin_domain); |
| 232 } | 220 } |
| OLD | NEW |