[Chromecast] Add metric reporting to ConnectivityCheckerImpl

BUG=internal b/28294837
TEST=Manual testing

Review-Url: https://codereview.chromium.org/1974723002
Cr-Commit-Position: refs/heads/master@{#393640}
diff --git a/chromecast/net/connectivity_checker_impl.cc b/chromecast/net/connectivity_checker_impl.cc
index 392ca8a..8658714 100644
--- a/chromecast/net/connectivity_checker_impl.cc
+++ b/chromecast/net/connectivity_checker_impl.cc
@@ -8,6 +8,8 @@
 #include "base/logging.h"
 #include "base/memory/ptr_util.h"
 #include "base/message_loop/message_loop.h"
+#include "base/values.h"
+#include "chromecast/base/metrics/cast_metrics_helper.h"
 #include "chromecast/net/net_switches.h"
 #include "net/base/request_priority.h"
 #include "net/http/http_response_headers.h"
@@ -42,6 +44,9 @@
 // downtime is less than 3 seconds.
 const char kNetworkChangedDelayInSeconds = 3;
 
+const char kMetricNameNetworkConnectivityCheckingErrorType[] =
+    "Network.ConnectivityChecking.ErrorType";
+
 }  // namespace
 
 ConnectivityCheckerImpl::ConnectivityCheckerImpl(
@@ -174,7 +179,7 @@
     return;
   }
   VLOG(1) << "Connectivity check failed: " << http_response_code;
-  OnUrlRequestError();
+  OnUrlRequestError(ErrorType::BAD_HTTP_STATUS);
   timeout_.Cancel();
 }
 
@@ -189,13 +194,19 @@
     bool fatal) {
   LOG(ERROR) << "OnSSLCertificateError: cert_status=" << ssl_info.cert_status;
   net::SSLClientSocket::ClearSessionCache();
-  OnUrlRequestError();
+  OnUrlRequestError(ErrorType::SSL_CERTIFICATE_ERROR);
   timeout_.Cancel();
 }
 
-void ConnectivityCheckerImpl::OnUrlRequestError() {
+void ConnectivityCheckerImpl::OnUrlRequestError(ErrorType type) {
   ++check_errors_;
   if (check_errors_ > kNumErrorsToNotifyOffline) {
+    // Only record event on the connectivity transition.
+    if (connected_) {
+      metrics::CastMetricsHelper::GetInstance()->RecordEventWithValue(
+          kMetricNameNetworkConnectivityCheckingErrorType,
+          static_cast<int>(type));
+    }
     check_errors_ = kNumErrorsToNotifyOffline;
     SetConnected(false);
   }
@@ -208,7 +219,7 @@
 
 void ConnectivityCheckerImpl::OnUrlRequestTimeout() {
   LOG(ERROR) << "time out";
-  OnUrlRequestError();
+  OnUrlRequestError(ErrorType::REQUEST_TIMEOUT);
 }
 
 void ConnectivityCheckerImpl::Cancel() {