Don't send metrics on update_engine restart without reboot.

Certain operations are performed on update_engine start that are
intended to be performed only right after boot. For example, when
an update is applied, a few metrics are sent during the next reboot
when update_engine starts. If the update is applied and
update_engine crashes before reboot, the new update_engine will send
the metrics as if the machine was rebooted.

This patch avoids this problem checking the
SystemState::system_rebooted() method.

BUG=chromium:260990
TEST=unittests

Change-Id: Ib360b1886db38e3abd926455edfa1eb657b2d621
Reviewed-on: https://gerrit.chromium.org/gerrit/63227
Reviewed-by: Gilad Arnold <garnold@chromium.org>
Tested-by: Alex Deymo <deymo@chromium.org>
Commit-Queue: Alex Deymo <deymo@chromium.org>
diff --git a/payload_state_unittest.cc b/payload_state_unittest.cc
index 5a8722f..eb66628 100644
--- a/payload_state_unittest.cc
+++ b/payload_state_unittest.cc
@@ -1017,6 +1017,8 @@
   EXPECT_CALL(*mock_system_state.mock_metrics_lib(), SendToUMA(
       "Installer.TimeToRebootMinutes",
       7, _, _, _));
+  EXPECT_CALL(mock_system_state, system_rebooted())
+      .WillRepeatedly(Return(true));
 
   payload_state2.UpdateEngineStarted();
 
@@ -1026,6 +1028,33 @@
   EXPECT_TRUE(utils::RecursiveUnlinkDir(temp_dir));
 }
 
+TEST(PayloadStateTest, RestartAfterCrash) {
+  PayloadState payload_state;
+  MockSystemState mock_system_state;
+  NiceMock<PrefsMock>* prefs = mock_system_state.mock_prefs();
+
+  EXPECT_TRUE(payload_state.Initialize(&mock_system_state));
+
+  // No prefs should be used after a crash.
+  EXPECT_CALL(*prefs, Exists(_)).Times(0);
+  EXPECT_CALL(*prefs, SetString(_, _)).Times(0);
+  EXPECT_CALL(*prefs, SetInt64(_, _)).Times(0);
+  EXPECT_CALL(*prefs, SetBoolean(_, _)).Times(0);
+  EXPECT_CALL(*prefs, GetString(_, _)).Times(0);
+  EXPECT_CALL(*prefs, GetInt64(_, _)).Times(0);
+  EXPECT_CALL(*prefs, GetBoolean(_, _)).Times(0);
+
+  // No metrics are reported after a crash.
+  EXPECT_CALL(*mock_system_state.mock_metrics_lib(),
+              SendToUMA(_, _, _, _, _)).Times(0);
+
+  // Simulate an update_engine restart without a reboot.
+  EXPECT_CALL(mock_system_state, system_rebooted())
+      .WillRepeatedly(Return(false));
+
+  payload_state.UpdateEngineStarted();
+}
+
 TEST(PayloadStateTest, CandidateUrlsComputedCorrectly) {
   OmahaResponse response;
   MockSystemState mock_system_state;