Add update reboot metric to the update engine.

This change add the Installer.UpdateNumReboots metric.

This records the number of reboots that occurred while an update was being
attempted. It uses a marker file stored in tmp to discover whether or not
it's already recorded the reboot.

BUG=chromium:226766
TEST=Unittests | ran an update on a test machine and rebooted/resumed and
checked about:histograms to confirm numbers. Also restart update-engine to
verify it didn't double count that.

Change-Id: I5d2af9d5b62a9d974c7c6243a89cb3359051b650
Reviewed-on: https://gerrit.chromium.org/gerrit/47710
Tested-by: Chris Sosa <sosa@chromium.org>
Commit-Queue: Chris Sosa <sosa@chromium.org>
Reviewed-by: Chris Sosa <sosa@chromium.org>
diff --git a/payload_state.h b/payload_state.h
index 5427c74..43fe5cc 100644
--- a/payload_state.h
+++ b/payload_state.h
@@ -35,6 +35,7 @@
   virtual void SetResponse(const OmahaResponse& response);
   virtual void DownloadComplete();
   virtual void DownloadProgress(size_t count);
+  virtual void UpdateResumed();
   virtual void UpdateRestarted();
   virtual void UpdateSucceeded();
   virtual void UpdateFailed(ActionExitCode error);
@@ -76,6 +77,10 @@
     return source < kNumDownloadSources ? total_bytes_downloaded_[source] : 0;
   }
 
+  virtual inline uint32_t GetNumReboots() {
+    return num_reboots_;
+  }
+
  private:
   // Increments the payload attempt number which governs the backoff behavior
   // at the time of the next update check.
@@ -112,6 +117,9 @@
   // Reports the metric related to number of URL switches.
   void ReportUpdateUrlSwitchesMetric();
 
+  // Reports the various metrics related to rebooting during an update.
+  void ReportRebootMetrics();
+
   // Resets all the persisted state values which are maintained relative to the
   // current response signature. The response signature itself is not reset.
   void ResetPersistedState();
@@ -235,6 +243,18 @@
   // The global state of the system.
   SystemState* system_state_;
 
+  // Initializes |num_reboots_| from the persisted state.
+  void LoadNumReboots();
+
+  // Sets |num_reboots| for the update attempt. Also persists the
+  // value being set so that we resume from the same value in case of a process
+  // restart.
+  void SetNumReboots(uint32_t num_reboots);
+
+  // Checks to see if the device rebooted since the last call and if so
+  // increments num_reboots.
+  void UpdateNumReboots();
+
   // Interface object with which we read/write persisted state. This must
   // be set by calling the Initialize method before calling any other method.
   PrefsInterface* prefs_;
@@ -276,6 +296,11 @@
   // data we read from the socket.
   DownloadSource current_download_source_;
 
+  // The number of system reboots during an update attempt. Technically since
+  // we don't go out of our way to not update it when not attempting an update,
+  // also records the number of reboots before the next update attempt starts.
+  uint32_t num_reboots_;
+
   // The timestamp until which we've to wait before attempting to download the
   // payload again, so as to backoff repeated downloads.
   base::Time backoff_expiry_time_;