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, the backend can't change. | |
vabr (Chromium)
2015/07/06 09:50:19
I'm a bit unclear about what the comment tries to
vasilii
2015/07/06 13:57:57
Done.
| |
49 GetBackend()->Shutdown(); | 71 GetBackend()->Shutdown(); |
50 thread_->Stop(); | |
51 } | 72 } |
52 | 73 |
53 scoped_refptr<base::SingleThreadTaskRunner> | 74 scoped_refptr<base::SingleThreadTaskRunner> |
54 PasswordStoreProxyMac::GetBackgroundTaskRunner() { | 75 PasswordStoreProxyMac::GetBackgroundTaskRunner() { |
55 return thread_ ? thread_->task_runner() : nullptr; | 76 return thread_ ? thread_->task_runner() : nullptr; |
56 } | 77 } |
57 | 78 |
58 password_manager::PasswordStore* PasswordStoreProxyMac::GetBackend() const { | 79 password_manager::PasswordStore* PasswordStoreProxyMac::GetBackend() const { |
59 if (password_store_mac_) | 80 if (password_store_mac_) |
60 return password_store_mac_.get(); | 81 return password_store_mac_.get(); |
61 return password_store_simple_.get(); | 82 return password_store_simple_.get(); |
62 } | 83 } |
63 | 84 |
64 void PasswordStoreProxyMac::InitOnBackgroundThread() { | 85 void PasswordStoreProxyMac::InitOnBackgroundThread(MigrationStatus status) { |
65 DCHECK(GetBackgroundTaskRunner()->BelongsToCurrentThread()); | 86 DCHECK(GetBackgroundTaskRunner()->BelongsToCurrentThread()); |
66 if (!login_metadata_db_->Init()) { | 87 if (!login_metadata_db_->Init()) { |
67 login_metadata_db_.reset(); | 88 login_metadata_db_.reset(); |
68 LOG(ERROR) << "Could not create/open login database."; | 89 LOG(ERROR) << "Could not create/open login database."; |
69 return; | |
70 } | 90 } |
71 if (password_store_mac_) | 91 |
92 if (status == MigrationStatus::MIGRATED) { | |
93 password_store_simple_->InitWithTaskRunner(GetBackgroundTaskRunner(), | |
94 login_metadata_db_.Pass()); | |
95 } else { | |
72 password_store_mac_->set_login_metadata_db(login_metadata_db_.get()); | 96 password_store_mac_->set_login_metadata_db(login_metadata_db_.get()); |
97 password_store_mac_->InitWithTaskRunner(GetBackgroundTaskRunner()); | |
98 if (login_metadata_db_ && (status == MigrationStatus::NOT_STARTED || | |
99 status == MigrationStatus::FAILED_ONCE)) { | |
100 // Let's try to migrate the passwords. | |
101 if (password_store_mac_->ImportFromKeychain() == | |
102 PasswordStoreMac::MIGRATION_OK) { | |
103 status = MigrationStatus::MIGRATED; | |
104 // Switch from |password_store_mac_| to |password_store_simple_|. | |
105 password_store_mac_->set_login_metadata_db(nullptr); | |
106 pending_ui_tasks_.push_back( | |
107 base::Bind(&PasswordStoreMac::Shutdown, password_store_mac_)); | |
108 password_store_mac_ = nullptr; | |
109 password_store_simple_ = new SimplePasswordStoreMac( | |
110 main_thread_runner_, GetBackgroundTaskRunner(), | |
111 login_metadata_db_.Pass()); | |
112 } else { | |
113 status = (status == MigrationStatus::FAILED_ONCE | |
114 ? MigrationStatus::FAILED_TWICE | |
115 : MigrationStatus::FAILED_ONCE); | |
116 } | |
117 pending_ui_tasks_.push_back( | |
118 base::Bind(&PasswordStoreProxyMac::UpdateStatusPref, this, status)); | |
119 } | |
120 } | |
121 if (!pending_ui_tasks_.empty()) { | |
122 main_thread_runner_->PostTask( | |
123 FROM_HERE, base::Bind(&PasswordStoreProxyMac::FlushPendingTasks, this)); | |
124 } | |
125 UMA_HISTOGRAM_ENUMERATION( | |
126 "PasswordManager.KeychainMigration.Status", static_cast<int>(status), | |
127 static_cast<int>(MigrationStatus::NUM_MIGRATION_STATUS)); | |
128 DCHECK(GetBackend()); | |
129 } | |
130 | |
131 void PasswordStoreProxyMac::UpdateStatusPref(MigrationStatus status) { | |
132 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
133 migration_status_.SetValue(static_cast<int>(status)); | |
134 } | |
135 | |
136 void PasswordStoreProxyMac::FlushPendingTasks() { | |
137 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
138 for (auto& task : pending_ui_tasks_) | |
139 task.Run(); | |
140 pending_ui_tasks_.clear(); | |
73 } | 141 } |
74 | 142 |
75 void PasswordStoreProxyMac::ReportMetricsImpl( | 143 void PasswordStoreProxyMac::ReportMetricsImpl( |
76 const std::string& sync_username, | 144 const std::string& sync_username, |
77 bool custom_passphrase_sync_enabled) { | 145 bool custom_passphrase_sync_enabled) { |
78 GetBackend()->ReportMetricsImpl(sync_username, | 146 GetBackend()->ReportMetricsImpl(sync_username, |
79 custom_passphrase_sync_enabled); | 147 custom_passphrase_sync_enabled); |
80 } | 148 } |
81 | 149 |
82 PasswordStoreChangeList PasswordStoreProxyMac::AddLoginImpl( | 150 PasswordStoreChangeList PasswordStoreProxyMac::AddLoginImpl( |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
128 } | 196 } |
129 | 197 |
130 void PasswordStoreProxyMac::RemoveSiteStatsImpl(const GURL& origin_domain) { | 198 void PasswordStoreProxyMac::RemoveSiteStatsImpl(const GURL& origin_domain) { |
131 GetBackend()->RemoveSiteStatsImpl(origin_domain); | 199 GetBackend()->RemoveSiteStatsImpl(origin_domain); |
132 } | 200 } |
133 | 201 |
134 scoped_ptr<password_manager::InteractionsStats> | 202 scoped_ptr<password_manager::InteractionsStats> |
135 PasswordStoreProxyMac::GetSiteStatsImpl(const GURL& origin_domain) { | 203 PasswordStoreProxyMac::GetSiteStatsImpl(const GURL& origin_domain) { |
136 return GetBackend()->GetSiteStatsImpl(origin_domain); | 204 return GetBackend()->GetSiteStatsImpl(origin_domain); |
137 } | 205 } |
OLD | NEW |