Revert "Remove the toast when user blocks 3PCs - Desktop"

This reverts commit 527549adb8c90b2f28f9d7faec8cbc887559145e.

Reason for revert: PWG is requesting a privacy review and launch approval for this change.  See http://b/389987327#comment5.

Original change's description:
> Remove the toast when user blocks 3PCs - Desktop
>
> Bug: 389987327
> Change-Id: I04f611c0a8dc1c76e17543512925e42e54da6c67
> Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/6196596
> Reviewed-by: Side YILMAZ <sideyilmaz@chromium.org>
> Commit-Queue: Side YILMAZ <sideyilmaz@chromium.org>
> Auto-Submit: Jacob Stanley <jacobstanley@google.com>
> Reviewed-by: Abe Boujane <boujane@google.com>
> Reviewed-by: Fiona Macintosh <fmacintosh@google.com>
> Cr-Commit-Position: refs/heads/main@{#1412442}

Bug: 389987327
Change-Id: I76fc576ecabdc1e176f172573bf5b5af2582f4da
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/6233612
Reviewed-by: Fiona Macintosh <fmacintosh@google.com>
Reviewed-by: Demetrios Papadopoulos <dpapad@chromium.org>
Commit-Queue: Kevin Graney <kmg@google.com>
Reviewed-by: Jacob Stanley <jacobstanley@google.com>
Cr-Commit-Position: refs/heads/main@{#1416260}
diff --git a/chrome/app/settings_strings.grdp b/chrome/app/settings_strings.grdp
index d2579634..356ba4c 100644
--- a/chrome/app/settings_strings.grdp
+++ b/chrome/app/settings_strings.grdp
@@ -1506,6 +1506,12 @@
   <message name="IDS_SETTINGS_PRIVACY_SANDBOX_TITLE" desc="The title of the Privacy Sandbox page. 'Privacy Sandbox' is a noun phrase (title case).">
     Privacy Sandbox
   </message>
+  <message name="IDS_SETTINGS_PRIVACY_SANDBOX_COOKIES_DIALOG" desc="Text of a dialog shown to users when they interacted with cookies to make them aware of the Privacy Sandbox.">
+    Learn about and control new technologies that aim to replace third-party cookies
+  </message>
+  <message name="IDS_SETTINGS_PRIVACY_SANDBOX_COOKIES_DIALOG_MORE" desc="Label on the button in the dialog shown to users when they interacted with cookies to make them aware of the Privacy Sandbox.">
+    More
+  </message>
 
   <!-- Privacy Sandbox Settings 4 - Privacy & Security Page -->
   <message name="IDS_SETTINGS_AD_PRIVACY_LINK_ROW_LABEL" desc="A new label that appears on the Privacy and security page (chrome://settings/privacy) between 'Third-party cookies' and 'Security'. This same text is used as a page title for the new Ad privacy section. This same pair of words is used in the Consent and Notice flow and must be consistently translated in both contexts. We mean privacy associated with ads, and not privacy from ads. **** CONTEXT PRIVACY SANDBOX **** Chrome’s Privacy Sandbox initiative 1) deprecates third-party cookies in Chrome, 2) supports free and open content on the web (by finding better ways to support ads online), 3) while providing stronger privacy protections for users. You can see a high-level description of this public project at www.privacysanbox.com. **** NEW SETTINGS SECTION IN CHROME **** 3 new ad-specific settings appear on an 'Ad privacy' page in Chrome settings. For an equivalent structure, see 'Security' on chrome://settings/privacy that opens chrome://settings/security. Likewise, 'Ad privacy' on chrome://settings/privacy will open chrome://settings/AdPrivacy.">
diff --git a/chrome/app/settings_strings_grdp/IDS_SETTINGS_PRIVACY_SANDBOX_COOKIES_DIALOG.png.sha1 b/chrome/app/settings_strings_grdp/IDS_SETTINGS_PRIVACY_SANDBOX_COOKIES_DIALOG.png.sha1
new file mode 100644
index 0000000..bc7005f
--- /dev/null
+++ b/chrome/app/settings_strings_grdp/IDS_SETTINGS_PRIVACY_SANDBOX_COOKIES_DIALOG.png.sha1
@@ -0,0 +1 @@
+835a8566f867c778f25d6cc1ce98196c41a77c25
\ No newline at end of file
diff --git a/chrome/app/settings_strings_grdp/IDS_SETTINGS_PRIVACY_SANDBOX_COOKIES_DIALOG_MORE.png.sha1 b/chrome/app/settings_strings_grdp/IDS_SETTINGS_PRIVACY_SANDBOX_COOKIES_DIALOG_MORE.png.sha1
new file mode 100644
index 0000000..bc7005f
--- /dev/null
+++ b/chrome/app/settings_strings_grdp/IDS_SETTINGS_PRIVACY_SANDBOX_COOKIES_DIALOG_MORE.png.sha1
@@ -0,0 +1 @@
+835a8566f867c778f25d6cc1ce98196c41a77c25
\ No newline at end of file
diff --git a/chrome/browser/resources/settings/privacy_page/cookies_page.html b/chrome/browser/resources/settings/privacy_page/cookies_page.html
index 55df1058..e4435960 100644
--- a/chrome/browser/resources/settings/privacy_page/cookies_page.html
+++ b/chrome/browser/resources/settings/privacy_page/cookies_page.html
@@ -74,6 +74,12 @@
         padding-inline-start: 0;
       }
 
+      #toastText {
+        align-items: center;
+        display: flex;
+        max-width: 300px;
+        min-height: var(--cr-section-two-line-min-height);
+      }
     </style>
     <template is="dom-if" if="[[!is3pcdRedesignEnabled_]]">
       <div id="explanationText" class="secondary">
