Upstream version 10.38.220.0
[platform/framework/web/crosswalk.git] / src / components / data_reduction_proxy / browser / data_reduction_proxy_usage_stats.cc
index bdbb9bf..a0af979 100644 (file)
@@ -28,6 +28,10 @@ namespace data_reduction_proxy {
 
 namespace {
 
+const int kMinFailedRequestsWhenUnavailable = 1;
+const int kMaxSuccessfulRequestsWhenUnavailable = 0;
+const int kMaxFailedRequestsBeforeReset = 3;
+
 // Records a net error code that resulted in bypassing the data reduction
 // proxy (|is_primary| is true) or the data reduction proxy fallback.
 void RecordDataReductionProxyBypassOnNetworkError(
@@ -79,9 +83,11 @@ DataReductionProxyUsageStats::DataReductionProxyUsageStats(
       last_bypass_type_(BYPASS_EVENT_TYPE_MAX),
       triggering_request_(true),
       ui_thread_proxy_(ui_thread_proxy),
-      eligible_num_requests_through_proxy_(0),
-      actual_num_requests_through_proxy_(0),
+      successful_requests_through_proxy_count_(0),
+      proxy_net_errors_count_(0),
       unavailable_(false) {
+  DCHECK(params);
+
   NetworkChangeNotifier::AddNetworkChangeObserver(this);
 };
 
@@ -93,13 +99,10 @@ void DataReductionProxyUsageStats::OnUrlRequestCompleted(
     const net::URLRequest* request, bool started) {
   DCHECK(thread_checker_.CalledOnValidThread());
 
-  if (request->status().status() == net::URLRequestStatus::SUCCESS) {
-    if (data_reduction_proxy_params_->IsDataReductionProxyEligible(request)) {
-      bool was_received_via_proxy =
-          data_reduction_proxy_params_->WasDataReductionProxyUsed(
-              request, NULL);
-      IncrementRequestCounts(was_received_via_proxy);
-    }
+  if (request->status().status() == net::URLRequestStatus::SUCCESS &&
+      data_reduction_proxy_params_->WasDataReductionProxyUsed(request, NULL)) {
+    successful_requests_through_proxy_count_++;
+    NotifyUnavailabilityIfChanged();
   }
 }
 
@@ -109,40 +112,23 @@ void DataReductionProxyUsageStats::OnNetworkChanged(
   ClearRequestCounts();
 }
 
-void DataReductionProxyUsageStats::IncrementRequestCounts(
-    bool was_received_via_proxy) {
-  DCHECK(thread_checker_.CalledOnValidThread());
-  if (was_received_via_proxy) {
-    actual_num_requests_through_proxy_++;
-  }
-  eligible_num_requests_through_proxy_++;
-
-  // To account for the case when the proxy works for a little while and then
-  // gets blocked, we reset the counts occasionally.
-  if (eligible_num_requests_through_proxy_ > 50
-      && actual_num_requests_through_proxy_ > 0) {
-    ClearRequestCounts();
-  } else {
-    MaybeNotifyUnavailability();
-  }
-}
-
 void DataReductionProxyUsageStats::ClearRequestCounts() {
   DCHECK(thread_checker_.CalledOnValidThread());
-  eligible_num_requests_through_proxy_ = 0;
-  actual_num_requests_through_proxy_ = 0;
-  MaybeNotifyUnavailability();
+  successful_requests_through_proxy_count_ = 0;
+  proxy_net_errors_count_ = 0;
 }
 
-void DataReductionProxyUsageStats::MaybeNotifyUnavailability() {
+void DataReductionProxyUsageStats::NotifyUnavailabilityIfChanged() {
   bool prev_unavailable = unavailable_;
-  unavailable_ = (eligible_num_requests_through_proxy_ > 0 &&
-      actual_num_requests_through_proxy_ == 0);
+  unavailable_ =
+      (proxy_net_errors_count_ >= kMinFailedRequestsWhenUnavailable &&
+          successful_requests_through_proxy_count_ <=
+              kMaxSuccessfulRequestsWhenUnavailable);
   if (prev_unavailable != unavailable_) {
-     ui_thread_proxy_->PostTask(FROM_HERE, base::Bind(
-          &DataReductionProxyUsageStats::NotifyUnavailabilityOnUIThread,
-          base::Unretained(this),
-          unavailable_));
+    ui_thread_proxy_->PostTask(FROM_HERE, base::Bind(
+        &DataReductionProxyUsageStats::NotifyUnavailabilityOnUIThread,
+        base::Unretained(this),
+        unavailable_));
   }
 }
 
@@ -165,11 +151,6 @@ void DataReductionProxyUsageStats::RecordBypassedBytesHistograms(
     const net::ProxyConfig& data_reduction_proxy_config) {
   int64 content_length = request.received_response_content_length();
 
-  if (data_reduction_proxy_enabled.GetValue()) {
-  LOG(WARNING) << "managed pac: " << (!data_reduction_proxy_config.Equals(
-          request.context()->proxy_service()->config()) ? "true" : "false");
-  }
-
   if (data_reduction_proxy_enabled.GetValue() &&
       !data_reduction_proxy_config.Equals(
           request.context()->proxy_service()->config())) {
@@ -203,15 +184,20 @@ void DataReductionProxyUsageStats::RecordBypassedBytesHistograms(
     return;
   }
 
-  if (triggering_request_) {
-    // We only record when audio or video triggers a bypass. We don't care
-    // about audio and video bypassed as collateral damage.
+  // Only record separate triggering request UMA for short, medium, and long
+  // bypass events.
+  if (triggering_request_ &&
+     (last_bypass_type_ ==  BYPASS_EVENT_TYPE_SHORT ||
+      last_bypass_type_ ==  BYPASS_EVENT_TYPE_MEDIUM ||
+      last_bypass_type_ ==  BYPASS_EVENT_TYPE_LONG)) {
     std::string mime_type;
     request.GetMimeType(&mime_type);
-    // MIME types are named by <media-type>/<subtype>. We check to see if the
-    // media type is audio or video.
-    if (mime_type.compare(0, 6, "audio/") == 0  ||
-        mime_type.compare(0, 6, "video/") == 0) {
+    // MIME types are named by <media-type>/<subtype>. Check to see if the
+    // media type is audio or video. Only record when triggered by short bypass,
+    // there isn't an audio or video bucket for medium or long bypasses.
+    if (last_bypass_type_ ==  BYPASS_EVENT_TYPE_SHORT &&
+       (mime_type.compare(0, 6, "audio/") == 0  ||
+        mime_type.compare(0, 6, "video/") == 0)) {
       RecordBypassedBytes(last_bypass_type_,
                           DataReductionProxyUsageStats::AUDIO_VIDEO,
                           content_length);
@@ -232,23 +218,37 @@ void DataReductionProxyUsageStats::RecordBypassedBytesHistograms(
     return;
   }
 
-  if (data_reduction_proxy_params_->
-          AreDataReductionProxiesBypassed(request, NULL)) {
-      RecordBypassedBytes(last_bypass_type_,
-                          DataReductionProxyUsageStats::NETWORK_ERROR,
-                          content_length);
+  if (data_reduction_proxy_params_->AreDataReductionProxiesBypassed(request,
+                                                                    NULL)) {
+    RecordBypassedBytes(last_bypass_type_,
+                        DataReductionProxyUsageStats::NETWORK_ERROR,
+                        content_length);
   }
 }
 
-void DataReductionProxyUsageStats::RecordBypassEventHistograms(
+void DataReductionProxyUsageStats::OnProxyFallback(
     const net::ProxyServer& bypassed_proxy,
-    int net_error) const {
+    int net_error) {
   DataReductionProxyTypeInfo data_reduction_proxy_info;
   if (bypassed_proxy.is_valid() && !bypassed_proxy.is_direct() &&
       data_reduction_proxy_params_->IsDataReductionProxy(
       bypassed_proxy.host_port_pair(), &data_reduction_proxy_info)) {
     if (data_reduction_proxy_info.is_ssl)
       return;
+
+    proxy_net_errors_count_++;
+
+    // To account for the case when the proxy is reachable for sometime, and
+    // then gets blocked, we reset counts when number of errors exceed
+    // the threshold.
+    if (proxy_net_errors_count_ >= kMaxFailedRequestsBeforeReset &&
+        successful_requests_through_proxy_count_ >
+            kMaxSuccessfulRequestsWhenUnavailable) {
+      ClearRequestCounts();
+    } else {
+      NotifyUnavailabilityIfChanged();
+    }
+
     if (!data_reduction_proxy_info.is_fallback) {
       RecordDataReductionProxyBypassInfo(
           true, false, bypassed_proxy, BYPASS_EVENT_TYPE_NETWORK_ERROR);