Commit 75313498 authored by Tarun Bansal's avatar Tarun Bansal Committed by Commit Bot

Add save data boolean to previews UKM

A boolean value is set to true if the save data feature
is enabled in Chrome at the time of page commit.

Bug: 827608
Change-Id: I2cfa207694bb5aefe5bcb756fe9443248415e4d4
Reviewed-on: https://chromium-review.googlesource.com/1033821Reviewed-by: default avatarRobert Kaplow <rkaplow@chromium.org>
Reviewed-by: default avatarRyan Sturm <ryansturm@chromium.org>
Commit-Queue: Tarun Bansal <tbansal@chromium.org>
Cr-Commit-Position: refs/heads/master@{#554813}
parent 2ec39745
......@@ -8,14 +8,19 @@
#include "base/time/time.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/loader/chrome_navigation_data.h"
#include "chrome/browser/net/spdyproxy/data_reduction_proxy_chrome_settings.h"
#include "chrome/browser/net/spdyproxy/data_reduction_proxy_chrome_settings_factory.h"
#include "chrome/browser/page_load_metrics/page_load_metrics_observer.h"
#include "chrome/browser/page_load_metrics/page_load_metrics_util.h"
#include "chrome/browser/previews/previews_infobar_delegate.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/common/page_load_metrics/page_load_timing.h"
#include "components/data_reduction_proxy/core/browser/data_reduction_proxy_data.h"
#include "components/data_reduction_proxy/core/browser/data_reduction_proxy_settings.h"
#include "components/previews/content/previews_content_util.h"
#include "components/ukm/ukm_source.h"
#include "content/public/browser/navigation_handle.h"
#include "content/public/browser/web_contents.h"
#include "content/public/common/previews_state.h"
#include "services/metrics/public/cpp/ukm_builders.h"
#include "services/metrics/public/cpp/ukm_recorder.h"
......@@ -30,6 +35,9 @@ page_load_metrics::PageLoadMetricsObserver::ObservePolicy
PreviewsUKMObserver::OnCommit(content::NavigationHandle* navigation_handle,
ukm::SourceId source_id) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
save_data_enabled_ = IsDataSaverEnabled(navigation_handle);
// As documented in content/public/browser/navigation_handle.h, this
// NavigationData is a clone of the NavigationData instance returned from
// ResourceDispatcherHostDelegate::GetNavigationData during commit.
......@@ -91,8 +99,10 @@ void PreviewsUKMObserver::RecordPreviewsTypes(
const page_load_metrics::PageLoadExtraInfo& info) {
// Only record previews types when they are active.
if (!server_lofi_seen_ && !client_lofi_seen_ && !lite_page_seen_ &&
!noscript_seen_ && !origin_opt_out_occurred_)
!noscript_seen_ && !origin_opt_out_occurred_ && !save_data_enabled_) {
return;
}
ukm::builders::Previews builder(info.source_id);
if (server_lofi_seen_)
builder.Setserver_lofi(1);
......@@ -106,6 +116,8 @@ void PreviewsUKMObserver::RecordPreviewsTypes(
builder.Setopt_out(1);
if (origin_opt_out_occurred_)
builder.Setorigin_opt_out(1);
if (save_data_enabled_)
builder.Setsave_data_enabled(1);
builder.Record(ukm::UkmRecorder::Get());
}
......@@ -131,4 +143,21 @@ void PreviewsUKMObserver::OnEventOccurred(const void* const event_key) {
opt_out_occurred_ = true;
}
bool PreviewsUKMObserver::IsDataSaverEnabled(
content::NavigationHandle* navigation_handle) const {
Profile* profile = Profile::FromBrowserContext(
navigation_handle->GetWebContents()->GetBrowserContext());
data_reduction_proxy::DataReductionProxySettings*
data_reduction_proxy_settings =
DataReductionProxyChromeSettingsFactory::GetForBrowserContext(
profile);
if (!data_reduction_proxy_settings) {
DCHECK(profile->IsOffTheRecord());
return false;
}
return data_reduction_proxy_settings->IsDataReductionProxyEnabled();
}
} // namespace previews
......@@ -37,6 +37,12 @@ class PreviewsUKMObserver : public page_load_metrics::PageLoadMetricsObserver {
extra_request_complete_info) override;
void OnEventOccurred(const void* const event_key) override;
protected:
// Returns true if data saver feature is enabled in Chrome. Virtualized for
// testing.
virtual bool IsDataSaverEnabled(
content::NavigationHandle* navigation_handle) const;
private:
void RecordPreviewsTypes(const page_load_metrics::PageLoadExtraInfo& info);
......@@ -46,6 +52,7 @@ class PreviewsUKMObserver : public page_load_metrics::PageLoadMetricsObserver {
bool noscript_seen_ = false;
bool opt_out_occurred_ = false;
bool origin_opt_out_occurred_ = false;
bool save_data_enabled_ = false;
SEQUENCE_CHECKER(sequence_checker_);
......
......@@ -23,6 +23,10 @@
#include "content/public/test/web_contents_tester.h"
#include "services/metrics/public/cpp/ukm_builders.h"
namespace content {
class NavigationHandle;
}
namespace previews {
namespace {
......@@ -49,12 +53,14 @@ class TestPreviewsUKMObserver : public PreviewsUKMObserver {
bool data_reduction_proxy_used,
bool lite_page_received,
bool noscript_on,
bool origin_opt_out_received)
bool origin_opt_out_received,
bool save_data_enabled)
: web_contents_(web_contents),
data_reduction_proxy_used_(data_reduction_proxy_used),
lite_page_received_(lite_page_received),
noscript_on_(noscript_on),
origin_opt_out_received_(origin_opt_out_received) {}
origin_opt_out_received_(origin_opt_out_received),
save_data_enabled_(save_data_enabled) {}
~TestPreviewsUKMObserver() override {}
......@@ -95,11 +101,17 @@ class TestPreviewsUKMObserver : public PreviewsUKMObserver {
}
private:
bool IsDataSaverEnabled(
content::NavigationHandle* navigation_handle) const override {
return save_data_enabled_;
}
content::WebContents* web_contents_;
bool data_reduction_proxy_used_;
bool lite_page_received_;
bool noscript_on_;
bool origin_opt_out_received_;
const bool save_data_enabled_;
DISALLOW_COPY_AND_ASSIGN(TestPreviewsUKMObserver);
};
......@@ -113,11 +125,13 @@ class PreviewsUKMObserverTest
void RunTest(bool data_reduction_proxy_used,
bool lite_page_received,
bool noscript_on,
bool origin_opt_out) {
bool origin_opt_out,
bool save_data_enabled) {
data_reduction_proxy_used_ = data_reduction_proxy_used;
lite_page_received_ = lite_page_received;
noscript_on_ = noscript_on;
origin_opt_out_ = origin_opt_out;
save_data_enabled_ = save_data_enabled;
NavigateAndCommit(GURL(kDefaultTestUrl));
}
......@@ -126,11 +140,13 @@ class PreviewsUKMObserverTest
bool lite_page_expected,
bool noscript_expected,
bool opt_out_expected,
bool origin_opt_out_expected) {
bool origin_opt_out_expected,
bool save_data_enabled_expected) {
using UkmEntry = ukm::builders::Previews;
auto entries = test_ukm_recorder().GetEntriesByName(UkmEntry::kEntryName);
if (!server_lofi_expected && !client_lofi_expected && !lite_page_expected &&
!noscript_expected && !opt_out_expected && !origin_opt_out_expected) {
!noscript_expected && !opt_out_expected && !origin_opt_out_expected &&
!save_data_enabled_expected) {
EXPECT_EQ(0u, entries.size());
return;
}
......@@ -150,6 +166,9 @@ class PreviewsUKMObserverTest
EXPECT_EQ(origin_opt_out_expected,
test_ukm_recorder().EntryHasMetric(
entry, UkmEntry::korigin_opt_outName));
EXPECT_EQ(save_data_enabled_expected,
test_ukm_recorder().EntryHasMetric(
entry, UkmEntry::ksave_data_enabledName));
}
}
......@@ -157,7 +176,7 @@ class PreviewsUKMObserverTest
void RegisterObservers(page_load_metrics::PageLoadTracker* tracker) override {
tracker->AddObserver(std::make_unique<TestPreviewsUKMObserver>(
web_contents(), data_reduction_proxy_used_, lite_page_received_,
noscript_on_, origin_opt_out_));
noscript_on_, origin_opt_out_, save_data_enabled_));
// Data is only added to the first navigation after RunTest().
data_reduction_proxy_used_ = false;
lite_page_received_ = false;
......@@ -170,50 +189,58 @@ class PreviewsUKMObserverTest
bool lite_page_received_ = false;
bool noscript_on_ = false;
bool origin_opt_out_ = false;
bool save_data_enabled_ = false;
DISALLOW_COPY_AND_ASSIGN(PreviewsUKMObserverTest);
};
TEST_F(PreviewsUKMObserverTest, NoPreviewSeen) {
RunTest(false /* data_reduction_proxy_used */, false /* lite_page_received */,
false /* noscript_on */, false /* origin_opt_out */);
false /* noscript_on */, false /* origin_opt_out */,
false /* save_data_enabled */);
NavigateToUntrackedUrl();
ValidateUKM(false /* server_lofi_expected */,
false /* client_lofi_expected */, false /* lite_page_expected */,
false /* noscript_expected */, false /* opt_out_expected */,
false /* origin_opt_out_expected */);
false /* origin_opt_out_expected */,
false /* save_data_enabled_expected */);
}
TEST_F(PreviewsUKMObserverTest, UntrackedPreviewTypeOptOut) {
RunTest(false /* data_reduction_proxy_used */, false /* lite_page_received */,
false /* noscript_on */, false /* origin_opt_out */);
false /* noscript_on */, false /* origin_opt_out */,
false /* save_data_enabled */);
observer()->BroadcastEventToObservers(
PreviewsInfoBarDelegate::OptOutEventKey());
NavigateToUntrackedUrl();
// Opt out should not be added sicne we don't track this type.
// Opt out should not be added since we don't track this type.
ValidateUKM(false /* server_lofi_expected */,
false /* client_lofi_expected */, false /* lite_page_expected */,
false /* noscript_expected */, false /* opt_out_expected */,
false /* origin_opt_out_expected */);
false /* origin_opt_out_expected */,
false /* save_data_enabled_expected */);
}
TEST_F(PreviewsUKMObserverTest, LitePageSeen) {
RunTest(true /* data_reduction_proxy_used */, true /* lite_page_received */,
false /* noscript_on */, false /* origin_opt_out */);
false /* noscript_on */, false /* origin_opt_out */,
false /* save_data_enabled */);
NavigateToUntrackedUrl();
ValidateUKM(false /* server_lofi_expected */,
false /* client_lofi_expected */, true /* lite_page_expected */,
false /* noscript_expected */, false /* opt_out_expected */,
false /* origin_opt_out_expected */);
false /* origin_opt_out_expected */,
false /* save_data_enabled_expected */);
}
TEST_F(PreviewsUKMObserverTest, LitePageOptOut) {
RunTest(true /* data_reduction_proxy_used */, true /* lite_page_received */,
false /* noscript_on */, false /* origin_opt_out */);
false /* noscript_on */, false /* origin_opt_out */,
false /* save_data_enabled */);
observer()->BroadcastEventToObservers(
PreviewsInfoBarDelegate::OptOutEventKey());
......@@ -222,24 +249,28 @@ TEST_F(PreviewsUKMObserverTest, LitePageOptOut) {
ValidateUKM(false /* server_lofi_expected */,
false /* client_lofi_expected */, true /* lite_page_expected */,
false /* noscript_expected */, true /* opt_out_expected */,
false /* origin_opt_out_expected */);
false /* origin_opt_out_expected */,
false /* save_data_enabled_expected */);
}
TEST_F(PreviewsUKMObserverTest, NoScriptSeen) {
RunTest(false /* data_reduction_proxy_used */, false /* lite_page_received */,
true /* noscript_on */, false /* origin_opt_out */);
true /* noscript_on */, false /* origin_opt_out */,
false /* save_data_enabled */);
NavigateToUntrackedUrl();
ValidateUKM(false /* server_lofi_expected */,
false /* client_lofi_expected */, false /* lite_page_expected */,
true /* noscript_expected */, false /* opt_out_expected */,
false /* origin_opt_out_expected */);
false /* origin_opt_out_expected */,
false /* save_data_enabled_expected */);
}
TEST_F(PreviewsUKMObserverTest, NoScriptOptOut) {
RunTest(false /* data_reduction_proxy_used */, false /* lite_page_received */,
true /* noscript_on */, false /* origin_opt_out */);
true /* noscript_on */, false /* origin_opt_out */,
false /* save_data_enabled */);
observer()->BroadcastEventToObservers(
PreviewsInfoBarDelegate::OptOutEventKey());
......@@ -248,12 +279,14 @@ TEST_F(PreviewsUKMObserverTest, NoScriptOptOut) {
ValidateUKM(false /* server_lofi_expected */,
false /* client_lofi_expected */, false /* lite_page_expected */,
true /* noscript_expected */, true /* opt_out_expected */,
false /* origin_opt_out_expected */);
false /* origin_opt_out_expected */,
false /* save_data_enabled_expected */);
}
TEST_F(PreviewsUKMObserverTest, ClientLoFiSeen) {
RunTest(false /* data_reduction_proxy_used */, false /* lite_page_received */,
false /* noscript_on */, false /* origin_opt_out */);
false /* noscript_on */, false /* origin_opt_out */,
false /* save_data_enabled */);
std::unique_ptr<data_reduction_proxy::DataReductionProxyData> data =
std::make_unique<data_reduction_proxy::DataReductionProxyData>();
......@@ -283,13 +316,14 @@ TEST_F(PreviewsUKMObserverTest, ClientLoFiSeen) {
ValidateUKM(false /* server_lofi_expected */, true /* client_lofi_expected */,
false /* lite_page_expected */, false /* noscript_expected */,
false /* opt_out_expected */,
false /* origin_opt_out_expected */);
false /* opt_out_expected */, false /* origin_opt_out_expected */,
false /* save_data_enabled_expected */);
}
TEST_F(PreviewsUKMObserverTest, ClientLoFiOptOut) {
RunTest(false /* data_reduction_proxy_used */, false /* lite_page_received */,
false /* noscript_on */, false /* origin_opt_out */);
false /* noscript_on */, false /* origin_opt_out */,
false /* save_data_enabled */);
std::unique_ptr<data_reduction_proxy::DataReductionProxyData> data =
std::make_unique<data_reduction_proxy::DataReductionProxyData>();
......@@ -320,12 +354,14 @@ TEST_F(PreviewsUKMObserverTest, ClientLoFiOptOut) {
ValidateUKM(false /* server_lofi_expected */, true /* client_lofi_expected */,
false /* lite_page_expected */, false /* noscript_expected */,
true /* opt_out_expected */, false /* origin_opt_out_expected */);
true /* opt_out_expected */, false /* origin_opt_out_expected */,
false /* save_data_enabled_expected */);
}
TEST_F(PreviewsUKMObserverTest, ServerLoFiSeen) {
RunTest(true /* data_reduction_proxy_used */, false /* lite_page_received */,
false /* noscript_on */, false /* origin_opt_out */);
false /* noscript_on */, false /* origin_opt_out */,
false /* save_data_enabled */);
std::unique_ptr<data_reduction_proxy::DataReductionProxyData> data =
std::make_unique<data_reduction_proxy::DataReductionProxyData>();
......@@ -355,13 +391,14 @@ TEST_F(PreviewsUKMObserverTest, ServerLoFiSeen) {
ValidateUKM(true /* server_lofi_expected */, false /* client_lofi_expected */,
false /* lite_page_expected */, false /* noscript_expected */,
false /* opt_out_expected */,
false /* origin_opt_out_expected */);
false /* opt_out_expected */, false /* origin_opt_out_expected */,
false /* save_data_enabled_expected */);
}
TEST_F(PreviewsUKMObserverTest, ServerLoFiOptOut) {
RunTest(true /* data_reduction_proxy_used */, false /* lite_page_received */,
false /* noscript_on */, false /* origin_opt_out */);
false /* noscript_on */, false /* origin_opt_out */,
false /* save_data_enabled */);
std::unique_ptr<data_reduction_proxy::DataReductionProxyData> data =
std::make_unique<data_reduction_proxy::DataReductionProxyData>();
......@@ -393,12 +430,14 @@ TEST_F(PreviewsUKMObserverTest, ServerLoFiOptOut) {
ValidateUKM(true /* server_lofi_expected */, false /* client_lofi_expected */,
false /* lite_page_expected */, false /* noscript_expected */,
true /* opt_out_expected */, false /* origin_opt_out_expected */);
true /* opt_out_expected */, false /* origin_opt_out_expected */,
false /* save_data_enabled_expected */);
}
TEST_F(PreviewsUKMObserverTest, BothLoFiSeen) {
RunTest(true /* data_reduction_proxy_used */, false /* lite_page_received */,
false /* noscript_on */, false /* origin_opt_out */);
false /* noscript_on */, false /* origin_opt_out */,
false /* save_data_enabled */);
std::unique_ptr<data_reduction_proxy::DataReductionProxyData> data1 =
std::make_unique<data_reduction_proxy::DataReductionProxyData>();
......@@ -433,13 +472,14 @@ TEST_F(PreviewsUKMObserverTest, BothLoFiSeen) {
NavigateToUntrackedUrl();
ValidateUKM(true /* server_lofi_expected */, true /* client_lofi_expected */,
false /* lite_page_expected */, false /* noscript_expected */,
false /* opt_out_expected */,
false /* origin_opt_out_expected */);
false /* opt_out_expected */, false /* origin_opt_out_expected */,
false /* save_data_enabled_expected */);
}
TEST_F(PreviewsUKMObserverTest, BothLoFiOptOut) {
RunTest(true /* data_reduction_proxy_used */, false /* lite_page_received */,
false /* noscript_on */, false /* origin_opt_out */);
false /* noscript_on */, false /* origin_opt_out */,
false /* save_data_enabled */);
std::unique_ptr<data_reduction_proxy::DataReductionProxyData> data1 =
std::make_unique<data_reduction_proxy::DataReductionProxyData>();
......@@ -475,19 +515,36 @@ TEST_F(PreviewsUKMObserverTest, BothLoFiOptOut) {
NavigateToUntrackedUrl();
ValidateUKM(true /* server_lofi_expected */, true /* client_lofi_expected */,
false /* lite_page_expected */, false /* noscript_expected */,
true /* opt_out_expected */, false /* origin_opt_out_expected */);
true /* opt_out_expected */, false /* origin_opt_out_expected */,
false /* save_data_enabled_expected */);
}
TEST_F(PreviewsUKMObserverTest, OriginOptOut) {
RunTest(false /* data_reduction_proxy_used */, false /* lite_page_received */,
false /* noscript_on */, true /* origin_opt_out */);
false /* noscript_on */, true /* origin_opt_out */,
false /* save_data_enabled */);
NavigateToUntrackedUrl();
ValidateUKM(false /* server_lofi_expected */,
false /* client_lofi_expected */, false /* lite_page_expected */,
false /* noscript_expected */, false /* opt_out_expected */,
true /* origin_opt_out_expected */,
false /* save_data_enabled_expected */);
}
TEST_F(PreviewsUKMObserverTest, DataSaverEnabled) {
RunTest(false /* data_reduction_proxy_used */, false /* lite_page_received */,
false /* noscript_on */, false /* origin_opt_out */,
true /* save_data_enabled */);
NavigateToUntrackedUrl();
ValidateUKM(false /* server_lofi_expected */,
false /* client_lofi_expected */, false /* lite_page_expected */,
false /* noscript_expected */, false /* opt_out_expected */,
true /* origin_opt_out_expected */);
false /* origin_opt_out_expected */,
true /* save_data_enabled_expected */);
}
} // namespace
......
......@@ -115,6 +115,8 @@ void DataReductionProxySettings::SetCallbackToRegisterSyntheticFieldTrial(
}
bool DataReductionProxySettings::IsDataReductionProxyEnabled() const {
if (spdy_proxy_auth_enabled_.GetPrefName().empty())
return false;
return spdy_proxy_auth_enabled_.GetValue() ||
params::ShouldForceEnableDataReductionProxy();
}
......
......@@ -2492,6 +2492,12 @@ be describing additional metrics about the same event.
by the origin site (rather than the user).
</summary>
</metric>
<metric name="save_data_enabled">
<summary>
Set to 1 if the data saver feature was enabled in Chrome at the time of
the page commit.
</summary>
</metric>
<metric name="server_lofi">
<summary>
Set to 1 when a user is shown a server lo-fi image in a page load.
......
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