Skip to content

Commit

Permalink
Don't show prompt if non normal browser. Also emit metrics
Browse files Browse the repository at this point in the history
(cherry picked from commit 3065b95)

Bug: b:302174476
Change-Id: I81a6822a69aa3b251e310e67d625ec06b62a3318
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4897641
Reviewed-by: Theodore Olsauskas-Warren <sauski@google.com>
Auto-Submit: Abe Boujane <boujane@google.com>
Commit-Queue: Abe Boujane <boujane@google.com>
Reviewed-by: Sean Harrison <harrisonsean@chromium.org>
Cr-Original-Commit-Position: refs/heads/main@{#1203192}
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4903069
Owners-Override: Krishna Govind <govind@chromium.org>
Reviewed-by: Krishna Govind <govind@chromium.org>
Commit-Queue: Krishna Govind <govind@chromium.org>
Cr-Commit-Position: refs/branch-heads/5993@{#1044}
Cr-Branched-From: 5113507-refs/heads/main@{#1192594}
  • Loading branch information
Abe Boujane authored and Chromium LUCI CQ committed Oct 2, 2023
1 parent 75f3efe commit 942d653
Show file tree
Hide file tree
Showing 9 changed files with 252 additions and 9 deletions.
7 changes: 5 additions & 2 deletions chrome/browser/ui/privacy_sandbox/privacy_sandbox_prompt.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ void ShowPrivacySandboxDialog(Browser* browser,
void ShowPrivacySandboxNoticeBubble(Browser* browser);

// Returns whether a Privacy Sandbox prompt can be shown in the |browser|.
// Checks if the maximum dialog size fits the prompt.
bool CanWindowFitPrivacySandboxPrompt(Browser* browser);
// Checks if the maximum dialog height fits the prompt height.
bool CanWindowHeightFitPrivacySandboxPrompt(Browser* browser);

// Returns whether a window can fit PrivacySandbox prompt width.
bool CanWindowWidthFitPrivacySandboxPrompt(Browser* browser);

#endif // CHROME_BROWSER_UI_PRIVACY_SANDBOX_PRIVACY_SANDBOX_PROMPT_H_
50 changes: 45 additions & 5 deletions chrome/browser/ui/privacy_sandbox/privacy_sandbox_prompt_helper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include "chrome/browser/ui/privacy_sandbox/privacy_sandbox_prompt_helper.h"

#include "base/feature_list.h"
#include "base/hash/hash.h"
#include "base/metrics/histogram_functions.h"
#include "chrome/browser/privacy_sandbox/privacy_sandbox_service.h"
Expand All @@ -16,6 +17,7 @@
#include "chrome/browser/ui/privacy_sandbox/privacy_sandbox_prompt.h"
#include "chrome/common/extensions/chrome_manifest_url_handlers.h"
#include "chrome/common/webui_url_constants.h"
#include "components/privacy_sandbox/privacy_sandbox_features.h"
#include "components/sync/service/sync_service.h"
#include "content/public/browser/navigation_handle.h"
#include "content/public/browser/web_contents.h"
Expand Down Expand Up @@ -171,19 +173,57 @@ void PrivacySandboxPromptHelper::DidFinishNavigation(
}
}

const bool is_window_too_small = !CanWindowFitPrivacySandboxPrompt(browser);
// The PrivacySandbox prompt can always fit inside a normal tabbed window due
// to its minimum width, so checking the height is enough here. Other non
// normal tabbed browsers will be exlcuded in a later check.
const bool is_window_too_small =
!CanWindowHeightFitPrivacySandboxPrompt(browser);
base::UmaHistogramBoolean("Settings.PrivacySandbox.DialogWindowTooSmall",
is_window_too_small);
// If the windows size is too small, it is difficult to read or interrupt with
// the dialog. The dialog is blocking modal, that is why we want to prevent it
// from showing if there isn't enough space.
// If the windows height is too small, it is difficult to read or interrupt
// with the dialog. The dialog is blocking modal, that is why we want to
// prevent it from showing if there isn't enough space.
if (is_window_too_small) {
base::UmaHistogramEnumeration(
kPrivacySandboxPromptHelperEventHistogram,
SettingsPrivacySandboxPromptHelperEvent::kWindowTooSmall);
return;
}

auto required_prompt_type = GetRequiredPromptType(profile());

// Avoid showing the prompt on popups, pip, anything that isn't a normal
// browser.
if (base::FeatureList::IsEnabled(
privacy_sandbox::kPrivacySandboxSuppressDialogOnNonNormalBrowsers) &&
browser->type() != Browser::TYPE_NORMAL) {
switch (required_prompt_type) {
case PrivacySandboxService::PromptType::kM1Consent:
// Record how often we were going to show a consent page on a window
// with a width that isn't enough for the prompt.
base::UmaHistogramBoolean(
"Settings.PrivacySandbox.CanNonNormalBrowserWindowFitConsentWidth",
CanWindowWidthFitPrivacySandboxPrompt(browser));
break;
case PrivacySandboxService::PromptType::kM1NoticeROW:
case PrivacySandboxService::PromptType::kM1NoticeEEA:
case PrivacySandboxService::PromptType::kM1NoticeRestricted:
// Record how often we were going to show a notice page on a window
// with a width that isn't enough for the prompt.
base::UmaHistogramBoolean(
"Settings.PrivacySandbox.CanNonNormalBrowserWindowFitNoticeWidth",
CanWindowWidthFitPrivacySandboxPrompt(browser));
break;
default:
break;
}

base::UmaHistogramEnumeration(
kPrivacySandboxPromptHelperEventHistogram,
SettingsPrivacySandboxPromptHelperEvent::kNonNormalBrowser);
return;
}

// Record the URL that the prompt was displayed over.
uint32_t host_hash = base::Hash(navigation_handle->GetURL().IsAboutBlank()
? "about:blank"
Expand All @@ -195,7 +235,7 @@ void PrivacySandboxPromptHelper::DidFinishNavigation(
browser->tab_strip_model()->GetIndexOfWebContents(
navigation_handle->GetWebContents()));

ShowPrivacySandboxPrompt(browser, GetRequiredPromptType(profile()));
ShowPrivacySandboxPrompt(browser, required_prompt_type);
base::UmaHistogramEnumeration(
kPrivacySandboxPromptHelperEventHistogram,
SettingsPrivacySandboxPromptHelperEvent::kPromptShown);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,13 @@ class PrivacySandboxPromptHelper
SinglePromptPerBrowser);
FRIEND_TEST_ALL_PREFIXES(PrivacySandboxPromptHelperTestWithParam,
MultipleBrowserWindows);
FRIEND_TEST_ALL_PREFIXES(
PrivacySandboxPromptNonNormalBrowserFeatureDisabledTest,
NonNormalBrowserShowsPrompt);
FRIEND_TEST_ALL_PREFIXES(PrivacySandboxPromptNonNormalBrowserTest,
NoPromptInSmallBrowser);
FRIEND_TEST_ALL_PREFIXES(PrivacySandboxPromptNonNormalBrowserTest,
NoPromptInLargeBrowser);

// Contains all the events that the helper goes through when attempting to
// show a Privacy Sandbox prompt. Must be kept in sync with the
Expand All @@ -70,9 +77,10 @@ class PrivacySandboxPromptHelper
kPromptAlreadyExistsForBrowser = 7,
kWindowTooSmall = 8,
kPromptShown = 9,
kNonNormalBrowser = 11,
// Add values above this line with a corresponding label in
// tools/metrics/histograms/enums.xml
kMaxValue = kPromptShown,
kMaxValue = kNonNormalBrowser,
};

explicit PrivacySandboxPromptHelper(content::WebContents* web_contents);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -503,3 +503,153 @@ INSTANTIATE_TEST_SUITE_P(
PrivacySandboxService::PromptType::kM1NoticeROW,
PrivacySandboxService::PromptType::kConsent,
PrivacySandboxService::PromptType::kNotice));

struct PrivacySandboxNonNormalBrowserTestData {
const PrivacySandboxService::PromptType prompt_type;
const char* width_histogram;
};

class PrivacySandboxPromptNonNormalBrowserTest
: public PrivacySandboxPromptHelperTest,
public testing::WithParamInterface<
PrivacySandboxNonNormalBrowserTestData> {
public:
PrivacySandboxService::PromptType TestPromptType() override {
return GetParam().prompt_type;
}
};

IN_PROC_BROWSER_TEST_P(PrivacySandboxPromptNonNormalBrowserTest,
NoPromptInLargeBrowser) {
base::HistogramTester histogram_tester;
EXPECT_CALL(*mock_privacy_sandbox_service(),
PromptOpenedForBrowser(testing::_, testing::_))
.Times(0);

NavigateParams params(browser(), GURL(chrome::kChromeUINewTabPageURL),
ui::PAGE_TRANSITION_FIRST);
params.window_action = NavigateParams::SHOW_WINDOW;
params.disposition = WindowOpenDisposition::NEW_POPUP;
params.window_features.bounds = gfx::Rect(0, 0, 500, 500);
ui_test_utils::NavigateToURL(&params);

ValidatePromptEventEntries(
&histogram_tester,
{{PrivacySandboxPromptHelper::SettingsPrivacySandboxPromptHelperEvent::
kCreated,
1},
{PrivacySandboxPromptHelper::SettingsPrivacySandboxPromptHelperEvent::
kNonNormalBrowser,
1},
{PrivacySandboxPromptHelper::SettingsPrivacySandboxPromptHelperEvent::
kPromptShown,
0}});

histogram_tester.ExpectBucketCount(GetParam().width_histogram, true, 1);
histogram_tester.ExpectBucketCount(GetParam().width_histogram, false, 0);
}

IN_PROC_BROWSER_TEST_P(PrivacySandboxPromptNonNormalBrowserTest,
NoPromptInSmallBrowser) {
base::HistogramTester histogram_tester;
EXPECT_CALL(*mock_privacy_sandbox_service(),
PromptOpenedForBrowser(testing::_, testing::_))
.Times(0);

NavigateParams params(browser(), GURL(chrome::kChromeUINewTabPageURL),
ui::PAGE_TRANSITION_FIRST);
params.window_action = NavigateParams::SHOW_WINDOW;
params.disposition = WindowOpenDisposition::NEW_POPUP;
params.window_features.bounds = gfx::Rect(0, 0, 200, 200);
ui_test_utils::NavigateToURL(&params);

ValidatePromptEventEntries(
&histogram_tester,
{{PrivacySandboxPromptHelper::SettingsPrivacySandboxPromptHelperEvent::
kCreated,
1},
{PrivacySandboxPromptHelper::SettingsPrivacySandboxPromptHelperEvent::
kNonNormalBrowser,
1},
{PrivacySandboxPromptHelper::SettingsPrivacySandboxPromptHelperEvent::
kPromptShown,
0}});

histogram_tester.ExpectBucketCount(GetParam().width_histogram, true, 0);
histogram_tester.ExpectBucketCount(GetParam().width_histogram, false, 1);
}

INSTANTIATE_TEST_SUITE_P(
PrivacySandboxPromptNonNormalBrowserTestInstance,
PrivacySandboxPromptNonNormalBrowserTest,
testing::Values<PrivacySandboxNonNormalBrowserTestData>(
PrivacySandboxNonNormalBrowserTestData{
PrivacySandboxService::PromptType::kM1Consent,
"Settings.PrivacySandbox.CanNonNormalBrowserWindowFitConsentWidth"},
PrivacySandboxNonNormalBrowserTestData{
PrivacySandboxService::PromptType::kM1NoticeEEA,
"Settings.PrivacySandbox.CanNonNormalBrowserWindowFitNoticeWidth"},
PrivacySandboxNonNormalBrowserTestData{
PrivacySandboxService::PromptType::kM1NoticeROW,
"Settings.PrivacySandbox.CanNonNormalBrowserWindowFitNoticeWidth"},
PrivacySandboxNonNormalBrowserTestData{
PrivacySandboxService::PromptType::kM1NoticeRestricted,
"Settings.PrivacySandbox."
"CanNonNormalBrowserWindowFitNoticeWidth"}));

class PrivacySandboxPromptNonNormalBrowserParamTest
: public PrivacySandboxPromptHelperTest {
public:
PrivacySandboxService::PromptType TestPromptType() override {
return PrivacySandboxService::PromptType::kM1NoticeEEA;
}
};

class PrivacySandboxPromptNonNormalBrowserFeatureDisabledTest
: public PrivacySandboxPromptHelperTestWithParam {
public:
void SetUpInProcessBrowserTestFixture() override {
feature_list_.InitWithFeatures(
{},
{privacy_sandbox::kPrivacySandboxSuppressDialogOnNonNormalBrowsers});
PrivacySandboxPromptHelperTest::SetUpInProcessBrowserTestFixture();
}

private:
PrivacySandboxService::PromptType TestPromptType() override {
return GetParam();
}
base::test::ScopedFeatureList feature_list_;
};

IN_PROC_BROWSER_TEST_P(PrivacySandboxPromptNonNormalBrowserFeatureDisabledTest,
NonNormalBrowserShowsPrompt) {
base::HistogramTester histogram_tester;
EXPECT_CALL(*mock_privacy_sandbox_service(),
PromptOpenedForBrowser(testing::_, testing::_))
.Times(1);

ui_test_utils::NavigateToURLWithDisposition(
browser(), GURL(url::kAboutBlankURL), WindowOpenDisposition::NEW_POPUP,
ui_test_utils::BROWSER_TEST_WAIT_FOR_LOAD_STOP);

ValidatePromptEventEntries(
&histogram_tester,
{{PrivacySandboxPromptHelper::SettingsPrivacySandboxPromptHelperEvent::
kCreated,
1},
{PrivacySandboxPromptHelper::SettingsPrivacySandboxPromptHelperEvent::
kNonNormalBrowser,
0},
{PrivacySandboxPromptHelper::SettingsPrivacySandboxPromptHelperEvent::
kPromptShown,
1}});
}

INSTANTIATE_TEST_SUITE_P(
PrivacySandboxPromptNonNormalBrowserFeatureDisabledTestInstance,
PrivacySandboxPromptNonNormalBrowserFeatureDisabledTest,
testing::Values(PrivacySandboxService::PromptType::kM1Consent,
PrivacySandboxService::PromptType::kM1NoticeEEA,
PrivacySandboxService::PromptType::kM1NoticeROW,
PrivacySandboxService::PromptType::kM1NoticeRestricted));
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ constexpr int kM1DialogWidth = 600;
constexpr int kDefaultConsentDialogHeight = 569;
constexpr int kDefaultNoticeDialogHeight = 494;
constexpr int kMinRequiredDialogHeight = 100;
constexpr int kMinRequiredDialogWidth = 400;

GURL GetDialogURL(PrivacySandboxService::PromptType prompt_type) {
GURL base_url = GURL(chrome::kChromeUIPrivacySandboxDialogURL);
Expand Down Expand Up @@ -99,14 +100,22 @@ class PrivacySandboxDialogDelegate : public views::DialogDelegate {
} // namespace

// static
bool CanWindowFitPrivacySandboxPrompt(Browser* browser) {
bool CanWindowHeightFitPrivacySandboxPrompt(Browser* browser) {
const int max_dialog_height = browser->window()
->GetWebContentsModalDialogHost()
->GetMaximumDialogSize()
.height();
return max_dialog_height >= kMinRequiredDialogHeight;
}

bool CanWindowWidthFitPrivacySandboxPrompt(Browser* browser) {
const int max_dialog_width = browser->window()
->GetWebContentsModalDialogHost()
->GetMaximumDialogSize()
.width();
return max_dialog_width >= kMinRequiredDialogWidth;
}

// static
void ShowPrivacySandboxDialog(Browser* browser,
PrivacySandboxService::PromptType prompt_type) {
Expand Down
5 changes: 5 additions & 0 deletions components/privacy_sandbox/privacy_sandbox_features.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@

namespace privacy_sandbox {

// Show the Tracking Protection onboarding flow if not already onboarded.
BASE_FEATURE(kPrivacySandboxSuppressDialogOnNonNormalBrowsers,
"PrivacySandboxSuppressDialogOnNonNormalBrowsers",
base::FEATURE_ENABLED_BY_DEFAULT);

BASE_FEATURE(kPrivacySandboxSettings4,
"PrivacySandboxSettings4",
base::FEATURE_DISABLED_BY_DEFAULT);
Expand Down
5 changes: 5 additions & 0 deletions components/privacy_sandbox/privacy_sandbox_features.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@

namespace privacy_sandbox {

// When true, do not show any privacySandbox dialog when the browser isn't a
// normal browser.
COMPONENT_EXPORT(PRIVACY_SANDBOX_FEATURES)
BASE_DECLARE_FEATURE(kPrivacySandboxSuppressDialogOnNonNormalBrowsers);

// Enables the fourth release of the Privacy Sandbox settings.
COMPONENT_EXPORT(PRIVACY_SANDBOX_FEATURES)
BASE_DECLARE_FEATURE(kPrivacySandboxSettings4);
Expand Down
1 change: 1 addition & 0 deletions tools/metrics/histograms/enums.xml
Original file line number Diff line number Diff line change
Expand Up @@ -97735,6 +97735,7 @@ https://www.dmtf.org/sites/default/files/standards/documents/DSP0134_2.7.1.pdf
<int value="7" label="Prompt already exists for browser"/>
<int value="8" label="Window too small for dialog"/>
<int value="9" label="Prompt shown"/>
<int value="11" label="Browser not a normal tabbed browser"/>
</enum>

<enum name="SettingsPrivacySandboxPromptStartupState">
Expand Down
22 changes: 22 additions & 0 deletions tools/metrics/histograms/metadata/settings/histograms.xml
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,28 @@ chromium-metrics-reviews@google.com.
</summary>
</histogram>

<histogram
name="Settings.PrivacySandbox.CanNonNormalBrowserWindowFitConsentWidth"
enum="Boolean" expires_after="2024-08-25">
<owner>boujane@google.com</owner>
<owner>koilos@google.com</owner>
<summary>
In case of non normal browser(Pop up or PIP for example), Records whether
the window width was too small to fit the consent dialog.
</summary>
</histogram>

<histogram
name="Settings.PrivacySandbox.CanNonNormalBrowserWindowFitNoticeWidth"
enum="Boolean" expires_after="2024-08-25">
<owner>boujane@google.com</owner>
<owner>koilos@google.com</owner>
<summary>
In case of non normal browser(Pop up or PIP for example), Records whether
the window width was too small to fit the notice dialog.
</summary>
</histogram>

<histogram name="Settings.PrivacySandbox.DeprecatedRedirect"
enum="BooleanRedirected" expires_after="2024-02-11">
<owner>olesiamarukhno@google.com</owner>
Expand Down

0 comments on commit 942d653

Please sign in to comment.