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 CancelWaitForDataReadyCallback() override;
38 bool IsDataReady() override;
39 bool IsReadyToPersist() override;
40 void Persist() override;
41 void OnExternalCacheHit() override;
44 struct CacheOperationDataShim;
53 WAIT_FOR_DATA_READY_DONE,
55 CREATE_OR_OPEN_COMPLETE,
62 // Enum to track number of times data read/parse/write API calls of
63 // QuicServerInfo to and from disk cache is called.
64 enum QuicServerInfoAPICall {
65 QUIC_SERVER_INFO_START = 0,
66 QUIC_SERVER_INFO_WAIT_FOR_DATA_READY = 1,
67 QUIC_SERVER_INFO_PARSE = 2,
68 QUIC_SERVER_INFO_WAIT_FOR_DATA_READY_CANCEL = 3,
69 QUIC_SERVER_INFO_READY_TO_PERSIST = 4,
70 QUIC_SERVER_INFO_PERSIST = 5,
71 QUIC_SERVER_INFO_EXTERNAL_CACHE_HIT = 6,
72 QUIC_SERVER_INFO_NUM_OF_API_CALLS = 7,
75 // Enum to track failure reasons to read/load/write of QuicServerInfo to
76 // and from disk cache.
78 WAIT_FOR_DATA_READY_INVALID_ARGUMENT_FAILURE = 0,
79 GET_BACKEND_FAILURE = 1,
81 CREATE_OR_OPEN_FAILURE = 3,
82 PARSE_NO_DATA_FAILURE = 4,
85 READY_TO_PERSIST_FAILURE = 7,
86 PERSIST_NO_BACKEND_FAILURE = 8,
92 ~DiskCacheBasedQuicServerInfo() override;
94 // Persists |pending_write_data_| if it is not empty, otherwise serializes the
95 // data and pesists it.
96 void PersistInternal();
98 std::string key() const;
100 // The |unused| parameter is a small hack so that we can have the
101 // CacheOperationDataShim object owned by the Callback that is created for
102 // this method. See comment above CacheOperationDataShim for details.
103 void OnIOComplete(CacheOperationDataShim* unused, int rv);
107 int DoGetBackendComplete(int rv);
108 int DoOpenComplete(int rv);
109 int DoReadComplete(int rv);
110 int DoWriteComplete(int rv);
111 int DoCreateOrOpenComplete(int rv);
117 int DoCreateOrOpen();
119 // DoWaitForDataReadyDone is the terminal state of the read operation.
120 int DoWaitForDataReadyDone();
122 // DoSetDone is the terminal state of the write operation.
125 // Tracks in a histogram the number of times data read/parse/write API calls
126 // of QuicServerInfo to and from disk cache is called.
127 void RecordQuicServerInfoStatus(QuicServerInfoAPICall call);
129 // Tracks in a histogram the failure reasons to read/load/write of
130 // QuicServerInfo to and from disk cache. It also saves the |failure| in
132 void RecordQuicServerInfoFailure(FailureReason failure);
134 // Tracks in a histogram if |last_failure_| is not NO_FAILURE.
135 void RecordLastFailure();
137 CacheOperationDataShim* data_shim_; // Owned by |io_callback_|.
138 CompletionCallback io_callback_;
141 bool found_entry_; // Controls the behavior of DoCreateOrOpen.
142 std::string new_data_;
143 std::string pending_write_data_;
144 const QuicServerId server_id_;
145 HttpCache* const http_cache_;
146 disk_cache::Backend* backend_;
147 disk_cache::Entry* entry_;
148 CompletionCallback wait_for_ready_callback_;
149 scoped_refptr<IOBuffer> read_buffer_;
150 scoped_refptr<IOBuffer> write_buffer_;
152 base::TimeTicks load_start_time_;
153 FailureReason last_failure_;
155 base::WeakPtrFactory<DiskCacheBasedQuicServerInfo> weak_factory_;
157 DISALLOW_COPY_AND_ASSIGN(DiskCacheBasedQuicServerInfo);
162 #endif // NET_HTTP_DISK_CACHE_BASED_QUIC_SERVER_INFO_H_