@@ -315,3 +321,12 @@
         search-filter="[[searchTerm]]"
         cookies-exception-type="third-party">
     </site-list>
+    <cr-toast id="toast">
+      <div id="toastText">$i18n{privacySandboxCookiesDialog}</div>
+      <cr-button on-click="onPrivacySandboxClick_">
+        $i18n{privacySandboxCookiesDialogMore}
+      </cr-button>
+      <a id="privacySandboxLink" href="adPrivacy"
+          target="_blank" tabindex="-1" aria-disabled="true"
+          role="none"></a>
+    </cr-toast>
diff --git a/chrome/browser/resources/settings/privacy_page/cookies_page.ts b/chrome/browser/resources/settings/privacy_page/cookies_page.ts
index 2b7d580..f5803c47 100644
--- a/chrome/browser/resources/settings/privacy_page/cookies_page.ts
+++ b/chrome/browser/resources/settings/privacy_page/cookies_page.ts
@@ -11,6 +11,7 @@
 import '/shared/settings/prefs/prefs.js';
 import 'chrome://resources/cr_elements/cr_icon_button/cr_icon_button.js';
 import 'chrome://resources/cr_elements/cr_link_row/cr_link_row.js';
+import 'chrome://resources/cr_elements/cr_toast/cr_toast.js';
 import '../controls/settings_toggle_button.js';
 import '../icons.html.js';
 import '../privacy_icons.html.js';
@@ -21,6 +22,7 @@
 import '../controls/settings_radio_group.js';
 
 import {PrefsMixin} from '/shared/settings/prefs/prefs_mixin.js';
+import type {CrToastElement} from 'chrome://resources/cr_elements/cr_toast/cr_toast.js';
 import {I18nMixin} from 'chrome://resources/cr_elements/i18n_mixin.js';
 import {WebUiListenerMixin} from 'chrome://resources/cr_elements/web_ui_listener_mixin.js';
 import {assert} from 'chrome://resources/js/assert.js';
@@ -34,14 +36,21 @@
 import type {MetricsBrowserProxy} from '../metrics_browser_proxy.js';
 import {MetricsBrowserProxyImpl, PrivacyElementInteractions} from '../metrics_browser_proxy.js';
 import {routes} from '../route.js';
-import {Router} from '../router.js';
+import type {Route} from '../router.js';
+import {RouteObserverMixin, Router} from '../router.js';
 import {ContentSetting, ContentSettingsTypes, CookieControlsMode} from '../site_settings/constants.js';
 import {ThirdPartyCookieBlockingSetting} from '../site_settings/site_settings_prefs_browser_proxy.js';
 
 import {getTemplate} from './cookies_page.html.js';
 
-const SettingsCookiesPageElementBase =
-    WebUiListenerMixin(I18nMixin(PrefsMixin(PolymerElement)));
+export interface SettingsCookiesPageElement {
+  $: {
+    toast: CrToastElement,
+  };
+}
+
+const SettingsCookiesPageElementBase = RouteObserverMixin(
+    WebUiListenerMixin(I18nMixin(PrefsMixin(PolymerElement))));
 
 export class SettingsCookiesPageElement extends SettingsCookiesPageElementBase {
   static get is() {
@@ -159,6 +168,12 @@
         selectSiteDataLinkRow);
   }
 
