Send UMA Stats for update engine error codes.

With the latest addition of new optional security checks for the update
manifest and operations checked in, we now want to track the number of
failures, if any, over time to help us decide when it is safe to make
the new security checks mandatory. This CL adds the UMA metric for
reporting the new (as well as the old) error codes to UMA for the first
time.

There's no change to the existing errors that are being sent to Omaha.
Due to UMA restrictions, some Omaha codes will be aggregated when being
sent to UMA.

BUG=chromium-os:34299
TEST=Unit tests pass, tested on real ZGB, all stats show up in
     chrome://histograms correctly for both dev mode and normal mode.
Change-Id: I3ce4645636311cedbb33f601e775951966c0a545
Reviewed-on: https://gerrit.chromium.org/gerrit/36408
Commit-Ready: Jay Srinivasan <jaysri@chromium.org>
Reviewed-by: Jay Srinivasan <jaysri@chromium.org>
Tested-by: Jay Srinivasan <jaysri@chromium.org>
diff --git a/utils.cc b/utils.cc
index 1e87262..1165db5 100644
--- a/utils.cc
+++ b/utils.cc
@@ -31,6 +31,7 @@
 #include <rootdev/rootdev.h>
 
 #include "update_engine/file_writer.h"
+#include "update_engine/install_plan.h"
 #include "update_engine/omaha_request_params.h"
 #include "update_engine/subprocess.h"
 
@@ -698,6 +699,29 @@
                       exp_time.second);
 }
 
+void SendErrorCodeToUMA(MetricsLibraryInterface* metrics_lib,
+                        ActionExitCode code)
+{
+  string metric = utils::IsNormalBootMode() ? "UpdateEngine.NormalErrorCodes" :
+                                              "UpdateEngine.DevModeErrorCodes";
+
+  // Ignore the higher order bits in the code by applying the mask as
+  // we want the enumerations to be in the small contiguous range
+  // with values less than kNumBucketsForUMAMetrics.
+  int actual_code = code & kActualCodeMask;
+
+  // Make additional adjustments required for UMA.
+  if (actual_code >= kActionCodeOmahaRequestHTTPResponseBase) {
+    // Since we want to keep the enums to a small value, aggregate all HTTP
+    // errors into this one bucket for UMA purposes.
+    actual_code = kActionCodeOmahaErrorInHTTPResponse;
+  }
+
+  LOG(INFO) << "Sending " << actual_code << " to UMA metric: " << metric;
+  metrics_lib->SendEnumToUMA(metric, actual_code, kNumBucketsForUMAMetrics);
+}
+
+
 }  // namespace utils
 
 }  // namespace chromeos_update_engine