AU: Simplify the automatic update check code a little.

Moves automatic update checks into the UpdateAttempter class.
This is in preparation for resolving 2394.

BUG=2394
TEST=unit tests, gmerged on device

Change-Id: I66ddf860cc7e19334187dc7f1892ead53fc936c6

Review URL: http://codereview.chromium.org/3167039
diff --git a/main.cc b/main.cc
index 251b10a..64c6aba 100644
--- a/main.cc
+++ b/main.cc
@@ -46,45 +46,6 @@
 
 namespace {
 
-const int kTimeoutOnce = 7 * 60;  // at 7 minutes
-const int kTimeoutPeriodic = 45 * 60;  // every 45 minutes
-const int kTimeoutFuzz = 10 * 60;  // +/- 5 minutes
-
-// Schedules an update check |seconds| from now, while adding some fuzz.
-void ScheduleUpdateCheck(int seconds,
-                         GSourceFunc update_function,
-                         UpdateAttempter* update_attempter) {
-  seconds = utils::FuzzInt(seconds, kTimeoutFuzz);
-  g_timeout_add_seconds(seconds, update_function, update_attempter);
-}
-
-gboolean UpdateOnce(void* arg) {
-  UpdateAttempter* update_attempter = reinterpret_cast<UpdateAttempter*>(arg);
-  update_attempter->Update("", "");
-  return FALSE;  // Don't run again.
-}
-
-gboolean UpdatePeriodically(void* arg) {
-  UpdateAttempter* update_attempter = reinterpret_cast<UpdateAttempter*>(arg);
-  update_attempter->Update("", "");
-  ScheduleUpdateCheck(kTimeoutPeriodic, &UpdatePeriodically, update_attempter);
-  return FALSE;  // Don't run again.
-}
-
-void SchedulePeriodicUpdateChecks(UpdateAttempter* update_attempter) {
-  if (!utils::IsOfficialBuild()) {
-    LOG(WARNING) << "Non-official build: periodic update checks disabled.";
-    return;
-  }
-  if (utils::IsRemovableDevice(utils::RootDevice(utils::BootDevice()))) {
-    LOG(WARNING) << "Removable device boot: periodic update checks disabled.";
-    return;
-  }
-  // Kick off periodic updating.
-  ScheduleUpdateCheck(kTimeoutOnce, &UpdateOnce, update_attempter);
-  ScheduleUpdateCheck(kTimeoutPeriodic, &UpdatePeriodically, update_attempter);
-}
-
 void SetupDbusService(UpdateEngineService* service) {
   DBusGConnection *bus;
   DBusGProxy *proxy;
@@ -167,7 +128,7 @@
   update_attempter.set_dbus_service(service);
   chromeos_update_engine::SetupDbusService(service);
 
-  chromeos_update_engine::SchedulePeriodicUpdateChecks(&update_attempter);
+  update_attempter.InitiatePeriodicUpdateChecks();
 
   // Update boot flags after 45 seconds
   g_timeout_add_seconds(45, &chromeos_update_engine::UpdateBootFlags, NULL);
diff --git a/update_attempter.cc b/update_attempter.cc
index 7873166..b0c470b 100644
--- a/update_attempter.cc
+++ b/update_attempter.cc
@@ -35,6 +35,21 @@
 
 namespace chromeos_update_engine {
 
+namespace {
+
+const int kTimeoutOnce = 7 * 60;  // at 7 minutes
+const int kTimeoutPeriodic = 45 * 60;  // every 45 minutes
+const int kTimeoutFuzz = 10 * 60;  // +/- 5 minutes
+
+gboolean CheckForUpdatePeriodically(void* arg) {
+  UpdateAttempter* update_attempter = reinterpret_cast<UpdateAttempter*>(arg);
+  update_attempter->Update("", "");
+  update_attempter->SchedulePeriodicUpdateCheck(kTimeoutPeriodic);
+  return FALSE;  // Don't run again.
+}
+
+}  // namespace {}
+
 const char* kUpdateCompletedMarker = "/tmp/update_engine_autoupdate_completed";
 
 const char* UpdateStatusToString(UpdateStatus status) {
@@ -411,6 +426,27 @@
   return true;
 }
 
+void UpdateAttempter::InitiatePeriodicUpdateChecks() {
+  if (!utils::IsOfficialBuild()) {
+    LOG(WARNING) << "Non-official build: periodic update checks disabled.";
+    return;
+  }
+  if (utils::IsRemovableDevice(utils::RootDevice(utils::BootDevice()))) {
+    LOG(WARNING) << "Removable device boot: periodic update checks disabled.";
+    return;
+  }
+  // Kick off periodic update checks. The first check is scheduled
+  // |kTimeoutOnce| seconds from now. Subsequent checks are scheduled
+  // at |kTimeoutPeriodic|-second intervals.
+  SchedulePeriodicUpdateCheck(kTimeoutOnce);
+}
+
+void UpdateAttempter::SchedulePeriodicUpdateCheck(int seconds) {
+  seconds = utils::FuzzInt(seconds, kTimeoutFuzz);
+  g_timeout_add_seconds(seconds, CheckForUpdatePeriodically, this);
+  LOG(INFO) << "Next update check in " << seconds << " seconds.";
+}
+
 void UpdateAttempter::SetPriority(utils::ProcessPriority priority) {
   if (priority_ == priority) {
     return;
diff --git a/update_attempter.h b/update_attempter.h
index b2ef371..1c9ad40 100644
--- a/update_attempter.h
+++ b/update_attempter.h
@@ -88,6 +88,13 @@
   // UPDATED_NEED_REBOOT. Returns true on sucess, false otherwise.
   bool RebootIfNeeded();
 
+  // Kicks off the periodic update checks, if necessary.
+  void InitiatePeriodicUpdateChecks();
+
+  // Schedules the next periodic update check |seconds| from now. Note
+  // that the actual timeout will be fuzzed.
+  void SchedulePeriodicUpdateCheck(int seconds);
+
   // DownloadActionDelegate methods
   void SetDownloadStatus(bool active);
   void BytesReceived(uint64_t bytes_received, uint64_t total);