Commit 7e7c1ff9 authored by John Delaney's avatar John Delaney Committed by Commit Bot

Enable heavy ads in incognito mode

The heavy ad intervention currently does not trigger when a blocklist
is not available. We do not setup the blocklist in incognito mode.

This change creates a blocklist for incognito profiles with no
backing store, which simply creates an in-memory blocklist. This allows
us to have very similar behavior across incognito & non-incognito.

Bug: 1022806
Change-Id: I0d1e48adc08eb6dd93fb8a9ac6dd6b848f0ca348
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1902528
Commit-Queue: John Delaney <johnidel@chromium.org>
Reviewed-by: default avatarAvi Drissman <avi@chromium.org>
Reviewed-by: default avatarCharlie Harrison <csharrison@chromium.org>
Cr-Commit-Position: refs/heads/master@{#713928}
parent 5d360810
......@@ -54,3 +54,14 @@ void HeavyAdService::Initialize(const base::FilePath& profile_path) {
heavy_ad_blocklist_ = std::make_unique<HeavyAdBlocklist>(
std::move(opt_out_store), base::DefaultClock::GetInstance(), this);
}
void HeavyAdService::InitializeOffTheRecord() {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
if (!base::FeatureList::IsEnabled(features::kHeavyAdPrivacyMitigations))
return;
// Providing a null out_out_store which sets up the blocklist in-memory only.
heavy_ad_blocklist_ = std::make_unique<HeavyAdBlocklist>(
nullptr /* opt_out_store */, base::DefaultClock::GetInstance(), this);
}
......@@ -29,6 +29,9 @@ class HeavyAdService : public KeyedService,
// disk.
void Initialize(const base::FilePath& profile_path);
// Initializes the blocklist with no backing store for incognito mode.
void InitializeOffTheRecord();
HeavyAdBlocklist* heavy_ad_blocklist() { return heavy_ad_blocklist_.get(); }
private:
......
......@@ -5,6 +5,7 @@
#include "chrome/browser/heavy_ad_intervention/heavy_ad_service_factory.h"
#include "chrome/browser/heavy_ad_intervention/heavy_ad_service.h"
#include "chrome/browser/profiles/incognito_helpers.h"
#include "chrome/browser/profiles/profile.h"
#include "components/keyed_service/content/browser_context_dependency_manager.h"
#include "content/public/browser/browser_context.h"
......@@ -39,3 +40,8 @@ KeyedService* HeavyAdServiceFactory::BuildServiceInstanceFor(
content::BrowserContext* context) const {
return new HeavyAdService();
}
content::BrowserContext* HeavyAdServiceFactory::GetBrowserContextToUse(
content::BrowserContext* context) const {
return chrome::GetBrowserContextOwnInstanceInIncognito(context);
}
......@@ -34,6 +34,8 @@ class HeavyAdServiceFactory : public BrowserContextKeyedServiceFactory {
// BrowserContextKeyedServiceFactory:
KeyedService* BuildServiceInstanceFor(
content::BrowserContext* context) const override;
content::BrowserContext* GetBrowserContextToUse(
content::BrowserContext* context) const override;
DISALLOW_COPY_AND_ASSIGN(HeavyAdServiceFactory);
};
......
......@@ -1103,8 +1103,7 @@ bool AdsPageLoadMetricsObserver::IsBlocklisted() {
auto* blocklist = GetHeavyAdBlocklist();
// Treat instances where the blocklist is unavailable as blocklisted. This
// includes incognito profiles, which do not create a blocklist service.
// Treat instances where the blocklist is unavailable as blocklisted.
if (!blocklist) {
heavy_ads_blocklist_blocklisted_ = true;
return true;
......
......@@ -1130,10 +1130,10 @@ IN_PROC_BROWSER_TEST_F(AdsPageLoadMetricsObserverResourceBrowserTest,
->trial_name()));
}
// Verifies that when the blacklist is at threshold, the heavy ad intervention
// Verifies that when the blocklist is at threshold, the heavy ad intervention
// does not trigger.
IN_PROC_BROWSER_TEST_F(AdsPageLoadMetricsObserverResourceBrowserTest,
HeavyAdInterventionBlacklistFull_InterventionBlocked) {
HeavyAdInterventionBlocklistFull_InterventionBlocked) {
base::HistogramTester histogram_tester;
auto large_resource_1 =
std::make_unique<net::test_server::ControllableHttpResponse>(
......@@ -1196,6 +1196,43 @@ IN_PROC_BROWSER_TEST_F(AdsPageLoadMetricsObserverResourceBrowserTest,
FrameData::HeavyAdStatus::kNetwork, 1);
}
// Verifies that the blocklist is setup correctly and the intervention triggers
// in incognito mode.
IN_PROC_BROWSER_TEST_F(AdsPageLoadMetricsObserverResourceBrowserTest,
HeavyAdInterventionIncognitoMode_InterventionFired) {
base::HistogramTester histogram_tester;
auto incomplete_resource_response =
std::make_unique<net::test_server::ControllableHttpResponse>(
embedded_test_server(), "/ads_observer/incomplete_resource.js",
true /*relative_url_is_prefix*/);
ASSERT_TRUE(embedded_test_server()->Start());
Browser* incognito_browser = CreateIncognitoBrowser();
content::WebContents* web_contents =
incognito_browser->tab_strip_model()->GetActiveWebContents();
// Create a navigation observer that will watch for the intervention to
// navigate the frame.
content::TestNavigationObserver error_observer(web_contents,
net::ERR_BLOCKED_BY_CLIENT);
// Create a waiter for the incognito contents.
auto waiter = std::make_unique<page_load_metrics::PageLoadMetricsTestWaiter>(
web_contents);
GURL url = embedded_test_server()->GetURL(
"/ads_observer/ad_with_incomplete_resource.html");
ui_test_utils::NavigateToURL(incognito_browser, url);
// Load a resource large enough to trigger the intervention.
LoadLargeResource(incomplete_resource_response.get(), kMaxHeavyAdNetworkSize);
// Wait for the intervention page navigation to finish on the frame.
error_observer.WaitForNavigationFinished();
// Check that the ad frame was navigated to the intervention page.
EXPECT_FALSE(error_observer.last_navigation_succeeded());
}
// Verify that UKM metrics are recorded correctly.
IN_PROC_BROWSER_TEST_F(AdsPageLoadMetricsObserverResourceBrowserTest,
RecordedUKMMetrics) {
......
......@@ -31,6 +31,8 @@
#include "chrome/browser/download/chrome_download_manager_delegate.h"
#include "chrome/browser/download/download_core_service.h"
#include "chrome/browser/download/download_core_service_factory.h"
#include "chrome/browser/heavy_ad_intervention/heavy_ad_service.h"
#include "chrome/browser/heavy_ad_intervention/heavy_ad_service_factory.h"
#include "chrome/browser/native_file_system/chrome_native_file_system_permission_context.h"
#include "chrome/browser/native_file_system/native_file_system_permission_context_factory.h"
#include "chrome/browser/permissions/permission_manager.h"
......@@ -187,6 +189,8 @@ void OffTheRecordProfileImpl::Init() {
// AccessibilityLabelsService has a default prefs behavior in incognito.
AccessibilityLabelsService::InitOffTheRecordPrefs(this);
HeavyAdServiceFactory::GetForBrowserContext(this)->InitializeOffTheRecord();
}
OffTheRecordProfileImpl::~OffTheRecordProfileImpl() {
......
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