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