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/message_loop/message_loop.h"
9 #include "base/stl_util.h"
11 ////////////////////////////////////////////////////////////////////////////////
13 // WebDataRequest implementation.
15 ////////////////////////////////////////////////////////////////////////////////
17 WebDataRequest::WebDataRequest(WebDataServiceConsumer* consumer,
18 WebDataRequestManager* manager)
19 : manager_(manager), cancelled_(false), consumer_(consumer) {
20 handle_ = manager_->GetNextRequestHandle();
21 message_loop_ = base::MessageLoop::current();
22 manager_->RegisterRequest(this);
25 WebDataRequest::~WebDataRequest() {
27 manager_->CancelRequest(handle_);
34 WebDataServiceBase::Handle WebDataRequest::GetHandle() const {
38 WebDataServiceConsumer* WebDataRequest::GetConsumer() const {
42 base::MessageLoop* WebDataRequest::GetMessageLoop() const {
46 bool WebDataRequest::IsCancelled() const {
47 base::AutoLock l(cancel_lock_);
51 void WebDataRequest::Cancel() {
52 base::AutoLock l(cancel_lock_);
58 void WebDataRequest::OnComplete() {
62 void WebDataRequest::SetResult(scoped_ptr<WDTypedResult> r) {
66 scoped_ptr<WDTypedResult> WebDataRequest::GetResult(){
67 return result_.Pass();
70 ////////////////////////////////////////////////////////////////////////////////
72 // WebDataRequestManager implementation.
74 ////////////////////////////////////////////////////////////////////////////////
76 WebDataRequestManager::WebDataRequestManager()
77 : next_request_handle_(1) {
80 WebDataRequestManager::~WebDataRequestManager() {
81 base::AutoLock l(pending_lock_);
82 for (RequestMap::iterator i = pending_requests_.begin();
83 i != pending_requests_.end(); ++i) {
86 pending_requests_.clear();
89 void WebDataRequestManager::RegisterRequest(WebDataRequest* request) {
90 base::AutoLock l(pending_lock_);
91 pending_requests_[request->GetHandle()] = request;
94 int WebDataRequestManager::GetNextRequestHandle() {
95 base::AutoLock l(pending_lock_);
96 return ++next_request_handle_;
99 void WebDataRequestManager::CancelRequest(WebDataServiceBase::Handle h) {
100 base::AutoLock l(pending_lock_);
101 RequestMap::iterator i = pending_requests_.find(h);
102 if (i == pending_requests_.end()) {
103 NOTREACHED() << "Canceling a nonexistent web data service request";
107 pending_requests_.erase(i);
110 void WebDataRequestManager::RequestCompleted(
111 scoped_ptr<WebDataRequest> request) {
112 base::MessageLoop* loop = request->GetMessageLoop();
113 loop->PostTask(FROM_HERE,
114 base::Bind(&WebDataRequestManager::RequestCompletedOnThread,
116 base::Passed(&request)));
119 void WebDataRequestManager::RequestCompletedOnThread(
120 scoped_ptr<WebDataRequest> request) {
121 if (request->IsCancelled())
124 base::AutoLock l(pending_lock_);
125 RequestMap::iterator i = pending_requests_.find(request->GetHandle());
126 if (i == pending_requests_.end()) {
127 NOTREACHED() << "Request completed called for an unknown request";
131 // Take ownership of the request object and remove it from the map.
132 pending_requests_.erase(i);
135 // Notify the consumer if needed.
136 if (!request->IsCancelled()) {
137 WebDataServiceConsumer* consumer = request->GetConsumer();
138 request->OnComplete();
140 scoped_ptr<WDTypedResult> r = request->GetResult();
141 consumer->OnWebDataServiceRequestDone(request->GetHandle(), r.get());