backoff_entry_serializer.h 2.8 KB

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