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 #ifndef NET_HTTP_DISK_CACHE_BASED_QUIC_SERVER_INFO_H_
6 #define NET_HTTP_DISK_CACHE_BASED_QUIC_SERVER_INFO_H_
10 #include "base/memory/ref_counted.h"
11 #include "base/memory/weak_ptr.h"
12 #include "base/threading/non_thread_safe.h"
13 #include "base/time/time.h"
14 #include "net/base/completion_callback.h"
15 #include "net/disk_cache/disk_cache.h"
16 #include "net/quic/crypto/quic_server_info.h"
24 // DiskCacheBasedQuicServerInfo fetches information about a QUIC server from
25 // our standard disk cache. Since the information is defined to be
26 // non-sensitive, it's ok for us to keep it on disk.
27 class NET_EXPORT_PRIVATE DiskCacheBasedQuicServerInfo
28 : public QuicServerInfo
,
29 public NON_EXPORTED_BASE(base::NonThreadSafe
) {
31 DiskCacheBasedQuicServerInfo(const QuicServerId
& server_id
,
32 HttpCache
* http_cache
);
34 // QuicServerInfo implementation.
35 void Start() override
;
36 int WaitForDataReady(const CompletionCallback
& callback
) override
;
37 void ResetWaitForDataReadyCallback() override
;
38 void CancelWaitForDataReadyCallback() override
;
39 bool IsDataReady() override
;
40 bool IsReadyToPersist() override
;
41 void Persist() override
;
42 void OnExternalCacheHit() override
;
45 struct CacheOperationDataShim
;
54 WAIT_FOR_DATA_READY_DONE
,
56 CREATE_OR_OPEN_COMPLETE
,
63 // Enum to track number of times data read/parse/write API calls of
64 // QuicServerInfo to and from disk cache is called.
65 enum QuicServerInfoAPICall
{
66 QUIC_SERVER_INFO_START
= 0,
67 QUIC_SERVER_INFO_WAIT_FOR_DATA_READY
= 1,
68 QUIC_SERVER_INFO_PARSE
= 2,
69 QUIC_SERVER_INFO_WAIT_FOR_DATA_READY_CANCEL
= 3,
70 QUIC_SERVER_INFO_READY_TO_PERSIST
= 4,
71 QUIC_SERVER_INFO_PERSIST
= 5,
72 QUIC_SERVER_INFO_EXTERNAL_CACHE_HIT
= 6,
73 QUIC_SERVER_INFO_NUM_OF_API_CALLS
= 7,
76 // Enum to track failure reasons to read/load/write of QuicServerInfo to
77 // and from disk cache.
79 WAIT_FOR_DATA_READY_INVALID_ARGUMENT_FAILURE
= 0,
80 GET_BACKEND_FAILURE
= 1,
82 CREATE_OR_OPEN_FAILURE
= 3,
83 PARSE_NO_DATA_FAILURE
= 4,
86 READY_TO_PERSIST_FAILURE
= 7,
87 PERSIST_NO_BACKEND_FAILURE
= 8,
93 ~DiskCacheBasedQuicServerInfo() override
;
95 // Persists |pending_write_data_| if it is not empty, otherwise serializes the
96 // data and pesists it.
97 void PersistInternal();
99 std::string
key() const;
101 // The |unused| parameter is a small hack so that we can have the
102 // CacheOperationDataShim object owned by the Callback that is created for
103 // this method. See comment above CacheOperationDataShim for details.
104 void OnIOComplete(CacheOperationDataShim
* unused
, int rv
);
108 int DoGetBackendComplete(int rv
);
109 int DoOpenComplete(int rv
);
110 int DoReadComplete(int rv
);
111 int DoWriteComplete(int rv
);
112 int DoCreateOrOpenComplete(int rv
);
118 int DoCreateOrOpen();
120 // DoWaitForDataReadyDone is the terminal state of the read operation.
121 int DoWaitForDataReadyDone();
123 // DoSetDone is the terminal state of the write operation.
126 // Tracks in a histogram the number of times data read/parse/write API calls
127 // of QuicServerInfo to and from disk cache is called.
128 void RecordQuicServerInfoStatus(QuicServerInfoAPICall call
);
130 // Tracks in a histogram the failure reasons to read/load/write of
131 // QuicServerInfo to and from disk cache. It also saves the |failure| in
133 void RecordQuicServerInfoFailure(FailureReason failure
);
135 // Tracks in a histogram if |last_failure_| is not NO_FAILURE.
136 void RecordLastFailure();
138 CacheOperationDataShim
* data_shim_
; // Owned by |io_callback_|.
139 CompletionCallback io_callback_
;
142 bool found_entry_
; // Controls the behavior of DoCreateOrOpen.
143 std::string new_data_
;
144 std::string pending_write_data_
;
145 const QuicServerId server_id_
;
146 HttpCache
* const http_cache_
;
147 disk_cache::Backend
* backend_
;
148 disk_cache::Entry
* entry_
;
149 CompletionCallback wait_for_ready_callback_
;
150 scoped_refptr
<IOBuffer
> read_buffer_
;
151 scoped_refptr
<IOBuffer
> write_buffer_
;
153 base::TimeTicks load_start_time_
;
154 FailureReason last_failure_
;
156 base::WeakPtrFactory
<DiskCacheBasedQuicServerInfo
> weak_factory_
;
158 DISALLOW_COPY_AND_ASSIGN(DiskCacheBasedQuicServerInfo
);
163 #endif // NET_HTTP_DISK_CACHE_BASED_QUIC_SERVER_INFO_H_