+  override currentRouteChanged(route: Route) {
+    if (route !== routes.COOKIES) {
+      this.$.toast.hide();
+    }
+  }
+
   private onSiteDataClick_() {
     Router.getInstance().navigateTo(routes.SITE_SETTINGS_ALL);
   }
@@ -183,18 +198,23 @@
         PrivacyElementInteractions.IP_PROTECTION);
   }
 
-  private record3pcBlockingAction(switchedToBlock3pcs: boolean) {
+  private showOrHideToast(switchedToBlock3pcs: boolean) {
     // If this change resulted in the user now blocking 3P cookies where they
     // previously were not, and any of privacy sandbox APIs are enabled,
-    // record the action.
+    // the privacy sandbox toast should be shown.
     const areAnyPrivacySandboxApisEnabled =
         this.getPref('privacy_sandbox.m1.topics_enabled').value ||
         this.getPref('privacy_sandbox.m1.fledge_enabled').value ||
         this.getPref('privacy_sandbox.m1.ad_measurement_enabled').value;
 
     if (areAnyPrivacySandboxApisEnabled && switchedToBlock3pcs) {
+      if (!loadTimeData.getBoolean('isPrivacySandboxRestricted')) {
+        this.$.toast.show();
+      }
       this.metricsBrowserProxy_.recordAction(
           'Settings.PrivacySandbox.Block3PCookies');
+    } else {
+      this.$.toast.hide();
     }
   }
 
@@ -218,7 +238,7 @@
 
     const currentCookieControlsMode =
         this.getPref('profile.cookie_controls_mode').value;
-    this.record3pcBlockingAction(
+    this.showOrHideToast(
         (currentCookieControlsMode === CookieControlsMode.OFF ||
          currentCookieControlsMode === CookieControlsMode.INCOGNITO_ONLY) &&
         selection === CookieControlsMode.BLOCK_THIRD_PARTY);
@@ -242,7 +262,7 @@
 
     const currentThirdPartyCookieBlockingSetting =
         this.getPref('generated.third_party_cookie_blocking_setting').value;
-    this.record3pcBlockingAction(
+    this.showOrHideToast(
         currentThirdPartyCookieBlockingSetting ===
             ThirdPartyCookieBlockingSetting.INCOGNITO_ONLY &&
         selection === ThirdPartyCookieBlockingSetting.BLOCK_THIRD_PARTY);
@@ -250,6 +270,16 @@
     thirdPartyCookieBlockingSettingGroup.sendPrefChange();
   }
 
+  private onPrivacySandboxClick_() {
+    this.metricsBrowserProxy_.recordAction(
+        'Settings.PrivacySandbox.OpenedFromCookiesPageToast');
+    this.$.toast.hide();
+    // TODO(crbug.com/40162029): Replace this with an ordinary OpenWindowProxy
+    // call.
+    this.shadowRoot!.querySelector<HTMLAnchorElement>(
+                        '#privacySandboxLink')!.click();
+  }
+
   private relatedWebsiteSetsToggleDisabled_() {
     return this.getPref('profile.cookie_controls_mode').value !==
         CookieControlsMode.BLOCK_THIRD_PARTY;
diff --git a/chrome/browser/ui/webui/settings/settings_localized_strings_privacy_sandbox_provider.cc b/chrome/browser/ui/webui/settings/settings_localized_strings_privacy_sandbox_provider.cc
index 8ea5870..9da8bc4 100644
--- a/chrome/browser/ui/webui/settings/settings_localized_strings_privacy_sandbox_provider.cc
+++ b/chrome/browser/ui/webui/settings/settings_localized_strings_privacy_sandbox_provider.cc
@@ -46,6 +46,10 @@
   // replace those before the corresponding flag value is checked, which is why
   // they are included independently of the flag value.
   static constexpr webui::LocalizedString kLocalizedStrings[] = {
+      {"privacySandboxCookiesDialog",
+       IDS_SETTINGS_PRIVACY_SANDBOX_COOKIES_DIALOG},
+      {"privacySandboxCookiesDialogMore",
+       IDS_SETTINGS_PRIVACY_SANDBOX_COOKIES_DIALOG_MORE},
       {"privacySandboxLearnMoreDialogTopicsDataTypes",
        IDS_SETTINGS_PRIVACY_SANDBOX_LEARN_MORE_DIALOG_TOPICS_DATA_TYPES},
       {"privacySandboxLearnMoreDialogTopicsDataUsage",
diff --git a/chrome/test/data/webui/settings/cookies_page_test.ts b/chrome/test/data/webui/settings/cookies_page_test.ts
index 7d491b84..ee3cfea 100644
--- a/chrome/test/data/webui/settings/cookies_page_test.ts
+++ b/chrome/test/data/webui/settings/cookies_page_test.ts
@@ -8,7 +8,7 @@
 import type {SettingsCollapseRadioButtonElement, SettingsRadioGroupElement, SettingsCookiesPageElement} from 'chrome://settings/lazy_load.js';
 import {CookieControlsMode, ContentSettingsTypes, SITE_EXCEPTION_WILDCARD, SiteSettingsPrefsBrowserProxyImpl,ThirdPartyCookieBlockingSetting} from 'chrome://settings/lazy_load.js';
 import type {SettingsPrefsElement, SettingsToggleButtonElement} from 'chrome://settings/settings.js';
-import {CrSettingsPrefs, MetricsBrowserProxyImpl, PrivacyElementInteractions, resetRouterForTesting, Router} from 'chrome://settings/settings.js';
+import {CrSettingsPrefs, MetricsBrowserProxyImpl, PrivacyElementInteractions, resetRouterForTesting, Router, routes} from 'chrome://settings/settings.js';
 import {assertEquals, assertFalse, assertTrue} from 'chrome://webui-test/chai_assert.js';
 import {eventToPromise, isChildVisible} from 'chrome://webui-test/test_util.js';
 import {flushTasks} from 'chrome://webui-test/polymer_test_util.js';
@@ -182,29 +182,160 @@
         testMetricsBrowserProxy.reset();
       });
 
-  test('cookieBlockingSettingsUserActions', async function() {
-    // Disabling third-party cookies should update the cookie controls mode pref
-    // and emit the user action.
+  test('privacySandboxToast', async function() {
+    assertFalse(page.$.toast.open);
+
+    // Disabling third-party cookies should display the privacy sandbox toast.
     blockThirdParty().click();
     await eventToPromise('selected-changed', primarySettingGroup());
     await flushTasks();
     assertEquals(
         page.getPref('profile.cookie_controls_mode.value'),
         CookieControlsMode.BLOCK_THIRD_PARTY);
-    assertEquals(
-        'Settings.PrivacySandbox.Block3PCookies',
-        await testMetricsBrowserProxy.whenCalled('recordAction'));
-    testMetricsBrowserProxy.resetResolver('recordAction');
+    // TODO(crbug.com/40244046): Check histograms.
+    assertTrue(page.$.toast.open);
 
-    // Disabling privacy sandbox APIs should not affect cookie blocking, but it
-    // does affect the user action to not be emitted here.
+    // Clicking the toast link should be recorded in UMA and should dismiss
+    // the toast.
+    page.$.toast.querySelector('cr-button')!.click();
+    // TODO(crbug.com/40244046): Check histograms.
+    assertFalse(page.$.toast.open);
+
+    // Re-enabling 3P cookies for regular sessions should not display the toast.
+    blockThirdPartyIncognito().click();
+    await eventToPromise('selected-changed', primarySettingGroup());
+    await flushTasks();
+    assertEquals(
+        page.getPref('profile.cookie_controls_mode.value'),
+        CookieControlsMode.INCOGNITO_ONLY);
+    assertFalse(page.$.toast.open);
+
+    // The toast should not be displayed if the user has any privacy sandbox
+    // APIs disabled.
     page.set('prefs.privacy_sandbox.m1.topics_enabled.value', false);
     blockThirdParty().click();
     await flushTasks();
     assertEquals(
         page.getPref('profile.cookie_controls_mode.value'),
         CookieControlsMode.BLOCK_THIRD_PARTY);
-    assertEquals(0, testMetricsBrowserProxy.getCallCount('recordAction'));
+    assertFalse(page.$.toast.open);
+
+    // Reset the state to show the toast.
+    page.set('prefs.privacy_sandbox.m1.topics_enabled.value', true);
+    page.set(
+        'prefs.profile.cookie_controls_mode.value',
+        CookieControlsMode.INCOGNITO_ONLY);
+    blockThirdParty().click();
+    await eventToPromise('selected-changed', primarySettingGroup());
+    await flushTasks();
+    assertEquals(
+        page.getPref('profile.cookie_controls_mode.value'),
+        CookieControlsMode.BLOCK_THIRD_PARTY);
+    // TODO(crbug.com/40244046): Check histograms.
+    assertTrue(page.$.toast.open);
+
+    // Reselecting a non-3P cookie blocking setting should hide the toast.
+    allowThirdParty().click();
+    await eventToPromise('selected-changed', primarySettingGroup());
+    await flushTasks();
+    assertEquals(
+        page.getPref('profile.cookie_controls_mode.value'),
+        CookieControlsMode.OFF);
+    assertFalse(page.$.toast.open);
+
+    // Navigating away from the page should hide the toast, even if navigated
+    // back to.
+    blockThirdParty().click();
+    await eventToPromise('selected-changed', primarySettingGroup());
+    await flushTasks();
+    assertEquals(
+        page.getPref('profile.cookie_controls_mode.value'),
+        CookieControlsMode.BLOCK_THIRD_PARTY);
+    assertTrue(page.$.toast.open);
+    Router.getInstance().navigateTo(routes.BASIC);
+    Router.getInstance().navigateTo(routes.COOKIES);
+    await flushTasks();
+    assertFalse(page.$.toast.open);
+  });
+
+  test('privacySandboxToast_alwaysBlock3pcsIncognito', async function() {
+    loadTimeData.overrideValues({
+      isAlwaysBlock3pcsIncognitoEnabled: true,
+      is3pcdCookieSettingsRedesignEnabled: false,
+      isPrivacySandboxRestricted: false,
+    });
+    assertFalse(page.$.toast.open);
+
+    // Disabling third-party cookies should display the privacy sandbox toast.
+    page.set(
+        'prefs.generated.third_party_cookie_blocking_setting.value',
+        ThirdPartyCookieBlockingSetting.INCOGNITO_ONLY);
+    blockAll3pc().click();
+    await eventToPromise(
+        'selected-changed', thirdPartyCookieBlockingSettingGroup());
+    await flushTasks();
+    assertEquals(
+        page.getPref('generated.third_party_cookie_blocking_setting.value'),
+        ThirdPartyCookieBlockingSetting.BLOCK_THIRD_PARTY);
+    assertTrue(page.$.toast.open);
+
+    // Re-enabling 3P cookies for regular sessions should not display the toast.
+    block3pcIncognito().click();
+    await eventToPromise(
+        'selected-changed', thirdPartyCookieBlockingSettingGroup());
+    await flushTasks();
+    assertEquals(
+        page.getPref('generated.third_party_cookie_blocking_setting.value'),
+        ThirdPartyCookieBlockingSetting.INCOGNITO_ONLY);
+    assertFalse(page.$.toast.open);
+
+    // The toast should not be displayed if the user has any privacy sandbox
+    // APIs disabled.
+    page.set('prefs.privacy_sandbox.m1.topics_enabled.value', false);
+    blockAll3pc().click();
+    await flushTasks();
+    assertEquals(
+        page.getPref('generated.third_party_cookie_blocking_setting.value'),
+        ThirdPartyCookieBlockingSetting.BLOCK_THIRD_PARTY);
+    assertFalse(page.$.toast.open);
+
+    // Reset the state to show the toast.
+    page.set('prefs.privacy_sandbox.m1.topics_enabled.value', true);
+    page.set(
+        'prefs.generated.third_party_cookie_blocking_setting.value',
+        ThirdPartyCookieBlockingSetting.INCOGNITO_ONLY);
+    blockAll3pc().click();
+    await eventToPromise(
+        'selected-changed', thirdPartyCookieBlockingSettingGroup());
+    await flushTasks();
+    assertEquals(
+        page.getPref('generated.third_party_cookie_blocking_setting.value'),
+        ThirdPartyCookieBlockingSetting.BLOCK_THIRD_PARTY);
+    assertTrue(page.$.toast.open);
+
+    // Reselecting a non-3P cookie blocking setting should hide the toast.
+    block3pcIncognito().click();
+    await eventToPromise(
+        'selected-changed', thirdPartyCookieBlockingSettingGroup());
+    await flushTasks();
+    assertEquals(
+        page.getPref('generated.third_party_cookie_blocking_setting.value'),
+        ThirdPartyCookieBlockingSetting.INCOGNITO_ONLY);
+    assertFalse(page.$.toast.open);
+  });
+
+  test('privacySandboxToast_restrictedSandbox', async function() {
+    // No toast should be shown if the privacy sandbox is restricted
+    loadTimeData.overrideValues({isPrivacySandboxRestricted: true});
+    resetRouterForTesting();
+
+    page.set('prefs.privacy_sandbox.m1.topics_enabled.value', true);
+    blockThirdParty().click();
+    assertEquals(
+        'Settings.PrivacySandbox.Block3PCookies',
+        await testMetricsBrowserProxy.whenCalled('recordAction'));
+    testMetricsBrowserProxy.resetResolver('recordAction');
+    assertFalse(page.$.toast.open);
   });
 
   test('disabledRWSToggle', async () => {