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 | |
11 //////////////////////////////////////////////////////////////////////////////// | |
12 // | |
13 // WebDataRequest implementation. | |
14 // | |
15 //////////////////////////////////////////////////////////////////////////////// | |
16 | |
17 WebDataRequest::WebDataRequest(WebDataServiceConsumer* consumer, | |
18 WebDataRequestManager* manager) | |
19 : manager_(manager), | |
20 cancelled_(false), | |
21 consumer_(consumer), | |
22 result_(NULL) { | |
23 handle_ = manager_->GetNextRequestHandle(); | |
24 message_loop_ = MessageLoop::current(); | |
25 manager_->RegisterRequest(this); | |
26 } | |
27 | |
28 WebDataRequest::~WebDataRequest() { | |
29 if (manager_) { | |
30 manager_->CancelRequest(handle_); | |
31 } | |
32 if (result_.get()) { | |
33 result_->Destroy(); | |
34 } | |
35 } | |
36 | |
37 WebDataServiceBase::Handle WebDataRequest::GetHandle() const { | |
38 return handle_; | |
39 } | |
40 | |
41 WebDataServiceConsumer* WebDataRequest::GetConsumer() const { | |
42 return consumer_; | |
43 } | |
44 | |
45 MessageLoop* WebDataRequest::GetMessageLoop() const { | |
46 return message_loop_; | |
47 } | |
48 | |
49 bool WebDataRequest::IsCancelled() const { | |
50 base::AutoLock l(cancel_lock_); | |
51 return cancelled_; | |
52 } | |
53 | |
54 void WebDataRequest::Cancel() { | |
55 base::AutoLock l(cancel_lock_); | |
56 cancelled_ = true; | |
57 consumer_ = NULL; | |
58 manager_ = NULL; | |
59 } | |
60 | |
61 void WebDataRequest::OnComplete() { | |
62 manager_= NULL; | |
63 } | |
64 | |
65 void WebDataRequest::SetResult(scoped_ptr<WDTypedResult> r) { | |
66 result_ = r.Pass(); | |
67 } | |
68 | |
69 scoped_ptr<WDTypedResult> WebDataRequest::GetResult(){ | |
70 return result_.Pass(); | |
71 } | |
72 | |
73 //////////////////////////////////////////////////////////////////////////////// | |
74 // | |
75 // WebDataRequestManager implementation. | |
76 // | |
77 //////////////////////////////////////////////////////////////////////////////// | |
78 | |
79 WebDataRequestManager::WebDataRequestManager() | |
80 : next_request_handle_(1) { | |
81 } | |
82 | |
83 WebDataRequestManager::~WebDataRequestManager() { | |
84 base::AutoLock l(pending_lock_); | |
85 for (RequestMap::iterator i = pending_requests_.begin(); | |
86 i != pending_requests_.end(); ++i) { | |
87 i->second->Cancel(); | |
88 } | |
89 pending_requests_.clear(); | |
90 } | |
91 | |
92 void WebDataRequestManager::RegisterRequest(WebDataRequest* request) { | |
93 base::AutoLock l(pending_lock_); | |
94 pending_requests_[request->GetHandle()] = request; | |
95 } | |
96 | |
97 int WebDataRequestManager::GetNextRequestHandle() { | |
98 base::AutoLock l(pending_lock_); | |
99 return ++next_request_handle_; | |
100 } | |
101 | |
102 void WebDataRequestManager::CancelRequest(WebDataServiceBase::Handle h) { | |
103 base::AutoLock l(pending_lock_); | |
104 RequestMap::iterator i = pending_requests_.find(h); | |
105 if (i == pending_requests_.end()) { | |
106 NOTREACHED() << "Canceling a nonexistent web data service request"; | |
107 return; | |
108 } | |
109 i->second->Cancel(); | |
110 pending_requests_.erase(i); | |
111 } | |
112 | |
113 void WebDataRequestManager::RequestCompleted( | |
114 scoped_ptr<WebDataRequest> request) { | |
115 MessageLoop* loop = request->GetMessageLoop(); | |
116 loop->PostTask(FROM_HERE, base::Bind( | |
117 &WebDataRequestManager::RequestCompletedOnThread, | |
118 this, | |
119 base::Passed(&request))); | |
120 } | |
121 | |
122 void WebDataRequestManager::RequestCompletedOnThread( | |
123 scoped_ptr<WebDataRequest> request) { | |
124 if (request->IsCancelled()) | |
125 return; | |
126 { | |
127 base::AutoLock l(pending_lock_); | |
128 RequestMap::iterator i = pending_requests_.find(request->GetHandle()); | |
129 if (i == pending_requests_.end()) { | |
130 NOTREACHED() << "Request completed called for an unknown request"; | |
131 return; | |
132 } | |
133 | |
134 // Take ownership of the request object and remove it from the map. | |
135 pending_requests_.erase(i); | |
136 } | |
137 | |
138 // Notify the consumer if needed. | |
139 if (!request->IsCancelled()) { | |
140 WebDataServiceConsumer* consumer = request->GetConsumer(); | |
141 request->OnComplete(); | |
142 if (consumer) { | |
143 scoped_ptr<WDTypedResult> r = request->GetResult(); | |
144 consumer->OnWebDataServiceRequestDone(request->GetHandle(), r.get()); | |
145 } | |
146 } | |
147 | |
148 } | |
OLD | NEW |