1 // Copyright 2015 The Chromium Authors 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef NET_BASE_BACKOFF_ENTRY_SERIALIZER_H_ 6 #define NET_BASE_BACKOFF_ENTRY_SERIALIZER_H_ 7 8 #include <memory> 9 10 #include "base/time/time.h" 11 #include "base/values.h" 12 #include "net/base/backoff_entry.h" 13 #include "net/base/net_export.h" 14 15 namespace base { 16 class TickClock; 17 } 18 19 namespace net { 20 21 enum SerializationFormatVersion { 22 kVersion1 = 1, 23 kVersion2, 24 }; 25 26 // Serialize or deserialize a BackoffEntry, so it can persist beyond the 27 // lifetime of the browser. 28 class NET_EXPORT BackoffEntrySerializer { 29 public: 30 BackoffEntrySerializer() = delete; 31 BackoffEntrySerializer(const BackoffEntrySerializer&) = delete; 32 BackoffEntrySerializer& operator=(const BackoffEntrySerializer&) = delete; 33 34 // Serializes the release time and failure count into a List that can 35 // later be passed to Deserialize to re-create the given BackoffEntry. It 36 // always serializes using the latest format version. The Policy is not 37 // serialized, instead callers must pass an identical Policy* when 38 // deserializing. `time_now` should be `base::Time::Now()`, except for tests 39 // that want to simulate time changes. The release time TimeTicks will be 40 // converted to an absolute timestamp, thus the time will continue counting 41 // down even whilst the device is powered off, and will be partially 42 // vulnerable to changes in the system clock time. 43 static base::Value::List SerializeToList(const BackoffEntry& entry, 44 base::Time time_now); 45 46 // Deserializes a `list` back to a BackoffEntry. It supports all 47 // serialization format versions. `policy` MUST be the same Policy as the 48 // serialized entry had. `clock` may be NULL. Both `policy` and `clock` (if 49 // not NULL) must enclose lifetime of the returned BackoffEntry. `time_now` 50 // should be `base::Time::Now()`, except for tests that want to simulate time 51 // changes. The absolute timestamp that was serialized will be converted back 52 // to TimeTicks as best as possible. Returns NULL if deserialization was 53 // unsuccessful. 54 static std::unique_ptr<BackoffEntry> DeserializeFromList( 55 const base::Value::List& serialized, 56 const BackoffEntry::Policy* policy, 57 const base::TickClock* clock, 58 base::Time time_now); 59 }; 60 61 } // namespace net 62 63 #endif // NET_BASE_BACKOFF_ENTRY_SERIALIZER_H_ 64