OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
dhollowa
2013/01/04 01:34:50
nit: remove the "(c)" bit.
Cait (Slow)
2013/01/04 22:59:29
Done.
| |
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 "base/threading/thread.h" | |
dhollowa
2013/01/04 01:34:50
not used
Cait (Slow)
2013/01/04 22:59:29
Done.
| |
11 #include "chrome/browser/autofill/autofill_profile.h" | |
12 #include "chrome/browser/autofill/credit_card.h" | |
13 #include "chrome/browser/webdata/web_data_service.h" | |
14 | |
15 WebDataRequestManager::WebDataRequestManager() | |
16 : next_request_handle_(1) { | |
17 } | |
18 | |
19 void WebDataRequestManager::RegisterRequest(WebDataRequest* request) { | |
20 base::AutoLock l(pending_lock_); | |
21 pending_requests_[request->GetHandle()] = request; | |
22 } | |
23 | |
24 int WebDataRequestManager::GetNextRequestHandle() { | |
25 base::AutoLock l(pending_lock_); | |
26 return ++next_request_handle_; | |
27 } | |
28 | |
29 void WebDataRequestManager::CancelRequest(WebDataServiceBase::Handle h) { | |
30 base::AutoLock l(pending_lock_); | |
31 RequestMap::iterator i = pending_requests_.find(h); | |
32 if (i == pending_requests_.end()) { | |
33 NOTREACHED() << "Canceling a nonexistent web data service request"; | |
34 return; | |
35 } | |
36 i->second->Cancel(); | |
37 } | |
38 | |
39 void WebDataRequestManager::RequestCompleted(WebDataServiceBase::Handle h) { | |
40 pending_lock_.Acquire(); | |
41 RequestMap::iterator i = pending_requests_.find(h); | |
42 if (i == pending_requests_.end()) { | |
43 NOTREACHED() << "Request completed called for an unknown request"; | |
44 pending_lock_.Release(); | |
45 return; | |
46 } | |
47 | |
48 // Take ownership of the request object and remove it from the map. | |
49 scoped_ptr<WebDataRequest> request(i->second); | |
50 pending_requests_.erase(i); | |
51 pending_lock_.Release(); | |
52 | |
53 // Notify the consumer if needed. | |
54 WebDataServiceConsumer* consumer = NULL; | |
55 if (!request->IsCancelled(&consumer) && consumer) { | |
56 consumer->OnWebDataServiceRequestDone(request->GetHandle(), | |
57 request->GetResult()); | |
58 // TODO(caitkp): get rid of Autofill DEPS here. | |
dhollowa
2013/01/04 01:34:50
Yes, this is unfortunate. A possible solution wou
Cait (Slow)
2013/01/04 22:59:29
I added the optional callback and |Destroy| method
dhollowa
2013/01/04 23:57:36
Agreed. A second constructor would solve this.
O
| |
59 } else { | |
60 // Nobody is taken ownership of the result, either because it is cancelled | |
61 // or there is no consumer. Destroy results that require special handling. | |
62 WDTypedResult const *result = request->GetResult(); | |
63 if (result) { | |
64 if (result->GetType() == AUTOFILL_PROFILES_RESULT) { | |
65 const WDResult<std::vector<AutofillProfile*> >* r = | |
66 static_cast<const WDResult<std::vector<AutofillProfile*> >*>( | |
67 result); | |
68 std::vector<AutofillProfile*> profiles = r->GetValue(); | |
69 STLDeleteElements(&profiles); | |
70 } else if (result->GetType() == AUTOFILL_CREDITCARDS_RESULT) { | |
71 const WDResult<std::vector<CreditCard*> >* r = | |
72 static_cast<const WDResult<std::vector<CreditCard*> >*>(result); | |
73 | |
74 std::vector<CreditCard*> credit_cards = r->GetValue(); | |
75 STLDeleteElements(&credit_cards); | |
76 } | |
77 } | |
78 } | |
79 } | |
80 | |
81 //////////////////////////////////////////////////////////////////////////////// | |
82 // | |
83 // WebDataRequest implementation. | |
84 // | |
85 //////////////////////////////////////////////////////////////////////////////// | |
86 | |
87 WebDataRequest::WebDataRequest(WebDataService* service, | |
88 WebDataService::Handle handle, | |
89 WebDataServiceConsumer* consumer) | |
90 : service_(service), | |
91 handle_(handle), | |
92 cancelled_(false), | |
93 consumer_(consumer), | |
94 result_(NULL) { | |
95 message_loop_ = MessageLoop::current(); | |
96 } | |
97 | |
98 WebDataRequest::~WebDataRequest() { | |
99 delete result_; | |
100 } | |
101 | |
102 WebDataService::Handle WebDataRequest::GetHandle() const { | |
103 return handle_; | |
104 } | |
105 | |
106 bool WebDataRequest::IsCancelled( | |
107 WebDataServiceConsumer** consumer) const { | |
108 base::AutoLock l(cancel_lock_); | |
109 if (consumer) | |
110 *consumer = consumer_; | |
111 return cancelled_; | |
112 } | |
113 | |
114 void WebDataRequest::Cancel() { | |
115 base::AutoLock l(cancel_lock_); | |
116 cancelled_ = true; | |
117 consumer_ = NULL; | |
118 } | |
119 | |
120 void WebDataRequest::SetResult(WDTypedResult* r) { | |
121 result_ = r; | |
122 } | |
123 | |
124 const WDTypedResult* WebDataRequest::GetResult() const { | |
125 return result_; | |
126 } | |
127 | |
128 void WebDataRequest::RequestComplete() { | |
129 message_loop_->PostTask(FROM_HERE, Bind(&WebDataService::RequestCompleted, | |
130 service_.get(), handle_)); | |
131 } | |
OLD | NEW |