Commit 743dc24a authored by Sophie Chang's avatar Sophie Chang Committed by Commit Bot

Add UKM for NoScript and associated opt out

Bug: 783365
Change-Id: Ib19d2453ce4079b2367d8593dd0d1bf1c7fec301
Reviewed-on: https://chromium-review.googlesource.com/762101Reviewed-by: default avatarBryan McQuade <bmcquade@chromium.org>
Reviewed-by: default avatarRyan Sturm <ryansturm@chromium.org>
Commit-Queue: Sophie Chang <sophiechang@chromium.org>
Cr-Commit-Position: refs/heads/master@{#521409}
parent a5e6f2f8
......@@ -13,23 +13,16 @@
#include "chrome/browser/previews/previews_infobar_delegate.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/previews/content/previews_content_util.h"
#include "components/ukm/ukm_source.h"
#include "content/public/browser/navigation_handle.h"
#include "content/public/common/previews_state.h"
#include "services/metrics/public/cpp/ukm_builders.h"
#include "services/metrics/public/cpp/ukm_entry_builder.h"
#include "services/metrics/public/cpp/ukm_recorder.h"
namespace previews {
namespace {
const char kPreviewsName[] = "Previews";
const char kPreviewsServerLoFi[] = "server_lofi";
const char kPreviewsClientLoFi[] = "client_lofi";
const char kPreviewsLitePage[] = "lite_page";
const char kPreviewsOptOut[] = "opt_out";
} // namespace
PreviewsUKMObserver::PreviewsUKMObserver() {}
PreviewsUKMObserver::~PreviewsUKMObserver() {}
......@@ -53,6 +46,12 @@ PreviewsUKMObserver::OnCommit(content::NavigationHandle* navigation_handle,
if (data && data->used_data_reduction_proxy() && data->lite_page_received()) {
lite_page_seen_ = true;
}
content::PreviewsState previews_state =
chrome_navigation_data->previews_state();
if (previews_state && previews::GetMainFramePreviewsType(previews_state) ==
previews::PreviewsType::NOSCRIPT) {
noscript_seen_ = true;
}
return CONTINUE_OBSERVING;
}
......@@ -86,21 +85,21 @@ void PreviewsUKMObserver::OnComplete(
void PreviewsUKMObserver::RecordPreviewsTypes(
const page_load_metrics::PageLoadExtraInfo& info) {
// Only record previews types when they occur.
if (!server_lofi_seen_ && !client_lofi_seen_ && !lite_page_seen_)
return;
ukm::UkmRecorder* ukm_recorder = ukm::UkmRecorder::Get();
if (!ukm_recorder)
if (!server_lofi_seen_ && !client_lofi_seen_ && !lite_page_seen_ &&
!noscript_seen_)
return;
std::unique_ptr<ukm::UkmEntryBuilder> builder =
ukm_recorder->GetEntryBuilder(info.source_id, kPreviewsName);
ukm::builders::Previews builder(info.source_id);
if (server_lofi_seen_)
builder->AddMetric(kPreviewsServerLoFi, true);
builder.Setserver_lofi(1);
if (client_lofi_seen_)
builder->AddMetric(kPreviewsClientLoFi, true);
builder.Setclient_lofi(1);
if (lite_page_seen_)
builder->AddMetric(kPreviewsLitePage, true);
builder.Setlite_page(1);
if (noscript_seen_)
builder.Setnoscript(1);
if (opt_out_occurred_)
builder->AddMetric(kPreviewsOptOut, true);
builder.Setopt_out(1);
builder.Record(ukm::UkmRecorder::Get());
}
void PreviewsUKMObserver::OnLoadedResource(
......
......@@ -43,6 +43,7 @@ class PreviewsUKMObserver : public page_load_metrics::PageLoadMetricsObserver {
bool server_lofi_seen_ = false;
bool client_lofi_seen_ = false;
bool lite_page_seen_ = false;
bool noscript_seen_ = false;
bool opt_out_occurred_ = false;
SEQUENCE_CHECKER(sequence_checker_);
......
......@@ -17,6 +17,7 @@
#include "components/data_reduction_proxy/core/browser/data_reduction_proxy_data.h"
#include "components/ukm/test_ukm_recorder.h"
#include "components/ukm/ukm_source.h"
#include "content/public/common/previews_state.h"
#include "content/public/test/web_contents_tester.h"
#include "services/metrics/public/cpp/ukm_builders.h"
......@@ -44,10 +45,12 @@ class TestPreviewsUKMObserver : public PreviewsUKMObserver {
public:
TestPreviewsUKMObserver(content::WebContents* web_contents,
bool data_reduction_proxy_used,
bool lite_page_received)
bool lite_page_received,
bool noscript_on)
: web_contents_(web_contents),
data_reduction_proxy_used_(data_reduction_proxy_used),
lite_page_received_(lite_page_received) {}
lite_page_received_(lite_page_received),
noscript_on_(noscript_on) {}
~TestPreviewsUKMObserver() override {}
......@@ -59,6 +62,19 @@ class TestPreviewsUKMObserver : public PreviewsUKMObserver {
data->set_used_data_reduction_proxy(data_reduction_proxy_used_);
data->set_request_url(GURL(kDefaultTestUrl));
data->set_lite_page_received(lite_page_received_);
if (noscript_on_) {
// ChromeNavigationData is guaranteed to be non-null at this point, as
// DataForNavigationHandle is always called prior to this and creates one.
ChromeNavigationData* chrome_navigation_data =
static_cast<ChromeNavigationData*>(
navigation_handle->GetNavigationData());
content::PreviewsState previews_state =
chrome_navigation_data->previews_state();
chrome_navigation_data->set_previews_state(previews_state |=
content::NOSCRIPT_ON);
}
return PreviewsUKMObserver::OnCommit(navigation_handle, source_id);
}
......@@ -66,6 +82,7 @@ class TestPreviewsUKMObserver : public PreviewsUKMObserver {
content::WebContents* web_contents_;
bool data_reduction_proxy_used_;
bool lite_page_received_;
bool noscript_on_;
DISALLOW_COPY_AND_ASSIGN(TestPreviewsUKMObserver);
};
......@@ -76,20 +93,24 @@ class PreviewsUKMObserverTest
PreviewsUKMObserverTest() {}
~PreviewsUKMObserverTest() override {}
void RunTest(bool data_reduction_proxy_used, bool lite_page_received) {
void RunTest(bool data_reduction_proxy_used,
bool lite_page_received,
bool noscript_on) {
data_reduction_proxy_used_ = data_reduction_proxy_used;
lite_page_received_ = lite_page_received;
noscript_on_ = noscript_on;
NavigateAndCommit(GURL(kDefaultTestUrl));
}
void ValidateUKM(bool server_lofi_expected,
bool client_lofi_expected,
bool lite_page_expected,
bool noscript_expected,
bool opt_out_expected) {
using UkmEntry = ukm::builders::Previews;
auto entries = test_ukm_recorder().GetEntriesByName(UkmEntry::kEntryName);
if (!server_lofi_expected && !client_lofi_expected && !lite_page_expected &&
!opt_out_expected) {
!noscript_expected && !opt_out_expected) {
EXPECT_EQ(0u, entries.size());
return;
}
......@@ -102,6 +123,8 @@ class PreviewsUKMObserverTest
entry, UkmEntry::kclient_lofiName));
EXPECT_EQ(lite_page_expected, test_ukm_recorder().EntryHasMetric(
entry, UkmEntry::klite_pageName));
EXPECT_EQ(noscript_expected, test_ukm_recorder().EntryHasMetric(
entry, UkmEntry::knoscriptName));
EXPECT_EQ(opt_out_expected, test_ukm_recorder().EntryHasMetric(
entry, UkmEntry::kopt_outName));
}
......@@ -110,32 +133,35 @@ class PreviewsUKMObserverTest
protected:
void RegisterObservers(page_load_metrics::PageLoadTracker* tracker) override {
tracker->AddObserver(base::MakeUnique<TestPreviewsUKMObserver>(
web_contents(), data_reduction_proxy_used_, lite_page_received_));
web_contents(), data_reduction_proxy_used_, lite_page_received_,
noscript_on_));
// Data is only added to the first navigation after RunTest().
data_reduction_proxy_used_ = false;
lite_page_received_ = false;
noscript_on_ = false;
}
private:
bool data_reduction_proxy_used_ = false;
bool lite_page_received_ = false;
bool noscript_on_ = false;
DISALLOW_COPY_AND_ASSIGN(PreviewsUKMObserverTest);
};
TEST_F(PreviewsUKMObserverTest, NoPreviewSeen) {
RunTest(false /* data_reduction_proxy_used */,
false /* lite_page_received */);
RunTest(false /* data_reduction_proxy_used */, false /* lite_page_received */,
false /* noscript_on */);
NavigateToUntrackedUrl();
ValidateUKM(false /* server_lofi_expected */,
false /* client_lofi_expected */, false /* lite_page_expected */,
false /* opt_out_expected */);
false /* noscript_expected */, false /* opt_out_expected */);
}
TEST_F(PreviewsUKMObserverTest, UntrackedPreviewTypeOptOut) {
RunTest(false /* data_reduction_proxy_used */,
false /* lite_page_received */);
RunTest(false /* data_reduction_proxy_used */, false /* lite_page_received */,
false /* noscript_on */);
observer()->BroadcastEventToObservers(
PreviewsInfoBarDelegate::OptOutEventKey());
NavigateToUntrackedUrl();
......@@ -143,21 +169,23 @@ TEST_F(PreviewsUKMObserverTest, UntrackedPreviewTypeOptOut) {
// Opt out should not be added sicne we don't track this type.
ValidateUKM(false /* server_lofi_expected */,
false /* client_lofi_expected */, false /* lite_page_expected */,
false /* opt_out_expected */);
false /* noscript_expected */, false /* opt_out_expected */);
}
TEST_F(PreviewsUKMObserverTest, LitePageSeen) {
RunTest(true /* data_reduction_proxy_used */, true /* lite_page_received */);
RunTest(true /* data_reduction_proxy_used */, true /* lite_page_received */,
false /* noscript_on */);
NavigateToUntrackedUrl();
ValidateUKM(false /* server_lofi_expected */,
false /* client_lofi_expected */, true /* lite_page_expected */,
false /* opt_out_expected */);
false /* noscript_expected */, false /* opt_out_expected */);
}
TEST_F(PreviewsUKMObserverTest, LitePageOptOut) {
RunTest(true /* data_reduction_proxy_used */, true /* lite_page_received */);
RunTest(true /* data_reduction_proxy_used */, true /* lite_page_received */,
false /* noscript_on */);
observer()->BroadcastEventToObservers(
PreviewsInfoBarDelegate::OptOutEventKey());
......@@ -165,12 +193,36 @@ TEST_F(PreviewsUKMObserverTest, LitePageOptOut) {
ValidateUKM(false /* server_lofi_expected */,
false /* client_lofi_expected */, true /* lite_page_expected */,
true /* opt_out_expected */);
false /* noscript_expected */, true /* opt_out_expected */);
}
TEST_F(PreviewsUKMObserverTest, NoScriptSeen) {
RunTest(false /* data_reduction_proxy_used */, false /* lite_page_received */,
true /* noscript_on */);
NavigateToUntrackedUrl();
ValidateUKM(false /* server_lofi_expected */,
false /* client_lofi_expected */, false /* lite_page_expected */,
true /* noscript_expected */, false /* opt_out_expected */);
}
TEST_F(PreviewsUKMObserverTest, NoScriptOptOut) {
RunTest(false /* data_reduction_proxy_used */, false /* lite_page_received */,
true /* noscript_on */);
observer()->BroadcastEventToObservers(
PreviewsInfoBarDelegate::OptOutEventKey());
NavigateToUntrackedUrl();
ValidateUKM(false /* server_lofi_expected */,
false /* client_lofi_expected */, false /* lite_page_expected */,
true /* noscript_expected */, true /* opt_out_expected */);
}
TEST_F(PreviewsUKMObserverTest, ClientLoFiSeen) {
RunTest(false /* data_reduction_proxy_used */,
false /* lite_page_received */);
RunTest(false /* data_reduction_proxy_used */, false /* lite_page_received */,
false /* noscript_on */);
std::unique_ptr<data_reduction_proxy::DataReductionProxyData> data =
base::MakeUnique<data_reduction_proxy::DataReductionProxyData>();
......@@ -199,12 +251,13 @@ TEST_F(PreviewsUKMObserverTest, ClientLoFiSeen) {
NavigateToUntrackedUrl();
ValidateUKM(false /* server_lofi_expected */, true /* client_lofi_expected */,
false /* lite_page_expected */, false /* opt_out_expected */);
false /* lite_page_expected */, false /* noscript_expected */,
false /* opt_out_expected */);
}
TEST_F(PreviewsUKMObserverTest, ClientLoFiOptOut) {
RunTest(false /* data_reduction_proxy_used */,
false /* lite_page_received */);
RunTest(false /* data_reduction_proxy_used */, false /* lite_page_received */,
false /* noscript_on */);
std::unique_ptr<data_reduction_proxy::DataReductionProxyData> data =
base::MakeUnique<data_reduction_proxy::DataReductionProxyData>();
......@@ -234,11 +287,13 @@ TEST_F(PreviewsUKMObserverTest, ClientLoFiOptOut) {
NavigateToUntrackedUrl();
ValidateUKM(false /* server_lofi_expected */, true /* client_lofi_expected */,
false /* lite_page_expected */, true /* opt_out_expected */);
false /* lite_page_expected */, false /* noscript_expected */,
true /* opt_out_expected */);
}
TEST_F(PreviewsUKMObserverTest, ServerLoFiSeen) {
RunTest(true /* data_reduction_proxy_used */, false /* lite_page_received */);
RunTest(true /* data_reduction_proxy_used */, false /* lite_page_received */,
false /* noscript_on */);
std::unique_ptr<data_reduction_proxy::DataReductionProxyData> data =
base::MakeUnique<data_reduction_proxy::DataReductionProxyData>();
......@@ -267,11 +322,13 @@ TEST_F(PreviewsUKMObserverTest, ServerLoFiSeen) {
NavigateToUntrackedUrl();
ValidateUKM(true /* server_lofi_expected */, false /* client_lofi_expected */,
false /* lite_page_expected */, false /* opt_out_expected */);
false /* lite_page_expected */, false /* noscript_expected */,
false /* opt_out_expected */);
}
TEST_F(PreviewsUKMObserverTest, ServerLoFiOptOut) {
RunTest(true /* data_reduction_proxy_used */, false /* lite_page_received */);
RunTest(true /* data_reduction_proxy_used */, false /* lite_page_received */,
false /* noscript_on */);
std::unique_ptr<data_reduction_proxy::DataReductionProxyData> data =
base::MakeUnique<data_reduction_proxy::DataReductionProxyData>();
......@@ -302,11 +359,13 @@ TEST_F(PreviewsUKMObserverTest, ServerLoFiOptOut) {
NavigateToUntrackedUrl();
ValidateUKM(true /* server_lofi_expected */, false /* client_lofi_expected */,
false /* lite_page_expected */, true /* opt_out_expected */);
false /* lite_page_expected */, false /* noscript_expected */,
true /* opt_out_expected */);
}
TEST_F(PreviewsUKMObserverTest, BothLoFiSeen) {
RunTest(true /* data_reduction_proxy_used */, false /* lite_page_received */);
RunTest(true /* data_reduction_proxy_used */, false /* lite_page_received */,
false /* noscript_on */);
std::unique_ptr<data_reduction_proxy::DataReductionProxyData> data1 =
base::MakeUnique<data_reduction_proxy::DataReductionProxyData>();
......@@ -340,11 +399,13 @@ TEST_F(PreviewsUKMObserverTest, BothLoFiSeen) {
NavigateToUntrackedUrl();
ValidateUKM(true /* server_lofi_expected */, true /* client_lofi_expected */,
false /* lite_page_expected */, false /* opt_out_expected */);
false /* lite_page_expected */, false /* noscript_expected */,
false /* opt_out_expected */);
}
TEST_F(PreviewsUKMObserverTest, BothLoFiOptOut) {
RunTest(true /* data_reduction_proxy_used */, false /* lite_page_received */);
RunTest(true /* data_reduction_proxy_used */, false /* lite_page_received */,
false /* noscript_on */);
std::unique_ptr<data_reduction_proxy::DataReductionProxyData> data1 =
base::MakeUnique<data_reduction_proxy::DataReductionProxyData>();
......@@ -379,7 +440,8 @@ TEST_F(PreviewsUKMObserverTest, BothLoFiOptOut) {
PreviewsInfoBarDelegate::OptOutEventKey());
NavigateToUntrackedUrl();
ValidateUKM(true /* server_lofi_expected */, true /* client_lofi_expected */,
false /* lite_page_expected */, true /* opt_out_expected */);
false /* lite_page_expected */, false /* noscript_expected */,
true /* opt_out_expected */);
}
} // namespace
......
......@@ -1543,6 +1543,11 @@ be describing additional metrics about the same event.
Set to 1 when a user is shown a lite page in page load.
</summary>
</metric>
<metric name="noscript">
<summary>
Set to 1 when a user is shown a NoScript preview on a page load.
</summary>
</metric>
<metric name="opt_out">
<summary>
Set to 1 when a user clicks &quot;Show Original&quot; on a preview page
......
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