| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef NET_QUIC_CHROMIUM_QUIC_SERVER_INFO_H_ | 5 #ifndef NET_QUIC_CHROMIUM_QUIC_SERVER_INFO_H_ |
| 6 #define NET_QUIC_CHROMIUM_QUIC_SERVER_INFO_H_ | 6 #define NET_QUIC_CHROMIUM_QUIC_SERVER_INFO_H_ |
| 7 | 7 |
| 8 #include <memory> | 8 #include <memory> |
| 9 #include <string> | 9 #include <string> |
| 10 #include <vector> | 10 #include <vector> |
| 11 | 11 |
| 12 #include "base/macros.h" | 12 #include "base/macros.h" |
| 13 #include "base/memory/weak_ptr.h" | 13 #include "base/memory/weak_ptr.h" |
| 14 #include "base/time/time.h" | 14 #include "base/time/time.h" |
| 15 #include "net/base/completion_callback.h" | 15 #include "net/base/completion_callback.h" |
| 16 #include "net/quic/core/quic_server_id.h" | 16 #include "net/quic/core/quic_server_id.h" |
| 17 #include "net/quic/platform/api/quic_export.h" | 17 #include "net/quic/platform/api/quic_export.h" |
| 18 | 18 |
| 19 namespace net { | 19 namespace net { |
| 20 | 20 |
| 21 // QuicServerInfo is an interface for fetching information about a QUIC server. | 21 // QuicServerInfo is an interface for fetching information about a QUIC server. |
| 22 // This information may be stored on disk so does not include keys or other | 22 // This information may be stored on disk so does not include keys or other |
| 23 // sensitive information. Primarily it's intended for caching the QUIC server's | 23 // sensitive information. Primarily it's intended for caching the QUIC server's |
| 24 // crypto config. | 24 // crypto config. |
| 25 class QUIC_EXPORT_PRIVATE QuicServerInfo { | 25 class QUIC_EXPORT_PRIVATE QuicServerInfo { |
| 26 public: | 26 public: |
| 27 // Enum to track number of times data read/parse/write API calls of | |
| 28 // QuicServerInfo to and from disk cache is called. | |
| 29 enum QuicServerInfoAPICall { | |
| 30 QUIC_SERVER_INFO_START = 0, | |
| 31 QUIC_SERVER_INFO_WAIT_FOR_DATA_READY = 1, | |
| 32 QUIC_SERVER_INFO_PARSE = 2, | |
| 33 QUIC_SERVER_INFO_WAIT_FOR_DATA_READY_CANCEL = 3, | |
| 34 QUIC_SERVER_INFO_READY_TO_PERSIST = 4, | |
| 35 QUIC_SERVER_INFO_PERSIST = 5, | |
| 36 QUIC_SERVER_INFO_EXTERNAL_CACHE_HIT = 6, | |
| 37 QUIC_SERVER_INFO_RESET_WAIT_FOR_DATA_READY = 7, | |
| 38 QUIC_SERVER_INFO_NUM_OF_API_CALLS = 8, | |
| 39 }; | |
| 40 | |
| 41 // Enum to track failure reasons to read/load/write of QuicServerInfo to | 27 // Enum to track failure reasons to read/load/write of QuicServerInfo to |
| 42 // and from disk cache. | 28 // and from disk cache. |
| 43 enum FailureReason { | 29 enum FailureReason { |
| 44 WAIT_FOR_DATA_READY_INVALID_ARGUMENT_FAILURE = 0, | 30 WAIT_FOR_DATA_READY_INVALID_ARGUMENT_FAILURE = 0, |
| 45 GET_BACKEND_FAILURE = 1, | 31 GET_BACKEND_FAILURE = 1, |
| 46 OPEN_FAILURE = 2, | 32 OPEN_FAILURE = 2, |
| 47 CREATE_OR_OPEN_FAILURE = 3, | 33 CREATE_OR_OPEN_FAILURE = 3, |
| 48 PARSE_NO_DATA_FAILURE = 4, | 34 PARSE_NO_DATA_FAILURE = 4, |
| 49 PARSE_FAILURE = 5, | 35 PARSE_FAILURE = 5, |
| 50 READ_FAILURE = 6, | 36 READ_FAILURE = 6, |
| 51 READY_TO_PERSIST_FAILURE = 7, | 37 READY_TO_PERSIST_FAILURE = 7, |
| 52 PERSIST_NO_BACKEND_FAILURE = 8, | 38 PERSIST_NO_BACKEND_FAILURE = 8, |
| 53 WRITE_FAILURE = 9, | 39 WRITE_FAILURE = 9, |
| 54 NO_FAILURE = 10, | 40 NO_FAILURE = 10, |
| 55 PARSE_DATA_DECODE_FAILURE = 11, | 41 PARSE_DATA_DECODE_FAILURE = 11, |
| 56 NUM_OF_FAILURES = 12, | 42 NUM_OF_FAILURES = 12, |
| 57 }; | 43 }; |
| 58 | 44 |
| 59 explicit QuicServerInfo(const QuicServerId& server_id); | 45 explicit QuicServerInfo(const QuicServerId& server_id); |
| 60 virtual ~QuicServerInfo(); | 46 virtual ~QuicServerInfo(); |
| 61 | 47 |
| 62 // Start will commence the lookup. This must be called before any other | 48 // Fetches the server config from the backing store, and returns true |
| 63 // methods. By opportunistically calling this early, it may be possible to | 49 // if the server config was found. |
| 64 // overlap this object's lookup and reduce latency. | 50 virtual bool Load() = 0; |
| 65 virtual void Start() = 0; | |
| 66 | 51 |
| 67 // WaitForDataReady returns OK if the fetch of the requested data has | 52 // Persist allows for the server information to be updated for future uses. |
| 68 // completed. Otherwise it returns ERR_IO_PENDING and will call |callback| on | |
| 69 // the current thread when ready. | |
| 70 // | |
| 71 // Only a single callback can be outstanding at a given time and, in the | |
| 72 // event that WaitForDataReady returns OK, it's the caller's responsibility | |
| 73 // to delete |callback|. | |
| 74 // | |
| 75 // |callback| may be NULL, in which case ERR_IO_PENDING may still be returned | |
| 76 // but, obviously, a callback will never be made. | |
| 77 virtual int WaitForDataReady(const CompletionCallback& callback) = 0; | |
| 78 | |
| 79 // Reset's WaitForDataReady callback. This method shouldn't have any side | |
| 80 // effects (could be called even if HttpCache doesn't exist). | |
| 81 virtual void ResetWaitForDataReadyCallback() = 0; | |
| 82 | |
| 83 // Cancel's WaitForDataReady callback. |callback| passed in WaitForDataReady | |
| 84 // will not be called. | |
| 85 virtual void CancelWaitForDataReadyCallback() = 0; | |
| 86 | |
| 87 // Returns true if data is loaded from disk cache and ready (WaitForDataReady | |
| 88 // doesn't have a pending callback). | |
| 89 virtual bool IsDataReady() = 0; | |
| 90 | |
| 91 // Returns true if the object is ready to persist data, in other words, if | |
| 92 // data is loaded from disk cache and ready and there are no pending writes. | |
| 93 virtual bool IsReadyToPersist() = 0; | |
| 94 | |
| 95 // Persist allows for the server information to be updated for future users. | |
| 96 // This is a fire and forget operation: the caller may drop its reference | |
| 97 // from this object and the store operation will still complete. This can | |
| 98 // only be called once WaitForDataReady has returned OK or called its | |
| 99 // callback. | |
| 100 virtual void Persist() = 0; | 53 virtual void Persist() = 0; |
| 101 | 54 |
| 102 // Called whenever an external cache reuses quic server config. | |
| 103 virtual void OnExternalCacheHit() = 0; | |
| 104 | |
| 105 // Returns the size of dynamically allocated memory in bytes. | 55 // Returns the size of dynamically allocated memory in bytes. |
| 106 virtual size_t EstimateMemoryUsage() const = 0; | 56 virtual size_t EstimateMemoryUsage() const = 0; |
| 107 | 57 |
| 108 struct State { | 58 struct State { |
| 109 State(); | 59 State(); |
| 110 ~State(); | 60 ~State(); |
| 111 | 61 |
| 112 void Clear(); | 62 void Clear(); |
| 113 | 63 |
| 114 // This class matches QuicClientCryptoConfig::CachedState. | 64 // This class matches QuicClientCryptoConfig::CachedState. |
| 115 std::string server_config; // A serialized handshake message. | 65 std::string server_config; // A serialized handshake message. |
| 116 std::string source_address_token; // An opaque proof of IP ownership. | 66 std::string source_address_token; // An opaque proof of IP ownership. |
| 117 std::string cert_sct; // Signed timestamp of the leaf cert. | 67 std::string cert_sct; // Signed timestamp of the leaf cert. |
| 118 std::string chlo_hash; // Hash of the CHLO message. | 68 std::string chlo_hash; // Hash of the CHLO message. |
| 119 std::vector<std::string> certs; // A list of certificates in leaf-first | 69 std::vector<std::string> certs; // A list of certificates in leaf-first |
| 120 // order. | 70 // order. |
| 121 std::string server_config_sig; // A signature of |server_config_|. | 71 std::string server_config_sig; // A signature of |server_config_|. |
| 122 | 72 |
| 123 private: | 73 private: |
| 124 DISALLOW_COPY_AND_ASSIGN(State); | 74 DISALLOW_COPY_AND_ASSIGN(State); |
| 125 }; | 75 }; |
| 126 | 76 |
| 127 // Once the data is ready, it can be read using the following members. These | 77 // Once the data is ready, it can be read using the following members. These |
| 128 // members can then be updated before calling |Persist|. | 78 // members can then be updated before calling |Persist|. |
| 129 const State& state() const; | 79 const State& state() const; |
| 130 State* mutable_state(); | 80 State* mutable_state(); |
| 131 | 81 |
| 132 base::TimeTicks wait_for_data_start_time() const { | |
| 133 return wait_for_data_start_time_; | |
| 134 } | |
| 135 | |
| 136 base::TimeTicks wait_for_data_end_time() const { | |
| 137 return wait_for_data_end_time_; | |
| 138 } | |
| 139 | |
| 140 protected: | 82 protected: |
| 141 // Parse parses pickled data and fills out the public member fields of this | 83 // Parse parses pickled data and fills out the public member fields of this |
| 142 // object. It returns true iff the parse was successful. The public member | 84 // object. It returns true iff the parse was successful. The public member |
| 143 // fields will be set to something sane in any case. | 85 // fields will be set to something sane in any case. |
| 144 bool Parse(const std::string& data); | 86 bool Parse(const std::string& data); |
| 145 std::string Serialize(); | 87 std::string Serialize(); |
| 146 | 88 |
| 147 State state_; | 89 State state_; |
| 148 | 90 |
| 149 // Time when WaitForDataReady was called and when it has finished. | |
| 150 base::TimeTicks wait_for_data_start_time_; | |
| 151 base::TimeTicks wait_for_data_end_time_; | |
| 152 | |
| 153 // This is the QUIC server (hostname, port, is_https, privacy_mode) tuple for | 91 // This is the QUIC server (hostname, port, is_https, privacy_mode) tuple for |
| 154 // which we restore the crypto_config. | 92 // which we restore the crypto_config. |
| 155 const QuicServerId server_id_; | 93 const QuicServerId server_id_; |
| 156 | 94 |
| 157 private: | 95 private: |
| 158 // ParseInner is a helper function for Parse. | 96 // ParseInner is a helper function for Parse. |
| 159 bool ParseInner(const std::string& data); | 97 bool ParseInner(const std::string& data); |
| 160 | 98 |
| 161 // SerializeInner is a helper function for Serialize. | 99 // SerializeInner is a helper function for Serialize. |
| 162 std::string SerializeInner() const; | 100 std::string SerializeInner() const; |
| 163 | 101 |
| 164 DISALLOW_COPY_AND_ASSIGN(QuicServerInfo); | 102 DISALLOW_COPY_AND_ASSIGN(QuicServerInfo); |
| 165 }; | 103 }; |
| 166 | 104 |
| 167 class QUIC_EXPORT_PRIVATE QuicServerInfoFactory { | |
| 168 public: | |
| 169 QuicServerInfoFactory() {} | |
| 170 virtual ~QuicServerInfoFactory(); | |
| 171 | |
| 172 // GetForServer returns a fresh, allocated QuicServerInfo for the given | |
| 173 // |server_id| or NULL on failure. | |
| 174 virtual std::unique_ptr<QuicServerInfo> GetForServer( | |
| 175 const QuicServerId& server_id) = 0; | |
| 176 | |
| 177 private: | |
| 178 DISALLOW_COPY_AND_ASSIGN(QuicServerInfoFactory); | |
| 179 }; | |
| 180 | |
| 181 } // namespace net | 105 } // namespace net |
| 182 | 106 |
| 183 #endif // NET_QUIC_CHROMIUM_QUIC_SERVER_INFO_H_ | 107 #endif // NET_QUIC_CHROMIUM_QUIC_SERVER_INFO_H_ |
| OLD | NEW |