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/callback_helpers.h"
10 #include "base/logging.h"
11 #include "base/metrics/histogram.h"
12 #include "base/profiler/scoped_tracker.h"
13 #include "net/base/completion_callback.h"
14 #include "net/base/io_buffer.h"
15 #include "net/base/net_errors.h"
16 #include "net/http/http_cache.h"
17 #include "net/http/http_network_session.h"
18 #include "net/quic/quic_server_id.h"
22 // Some APIs inside disk_cache take a handle that the caller must keep alive
23 // until the API has finished its asynchronous execution.
25 // Unfortunately, DiskCacheBasedQuicServerInfo may be deleted before the
26 // operation completes causing a use-after-free.
28 // This data shim struct is meant to provide a location for the disk_cache
29 // APIs to write into even if the originating DiskCacheBasedQuicServerInfo
30 // object has been deleted. The lifetime for instances of this struct
31 // should be bound to the CompletionCallback that is passed to the disk_cache
32 // API. We do this by binding an instance of this struct to an unused
33 // parameter for OnIOComplete() using base::Owned().
35 // This is a hack. A better fix is to make it so that the disk_cache APIs
36 // take a Callback to a mutator for setting the output value rather than
37 // writing into a raw handle. Then the caller can just pass in a Callback
38 // bound to WeakPtr for itself. This callback would correctly "no-op" itself
39 // when the DiskCacheBasedQuicServerInfo object is deleted.
41 // TODO(ajwong): Change disk_cache's API to return results via Callback.
42 struct DiskCacheBasedQuicServerInfo::CacheOperationDataShim
{
43 CacheOperationDataShim() : backend(NULL
), entry(NULL
) {}
45 disk_cache::Backend
* backend
;
46 disk_cache::Entry
* entry
;
49 DiskCacheBasedQuicServerInfo::DiskCacheBasedQuicServerInfo(
50 const QuicServerId
& server_id
,
51 HttpCache
* http_cache
)
52 : QuicServerInfo(server_id
),
53 data_shim_(new CacheOperationDataShim()),
57 server_id_(server_id
),
58 http_cache_(http_cache
),
61 last_failure_(NO_FAILURE
),
64 base::Bind(&DiskCacheBasedQuicServerInfo::OnIOComplete
,
65 weak_factory_
.GetWeakPtr(),
66 base::Owned(data_shim_
)); // Ownership assigned.
69 void DiskCacheBasedQuicServerInfo::Start() {
70 DCHECK(CalledOnValidThread());
71 DCHECK_EQ(GET_BACKEND
, state_
);
72 DCHECK_EQ(last_failure_
, NO_FAILURE
);
73 RecordQuicServerInfoStatus(QUIC_SERVER_INFO_START
);
74 load_start_time_
= base::TimeTicks::Now();
78 int DiskCacheBasedQuicServerInfo::WaitForDataReady(
79 const CompletionCallback
& callback
) {
80 DCHECK(CalledOnValidThread());
81 DCHECK_NE(GET_BACKEND
, state_
);
82 wait_for_data_start_time_
= base::TimeTicks::Now();
84 RecordQuicServerInfoStatus(QUIC_SERVER_INFO_WAIT_FOR_DATA_READY
);
86 wait_for_data_end_time_
= base::TimeTicks::Now();
91 if (!callback
.is_null()) {
92 // Prevent a new callback for WaitForDataReady overwriting an existing
93 // pending callback (|wait_for_ready_callback_|).
94 if (!wait_for_ready_callback_
.is_null()) {
95 RecordQuicServerInfoFailure(WAIT_FOR_DATA_READY_INVALID_ARGUMENT_FAILURE
);
96 return ERR_INVALID_ARGUMENT
;
98 wait_for_ready_callback_
= callback
;
101 return ERR_IO_PENDING
;
104 void DiskCacheBasedQuicServerInfo::CancelWaitForDataReadyCallback() {
105 DCHECK(CalledOnValidThread());
107 RecordQuicServerInfoStatus(QUIC_SERVER_INFO_WAIT_FOR_DATA_READY_CANCEL
);
108 if (!wait_for_ready_callback_
.is_null()) {
110 wait_for_ready_callback_
.Reset();
114 bool DiskCacheBasedQuicServerInfo::IsDataReady() {
118 bool DiskCacheBasedQuicServerInfo::IsReadyToPersist() {
119 // The data can be persisted if it has been loaded from the disk cache
120 // and there are no pending writes.
121 RecordQuicServerInfoStatus(QUIC_SERVER_INFO_READY_TO_PERSIST
);
122 if (ready_
&& new_data_
.empty())
124 RecordQuicServerInfoFailure(READY_TO_PERSIST_FAILURE
);
128 void DiskCacheBasedQuicServerInfo::Persist() {
129 DCHECK(CalledOnValidThread());
130 if (!IsReadyToPersist()) {
131 // Handle updates while a write is pending or if we haven't loaded from disk
132 // cache. Save the data to be written into a temporary buffer and then
133 // persist that data when we are ready to persist.
134 pending_write_data_
= Serialize();
140 void DiskCacheBasedQuicServerInfo::PersistInternal() {
141 DCHECK(CalledOnValidThread());
142 DCHECK_NE(GET_BACKEND
, state_
);
143 DCHECK(new_data_
.empty());
145 DCHECK(wait_for_ready_callback_
.is_null());
147 if (pending_write_data_
.empty()) {
148 new_data_
= Serialize();
150 new_data_
= pending_write_data_
;
151 pending_write_data_
.clear();
154 RecordQuicServerInfoStatus(QUIC_SERVER_INFO_PERSIST
);
156 RecordQuicServerInfoFailure(PERSIST_NO_BACKEND_FAILURE
);
160 state_
= CREATE_OR_OPEN
;
164 void DiskCacheBasedQuicServerInfo::OnExternalCacheHit() {
165 DCHECK(CalledOnValidThread());
166 DCHECK_NE(GET_BACKEND
, state_
);
168 RecordQuicServerInfoStatus(QUIC_SERVER_INFO_EXTERNAL_CACHE_HIT
);
170 RecordQuicServerInfoFailure(PERSIST_NO_BACKEND_FAILURE
);
174 backend_
->OnExternalCacheHit(key());
177 DiskCacheBasedQuicServerInfo::~DiskCacheBasedQuicServerInfo() {
178 DCHECK(wait_for_ready_callback_
.is_null());
183 std::string
DiskCacheBasedQuicServerInfo::key() const {
184 return "quicserverinfo:" + server_id_
.ToString();
187 void DiskCacheBasedQuicServerInfo::OnIOComplete(CacheOperationDataShim
* unused
,
189 // TODO(vadimt): Remove ScopedTracker below once crbug.com/422516 is fixed.
190 tracked_objects::ScopedTracker
tracking_profile(
191 FROM_HERE_WITH_EXPLICIT_FUNCTION(
192 "422516 DiskCacheBasedQuicServerInfo::OnIOComplete"));
194 DCHECK_NE(NONE
, state_
);
196 if (rv
== ERR_IO_PENDING
)
198 if (!wait_for_ready_callback_
.is_null()) {
199 wait_for_data_end_time_
= base::TimeTicks::Now();
201 base::ResetAndReturn(&wait_for_ready_callback_
).Run(rv
);
203 if (ready_
&& !pending_write_data_
.empty()) {
204 DCHECK_EQ(NONE
, state_
);
209 int DiskCacheBasedQuicServerInfo::DoLoop(int rv
) {
215 case GET_BACKEND_COMPLETE
:
216 rv
= DoGetBackendComplete(rv
);
222 rv
= DoOpenComplete(rv
);
228 rv
= DoReadComplete(rv
);
230 case WAIT_FOR_DATA_READY_DONE
:
231 rv
= DoWaitForDataReadyDone();
234 rv
= DoCreateOrOpen();
236 case CREATE_OR_OPEN_COMPLETE
:
237 rv
= DoCreateOrOpenComplete(rv
);
243 rv
= DoWriteComplete(rv
);
252 } while (rv
!= ERR_IO_PENDING
&& state_
!= NONE
);
257 int DiskCacheBasedQuicServerInfo::DoGetBackendComplete(int rv
) {
259 backend_
= data_shim_
->backend
;
262 RecordQuicServerInfoFailure(GET_BACKEND_FAILURE
);
263 state_
= WAIT_FOR_DATA_READY_DONE
;
268 int DiskCacheBasedQuicServerInfo::DoOpenComplete(int rv
) {
270 entry_
= data_shim_
->entry
;
274 RecordQuicServerInfoFailure(OPEN_FAILURE
);
275 state_
= WAIT_FOR_DATA_READY_DONE
;
281 int DiskCacheBasedQuicServerInfo::DoReadComplete(int rv
) {
283 data_
.assign(read_buffer_
->data(), rv
);
285 RecordQuicServerInfoFailure(READ_FAILURE
);
287 state_
= WAIT_FOR_DATA_READY_DONE
;
291 int DiskCacheBasedQuicServerInfo::DoWriteComplete(int rv
) {
293 RecordQuicServerInfoFailure(WRITE_FAILURE
);
298 int DiskCacheBasedQuicServerInfo::DoCreateOrOpenComplete(int rv
) {
300 RecordQuicServerInfoFailure(CREATE_OR_OPEN_FAILURE
);
304 entry_
= data_shim_
->entry
;
313 int DiskCacheBasedQuicServerInfo::DoGetBackend() {
314 state_
= GET_BACKEND_COMPLETE
;
315 return http_cache_
->GetBackend(&data_shim_
->backend
, io_callback_
);
318 int DiskCacheBasedQuicServerInfo::DoOpen() {
319 state_
= OPEN_COMPLETE
;
320 return backend_
->OpenEntry(key(), &data_shim_
->entry
, io_callback_
);
323 int DiskCacheBasedQuicServerInfo::DoRead() {
324 const int32 size
= entry_
->GetDataSize(0 /* index */);
326 state_
= WAIT_FOR_DATA_READY_DONE
;
330 read_buffer_
= new IOBuffer(size
);
331 state_
= READ_COMPLETE
;
332 return entry_
->ReadData(
333 0 /* index */, 0 /* offset */, read_buffer_
.get(), size
, io_callback_
);
336 int DiskCacheBasedQuicServerInfo::DoWrite() {
337 write_buffer_
= new IOBuffer(new_data_
.size());
338 memcpy(write_buffer_
->data(), new_data_
.data(), new_data_
.size());
339 state_
= WRITE_COMPLETE
;
341 return entry_
->WriteData(0 /* index */,
346 true /* truncate */);
349 int DiskCacheBasedQuicServerInfo::DoCreateOrOpen() {
350 state_
= CREATE_OR_OPEN_COMPLETE
;
355 return backend_
->OpenEntry(key(), &data_shim_
->entry
, io_callback_
);
358 return backend_
->CreateEntry(key(), &data_shim_
->entry
, io_callback_
);
361 int DiskCacheBasedQuicServerInfo::DoWaitForDataReadyDone() {
365 // We close the entry because, if we shutdown before ::Persist is called,
366 // then we might leak a cache reference, which causes a DCHECK on shutdown.
371 RecordQuicServerInfoStatus(QUIC_SERVER_INFO_PARSE
);
374 RecordQuicServerInfoFailure(PARSE_NO_DATA_FAILURE
);
376 RecordQuicServerInfoFailure(PARSE_FAILURE
);
379 UMA_HISTOGRAM_TIMES("Net.QuicServerInfo.DiskCacheLoadTime",
380 base::TimeTicks::Now() - load_start_time_
);
384 int DiskCacheBasedQuicServerInfo::DoSetDone() {
393 void DiskCacheBasedQuicServerInfo::RecordQuicServerInfoStatus(
394 QuicServerInfoAPICall call
) {
396 UMA_HISTOGRAM_ENUMERATION("Net.QuicDiskCache.APICall.NoBackend", call
,
397 QUIC_SERVER_INFO_NUM_OF_API_CALLS
);
398 } else if (backend_
->GetCacheType() == net::MEMORY_CACHE
) {
399 UMA_HISTOGRAM_ENUMERATION("Net.QuicDiskCache.APICall.MemoryCache", call
,
400 QUIC_SERVER_INFO_NUM_OF_API_CALLS
);
402 UMA_HISTOGRAM_ENUMERATION("Net.QuicDiskCache.APICall.DiskCache", call
,
403 QUIC_SERVER_INFO_NUM_OF_API_CALLS
);
407 void DiskCacheBasedQuicServerInfo::RecordLastFailure() {
408 if (last_failure_
!= NO_FAILURE
) {
409 UMA_HISTOGRAM_ENUMERATION(
410 "Net.QuicDiskCache.FailureReason.WaitForDataReady",
411 last_failure_
, NUM_OF_FAILURES
);
413 last_failure_
= NO_FAILURE
;
416 void DiskCacheBasedQuicServerInfo::RecordQuicServerInfoFailure(
417 FailureReason failure
) {
418 last_failure_
= failure
;
421 UMA_HISTOGRAM_ENUMERATION("Net.QuicDiskCache.FailureReason.NoBackend",
422 failure
, NUM_OF_FAILURES
);
423 } else if (backend_
->GetCacheType() == net::MEMORY_CACHE
) {
424 UMA_HISTOGRAM_ENUMERATION("Net.QuicDiskCache.FailureReason.MemoryCache",
425 failure
, NUM_OF_FAILURES
);
427 UMA_HISTOGRAM_ENUMERATION("Net.QuicDiskCache.FailureReason.DiskCache",
428 failure
, NUM_OF_FAILURES
);