Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(71)

Side by Side Diff: chrome/browser/webdata/web_data_request_manager.cc

Issue 11761016: Separate WebDataRequest functionality from WebDataService (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Add second ctor Created 7 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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 #include "chrome/browser/autofill/autofill_profile.h"
11 #include "chrome/browser/autofill/credit_card.h"
12 #include "chrome/browser/webdata/web_data_service.h"
13
14 ////////////////////////////////////////////////////////////////////////////////
15 //
16 // WebDataRequest implementation.
17 //
18 ////////////////////////////////////////////////////////////////////////////////
19
20 WebDataRequest::WebDataRequest(WebDataService* service,
21 WebDataServiceConsumer* consumer,
22 WebDataRequestManager* manager)
23 : service_(service),
24 cancelled_(false),
25 consumer_(consumer),
26 result_(NULL) {
27 handle_ = manager->GetNextRequestHandle();
28 message_loop_ = MessageLoop::current();
29 manager->RegisterRequest(this);
30 }
31
32 WebDataRequest::~WebDataRequest() {
33 delete result_;
34 }
35
36 WebDataService::Handle WebDataRequest::GetHandle() const {
37 return handle_;
38 }
39
40 WebDataServiceConsumer* WebDataRequest::GetConsumer() const {
41 return consumer_;
42 }
43
44 bool WebDataRequest::IsCancelled() const {
45 base::AutoLock l(cancel_lock_);
46 return cancelled_;
47 }
48
49 void WebDataRequest::Cancel() {
50 base::AutoLock l(cancel_lock_);
51 cancelled_ = true;
52 consumer_ = NULL;
53 }
54
55 void WebDataRequest::SetResult(WDTypedResult* r) {
56 result_ = r;
57 }
58
59 const WDTypedResult* WebDataRequest::GetResult() const {
60 return result_;
61 }
62
63 void WebDataRequest::RequestComplete() {
64 message_loop_->PostTask(FROM_HERE, Bind(&WebDataService::RequestCompleted,
65 service_.get(), handle_));
66 }
67
68 ////////////////////////////////////////////////////////////////////////////////
69 //
70 // WebDataRequestManager implementation.
71 //
72 ////////////////////////////////////////////////////////////////////////////////
73
74 WebDataRequestManager::WebDataRequestManager()
75 : next_request_handle_(1) {
76 }
77
78 void WebDataRequestManager::RegisterRequest(WebDataRequest* request) {
79 base::AutoLock l(pending_lock_);
80 pending_requests_[request->GetHandle()] = request;
81 }
82
83 int WebDataRequestManager::GetNextRequestHandle() {
84 base::AutoLock l(pending_lock_);
85 return ++next_request_handle_;
86 }
87
88 void WebDataRequestManager::CancelRequest(WebDataServiceBase::Handle h) {
89 base::AutoLock l(pending_lock_);
90 RequestMap::iterator i = pending_requests_.find(h);
91 if (i == pending_requests_.end()) {
92 NOTREACHED() << "Canceling a nonexistent web data service request";
93 return;
94 }
95 i->second->Cancel();
96 }
97
98 void WebDataRequestManager::RequestCompleted(WebDataServiceBase::Handle h) {
99 pending_lock_.Acquire();
100 RequestMap::iterator i = pending_requests_.find(h);
101 if (i == pending_requests_.end()) {
102 NOTREACHED() << "Request completed called for an unknown request";
103 pending_lock_.Release();
104 return;
105 }
106
107 // Take ownership of the request object and remove it from the map.
108 scoped_ptr<WebDataRequest> request(i->second);
109 pending_requests_.erase(i);
110 pending_lock_.Release();
111
112 // Notify the consumer if needed.
113 WebDataServiceConsumer* consumer = request->GetConsumer();
114 if (!request->IsCancelled() && consumer) {
115 consumer->OnWebDataServiceRequestDone(request->GetHandle(),
116 request->GetResult());
117 // TODO(caitkp): get rid of Autofill DEPS here.
118 } else {
119 // Nobody is taken ownership of the result, either because it is cancelled
120 // or there is no consumer. Destroy results that require special handling.
121 WDTypedResult const *result = request->GetResult();
122 if (result) {
123 result->Destroy();
124 }
125 }
126 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698