| 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_win.h" | 5 #include "chrome/browser/password_manager/password_store_win.h" |
| 6 | 6 |
| 7 #include <map> | 7 #include <map> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
| 12 #include "base/strings/string_util.h" | 12 #include "base/strings/string_util.h" |
| 13 #include "base/strings/utf_string_conversions.h" | 13 #include "base/strings/utf_string_conversions.h" |
| 14 #include "components/os_crypt/ie7_password_win.h" | 14 #include "components/os_crypt/ie7_password_win.h" |
| 15 #include "components/password_manager/core/browser/password_manager.h" | 15 #include "components/password_manager/core/browser/password_manager.h" |
| 16 #include "components/password_manager/core/browser/webdata/password_web_data_ser
vice_win.h" | 16 #include "components/password_manager/core/browser/webdata/password_web_data_ser
vice_win.h" |
| 17 #include "content/public/browser/browser_thread.h" | 17 #include "content/public/browser/browser_thread.h" |
| 18 | 18 |
| 19 using autofill::PasswordForm; | 19 using autofill::PasswordForm; |
| 20 using content::BrowserThread; | 20 using content::BrowserThread; |
| 21 using password_manager::PasswordStoreDefault; | 21 using password_manager::PasswordStoreDefault; |
| 22 | 22 |
| 23 // Handles requests to PasswordWebDataService. | 23 // Handles requests to PasswordWebDataService. |
| 24 class PasswordStoreWin::DBHandler : public WebDataServiceConsumer { | 24 class PasswordStoreWin::DBHandler : public WebDataServiceConsumer { |
| 25 public: | 25 public: |
| 26 typedef base::Callback<void(ScopedVector<PasswordForm>)> ResultCallback; |
| 27 |
| 26 DBHandler(const scoped_refptr<PasswordWebDataService>& web_data_service, | 28 DBHandler(const scoped_refptr<PasswordWebDataService>& web_data_service, |
| 27 PasswordStoreWin* password_store) | 29 PasswordStoreWin* password_store) |
| 28 : web_data_service_(web_data_service), password_store_(password_store) {} | 30 : web_data_service_(web_data_service), password_store_(password_store) {} |
| 29 | 31 |
| 30 ~DBHandler(); | 32 ~DBHandler(); |
| 31 | 33 |
| 32 // Requests the IE7 login for |form|. This is async. |callback_runner| will be | 34 // Requests the IE7 login for |form|. This is async. |result_callback| will be |
| 33 // run when complete. | 35 // run when complete. |
| 34 void GetIE7Login( | 36 void GetIE7Login(const PasswordForm& form, |
| 35 const PasswordForm& form, | 37 const ResultCallback& result_callback); |
| 36 const PasswordStoreWin::ConsumerCallbackRunner& callback_runner); | |
| 37 | 38 |
| 38 private: | 39 private: |
| 39 struct RequestInfo { | 40 struct RequestInfo { |
| 40 RequestInfo() {} | 41 RequestInfo() {} |
| 41 | 42 |
| 42 RequestInfo(PasswordForm* request_form, | 43 RequestInfo(PasswordForm* request_form, |
| 43 const PasswordStoreWin::ConsumerCallbackRunner& runner) | 44 const ResultCallback& result_callback) |
| 44 : form(request_form), | 45 : form(request_form), result_callback(result_callback) {} |
| 45 callback_runner(runner) {} | |
| 46 | 46 |
| 47 PasswordForm* form; | 47 PasswordForm* form; |
| 48 PasswordStoreWin::ConsumerCallbackRunner callback_runner; | 48 ResultCallback result_callback; |
| 49 }; | 49 }; |
| 50 | 50 |
| 51 // Holds info associated with in-flight GetIE7Login requests. | 51 // Holds info associated with in-flight GetIE7Login requests. |
| 52 typedef std::map<PasswordWebDataService::Handle, RequestInfo> | 52 typedef std::map<PasswordWebDataService::Handle, RequestInfo> |
| 53 PendingRequestMap; | 53 PendingRequestMap; |
| 54 | 54 |
| 55 // Gets logins from IE7 if no others are found. Also copies them into | 55 // Gets logins from IE7 if no others are found. Also copies them into |
| 56 // Chrome's WebDatabase so we don't need to look next time. | 56 // Chrome's WebDatabase so we don't need to look next time. |
| 57 ScopedVector<autofill::PasswordForm> GetIE7Results( | 57 ScopedVector<autofill::PasswordForm> GetIE7Results( |
| 58 const WDTypedResult* result, | 58 const WDTypedResult* result, |
| (...skipping 20 matching lines...) Expand all Loading... |
| 79 for (PendingRequestMap::const_iterator i = pending_requests_.begin(); | 79 for (PendingRequestMap::const_iterator i = pending_requests_.begin(); |
| 80 i != pending_requests_.end(); | 80 i != pending_requests_.end(); |
| 81 ++i) { | 81 ++i) { |
| 82 web_data_service_->CancelRequest(i->first); | 82 web_data_service_->CancelRequest(i->first); |
| 83 delete i->second.form; | 83 delete i->second.form; |
| 84 } | 84 } |
| 85 } | 85 } |
| 86 | 86 |
| 87 void PasswordStoreWin::DBHandler::GetIE7Login( | 87 void PasswordStoreWin::DBHandler::GetIE7Login( |
| 88 const PasswordForm& form, | 88 const PasswordForm& form, |
| 89 const PasswordStoreWin::ConsumerCallbackRunner& callback_runner) { | 89 const ResultCallback& result_callback) { |
| 90 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); | 90 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); |
| 91 IE7PasswordInfo info; | 91 IE7PasswordInfo info; |
| 92 info.url_hash = | 92 info.url_hash = |
| 93 ie7_password::GetUrlHash(base::UTF8ToWide(form.origin.spec())); | 93 ie7_password::GetUrlHash(base::UTF8ToWide(form.origin.spec())); |
| 94 PasswordWebDataService::Handle handle = | 94 PasswordWebDataService::Handle handle = |
| 95 web_data_service_->GetIE7Login(info, this); | 95 web_data_service_->GetIE7Login(info, this); |
| 96 pending_requests_[handle] = | 96 pending_requests_[handle] = |
| 97 RequestInfo(new PasswordForm(form), callback_runner); | 97 RequestInfo(new PasswordForm(form), result_callback); |
| 98 } | 98 } |
| 99 | 99 |
| 100 ScopedVector<autofill::PasswordForm> PasswordStoreWin::DBHandler::GetIE7Results( | 100 ScopedVector<autofill::PasswordForm> PasswordStoreWin::DBHandler::GetIE7Results( |
| 101 const WDTypedResult* result, | 101 const WDTypedResult* result, |
| 102 const PasswordForm& form) { | 102 const PasswordForm& form) { |
| 103 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); | 103 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); |
| 104 ScopedVector<autofill::PasswordForm> matched_forms; | 104 ScopedVector<autofill::PasswordForm> matched_forms; |
| 105 const WDResult<IE7PasswordInfo>* r = | 105 const WDResult<IE7PasswordInfo>* r = |
| 106 static_cast<const WDResult<IE7PasswordInfo>*>(result); | 106 static_cast<const WDResult<IE7PasswordInfo>*>(result); |
| 107 IE7PasswordInfo info = r->GetValue(); | 107 IE7PasswordInfo info = r->GetValue(); |
| (...skipping 30 matching lines...) Expand all Loading... |
| 138 | 138 |
| 139 void PasswordStoreWin::DBHandler::OnWebDataServiceRequestDone( | 139 void PasswordStoreWin::DBHandler::OnWebDataServiceRequestDone( |
| 140 PasswordWebDataService::Handle handle, | 140 PasswordWebDataService::Handle handle, |
| 141 const WDTypedResult* result) { | 141 const WDTypedResult* result) { |
| 142 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); | 142 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); |
| 143 | 143 |
| 144 PendingRequestMap::iterator i = pending_requests_.find(handle); | 144 PendingRequestMap::iterator i = pending_requests_.find(handle); |
| 145 DCHECK(i != pending_requests_.end()); | 145 DCHECK(i != pending_requests_.end()); |
| 146 | 146 |
| 147 scoped_ptr<PasswordForm> form(i->second.form); | 147 scoped_ptr<PasswordForm> form(i->second.form); |
| 148 PasswordStoreWin::ConsumerCallbackRunner callback_runner( | 148 ResultCallback result_callback(i->second.result_callback); |
| 149 i->second.callback_runner); | |
| 150 pending_requests_.erase(i); | 149 pending_requests_.erase(i); |
| 151 | 150 |
| 152 if (!result) { | 151 if (!result) { |
| 153 // The WDS returns NULL if it is shutting down. Run callback with empty | 152 // The WDS returns NULL if it is shutting down. Run callback with empty |
| 154 // result. | 153 // result. |
| 155 callback_runner.Run(ScopedVector<autofill::PasswordForm>()); | 154 result_callback.Run(ScopedVector<autofill::PasswordForm>()); |
| 156 return; | 155 return; |
| 157 } | 156 } |
| 158 | 157 |
| 159 DCHECK_EQ(PASSWORD_IE7_RESULT, result->GetType()); | 158 DCHECK_EQ(PASSWORD_IE7_RESULT, result->GetType()); |
| 160 callback_runner.Run(GetIE7Results(result, *form)); | 159 result_callback.Run(GetIE7Results(result, *form)); |
| 161 } | 160 } |
| 162 | 161 |
| 163 PasswordStoreWin::PasswordStoreWin( | 162 PasswordStoreWin::PasswordStoreWin( |
| 164 scoped_refptr<base::SingleThreadTaskRunner> main_thread_runner, | 163 scoped_refptr<base::SingleThreadTaskRunner> main_thread_runner, |
| 165 scoped_refptr<base::SingleThreadTaskRunner> db_thread_runner, | 164 scoped_refptr<base::SingleThreadTaskRunner> db_thread_runner, |
| 166 scoped_ptr<password_manager::LoginDatabase> login_db, | 165 scoped_ptr<password_manager::LoginDatabase> login_db, |
| 167 const scoped_refptr<PasswordWebDataService>& web_data_service) | 166 const scoped_refptr<PasswordWebDataService>& web_data_service) |
| 168 : PasswordStoreDefault(main_thread_runner, | 167 : PasswordStoreDefault(main_thread_runner, |
| 169 db_thread_runner, | 168 db_thread_runner, |
| 170 login_db.Pass()) { | 169 login_db.Pass()) { |
| 171 db_handler_.reset(new DBHandler(web_data_service, this)); | 170 db_handler_.reset(new DBHandler(web_data_service, this)); |
| 172 } | 171 } |
| 173 | 172 |
| 174 PasswordStoreWin::~PasswordStoreWin() { | 173 PasswordStoreWin::~PasswordStoreWin() { |
| 175 } | 174 } |
| 176 | 175 |
| 177 void PasswordStoreWin::ShutdownOnDBThread() { | 176 void PasswordStoreWin::ShutdownOnDBThread() { |
| 178 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); | 177 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); |
| 179 db_handler_.reset(); | 178 db_handler_.reset(); |
| 180 } | 179 } |
| 181 | 180 |
| 182 void PasswordStoreWin::Shutdown() { | 181 void PasswordStoreWin::Shutdown() { |
| 183 BrowserThread::PostTask( | 182 BrowserThread::PostTask( |
| 184 BrowserThread::DB, FROM_HERE, | 183 BrowserThread::DB, FROM_HERE, |
| 185 base::Bind(&PasswordStoreWin::ShutdownOnDBThread, this)); | 184 base::Bind(&PasswordStoreWin::ShutdownOnDBThread, this)); |
| 186 PasswordStoreDefault::Shutdown(); | 185 PasswordStoreDefault::Shutdown(); |
| 187 } | 186 } |
| 188 | 187 |
| 189 void PasswordStoreWin::GetLoginsImpl( | 188 void PasswordStoreWin::GetLoginsImpl(const PasswordForm& form, |
| 190 const PasswordForm& form, | 189 AuthorizationPromptPolicy prompt_policy, |
| 191 AuthorizationPromptPolicy prompt_policy, | 190 scoped_ptr<GetLoginsRequest> request) { |
| 192 const ConsumerCallbackRunner& callback_runner) { | |
| 193 // When importing from IE7, the credentials are first stored into a temporary | 191 // When importing from IE7, the credentials are first stored into a temporary |
| 194 // Web SQL database. Then, after each GetLogins() request that does not yield | 192 // Web SQL database. Then, after each GetLogins() request that does not yield |
| 195 // any matches from the LoginDatabase, the matching credentials in the Web SQL | 193 // any matches from the LoginDatabase, the matching credentials in the Web SQL |
| 196 // database, if any, are returned as results instead, and simultaneously get | 194 // database, if any, are returned as results instead, and simultaneously get |
| 197 // moved to the LoginDatabase, so next time they will be found immediately. | 195 // moved to the LoginDatabase, so next time they will be found immediately. |
| 198 // TODO(engedy): Make the IE7-specific code synchronous, so FillMatchingLogins | 196 // TODO(engedy): Make the IE7-specific code synchronous, so FillMatchingLogins |
| 199 // can be overridden instead. See: https://crbug.com/78830. | 197 // can be overridden instead. See: https://crbug.com/78830. |
| 200 // TODO(engedy): Credentials should be imported into the LoginDatabase in the | 198 // TODO(engedy): Credentials should be imported into the LoginDatabase in the |
| 201 // first place. See: https://crbug.com/456119. | 199 // first place. See: https://crbug.com/456119. |
| 202 ScopedVector<autofill::PasswordForm> matched_forms( | 200 ScopedVector<autofill::PasswordForm> matched_forms( |
| 203 FillMatchingLogins(form, prompt_policy)); | 201 FillMatchingLogins(form, prompt_policy)); |
| 204 if (matched_forms.empty() && db_handler_) | 202 if (matched_forms.empty() && db_handler_) { |
| 205 db_handler_->GetIE7Login(form, callback_runner); | 203 db_handler_->GetIE7Login( |
| 206 else | 204 form, base::Bind(&GetLoginsRequest::NotifyConsumerWithResults, |
| 207 callback_runner.Run(matched_forms.Pass()); | 205 base::Owned(request.release()))); |
| 206 } else { |
| 207 request->NotifyConsumerWithResults(matched_forms.Pass()); |
| 208 } |
| 208 } | 209 } |
| OLD | NEW |