Index: net/base/backoff_entry_serializer.cc |
diff --git a/net/base/backoff_entry_serializer.cc b/net/base/backoff_entry_serializer.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..13e91a7bd1346f54392c6a34d6dd96930def4101 |
--- /dev/null |
+++ b/net/base/backoff_entry_serializer.cc |
@@ -0,0 +1,82 @@ |
+// Copyright 2015 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "net/base/backoff_entry_serializer.h" |
+ |
+#include "base/time/tick_clock.h" |
+#include "base/values.h" |
+#include "net/base/backoff_entry.h" |
+ |
+namespace { |
+const int SERIALIZATION_VERSION_NUMBER = 1; |
mmenke
2015/05/06 17:34:48
Think this is worth a short comment
mmenke
2015/05/06 17:34:48
nit: kSerializationVersionNumber (Or maybe just k
johnme
2015/05/06 20:38:08
Done.
johnme
2015/05/06 20:38:08
Done.
|
+} // namespace |
+ |
+namespace net { |
+ |
+scoped_ptr<base::ListValue> BackoffEntrySerializer::SerializeToValue( |
+ const BackoffEntry& entry, base::Time time_now) { |
+ scoped_ptr<base::ListValue> serialized(new base::ListValue()); |
+ serialized->AppendInteger(SERIALIZATION_VERSION_NUMBER); |
+ |
+ serialized->AppendInteger(entry.failure_count()); |
+ |
+ // Can't use entry.GetTimeUntilRelease as it doesn't allow negative deltas. |
+ base::TimeDelta backoff_duration = |
+ entry.GetReleaseTime() - entry.tick_clock()->NowTicks(); |
+ // Redundantly stores both the remaining time delta and the absolute time. |
+ // The delta is used to work around some cases where wall clock time changes. |
+ serialized->AppendDouble(backoff_duration.InSecondsF()); |
+ base::Time absolute_release_time = backoff_duration + time_now; |
+ serialized->AppendDouble(absolute_release_time.ToDoubleT()); |
+ |
+ return serialized; |
+} |
+ |
+scoped_ptr<BackoffEntry> BackoffEntrySerializer::DeserializeFromValue( |
+ const base::ListValue& serialized, const BackoffEntry::Policy* policy, |
+ base::TickClock* tick_clock, base::Time time_now) { |
+ if (serialized.GetSize() != 4) |
+ return nullptr; |
+ int version_number; |
+ if (!serialized.GetInteger(0, &version_number) || |
+ version_number != SERIALIZATION_VERSION_NUMBER) { |
+ return nullptr; |
+ } |
+ |
+ int failure_count; |
+ if (!serialized.GetInteger(1, &failure_count) || failure_count < 0) |
+ return nullptr; |
+ double original_backoff_duration_double; |
+ if (!serialized.GetDouble(2, &original_backoff_duration_double)) |
+ return nullptr; |
+ double absolute_release_time_double; |
+ if (!serialized.GetDouble(3, &absolute_release_time_double) || |
+ absolute_release_time_double < 0) { |
+ return nullptr; |
+ } |
+ |
+ scoped_ptr<BackoffEntry> entry(new BackoffEntry(policy, tick_clock)); |
+ |
+ for (int n = 0; n < failure_count; n++) |
+ entry->InformOfRequest(false); |
+ |
+ base::TimeDelta original_backoff_duration = |
+ base::TimeDelta::FromSecondsD(original_backoff_duration_double); |
+ base::Time absolute_release_time = |
+ base::Time::FromDoubleT(absolute_release_time_double); |
+ base::TimeDelta backoff_duration = |
+ absolute_release_time - time_now; |
mmenke
2015/05/06 17:34:48
nit: Does this fit on one line?
johnme
2015/05/06 20:38:08
Done.
|
+ // In cases where the system wall clock is rewound, use the redundant |
+ // original_backoff_duration to ensure the backoff duration isn't longer |
+ // than it was before serializing (note that it's not possible to protect |
+ // against the clock being wound forward). |
+ if (backoff_duration > original_backoff_duration) |
+ backoff_duration = original_backoff_duration; |
+ entry->SetCustomReleaseTime( |
+ entry->BackoffDurationToReleaseTime(backoff_duration)); |
+ |
+ return entry; |
+} |
+ |
+} // namespace net |