Add an UMA stat to be able to see if the User pods are show on start screen,
[chromium-blink-merge.git] / components / webdata / common / web_data_request_manager.cc
blob9a928556a26050d8acd074bfb9918a58417d1449
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/message_loop/message_loop.h"
9 #include "base/profiler/scoped_tracker.h"
10 #include "base/stl_util.h"
12 ////////////////////////////////////////////////////////////////////////////////
14 // WebDataRequest implementation.
16 ////////////////////////////////////////////////////////////////////////////////
18 WebDataRequest::WebDataRequest(WebDataServiceConsumer* consumer,
19 WebDataRequestManager* manager)
20 : manager_(manager), cancelled_(false), consumer_(consumer) {
21 handle_ = manager_->GetNextRequestHandle();
22 message_loop_ = base::MessageLoop::current();
23 manager_->RegisterRequest(this);
26 WebDataRequest::~WebDataRequest() {
27 if (manager_) {
28 manager_->CancelRequest(handle_);
30 if (result_.get()) {
31 result_->Destroy();
35 WebDataServiceBase::Handle WebDataRequest::GetHandle() const {
36 return handle_;
39 WebDataServiceConsumer* WebDataRequest::GetConsumer() const {
40 return consumer_;
43 base::MessageLoop* WebDataRequest::GetMessageLoop() const {
44 return message_loop_;
47 bool WebDataRequest::IsCancelled() const {
48 base::AutoLock l(cancel_lock_);
49 return cancelled_;
52 void WebDataRequest::Cancel() {
53 base::AutoLock l(cancel_lock_);
54 cancelled_ = true;
55 consumer_ = NULL;
56 manager_ = NULL;
59 void WebDataRequest::OnComplete() {
60 manager_= NULL;
63 void WebDataRequest::SetResult(scoped_ptr<WDTypedResult> r) {
64 result_ = r.Pass();
67 scoped_ptr<WDTypedResult> WebDataRequest::GetResult(){
68 return result_.Pass();
71 ////////////////////////////////////////////////////////////////////////////////
73 // WebDataRequestManager implementation.
75 ////////////////////////////////////////////////////////////////////////////////
77 WebDataRequestManager::WebDataRequestManager()
78 : next_request_handle_(1) {
81 WebDataRequestManager::~WebDataRequestManager() {
82 base::AutoLock l(pending_lock_);
83 for (RequestMap::iterator i = pending_requests_.begin();
84 i != pending_requests_.end(); ++i) {
85 i->second->Cancel();
87 pending_requests_.clear();
90 void WebDataRequestManager::RegisterRequest(WebDataRequest* request) {
91 base::AutoLock l(pending_lock_);
92 pending_requests_[request->GetHandle()] = request;
95 int WebDataRequestManager::GetNextRequestHandle() {
96 base::AutoLock l(pending_lock_);
97 return ++next_request_handle_;
100 void WebDataRequestManager::CancelRequest(WebDataServiceBase::Handle h) {
101 base::AutoLock l(pending_lock_);
102 RequestMap::iterator i = pending_requests_.find(h);
103 if (i == pending_requests_.end()) {
104 NOTREACHED() << "Canceling a nonexistent web data service request";
105 return;
107 i->second->Cancel();
108 pending_requests_.erase(i);
111 void WebDataRequestManager::RequestCompleted(
112 scoped_ptr<WebDataRequest> request) {
113 base::MessageLoop* loop = request->GetMessageLoop();
114 loop->PostTask(FROM_HERE,
115 base::Bind(&WebDataRequestManager::RequestCompletedOnThread,
116 this,
117 base::Passed(&request)));
120 void WebDataRequestManager::RequestCompletedOnThread(
121 scoped_ptr<WebDataRequest> request) {
122 if (request->IsCancelled())
123 return;
125 // TODO(robliao): Remove ScopedTracker below once https://crbug.com/422460 is
126 // fixed.
127 tracked_objects::ScopedTracker tracking_profile1(
128 FROM_HERE_WITH_EXPLICIT_FUNCTION(
129 "422460 WebDataRequestManager::RequestCompletedOnThread::UpdateMap"));
131 base::AutoLock l(pending_lock_);
132 RequestMap::iterator i = pending_requests_.find(request->GetHandle());
133 if (i == pending_requests_.end()) {
134 NOTREACHED() << "Request completed called for an unknown request";
135 return;
138 // Take ownership of the request object and remove it from the map.
139 pending_requests_.erase(i);
142 // TODO(robliao): Remove ScopedTracker below once https://crbug.com/422460 is
143 // fixed.
144 tracked_objects::ScopedTracker tracking_profile2(
145 FROM_HERE_WITH_EXPLICIT_FUNCTION(
146 "422460 "
147 "WebDataRequestManager::RequestCompletedOnThread::NotifyConsumer"));
149 // Notify the consumer if needed.
150 if (!request->IsCancelled()) {
151 WebDataServiceConsumer* consumer = request->GetConsumer();
152 request->OnComplete();
153 if (consumer) {
154 scoped_ptr<WDTypedResult> r = request->GetResult();
155 consumer->OnWebDataServiceRequestDone(request->GetHandle(), r.get());