update_engine: add staging pref to payload_state

Add kPrefsWallClockStagingWaitPeriod as a new pref so that the staging
wait period persists accross reboots. Change the old
kPrefsWallClockWaitPeriod constant to
kPrefsWallClockScatteringWaitPeriod to differentiate more clearly
between the two.

BUG=chromium:858621
TEST=cros_workon_make update_engine --test

Change-Id: Ifc6aeb44271321cdd2f0d4cfa583a7bbc32b9004
Reviewed-on: https://chromium-review.googlesource.com/1138741
Commit-Ready: Adolfo Higueros <[email protected]>
Tested-by: Adolfo Higueros <[email protected]>
Reviewed-by: Amin Hassani <[email protected]>
Reviewed-by: May Lippert <[email protected]>
diff --git a/payload_state.cc b/payload_state.cc
index d891da0..4670b14 100644
--- a/payload_state.cc
+++ b/payload_state.cc
@@ -793,6 +793,7 @@
   SetP2PNumAttempts(0);
   SetP2PFirstAttemptTimestamp(Time());  // Set to null time
   SetScatteringWaitPeriod(TimeDelta());
+  SetStagingWaitPeriod(TimeDelta());
 }
 
 void PayloadState::ResetRollbackVersion() {
@@ -915,7 +916,7 @@
 
 void PayloadState::LoadScatteringWaitPeriod() {
   SetScatteringWaitPeriod(TimeDelta::FromSeconds(
-      GetPersistedValue(kPrefsWallClockWaitPeriod, prefs_)));
+      GetPersistedValue(kPrefsWallClockScatteringWaitPeriod, prefs_)));
 }
 
 void PayloadState::SetScatteringWaitPeriod(TimeDelta wait_period) {
@@ -924,10 +925,27 @@
   LOG(INFO) << "Scattering Wait Period (seconds) = "
             << scattering_wait_period_.InSeconds();
   if (scattering_wait_period_.InSeconds() > 0) {
-    prefs_->SetInt64(kPrefsWallClockWaitPeriod,
+    prefs_->SetInt64(kPrefsWallClockScatteringWaitPeriod,
                      scattering_wait_period_.InSeconds());
   } else {
-    prefs_->Delete(kPrefsWallClockWaitPeriod);
+    prefs_->Delete(kPrefsWallClockScatteringWaitPeriod);
+  }
+}
+
+void PayloadState::LoadStagingWaitPeriod() {
+  SetStagingWaitPeriod(TimeDelta::FromSeconds(
+      GetPersistedValue(kPrefsWallClockStagingWaitPeriod, prefs_)));
+}
+
+void PayloadState::SetStagingWaitPeriod(TimeDelta wait_period) {
+  CHECK(prefs_);
+  staging_wait_period_ = wait_period;
+  LOG(INFO) << "Staging Wait Period (days) =" << staging_wait_period_.InDays();
+  if (staging_wait_period_.InSeconds() > 0) {
+    prefs_->SetInt64(kPrefsWallClockStagingWaitPeriod,
+                     staging_wait_period_.InSeconds());
+  } else {
+    prefs_->Delete(kPrefsWallClockStagingWaitPeriod);
   }
 }