| OLD | NEW |
| 1 // Copyright (c) 2006-2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2009 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_default.h" | 5 #include "chrome/browser/password_manager/password_store_default.h" |
| 6 #include "chrome/browser/webdata/web_data_service.h" | 6 #include "chrome/browser/webdata/web_data_service.h" |
| 7 #include "chrome/common/chrome_constants.h" | 7 #include "chrome/common/chrome_constants.h" |
| 8 | 8 |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/task.h" | 10 #include "base/task.h" |
| 11 | 11 |
| 12 PasswordStoreDefault::PasswordStoreDefault(WebDataService* web_data_service) | 12 PasswordStoreDefault::PasswordStoreDefault(WebDataService* web_data_service) |
| 13 : web_data_service_(web_data_service) { | 13 : web_data_service_(web_data_service) { |
| 14 } | 14 } |
| 15 | 15 |
| 16 void PasswordStoreDefault::AddLoginImpl(const PasswordForm& form) { | 16 PasswordStoreDefault::~PasswordStoreDefault() { |
| 17 for (PendingRequestMap::const_iterator it = pending_requests_.begin(); |
| 18 it != pending_requests_.end(); ++it) { |
| 19 web_data_service_->CancelRequest(it->first); |
| 20 } |
| 21 } |
| 22 |
| 23 // Override all the public methods to do avoid passthroughs to the Impl |
| 24 // versions. Since we are calling through to WebDataService, which is |
| 25 // asynchronous, we'll still behave as the caller expects. |
| 26 void PasswordStoreDefault::AddLogin(const PasswordForm& form) { |
| 17 web_data_service_->AddLogin(form); | 27 web_data_service_->AddLogin(form); |
| 18 } | 28 } |
| 19 | 29 |
| 20 void PasswordStoreDefault::RemoveLoginImpl(const PasswordForm& form) { | 30 void PasswordStoreDefault::UpdateLogin(const PasswordForm& form) { |
| 31 web_data_service_->UpdateLogin(form); |
| 32 } |
| 33 |
| 34 void PasswordStoreDefault::RemoveLogin(const PasswordForm& form) { |
| 21 web_data_service_->RemoveLogin(form); | 35 web_data_service_->RemoveLogin(form); |
| 22 } | 36 } |
| 23 | 37 |
| 38 int PasswordStoreDefault::GetLogins(const PasswordForm& form, |
| 39 PasswordStoreConsumer* consumer) { |
| 40 int handle = handle_++; |
| 41 GetLoginsRequest* request = new GetLoginsRequest(form, consumer, handle); |
| 42 |
| 43 int web_data_handle = web_data_service_->GetLogins(form, this); |
| 44 pending_requests_.insert(PendingRequestMap::value_type(web_data_handle, |
| 45 request)); |
| 46 return handle; |
| 47 } |
| 48 |
| 49 void PasswordStoreDefault::CancelLoginsQuery(int handle) { |
| 50 for (PendingRequestMap::iterator it = pending_requests_.begin(); |
| 51 it != pending_requests_.end(); ++it) { |
| 52 GetLoginsRequest* request = it->second; |
| 53 if (request->handle == handle) { |
| 54 web_data_service_->CancelRequest(it->first); |
| 55 delete request; |
| 56 pending_requests_.erase(it); |
| 57 return; |
| 58 } |
| 59 } |
| 60 } |
| 61 |
| 62 void PasswordStoreDefault::AddLoginImpl(const PasswordForm& form) { |
| 63 NOTREACHED(); |
| 64 } |
| 65 |
| 66 void PasswordStoreDefault::RemoveLoginImpl(const PasswordForm& form) { |
| 67 NOTREACHED(); |
| 68 } |
| 69 |
| 24 void PasswordStoreDefault::UpdateLoginImpl(const PasswordForm& form) { | 70 void PasswordStoreDefault::UpdateLoginImpl(const PasswordForm& form) { |
| 25 web_data_service_->UpdateLogin(form); | 71 NOTREACHED(); |
| 26 } | 72 } |
| 27 | 73 |
| 28 void PasswordStoreDefault::GetLoginsImpl(GetLoginsRequest* request) { | 74 void PasswordStoreDefault::GetLoginsImpl(GetLoginsRequest* request) { |
| 29 int web_data_handle = web_data_service_->GetLogins(request->form, this); | 75 NOTREACHED(); |
| 30 AddPendingWebDataServiceRequest(web_data_handle, request); | |
| 31 } | 76 } |
| 32 | 77 |
| 33 void PasswordStoreDefault::OnWebDataServiceRequestDone( | 78 void PasswordStoreDefault::OnWebDataServiceRequestDone( |
| 34 WebDataService::Handle h, | 79 WebDataService::Handle h, |
| 35 const WDTypedResult *result) { | 80 const WDTypedResult *result) { |
| 36 // Look up this handle in our request map to get the original | 81 // Look up this handle in our request map to get the original |
| 37 // GetLoginsRequest. | 82 // GetLoginsRequest. |
| 38 PendingRequestMap::iterator it(pending_requests_.find(h)); | 83 PendingRequestMap::iterator it(pending_requests_.find(h)); |
| 39 DCHECK(it != pending_requests_.end()); | 84 // If the request was cancelled, we are done. |
| 85 if (it == pending_requests_.end()) |
| 86 return; |
| 40 | 87 |
| 41 GetLoginsRequest* request = it->second; | 88 scoped_ptr<GetLoginsRequest> request(it->second); |
| 42 pending_requests_.erase(it); | 89 pending_requests_.erase(it); |
| 43 | 90 |
| 44 DCHECK(result); | 91 DCHECK(result); |
| 45 if (!result) | 92 if (!result) |
| 46 return; | 93 return; |
| 47 | 94 |
| 48 const WDResult<std::vector<PasswordForm*> >* r = | 95 const WDResult<std::vector<PasswordForm*> >* r = |
| 49 static_cast<const WDResult<std::vector<PasswordForm*> >*>(result); | 96 static_cast<const WDResult<std::vector<PasswordForm*> >*>(result); |
| 50 | 97 |
| 51 NotifyConsumer(request, r->GetValue()); | 98 request->consumer->OnPasswordStoreRequestDone(request->handle, |
| 52 | 99 r->GetValue()); |
| 53 RemovePendingWebDataServiceRequest(h); | |
| 54 } | 100 } |
| 55 | |
| 56 void PasswordStoreDefault::AddPendingWebDataServiceRequest( | |
| 57 WebDataService::Handle handle, GetLoginsRequest* request) { | |
| 58 pending_requests_.insert(PendingRequestMap::value_type(handle, request)); | |
| 59 | |
| 60 // WebDataService callbacks are non-retaining. Since there would be a race | |
| 61 // around cancelling the requests in the desctructor vs. getting a callback | |
| 62 // in this worker thread, just make sure that we stick around instead. | |
| 63 this->AddRef(); | |
| 64 } | |
| 65 | |
| 66 void PasswordStoreDefault::RemovePendingWebDataServiceRequest( | |
| 67 WebDataService::Handle handle) { | |
| 68 PendingRequestMap::iterator it(pending_requests_.find(handle)); | |
| 69 DCHECK(it != pending_requests_.end()); | |
| 70 pending_requests_.erase(it); | |
| 71 | |
| 72 // Balance the AddRef in AddPendingWebDataServiceRequest. | |
| 73 this->Release(); | |
| 74 } | |
| 75 | |
| 76 PasswordStore::GetLoginsRequest* | |
| 77 PasswordStoreDefault::GetLoginsRequestForWebDataServiceRequest( | |
| 78 WebDataService::Handle handle) { | |
| 79 PendingRequestMap::iterator it(pending_requests_.find(handle)); | |
| 80 if (it == pending_requests_.end()) | |
| 81 return NULL; | |
| 82 return it->second; | |
| 83 } | |
| OLD | NEW |