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) {
web_contents, previews::PreviewsType::LOFI,
base::Time() /* previews_freshness */, true /* is_data_saver_user */,
false /* is_reload */,
base::Bind(&AddPreviewNavigationToBlackListCallback,
web_contents->GetBrowserContext(),
web_contents->GetController()
.GetLastCommittedEntry()
->GetRedirectChain()[0],
previews::PreviewsType::LOFI, page_id),
base::BindOnce(&AddPreviewNavigationToBlackListCallback,
web_contents->GetBrowserContext(),
web_contents->GetController()
.GetLastCommittedEntry()
->GetRedirectChain()[0],
previews::PreviewsType::LOFI, page_id),
previews_ui_service);
}
......
......@@ -98,7 +98,7 @@ void InformPLMOfOptOut(content::WebContents* web_contents) {
PreviewsInfoBarDelegate::~PreviewsInfoBarDelegate() {
if (!on_dismiss_callback_.is_null())
on_dismiss_callback_.Run(false);
std::move(on_dismiss_callback_).Run(false);
RecordPreviewsInfoBarAction(previews_type_, infobar_dismissed_action_);
}
......@@ -110,7 +110,7 @@ void PreviewsInfoBarDelegate::Create(
base::Time previews_freshness,
bool is_data_saver_user,
bool is_reload,
const OnDismissPreviewsInfobarCallback& on_dismiss_callback,
OnDismissPreviewsInfobarCallback on_dismiss_callback,
previews::PreviewsUIService* previews_ui_service) {
PreviewsInfoBarTabHelper* infobar_tab_helper =
PreviewsInfoBarTabHelper::FromWebContents(web_contents);
......@@ -126,7 +126,7 @@ void PreviewsInfoBarDelegate::Create(
std::unique_ptr<PreviewsInfoBarDelegate> delegate(new PreviewsInfoBarDelegate(
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)
std::unique_ptr<infobars::InfoBar> infobar_ptr(
......@@ -162,7 +162,7 @@ PreviewsInfoBarDelegate::PreviewsInfoBarDelegate(
base::Time previews_freshness,
bool is_data_saver_user,
bool is_reload,
const OnDismissPreviewsInfobarCallback& on_dismiss_callback)
OnDismissPreviewsInfobarCallback on_dismiss_callback)
: ConfirmInfoBarDelegate(),
infobar_tab_helper_(infobar_tab_helper),
previews_type_(previews_type),
......@@ -172,7 +172,7 @@ PreviewsInfoBarDelegate::PreviewsInfoBarDelegate(
message_text_(l10n_util::GetStringUTF16(
is_data_saver_user ? IDS_PREVIEWS_INFOBAR_SAVED_DATA_TITLE
: IDS_PREVIEWS_INFOBAR_FASTER_PAGE_TITLE)),
on_dismiss_callback_(on_dismiss_callback) {}
on_dismiss_callback_(std::move(on_dismiss_callback)) {}
infobars::InfoBarDelegate::InfoBarIdentifier
PreviewsInfoBarDelegate::GetIdentifier() const {
......@@ -225,8 +225,7 @@ base::string16 PreviewsInfoBarDelegate::GetLinkText() const {
bool PreviewsInfoBarDelegate::LinkClicked(WindowOpenDisposition disposition) {
infobar_dismissed_action_ = INFOBAR_LOAD_ORIGINAL_CLICKED;
if (!on_dismiss_callback_.is_null())
on_dismiss_callback_.Run(true);
on_dismiss_callback_.Reset();
std::move(on_dismiss_callback_).Run(true);
content::WebContents* web_contents =
InfoBarService::WebContentsFromInfoBar(infobar());
......
......@@ -27,7 +27,8 @@ class PreviewsUIService;
// infobar.
class PreviewsInfoBarDelegate : public ConfirmInfoBarDelegate {
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
// the enum of the same name in metrics/histograms/histograms.xml.
......@@ -66,7 +67,7 @@ class PreviewsInfoBarDelegate : public ConfirmInfoBarDelegate {
bool is_reload,
// TODO(ryansturm): Replace |on_dismiss_callback| with direct call to
// |previews_ui_service|.
const OnDismissPreviewsInfobarCallback& on_dismiss_callback,
OnDismissPreviewsInfobarCallback on_dismiss_callback,
previews::PreviewsUIService* previews_ui_service);
// ConfirmInfoBarDelegate overrides:
......@@ -80,13 +81,12 @@ class PreviewsInfoBarDelegate : public ConfirmInfoBarDelegate {
static const void* OptOutEventKey();
private:
PreviewsInfoBarDelegate(
PreviewsInfoBarTabHelper* infobar_tab_helper,
previews::PreviewsType previews_type,
base::Time previews_freshness,
bool is_data_saver_user,
bool is_reload,
const OnDismissPreviewsInfobarCallback& on_dismiss_callback);
PreviewsInfoBarDelegate(PreviewsInfoBarTabHelper* infobar_tab_helper,
previews::PreviewsType previews_type,
base::Time previews_freshness,
bool is_data_saver_user,
bool is_reload,
OnDismissPreviewsInfobarCallback on_dismiss_callback);
// ConfirmInfoBarDelegate overrides:
infobars::InfoBarDelegate::InfoBarIdentifier GetIdentifier() const override;
......
......@@ -118,6 +118,11 @@ void PreviewsInfoBarTabHelper::DidFinishNavigation(
bool data_saver_enabled =
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()
->UpdateContentLengths(0, uncached_size, data_saver_enabled,
data_reduction_proxy::HTTPS,
......@@ -128,10 +133,10 @@ void PreviewsInfoBarTabHelper::DidFinishNavigation(
base::Time() /* previews_freshness */,
data_reduction_proxy_settings && data_saver_enabled,
false /* is_reload */,
base::Bind(&AddPreviewNavigationCallback,
web_contents()->GetBrowserContext(),
navigation_handle->GetRedirectChain()[0],
previews::PreviewsType::OFFLINE, page_id),
base::BindOnce(&AddPreviewNavigationCallback,
web_contents()->GetBrowserContext(),
navigation_handle->GetRedirectChain()[0],
previews::PreviewsType::OFFLINE, page_id),
previews_ui_service);
// Don't try to show other infobars if this is an offline preview.
return;
......@@ -148,10 +153,10 @@ void PreviewsInfoBarTabHelper::DidFinishNavigation(
web_contents(), main_frame_preview,
base::Time() /* previews_freshness */, true /* is_data_saver_user */,
is_reload,
base::Bind(&AddPreviewNavigationCallback,
web_contents()->GetBrowserContext(),
navigation_handle->GetRedirectChain()[0],
main_frame_preview, page_id),
base::BindOnce(&AddPreviewNavigationCallback,
web_contents()->GetBrowserContext(),
navigation_handle->GetRedirectChain()[0],
main_frame_preview, page_id),
previews_ui_service);
}
}
......
......@@ -23,6 +23,7 @@
#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_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/request_header/offline_page_header.h"
#include "components/offline_pages/features/features.h"
......@@ -136,10 +137,12 @@ class PreviewsInfoBarTabHelperUnitTest
->SetNavigationData(test_handle_.get(), std::move(navigation_data));
}
private:
std::unique_ptr<content::NavigationHandle> test_handle_;
protected:
std::unique_ptr<data_reduction_proxy::DataReductionProxyTestContext>
drp_test_context_;
private:
std::unique_ptr<content::NavigationHandle> test_handle_;
};
TEST_F(PreviewsInfoBarTabHelperUnitTest,
......@@ -238,6 +241,20 @@ TEST_F(PreviewsInfoBarTabHelperUnitTest, CreateOfflineInfoBar) {
offline_pages::OfflinePageHeader header;
offline_pages::OfflinePageTabHelper::FromWebContents(web_contents())
->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();
InfoBarService* infobar_service =
......@@ -249,9 +266,6 @@ TEST_F(PreviewsInfoBarTabHelperUnitTest, CreateOfflineInfoBar) {
content::WebContentsTester::For(web_contents())
->NavigateAndCommit(GURL(kTestUrl));
auto* data_reduction_proxy_settings =
DataReductionProxyChromeSettingsFactory::GetForBrowserContext(
web_contents()->GetBrowserContext());
EXPECT_EQ(0, data_reduction_proxy_settings->data_reduction_proxy_service()
->compression_stats()
->GetHttpReceivedContentLength());
......@@ -263,6 +277,24 @@ TEST_F(PreviewsInfoBarTabHelperUnitTest, CreateOfflineInfoBar) {
->compression_stats()
->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());
}
#endif // BUILDFLAG(ENABLE_OFFLINE_PAGES)
......@@ -137,6 +137,12 @@ class DataReductionProxyCompressionStats {
// persists data usage to memory when pref is disabled.
void SetDataUsageReportingEnabled(bool enabled);
// Returns |data_usage_map_|.
const DataReductionProxyCompressionStats::SiteUsageMap&
DataUsageMapForTesting() const {
return data_usage_map_;
}
private:
// Enum to track the state of loading data usage from storage.
enum CurrentDataUsageLoadStatus { NOT_LOADED = 0, LOADING = 1, LOADED = 2 };
......
......@@ -142,10 +142,6 @@ class DataReductionProxyCompressionStatsTest : public testing::Test {
now_delta_ += base::TimeDelta::FromHours(hours);
}
DataReductionProxyCompressionStats::SiteUsageMap* DataUsageMap() {
return &compression_stats_->data_usage_map_;
}
void SetUpPrefs() {
CreatePrefList(prefs::kDailyHttpOriginalContentLength);
CreatePrefList(prefs::kDailyHttpReceivedContentLength);
......@@ -1298,7 +1294,7 @@ TEST_F(DataReductionProxyCompressionStatsTest, DeleteBrowsingHistory) {
DeleteBrowsingHistory(now, now);
base::RunLoop().RunUntilIdle();
ASSERT_TRUE(DataUsageMap()->empty());
ASSERT_TRUE(compression_stats()->DataUsageMapForTesting().empty());
auto expected_data_usage =
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