CPULimiter: Refactor class to manage the CPU limitation.

This new class replaces the functionality embedded in UpdateAttempter
that limits the max CPU usage allowed by update_engine. This refactor
helps reusing this class outside of the brillo UpdateAttempter.

Bug: None
TEST=FEATURES=test emerge-link update_engine

Change-Id: Ib5487d314846b497a44bb78a3b94609571e0fe38
diff --git a/update_attempter.cc b/update_attempter.cc
index 4e0b997..b826121 100644
--- a/update_attempter.cc
+++ b/update_attempter.cc
@@ -133,7 +133,6 @@
   // CertificateChecker might not be initialized in unittests.
   if (cert_checker_)
     cert_checker_->SetObserver(nullptr);
-  CleanupCpuSharesManagement();
   // Release ourselves as the ActionProcessor's delegate to prevent
   // re-scheduling the updates due to the processing stopped.
   processor_->set_delegate(nullptr);
@@ -910,7 +909,7 @@
   actions_.clear();
 
   // Reset cpu shares back to normal.
-  CleanupCpuSharesManagement();
+  cpu_limiter_.StopLimiter();
 
   if (status_ == UpdateStatus::REPORTING_ERROR_EVENT) {
     LOG(INFO) << "Error event sent.";
@@ -987,7 +986,7 @@
 
 void UpdateAttempter::ProcessingStopped(const ActionProcessor* processor) {
   // Reset cpu shares back to normal.
-  CleanupCpuSharesManagement();
+  cpu_limiter_.StopLimiter();
   download_progress_ = 0.0;
   SetStatusAndNotify(UpdateStatus::IDLE);
   ScheduleUpdates();
@@ -1053,7 +1052,7 @@
     new_version_ = plan.version;
     new_payload_size_ = plan.payload_size;
     SetupDownload();
-    SetupCpuSharesManagement();
+    cpu_limiter_.StartLimiter();
     SetStatusAndNotify(UpdateStatus::UPDATE_AVAILABLE);
   } else if (type == DownloadAction::StaticType()) {
     SetStatusAndNotify(UpdateStatus::FINALIZING);
@@ -1340,41 +1339,6 @@
   return true;
 }
 
-void UpdateAttempter::SetCpuShares(utils::CpuShares shares) {
-  if (shares_ == shares) {
-    return;
-  }
-  if (utils::SetCpuShares(shares)) {
-    shares_ = shares;
-    LOG(INFO) << "CPU shares = " << shares_;
-  }
-}
-
-void UpdateAttempter::SetupCpuSharesManagement() {
-  if (manage_shares_id_ != MessageLoop::kTaskIdNull) {
-    LOG(ERROR) << "Cpu shares timeout source hasn't been destroyed.";
-    CleanupCpuSharesManagement();
-  }
-  const int kCpuSharesTimeout = 2 * 60 * 60;  // 2 hours
-  manage_shares_id_ = MessageLoop::current()->PostDelayedTask(
-      FROM_HERE,
-      Bind(&UpdateAttempter::ManageCpuSharesCallback, base::Unretained(this)),
-      TimeDelta::FromSeconds(kCpuSharesTimeout));
-  SetCpuShares(utils::kCpuSharesLow);
-}
-
-void UpdateAttempter::CleanupCpuSharesManagement() {
-  if (manage_shares_id_ != MessageLoop::kTaskIdNull) {
-    // The UpdateAttempter is instantiated by default by the FakeSystemState,
-    // even when it is not used. We check the manage_shares_id_ before calling
-    // the MessageLoop::current() since the unit test using a FakeSystemState
-    // may have not define a MessageLoop for the current thread.
-    MessageLoop::current()->CancelTask(manage_shares_id_);
-    manage_shares_id_ = MessageLoop::kTaskIdNull;
-  }
-  SetCpuShares(utils::kCpuSharesNormal);
-}
-
 void UpdateAttempter::ScheduleProcessingStart() {
   LOG(INFO) << "Scheduling an action processor start.";
   start_action_processor_ = false;
@@ -1383,11 +1347,6 @@
       Bind([this] { this->processor_->StartProcessing(); }));
 }
 
-void UpdateAttempter::ManageCpuSharesCallback() {
-  SetCpuShares(utils::kCpuSharesNormal);
-  manage_shares_id_ = MessageLoop::kTaskIdNull;
-}
-
 void UpdateAttempter::DisableDeltaUpdateIfNeeded() {
   int64_t delta_failures;
   if (omaha_request_params_->delta_okay() &&