Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1472)

Unified Diff: components/data_reduction_proxy/browser/data_reduction_proxy_usage_stats.cc

Issue 412143009: Moved data reduction proxy initialization logic to ProfileImplIOData (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: nit Created 6 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: components/data_reduction_proxy/browser/data_reduction_proxy_usage_stats.cc
diff --git a/components/data_reduction_proxy/browser/data_reduction_proxy_usage_stats.cc b/components/data_reduction_proxy/browser/data_reduction_proxy_usage_stats.cc
index d500301b25c7243dbc140fc831bdd17a5d24a6fb..bddfef4d88aa37c442020a1eff86616964a5b811 100644
--- a/components/data_reduction_proxy/browser/data_reduction_proxy_usage_stats.cc
+++ b/components/data_reduction_proxy/browser/data_reduction_proxy_usage_stats.cc
@@ -4,6 +4,9 @@
#include "components/data_reduction_proxy/browser/data_reduction_proxy_usage_stats.h"
+#include "base/bind.h"
+#include "base/callback.h"
+#include "base/message_loop/message_loop_proxy.h"
#include "base/metrics/histogram.h"
#include "net/base/net_errors.h"
#include "net/proxy/proxy_retry_info.h"
@@ -23,15 +26,14 @@ namespace data_reduction_proxy {
DataReductionProxyUsageStats::DataReductionProxyUsageStats(
DataReductionProxyParams* params,
- MessageLoopProxy* ui_thread_proxy,
- MessageLoopProxy* io_thread_proxy)
+ MessageLoopProxy* ui_thread_proxy)
: data_reduction_proxy_params_(params),
last_bypass_type_(ProxyService::BYPASS_EVENT_TYPE_MAX),
triggering_request_(true),
ui_thread_proxy_(ui_thread_proxy),
- io_thread_proxy_(io_thread_proxy),
eligible_num_requests_through_proxy_(0),
- actual_num_requests_through_proxy_(0) {
+ actual_num_requests_through_proxy_(0),
+ unavailable_(false) {
NetworkChangeNotifier::AddNetworkChangeObserver(this);
};
@@ -41,47 +43,27 @@ DataReductionProxyUsageStats::~DataReductionProxyUsageStats() {
void DataReductionProxyUsageStats::OnUrlRequestCompleted(
const net::URLRequest* request, bool started) {
- DCHECK(io_thread_proxy_->BelongsToCurrentThread());
+ 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);
- ui_thread_proxy_->PostTask(FROM_HERE, base::Bind(
- &DataReductionProxyUsageStats::IncRequestCountsOnUiThread,
- base::Unretained(this), was_received_via_proxy));
+ IncrementRequestCounts(was_received_via_proxy);
}
}
}
-/**
- * Determines if the data reduction proxy is currently unreachable. We keep
- * track of count of requests which go through data reduction proxy as well as
- * count of requests which are eligible to go through the proxy. If and only if
- * no requests go through proxy and some requests were eligible, we conclude
- * that the proxy is unreachable.
- *
- * Returns true if the data reduction proxy is unreachable.
- */
-bool DataReductionProxyUsageStats::isDataReductionProxyUnreachable() {
- DCHECK(ui_thread_proxy_->BelongsToCurrentThread());
-
- return eligible_num_requests_through_proxy_ > 0 &&
- actual_num_requests_through_proxy_ == 0;
-}
-
void DataReductionProxyUsageStats::OnNetworkChanged(
NetworkChangeNotifier::ConnectionType type) {
DCHECK(thread_checker_.CalledOnValidThread());
- ui_thread_proxy_->PostTask(FROM_HERE, base::Bind(
- &DataReductionProxyUsageStats::ClearRequestCountsOnUiThread,
- base::Unretained(this)));
+ ClearRequestCounts();
}
-void DataReductionProxyUsageStats::IncRequestCountsOnUiThread(
+void DataReductionProxyUsageStats::IncrementRequestCounts(
bool was_received_via_proxy) {
- DCHECK(ui_thread_proxy_->BelongsToCurrentThread());
+ DCHECK(thread_checker_.CalledOnValidThread());
if (was_received_via_proxy) {
actual_num_requests_through_proxy_++;
}
@@ -91,14 +73,36 @@ void DataReductionProxyUsageStats::IncRequestCountsOnUiThread(
// gets blocked, we reset the counts occasionally.
if (eligible_num_requests_through_proxy_ > 50
&& actual_num_requests_through_proxy_ > 0) {
- ClearRequestCountsOnUiThread();
+ ClearRequestCounts();
+ } else {
+ MaybeNotifyUnavailability();
}
}
-void DataReductionProxyUsageStats::ClearRequestCountsOnUiThread() {
- DCHECK(ui_thread_proxy_->BelongsToCurrentThread());
+void DataReductionProxyUsageStats::ClearRequestCounts() {
+ DCHECK(thread_checker_.CalledOnValidThread());
eligible_num_requests_through_proxy_ = 0;
actual_num_requests_through_proxy_ = 0;
+ MaybeNotifyUnavailability();
+}
+
+void DataReductionProxyUsageStats::MaybeNotifyUnavailability() {
+ bool prev_unavailable = unavailable_;
+ unavailable_ = (eligible_num_requests_through_proxy_ > 0 &&
+ actual_num_requests_through_proxy_ == 0);
+ if (prev_unavailable != unavailable_) {
+ ui_thread_proxy_->PostTask(FROM_HERE, base::Bind(
+ &DataReductionProxyUsageStats::NotifyUnavailabilityOnUIThread,
+ base::Unretained(this),
+ unavailable_));
+ }
+}
+
+void DataReductionProxyUsageStats::NotifyUnavailabilityOnUIThread(
+ bool unavailable) {
+ DCHECK(ui_thread_proxy_->BelongsToCurrentThread());
+ if (!unavailable_callback_.is_null())
+ unavailable_callback_.Run(unavailable);
}
void DataReductionProxyUsageStats::SetBypassType(

Powered by Google App Engine
This is Rietveld 408576698