Change ErrorCode into an enum class.

This change is needed in order for us to be able to import ErrorCode
symbols from chromeos_update_engine into chromeos_update_manager.
Unfortunately, shifting from plain 'enum' into an 'enum class' means
that the compiler treats the new class as a distinct type from int,
which in turn means that plenty of seamless arithmetic/bitwise
operations we used for manipulating error code values throughout the
code needed to be retrofitted with static_cast operators.

In the future, we should consider imposing a proper abstraction on
update engine error codes that'll prevent mingling with value encoding
directly and prevent such nastiness. It'll also make things more
coherent (types, semantics) and safer.

BUG=chromium:358329
TEST=Unit tests.

Change-Id: Ie55fa566b764cdab6c4785d995fb6daee4cb32d3
Reviewed-on: https://chromium-review.googlesource.com/203209
Tested-by: Gilad Arnold <garnold@chromium.org>
Reviewed-by: Alex Deymo <deymo@chromium.org>
Commit-Queue: Gilad Arnold <garnold@chromium.org>
diff --git a/update_attempter.cc b/update_attempter.cc
index dd52d0d..46a798b 100644
--- a/update_attempter.cc
+++ b/update_attempter.cc
@@ -89,24 +89,24 @@
   }
 }
 
-// Turns a generic kErrorCodeError to a generic error code specific
-// to |action| (e.g., kErrorCodeFilesystemCopierError). If |code| is
-// not kErrorCodeError, or the action is not matched, returns |code|
+// Turns a generic ErrorCode::kError to a generic error code specific
+// to |action| (e.g., ErrorCode::kFilesystemCopierError). If |code| is
+// not ErrorCode::kError, or the action is not matched, returns |code|
 // unchanged.
 ErrorCode GetErrorCodeForAction(AbstractAction* action,
                                      ErrorCode code) {
-  if (code != kErrorCodeError)
+  if (code != ErrorCode::kError)
     return code;
 
   const string type = action->Type();
   if (type == OmahaRequestAction::StaticType())
-    return kErrorCodeOmahaRequestError;
+    return ErrorCode::kOmahaRequestError;
   if (type == OmahaResponseHandlerAction::StaticType())
-    return kErrorCodeOmahaResponseHandlerError;
+    return ErrorCode::kOmahaResponseHandlerError;
   if (type == FilesystemCopierAction::StaticType())
-    return kErrorCodeFilesystemCopierError;
+    return ErrorCode::kFilesystemCopierError;
   if (type == PostinstallRunnerAction::StaticType())
-    return kErrorCodePostinstallRunnerError;
+    return ErrorCode::kPostinstallRunnerError;
 
   return code;
 }
@@ -850,7 +850,7 @@
         "so requesting reboot from user.";
   }
 
-  if (code == kErrorCodeSuccess) {
+  if (code == ErrorCode::kSuccess) {
     WriteUpdateCompletedMarker();
     prefs_->SetInt64(kPrefsDeltaUpdateFailures, 0);
     prefs_->SetString(kPrefsPreviousVersion,
@@ -957,12 +957,12 @@
       }
     }
   }
-  if (code != kErrorCodeSuccess) {
+  if (code != ErrorCode::kSuccess) {
     // If the current state is at or past the download phase, count the failure
     // in case a switch to full update becomes necessary. Ignore network
     // transfer timeouts and failures.
     if (status_ >= UPDATE_STATUS_DOWNLOADING &&
-        code != kErrorCodeDownloadTransferError) {
+        code != ErrorCode::kDownloadTransferError) {
       MarkDeltaUpdateFailure();
     }
     // On failure, schedule an error event to be sent to Omaha.
@@ -1128,17 +1128,17 @@
   uint32_t flags = 0;
 
   if (!system_state_->hardware()->IsNormalBootMode())
-    flags |= kErrorCodeDevModeFlag;
+    flags |= static_cast<uint32_t>(ErrorCode::kDevModeFlag);
 
   if (response_handler_action_.get() &&
       response_handler_action_->install_plan().is_resume)
-    flags |= kErrorCodeResumedFlag;
+    flags |= static_cast<uint32_t>(ErrorCode::kResumedFlag);
 
   if (!system_state_->hardware()->IsOfficialBuild())
-    flags |= kErrorCodeTestImageFlag;
+    flags |= static_cast<uint32_t>(ErrorCode::kTestImageFlag);
 
   if (omaha_request_params_->update_url() != kProductionOmahaUrl)
-    flags |= kErrorCodeTestOmahaUrlFlag;
+    flags |= static_cast<uint32_t>(ErrorCode::kTestOmahaUrlFlag);
 
   return flags;
 }
@@ -1152,7 +1152,7 @@
                << params->target_channel()
                << " is different from the download channel: "
                << params->download_channel();
-    *cancel_reason = kErrorCodeUpdateCanceledByChannelChange;
+    *cancel_reason = ErrorCode::kUpdateCanceledByChannelChange;
     return true;
   }
 
@@ -1180,7 +1180,7 @@
   // failure has not occurred while sending an error event -- in which case
   // don't schedule another. This shouldn't really happen but just in case...
   if ((action->Type() == OmahaResponseHandlerAction::StaticType() &&
-       code == kErrorCodeError) ||
+       code == ErrorCode::kError) ||
       status_ == UPDATE_STATUS_REPORTING_ERROR_EVENT) {
     return;
   }
@@ -1192,9 +1192,9 @@
   // the switch cases below.
   OmahaEvent::Result event_result;
   switch (code) {
-    case kErrorCodeOmahaUpdateIgnoredPerPolicy:
-    case kErrorCodeOmahaUpdateDeferredPerPolicy:
-    case kErrorCodeOmahaUpdateDeferredForBackoff:
+    case ErrorCode::kOmahaUpdateIgnoredPerPolicy:
+    case ErrorCode::kOmahaUpdateDeferredPerPolicy:
+    case ErrorCode::kOmahaUpdateDeferredForBackoff:
       event_result = OmahaEvent::kResultUpdateDeferred;
       break;
     default:
@@ -1203,10 +1203,11 @@
   }
 
   code = GetErrorCodeForAction(action, code);
-  fake_update_success_ = code == kErrorCodePostinstallBootedFromFirmwareB;
+  fake_update_success_ = code == ErrorCode::kPostinstallBootedFromFirmwareB;
 
   // Compute the final error code with all the bit flags to be sent to Omaha.
-  code = static_cast<ErrorCode>(code | GetErrorCodeFlags());
+  code = static_cast<ErrorCode>(
+      static_cast<uint32_t>(code) | GetErrorCodeFlags());
   error_event_.reset(new OmahaEvent(OmahaEvent::kTypeUpdateComplete,
                                     event_result,
                                     code));