blob: c6e15f923365459dcc6a73a2dcd315e363d066f7 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
|
// Copyright 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/webdata/common/web_data_request_manager.h"
#include "base/bind.h"
#include "base/message_loop.h"
#include "base/stl_util.h"
////////////////////////////////////////////////////////////////////////////////
//
// WebDataRequest implementation.
//
////////////////////////////////////////////////////////////////////////////////
WebDataRequest::WebDataRequest(WebDataServiceConsumer* consumer,
WebDataRequestManager* manager)
: manager_(manager),
cancelled_(false),
consumer_(consumer),
result_(NULL) {
handle_ = manager_->GetNextRequestHandle();
message_loop_ = base::MessageLoop::current();
manager_->RegisterRequest(this);
}
WebDataRequest::~WebDataRequest() {
if (manager_) {
manager_->CancelRequest(handle_);
}
if (result_.get()) {
result_->Destroy();
}
}
WebDataServiceBase::Handle WebDataRequest::GetHandle() const {
return handle_;
}
WebDataServiceConsumer* WebDataRequest::GetConsumer() const {
return consumer_;
}
MessageLoop* WebDataRequest::GetMessageLoop() const {
return message_loop_;
}
bool WebDataRequest::IsCancelled() const {
base::AutoLock l(cancel_lock_);
return cancelled_;
}
void WebDataRequest::Cancel() {
base::AutoLock l(cancel_lock_);
cancelled_ = true;
consumer_ = NULL;
manager_ = NULL;
}
void WebDataRequest::OnComplete() {
manager_= NULL;
}
void WebDataRequest::SetResult(scoped_ptr<WDTypedResult> r) {
result_ = r.Pass();
}
scoped_ptr<WDTypedResult> WebDataRequest::GetResult(){
return result_.Pass();
}
////////////////////////////////////////////////////////////////////////////////
//
// WebDataRequestManager implementation.
//
////////////////////////////////////////////////////////////////////////////////
WebDataRequestManager::WebDataRequestManager()
: next_request_handle_(1) {
}
WebDataRequestManager::~WebDataRequestManager() {
base::AutoLock l(pending_lock_);
for (RequestMap::iterator i = pending_requests_.begin();
i != pending_requests_.end(); ++i) {
i->second->Cancel();
}
pending_requests_.clear();
}
void WebDataRequestManager::RegisterRequest(WebDataRequest* request) {
base::AutoLock l(pending_lock_);
pending_requests_[request->GetHandle()] = request;
}
int WebDataRequestManager::GetNextRequestHandle() {
base::AutoLock l(pending_lock_);
return ++next_request_handle_;
}
void WebDataRequestManager::CancelRequest(WebDataServiceBase::Handle h) {
base::AutoLock l(pending_lock_);
RequestMap::iterator i = pending_requests_.find(h);
if (i == pending_requests_.end()) {
NOTREACHED() << "Canceling a nonexistent web data service request";
return;
}
i->second->Cancel();
pending_requests_.erase(i);
}
void WebDataRequestManager::RequestCompleted(
scoped_ptr<WebDataRequest> request) {
base::MessageLoop* loop = request->GetMessageLoop();
loop->PostTask(FROM_HERE,
base::Bind(&WebDataRequestManager::RequestCompletedOnThread,
this,
base::Passed(&request)));
}
void WebDataRequestManager::RequestCompletedOnThread(
scoped_ptr<WebDataRequest> request) {
if (request->IsCancelled())
return;
{
base::AutoLock l(pending_lock_);
RequestMap::iterator i = pending_requests_.find(request->GetHandle());
if (i == pending_requests_.end()) {
NOTREACHED() << "Request completed called for an unknown request";
return;
}
// Take ownership of the request object and remove it from the map.
pending_requests_.erase(i);
}
// Notify the consumer if needed.
if (!request->IsCancelled()) {
WebDataServiceConsumer* consumer = request->GetConsumer();
request->OnComplete();
if (consumer) {
scoped_ptr<WDTypedResult> r = request->GetResult();
consumer->OnWebDataServiceRequestDone(request->GetHandle(), r.get());
}
}
}
|