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.
5 #include "components/webdata/common/web_data_request_manager.h"
8 #include "base/location.h"
9 #include "base/message_loop/message_loop.h"
10 #include "base/profiler/scoped_tracker.h"
11 #include "base/single_thread_task_runner.h"
12 #include "base/stl_util.h"
14 ////////////////////////////////////////////////////////////////////////////////
16 // WebDataRequest implementation.
18 ////////////////////////////////////////////////////////////////////////////////
20 WebDataRequest::WebDataRequest(WebDataServiceConsumer
* consumer
,
21 WebDataRequestManager
* manager
)
22 : manager_(manager
), cancelled_(false), consumer_(consumer
) {
23 handle_
= manager_
->GetNextRequestHandle();
24 message_loop_
= base::MessageLoop::current();
25 manager_
->RegisterRequest(this);
28 WebDataRequest::~WebDataRequest() {
30 manager_
->CancelRequest(handle_
);
37 WebDataServiceBase::Handle
WebDataRequest::GetHandle() const {
41 WebDataServiceConsumer
* WebDataRequest::GetConsumer() const {
45 base::MessageLoop
* WebDataRequest::GetMessageLoop() const {
49 bool WebDataRequest::IsCancelled() const {
50 base::AutoLock
l(cancel_lock_
);
54 void WebDataRequest::Cancel() {
55 base::AutoLock
l(cancel_lock_
);
61 void WebDataRequest::OnComplete() {
65 void WebDataRequest::SetResult(scoped_ptr
<WDTypedResult
> r
) {
69 scoped_ptr
<WDTypedResult
> WebDataRequest::GetResult(){
70 return result_
.Pass();
73 ////////////////////////////////////////////////////////////////////////////////
75 // WebDataRequestManager implementation.
77 ////////////////////////////////////////////////////////////////////////////////
79 WebDataRequestManager::WebDataRequestManager()
80 : next_request_handle_(1) {
83 WebDataRequestManager::~WebDataRequestManager() {
84 base::AutoLock
l(pending_lock_
);
85 for (RequestMap::iterator i
= pending_requests_
.begin();
86 i
!= pending_requests_
.end(); ++i
) {
89 pending_requests_
.clear();
92 void WebDataRequestManager::RegisterRequest(WebDataRequest
* request
) {
93 base::AutoLock
l(pending_lock_
);
94 pending_requests_
[request
->GetHandle()] = request
;
97 int WebDataRequestManager::GetNextRequestHandle() {
98 base::AutoLock
l(pending_lock_
);
99 return ++next_request_handle_
;
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";
110 pending_requests_
.erase(i
);
113 void WebDataRequestManager::RequestCompleted(
114 scoped_ptr
<WebDataRequest
> request
) {
115 base::MessageLoop
* loop
= request
->GetMessageLoop();
116 loop
->task_runner()->PostTask(
117 FROM_HERE
, base::Bind(&WebDataRequestManager::RequestCompletedOnThread
,
118 this, base::Passed(&request
)));
121 void WebDataRequestManager::RequestCompletedOnThread(
122 scoped_ptr
<WebDataRequest
> request
) {
123 if (request
->IsCancelled())
126 // TODO(robliao): Remove ScopedTracker below once https://crbug.com/422460 is
128 tracked_objects::ScopedTracker
tracking_profile1(
129 FROM_HERE_WITH_EXPLICIT_FUNCTION(
130 "422460 WebDataRequestManager::RequestCompletedOnThread::UpdateMap"));
132 base::AutoLock
l(pending_lock_
);
133 RequestMap::iterator i
= pending_requests_
.find(request
->GetHandle());
134 if (i
== pending_requests_
.end()) {
135 NOTREACHED() << "Request completed called for an unknown request";
139 // Take ownership of the request object and remove it from the map.
140 pending_requests_
.erase(i
);
143 // TODO(robliao): Remove ScopedTracker below once https://crbug.com/422460 is
145 tracked_objects::ScopedTracker
tracking_profile2(
146 FROM_HERE_WITH_EXPLICIT_FUNCTION(
148 "WebDataRequestManager::RequestCompletedOnThread::NotifyConsumer"));
150 // Notify the consumer if needed.
151 if (!request
->IsCancelled()) {
152 WebDataServiceConsumer
* consumer
= request
->GetConsumer();
153 request
->OnComplete();
155 scoped_ptr
<WDTypedResult
> r
= request
->GetResult();
156 consumer
->OnWebDataServiceRequestDone(request
->GetHandle(), r
.get());