| 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 |
| 27 // Enum to track failure reasons to read/load/write of QuicServerInfo to | 41 // Enum to track failure reasons to read/load/write of QuicServerInfo to |
| 28 // and from disk cache. | 42 // and from disk cache. |
| 29 enum FailureReason { | 43 enum FailureReason { |
| 30 WAIT_FOR_DATA_READY_INVALID_ARGUMENT_FAILURE = 0, | 44 WAIT_FOR_DATA_READY_INVALID_ARGUMENT_FAILURE = 0, |
| 31 GET_BACKEND_FAILURE = 1, | 45 GET_BACKEND_FAILURE = 1, |
| 32 OPEN_FAILURE = 2, | 46 OPEN_FAILURE = 2, |
| 33 CREATE_OR_OPEN_FAILURE = 3, | 47 CREATE_OR_OPEN_FAILURE = 3, |
| 34 PARSE_NO_DATA_FAILURE = 4, | 48 PARSE_NO_DATA_FAILURE = 4, |
| 35 PARSE_FAILURE = 5, | 49 PARSE_FAILURE = 5, |
| 36 READ_FAILURE = 6, | 50 READ_FAILURE = 6, |
| 37 READY_TO_PERSIST_FAILURE = 7, | 51 READY_TO_PERSIST_FAILURE = 7, |
| 38 PERSIST_NO_BACKEND_FAILURE = 8, | 52 PERSIST_NO_BACKEND_FAILURE = 8, |
| 39 WRITE_FAILURE = 9, | 53 WRITE_FAILURE = 9, |
| 40 NO_FAILURE = 10, | 54 NO_FAILURE = 10, |
| 41 PARSE_DATA_DECODE_FAILURE = 11, | 55 PARSE_DATA_DECODE_FAILURE = 11, |
| 42 NUM_OF_FAILURES = 12, | 56 NUM_OF_FAILURES = 12, |
| 43 }; | 57 }; |
| 44 | 58 |
| 45 explicit QuicServerInfo(const QuicServerId& server_id); | 59 explicit QuicServerInfo(const QuicServerId& server_id); |
| 46 virtual ~QuicServerInfo(); | 60 virtual ~QuicServerInfo(); |
| 47 | 61 |
| 48 // Fetches the server config from the backing store, and returns true | 62 // Start will commence the lookup. This must be called before any other |
| 49 // if the server config was found. | 63 // methods. By opportunistically calling this early, it may be possible to |
| 50 virtual bool Load() = 0; | 64 // overlap this object's lookup and reduce latency. |
| 65 virtual void Start() = 0; |
| 51 | 66 |
| 52 // Persist allows for the server information to be updated for future uses. | 67 // WaitForDataReady returns OK if the fetch of the requested data has |
| 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. |
| 53 virtual void Persist() = 0; | 100 virtual void Persist() = 0; |
| 54 | 101 |
| 102 // Called whenever an external cache reuses quic server config. |
| 103 virtual void OnExternalCacheHit() = 0; |
| 104 |
| 55 // Returns the size of dynamically allocated memory in bytes. | 105 // Returns the size of dynamically allocated memory in bytes. |
| 56 virtual size_t EstimateMemoryUsage() const = 0; | 106 virtual size_t EstimateMemoryUsage() const = 0; |
| 57 | 107 |
| 58 struct State { | 108 struct State { |
| 59 State(); | 109 State(); |
| 60 ~State(); | 110 ~State(); |
| 61 | 111 |
| 62 void Clear(); | 112 void Clear(); |
| 63 | 113 |
| 64 // This class matches QuicClientCryptoConfig::CachedState. | 114 // This class matches QuicClientCryptoConfig::CachedState. |
| 65 std::string server_config; // A serialized handshake message. | 115 std::string server_config; // A serialized handshake message. |
| 66 std::string source_address_token; // An opaque proof of IP ownership. | 116 std::string source_address_token; // An opaque proof of IP ownership. |
| 67 std::string cert_sct; // Signed timestamp of the leaf cert. | 117 std::string cert_sct; // Signed timestamp of the leaf cert. |
| 68 std::string chlo_hash; // Hash of the CHLO message. | 118 std::string chlo_hash; // Hash of the CHLO message. |
| 69 std::vector<std::string> certs; // A list of certificates in leaf-first | 119 std::vector<std::string> certs; // A list of certificates in leaf-first |
| 70 // order. | 120 // order. |
| 71 std::string server_config_sig; // A signature of |server_config_|. | 121 std::string server_config_sig; // A signature of |server_config_|. |
| 72 | 122 |
| 73 private: | 123 private: |
| 74 DISALLOW_COPY_AND_ASSIGN(State); | 124 DISALLOW_COPY_AND_ASSIGN(State); |
| 75 }; | 125 }; |
| 76 | 126 |
| 77 // Once the data is ready, it can be read using the following members. These | 127 // Once the data is ready, it can be read using the following members. These |
| 78 // members can then be updated before calling |Persist|. | 128 // members can then be updated before calling |Persist|. |
| 79 const State& state() const; | 129 const State& state() const; |
| 80 State* mutable_state(); | 130 State* mutable_state(); |
| 81 | 131 |
| 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 |
| 82 protected: | 140 protected: |
| 83 // Parse parses pickled data and fills out the public member fields of this | 141 // Parse parses pickled data and fills out the public member fields of this |
| 84 // object. It returns true iff the parse was successful. The public member | 142 // object. It returns true iff the parse was successful. The public member |
| 85 // fields will be set to something sane in any case. | 143 // fields will be set to something sane in any case. |
| 86 bool Parse(const std::string& data); | 144 bool Parse(const std::string& data); |
| 87 std::string Serialize(); | 145 std::string Serialize(); |
| 88 | 146 |
| 89 State state_; | 147 State state_; |
| 90 | 148 |
| 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 |
| 91 // This is the QUIC server (hostname, port, is_https, privacy_mode) tuple for | 153 // This is the QUIC server (hostname, port, is_https, privacy_mode) tuple for |
| 92 // which we restore the crypto_config. | 154 // which we restore the crypto_config. |
| 93 const QuicServerId server_id_; | 155 const QuicServerId server_id_; |
| 94 | 156 |
| 95 private: | 157 private: |
| 96 // ParseInner is a helper function for Parse. | 158 // ParseInner is a helper function for Parse. |
| 97 bool ParseInner(const std::string& data); | 159 bool ParseInner(const std::string& data); |
| 98 | 160 |
| 99 // SerializeInner is a helper function for Serialize. | 161 // SerializeInner is a helper function for Serialize. |
| 100 std::string SerializeInner() const; | 162 std::string SerializeInner() const; |
| 101 | 163 |
| 102 DISALLOW_COPY_AND_ASSIGN(QuicServerInfo); | 164 DISALLOW_COPY_AND_ASSIGN(QuicServerInfo); |
| 103 }; | 165 }; |
| 104 | 166 |
| 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 |
| 105 } // namespace net | 181 } // namespace net |
| 106 | 182 |
| 107 #endif // NET_QUIC_CHROMIUM_QUIC_SERVER_INFO_H_ | 183 #endif // NET_QUIC_CHROMIUM_QUIC_SERVER_INFO_H_ |
| OLD | NEW |