OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/webdata/web_data_request_manager.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/message_loop.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" |
| 13 |
| 14 //////////////////////////////////////////////////////////////////////////////// |
| 15 // |
| 16 // WebDataRequest implementation. |
| 17 // |
| 18 //////////////////////////////////////////////////////////////////////////////// |
| 19 |
| 20 WebDataRequest::WebDataRequest(WebDataService* service, |
| 21 WebDataServiceConsumer* consumer, |
| 22 WebDataRequestManager* manager) |
| 23 : service_(service), |
| 24 cancelled_(false), |
| 25 consumer_(consumer), |
| 26 result_(NULL) { |
| 27 handle_ = manager->GetNextRequestHandle(); |
| 28 message_loop_ = MessageLoop::current(); |
| 29 manager->RegisterRequest(this); |
| 30 } |
| 31 |
| 32 WebDataRequest::~WebDataRequest() { |
| 33 delete result_; |
| 34 } |
| 35 |
| 36 WebDataService::Handle WebDataRequest::GetHandle() const { |
| 37 return handle_; |
| 38 } |
| 39 |
| 40 WebDataServiceConsumer* WebDataRequest::GetConsumer() const { |
| 41 return consumer_; |
| 42 } |
| 43 |
| 44 bool WebDataRequest::IsCancelled() const { |
| 45 base::AutoLock l(cancel_lock_); |
| 46 return cancelled_; |
| 47 } |
| 48 |
| 49 void WebDataRequest::Cancel() { |
| 50 base::AutoLock l(cancel_lock_); |
| 51 cancelled_ = true; |
| 52 consumer_ = NULL; |
| 53 } |
| 54 |
| 55 void WebDataRequest::SetResult(WDTypedResult* r) { |
| 56 result_ = r; |
| 57 } |
| 58 |
| 59 const WDTypedResult* WebDataRequest::GetResult() const { |
| 60 return result_; |
| 61 } |
| 62 |
| 63 void WebDataRequest::RequestComplete() { |
| 64 message_loop_->PostTask(FROM_HERE, Bind(&WebDataService::RequestCompleted, |
| 65 service_.get(), handle_)); |
| 66 } |
| 67 |
| 68 //////////////////////////////////////////////////////////////////////////////// |
| 69 // |
| 70 // WebDataRequestManager implementation. |
| 71 // |
| 72 //////////////////////////////////////////////////////////////////////////////// |
| 73 |
| 74 WebDataRequestManager::WebDataRequestManager() |
| 75 : next_request_handle_(1) { |
| 76 } |
| 77 |
| 78 WebDataRequestManager::~WebDataRequestManager() { |
| 79 } |
| 80 |
| 81 void WebDataRequestManager::RegisterRequest(WebDataRequest* request) { |
| 82 base::AutoLock l(pending_lock_); |
| 83 pending_requests_[request->GetHandle()] = request; |
| 84 } |
| 85 |
| 86 int WebDataRequestManager::GetNextRequestHandle() { |
| 87 base::AutoLock l(pending_lock_); |
| 88 return ++next_request_handle_; |
| 89 } |
| 90 |
| 91 void WebDataRequestManager::CancelRequest(WebDataServiceBase::Handle h) { |
| 92 base::AutoLock l(pending_lock_); |
| 93 RequestMap::iterator i = pending_requests_.find(h); |
| 94 if (i == pending_requests_.end()) { |
| 95 NOTREACHED() << "Canceling a nonexistent web data service request"; |
| 96 return; |
| 97 } |
| 98 i->second->Cancel(); |
| 99 } |
| 100 |
| 101 void WebDataRequestManager::RequestCompleted(WebDataServiceBase::Handle h) { |
| 102 pending_lock_.Acquire(); |
| 103 RequestMap::iterator i = pending_requests_.find(h); |
| 104 if (i == pending_requests_.end()) { |
| 105 NOTREACHED() << "Request completed called for an unknown request"; |
| 106 pending_lock_.Release(); |
| 107 return; |
| 108 } |
| 109 |
| 110 // Take ownership of the request object and remove it from the map. |
| 111 scoped_ptr<WebDataRequest> request(i->second); |
| 112 pending_requests_.erase(i); |
| 113 pending_lock_.Release(); |
| 114 |
| 115 // Notify the consumer if needed. |
| 116 WebDataServiceConsumer* consumer = request->GetConsumer(); |
| 117 if (!request->IsCancelled() && consumer) { |
| 118 consumer->OnWebDataServiceRequestDone(request->GetHandle(), |
| 119 request->GetResult()); |
| 120 // TODO(caitkp): get rid of Autofill DEPS here. |
| 121 } else { |
| 122 // Nobody is taken ownership of the result, either because it is cancelled |
| 123 // or there is no consumer. Destroy results that require special handling. |
| 124 WDTypedResult const *result = request->GetResult(); |
| 125 if (result) { |
| 126 result->Destroy(); |
| 127 } |
| 128 } |
| 129 } |
OLD | NEW |