summaryrefslogtreecommitdiffstats
path: root/components
diff options
context:
space:
mode:
authormegjablon <megjablon@chromium.org>2015-02-10 12:06:20 -0800
committerCommit bot <commit-bot@chromium.org>2015-02-10 20:06:58 +0000
commit85594ccf495b97ccd4642dd5f5ab98e31d1d2003 (patch)
tree79662d78d347a2eea358e259b7ec7806a2357b9b /components
parent938dade324896a4ac352c351f594814c7334b290 (diff)
downloadchromium_src-85594ccf495b97ccd4642dd5f5ab98e31d1d2003.zip
chromium_src-85594ccf495b97ccd4642dd5f5ab98e31d1d2003.tar.gz
chromium_src-85594ccf495b97ccd4642dd5f5ab98e31d1d2003.tar.bz2
DataReductionProxyStatisticsPrefs should support WeakPtr
The DRPStatisticsPrefs object is referenced via a raw pointer in DRPNetworkDelegate and DRPSettings. It should instead be referenced via WeakPtr. BUG=453155,455559,444939 Committed: https://crrev.com/149bd770d674e8125f81cd649a8deece3a851682 Cr-Commit-Position: refs/heads/master@{#315419} Review URL: https://codereview.chromium.org/888713002 Cr-Commit-Position: refs/heads/master@{#315621}
Diffstat (limited to 'components')
-rw-r--r--components/data_reduction_proxy/core/browser/data_reduction_proxy_io_data.cc36
-rw-r--r--components/data_reduction_proxy/core/browser/data_reduction_proxy_io_data.h20
-rw-r--r--components/data_reduction_proxy/core/browser/data_reduction_proxy_io_data_unittest.cc10
-rw-r--r--components/data_reduction_proxy/core/browser/data_reduction_proxy_metrics.cc28
-rw-r--r--components/data_reduction_proxy/core/browser/data_reduction_proxy_metrics.h8
-rw-r--r--components/data_reduction_proxy/core/browser/data_reduction_proxy_metrics_unittest.cc36
-rw-r--r--components/data_reduction_proxy/core/browser/data_reduction_proxy_network_delegate.cc43
-rw-r--r--components/data_reduction_proxy/core/browser/data_reduction_proxy_network_delegate.h16
-rw-r--r--components/data_reduction_proxy/core/browser/data_reduction_proxy_network_delegate_unittest.cc296
-rw-r--r--components/data_reduction_proxy/core/browser/data_reduction_proxy_settings.cc24
-rw-r--r--components/data_reduction_proxy/core/browser/data_reduction_proxy_settings.h20
-rw-r--r--components/data_reduction_proxy/core/browser/data_reduction_proxy_settings_test_utils.cc21
-rw-r--r--components/data_reduction_proxy/core/browser/data_reduction_proxy_settings_test_utils.h2
-rw-r--r--components/data_reduction_proxy/core/browser/data_reduction_proxy_settings_unittest.cc3
-rw-r--r--components/data_reduction_proxy/core/browser/data_reduction_proxy_statistics_prefs.cc15
-rw-r--r--components/data_reduction_proxy/core/browser/data_reduction_proxy_statistics_prefs.h4
16 files changed, 307 insertions, 275 deletions
diff --git a/components/data_reduction_proxy/core/browser/data_reduction_proxy_io_data.cc b/components/data_reduction_proxy/core/browser/data_reduction_proxy_io_data.cc
index 71d206e..c4f779f 100644
--- a/components/data_reduction_proxy/core/browser/data_reduction_proxy_io_data.cc
+++ b/components/data_reduction_proxy/core/browser/data_reduction_proxy_io_data.cc
@@ -15,6 +15,7 @@
#include "components/data_reduction_proxy/core/browser/data_reduction_proxy_interceptor.h"
#include "components/data_reduction_proxy/core/browser/data_reduction_proxy_network_delegate.h"
#include "components/data_reduction_proxy/core/browser/data_reduction_proxy_settings.h"
+#include "components/data_reduction_proxy/core/browser/data_reduction_proxy_statistics_prefs.h"
#include "components/data_reduction_proxy/core/browser/data_reduction_proxy_usage_stats.h"
#include "components/data_reduction_proxy/core/common/data_reduction_proxy_event_store.h"
#include "components/data_reduction_proxy/core/common/data_reduction_proxy_params.h"
@@ -31,7 +32,7 @@ DataReductionProxyIOData::DataReductionProxyIOData(
scoped_refptr<base::SingleThreadTaskRunner> io_task_runner,
scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner)
: client_(client),
- statistics_prefs_(statistics_prefs.Pass()),
+ temporary_statistics_prefs_(statistics_prefs.Pass()),
settings_(settings),
net_log_(net_log),
io_task_runner_(io_task_runner),
@@ -50,6 +51,8 @@ DataReductionProxyIOData::DataReductionProxyIOData(
proxy_delegate_.reset(
new data_reduction_proxy::DataReductionProxyDelegate(
auth_request_handler_.get(), params_.get()));
+ if (temporary_statistics_prefs_)
+ statistics_prefs_ = temporary_statistics_prefs_->GetWeakPtr();
}
DataReductionProxyIOData::~DataReductionProxyIOData() {
@@ -62,17 +65,23 @@ void DataReductionProxyIOData::InitOnUIThread(PrefService* pref_service) {
enabled_.MoveToThread(io_task_runner_);
}
-void DataReductionProxyIOData::ShutdownOnUIThread() {
+void DataReductionProxyIOData::ShutdownOnUIThread() {
DCHECK(!shutdown_on_ui_);
DCHECK(ui_task_runner_->BelongsToCurrentThread());
- if (statistics_prefs_) {
- statistics_prefs_->WritePrefs();
- statistics_prefs_->ShutdownOnUIThread();
- }
enabled_.Destroy();
shutdown_on_ui_ = true;
}
+void DataReductionProxyIOData::SetDataReductionProxyStatisticsPrefs(
+ base::WeakPtr<DataReductionProxyStatisticsPrefs> statistics_prefs) {
+ statistics_prefs_ = statistics_prefs;
+}
+
+scoped_ptr<DataReductionProxyStatisticsPrefs>
+DataReductionProxyIOData::PassStatisticsPrefs() {
+ return temporary_statistics_prefs_.Pass();
+}
+
bool DataReductionProxyIOData::IsEnabled() const {
DCHECK(io_task_runner_->BelongsToCurrentThread());
return enabled_.GetValue() ||
@@ -87,15 +96,6 @@ DataReductionProxyIOData::CreateInterceptor() {
params_.get(), usage_stats_.get(), event_store_.get()));
}
-void DataReductionProxyIOData::EnableCompressionStatisticsLogging(
- PrefService* prefs,
- const base::TimeDelta& commit_delay) {
- DCHECK(ui_task_runner_->BelongsToCurrentThread());
- statistics_prefs_.reset(
- new DataReductionProxyStatisticsPrefs(
- prefs, ui_task_runner_, commit_delay));
-}
-
scoped_ptr<DataReductionProxyNetworkDelegate>
DataReductionProxyIOData::CreateNetworkDelegate(
scoped_ptr<net::NetworkDelegate> wrapped_network_delegate,
@@ -106,12 +106,10 @@ DataReductionProxyIOData::CreateNetworkDelegate(
wrapped_network_delegate.Pass(), params_.get(),
auth_request_handler_.get(), configurator_.get()));
if (track_proxy_bypass_statistics && !usage_stats_) {
- usage_stats_.reset(
- new data_reduction_proxy::DataReductionProxyUsageStats(
+ usage_stats_.reset(new data_reduction_proxy::DataReductionProxyUsageStats(
params_.get(), settings_, ui_task_runner_));
network_delegate->InitStatisticsPrefsAndUMA(
- ui_task_runner_, statistics_prefs_.get(), &enabled_,
- usage_stats_.get());
+ ui_task_runner_, statistics_prefs_, &enabled_, usage_stats_.get());
}
return network_delegate.Pass();
}
diff --git a/components/data_reduction_proxy/core/browser/data_reduction_proxy_io_data.h b/components/data_reduction_proxy/core/browser/data_reduction_proxy_io_data.h
index 2eecb6a..ecbb0f0 100644
--- a/components/data_reduction_proxy/core/browser/data_reduction_proxy_io_data.h
+++ b/components/data_reduction_proxy/core/browser/data_reduction_proxy_io_data.h
@@ -6,6 +6,7 @@
#define COMPONENTS_DATA_REDUCTION_PROXY_CORE_BROWSER_DATA_REDUCTION_PROXY_IO_DATA_H_
#include "base/memory/scoped_ptr.h"
+#include "base/memory/weak_ptr.h"
#include "base/prefs/pref_member.h"
#include "components/data_reduction_proxy/core/browser/data_reduction_proxy_auth_request_handler.h"
#include "components/data_reduction_proxy/core/browser/data_reduction_proxy_delegate.h"
@@ -52,11 +53,11 @@ class DataReductionProxyIOData {
// Destroys the statistics preferences.
void ShutdownOnUIThread();
- // Constructs statistics prefs. This is not necessary if a valid statistics
- // prefs is passed into the constructor.
- void EnableCompressionStatisticsLogging(
- PrefService* prefs,
- const base::TimeDelta& commit_delay);
+ void SetDataReductionProxyStatisticsPrefs(
+ base::WeakPtr<DataReductionProxyStatisticsPrefs> statistics_prefs);
+
+ // Passes ownership of |statistics_prefs_|.
+ scoped_ptr<DataReductionProxyStatisticsPrefs> PassStatisticsPrefs();
// Creates an interceptor suitable for following the Data Reduction Proxy
// bypass protocol.
@@ -81,10 +82,6 @@ class DataReductionProxyIOData {
return event_store_.get();
}
- DataReductionProxyStatisticsPrefs* statistics_prefs() const {
- return statistics_prefs_.get();
- }
-
DataReductionProxyAuthRequestHandler* auth_request_handler() const {
return auth_request_handler_.get();
}
@@ -110,7 +107,10 @@ class DataReductionProxyIOData {
scoped_ptr<DataReductionProxyParams> params_;
// Tracker of compression statistics to be displayed to the user.
- scoped_ptr<DataReductionProxyStatisticsPrefs> statistics_prefs_;
+ base::WeakPtr<DataReductionProxyStatisticsPrefs> statistics_prefs_;
+ // |temporary_statistics_prefs_| is used only until DataReductionProxySettings
+ // initialization and is dead after.
+ scoped_ptr<DataReductionProxyStatisticsPrefs> temporary_statistics_prefs_;
// Tracker of Data Reduction Proxy-related events, e.g., for logging.
scoped_ptr<DataReductionProxyEventStore> event_store_;
diff --git a/components/data_reduction_proxy/core/browser/data_reduction_proxy_io_data_unittest.cc b/components/data_reduction_proxy/core/browser/data_reduction_proxy_io_data_unittest.cc
index 641f625..a0ebac6 100644
--- a/components/data_reduction_proxy/core/browser/data_reduction_proxy_io_data_unittest.cc
+++ b/components/data_reduction_proxy/core/browser/data_reduction_proxy_io_data_unittest.cc
@@ -109,6 +109,8 @@ TEST_F(DataReductionProxyIODataTest, TestConstruction) {
new DataReductionProxyIOData(
Client::UNKNOWN, make_scoped_ptr(statistics_prefs), settings(),
net_log(), message_loop_proxy(), message_loop_proxy()));
+ settings()->SetDataReductionProxyStatisticsPrefs(
+ io_data->PassStatisticsPrefs());
// Check that io_data creates an interceptor. Such an interceptor is
// thoroughly tested by DataReductionProxyInterceptoTest.
@@ -118,13 +120,7 @@ TEST_F(DataReductionProxyIODataTest, TestConstruction) {
// At this point io_data->statistics_prefs() should be set and compression
// statistics logging should be enabled.
- EXPECT_EQ(statistics_prefs, io_data->statistics_prefs());
-
- // Check if explicitly enabling compression statistics logging results in
- // a new logging object being created.
- io_data->EnableCompressionStatisticsLogging(prefs(), base::TimeDelta());
- EXPECT_NE(statistics_prefs, io_data->statistics_prefs());
- EXPECT_TRUE(io_data->statistics_prefs());
+ EXPECT_EQ(statistics_prefs, settings()->statistics_prefs().get());
// When creating a network delegate, expect that it properly wraps a
// network delegate. Such a network delegate is thoroughly tested by
diff --git a/components/data_reduction_proxy/core/browser/data_reduction_proxy_metrics.cc b/components/data_reduction_proxy/core/browser/data_reduction_proxy_metrics.cc
index c712a15..e14c0aa 100644
--- a/components/data_reduction_proxy/core/browser/data_reduction_proxy_metrics.cc
+++ b/components/data_reduction_proxy/core/browser/data_reduction_proxy_metrics.cc
@@ -466,32 +466,4 @@ void UpdateContentLengthPrefsForDataReductionProxy(
}
}
-void UpdateContentLengthPrefs(int received_content_length,
- int original_content_length,
- bool data_reduction_proxy_enabled,
- DataReductionProxyRequestType request_type,
- DataReductionProxyStatisticsPrefs* prefs) {
- DCHECK(prefs);
- int64 total_received = prefs->GetInt64(
- data_reduction_proxy::prefs::kHttpReceivedContentLength);
- int64 total_original = prefs->GetInt64(
- data_reduction_proxy::prefs::kHttpOriginalContentLength);
- total_received += received_content_length;
- total_original += original_content_length;
- prefs->SetInt64(
- data_reduction_proxy::prefs::kHttpReceivedContentLength,
- total_received);
- prefs->SetInt64(
- data_reduction_proxy::prefs::kHttpOriginalContentLength,
- total_original);
-
- UpdateContentLengthPrefsForDataReductionProxy(
- received_content_length,
- original_content_length,
- data_reduction_proxy_enabled,
- request_type,
- base::Time::Now(),
- prefs);
-}
-
} // namespace data_reduction_proxy
diff --git a/components/data_reduction_proxy/core/browser/data_reduction_proxy_metrics.h b/components/data_reduction_proxy/core/browser/data_reduction_proxy_metrics.h
index 73c5752..a14a4a7 100644
--- a/components/data_reduction_proxy/core/browser/data_reduction_proxy_metrics.h
+++ b/components/data_reduction_proxy/core/browser/data_reduction_proxy_metrics.h
@@ -55,14 +55,6 @@ void UpdateContentLengthPrefsForDataReductionProxy(
base::Time now,
DataReductionProxyStatisticsPrefs* prefs);
-// Records daily data savings statistics to prefs and reports data savings UMA.
-void UpdateContentLengthPrefs(
- int received_content_length,
- int original_content_length,
- bool data_reduction_proxy_enabled,
- DataReductionProxyRequestType request_type,
- DataReductionProxyStatisticsPrefs* prefs);
-
} // namespace data_reduction_proxy
#endif // COMPONENTS_DATA_REDUCTION_PROXY_CORE_BROWSER_DATA_REDUCTION_PROXY_METRICS_H_
diff --git a/components/data_reduction_proxy/core/browser/data_reduction_proxy_metrics_unittest.cc b/components/data_reduction_proxy/core/browser/data_reduction_proxy_metrics_unittest.cc
index 64b0ed1..4584d6c6 100644
--- a/components/data_reduction_proxy/core/browser/data_reduction_proxy_metrics_unittest.cc
+++ b/components/data_reduction_proxy/core/browser/data_reduction_proxy_metrics_unittest.cc
@@ -91,42 +91,6 @@ class ChromeNetworkDataSavingMetricsTest : public testing::Test {
scoped_ptr<DataReductionProxyStatisticsPrefs> statistics_prefs_;
};
-TEST_F(ChromeNetworkDataSavingMetricsTest, TotalLengths) {
- const int64 kOriginalLength = 200;
- const int64 kReceivedLength = 100;
-
- UpdateContentLengthPrefs(
- kReceivedLength, kOriginalLength,
- pref_service_.GetBoolean(
- data_reduction_proxy::prefs::kDataReductionProxyEnabled),
- UNKNOWN_TYPE, statistics_prefs_.get());
-
- EXPECT_EQ(kReceivedLength,
- statistics_prefs_->GetInt64(
- data_reduction_proxy::prefs::kHttpReceivedContentLength));
- EXPECT_FALSE(pref_service_.GetBoolean(
- data_reduction_proxy::prefs::kDataReductionProxyEnabled));
- EXPECT_EQ(kOriginalLength,
- statistics_prefs_->GetInt64(
- data_reduction_proxy::prefs::kHttpOriginalContentLength));
-
- // Record the same numbers again, and total lengths should be doubled.
- UpdateContentLengthPrefs(
- kReceivedLength, kOriginalLength,
- pref_service_.GetBoolean(
- data_reduction_proxy::prefs::kDataReductionProxyEnabled),
- UNKNOWN_TYPE, statistics_prefs_.get());
-
- EXPECT_EQ(kReceivedLength * 2,
- statistics_prefs_->GetInt64(
- data_reduction_proxy::prefs::kHttpReceivedContentLength));
- EXPECT_FALSE(pref_service_.GetBoolean(
- data_reduction_proxy::prefs::kDataReductionProxyEnabled));
- EXPECT_EQ(kOriginalLength * 2,
- statistics_prefs_->GetInt64(
- data_reduction_proxy::prefs::kHttpOriginalContentLength));
-}
-
// The initial last update time used in test. There is no leap second a few
// days around this time used in the test.
// Note: No time zone is specified. Local time will be assumed by
diff --git a/components/data_reduction_proxy/core/browser/data_reduction_proxy_network_delegate.cc b/components/data_reduction_proxy/core/browser/data_reduction_proxy_network_delegate.cc
index b925211..8f14bd6 100644
--- a/components/data_reduction_proxy/core/browser/data_reduction_proxy_network_delegate.cc
+++ b/components/data_reduction_proxy/core/browser/data_reduction_proxy_network_delegate.cc
@@ -5,6 +5,7 @@
#include "components/data_reduction_proxy/core/browser/data_reduction_proxy_network_delegate.h"
#include "base/bind.h"
+#include "base/bind_helpers.h"
#include "base/metrics/histogram.h"
#include "base/prefs/pref_service.h"
#include "base/single_thread_task_runner.h"
@@ -84,7 +85,6 @@ DataReductionProxyNetworkDelegate::DataReductionProxyNetworkDelegate(
data_reduction_proxy_params_(params),
data_reduction_proxy_usage_stats_(NULL),
data_reduction_proxy_auth_request_handler_(handler),
- data_reduction_proxy_statistics_prefs_(NULL),
configurator_(configurator) {
DCHECK(data_reduction_proxy_params_);
DCHECK(data_reduction_proxy_auth_request_handler_);
@@ -95,10 +95,9 @@ DataReductionProxyNetworkDelegate::~DataReductionProxyNetworkDelegate() {
void DataReductionProxyNetworkDelegate::InitStatisticsPrefsAndUMA(
scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner,
- DataReductionProxyStatisticsPrefs* statistics_prefs,
+ const base::WeakPtr<DataReductionProxyStatisticsPrefs>& statistics_prefs,
BooleanPrefMember* data_reduction_proxy_enabled,
DataReductionProxyUsageStats* usage_stats) {
- DCHECK(statistics_prefs);
DCHECK(data_reduction_proxy_enabled);
DCHECK(usage_stats);
ui_task_runner_ = ui_task_runner;
@@ -233,21 +232,49 @@ void DataReductionProxyNetworkDelegate::AccumulateContentLength(
DataReductionProxyRequestType request_type) {
DCHECK_GE(received_content_length, 0);
DCHECK_GE(original_content_length, 0);
- if (data_reduction_proxy_enabled_ &&
- data_reduction_proxy_statistics_prefs_) {
+ if (data_reduction_proxy_enabled_) {
ui_task_runner_->PostTask(
FROM_HERE,
- base::Bind(&UpdateContentLengthPrefs,
+ base::Bind(&DataReductionProxyNetworkDelegate::UpdateContentLengthPrefs,
+ base::Unretained(this),
received_content_length,
original_content_length,
data_reduction_proxy_enabled_->GetValue(),
- request_type,
- data_reduction_proxy_statistics_prefs_));
+ request_type));
}
received_content_length_ += received_content_length;
original_content_length_ += original_content_length;
}
+void DataReductionProxyNetworkDelegate::UpdateContentLengthPrefs(
+ int received_content_length,
+ int original_content_length,
+ bool data_reduction_proxy_enabled,
+ DataReductionProxyRequestType request_type) {
+ if (data_reduction_proxy_statistics_prefs_) {
+ int64 total_received = data_reduction_proxy_statistics_prefs_->GetInt64(
+ data_reduction_proxy::prefs::kHttpReceivedContentLength);
+ int64 total_original = data_reduction_proxy_statistics_prefs_->GetInt64(
+ data_reduction_proxy::prefs::kHttpOriginalContentLength);
+ total_received += received_content_length;
+ total_original += original_content_length;
+ data_reduction_proxy_statistics_prefs_->SetInt64(
+ data_reduction_proxy::prefs::kHttpReceivedContentLength,
+ total_received);
+ data_reduction_proxy_statistics_prefs_->SetInt64(
+ data_reduction_proxy::prefs::kHttpOriginalContentLength,
+ total_original);
+
+ UpdateContentLengthPrefsForDataReductionProxy(
+ received_content_length,
+ original_content_length,
+ data_reduction_proxy_enabled,
+ request_type,
+ base::Time::Now(),
+ data_reduction_proxy_statistics_prefs_.get());
+ }
+}
+
void OnResolveProxyHandler(const GURL& url,
int load_flags,
const net::ProxyConfig& data_reduction_proxy_config,
diff --git a/components/data_reduction_proxy/core/browser/data_reduction_proxy_network_delegate.h b/components/data_reduction_proxy/core/browser/data_reduction_proxy_network_delegate.h
index 7da1858..28712ee 100644
--- a/components/data_reduction_proxy/core/browser/data_reduction_proxy_network_delegate.h
+++ b/components/data_reduction_proxy/core/browser/data_reduction_proxy_network_delegate.h
@@ -6,8 +6,10 @@
#define COMPONENTS_DATA_REDUCTION_PROXY_CORE_BROWSER_DATA_REDUCTION_PROXY_NETWORK_DELEGATE_H_
#include "base/basictypes.h"
+#include "base/gtest_prod_util.h"
#include "base/memory/ref_counted.h"
#include "base/memory/scoped_ptr.h"
+#include "base/memory/weak_ptr.h"
#include "base/values.h"
#include "components/data_reduction_proxy/core/browser/data_reduction_proxy_metrics.h"
#include "net/base/layered_network_delegate.h"
@@ -68,7 +70,7 @@ class DataReductionProxyNetworkDelegate : public net::LayeredNetworkDelegate {
// report UMA.
void InitStatisticsPrefsAndUMA(
scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner,
- DataReductionProxyStatisticsPrefs* statistics_prefs,
+ const base::WeakPtr<DataReductionProxyStatisticsPrefs>& statistics_prefs,
BooleanPrefMember* data_reduction_proxy_enabled,
DataReductionProxyUsageStats* usage_stats);
@@ -83,6 +85,8 @@ class DataReductionProxyNetworkDelegate : public net::LayeredNetworkDelegate {
base::Value* SessionNetworkStatsInfoToValue() const;
private:
+ FRIEND_TEST_ALL_PREFIXES(DataReductionProxyNetworkDelegateTest, TotalLengths);
+
// Called as the proxy is being resolved for |url|. Allows the delegate to
// override the proxy resolution decision made by ProxyService. The delegate
// may override the decision by modifying the ProxyInfo |result|.
@@ -119,6 +123,13 @@ class DataReductionProxyNetworkDelegate : public net::LayeredNetworkDelegate {
int64 original_content_length,
DataReductionProxyRequestType request_type);
+ // Records daily data savings statistics to prefs and reports data savings
+ // UMA.
+ void UpdateContentLengthPrefs(int received_content_length,
+ int original_content_length,
+ bool data_reduction_proxy_enabled,
+ DataReductionProxyRequestType request_type);
+
scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner_;
// Total size of all content (excluding headers) that has been received
@@ -140,7 +151,8 @@ class DataReductionProxyNetworkDelegate : public net::LayeredNetworkDelegate {
DataReductionProxyAuthRequestHandler*
data_reduction_proxy_auth_request_handler_;
- DataReductionProxyStatisticsPrefs* data_reduction_proxy_statistics_prefs_;
+ base::WeakPtr<DataReductionProxyStatisticsPrefs>
+ data_reduction_proxy_statistics_prefs_;
const DataReductionProxyConfigurator* configurator_;
diff --git a/components/data_reduction_proxy/core/browser/data_reduction_proxy_network_delegate_unittest.cc b/components/data_reduction_proxy/core/browser/data_reduction_proxy_network_delegate_unittest.cc
index 46e235e..3d53c06 100644
--- a/components/data_reduction_proxy/core/browser/data_reduction_proxy_network_delegate_unittest.cc
+++ b/components/data_reduction_proxy/core/browser/data_reduction_proxy_network_delegate_unittest.cc
@@ -13,9 +13,13 @@
#include "base/run_loop.h"
#include "base/strings/string_number_conversions.h"
#include "base/test/histogram_tester.h"
+#include "base/test/test_simple_task_runner.h"
#include "base/time/time.h"
#include "components/data_reduction_proxy/core/browser/data_reduction_proxy_auth_request_handler.h"
#include "components/data_reduction_proxy/core/browser/data_reduction_proxy_configurator.h"
+#include "components/data_reduction_proxy/core/browser/data_reduction_proxy_metrics.h"
+#include "components/data_reduction_proxy/core/browser/data_reduction_proxy_prefs.h"
+#include "components/data_reduction_proxy/core/browser/data_reduction_proxy_statistics_prefs.h"
#include "components/data_reduction_proxy/core/common/data_reduction_proxy_event_store.h"
#include "components/data_reduction_proxy/core/common/data_reduction_proxy_headers_test_utils.h"
#include "components/data_reduction_proxy/core/common/data_reduction_proxy_params_test_utils.h"
@@ -77,6 +81,26 @@ class DataReductionProxyNetworkDelegateTest : public testing::Test {
test_job_interceptor_));
context_.set_job_factory(&test_job_factory_);
event_store_.reset(new DataReductionProxyEventStore(message_loop_proxy()));
+
+ params_.reset(
+ new TestDataReductionProxyParams(
+ DataReductionProxyParams::kAllowed |
+ DataReductionProxyParams::kFallbackAllowed |
+ DataReductionProxyParams::kPromoAllowed,
+ TestDataReductionProxyParams::HAS_EVERYTHING &
+ ~TestDataReductionProxyParams::HAS_DEV_ORIGIN &
+ ~TestDataReductionProxyParams::HAS_DEV_FALLBACK_ORIGIN));
+
+ auth_handler_.reset(new DataReductionProxyAuthRequestHandler(
+ kClient, params_.get(), message_loop_proxy()));
+
+ configurator_.reset(new DataReductionProxyConfigurator(
+ message_loop_proxy(), net_log(), event_store()));
+
+ data_reduction_proxy_network_delegate_.reset(
+ new DataReductionProxyNetworkDelegate(
+ scoped_ptr<net::NetworkDelegate>(new TestNetworkDelegate()),
+ params_.get(), auth_handler_.get(), configurator_.get()));
}
const net::ProxyConfig& GetProxyConfig() const {
@@ -129,6 +153,12 @@ class DataReductionProxyNetworkDelegateTest : public testing::Test {
return event_store_.get();
}
+ scoped_ptr<TestDataReductionProxyParams> params_;
+ scoped_ptr<DataReductionProxyAuthRequestHandler> auth_handler_;
+ scoped_ptr<DataReductionProxyConfigurator> configurator_;
+ scoped_ptr<DataReductionProxyNetworkDelegate>
+ data_reduction_proxy_network_delegate_;
+
private:
base::MessageLoopForIO loop_;
net::TestURLRequestContext context_;
@@ -144,40 +174,17 @@ class DataReductionProxyNetworkDelegateTest : public testing::Test {
};
TEST_F(DataReductionProxyNetworkDelegateTest, AuthenticationTest) {
- scoped_ptr<TestDataReductionProxyParams> params(
- new TestDataReductionProxyParams(
- DataReductionProxyParams::kAllowed |
- DataReductionProxyParams::kFallbackAllowed |
- DataReductionProxyParams::kPromoAllowed,
- TestDataReductionProxyParams::HAS_EVERYTHING &
- ~TestDataReductionProxyParams::HAS_DEV_ORIGIN &
- ~TestDataReductionProxyParams::HAS_DEV_FALLBACK_ORIGIN));
- // loop_proxy_ is just the current message loop. This means loop_proxy_
- // is the network thread used by DataReductionProxyAuthRequestHandler.
- DataReductionProxyAuthRequestHandler auth_handler(
- kClient, params.get(), message_loop_proxy());
-
- scoped_ptr<DataReductionProxyConfigurator> configurator(
- new DataReductionProxyConfigurator(message_loop_proxy(), net_log(),
- event_store()));
-
- scoped_ptr<DataReductionProxyNetworkDelegate> network_delegate(
- new DataReductionProxyNetworkDelegate(
- scoped_ptr<net::NetworkDelegate>(new TestNetworkDelegate()),
- params.get(), &auth_handler,
- configurator.get()));
-
- set_network_delegate(network_delegate.get());
+ set_network_delegate(data_reduction_proxy_network_delegate_.get());
scoped_ptr<net::URLRequest> fake_request(
FetchURLRequest(GURL("http://www.google.com/"), std::string(), 0));
net::ProxyInfo data_reduction_proxy_info;
std::string data_reduction_proxy;
- base::TrimString(params->DefaultOrigin(), "/", &data_reduction_proxy);
+ base::TrimString(params_->DefaultOrigin(), "/", &data_reduction_proxy);
data_reduction_proxy_info.UseNamedProxy(data_reduction_proxy);
net::HttpRequestHeaders headers;
- network_delegate->NotifyBeforeSendProxyHeaders(
+ data_reduction_proxy_network_delegate_->NotifyBeforeSendProxyHeaders(
fake_request.get(), data_reduction_proxy_info, &headers);
EXPECT_TRUE(headers.HasHeader(kChromeProxyHeader));
@@ -208,30 +215,9 @@ TEST_F(DataReductionProxyNetworkDelegateTest, NetHistograms) {
const int64 kResponseContentLength = 100;
const int64 kOriginalContentLength = 200;
- scoped_ptr<TestDataReductionProxyParams> params(
- new TestDataReductionProxyParams(
- DataReductionProxyParams::kAllowed |
- DataReductionProxyParams::kFallbackAllowed |
- DataReductionProxyParams::kPromoAllowed,
- TestDataReductionProxyParams::HAS_EVERYTHING &
- ~TestDataReductionProxyParams::HAS_DEV_ORIGIN &
- ~TestDataReductionProxyParams::HAS_DEV_FALLBACK_ORIGIN));
- // loop_proxy_ is just the current message loop. This means loop_proxy_
- // is the network thread used by DataReductionProxyAuthRequestHandler.
- DataReductionProxyAuthRequestHandler auth_handler(
- kClient, params.get(), message_loop_proxy());
-
base::HistogramTester histogram_tester;
- scoped_ptr<DataReductionProxyConfigurator> configurator(
- new DataReductionProxyConfigurator(message_loop_proxy(), net_log(),
- event_store()));
- scoped_ptr<DataReductionProxyNetworkDelegate> network_delegate(
- new DataReductionProxyNetworkDelegate(
- scoped_ptr<net::NetworkDelegate>(
- new TestNetworkDelegate()), params.get(), &auth_handler,
- configurator.get()));
- set_network_delegate(network_delegate.get());
+ set_network_delegate(data_reduction_proxy_network_delegate_.get());
std::string raw_headers =
"HTTP/1.1 200 OK\n"
@@ -275,65 +261,6 @@ TEST_F(DataReductionProxyNetworkDelegateTest, NetHistograms) {
kResponseContentLength, 1);
}
-class DataReductionProxyHistoricNetworkStatsTest
- : public testing::Test {
- public:
- DataReductionProxyHistoricNetworkStatsTest() {}
-
- void SetUp() override {
- simple_pref_service_.registry()->RegisterInt64Pref(
- prefs::kHttpReceivedContentLength, 0);
- simple_pref_service_.registry()->RegisterInt64Pref(
- prefs::kHttpOriginalContentLength, 0);
- }
-
- // Verify the pref values in |dict| are equal to that in
- // |simple_pref_service|.
- void VerifyPrefs(base::DictionaryValue* dict) {
- base::string16 dict_pref_string;
- int64 dict_pref;
- int64 service_pref;
-
- dict->GetString("historic_original_content_length", &dict_pref_string);
- base::StringToInt64(dict_pref_string, &dict_pref);
- service_pref = simple_pref_service_.GetInt64(
- prefs::kHttpOriginalContentLength);
- EXPECT_EQ(service_pref, dict_pref);
-
- dict->GetString("historic_received_content_length", &dict_pref_string);
- base::StringToInt64(dict_pref_string, &dict_pref);
- service_pref = simple_pref_service_.GetInt64(
- prefs::kHttpReceivedContentLength);
- EXPECT_EQ(service_pref, dict_pref);
- }
-
- TestingPrefServiceSimple simple_pref_service_;
-};
-
-TEST_F(DataReductionProxyHistoricNetworkStatsTest,
- HistoricNetworkStatsInfoToValue) {
- const int64 kOriginalLength = 150;
- const int64 kReceivedLength = 100;
-
- base::DictionaryValue* dict = nullptr;
- scoped_ptr<base::Value> stats_value(
- DataReductionProxyNetworkDelegate::HistoricNetworkStatsInfoToValue(
- &simple_pref_service_));
- EXPECT_TRUE(stats_value->GetAsDictionary(&dict));
- VerifyPrefs(dict);
-
- simple_pref_service_.SetInt64(prefs::kHttpOriginalContentLength,
- kOriginalLength);
- simple_pref_service_.SetInt64(prefs::kHttpReceivedContentLength,
- kReceivedLength);
-
- stats_value.reset(
- DataReductionProxyNetworkDelegate::HistoricNetworkStatsInfoToValue(
- &simple_pref_service_));
- EXPECT_TRUE(stats_value->GetAsDictionary(&dict));
- VerifyPrefs(dict);
-}
-
class BadEntropyProvider : public base::FieldTrial::EntropyProvider {
public:
~BadEntropyProvider() override {}
@@ -348,23 +275,15 @@ TEST_F(DataReductionProxyNetworkDelegateTest, OnResolveProxyHandler) {
int load_flags = net::LOAD_NORMAL;
GURL url("http://www.google.com/");
- TestDataReductionProxyParams test_params(
- DataReductionProxyParams::kAllowed |
- DataReductionProxyParams::kFallbackAllowed |
- DataReductionProxyParams::kPromoAllowed,
- TestDataReductionProxyParams::HAS_EVERYTHING &
- ~TestDataReductionProxyParams::HAS_DEV_ORIGIN &
- ~TestDataReductionProxyParams::HAS_DEV_FALLBACK_ORIGIN);
-
// Data reduction proxy info
net::ProxyInfo data_reduction_proxy_info;
std::string data_reduction_proxy;
- base::TrimString(test_params.DefaultOrigin(), "/", &data_reduction_proxy);
+ base::TrimString(params_->DefaultOrigin(), "/", &data_reduction_proxy);
data_reduction_proxy_info.UsePacString(
"PROXY " +
net::ProxyServer::FromURI(
- test_params.DefaultOrigin(),
- net::ProxyServer::SCHEME_HTTP).host_port_pair().ToString() +
+ params_->DefaultOrigin(),
+ net::ProxyServer::SCHEME_HTTP).host_port_pair().ToString() +
"; DIRECT");
EXPECT_FALSE(data_reduction_proxy_info.is_empty());
@@ -400,7 +319,7 @@ TEST_F(DataReductionProxyNetworkDelegateTest, OnResolveProxyHandler) {
// Another proxy is used. It should be used afterwards.
result.Use(other_proxy_info);
OnResolveProxyHandler(url, load_flags, data_reduction_proxy_config,
- empty_proxy_retry_info, &test_params, &result);
+ empty_proxy_retry_info, params_.get(), &result);
EXPECT_EQ(other_proxy_info.proxy_server(), result.proxy_server());
// A direct connection is used. The data reduction proxy should be used
@@ -409,7 +328,7 @@ TEST_F(DataReductionProxyNetworkDelegateTest, OnResolveProxyHandler) {
result.Use(direct_proxy_info);
net::ProxyConfig::ID prev_id = result.config_id();
OnResolveProxyHandler(url, load_flags, data_reduction_proxy_config,
- empty_proxy_retry_info, &test_params, &result);
+ empty_proxy_retry_info, params_.get(), &result);
EXPECT_EQ(data_reduction_proxy_info.proxy_server(), result.proxy_server());
// Only the proxy list should be updated, not he proxy info.
EXPECT_EQ(result.config_id(), prev_id);
@@ -419,45 +338,46 @@ TEST_F(DataReductionProxyNetworkDelegateTest, OnResolveProxyHandler) {
result.Use(direct_proxy_info);
prev_id = result.config_id();
OnResolveProxyHandler(url, load_flags, data_reduction_proxy_config,
- data_reduction_proxy_retry_info, &test_params, &result);
+ data_reduction_proxy_retry_info,
+ params_.get(), &result);
EXPECT_TRUE(result.proxy_server().is_direct());
EXPECT_EQ(result.config_id(), prev_id);
// Test that ws:// and wss:// URLs bypass the data reduction proxy.
result.UseDirect();
OnResolveProxyHandler(GURL("ws://echo.websocket.org/"),
- load_flags, data_reduction_proxy_config,
- empty_proxy_retry_info, &test_params, &result);
+ load_flags, data_reduction_proxy_config,
+ empty_proxy_retry_info, params_.get(), &result);
EXPECT_TRUE(result.is_direct());
OnResolveProxyHandler(GURL("wss://echo.websocket.org/"),
- load_flags, data_reduction_proxy_config,
- empty_proxy_retry_info, &test_params, &result);
+ load_flags, data_reduction_proxy_config,
+ empty_proxy_retry_info, params_.get(), &result);
EXPECT_TRUE(result.is_direct());
// Without DataCompressionProxyCriticalBypass Finch trial set, the
// BYPASS_DATA_REDUCTION_PROXY load flag should be ignored.
OnResolveProxyHandler(url, load_flags, data_reduction_proxy_config,
- empty_proxy_retry_info, &test_params,
+ empty_proxy_retry_info, params_.get(),
&result);
EXPECT_FALSE(result.is_direct());
OnResolveProxyHandler(url, load_flags, data_reduction_proxy_config,
empty_proxy_retry_info,
- &test_params, &other_proxy_info);
+ params_.get(), &other_proxy_info);
EXPECT_FALSE(other_proxy_info.is_direct());
load_flags |= net::LOAD_BYPASS_DATA_REDUCTION_PROXY;
result.UseDirect();
OnResolveProxyHandler(url, load_flags, data_reduction_proxy_config,
- empty_proxy_retry_info, &test_params,
+ empty_proxy_retry_info, params_.get(),
&result);
EXPECT_FALSE(result.is_direct());
OnResolveProxyHandler(url, load_flags, data_reduction_proxy_config,
empty_proxy_retry_info,
- &test_params, &other_proxy_info);
+ params_.get(), &other_proxy_info);
EXPECT_FALSE(other_proxy_info.is_direct());
// With Finch trial set, should only bypass if LOAD flag is set and the
@@ -472,12 +392,12 @@ TEST_F(DataReductionProxyNetworkDelegateTest, OnResolveProxyHandler) {
result.UseDirect();
OnResolveProxyHandler(url, load_flags, data_reduction_proxy_config,
- empty_proxy_retry_info, &test_params,
+ empty_proxy_retry_info, params_.get(),
&result);
EXPECT_FALSE(result.is_direct());
OnResolveProxyHandler(url, load_flags, data_reduction_proxy_config,
- empty_proxy_retry_info, &test_params,
+ empty_proxy_retry_info, params_.get(),
&other_proxy_info);
EXPECT_FALSE(other_proxy_info.is_direct());
@@ -485,14 +405,124 @@ TEST_F(DataReductionProxyNetworkDelegateTest, OnResolveProxyHandler) {
result.UseDirect();
OnResolveProxyHandler(url, load_flags, data_reduction_proxy_config,
- empty_proxy_retry_info, &test_params,
+ empty_proxy_retry_info, params_.get(),
&result);
EXPECT_TRUE(result.is_direct());
OnResolveProxyHandler(url, load_flags, data_reduction_proxy_config,
- empty_proxy_retry_info, &test_params,
+ empty_proxy_retry_info, params_.get(),
&other_proxy_info);
EXPECT_FALSE(other_proxy_info.is_direct());
}
+TEST_F(DataReductionProxyNetworkDelegateTest, TotalLengths) {
+ const int64 kOriginalLength = 200;
+ const int64 kReceivedLength = 100;
+
+ TestingPrefServiceSimple pref_service;
+ scoped_ptr<DataReductionProxyStatisticsPrefs> statistics_prefs(
+ new DataReductionProxyStatisticsPrefs(
+ &pref_service,
+ scoped_refptr<base::TestSimpleTaskRunner>(
+ new base::TestSimpleTaskRunner()),
+ base::TimeDelta()));
+ PrefRegistrySimple* registry = pref_service.registry();
+ data_reduction_proxy::RegisterSimpleProfilePrefs(registry);
+
+ data_reduction_proxy_network_delegate_->
+ data_reduction_proxy_statistics_prefs_ =
+ statistics_prefs->GetWeakPtr();
+
+ data_reduction_proxy_network_delegate_->UpdateContentLengthPrefs(
+ kReceivedLength, kOriginalLength,
+ pref_service.GetBoolean(
+ data_reduction_proxy::prefs::kDataReductionProxyEnabled),
+ UNKNOWN_TYPE);
+
+ EXPECT_EQ(kReceivedLength,
+ statistics_prefs->GetInt64(
+ data_reduction_proxy::prefs::kHttpReceivedContentLength));
+ EXPECT_FALSE(pref_service.GetBoolean(
+ data_reduction_proxy::prefs::kDataReductionProxyEnabled));
+ EXPECT_EQ(kOriginalLength,
+ statistics_prefs->GetInt64(
+ data_reduction_proxy::prefs::kHttpOriginalContentLength));
+
+ // Record the same numbers again, and total lengths should be doubled.
+ data_reduction_proxy_network_delegate_->UpdateContentLengthPrefs(
+ kReceivedLength, kOriginalLength,
+ pref_service.GetBoolean(
+ data_reduction_proxy::prefs::kDataReductionProxyEnabled),
+ UNKNOWN_TYPE);
+
+ EXPECT_EQ(kReceivedLength * 2,
+ statistics_prefs->GetInt64(
+ data_reduction_proxy::prefs::kHttpReceivedContentLength));
+ EXPECT_FALSE(pref_service.GetBoolean(
+ data_reduction_proxy::prefs::kDataReductionProxyEnabled));
+ EXPECT_EQ(kOriginalLength * 2,
+ statistics_prefs->GetInt64(
+ data_reduction_proxy::prefs::kHttpOriginalContentLength));
+}
+
+class DataReductionProxyHistoricNetworkStatsTest
+ : public testing::Test {
+ public:
+ DataReductionProxyHistoricNetworkStatsTest() {
+ }
+
+ void SetUp() override {
+ simple_pref_service_.registry()->RegisterInt64Pref(
+ prefs::kHttpReceivedContentLength, 0);
+ simple_pref_service_.registry()->RegisterInt64Pref(
+ prefs::kHttpOriginalContentLength, 0);
+ }
+
+ // Verify the pref values in |dict| are equal to that in
+ // |simple_pref_service|.
+ void VerifyPrefs(base::DictionaryValue* dict) {
+ base::string16 dict_pref_string;
+ int64 dict_pref;
+ int64 service_pref;
+
+ dict->GetString("historic_original_content_length", &dict_pref_string);
+ base::StringToInt64(dict_pref_string, &dict_pref);
+ service_pref = simple_pref_service_.GetInt64(
+ prefs::kHttpOriginalContentLength);
+ EXPECT_EQ(service_pref, dict_pref);
+
+ dict->GetString("historic_received_content_length", &dict_pref_string);
+ base::StringToInt64(dict_pref_string, &dict_pref);
+ service_pref = simple_pref_service_.GetInt64(
+ prefs::kHttpReceivedContentLength);
+ EXPECT_EQ(service_pref, dict_pref);
+ }
+
+ TestingPrefServiceSimple simple_pref_service_;
+};
+
+TEST_F(DataReductionProxyHistoricNetworkStatsTest,
+ HistoricNetworkStatsInfoToValue) {
+ const int64 kOriginalLength = 150;
+ const int64 kReceivedLength = 100;
+
+ base::DictionaryValue* dict = nullptr;
+ scoped_ptr<base::Value> stats_value(
+ DataReductionProxyNetworkDelegate::HistoricNetworkStatsInfoToValue(
+ &simple_pref_service_));
+ EXPECT_TRUE(stats_value->GetAsDictionary(&dict));
+ VerifyPrefs(dict);
+
+ simple_pref_service_.SetInt64(prefs::kHttpOriginalContentLength,
+ kOriginalLength);
+ simple_pref_service_.SetInt64(prefs::kHttpReceivedContentLength,
+ kReceivedLength);
+
+ stats_value.reset(
+ DataReductionProxyNetworkDelegate::HistoricNetworkStatsInfoToValue(
+ &simple_pref_service_));
+ EXPECT_TRUE(stats_value->GetAsDictionary(&dict));
+ VerifyPrefs(dict);
+}
+
} // namespace data_reduction_proxy
diff --git a/components/data_reduction_proxy/core/browser/data_reduction_proxy_settings.cc b/components/data_reduction_proxy/core/browser/data_reduction_proxy_settings.cc
index 26fad47..2dba435 100644
--- a/components/data_reduction_proxy/core/browser/data_reduction_proxy_settings.cc
+++ b/components/data_reduction_proxy/core/browser/data_reduction_proxy_settings.cc
@@ -128,14 +128,17 @@ void DataReductionProxySettings::InitPrefMembers() {
void DataReductionProxySettings::InitDataReductionProxySettings(
PrefService* prefs,
+ scoped_ptr<DataReductionProxyStatisticsPrefs> statistics_prefs,
net::URLRequestContextGetter* url_request_context_getter,
net::NetLog* net_log,
DataReductionProxyEventStore* event_store) {
DCHECK(thread_checker_.CalledOnValidThread());
DCHECK(prefs);
+ DCHECK(!statistics_prefs_);
DCHECK(url_request_context_getter);
DCHECK(event_store);
prefs_ = prefs;
+ statistics_prefs_ = statistics_prefs.Pass();
url_request_context_getter_ = url_request_context_getter;
net_log_ = net_log;
event_store_ = event_store;
@@ -151,8 +154,25 @@ void DataReductionProxySettings::InitDataReductionProxySettings(
}
void DataReductionProxySettings::SetDataReductionProxyStatisticsPrefs(
- DataReductionProxyStatisticsPrefs* statistics_prefs) {
- statistics_prefs_ = statistics_prefs;
+ scoped_ptr<DataReductionProxyStatisticsPrefs> statistics_prefs) {
+ statistics_prefs_ = statistics_prefs.Pass();
+}
+
+void DataReductionProxySettings::EnableCompressionStatisticsLogging(
+ PrefService* prefs,
+ scoped_refptr<base::SequencedTaskRunner> ui_task_runner,
+ const base::TimeDelta& commit_delay) {
+ DCHECK(!statistics_prefs_);
+ statistics_prefs_.reset(
+ new DataReductionProxyStatisticsPrefs(
+ prefs, ui_task_runner, commit_delay));
+}
+
+base::WeakPtr<DataReductionProxyStatisticsPrefs>
+DataReductionProxySettings::statistics_prefs() {
+ if (statistics_prefs_)
+ return statistics_prefs_->GetWeakPtr();
+ return base::WeakPtr<DataReductionProxyStatisticsPrefs>();
}
void DataReductionProxySettings::SetOnDataReductionEnabledCallback(
diff --git a/components/data_reduction_proxy/core/browser/data_reduction_proxy_settings.h b/components/data_reduction_proxy/core/browser/data_reduction_proxy_settings.h
index 9116687..61539be 100644
--- a/components/data_reduction_proxy/core/browser/data_reduction_proxy_settings.h
+++ b/components/data_reduction_proxy/core/browser/data_reduction_proxy_settings.h
@@ -36,6 +36,7 @@ class URLRequestContextGetter;
namespace data_reduction_proxy {
class DataReductionProxyEventStore;
+class DataReductionProxyStatisticsPrefs;
// The number of days of bandwidth usage statistics that are tracked.
const unsigned int kNumDaysInHistory = 60;
@@ -83,14 +84,19 @@ class DataReductionProxySettings
// |DataReductionProxySettings| instance.
void InitDataReductionProxySettings(
PrefService* prefs,
+ scoped_ptr<DataReductionProxyStatisticsPrefs> statistics_prefs,
net::URLRequestContextGetter* url_request_context_getter,
net::NetLog* net_log,
DataReductionProxyEventStore* event_store);
- // Sets the |statistics_prefs_| to be used for data reduction proxy pref reads
- // and writes.
- void SetDataReductionProxyStatisticsPrefs(
- DataReductionProxyStatisticsPrefs* statistics_prefs);
+ // Constructs statistics prefs. This should not be called if a valid
+ // statistics prefs is passed into the constructor.
+ void EnableCompressionStatisticsLogging(
+ PrefService* prefs,
+ scoped_refptr<base::SequencedTaskRunner> ui_task_runner,
+ const base::TimeDelta& commit_delay);
+
+ base::WeakPtr<DataReductionProxyStatisticsPrefs> statistics_prefs();
// Sets the |on_data_reduction_proxy_enabled_| callback and runs to register
// the DataReductionProxyEnabled synthetic field trial.
@@ -163,6 +169,10 @@ class DataReductionProxySettings
return event_store_;
}
+ // Used for testing.
+ void SetDataReductionProxyStatisticsPrefs(
+ scoped_ptr<DataReductionProxyStatisticsPrefs> statistics_prefs);
+
protected:
void InitPrefMembers();
@@ -283,7 +293,7 @@ class DataReductionProxySettings
BooleanPrefMember data_reduction_proxy_alternative_enabled_;
PrefService* prefs_;
- DataReductionProxyStatisticsPrefs* statistics_prefs_;
+ scoped_ptr<DataReductionProxyStatisticsPrefs> statistics_prefs_;
net::URLRequestContextGetter* url_request_context_getter_;
diff --git a/components/data_reduction_proxy/core/browser/data_reduction_proxy_settings_test_utils.cc b/components/data_reduction_proxy/core/browser/data_reduction_proxy_settings_test_utils.cc
index 053b0bc..bab1889 100644
--- a/components/data_reduction_proxy/core/browser/data_reduction_proxy_settings_test_utils.cc
+++ b/components/data_reduction_proxy/core/browser/data_reduction_proxy_settings_test_utils.cc
@@ -68,11 +68,6 @@ void DataReductionProxySettingsTestBase::SetUp() {
registry->RegisterBooleanPref(prefs::kDataReductionProxyWasEnabledBefore,
false);
- statistics_prefs_.reset(new DataReductionProxyStatisticsPrefs(
- &pref_service_,
- scoped_refptr<base::TestSimpleTaskRunner>(
- new base::TestSimpleTaskRunner()),
- base::TimeDelta()));
event_store_.reset(new DataReductionProxyEventStore(
scoped_refptr<base::TestSimpleTaskRunner>(
new base::TestSimpleTaskRunner())));
@@ -90,9 +85,17 @@ void DataReductionProxySettingsTestBase::SetUp() {
received_update->Insert(0, new base::StringValue(base::Int64ToString(i)));
}
last_update_time_ = base::Time::Now().LocalMidnight();
- statistics_prefs_->SetInt64(
+ scoped_ptr<DataReductionProxyStatisticsPrefs> statistics_prefs(
+ new DataReductionProxyStatisticsPrefs(
+ &pref_service_,
+ scoped_refptr<base::TestSimpleTaskRunner>(
+ new base::TestSimpleTaskRunner()),
+ base::TimeDelta()));
+ statistics_prefs->SetInt64(
prefs::kDailyHttpContentLengthLastUpdateDate,
last_update_time_.ToInternalValue());
+ settings_->SetDataReductionProxyStatisticsPrefs(
+ statistics_prefs.Pass());
expected_params_.reset(new TestDataReductionProxyParams(
DataReductionProxyParams::kAllowed |
DataReductionProxyParams::kFallbackAllowed |
@@ -134,7 +137,6 @@ void DataReductionProxySettingsTestBase::ResetSettings(bool allowed,
scoped_refptr<base::TestSimpleTaskRunner>(
new base::TestSimpleTaskRunner()), &net_log_, event_store_.get()));
settings_->configurator_ = configurator_.get();
- settings_->SetDataReductionProxyStatisticsPrefs(statistics_prefs_.get());
}
// Explicitly generate required instantiations.
@@ -269,8 +271,13 @@ void DataReductionProxySettingsTestBase::CheckInitDataReductionProxy(
scoped_refptr<net::TestURLRequestContextGetter> request_context =
new net::TestURLRequestContextGetter(base::MessageLoopProxy::current());
+ // Delete statistics prefs, otherwise the DCHECK in
+ // InitDataReductionProxySettings fails.
+ settings_->SetDataReductionProxyStatisticsPrefs(
+ scoped_ptr<data_reduction_proxy::DataReductionProxyStatisticsPrefs>());
settings_->InitDataReductionProxySettings(
&pref_service_,
+ scoped_ptr<data_reduction_proxy::DataReductionProxyStatisticsPrefs>(),
request_context.get(),
&net_log_,
event_store_.get());
diff --git a/components/data_reduction_proxy/core/browser/data_reduction_proxy_settings_test_utils.h b/components/data_reduction_proxy/core/browser/data_reduction_proxy_settings_test_utils.h
index c364706..ab8b06c 100644
--- a/components/data_reduction_proxy/core/browser/data_reduction_proxy_settings_test_utils.h
+++ b/components/data_reduction_proxy/core/browser/data_reduction_proxy_settings_test_utils.h
@@ -125,6 +125,7 @@ class DataReductionProxySettingsTestBase : public testing::Test {
bool expected_enabled,
bool expected_fallback_restricted);
void CheckOnPrefChange(bool enabled, bool expected_enabled, bool managed);
+ void InitWithStatisticsPrefs();
void CheckInitDataReductionProxy(bool enabled_at_startup);
void RegisterSyntheticFieldTrialCallback(bool proxy_enabled) {
proxy_enabled_ = proxy_enabled;
@@ -136,7 +137,6 @@ class DataReductionProxySettingsTestBase : public testing::Test {
scoped_ptr<TestDataReductionProxyParams> expected_params_;
base::Time last_update_time_;
bool proxy_enabled_;
- scoped_ptr<DataReductionProxyStatisticsPrefs> statistics_prefs_;
net::CapturingNetLog net_log_;
scoped_ptr<DataReductionProxyEventStore> event_store_;
};
diff --git a/components/data_reduction_proxy/core/browser/data_reduction_proxy_settings_unittest.cc b/components/data_reduction_proxy/core/browser/data_reduction_proxy_settings_unittest.cc
index f8be374..b588554 100644
--- a/components/data_reduction_proxy/core/browser/data_reduction_proxy_settings_unittest.cc
+++ b/components/data_reduction_proxy/core/browser/data_reduction_proxy_settings_unittest.cc
@@ -29,6 +29,8 @@ const char kProbeURLWithNoResponse[] = "http://no.org/";
namespace data_reduction_proxy {
+class DataReductionProxyStatisticsPrefs;
+
class DataReductionProxySettingsTest
: public ConcreteDataReductionProxySettingsTest<
DataReductionProxySettings> {
@@ -423,6 +425,7 @@ TEST_F(DataReductionProxySettingsTest, CheckInitMetricsWhenNotAllowed) {
new net::TestURLRequestContextGetter(base::MessageLoopProxy::current());
settings_->InitDataReductionProxySettings(
&pref_service_,
+ scoped_ptr<DataReductionProxyStatisticsPrefs>(),
request_context.get(),
&net_log_,
event_store_.get());
diff --git a/components/data_reduction_proxy/core/browser/data_reduction_proxy_statistics_prefs.cc b/components/data_reduction_proxy/core/browser/data_reduction_proxy_statistics_prefs.cc
index 93d2320..1b28d5d7 100644
--- a/components/data_reduction_proxy/core/browser/data_reduction_proxy_statistics_prefs.cc
+++ b/components/data_reduction_proxy/core/browser/data_reduction_proxy_statistics_prefs.cc
@@ -33,12 +33,8 @@ DataReductionProxyStatisticsPrefs::DataReductionProxyStatisticsPrefs(
}
DataReductionProxyStatisticsPrefs::~DataReductionProxyStatisticsPrefs() {
- // This object is created on UI thread, but destroyed on IO thread. So no
- // DCHECK on thread_checker_ here.
-}
-
-void DataReductionProxyStatisticsPrefs::ShutdownOnUIThread() {
DCHECK(thread_checker_.CalledOnValidThread());
+ WritePrefs();
pref_change_registrar_->RemoveAll();
weak_factory_.InvalidateWeakPtrs();
}
@@ -77,7 +73,7 @@ void DataReductionProxyStatisticsPrefs::Init() {
pref_change_registrar_->Init(pref_service_);
pref_change_registrar_->Add(prefs::kUpdateDailyReceivedContentLengths,
base::Bind(&DataReductionProxyStatisticsPrefs::OnUpdateContentLengths,
- weak_factory_.GetWeakPtr()));
+ GetWeakPtr()));
}
void DataReductionProxyStatisticsPrefs::OnUpdateContentLengths() {
@@ -155,7 +151,7 @@ void DataReductionProxyStatisticsPrefs::DelayedWritePrefs() {
task_runner_->PostDelayedTask(
FROM_HERE,
base::Bind(&DataReductionProxyStatisticsPrefs::WritePrefs,
- weak_factory_.GetWeakPtr()),
+ GetWeakPtr()),
delay_);
delayed_task_posted_ = true;
@@ -186,4 +182,9 @@ int64 DataReductionProxyStatisticsPrefs::GetListPrefInt64Value(
return value;
}
+base::WeakPtr<DataReductionProxyStatisticsPrefs>
+DataReductionProxyStatisticsPrefs::GetWeakPtr() {
+ return weak_factory_.GetWeakPtr();
+}
+
} // namespace data_reduction_proxy
diff --git a/components/data_reduction_proxy/core/browser/data_reduction_proxy_statistics_prefs.h b/components/data_reduction_proxy/core/browser/data_reduction_proxy_statistics_prefs.h
index a7e48a6..3aea4f4 100644
--- a/components/data_reduction_proxy/core/browser/data_reduction_proxy_statistics_prefs.h
+++ b/components/data_reduction_proxy/core/browser/data_reduction_proxy_statistics_prefs.h
@@ -48,8 +48,6 @@ public:
// |list_pref_map_|.
void Init();
- void ShutdownOnUIThread();
-
void OnUpdateContentLengths();
// Gets the int64 pref at |pref_path| from the |DataReductionProxyPrefMap|.
@@ -66,6 +64,8 @@ public:
// |DataReductionProxyListPrefMap| to |pref_service|.
void WritePrefs();
+ base::WeakPtr<DataReductionProxyStatisticsPrefs> GetWeakPtr();
+
private:
typedef std::map<const char*, int64> DataReductionProxyPrefMap;
typedef base::ScopedPtrHashMap<const char*, base::ListValue>