| 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" | |
| 8 #include "chrome/browser/password_manager/password_store_mac.h" | 7 #include "chrome/browser/password_manager/password_store_mac.h" |
| 9 #include "chrome/browser/password_manager/simple_password_store_mac.h" | 8 #include "chrome/browser/password_manager/simple_password_store_mac.h" |
| 10 #include "content/public/browser/browser_thread.h" | 9 #include "content/public/browser/browser_thread.h" |
| 11 #include "crypto/apple_keychain.h" | 10 #include "crypto/apple_keychain.h" |
| 12 | 11 |
| 13 using password_manager::MigrationStatus; | |
| 14 using password_manager::PasswordStoreChangeList; | 12 using password_manager::PasswordStoreChangeList; |
| 15 | 13 |
| 16 PasswordStoreProxyMac::PasswordStoreProxyMac( | 14 PasswordStoreProxyMac::PasswordStoreProxyMac( |
| 17 scoped_refptr<base::SingleThreadTaskRunner> main_thread_runner, | 15 scoped_refptr<base::SingleThreadTaskRunner> main_thread_runner, |
| 18 scoped_ptr<crypto::AppleKeychain> keychain, | 16 scoped_ptr<crypto::AppleKeychain> keychain, |
| 19 scoped_ptr<password_manager::LoginDatabase> login_db, | 17 scoped_ptr<password_manager::LoginDatabase> login_db) |
| 20 PrefService* prefs) | |
| 21 : PasswordStore(main_thread_runner, nullptr), | 18 : PasswordStore(main_thread_runner, nullptr), |
| 22 login_metadata_db_(login_db.Pass()) { | 19 login_metadata_db_(login_db.Pass()) { |
| 23 DCHECK(login_metadata_db_); | 20 DCHECK(login_metadata_db_); |
| 24 migration_status_.Init(password_manager::prefs::kKeychainMigrationStatus, | 21 // TODO(vasilii): for now the class is just a wrapper around PasswordStoreMac. |
| 25 prefs); | 22 password_store_mac_ = |
| 26 if (migration_status_.GetValue() == | 23 new PasswordStoreMac(main_thread_runner, nullptr, keychain.Pass()); |
| 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 } | |
| 35 } | 24 } |
| 36 | 25 |
| 37 PasswordStoreProxyMac::~PasswordStoreProxyMac() { | 26 PasswordStoreProxyMac::~PasswordStoreProxyMac() { |
| 38 } | 27 } |
| 39 | 28 |
| 40 bool PasswordStoreProxyMac::Init( | 29 bool PasswordStoreProxyMac::Init( |
| 41 const syncer::SyncableService::StartSyncFlare& flare) { | 30 const syncer::SyncableService::StartSyncFlare& flare) { |
| 42 // Set up a background thread. | 31 // Set up a background thread. |
| 43 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 32 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 44 thread_.reset(new base::Thread("Chrome_PasswordStore_Thread")); | 33 thread_.reset(new base::Thread("Chrome_PasswordStore_Thread")); |
| 45 | 34 |
| 46 if (!thread_->Start()) { | 35 if (!thread_->Start()) { |
| 47 thread_.reset(); | 36 thread_.reset(); |
| 48 return false; | 37 return false; |
| 49 } | 38 } |
| 50 | 39 |
| 51 if (!password_manager::PasswordStore::Init(flare)) | 40 ScheduleTask( |
| 52 return false; | 41 base::Bind(&PasswordStoreProxyMac::InitOnBackgroundThread, this)); |
| 53 | 42 password_store_mac_->InitWithTaskRunner(GetBackgroundTaskRunner()); |
| 54 return ScheduleTask( | 43 return password_manager::PasswordStore::Init(flare); |
| 55 base::Bind(&PasswordStoreProxyMac::InitOnBackgroundThread, this, | |
| 56 static_cast<MigrationStatus>(migration_status_.GetValue()))); | |
| 57 } | 44 } |
| 58 | 45 |
| 59 void PasswordStoreProxyMac::Shutdown() { | 46 void PasswordStoreProxyMac::Shutdown() { |
| 60 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 47 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 61 PasswordStore::Shutdown(); | 48 PasswordStore::Shutdown(); |
| 49 GetBackend()->Shutdown(); |
| 62 thread_->Stop(); | 50 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. | |
| 74 GetBackend()->Shutdown(); | |
| 75 } | 51 } |
| 76 | 52 |
| 77 scoped_refptr<base::SingleThreadTaskRunner> | 53 scoped_refptr<base::SingleThreadTaskRunner> |
| 78 PasswordStoreProxyMac::GetBackgroundTaskRunner() { | 54 PasswordStoreProxyMac::GetBackgroundTaskRunner() { |
| 79 return thread_ ? thread_->task_runner() : nullptr; | 55 return thread_ ? thread_->task_runner() : nullptr; |
| 80 } | 56 } |
| 81 | 57 |
| 82 password_manager::PasswordStore* PasswordStoreProxyMac::GetBackend() const { | 58 password_manager::PasswordStore* PasswordStoreProxyMac::GetBackend() const { |
| 83 if (password_store_mac_) | 59 if (password_store_mac_) |
| 84 return password_store_mac_.get(); | 60 return password_store_mac_.get(); |
| 85 return password_store_simple_.get(); | 61 return password_store_simple_.get(); |
| 86 } | 62 } |
| 87 | 63 |
| 88 void PasswordStoreProxyMac::InitOnBackgroundThread(MigrationStatus status) { | 64 void PasswordStoreProxyMac::InitOnBackgroundThread() { |
| 89 DCHECK(GetBackgroundTaskRunner()->BelongsToCurrentThread()); | 65 DCHECK(GetBackgroundTaskRunner()->BelongsToCurrentThread()); |
| 90 if (!login_metadata_db_->Init()) { | 66 if (!login_metadata_db_->Init()) { |
| 91 login_metadata_db_.reset(); | 67 login_metadata_db_.reset(); |
| 92 LOG(ERROR) << "Could not create/open login database."; | 68 LOG(ERROR) << "Could not create/open login database."; |
| 69 return; |
| 93 } | 70 } |
| 94 | 71 if (password_store_mac_) |
| 95 if (status == MigrationStatus::MIGRATED) { | |
| 96 password_store_simple_->InitWithTaskRunner(GetBackgroundTaskRunner(), | |
| 97 login_metadata_db_.Pass()); | |
| 98 } else { | |
| 99 password_store_mac_->set_login_metadata_db(login_metadata_db_.get()); | 72 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(); | |
| 145 } | 73 } |
| 146 | 74 |
| 147 void PasswordStoreProxyMac::ReportMetricsImpl( | 75 void PasswordStoreProxyMac::ReportMetricsImpl( |
| 148 const std::string& sync_username, | 76 const std::string& sync_username, |
| 149 bool custom_passphrase_sync_enabled) { | 77 bool custom_passphrase_sync_enabled) { |
| 150 GetBackend()->ReportMetricsImpl(sync_username, | 78 GetBackend()->ReportMetricsImpl(sync_username, |
| 151 custom_passphrase_sync_enabled); | 79 custom_passphrase_sync_enabled); |
| 152 } | 80 } |
| 153 | 81 |
| 154 PasswordStoreChangeList PasswordStoreProxyMac::AddLoginImpl( | 82 PasswordStoreChangeList PasswordStoreProxyMac::AddLoginImpl( |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 200 } | 128 } |
| 201 | 129 |
| 202 void PasswordStoreProxyMac::RemoveSiteStatsImpl(const GURL& origin_domain) { | 130 void PasswordStoreProxyMac::RemoveSiteStatsImpl(const GURL& origin_domain) { |
| 203 GetBackend()->RemoveSiteStatsImpl(origin_domain); | 131 GetBackend()->RemoveSiteStatsImpl(origin_domain); |
| 204 } | 132 } |
| 205 | 133 |
| 206 scoped_ptr<password_manager::InteractionsStats> | 134 scoped_ptr<password_manager::InteractionsStats> |
| 207 PasswordStoreProxyMac::GetSiteStatsImpl(const GURL& origin_domain) { | 135 PasswordStoreProxyMac::GetSiteStatsImpl(const GURL& origin_domain) { |
| 208 return GetBackend()->GetSiteStatsImpl(origin_domain); | 136 return GetBackend()->GetSiteStatsImpl(origin_domain); |
| 209 } | 137 } |
| OLD | NEW |