Commit 90ff2857 authored by Ryan Sturm's avatar Ryan Sturm Committed by Commit Bot

Adding per host data savings for offline previews

This tracks per host data savings for offline previews as well as
cleaning up the code somewhat.

Bug: 794642
Change-Id: I2660beeee2c6ef8f7939eff4861b78203dab63d6
Reviewed-on: https://chromium-review.googlesource.com/825362
Commit-Queue: Ryan Sturm <ryansturm@chromium.org>
Reviewed-by: default avatarTarun Bansal <tbansal@chromium.org>
Cr-Commit-Position: refs/heads/master@{#524194}
parent f836a6f5
...@@ -80,12 +80,12 @@ void OnLoFiResponseReceivedOnUI(content::WebContents* web_contents) { ...@@ -80,12 +80,12 @@ void OnLoFiResponseReceivedOnUI(content::WebContents* web_contents) {
web_contents, previews::PreviewsType::LOFI, web_contents, previews::PreviewsType::LOFI,
base::Time() /* previews_freshness */, true /* is_data_saver_user */, base::Time() /* previews_freshness */, true /* is_data_saver_user */,
false /* is_reload */, false /* is_reload */,
base::Bind(&AddPreviewNavigationToBlackListCallback, base::BindOnce(&AddPreviewNavigationToBlackListCallback,
web_contents->GetBrowserContext(), web_contents->GetBrowserContext(),
web_contents->GetController() web_contents->GetController()
.GetLastCommittedEntry() .GetLastCommittedEntry()
->GetRedirectChain()[0], ->GetRedirectChain()[0],
previews::PreviewsType::LOFI, page_id), previews::PreviewsType::LOFI, page_id),
previews_ui_service); previews_ui_service);
} }
......
...@@ -98,7 +98,7 @@ void InformPLMOfOptOut(content::WebContents* web_contents) { ...@@ -98,7 +98,7 @@ void InformPLMOfOptOut(content::WebContents* web_contents) {
PreviewsInfoBarDelegate::~PreviewsInfoBarDelegate() { PreviewsInfoBarDelegate::~PreviewsInfoBarDelegate() {
if (!on_dismiss_callback_.is_null()) if (!on_dismiss_callback_.is_null())
on_dismiss_callback_.Run(false); std::move(on_dismiss_callback_).Run(false);
RecordPreviewsInfoBarAction(previews_type_, infobar_dismissed_action_); RecordPreviewsInfoBarAction(previews_type_, infobar_dismissed_action_);
} }
...@@ -110,7 +110,7 @@ void PreviewsInfoBarDelegate::Create( ...@@ -110,7 +110,7 @@ void PreviewsInfoBarDelegate::Create(
base::Time previews_freshness, base::Time previews_freshness,
bool is_data_saver_user, bool is_data_saver_user,
bool is_reload, bool is_reload,
const OnDismissPreviewsInfobarCallback& on_dismiss_callback, OnDismissPreviewsInfobarCallback on_dismiss_callback,
previews::PreviewsUIService* previews_ui_service) { previews::PreviewsUIService* previews_ui_service) {
PreviewsInfoBarTabHelper* infobar_tab_helper = PreviewsInfoBarTabHelper* infobar_tab_helper =
PreviewsInfoBarTabHelper::FromWebContents(web_contents); PreviewsInfoBarTabHelper::FromWebContents(web_contents);
...@@ -126,7 +126,7 @@ void PreviewsInfoBarDelegate::Create( ...@@ -126,7 +126,7 @@ void PreviewsInfoBarDelegate::Create(
std::unique_ptr<PreviewsInfoBarDelegate> delegate(new PreviewsInfoBarDelegate( std::unique_ptr<PreviewsInfoBarDelegate> delegate(new PreviewsInfoBarDelegate(
infobar_tab_helper, previews_type, previews_freshness, is_data_saver_user, infobar_tab_helper, previews_type, previews_freshness, is_data_saver_user,
is_reload, on_dismiss_callback)); is_reload, std::move(on_dismiss_callback)));
#if defined(OS_ANDROID) #if defined(OS_ANDROID)
std::unique_ptr<infobars::InfoBar> infobar_ptr( std::unique_ptr<infobars::InfoBar> infobar_ptr(
...@@ -162,7 +162,7 @@ PreviewsInfoBarDelegate::PreviewsInfoBarDelegate( ...@@ -162,7 +162,7 @@ PreviewsInfoBarDelegate::PreviewsInfoBarDelegate(
base::Time previews_freshness, base::Time previews_freshness,
bool is_data_saver_user, bool is_data_saver_user,
bool is_reload, bool is_reload,
const OnDismissPreviewsInfobarCallback& on_dismiss_callback) OnDismissPreviewsInfobarCallback on_dismiss_callback)
: ConfirmInfoBarDelegate(), : ConfirmInfoBarDelegate(),
infobar_tab_helper_(infobar_tab_helper), infobar_tab_helper_(infobar_tab_helper),
previews_type_(previews_type), previews_type_(previews_type),
...@@ -172,7 +172,7 @@ PreviewsInfoBarDelegate::PreviewsInfoBarDelegate( ...@@ -172,7 +172,7 @@ PreviewsInfoBarDelegate::PreviewsInfoBarDelegate(
message_text_(l10n_util::GetStringUTF16( message_text_(l10n_util::GetStringUTF16(
is_data_saver_user ? IDS_PREVIEWS_INFOBAR_SAVED_DATA_TITLE is_data_saver_user ? IDS_PREVIEWS_INFOBAR_SAVED_DATA_TITLE
: IDS_PREVIEWS_INFOBAR_FASTER_PAGE_TITLE)), : IDS_PREVIEWS_INFOBAR_FASTER_PAGE_TITLE)),
on_dismiss_callback_(on_dismiss_callback) {} on_dismiss_callback_(std::move(on_dismiss_callback)) {}
infobars::InfoBarDelegate::InfoBarIdentifier infobars::InfoBarDelegate::InfoBarIdentifier
PreviewsInfoBarDelegate::GetIdentifier() const { PreviewsInfoBarDelegate::GetIdentifier() const {
...@@ -225,8 +225,7 @@ base::string16 PreviewsInfoBarDelegate::GetLinkText() const { ...@@ -225,8 +225,7 @@ base::string16 PreviewsInfoBarDelegate::GetLinkText() const {
bool PreviewsInfoBarDelegate::LinkClicked(WindowOpenDisposition disposition) { bool PreviewsInfoBarDelegate::LinkClicked(WindowOpenDisposition disposition) {
infobar_dismissed_action_ = INFOBAR_LOAD_ORIGINAL_CLICKED; infobar_dismissed_action_ = INFOBAR_LOAD_ORIGINAL_CLICKED;
if (!on_dismiss_callback_.is_null()) if (!on_dismiss_callback_.is_null())
on_dismiss_callback_.Run(true); std::move(on_dismiss_callback_).Run(true);
on_dismiss_callback_.Reset();
content::WebContents* web_contents = content::WebContents* web_contents =
InfoBarService::WebContentsFromInfoBar(infobar()); InfoBarService::WebContentsFromInfoBar(infobar());
......
...@@ -27,7 +27,8 @@ class PreviewsUIService; ...@@ -27,7 +27,8 @@ class PreviewsUIService;
// infobar. // infobar.
class PreviewsInfoBarDelegate : public ConfirmInfoBarDelegate { class PreviewsInfoBarDelegate : public ConfirmInfoBarDelegate {
public: public:
typedef base::Callback<void(bool opt_out)> OnDismissPreviewsInfobarCallback; typedef base::OnceCallback<void(bool opt_out)>
OnDismissPreviewsInfobarCallback;
// Actions on the previews infobar. This enum must remain synchronized with // Actions on the previews infobar. This enum must remain synchronized with
// the enum of the same name in metrics/histograms/histograms.xml. // the enum of the same name in metrics/histograms/histograms.xml.
...@@ -66,7 +67,7 @@ class PreviewsInfoBarDelegate : public ConfirmInfoBarDelegate { ...@@ -66,7 +67,7 @@ class PreviewsInfoBarDelegate : public ConfirmInfoBarDelegate {
bool is_reload, bool is_reload,
// TODO(ryansturm): Replace |on_dismiss_callback| with direct call to // TODO(ryansturm): Replace |on_dismiss_callback| with direct call to
// |previews_ui_service|. // |previews_ui_service|.
const OnDismissPreviewsInfobarCallback& on_dismiss_callback, OnDismissPreviewsInfobarCallback on_dismiss_callback,
previews::PreviewsUIService* previews_ui_service); previews::PreviewsUIService* previews_ui_service);
// ConfirmInfoBarDelegate overrides: // ConfirmInfoBarDelegate overrides:
...@@ -80,13 +81,12 @@ class PreviewsInfoBarDelegate : public ConfirmInfoBarDelegate { ...@@ -80,13 +81,12 @@ class PreviewsInfoBarDelegate : public ConfirmInfoBarDelegate {
static const void* OptOutEventKey(); static const void* OptOutEventKey();
private: private:
PreviewsInfoBarDelegate( PreviewsInfoBarDelegate(PreviewsInfoBarTabHelper* infobar_tab_helper,
PreviewsInfoBarTabHelper* infobar_tab_helper, previews::PreviewsType previews_type,
previews::PreviewsType previews_type, base::Time previews_freshness,
base::Time previews_freshness, bool is_data_saver_user,
bool is_data_saver_user, bool is_reload,
bool is_reload, OnDismissPreviewsInfobarCallback on_dismiss_callback);
const OnDismissPreviewsInfobarCallback& on_dismiss_callback);
// ConfirmInfoBarDelegate overrides: // ConfirmInfoBarDelegate overrides:
infobars::InfoBarDelegate::InfoBarIdentifier GetIdentifier() const override; infobars::InfoBarDelegate::InfoBarIdentifier GetIdentifier() const override;
......
...@@ -118,6 +118,11 @@ void PreviewsInfoBarTabHelper::DidFinishNavigation( ...@@ -118,6 +118,11 @@ void PreviewsInfoBarTabHelper::DidFinishNavigation(
bool data_saver_enabled = bool data_saver_enabled =
data_reduction_proxy_settings->IsDataReductionProxyEnabled(); data_reduction_proxy_settings->IsDataReductionProxyEnabled();
data_reduction_proxy_settings->data_reduction_proxy_service()
->UpdateDataUseForHost(0, uncached_size,
navigation_handle->GetRedirectChain()[0].host());
data_reduction_proxy_settings->data_reduction_proxy_service() data_reduction_proxy_settings->data_reduction_proxy_service()
->UpdateContentLengths(0, uncached_size, data_saver_enabled, ->UpdateContentLengths(0, uncached_size, data_saver_enabled,
data_reduction_proxy::HTTPS, data_reduction_proxy::HTTPS,
...@@ -128,10 +133,10 @@ void PreviewsInfoBarTabHelper::DidFinishNavigation( ...@@ -128,10 +133,10 @@ void PreviewsInfoBarTabHelper::DidFinishNavigation(
base::Time() /* previews_freshness */, base::Time() /* previews_freshness */,
data_reduction_proxy_settings && data_saver_enabled, data_reduction_proxy_settings && data_saver_enabled,
false /* is_reload */, false /* is_reload */,
base::Bind(&AddPreviewNavigationCallback, base::BindOnce(&AddPreviewNavigationCallback,
web_contents()->GetBrowserContext(), web_contents()->GetBrowserContext(),
navigation_handle->GetRedirectChain()[0], navigation_handle->GetRedirectChain()[0],
previews::PreviewsType::OFFLINE, page_id), previews::PreviewsType::OFFLINE, page_id),
previews_ui_service); previews_ui_service);
// Don't try to show other infobars if this is an offline preview. // Don't try to show other infobars if this is an offline preview.
return; return;
...@@ -148,10 +153,10 @@ void PreviewsInfoBarTabHelper::DidFinishNavigation( ...@@ -148,10 +153,10 @@ void PreviewsInfoBarTabHelper::DidFinishNavigation(
web_contents(), main_frame_preview, web_contents(), main_frame_preview,
base::Time() /* previews_freshness */, true /* is_data_saver_user */, base::Time() /* previews_freshness */, true /* is_data_saver_user */,
is_reload, is_reload,
base::Bind(&AddPreviewNavigationCallback, base::BindOnce(&AddPreviewNavigationCallback,
web_contents()->GetBrowserContext(), web_contents()->GetBrowserContext(),
navigation_handle->GetRedirectChain()[0], navigation_handle->GetRedirectChain()[0],
main_frame_preview, page_id), main_frame_preview, page_id),
previews_ui_service); previews_ui_service);
} }
} }
......
...@@ -23,6 +23,7 @@ ...@@ -23,6 +23,7 @@
#include "components/data_reduction_proxy/core/browser/data_reduction_proxy_test_utils.h" #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_test_utils.h"
#include "components/data_reduction_proxy/core/common/data_reduction_proxy_headers.h" #include "components/data_reduction_proxy/core/common/data_reduction_proxy_headers.h"
#include "components/data_reduction_proxy/core/common/data_reduction_proxy_pref_names.h" #include "components/data_reduction_proxy/core/common/data_reduction_proxy_pref_names.h"
#include "components/data_reduction_proxy/proto/data_store.pb.h"
#include "components/offline_pages/core/offline_page_item.h" #include "components/offline_pages/core/offline_page_item.h"
#include "components/offline_pages/core/request_header/offline_page_header.h" #include "components/offline_pages/core/request_header/offline_page_header.h"
#include "components/offline_pages/features/features.h" #include "components/offline_pages/features/features.h"
...@@ -136,10 +137,12 @@ class PreviewsInfoBarTabHelperUnitTest ...@@ -136,10 +137,12 @@ class PreviewsInfoBarTabHelperUnitTest
->SetNavigationData(test_handle_.get(), std::move(navigation_data)); ->SetNavigationData(test_handle_.get(), std::move(navigation_data));
} }
private: protected:
std::unique_ptr<content::NavigationHandle> test_handle_;
std::unique_ptr<data_reduction_proxy::DataReductionProxyTestContext> std::unique_ptr<data_reduction_proxy::DataReductionProxyTestContext>
drp_test_context_; drp_test_context_;
private:
std::unique_ptr<content::NavigationHandle> test_handle_;
}; };
TEST_F(PreviewsInfoBarTabHelperUnitTest, TEST_F(PreviewsInfoBarTabHelperUnitTest,
...@@ -238,6 +241,20 @@ TEST_F(PreviewsInfoBarTabHelperUnitTest, CreateOfflineInfoBar) { ...@@ -238,6 +241,20 @@ TEST_F(PreviewsInfoBarTabHelperUnitTest, CreateOfflineInfoBar) {
offline_pages::OfflinePageHeader header; offline_pages::OfflinePageHeader header;
offline_pages::OfflinePageTabHelper::FromWebContents(web_contents()) offline_pages::OfflinePageTabHelper::FromWebContents(web_contents())
->SetOfflinePage(item, header, true); ->SetOfflinePage(item, header, true);
auto* data_reduction_proxy_settings =
DataReductionProxyChromeSettingsFactory::GetForBrowserContext(
web_contents()->GetBrowserContext());
EXPECT_TRUE(data_reduction_proxy_settings->data_reduction_proxy_service()
->compression_stats()
->DataUsageMapForTesting()
.empty());
drp_test_context_->pref_service()->SetBoolean("data_usage_reporting.enabled",
true);
base::RunLoop().RunUntilIdle();
CallDidFinishNavigation(); CallDidFinishNavigation();
InfoBarService* infobar_service = InfoBarService* infobar_service =
...@@ -249,9 +266,6 @@ TEST_F(PreviewsInfoBarTabHelperUnitTest, CreateOfflineInfoBar) { ...@@ -249,9 +266,6 @@ TEST_F(PreviewsInfoBarTabHelperUnitTest, CreateOfflineInfoBar) {
content::WebContentsTester::For(web_contents()) content::WebContentsTester::For(web_contents())
->NavigateAndCommit(GURL(kTestUrl)); ->NavigateAndCommit(GURL(kTestUrl));
auto* data_reduction_proxy_settings =
DataReductionProxyChromeSettingsFactory::GetForBrowserContext(
web_contents()->GetBrowserContext());
EXPECT_EQ(0, data_reduction_proxy_settings->data_reduction_proxy_service() EXPECT_EQ(0, data_reduction_proxy_settings->data_reduction_proxy_service()
->compression_stats() ->compression_stats()
->GetHttpReceivedContentLength()); ->GetHttpReceivedContentLength());
...@@ -263,6 +277,24 @@ TEST_F(PreviewsInfoBarTabHelperUnitTest, CreateOfflineInfoBar) { ...@@ -263,6 +277,24 @@ TEST_F(PreviewsInfoBarTabHelperUnitTest, CreateOfflineInfoBar) {
->compression_stats() ->compression_stats()
->GetHttpOriginalContentLength()); ->GetHttpOriginalContentLength());
EXPECT_FALSE(data_reduction_proxy_settings->data_reduction_proxy_service()
->compression_stats()
->DataUsageMapForTesting()
.empty());
// Normalize the host name.
std::string host = GURL(kTestUrl).host();
size_t pos = host.find("://");
if (pos != std::string::npos)
host = host.substr(pos + 3);
EXPECT_EQ(expected_file_size,
data_reduction_proxy_settings->data_reduction_proxy_service()
->compression_stats()
->DataUsageMapForTesting()
.find(host)
->second->original_size());
EXPECT_FALSE(infobar_tab_helper->displayed_preview_infobar()); EXPECT_FALSE(infobar_tab_helper->displayed_preview_infobar());
} }
#endif // BUILDFLAG(ENABLE_OFFLINE_PAGES) #endif // BUILDFLAG(ENABLE_OFFLINE_PAGES)
...@@ -137,6 +137,12 @@ class DataReductionProxyCompressionStats { ...@@ -137,6 +137,12 @@ class DataReductionProxyCompressionStats {
// persists data usage to memory when pref is disabled. // persists data usage to memory when pref is disabled.
void SetDataUsageReportingEnabled(bool enabled); void SetDataUsageReportingEnabled(bool enabled);
// Returns |data_usage_map_|.
const DataReductionProxyCompressionStats::SiteUsageMap&
DataUsageMapForTesting() const {
return data_usage_map_;
}
private: private:
// Enum to track the state of loading data usage from storage. // Enum to track the state of loading data usage from storage.
enum CurrentDataUsageLoadStatus { NOT_LOADED = 0, LOADING = 1, LOADED = 2 }; enum CurrentDataUsageLoadStatus { NOT_LOADED = 0, LOADING = 1, LOADED = 2 };
......
...@@ -142,10 +142,6 @@ class DataReductionProxyCompressionStatsTest : public testing::Test { ...@@ -142,10 +142,6 @@ class DataReductionProxyCompressionStatsTest : public testing::Test {
now_delta_ += base::TimeDelta::FromHours(hours); now_delta_ += base::TimeDelta::FromHours(hours);
} }
DataReductionProxyCompressionStats::SiteUsageMap* DataUsageMap() {
return &compression_stats_->data_usage_map_;
}
void SetUpPrefs() { void SetUpPrefs() {
CreatePrefList(prefs::kDailyHttpOriginalContentLength); CreatePrefList(prefs::kDailyHttpOriginalContentLength);
CreatePrefList(prefs::kDailyHttpReceivedContentLength); CreatePrefList(prefs::kDailyHttpReceivedContentLength);
...@@ -1298,7 +1294,7 @@ TEST_F(DataReductionProxyCompressionStatsTest, DeleteBrowsingHistory) { ...@@ -1298,7 +1294,7 @@ TEST_F(DataReductionProxyCompressionStatsTest, DeleteBrowsingHistory) {
DeleteBrowsingHistory(now, now); DeleteBrowsingHistory(now, now);
base::RunLoop().RunUntilIdle(); base::RunLoop().RunUntilIdle();
ASSERT_TRUE(DataUsageMap()->empty()); ASSERT_TRUE(compression_stats()->DataUsageMapForTesting().empty());
auto expected_data_usage = auto expected_data_usage =
base::MakeUnique<std::vector<data_reduction_proxy::DataUsageBucket>>( base::MakeUnique<std::vector<data_reduction_proxy::DataUsageBucket>>(
......
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