| 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 "base/metrics/histogram_macros.h" |
| 7 #include "chrome/browser/password_manager/password_store_mac.h" | 8 #include "chrome/browser/password_manager/password_store_mac.h" |
| 8 #include "chrome/browser/password_manager/simple_password_store_mac.h" | 9 #include "chrome/browser/password_manager/simple_password_store_mac.h" |
| 9 #include "content/public/browser/browser_thread.h" | 10 #include "content/public/browser/browser_thread.h" |
| 10 #include "crypto/apple_keychain.h" | 11 #include "crypto/apple_keychain.h" |
| 11 | 12 |
| 13 using password_manager::MigrationStatus; |
| 12 using password_manager::PasswordStoreChangeList; | 14 using password_manager::PasswordStoreChangeList; |
| 13 | 15 |
| 14 PasswordStoreProxyMac::PasswordStoreProxyMac( | 16 PasswordStoreProxyMac::PasswordStoreProxyMac( |
| 15 scoped_refptr<base::SingleThreadTaskRunner> main_thread_runner, | 17 scoped_refptr<base::SingleThreadTaskRunner> main_thread_runner, |
| 16 scoped_ptr<crypto::AppleKeychain> keychain, | 18 scoped_ptr<crypto::AppleKeychain> keychain, |
| 17 scoped_ptr<password_manager::LoginDatabase> login_db) | 19 scoped_ptr<password_manager::LoginDatabase> login_db, |
| 20 PrefService* prefs) |
| 18 : PasswordStore(main_thread_runner, nullptr), | 21 : PasswordStore(main_thread_runner, nullptr), |
| 19 login_metadata_db_(login_db.Pass()) { | 22 login_metadata_db_(login_db.Pass()) { |
| 20 DCHECK(login_metadata_db_); | 23 DCHECK(login_metadata_db_); |
| 21 // TODO(vasilii): for now the class is just a wrapper around PasswordStoreMac. | 24 migration_status_.Init(password_manager::prefs::kKeychainMigrationStatus, |
| 22 password_store_mac_ = | 25 prefs); |
| 23 new PasswordStoreMac(main_thread_runner, nullptr, keychain.Pass()); | 26 if (migration_status_.GetValue() == |
| 27 static_cast<int>(MigrationStatus::MIGRATED)) { |
| 28 // The login database will be set later after initialization. |
| 29 password_store_simple_ = |
| 30 new SimplePasswordStoreMac(main_thread_runner, nullptr, nullptr); |
| 31 } else { |
| 32 password_store_mac_ = |
| 33 new PasswordStoreMac(main_thread_runner, nullptr, keychain.Pass()); |
| 34 } |
| 24 } | 35 } |
| 25 | 36 |
| 26 PasswordStoreProxyMac::~PasswordStoreProxyMac() { | 37 PasswordStoreProxyMac::~PasswordStoreProxyMac() { |
| 27 } | 38 } |
| 28 | 39 |
| 29 bool PasswordStoreProxyMac::Init( | 40 bool PasswordStoreProxyMac::Init( |
| 30 const syncer::SyncableService::StartSyncFlare& flare) { | 41 const syncer::SyncableService::StartSyncFlare& flare) { |
| 31 // Set up a background thread. | 42 // Set up a background thread. |
| 32 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 43 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 33 thread_.reset(new base::Thread("Chrome_PasswordStore_Thread")); | 44 thread_.reset(new base::Thread("Chrome_PasswordStore_Thread")); |
| 34 | 45 |
| 35 if (!thread_->Start()) { | 46 if (!thread_->Start()) { |
| 36 thread_.reset(); | 47 thread_.reset(); |
| 37 return false; | 48 return false; |
| 38 } | 49 } |
| 39 | 50 |
| 40 ScheduleTask( | 51 if (!password_manager::PasswordStore::Init(flare)) |
| 41 base::Bind(&PasswordStoreProxyMac::InitOnBackgroundThread, this)); | 52 return false; |
| 42 password_store_mac_->InitWithTaskRunner(GetBackgroundTaskRunner()); | 53 |
| 43 return password_manager::PasswordStore::Init(flare); | 54 return ScheduleTask( |
| 55 base::Bind(&PasswordStoreProxyMac::InitOnBackgroundThread, this, |
| 56 static_cast<MigrationStatus>(migration_status_.GetValue()))); |
| 44 } | 57 } |
| 45 | 58 |
| 46 void PasswordStoreProxyMac::Shutdown() { | 59 void PasswordStoreProxyMac::Shutdown() { |
| 47 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 60 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 48 PasswordStore::Shutdown(); | 61 PasswordStore::Shutdown(); |
| 62 thread_->Stop(); |
| 63 |
| 64 // Execute the task which are still pending. |
| 65 FlushPendingTasks(); |
| 66 |
| 67 // Unsubscribe the observer, otherwise it's too late in the destructor. |
| 68 migration_status_.Destroy(); |
| 69 |
| 70 // After the thread has stopped it's impossible to switch from one backend to |
| 71 // another. GetBackend() returns the correct result. |
| 72 // The backend doesn't need the background thread as PasswordStore::Init() and |
| 73 // other public methods were never called on it. |
| 49 GetBackend()->Shutdown(); | 74 GetBackend()->Shutdown(); |
| 50 thread_->Stop(); | |
| 51 } | 75 } |
| 52 | 76 |
| 53 scoped_refptr<base::SingleThreadTaskRunner> | 77 scoped_refptr<base::SingleThreadTaskRunner> |
| 54 PasswordStoreProxyMac::GetBackgroundTaskRunner() { | 78 PasswordStoreProxyMac::GetBackgroundTaskRunner() { |
| 55 return thread_ ? thread_->task_runner() : nullptr; | 79 return thread_ ? thread_->task_runner() : nullptr; |
| 56 } | 80 } |
| 57 | 81 |
| 58 password_manager::PasswordStore* PasswordStoreProxyMac::GetBackend() const { | 82 password_manager::PasswordStore* PasswordStoreProxyMac::GetBackend() const { |
| 59 if (password_store_mac_) | 83 if (password_store_mac_) |
| 60 return password_store_mac_.get(); | 84 return password_store_mac_.get(); |
| 61 return password_store_simple_.get(); | 85 return password_store_simple_.get(); |
| 62 } | 86 } |
| 63 | 87 |
| 64 void PasswordStoreProxyMac::InitOnBackgroundThread() { | 88 void PasswordStoreProxyMac::InitOnBackgroundThread(MigrationStatus status) { |
| 65 DCHECK(GetBackgroundTaskRunner()->BelongsToCurrentThread()); | 89 DCHECK(GetBackgroundTaskRunner()->BelongsToCurrentThread()); |
| 66 if (!login_metadata_db_->Init()) { | 90 if (!login_metadata_db_->Init()) { |
| 67 login_metadata_db_.reset(); | 91 login_metadata_db_.reset(); |
| 68 LOG(ERROR) << "Could not create/open login database."; | 92 LOG(ERROR) << "Could not create/open login database."; |
| 69 return; | |
| 70 } | 93 } |
| 71 if (password_store_mac_) | 94 |
| 95 if (status == MigrationStatus::MIGRATED) { |
| 96 password_store_simple_->InitWithTaskRunner(GetBackgroundTaskRunner(), |
| 97 login_metadata_db_.Pass()); |
| 98 } else { |
| 72 password_store_mac_->set_login_metadata_db(login_metadata_db_.get()); | 99 password_store_mac_->set_login_metadata_db(login_metadata_db_.get()); |
| 100 password_store_mac_->InitWithTaskRunner(GetBackgroundTaskRunner()); |
| 101 if (login_metadata_db_ && (status == MigrationStatus::NOT_STARTED || |
| 102 status == MigrationStatus::FAILED_ONCE)) { |
| 103 // Let's try to migrate the passwords. |
| 104 if (password_store_mac_->ImportFromKeychain() == |
| 105 PasswordStoreMac::MIGRATION_OK) { |
| 106 status = MigrationStatus::MIGRATED; |
| 107 // Switch from |password_store_mac_| to |password_store_simple_|. |
| 108 password_store_mac_->set_login_metadata_db(nullptr); |
| 109 pending_ui_tasks_.push_back( |
| 110 base::Bind(&PasswordStoreMac::Shutdown, password_store_mac_)); |
| 111 password_store_mac_ = nullptr; |
| 112 DCHECK(!password_store_simple_); |
| 113 password_store_simple_ = new SimplePasswordStoreMac( |
| 114 main_thread_runner_, GetBackgroundTaskRunner(), |
| 115 login_metadata_db_.Pass()); |
| 116 } else { |
| 117 status = (status == MigrationStatus::FAILED_ONCE |
| 118 ? MigrationStatus::FAILED_TWICE |
| 119 : MigrationStatus::FAILED_ONCE); |
| 120 } |
| 121 pending_ui_tasks_.push_back( |
| 122 base::Bind(&PasswordStoreProxyMac::UpdateStatusPref, this, status)); |
| 123 } |
| 124 } |
| 125 if (!pending_ui_tasks_.empty()) { |
| 126 main_thread_runner_->PostTask( |
| 127 FROM_HERE, base::Bind(&PasswordStoreProxyMac::FlushPendingTasks, this)); |
| 128 } |
| 129 UMA_HISTOGRAM_ENUMERATION( |
| 130 "PasswordManager.KeychainMigration.Status", static_cast<int>(status), |
| 131 static_cast<int>(MigrationStatus::MIGRATION_STATUS_COUNT)); |
| 132 DCHECK(GetBackend()); |
| 133 } |
| 134 |
| 135 void PasswordStoreProxyMac::UpdateStatusPref(MigrationStatus status) { |
| 136 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 137 migration_status_.SetValue(static_cast<int>(status)); |
| 138 } |
| 139 |
| 140 void PasswordStoreProxyMac::FlushPendingTasks() { |
| 141 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 142 for (auto& task : pending_ui_tasks_) |
| 143 task.Run(); |
| 144 pending_ui_tasks_.clear(); |
| 73 } | 145 } |
| 74 | 146 |
| 75 void PasswordStoreProxyMac::ReportMetricsImpl( | 147 void PasswordStoreProxyMac::ReportMetricsImpl( |
| 76 const std::string& sync_username, | 148 const std::string& sync_username, |
| 77 bool custom_passphrase_sync_enabled) { | 149 bool custom_passphrase_sync_enabled) { |
| 78 GetBackend()->ReportMetricsImpl(sync_username, | 150 GetBackend()->ReportMetricsImpl(sync_username, |
| 79 custom_passphrase_sync_enabled); | 151 custom_passphrase_sync_enabled); |
| 80 } | 152 } |
| 81 | 153 |
| 82 PasswordStoreChangeList PasswordStoreProxyMac::AddLoginImpl( | 154 PasswordStoreChangeList PasswordStoreProxyMac::AddLoginImpl( |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 128 } | 200 } |
| 129 | 201 |
| 130 void PasswordStoreProxyMac::RemoveSiteStatsImpl(const GURL& origin_domain) { | 202 void PasswordStoreProxyMac::RemoveSiteStatsImpl(const GURL& origin_domain) { |
| 131 GetBackend()->RemoveSiteStatsImpl(origin_domain); | 203 GetBackend()->RemoveSiteStatsImpl(origin_domain); |
| 132 } | 204 } |
| 133 | 205 |
| 134 scoped_ptr<password_manager::InteractionsStats> | 206 scoped_ptr<password_manager::InteractionsStats> |
| 135 PasswordStoreProxyMac::GetSiteStatsImpl(const GURL& origin_domain) { | 207 PasswordStoreProxyMac::GetSiteStatsImpl(const GURL& origin_domain) { |
| 136 return GetBackend()->GetSiteStatsImpl(origin_domain); | 208 return GetBackend()->GetSiteStatsImpl(origin_domain); |
| 137 } | 209 } |
| OLD | NEW |