Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 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/webdata/web_data_request_manager.h" | 5 #include "chrome/browser/webdata/web_data_request_manager.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/message_loop.h" | 8 #include "base/message_loop.h" |
| 9 #include "base/stl_util.h" | 9 #include "base/stl_util.h" |
| 10 #include "chrome/browser/autofill/autofill_profile.h" | |
| 11 #include "chrome/browser/autofill/credit_card.h" | |
| 12 #include "chrome/browser/webdata/web_data_service.h" | 10 #include "chrome/browser/webdata/web_data_service.h" |
| 13 | 11 |
| 14 //////////////////////////////////////////////////////////////////////////////// | 12 //////////////////////////////////////////////////////////////////////////////// |
| 15 // | 13 // |
| 16 // WebDataRequest implementation. | 14 // WebDataRequest implementation. |
| 17 // | 15 // |
| 18 //////////////////////////////////////////////////////////////////////////////// | 16 //////////////////////////////////////////////////////////////////////////////// |
| 19 | 17 |
| 20 WebDataRequest::WebDataRequest(WebDataService* service, | 18 WebDataRequest::WebDataRequest(WebDataServiceConsumer* consumer, |
| 21 WebDataServiceConsumer* consumer, | |
| 22 WebDataRequestManager* manager) | 19 WebDataRequestManager* manager) |
| 23 : service_(service), | 20 : manager_(manager), |
| 24 cancelled_(false), | 21 cancelled_(false), |
| 25 consumer_(consumer), | 22 consumer_(consumer), |
| 26 result_(NULL) { | 23 result_(NULL) { |
| 27 handle_ = manager->GetNextRequestHandle(); | 24 handle_ = manager_->GetNextRequestHandle(); |
| 28 message_loop_ = MessageLoop::current(); | 25 message_loop_ = MessageLoop::current(); |
| 29 manager->RegisterRequest(this); | 26 manager_->RegisterRequest(this); |
| 30 } | 27 } |
| 31 | 28 |
| 32 WebDataRequest::~WebDataRequest() { | 29 WebDataRequest::~WebDataRequest() { |
| 30 if (manager_) { | |
| 31 manager_->CancelRequest(handle_); | |
| 32 } | |
| 33 if (result_.get()) { | |
| 34 result_->Destroy(); | |
| 35 } | |
| 33 } | 36 } |
| 34 | 37 |
| 35 WebDataService::Handle WebDataRequest::GetHandle() const { | 38 WebDataService::Handle WebDataRequest::GetHandle() const { |
| 36 return handle_; | 39 return handle_; |
| 37 } | 40 } |
| 38 | 41 |
| 39 WebDataServiceConsumer* WebDataRequest::GetConsumer() const { | 42 WebDataServiceConsumer* WebDataRequest::GetConsumer() const { |
| 40 return consumer_; | 43 return consumer_; |
| 41 } | 44 } |
| 42 | 45 |
| 46 MessageLoop* WebDataRequest::GetMessageLoop() const { | |
| 47 return message_loop_; | |
| 48 } | |
| 49 | |
| 43 bool WebDataRequest::IsCancelled() const { | 50 bool WebDataRequest::IsCancelled() const { |
| 44 base::AutoLock l(cancel_lock_); | 51 base::AutoLock l(cancel_lock_); |
| 45 return cancelled_; | 52 return cancelled_; |
| 46 } | 53 } |
| 47 | 54 |
| 48 void WebDataRequest::Cancel() { | 55 void WebDataRequest::Cancel() { |
| 49 base::AutoLock l(cancel_lock_); | 56 base::AutoLock l(cancel_lock_); |
| 50 cancelled_ = true; | 57 cancelled_ = true; |
| 51 consumer_ = NULL; | 58 consumer_ = NULL; |
| 59 manager_ = NULL; | |
| 60 } | |
| 61 | |
| 62 void WebDataRequest::OnComplete() { | |
| 63 manager_= NULL; | |
| 52 } | 64 } |
| 53 | 65 |
| 54 void WebDataRequest::SetResult(scoped_ptr<WDTypedResult> r) { | 66 void WebDataRequest::SetResult(scoped_ptr<WDTypedResult> r) { |
| 55 result_ = r.Pass(); | 67 result_ = r.Pass(); |
| 56 } | 68 } |
| 57 | 69 |
| 58 const WDTypedResult* WebDataRequest::GetResult() const { | 70 scoped_ptr<WDTypedResult> WebDataRequest::GetResult(){ |
| 59 return result_.get(); | 71 return result_.Pass(); |
| 60 } | |
| 61 | |
| 62 void WebDataRequest::RequestComplete() { | |
| 63 message_loop_->PostTask(FROM_HERE, Bind(&WebDataService::RequestCompleted, | |
| 64 service_.get(), handle_)); | |
| 65 } | 72 } |
| 66 | 73 |
| 67 //////////////////////////////////////////////////////////////////////////////// | 74 //////////////////////////////////////////////////////////////////////////////// |
| 68 // | 75 // |
| 69 // WebDataRequestManager implementation. | 76 // WebDataRequestManager implementation. |
| 70 // | 77 // |
| 71 //////////////////////////////////////////////////////////////////////////////// | 78 //////////////////////////////////////////////////////////////////////////////// |
| 72 | 79 |
| 73 WebDataRequestManager::WebDataRequestManager() | 80 WebDataRequestManager::WebDataRequestManager() |
| 74 : next_request_handle_(1) { | 81 : next_request_handle_(1) { |
| 75 } | 82 } |
| 76 | 83 |
| 77 WebDataRequestManager::~WebDataRequestManager() { | 84 WebDataRequestManager::~WebDataRequestManager() { |
| 85 base::AutoLock l(pending_lock_); | |
| 86 for (RequestMap::iterator i = pending_requests_.begin(); | |
| 87 i != pending_requests_.end(); ++i) { | |
| 88 i->second->Cancel(); | |
| 89 } | |
| 90 pending_requests_.clear(); | |
| 78 } | 91 } |
| 79 | 92 |
| 80 void WebDataRequestManager::RegisterRequest(WebDataRequest* request) { | 93 void WebDataRequestManager::RegisterRequest(WebDataRequest* request) { |
| 81 base::AutoLock l(pending_lock_); | 94 base::AutoLock l(pending_lock_); |
| 82 pending_requests_[request->GetHandle()] = request; | 95 pending_requests_[request->GetHandle()] = request; |
| 83 } | 96 } |
| 84 | 97 |
| 85 int WebDataRequestManager::GetNextRequestHandle() { | 98 int WebDataRequestManager::GetNextRequestHandle() { |
| 86 base::AutoLock l(pending_lock_); | 99 base::AutoLock l(pending_lock_); |
| 87 return ++next_request_handle_; | 100 return ++next_request_handle_; |
| 88 } | 101 } |
| 89 | 102 |
| 90 void WebDataRequestManager::CancelRequest(WebDataServiceBase::Handle h) { | 103 void WebDataRequestManager::CancelRequest(WebDataServiceBase::Handle h) { |
| 91 base::AutoLock l(pending_lock_); | 104 base::AutoLock l(pending_lock_); |
| 92 RequestMap::iterator i = pending_requests_.find(h); | 105 RequestMap::iterator i = pending_requests_.find(h); |
| 93 if (i == pending_requests_.end()) { | 106 if (i == pending_requests_.end()) |
| 94 NOTREACHED() << "Canceling a nonexistent web data service request"; | 107 NOTREACHED() << "Canceling a nonexistent web data service request"; |
|
erikwright (departed)
2013/01/28 20:01:05
If you are really convinced this can never happen,
Cait (Slow)
2013/01/29 01:10:29
Done.
| |
| 95 return; | |
| 96 } | |
| 97 i->second->Cancel(); | 108 i->second->Cancel(); |
| 109 pending_requests_.erase(i); | |
| 98 } | 110 } |
| 99 | 111 |
| 100 void WebDataRequestManager::RequestCompleted(WebDataServiceBase::Handle h) { | 112 void WebDataRequestManager::RequestCompleted( |
| 113 scoped_ptr<WebDataRequest> request) { | |
| 114 MessageLoop* loop = request->GetMessageLoop(); | |
| 115 loop->PostTask(FROM_HERE, base::Bind( | |
| 116 &WebDataRequestManager::RequestCompletedOnThread, | |
| 117 this, | |
| 118 base::Passed(&request))); | |
| 119 } | |
| 120 | |
| 121 void WebDataRequestManager::RequestCompletedOnThread( | |
| 122 scoped_ptr<WebDataRequest> request) { | |
| 123 if (request->IsCancelled()) | |
| 124 return; | |
| 101 pending_lock_.Acquire(); | 125 pending_lock_.Acquire(); |
|
erikwright (departed)
2013/01/28 20:01:05
Can you go ahead and convert this to an auto-lock?
Cait (Slow)
2013/01/29 01:10:29
Done.
| |
| 102 RequestMap::iterator i = pending_requests_.find(h); | 126 RequestMap::iterator i = pending_requests_.find(request->GetHandle()); |
| 103 if (i == pending_requests_.end()) { | 127 if (i == pending_requests_.end()) { |
| 104 NOTREACHED() << "Request completed called for an unknown request"; | 128 NOTREACHED() << "Request completed called for an unknown request"; |
| 105 pending_lock_.Release(); | 129 pending_lock_.Release(); |
| 106 return; | 130 return; |
| 107 } | 131 } |
| 108 | 132 |
| 109 // Take ownership of the request object and remove it from the map. | 133 // Take ownership of the request object and remove it from the map. |
| 110 scoped_ptr<WebDataRequest> request(i->second); | |
| 111 pending_requests_.erase(i); | 134 pending_requests_.erase(i); |
| 112 pending_lock_.Release(); | 135 pending_lock_.Release(); |
| 113 | 136 |
| 114 // Notify the consumer if needed. | 137 // Notify the consumer if needed. |
| 115 WebDataServiceConsumer* consumer = request->GetConsumer(); | 138 if (!request->IsCancelled()) { |
| 116 if (!request->IsCancelled() && consumer) { | 139 WebDataServiceConsumer* consumer = request->GetConsumer(); |
| 117 consumer->OnWebDataServiceRequestDone(request->GetHandle(), | 140 request->OnComplete(); |
| 118 request->GetResult()); | 141 if (consumer) { |
| 119 } else { | 142 scoped_ptr<WDTypedResult> r = request->GetResult(); |
| 120 // Nobody is taken ownership of the result, either because it is cancelled | 143 consumer->OnWebDataServiceRequestDone(request->GetHandle(), |
|
erikwright (departed)
2013/01/28 20:01:05
This looks like it might not need to wrap.
Cait (Slow)
2013/01/29 01:10:29
Done.
| |
| 121 // or there is no consumer. Destroy results that require special handling. | 144 r.get()); |
| 122 WDTypedResult const *result = request->GetResult(); | |
| 123 if (result) { | |
| 124 result->Destroy(); | |
| 125 } | 145 } |
| 126 } | 146 } |
| 147 | |
| 127 } | 148 } |
| OLD | NEW |