1 // Copyright 2014 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_quic_server_info.h"
8 #include "base/callback.h"
9 #include "base/logging.h"
10 #include "net/base/completion_callback.h"
11 #include "net/base/io_buffer.h"
12 #include "net/base/net_errors.h"
13 #include "net/http/http_cache.h"
14 #include "net/http/http_network_session.h"
15 #include "net/quic/quic_server_id.h"
19 // Some APIs inside disk_cache take a handle that the caller must keep alive
20 // until the API has finished its asynchronous execution.
22 // Unfortunately, DiskCacheBasedQuicServerInfo may be deleted before the
23 // operation completes causing a use-after-free.
25 // This data shim struct is meant to provide a location for the disk_cache
26 // APIs to write into even if the originating DiskCacheBasedQuicServerInfo
27 // object has been deleted. The lifetime for instances of this struct
28 // should be bound to the CompletionCallback that is passed to the disk_cache
29 // API. We do this by binding an instance of this struct to an unused
30 // parameter for OnIOComplete() using base::Owned().
32 // This is a hack. A better fix is to make it so that the disk_cache APIs
33 // take a Callback to a mutator for setting the output value rather than
34 // writing into a raw handle. Then the caller can just pass in a Callback
35 // bound to WeakPtr for itself. This callback would correctly "no-op" itself
36 // when the DiskCacheBasedQuicServerInfo object is deleted.
38 // TODO(ajwong): Change disk_cache's API to return results via Callback.
39 struct DiskCacheBasedQuicServerInfo::CacheOperationDataShim
{
40 CacheOperationDataShim() : backend(NULL
), entry(NULL
) {}
42 disk_cache::Backend
* backend
;
43 disk_cache::Entry
* entry
;
46 DiskCacheBasedQuicServerInfo::DiskCacheBasedQuicServerInfo(
47 const QuicServerId
& server_id
,
48 HttpCache
* http_cache
)
49 : QuicServerInfo(server_id
),
51 data_shim_(new CacheOperationDataShim()),
53 base::Bind(&DiskCacheBasedQuicServerInfo::OnIOComplete
,
54 weak_factory_
.GetWeakPtr(),
55 base::Owned(data_shim_
))), // Ownership assigned.
59 server_id_(server_id
),
60 http_cache_(http_cache
),
65 void DiskCacheBasedQuicServerInfo::Start() {
66 DCHECK(CalledOnValidThread());
67 DCHECK_EQ(GET_BACKEND
, state_
);
71 int DiskCacheBasedQuicServerInfo::WaitForDataReady(
72 const CompletionCallback
& callback
) {
73 DCHECK(CalledOnValidThread());
74 DCHECK_NE(GET_BACKEND
, state_
);
79 if (!callback
.is_null()) {
80 // Prevent a new callback for WaitForDataReady overwriting an existing
81 // pending callback (|user_callback_|).
82 if (!user_callback_
.is_null())
83 return ERR_INVALID_ARGUMENT
;
84 user_callback_
= callback
;
87 return ERR_IO_PENDING
;
90 bool DiskCacheBasedQuicServerInfo::IsDataReady() {
94 bool DiskCacheBasedQuicServerInfo::IsReadyToPersist() {
95 // The data can be persisted if it has been loaded from the disk cache
96 // and there are no pending writes.
97 return ready_
&& new_data_
.empty();
100 void DiskCacheBasedQuicServerInfo::Persist() {
101 DCHECK(CalledOnValidThread());
102 DCHECK_NE(GET_BACKEND
, state_
);
104 DCHECK(new_data_
.empty());
106 DCHECK(user_callback_
.is_null());
107 new_data_
= Serialize();
112 state_
= CREATE_OR_OPEN
;
116 DiskCacheBasedQuicServerInfo::~DiskCacheBasedQuicServerInfo() {
117 DCHECK(user_callback_
.is_null());
122 std::string
DiskCacheBasedQuicServerInfo::key() const {
123 return "quicserverinfo:" + server_id_
.ToString();
126 void DiskCacheBasedQuicServerInfo::OnIOComplete(CacheOperationDataShim
* unused
,
128 DCHECK_NE(NONE
, state_
);
130 if (rv
!= ERR_IO_PENDING
&& !user_callback_
.is_null()) {
131 CompletionCallback callback
= user_callback_
;
132 user_callback_
.Reset();
137 int DiskCacheBasedQuicServerInfo::DoLoop(int rv
) {
143 case GET_BACKEND_COMPLETE
:
144 rv
= DoGetBackendComplete(rv
);
150 rv
= DoOpenComplete(rv
);
156 rv
= DoReadComplete(rv
);
158 case WAIT_FOR_DATA_READY_DONE
:
159 rv
= DoWaitForDataReadyDone();
162 rv
= DoCreateOrOpen();
164 case CREATE_OR_OPEN_COMPLETE
:
165 rv
= DoCreateOrOpenComplete(rv
);
171 rv
= DoWriteComplete(rv
);
180 } while (rv
!= ERR_IO_PENDING
&& state_
!= NONE
);
185 int DiskCacheBasedQuicServerInfo::DoGetBackendComplete(int rv
) {
187 backend_
= data_shim_
->backend
;
190 state_
= WAIT_FOR_DATA_READY_DONE
;
195 int DiskCacheBasedQuicServerInfo::DoOpenComplete(int rv
) {
197 entry_
= data_shim_
->entry
;
201 state_
= WAIT_FOR_DATA_READY_DONE
;
207 int DiskCacheBasedQuicServerInfo::DoReadComplete(int rv
) {
209 data_
.assign(read_buffer_
->data(), rv
);
211 state_
= WAIT_FOR_DATA_READY_DONE
;
215 int DiskCacheBasedQuicServerInfo::DoWriteComplete(int rv
) {
220 int DiskCacheBasedQuicServerInfo::DoCreateOrOpenComplete(int rv
) {
224 entry_
= data_shim_
->entry
;
230 int DiskCacheBasedQuicServerInfo::DoGetBackend() {
231 state_
= GET_BACKEND_COMPLETE
;
232 return http_cache_
->GetBackend(&data_shim_
->backend
, io_callback_
);
235 int DiskCacheBasedQuicServerInfo::DoOpen() {
236 state_
= OPEN_COMPLETE
;
237 return backend_
->OpenEntry(key(), &data_shim_
->entry
, io_callback_
);
240 int DiskCacheBasedQuicServerInfo::DoRead() {
241 const int32 size
= entry_
->GetDataSize(0 /* index */);
243 state_
= WAIT_FOR_DATA_READY_DONE
;
247 read_buffer_
= new IOBuffer(size
);
248 state_
= READ_COMPLETE
;
249 return entry_
->ReadData(
250 0 /* index */, 0 /* offset */, read_buffer_
, size
, io_callback_
);
253 int DiskCacheBasedQuicServerInfo::DoWrite() {
254 write_buffer_
= new IOBuffer(new_data_
.size());
255 memcpy(write_buffer_
->data(), new_data_
.data(), new_data_
.size());
256 state_
= WRITE_COMPLETE
;
258 return entry_
->WriteData(
259 0 /* index */, 0 /* offset */, write_buffer_
, new_data_
.size(),
260 io_callback_
, true /* truncate */);
263 int DiskCacheBasedQuicServerInfo::DoCreateOrOpen() {
264 DCHECK(entry_
== NULL
);
265 state_
= CREATE_OR_OPEN_COMPLETE
;
267 return backend_
->OpenEntry(key(), &data_shim_
->entry
, io_callback_
);
270 return backend_
->CreateEntry(key(), &data_shim_
->entry
, io_callback_
);
273 int DiskCacheBasedQuicServerInfo::DoWaitForDataReadyDone() {
277 // We close the entry because, if we shutdown before ::Persist is called,
278 // then we might leak a cache reference, which causes a DCHECK on shutdown.
286 int DiskCacheBasedQuicServerInfo::DoSetDone() {