Low-latency AudioInputStream implementation based on WASAPI for Windows.
[chromium-blink-merge.git] / net / http / disk_cache_based_ssl_host_info.cc
blob0b6d3ae27aa9c38bc1f8f3f4655e3d965b1a3e90
1 // Copyright (c) 2011 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 "net/http/disk_cache_based_ssl_host_info.h"
7 #include "base/callback.h"
8 #include "base/logging.h"
9 #include "net/base/io_buffer.h"
10 #include "net/base/net_errors.h"
11 #include "net/http/http_cache.h"
12 #include "net/http/http_network_session.h"
14 namespace net {
16 DiskCacheBasedSSLHostInfo::CallbackImpl::CallbackImpl(
17 const base::WeakPtr<DiskCacheBasedSSLHostInfo>& obj,
18 void (DiskCacheBasedSSLHostInfo::*meth)(int))
19 : obj_(obj),
20 meth_(meth),
21 backend_(NULL),
22 entry_(NULL) {
25 DiskCacheBasedSSLHostInfo::CallbackImpl::~CallbackImpl() {}
27 void DiskCacheBasedSSLHostInfo::CallbackImpl::RunWithParams(
28 const Tuple1<int>& params) {
29 if (!obj_) {
30 delete this;
31 } else {
32 DispatchToMethod(obj_.get(), meth_, params);
36 DiskCacheBasedSSLHostInfo::DiskCacheBasedSSLHostInfo(
37 const std::string& hostname,
38 const SSLConfig& ssl_config,
39 CertVerifier* cert_verifier,
40 HttpCache* http_cache)
41 : SSLHostInfo(hostname, ssl_config, cert_verifier),
42 weak_ptr_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)),
43 callback_(new CallbackImpl(weak_ptr_factory_.GetWeakPtr(),
44 &DiskCacheBasedSSLHostInfo::OnIOComplete)),
45 state_(GET_BACKEND),
46 ready_(false),
47 hostname_(hostname),
48 http_cache_(http_cache),
49 backend_(NULL),
50 entry_(NULL),
51 user_callback_(NULL) {
54 void DiskCacheBasedSSLHostInfo::Start() {
55 DCHECK(CalledOnValidThread());
56 DCHECK_EQ(GET_BACKEND, state_);
57 DoLoop(OK);
60 int DiskCacheBasedSSLHostInfo::WaitForDataReady(OldCompletionCallback* callback) {
61 DCHECK(CalledOnValidThread());
62 DCHECK(state_ != GET_BACKEND);
64 if (ready_)
65 return OK;
66 if (callback) {
67 DCHECK(!user_callback_);
68 user_callback_ = callback;
70 return ERR_IO_PENDING;
73 void DiskCacheBasedSSLHostInfo::Persist() {
74 DCHECK(CalledOnValidThread());
75 DCHECK(state_ != GET_BACKEND);
77 DCHECK(new_data_.empty());
78 CHECK(ready_);
79 DCHECK(user_callback_ == NULL);
80 new_data_ = Serialize();
82 if (!backend_)
83 return;
85 state_ = CREATE;
86 DoLoop(OK);
89 DiskCacheBasedSSLHostInfo::~DiskCacheBasedSSLHostInfo() {
90 DCHECK(!user_callback_);
91 if (entry_)
92 entry_->Close();
93 if (!IsCallbackPending())
94 delete callback_;
97 std::string DiskCacheBasedSSLHostInfo::key() const {
98 return "sslhostinfo:" + hostname_;
101 void DiskCacheBasedSSLHostInfo::OnIOComplete(int rv) {
102 rv = DoLoop(rv);
103 if (rv != ERR_IO_PENDING) {
104 OldCompletionCallback* callback = user_callback_;
105 user_callback_ = NULL;
106 if (callback)
107 callback->Run(rv);
111 int DiskCacheBasedSSLHostInfo::DoLoop(int rv) {
112 do {
113 switch (state_) {
114 case GET_BACKEND:
115 rv = DoGetBackend();
116 break;
117 case GET_BACKEND_COMPLETE:
118 rv = DoGetBackendComplete(rv);
119 break;
120 case OPEN:
121 rv = DoOpen();
122 break;
123 case OPEN_COMPLETE:
124 rv = DoOpenComplete(rv);
125 break;
126 case READ:
127 rv = DoRead();
128 break;
129 case READ_COMPLETE:
130 rv = DoReadComplete(rv);
131 break;
132 case WAIT_FOR_DATA_READY_DONE:
133 rv = DoWaitForDataReadyDone();
134 break;
135 case CREATE:
136 rv = DoCreate();
137 break;
138 case CREATE_COMPLETE:
139 rv = DoCreateComplete(rv);
140 break;
141 case WRITE:
142 rv = DoWrite();
143 break;
144 case WRITE_COMPLETE:
145 rv = DoWriteComplete(rv);
146 break;
147 case SET_DONE:
148 rv = DoSetDone();
149 break;
150 default:
151 rv = OK;
152 NOTREACHED();
154 } while (rv != ERR_IO_PENDING && state_ != NONE);
156 return rv;
159 int DiskCacheBasedSSLHostInfo::DoGetBackendComplete(int rv) {
160 if (rv == OK) {
161 backend_ = callback_->backend();
162 state_ = OPEN;
163 } else {
164 state_ = WAIT_FOR_DATA_READY_DONE;
166 return OK;
169 int DiskCacheBasedSSLHostInfo::DoOpenComplete(int rv) {
170 if (rv == OK) {
171 entry_ = callback_->entry();
172 state_ = READ;
173 } else {
174 state_ = WAIT_FOR_DATA_READY_DONE;
177 return OK;
180 int DiskCacheBasedSSLHostInfo::DoReadComplete(int rv) {
181 if (rv > 0)
182 data_.assign(read_buffer_->data(), rv);
184 state_ = WAIT_FOR_DATA_READY_DONE;
185 return OK;
188 int DiskCacheBasedSSLHostInfo::DoWriteComplete(int rv) {
189 state_ = SET_DONE;
190 return OK;
193 int DiskCacheBasedSSLHostInfo::DoCreateComplete(int rv) {
194 if (rv != OK) {
195 state_ = SET_DONE;
196 } else {
197 entry_ = callback_->entry();
198 state_ = WRITE;
200 return OK;
203 int DiskCacheBasedSSLHostInfo::DoGetBackend() {
204 state_ = GET_BACKEND_COMPLETE;
205 return http_cache_->GetBackend(callback_->backend_pointer(), callback_);
208 int DiskCacheBasedSSLHostInfo::DoOpen() {
209 state_ = OPEN_COMPLETE;
210 return backend_->OpenEntry(key(), callback_->entry_pointer(), callback_);
213 int DiskCacheBasedSSLHostInfo::DoRead() {
214 const int32 size = entry_->GetDataSize(0 /* index */);
215 if (!size) {
216 state_ = WAIT_FOR_DATA_READY_DONE;
217 return OK;
220 read_buffer_ = new IOBuffer(size);
221 state_ = READ_COMPLETE;
222 return entry_->ReadData(0 /* index */, 0 /* offset */, read_buffer_,
223 size, callback_);
226 int DiskCacheBasedSSLHostInfo::DoWrite() {
227 write_buffer_ = new IOBuffer(new_data_.size());
228 memcpy(write_buffer_->data(), new_data_.data(), new_data_.size());
229 state_ = WRITE_COMPLETE;
231 return entry_->WriteData(0 /* index */, 0 /* offset */, write_buffer_,
232 new_data_.size(), callback_, true /* truncate */);
235 int DiskCacheBasedSSLHostInfo::DoCreate() {
236 DCHECK(entry_ == NULL);
237 state_ = CREATE_COMPLETE;
238 return backend_->CreateEntry(key(), callback_->entry_pointer(), callback_);
241 int DiskCacheBasedSSLHostInfo::DoWaitForDataReadyDone() {
242 DCHECK(!ready_);
243 state_ = NONE;
244 ready_ = true;
245 // We close the entry because, if we shutdown before ::Persist is called,
246 // then we might leak a cache reference, which causes a DCHECK on shutdown.
247 if (entry_)
248 entry_->Close();
249 entry_ = NULL;
250 Parse(data_);
251 return OK;
254 int DiskCacheBasedSSLHostInfo::DoSetDone() {
255 if (entry_)
256 entry_->Close();
257 entry_ = NULL;
258 state_ = NONE;
259 return OK;
262 bool DiskCacheBasedSSLHostInfo::IsCallbackPending() const {
263 switch (state_) {
264 case GET_BACKEND_COMPLETE:
265 case OPEN_COMPLETE:
266 case READ_COMPLETE:
267 case CREATE_COMPLETE:
268 case WRITE_COMPLETE:
269 return true;
270 default:
271 return false;
275 } // namespace net