Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / components / webdata / common / web_data_request_manager.cc
blob701f8f69959cec82513d621b7b5fa2f12647d67c
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"
7 #include "base/bind.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() {
29 if (manager_) {
30 manager_->CancelRequest(handle_);
32 if (result_.get()) {
33 result_->Destroy();
37 WebDataServiceBase::Handle WebDataRequest::GetHandle() const {
38 return handle_;
41 WebDataServiceConsumer* WebDataRequest::GetConsumer() const {
42 return consumer_;
45 base::MessageLoop* WebDataRequest::GetMessageLoop() const {
46 return message_loop_;
49 bool WebDataRequest::IsCancelled() const {
50 base::AutoLock l(cancel_lock_);
51 return cancelled_;
54 void WebDataRequest::Cancel() {
55 base::AutoLock l(cancel_lock_);
56 cancelled_ = true;
57 consumer_ = NULL;
58 manager_ = NULL;
61 void WebDataRequest::OnComplete() {
62 manager_= NULL;
65 void WebDataRequest::SetResult(scoped_ptr<WDTypedResult> r) {
66 result_ = r.Pass();
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) {
87 i->second->Cancel();
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";
107 return;
109 i->second->Cancel();
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())
124 return;
126 // TODO(robliao): Remove ScopedTracker below once https://crbug.com/422460 is
127 // fixed.
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";
136 return;
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
144 // fixed.
145 tracked_objects::ScopedTracker tracking_profile2(
146 FROM_HERE_WITH_EXPLICIT_FUNCTION(
147 "422460 "
148 "WebDataRequestManager::RequestCompletedOnThread::NotifyConsumer"));
150 // Notify the consumer if needed.
151 if (!request->IsCancelled()) {
152 WebDataServiceConsumer* consumer = request->GetConsumer();
153 request->OnComplete();
154 if (consumer) {
155 scoped_ptr<WDTypedResult> r = request->GetResult();
156 consumer->OnWebDataServiceRequestDone(request->GetHandle(), r.get());