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"
16 DiskCacheBasedSSLHostInfo::CallbackImpl::CallbackImpl(
17 const base::WeakPtr
<DiskCacheBasedSSLHostInfo
>& obj
,
18 void (DiskCacheBasedSSLHostInfo::*meth
)(int))
25 DiskCacheBasedSSLHostInfo::CallbackImpl::~CallbackImpl() {}
27 void DiskCacheBasedSSLHostInfo::CallbackImpl::RunWithParams(
28 const Tuple1
<int>& params
) {
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
)),
48 http_cache_(http_cache
),
51 user_callback_(NULL
) {
54 void DiskCacheBasedSSLHostInfo::Start() {
55 DCHECK(CalledOnValidThread());
56 DCHECK_EQ(GET_BACKEND
, state_
);
60 int DiskCacheBasedSSLHostInfo::WaitForDataReady(OldCompletionCallback
* callback
) {
61 DCHECK(CalledOnValidThread());
62 DCHECK(state_
!= GET_BACKEND
);
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());
79 DCHECK(user_callback_
== NULL
);
80 new_data_
= Serialize();
89 DiskCacheBasedSSLHostInfo::~DiskCacheBasedSSLHostInfo() {
90 DCHECK(!user_callback_
);
93 if (!IsCallbackPending())
97 std::string
DiskCacheBasedSSLHostInfo::key() const {
98 return "sslhostinfo:" + hostname_
;
101 void DiskCacheBasedSSLHostInfo::OnIOComplete(int rv
) {
103 if (rv
!= ERR_IO_PENDING
) {
104 OldCompletionCallback
* callback
= user_callback_
;
105 user_callback_
= NULL
;
111 int DiskCacheBasedSSLHostInfo::DoLoop(int rv
) {
117 case GET_BACKEND_COMPLETE
:
118 rv
= DoGetBackendComplete(rv
);
124 rv
= DoOpenComplete(rv
);
130 rv
= DoReadComplete(rv
);
132 case WAIT_FOR_DATA_READY_DONE
:
133 rv
= DoWaitForDataReadyDone();
138 case CREATE_COMPLETE
:
139 rv
= DoCreateComplete(rv
);
145 rv
= DoWriteComplete(rv
);
154 } while (rv
!= ERR_IO_PENDING
&& state_
!= NONE
);
159 int DiskCacheBasedSSLHostInfo::DoGetBackendComplete(int rv
) {
161 backend_
= callback_
->backend();
164 state_
= WAIT_FOR_DATA_READY_DONE
;
169 int DiskCacheBasedSSLHostInfo::DoOpenComplete(int rv
) {
171 entry_
= callback_
->entry();
174 state_
= WAIT_FOR_DATA_READY_DONE
;
180 int DiskCacheBasedSSLHostInfo::DoReadComplete(int rv
) {
182 data_
.assign(read_buffer_
->data(), rv
);
184 state_
= WAIT_FOR_DATA_READY_DONE
;
188 int DiskCacheBasedSSLHostInfo::DoWriteComplete(int rv
) {
193 int DiskCacheBasedSSLHostInfo::DoCreateComplete(int rv
) {
197 entry_
= callback_
->entry();
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 */);
216 state_
= WAIT_FOR_DATA_READY_DONE
;
220 read_buffer_
= new IOBuffer(size
);
221 state_
= READ_COMPLETE
;
222 return entry_
->ReadData(0 /* index */, 0 /* offset */, read_buffer_
,
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() {
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.
254 int DiskCacheBasedSSLHostInfo::DoSetDone() {
262 bool DiskCacheBasedSSLHostInfo::IsCallbackPending() const {
264 case GET_BACKEND_COMPLETE
:
267 case CREATE_COMPLETE
: