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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
|
// Copyright (c) 2010 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 "net/http/disk_cache_based_ssl_host_info.h"
#include "base/callback.h"
#include "base/logging.h"
#include "net/base/io_buffer.h"
#include "net/base/net_errors.h"
#include "net/http/http_cache.h"
namespace net {
DiskCacheBasedSSLHostInfo::DiskCacheBasedSSLHostInfo(
const std::string& hostname, HttpCache* http_cache)
: callback_(new CancelableCompletionCallback<DiskCacheBasedSSLHostInfo>(
ALLOW_THIS_IN_INITIALIZER_LIST(this),
&DiskCacheBasedSSLHostInfo::DoLoop)),
state_(GET_BACKEND),
ready_(false),
hostname_(hostname),
http_cache_(http_cache),
backend_(NULL),
entry_(NULL),
user_callback_(NULL) {
}
void DiskCacheBasedSSLHostInfo::Start() {
DCHECK(CalledOnValidThread());
DCHECK_EQ(GET_BACKEND, state_);
DoLoop(OK);
}
DiskCacheBasedSSLHostInfo::~DiskCacheBasedSSLHostInfo() {
DCHECK(!user_callback_);
if (entry_)
entry_->Close();
callback_->Cancel();
}
std::string DiskCacheBasedSSLHostInfo::key() const {
return "sslhostinfo:" + hostname_;
}
void DiskCacheBasedSSLHostInfo::DoLoop(int rv) {
do {
switch (state_) {
case GET_BACKEND:
rv = DoGetBackend();
break;
case GET_BACKEND_COMPLETE:
rv = DoGetBackendComplete(rv);
break;
case OPEN:
rv = DoOpen();
break;
case OPEN_COMPLETE:
rv = DoOpenComplete(rv);
break;
case READ:
rv = DoRead();
break;
case READ_COMPLETE:
rv = DoReadComplete(rv);
break;
case WAIT_FOR_DATA_READY_DONE:
rv = WaitForDataReadyDone();
break;
case CREATE:
rv = DoCreate();
break;
case CREATE_COMPLETE:
rv = DoCreateComplete(rv);
break;
case WRITE:
rv = DoWrite();
break;
case WRITE_COMPLETE:
rv = DoWriteComplete(rv);
break;
case SET_DONE:
rv = SetDone();
break;
default:
rv = OK;
NOTREACHED();
}
} while (rv != ERR_IO_PENDING && state_ != NONE);
}
int DiskCacheBasedSSLHostInfo::DoGetBackend() {
state_ = GET_BACKEND_COMPLETE;
return http_cache_->GetBackend(&backend_, callback_.get());
}
int DiskCacheBasedSSLHostInfo::DoGetBackendComplete(int rv) {
if (rv == OK) {
state_ = OPEN;
} else {
state_ = WAIT_FOR_DATA_READY_DONE;
}
return OK;
}
int DiskCacheBasedSSLHostInfo::DoOpen() {
state_ = OPEN_COMPLETE;
return backend_->OpenEntry(key(), &entry_, callback_.get());
}
int DiskCacheBasedSSLHostInfo::DoOpenComplete(int rv) {
if (rv == OK) {
state_ = READ;
} else {
state_ = WAIT_FOR_DATA_READY_DONE;
}
return OK;
}
int DiskCacheBasedSSLHostInfo::DoRead() {
const int32 size = entry_->GetDataSize(0 /* index */);
if (!size) {
state_ = WAIT_FOR_DATA_READY_DONE;
return OK;
}
read_buffer_ = new IOBuffer(size);
state_ = READ_COMPLETE;
return entry_->ReadData(0 /* index */, 0 /* offset */, read_buffer_,
size, callback_.get());
}
int DiskCacheBasedSSLHostInfo::DoReadComplete(int rv) {
if (rv > 0)
data_ = std::string(read_buffer_->data(), rv);
state_ = WAIT_FOR_DATA_READY_DONE;
return OK;
}
int DiskCacheBasedSSLHostInfo::WaitForDataReadyDone() {
CompletionCallback* callback;
DCHECK(!ready_);
state_ = NONE;
ready_ = true;
callback = user_callback_;
user_callback_ = NULL;
// We close the entry because, if we shutdown before ::Set is called, then we
// might leak a cache reference, which causes a DCHECK on shutdown.
if (entry_)
entry_->Close();
entry_ = NULL;
if (callback)
callback->Run(OK);
return OK;
}
int DiskCacheBasedSSLHostInfo::WaitForDataReady(CompletionCallback* callback) {
DCHECK(CalledOnValidThread());
DCHECK(state_ != GET_BACKEND);
if (ready_)
return OK;
if (callback) {
DCHECK(!user_callback_);
user_callback_ = callback;
}
return ERR_IO_PENDING;
}
void DiskCacheBasedSSLHostInfo::Set(const std::string& new_data) {
DCHECK(CalledOnValidThread());
DCHECK(state_ != GET_BACKEND);
if (new_data.empty())
return;
DCHECK(new_data_.empty());
CHECK(ready_);
DCHECK(user_callback_ == NULL);
new_data_ = new_data;
if (!backend_)
return;
state_ = CREATE;
DoLoop(OK);
}
int DiskCacheBasedSSLHostInfo::DoCreate() {
DCHECK(entry_ == NULL);
state_ = CREATE_COMPLETE;
return backend_->CreateEntry(key(), &entry_, callback_.get());
}
int DiskCacheBasedSSLHostInfo::DoCreateComplete(int rv) {
if (rv != OK) {
state_ = SET_DONE;
} else {
state_ = WRITE;
}
return OK;
}
int DiskCacheBasedSSLHostInfo::DoWrite() {
write_buffer_ = new IOBuffer(new_data_.size());
memcpy(write_buffer_->data(), new_data_.data(), new_data_.size());
state_ = WRITE_COMPLETE;
return entry_->WriteData(0 /* index */, 0 /* offset */, write_buffer_,
new_data_.size(), callback_.get(),
true /* truncate */);
}
int DiskCacheBasedSSLHostInfo::DoWriteComplete(int rv) {
state_ = SET_DONE;
return OK;
}
int DiskCacheBasedSSLHostInfo::SetDone() {
if (entry_)
entry_->Close();
entry_ = NULL;
state_ = NONE;
return OK;
}
} // namespace net
|