Commit 794e7d9b authored by ryansturm's avatar ryansturm Committed by Commit bot

Adding a previews object, PreviewsDataSavings

This object hooks into existing data saver code to track savings based
on host, data used, and original size of the data.

BUG=615565

Review-Url: https://codereview.chromium.org/2257533003
Cr-Commit-Position: refs/heads/master@{#417359}
parent 8b85c43f
...@@ -43,7 +43,7 @@ class PerSiteDataUsage; ...@@ -43,7 +43,7 @@ class PerSiteDataUsage;
// |delay| amount of time. If |delay| is zero, the delayed pref service writes // |delay| amount of time. If |delay| is zero, the delayed pref service writes
// directly to the PrefService and does not store the prefs in memory. All // directly to the PrefService and does not store the prefs in memory. All
// prefs must be stored and read on the UI thread. // prefs must be stored and read on the UI thread.
class DataReductionProxyCompressionStats : public DataSavingsRecorder { class DataReductionProxyCompressionStats {
public: public:
typedef base::ScopedPtrHashMap<std::string, std::unique_ptr<PerSiteDataUsage>> typedef base::ScopedPtrHashMap<std::string, std::unique_ptr<PerSiteDataUsage>>
SiteUsageMap; SiteUsageMap;
...@@ -62,10 +62,12 @@ class DataReductionProxyCompressionStats : public DataSavingsRecorder { ...@@ -62,10 +62,12 @@ class DataReductionProxyCompressionStats : public DataSavingsRecorder {
const base::TimeDelta& delay); const base::TimeDelta& delay);
~DataReductionProxyCompressionStats(); ~DataReductionProxyCompressionStats();
// DataSavingsRecorder implementation: // Records detailed data usage broken down by connection type and domain.
// Assumes that the |data_used| has been recoreded by previous calls to
// UpdateContentLengths.
void UpdateDataSavings(const std::string& data_usage_host, void UpdateDataSavings(const std::string& data_usage_host,
int64_t data_used, int64_t data_used,
int64_t original_size) override; int64_t original_size);
// Records detailed data usage broken down by connection type and domain. Also // Records detailed data usage broken down by connection type and domain. Also
// records daily data savings statistics to prefs and reports data savings // records daily data savings statistics to prefs and reports data savings
......
...@@ -366,8 +366,15 @@ void DataReductionProxySettings::GetContentLengths( ...@@ -366,8 +366,15 @@ void DataReductionProxySettings::GetContentLengths(
days, original_content_length, received_content_length, last_update_time); days, original_content_length, received_content_length, last_update_time);
} }
bool DataReductionProxySettings::IsDataSaverEnabled() const { bool DataReductionProxySettings::UpdateDataSavings(
return IsDataReductionProxyEnabled(); const std::string& data_usage_host,
int64_t data_used,
int64_t original_size) {
if (!IsDataReductionProxyEnabled())
return false;
data_reduction_proxy_service_->compression_stats()->UpdateDataSavings(
data_usage_host, data_used, original_size);
return true;
} }
} // namespace data_reduction_proxy } // namespace data_reduction_proxy
...@@ -18,7 +18,7 @@ ...@@ -18,7 +18,7 @@
#include "base/threading/thread_checker.h" #include "base/threading/thread_checker.h"
#include "components/data_reduction_proxy/core/browser/data_reduction_proxy_metrics.h" #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_metrics.h"
#include "components/data_reduction_proxy/core/browser/data_reduction_proxy_service_observer.h" #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_service_observer.h"
#include "components/data_reduction_proxy/core/common/data_saver_status.h" #include "components/data_reduction_proxy/core/common/data_savings_recorder.h"
#include "components/prefs/pref_member.h" #include "components/prefs/pref_member.h"
#include "url/gurl.h" #include "url/gurl.h"
...@@ -71,7 +71,7 @@ enum DataReductionSettingsEnabledAction { ...@@ -71,7 +71,7 @@ enum DataReductionSettingsEnabledAction {
// This object lives on the UI thread and all of its methods are expected to // This object lives on the UI thread and all of its methods are expected to
// be called from there. // be called from there.
class DataReductionProxySettings : public DataReductionProxyServiceObserver, class DataReductionProxySettings : public DataReductionProxyServiceObserver,
public DataSaverStatus { public DataSavingsRecorder {
public: public:
typedef base::Callback<bool(const std::string&, const std::string&)> typedef base::Callback<bool(const std::string&, const std::string&)>
SyntheticFieldTrialRegistrationCallback; SyntheticFieldTrialRegistrationCallback;
...@@ -79,8 +79,10 @@ class DataReductionProxySettings : public DataReductionProxyServiceObserver, ...@@ -79,8 +79,10 @@ class DataReductionProxySettings : public DataReductionProxyServiceObserver,
DataReductionProxySettings(); DataReductionProxySettings();
virtual ~DataReductionProxySettings(); virtual ~DataReductionProxySettings();
// DataSaverStatus implementation: // DataSavingsRecorder implementation:
bool IsDataSaverEnabled() const override; bool UpdateDataSavings(const std::string& data_usage_host,
int64_t data_used,
int64_t original_size) override;
// Initializes the Data Reduction Proxy with the name of the preference that // Initializes the Data Reduction Proxy with the name of the preference that
// controls enabling it, profile prefs and a |DataReductionProxyIOData|. The // controls enabling it, profile prefs and a |DataReductionProxyIOData|. The
......
...@@ -64,17 +64,17 @@ TEST_F(DataReductionProxySettingsTest, TestIsProxyEnabledOrManaged) { ...@@ -64,17 +64,17 @@ TEST_F(DataReductionProxySettingsTest, TestIsProxyEnabledOrManaged) {
test_context_->config()->SetStateForTest(false, true); test_context_->config()->SetStateForTest(false, true);
EXPECT_FALSE(settings_->IsDataReductionProxyEnabled()); EXPECT_FALSE(settings_->IsDataReductionProxyEnabled());
EXPECT_FALSE(settings_->IsDataSaverEnabled()); EXPECT_FALSE(settings_->UpdateDataSavings(std::string(), 0, 0));
EXPECT_FALSE(settings_->IsDataReductionProxyManaged()); EXPECT_FALSE(settings_->IsDataReductionProxyManaged());
CheckOnPrefChange(true, true, false); CheckOnPrefChange(true, true, false);
EXPECT_TRUE(settings_->IsDataReductionProxyEnabled()); EXPECT_TRUE(settings_->IsDataReductionProxyEnabled());
EXPECT_TRUE(settings_->IsDataSaverEnabled()); EXPECT_TRUE(settings_->UpdateDataSavings(std::string(), 0, 0));
EXPECT_FALSE(settings_->IsDataReductionProxyManaged()); EXPECT_FALSE(settings_->IsDataReductionProxyManaged());
CheckOnPrefChange(true, true, true); CheckOnPrefChange(true, true, true);
EXPECT_TRUE(settings_->IsDataReductionProxyEnabled()); EXPECT_TRUE(settings_->IsDataReductionProxyEnabled());
EXPECT_TRUE(settings_->IsDataSaverEnabled()); EXPECT_TRUE(settings_->UpdateDataSavings(std::string(), 0, 0));
EXPECT_TRUE(settings_->IsDataReductionProxyManaged()); EXPECT_TRUE(settings_->IsDataReductionProxyManaged());
test_context_->RunUntilIdle(); test_context_->RunUntilIdle();
......
...@@ -29,7 +29,6 @@ template("common_tmpl") { ...@@ -29,7 +29,6 @@ template("common_tmpl") {
"data_reduction_proxy_switches.h", "data_reduction_proxy_switches.h",
"data_reduction_proxy_util.cc", "data_reduction_proxy_util.cc",
"data_reduction_proxy_util.h", "data_reduction_proxy_util.h",
"data_saver_status.h",
"data_savings_recorder.h", "data_savings_recorder.h",
"lofi_decider.h", "lofi_decider.h",
"lofi_ui_service.h", "lofi_ui_service.h",
......
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef COMPONENTS_DATA_REDUCTION_PROXY_CORE_COMMON_DATA_SAVER_STATUS_H_
#define COMPONENTS_DATA_REDUCTION_PROXY_CORE_COMMON_DATA_SAVER_STATUS_H_
namespace data_reduction_proxy {
// A class that reports the current state of data saver.
class DataSaverStatus {
public:
// Whether data saver is currently enabled.
virtual bool IsDataSaverEnabled() const = 0;
};
} // namespace data_reduction_proxy
#endif // COMPONENTS_DATA_REDUCTION_PROXY_CORE_COMMON_DATA_SAVER_STATUS_H_
...@@ -19,7 +19,8 @@ class DataSavingsRecorder { ...@@ -19,7 +19,8 @@ class DataSavingsRecorder {
// previous handling of URLRequests. Also records daily data savings // previous handling of URLRequests. Also records daily data savings
// statistics to prefs and reports data savings UMA. |data_used| and // statistics to prefs and reports data savings UMA. |data_used| and
// |original_size| are measured in bytes. // |original_size| are measured in bytes.
virtual void UpdateDataSavings(const std::string& data_usage_host, // Returns true if data savings was recorded.
virtual bool UpdateDataSavings(const std::string& data_usage_host,
int64_t data_used, int64_t data_used,
int64_t original_size) = 0; int64_t original_size) = 0;
}; };
......
...@@ -4,12 +4,15 @@ ...@@ -4,12 +4,15 @@
static_library("previews") { static_library("previews") {
sources = [ sources = [
"previews_data_savings.cc",
"previews_data_savings.h",
"previews_experiments.cc", "previews_experiments.cc",
"previews_experiments.h", "previews_experiments.h",
] ]
deps = [ deps = [
"//base", "//base",
"//components/data_reduction_proxy/core/common",
"//components/variations", "//components/variations",
] ]
} }
...@@ -17,12 +20,14 @@ static_library("previews") { ...@@ -17,12 +20,14 @@ static_library("previews") {
source_set("unit_tests") { source_set("unit_tests") {
testonly = true testonly = true
sources = [ sources = [
"previews_data_savings_unittest.cc",
"previews_experiments_unittest.cc", "previews_experiments_unittest.cc",
] ]
deps = [ deps = [
":previews", ":previews",
"//base", "//base",
"//components/data_reduction_proxy/core/common",
"//components/variations", "//components/variations",
"//testing/gtest", "//testing/gtest",
] ]
......
include_rules = [ include_rules = [
"+components/variations", "+components/variations",
"+components/data_reduction_proxy/core/common",
] ]
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/previews/previews_data_savings.h"
#include "base/logging.h"
#include "base/metrics/histogram_macros.h"
#include "components/data_reduction_proxy/core/common/data_savings_recorder.h"
namespace previews {
PreviewsDataSavings::PreviewsDataSavings(
data_reduction_proxy::DataSavingsRecorder* data_savings_recorder)
: data_savings_recorder_(data_savings_recorder) {
DCHECK(data_savings_recorder);
}
PreviewsDataSavings::~PreviewsDataSavings() {
DCHECK(thread_checker_.CalledOnValidThread());
}
void PreviewsDataSavings::RecordDataSavings(
const std::string& fully_qualified_domain_name,
int64_t data_used,
int64_t original_size) {
DCHECK(thread_checker_.CalledOnValidThread());
data_savings_recorder_->UpdateDataSavings(fully_qualified_domain_name,
data_used, original_size);
}
} // namespace previews
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROME_BROWSER_PREVIEWS_PREVIEWS_DATA_SAVINGS_H_
#define CHROME_BROWSER_PREVIEWS_PREVIEWS_DATA_SAVINGS_H_
#include <stdint.h>
#include <string>
#include "base/macros.h"
#include "base/threading/thread_checker.h"
namespace data_reduction_proxy {
class DataSavingsRecorder;
}
namespace previews {
// Provides an interface for previews to report data savings.
class PreviewsDataSavings {
public:
// Embedder must guarantee that |data_savings_recorder| and
// |data_reduction_proxy_settings| outlive this instance.
PreviewsDataSavings(
data_reduction_proxy::DataSavingsRecorder* data_savings_recorder);
~PreviewsDataSavings();
// Records the amount of data used by the preview, and the amount of data
// that would have been used if the original page had been loaded instead of
// the preview. |fully_qualified_domain_name| is the full host name to
// attribute the data savings to (e.g., codereview.chromium.org).
void RecordDataSavings(const std::string& fully_qualified_domain_name,
int64_t data_used,
int64_t original_size);
private:
// Owned by the embedder, must be valid for the lifetime of |this|.
// Provides a method to record data savings.
data_reduction_proxy::DataSavingsRecorder* data_savings_recorder_;
// Enforce usage on the UI thread.
base::ThreadChecker thread_checker_;
DISALLOW_COPY_AND_ASSIGN(PreviewsDataSavings);
};
} // namespace previews
#endif // CHROME_BROWSER_PREVIEWS_PREVIEWS_DATA_SAVINGS_H_
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/previews/previews_data_savings.h"
#include <stdint.h>
#include <memory>
#include <string>
#include "components/data_reduction_proxy/core/common/data_savings_recorder.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace previews {
namespace {
class TestDataSavingsRecorder
: public data_reduction_proxy::DataSavingsRecorder {
public:
TestDataSavingsRecorder()
: data_saver_enabled_(false), data_used_(0), original_size_(0) {}
~TestDataSavingsRecorder() {}
// data_reduction_proxy::DataSavingsRecorder implementation:
bool UpdateDataSavings(const std::string& host,
int64_t data_used,
int64_t original_size) override {
if (!data_saver_enabled_) {
return false;
}
last_host_ = host;
data_used_ += data_used;
original_size_ += original_size;
return true;
}
void set_data_saver_enabled(bool data_saver_enabled) {
data_saver_enabled_ = data_saver_enabled;
}
std::string last_host() const { return last_host_; }
int64_t data_used() const { return data_used_; }
int64_t original_size() const { return original_size_; }
private:
bool data_saver_enabled_;
std::string last_host_;
int64_t data_used_;
int64_t original_size_;
};
} // namespace
class PreviewsDataSavingsTest : public testing::Test {
public:
PreviewsDataSavingsTest()
: test_data_savings_recorder_(new TestDataSavingsRecorder()),
data_savings_(
new PreviewsDataSavings(test_data_savings_recorder_.get())) {}
~PreviewsDataSavingsTest() override {}
PreviewsDataSavings* data_savings() const { return data_savings_.get(); }
TestDataSavingsRecorder* test_data_savings_recorder() const {
return test_data_savings_recorder_.get();
}
private:
std::unique_ptr<TestDataSavingsRecorder> test_data_savings_recorder_;
std::unique_ptr<PreviewsDataSavings> data_savings_;
};
TEST_F(PreviewsDataSavingsTest, RecordDataSavings) {
int64_t original_size = 200;
int64_t data_used = 100;
std::string host = "host";
EXPECT_EQ(0, test_data_savings_recorder()->data_used());
EXPECT_EQ(0, test_data_savings_recorder()->original_size());
test_data_savings_recorder()->set_data_saver_enabled(false);
data_savings()->RecordDataSavings(host, data_used, original_size);
EXPECT_EQ(0, test_data_savings_recorder()->data_used());
EXPECT_EQ(0, test_data_savings_recorder()->original_size());
EXPECT_EQ(std::string(), test_data_savings_recorder()->last_host());
test_data_savings_recorder()->set_data_saver_enabled(true);
data_savings()->RecordDataSavings(host, data_used, original_size);
EXPECT_EQ(data_used, test_data_savings_recorder()->data_used());
EXPECT_EQ(original_size, test_data_savings_recorder()->original_size());
EXPECT_EQ(host, test_data_savings_recorder()->last_host());
}
} // namespace previews
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment