| 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_mac.h" | 5 #include "chrome/browser/password_manager/password_store_mac.h" |
| 6 #include "chrome/browser/password_manager/password_store_mac_internal.h" | 6 #include "chrome/browser/password_manager/password_store_mac_internal.h" |
| 7 | 7 |
| 8 #include <CoreServices/CoreServices.h> | 8 #include <CoreServices/CoreServices.h> |
| 9 #include <set> | 9 #include <set> |
| 10 #include <string> | 10 #include <string> |
| (...skipping 900 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 911 OSType MacKeychainPasswordFormAdapter::CreatorCodeForSearch() { | 911 OSType MacKeychainPasswordFormAdapter::CreatorCodeForSearch() { |
| 912 return finds_only_owned_ ? base::mac::CreatorCodeForApplication() : 0; | 912 return finds_only_owned_ ? base::mac::CreatorCodeForApplication() : 0; |
| 913 } | 913 } |
| 914 | 914 |
| 915 #pragma mark - | 915 #pragma mark - |
| 916 | 916 |
| 917 PasswordStoreMac::PasswordStoreMac( | 917 PasswordStoreMac::PasswordStoreMac( |
| 918 scoped_refptr<base::SingleThreadTaskRunner> main_thread_runner, | 918 scoped_refptr<base::SingleThreadTaskRunner> main_thread_runner, |
| 919 scoped_refptr<base::SingleThreadTaskRunner> db_thread_runner, | 919 scoped_refptr<base::SingleThreadTaskRunner> db_thread_runner, |
| 920 scoped_ptr<AppleKeychain> keychain, | 920 scoped_ptr<AppleKeychain> keychain, |
| 921 scoped_ptr<password_manager::LoginDatabase> login_db) | 921 password_manager::LoginDatabase* login_db) |
| 922 : password_manager::PasswordStore(main_thread_runner, db_thread_runner), | 922 : password_manager::PasswordStore(main_thread_runner, db_thread_runner), |
| 923 keychain_(keychain.Pass()), | 923 keychain_(keychain.Pass()), |
| 924 login_metadata_db_(login_db.Pass()) { | 924 login_metadata_db_(login_db) { |
| 925 DCHECK(keychain_.get()); | 925 DCHECK(keychain_.get()); |
| 926 DCHECK(login_metadata_db_.get()); | |
| 927 login_metadata_db_->set_clear_password_values(true); | 926 login_metadata_db_->set_clear_password_values(true); |
| 928 } | 927 } |
| 929 | 928 |
| 930 PasswordStoreMac::~PasswordStoreMac() {} | 929 PasswordStoreMac::~PasswordStoreMac() {} |
| 931 | 930 |
| 932 bool PasswordStoreMac::Init( | 931 void PasswordStoreMac::InitWithTaskRunner( |
| 933 const syncer::SyncableService::StartSyncFlare& flare) { | 932 scoped_refptr<base::SingleThreadTaskRunner> background_task_runner) { |
| 934 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 933 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 935 thread_.reset(new base::Thread("Chrome_PasswordStore_Thread")); | 934 db_thread_runner_ = background_task_runner; |
| 936 | |
| 937 if (!thread_->Start()) { | |
| 938 thread_.reset(NULL); | |
| 939 return false; | |
| 940 } | |
| 941 | |
| 942 ScheduleTask(base::Bind(&PasswordStoreMac::InitOnBackgroundThread, this)); | |
| 943 return password_manager::PasswordStore::Init(flare); | |
| 944 } | |
| 945 | |
| 946 void PasswordStoreMac::InitOnBackgroundThread() { | |
| 947 DCHECK(thread_->message_loop() == base::MessageLoop::current()); | |
| 948 DCHECK(login_metadata_db_); | |
| 949 if (!login_metadata_db_->Init()) { | |
| 950 login_metadata_db_.reset(); | |
| 951 LOG(ERROR) << "Could not create/open login database."; | |
| 952 } | |
| 953 } | 935 } |
| 954 | 936 |
| 955 void PasswordStoreMac::Shutdown() { | 937 void PasswordStoreMac::Shutdown() { |
| 956 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 938 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 957 password_manager::PasswordStore::Shutdown(); | 939 password_manager::PasswordStore::Shutdown(); |
| 958 thread_->Stop(); | |
| 959 } | 940 } |
| 960 | 941 |
| 961 // Mac stores passwords in the system keychain, which can block for an | 942 bool PasswordStoreMac::Init( |
| 962 // arbitrarily long time (most notably, it can block on user confirmation | 943 const syncer::SyncableService::StartSyncFlare& flare) { |
| 963 // from a dialog). Run tasks on a dedicated thread to avoid blocking the DB | 944 // The class should be used inside PasswordStoreProxyMac only. |
| 964 // thread. | 945 NOTREACHED(); |
| 965 scoped_refptr<base::SingleThreadTaskRunner> | 946 return true; |
| 966 PasswordStoreMac::GetBackgroundTaskRunner() { | |
| 967 return (thread_.get()) ? thread_->task_runner() : NULL; | |
| 968 } | 947 } |
| 969 | 948 |
| 970 void PasswordStoreMac::ReportMetricsImpl(const std::string& sync_username, | 949 void PasswordStoreMac::ReportMetricsImpl(const std::string& sync_username, |
| 971 bool custom_passphrase_sync_enabled) { | 950 bool custom_passphrase_sync_enabled) { |
| 972 if (!login_metadata_db_) | 951 if (!login_metadata_db_) |
| 973 return; | 952 return; |
| 974 login_metadata_db_->ReportMetrics(sync_username, | 953 login_metadata_db_->ReportMetrics(sync_username, |
| 975 custom_passphrase_sync_enabled); | 954 custom_passphrase_sync_enabled); |
| 976 } | 955 } |
| 977 | 956 |
| 978 PasswordStoreChangeList PasswordStoreMac::AddLoginImpl( | 957 PasswordStoreChangeList PasswordStoreMac::AddLoginImpl( |
| 979 const PasswordForm& form) { | 958 const PasswordForm& form) { |
| 980 DCHECK(thread_->message_loop() == base::MessageLoop::current()); | 959 DCHECK(GetBackgroundTaskRunner()->BelongsToCurrentThread()); |
| 981 if (login_metadata_db_ && AddToKeychainIfNecessary(form)) | 960 if (login_metadata_db_ && AddToKeychainIfNecessary(form)) |
| 982 return login_metadata_db_->AddLogin(form); | 961 return login_metadata_db_->AddLogin(form); |
| 983 return PasswordStoreChangeList(); | 962 return PasswordStoreChangeList(); |
| 984 } | 963 } |
| 985 | 964 |
| 986 PasswordStoreChangeList PasswordStoreMac::UpdateLoginImpl( | 965 PasswordStoreChangeList PasswordStoreMac::UpdateLoginImpl( |
| 987 const PasswordForm& form) { | 966 const PasswordForm& form) { |
| 988 DCHECK(thread_->message_loop() == base::MessageLoop::current()); | 967 DCHECK(GetBackgroundTaskRunner()->BelongsToCurrentThread()); |
| 989 if (!login_metadata_db_) | 968 if (!login_metadata_db_) |
| 990 return PasswordStoreChangeList(); | 969 return PasswordStoreChangeList(); |
| 991 | 970 |
| 992 PasswordStoreChangeList changes = login_metadata_db_->UpdateLogin(form); | 971 PasswordStoreChangeList changes = login_metadata_db_->UpdateLogin(form); |
| 993 | 972 |
| 994 MacKeychainPasswordFormAdapter keychain_adapter(keychain_.get()); | 973 MacKeychainPasswordFormAdapter keychain_adapter(keychain_.get()); |
| 995 if (changes.empty() && | 974 if (changes.empty() && |
| 996 !keychain_adapter.HasPasswordsMergeableWithForm(form)) { | 975 !keychain_adapter.HasPasswordsMergeableWithForm(form)) { |
| 997 // If the password isn't in either the DB or the keychain, then it must have | 976 // If the password isn't in either the DB or the keychain, then it must have |
| 998 // been deleted after autofill happened, and should not be re-added. | 977 // been deleted after autofill happened, and should not be re-added. |
| 999 return changes; | 978 return changes; |
| 1000 } | 979 } |
| 1001 | 980 |
| 1002 // The keychain add will update if there is a collision and add if there | 981 // The keychain add will update if there is a collision and add if there |
| 1003 // isn't, which is the behavior we want, so there's no separate update call. | 982 // isn't, which is the behavior we want, so there's no separate update call. |
| 1004 if (AddToKeychainIfNecessary(form) && changes.empty()) { | 983 if (AddToKeychainIfNecessary(form) && changes.empty()) { |
| 1005 changes = login_metadata_db_->AddLogin(form); | 984 changes = login_metadata_db_->AddLogin(form); |
| 1006 } | 985 } |
| 1007 return changes; | 986 return changes; |
| 1008 } | 987 } |
| 1009 | 988 |
| 1010 PasswordStoreChangeList PasswordStoreMac::RemoveLoginImpl( | 989 PasswordStoreChangeList PasswordStoreMac::RemoveLoginImpl( |
| 1011 const PasswordForm& form) { | 990 const PasswordForm& form) { |
| 1012 DCHECK(thread_->message_loop() == base::MessageLoop::current()); | 991 DCHECK(GetBackgroundTaskRunner()->BelongsToCurrentThread()); |
| 1013 PasswordStoreChangeList changes; | 992 PasswordStoreChangeList changes; |
| 1014 if (login_metadata_db_ && login_metadata_db_->RemoveLogin(form)) { | 993 if (login_metadata_db_ && login_metadata_db_->RemoveLogin(form)) { |
| 1015 // See if we own a Keychain item associated with this item. We can do an | 994 // See if we own a Keychain item associated with this item. We can do an |
| 1016 // exact search rather than messing around with trying to do fuzzy matching | 995 // exact search rather than messing around with trying to do fuzzy matching |
| 1017 // because passwords that we created will always have an exact-match | 996 // because passwords that we created will always have an exact-match |
| 1018 // database entry. | 997 // database entry. |
| 1019 // (If a user does lose their profile but not their keychain we'll treat the | 998 // (If a user does lose their profile but not their keychain we'll treat the |
| 1020 // entries we find like other imported entries anyway, so it's reasonable to | 999 // entries we find like other imported entries anyway, so it's reasonable to |
| 1021 // handle deletes on them the way we would for an imported item.) | 1000 // handle deletes on them the way we would for an imported item.) |
| 1022 MacKeychainPasswordFormAdapter owned_keychain_adapter(keychain_.get()); | 1001 MacKeychainPasswordFormAdapter owned_keychain_adapter(keychain_.get()); |
| (...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1114 AppendSecondToFirst(&matched_forms, &keychain_forms); | 1093 AppendSecondToFirst(&matched_forms, &keychain_forms); |
| 1115 | 1094 |
| 1116 if (!database_forms.empty()) { | 1095 if (!database_forms.empty()) { |
| 1117 RemoveDatabaseForms(&database_forms); | 1096 RemoveDatabaseForms(&database_forms); |
| 1118 NotifyLoginsChanged(FormsToRemoveChangeList(database_forms.get())); | 1097 NotifyLoginsChanged(FormsToRemoveChangeList(database_forms.get())); |
| 1119 } | 1098 } |
| 1120 | 1099 |
| 1121 return matched_forms.Pass(); | 1100 return matched_forms.Pass(); |
| 1122 } | 1101 } |
| 1123 | 1102 |
| 1124 void PasswordStoreMac::GetBlacklistLoginsImpl( | |
| 1125 scoped_ptr<PasswordStore::GetLoginsRequest> request) { | |
| 1126 ScopedVector<PasswordForm> obtained_forms; | |
| 1127 if (!FillBlacklistLogins(&obtained_forms)) | |
| 1128 obtained_forms.clear(); | |
| 1129 request->NotifyConsumerWithResults(obtained_forms.Pass()); | |
| 1130 } | |
| 1131 | |
| 1132 void PasswordStoreMac::GetAutofillableLoginsImpl( | |
| 1133 scoped_ptr<PasswordStore::GetLoginsRequest> request) { | |
| 1134 ScopedVector<PasswordForm> obtained_forms; | |
| 1135 if (!FillAutofillableLogins(&obtained_forms)) | |
| 1136 obtained_forms.clear(); | |
| 1137 request->NotifyConsumerWithResults(obtained_forms.Pass()); | |
| 1138 } | |
| 1139 | |
| 1140 bool PasswordStoreMac::FillAutofillableLogins( | 1103 bool PasswordStoreMac::FillAutofillableLogins( |
| 1141 ScopedVector<PasswordForm>* forms) { | 1104 ScopedVector<PasswordForm>* forms) { |
| 1142 DCHECK_EQ(thread_->message_loop(), base::MessageLoop::current()); | 1105 DCHECK(GetBackgroundTaskRunner()->BelongsToCurrentThread()); |
| 1143 forms->clear(); | 1106 forms->clear(); |
| 1144 | 1107 |
| 1145 ScopedVector<PasswordForm> database_forms; | 1108 ScopedVector<PasswordForm> database_forms; |
| 1146 if (!login_metadata_db_ || | 1109 if (!login_metadata_db_ || |
| 1147 !login_metadata_db_->GetAutofillableLogins(&database_forms)) | 1110 !login_metadata_db_->GetAutofillableLogins(&database_forms)) |
| 1148 return false; | 1111 return false; |
| 1149 | 1112 |
| 1150 internal_keychain_helpers::GetPasswordsForForms(*keychain_, &database_forms, | 1113 internal_keychain_helpers::GetPasswordsForForms(*keychain_, &database_forms, |
| 1151 forms); | 1114 forms); |
| 1152 | 1115 |
| 1153 if (!database_forms.empty()) { | 1116 if (!database_forms.empty()) { |
| 1154 RemoveDatabaseForms(&database_forms); | 1117 RemoveDatabaseForms(&database_forms); |
| 1155 NotifyLoginsChanged(FormsToRemoveChangeList(database_forms.get())); | 1118 NotifyLoginsChanged(FormsToRemoveChangeList(database_forms.get())); |
| 1156 } | 1119 } |
| 1157 | 1120 |
| 1158 return true; | 1121 return true; |
| 1159 } | 1122 } |
| 1160 | 1123 |
| 1161 bool PasswordStoreMac::FillBlacklistLogins(ScopedVector<PasswordForm>* forms) { | 1124 bool PasswordStoreMac::FillBlacklistLogins(ScopedVector<PasswordForm>* forms) { |
| 1162 DCHECK_EQ(thread_->message_loop(), base::MessageLoop::current()); | 1125 DCHECK(GetBackgroundTaskRunner()->BelongsToCurrentThread()); |
| 1163 return login_metadata_db_ && login_metadata_db_->GetBlacklistLogins(forms); | 1126 return login_metadata_db_ && login_metadata_db_->GetBlacklistLogins(forms); |
| 1164 } | 1127 } |
| 1165 | 1128 |
| 1166 void PasswordStoreMac::AddSiteStatsImpl( | 1129 void PasswordStoreMac::AddSiteStatsImpl( |
| 1167 const password_manager::InteractionsStats& stats) { | 1130 const password_manager::InteractionsStats& stats) { |
| 1168 DCHECK(thread_->message_loop() == base::MessageLoop::current()); | 1131 DCHECK(GetBackgroundTaskRunner()->BelongsToCurrentThread()); |
| 1169 if (login_metadata_db_) | 1132 if (login_metadata_db_) |
| 1170 login_metadata_db_->stats_table().AddRow(stats); | 1133 login_metadata_db_->stats_table().AddRow(stats); |
| 1171 } | 1134 } |
| 1172 | 1135 |
| 1173 void PasswordStoreMac::RemoveSiteStatsImpl(const GURL& origin_domain) { | 1136 void PasswordStoreMac::RemoveSiteStatsImpl(const GURL& origin_domain) { |
| 1174 DCHECK(thread_->message_loop() == base::MessageLoop::current()); | 1137 DCHECK(GetBackgroundTaskRunner()->BelongsToCurrentThread()); |
| 1175 if (login_metadata_db_) | 1138 if (login_metadata_db_) |
| 1176 login_metadata_db_->stats_table().RemoveRow(origin_domain); | 1139 login_metadata_db_->stats_table().RemoveRow(origin_domain); |
| 1177 } | 1140 } |
| 1178 | 1141 |
| 1179 scoped_ptr<password_manager::InteractionsStats> | 1142 scoped_ptr<password_manager::InteractionsStats> |
| 1180 PasswordStoreMac::GetSiteStatsImpl(const GURL& origin_domain) { | 1143 PasswordStoreMac::GetSiteStatsImpl(const GURL& origin_domain) { |
| 1181 DCHECK(thread_->message_loop() == base::MessageLoop::current()); | 1144 DCHECK(GetBackgroundTaskRunner()->BelongsToCurrentThread()); |
| 1182 return login_metadata_db_ | 1145 return login_metadata_db_ |
| 1183 ? login_metadata_db_->stats_table().GetRow(origin_domain) | 1146 ? login_metadata_db_->stats_table().GetRow(origin_domain) |
| 1184 : scoped_ptr<password_manager::InteractionsStats>(); | 1147 : scoped_ptr<password_manager::InteractionsStats>(); |
| 1185 } | 1148 } |
| 1186 | 1149 |
| 1187 bool PasswordStoreMac::AddToKeychainIfNecessary(const PasswordForm& form) { | 1150 bool PasswordStoreMac::AddToKeychainIfNecessary(const PasswordForm& form) { |
| 1188 if (form.blacklisted_by_user || !form.federation_url.is_empty()) | 1151 if (form.blacklisted_by_user || !form.federation_url.is_empty()) |
| 1189 return true; | 1152 return true; |
| 1190 MacKeychainPasswordFormAdapter keychainAdapter(keychain_.get()); | 1153 MacKeychainPasswordFormAdapter keychainAdapter(keychain_.get()); |
| 1191 return keychainAdapter.AddPassword(form); | 1154 return keychainAdapter.AddPassword(form); |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1247 ScopedVector<PasswordForm> forms_with_keychain_entry; | 1210 ScopedVector<PasswordForm> forms_with_keychain_entry; |
| 1248 internal_keychain_helpers::GetPasswordsForForms(*keychain_, &database_forms, | 1211 internal_keychain_helpers::GetPasswordsForForms(*keychain_, &database_forms, |
| 1249 &forms_with_keychain_entry); | 1212 &forms_with_keychain_entry); |
| 1250 | 1213 |
| 1251 // Clean up any orphaned database entries. | 1214 // Clean up any orphaned database entries. |
| 1252 RemoveDatabaseForms(&database_forms); | 1215 RemoveDatabaseForms(&database_forms); |
| 1253 | 1216 |
| 1254 // Move the orphaned DB forms to the output parameter. | 1217 // Move the orphaned DB forms to the output parameter. |
| 1255 AppendSecondToFirst(orphaned_forms, &database_forms); | 1218 AppendSecondToFirst(orphaned_forms, &database_forms); |
| 1256 } | 1219 } |
| OLD | NEW |