From d08987cd4ddd6dea65e1ea1cc3c62bd65e20aa98 Mon Sep 17 00:00:00 2001 From: Emerick Rogul Date: Tue, 21 Jan 2025 14:27:08 -0500 Subject: [PATCH] Allow deep linking to default search engine dialogs (uplift to 1.75.x) (#27273) Merge pull request #27223 from brave/emerick-settings-search-engine-deep-linking Allow deep linking to default search engine dialogs --- .../settings/brave_overrides/config.ts | 11 ++--- .../settings/brave_overrides/search_page.ts | 45 +++++++++++++++++-- browser/resources/settings/brave_routes.ts | 8 +++- .../brave_search_engines_page.ts | 14 ++++-- ...browser-resources-settings-router.ts.patch | 4 +- 5 files changed, 68 insertions(+), 14 deletions(-) diff --git a/browser/resources/settings/brave_overrides/config.ts b/browser/resources/settings/brave_overrides/config.ts index 3e124f298b0c..2f79545bc787 100644 --- a/browser/resources/settings/brave_overrides/config.ts +++ b/browser/resources/settings/brave_overrides/config.ts @@ -10,11 +10,12 @@ import {RegisterPolymerComponentToIgnore} from 'chrome://resources/brave/polymer // 1. Make sure we ignore the chromium components when they are defined // 2. Override the chromium components with a subclass and define the // components with their original chromium name. -// (This is because the chomium components define themselves via customElements.define -// in their module, so we want to register to ignore the component before the module -// is imported). +// (This is because the chomium components define themselves via +// customElements.define in their module, so we want to register to ignore the +// component before the module is imported). RegisterPolymerComponentToIgnore('add-site-dialog') -RegisterPolymerComponentToIgnore('settings-site-settings-page') -RegisterPolymerComponentToIgnore('settings-clear-browsing-data-dialog') RegisterPolymerComponentToIgnore('settings-autofill-page') +RegisterPolymerComponentToIgnore('settings-clear-browsing-data-dialog') +RegisterPolymerComponentToIgnore('settings-search-page') +RegisterPolymerComponentToIgnore('settings-site-settings-page') diff --git a/browser/resources/settings/brave_overrides/search_page.ts b/browser/resources/settings/brave_overrides/search_page.ts index 946041840d95..aa61863279be 100644 --- a/browser/resources/settings/brave_overrides/search_page.ts +++ b/browser/resources/settings/brave_overrides/search_page.ts @@ -5,15 +5,23 @@ import '../brave_search_engines_page/brave_search_engines_page.js' -import '../brave_search_engines_page/private_search_engine_list_dialog.js'; +import '../brave_search_engines_page/private_search_engine_list_dialog.js' -import {RegisterPolymerTemplateModifications} from 'chrome://resources/brave/polymer_overriding.js' +import { + RegisterPolymerComponentReplacement, + RegisterPolymerTemplateModifications +} from 'chrome://resources/brave/polymer_overriding.js' + +import {SettingsSearchPageElement} from '../search_page/search_page.js' +import {routes} from '../route.js' +import {type Route, RouteObserverMixin, Router} from '../router.js' import {getTrustedHTML} from 'chrome://resources/js/static_types.js' import {loadTimeData} from '../i18n_setup.js' RegisterPolymerTemplateModifications({ 'settings-search-page': (templateContent) => { - const enginesSubpageTrigger = templateContent.getElementById('enginesSubpageTrigger') + const enginesSubpageTrigger = + templateContent.getElementById('enginesSubpageTrigger') if (!enginesSubpageTrigger) { console.error(`[Settings] Couldn't find enginesSubpageTrigger`) } else { @@ -34,3 +42,34 @@ RegisterPolymerTemplateModifications({ } } }) + +// Subclass and replace the settings-search-page to make the default search +// dialog navigable via deep linking +RegisterPolymerComponentReplacement( + 'settings-search-page', + class BraveSettingsSearchPageElement + extends RouteObserverMixin(SettingsSearchPageElement) { + override ready() { + super.ready() + + // onOpenDialogButtonClick_ is private in the superclass, so we have to + // replace it to change its functionality + const anyThis = this as any + anyThis.onOpenDialogButtonClick_ = () => { + Router.getInstance().navigateTo(routes.DEFAULT_SEARCH) + } + + // onSearcgEngineListDialogClose_ is private in the superclass, so we have + // to replace it to change its functionality + anyThis.onSearchEngineListDialogClose_ = () => { + Router.getInstance().navigateTo(routes.SEARCH) + } + } + + override currentRouteChanged() { + const anyThis = this as any + anyThis.showSearchEngineListDialog_ = + Router.getInstance().getCurrentRoute() === routes.DEFAULT_SEARCH + } + } +) diff --git a/browser/resources/settings/brave_routes.ts b/browser/resources/settings/brave_routes.ts index 3254409c6959..18d3677c3ca5 100644 --- a/browser/resources/settings/brave_routes.ts +++ b/browser/resources/settings/brave_routes.ts @@ -59,7 +59,13 @@ export default function addBraveRoutes(r: Partial) { if (r.FONTS) { delete r.FONTS } - r.FONTS = r.BRAVE_CONTENT.createChild('/fonts'); + r.FONTS = r.BRAVE_CONTENT.createChild('/fonts') + } + if (r.SEARCH) { + r.DEFAULT_SEARCH = r.SEARCH.createChild('defaultSearch') + r.DEFAULT_SEARCH.isNavigableDialog = true + r.PRIVATE_SEARCH = r.SEARCH.createChild('privateSearch') + r.PRIVATE_SEARCH.isNavigableDialog = true } if (r.SITE_SETTINGS) { r.SITE_SETTINGS_AUTOPLAY = r.SITE_SETTINGS.createChild('autoplay') diff --git a/browser/resources/settings/brave_search_engines_page/brave_search_engines_page.ts b/browser/resources/settings/brave_search_engines_page/brave_search_engines_page.ts index 5def1cf56b10..d86215c0200e 100644 --- a/browser/resources/settings/brave_search_engines_page/brave_search_engines_page.ts +++ b/browser/resources/settings/brave_search_engines_page/brave_search_engines_page.ts @@ -9,13 +9,16 @@ import '../settings_vars.css.js' import {PolymerElement} from 'chrome://resources/polymer/v3_0/polymer/polymer_bundled.min.js' import {WebUiListenerMixin} from 'chrome://resources/cr_elements/web_ui_listener_mixin.js' import {I18nMixin} from 'chrome://resources/cr_elements/i18n_mixin.js' +import {RouteObserverMixin, Router} from '../router.js' +import {routes} from '../route.js' import {loadTimeData} from '../i18n_setup.js' import {BraveSearchEnginesPageBrowserProxyImpl} from './brave_search_engines_page_browser_proxy.js' import {getTemplate} from './brave_search_engines_page.html.js' import type {CrToastElement} from 'chrome://resources/cr_elements/cr_toast/cr_toast.js' import type {SearchEngine} from '../search_engines_page/search_engines_browser_proxy.js' -const BraveSearchEnginesPageBase = WebUiListenerMixin(I18nMixin(PolymerElement)) +const BraveSearchEnginesPageBase = + WebUiListenerMixin(I18nMixin(RouteObserverMixin(PolymerElement))) class BraveSearchEnginesPage extends BraveSearchEnginesPageBase { static get is() { @@ -68,12 +71,17 @@ class BraveSearchEnginesPage extends BraveSearchEnginesPageBase { 'private-search-engines-changed', updatePrivateSearchEngines) } + override currentRouteChanged() { + this.showPrivateSearchEngineListDialog_ = + Router.getInstance().getCurrentRoute() === routes.PRIVATE_SEARCH + } + private onOpenPrivateDialogButtonClick_() { - this.showPrivateSearchEngineListDialog_ = true + Router.getInstance().navigateTo(routes.PRIVATE_SEARCH) } private onPrivateSearchEngineListDialogClose_() { - this.showPrivateSearchEngineListDialog_ = false + Router.getInstance().navigateTo(routes.SEARCH) } private onPrivateSearchEngineChangedInDialog_(e: CustomEvent) { diff --git a/patches/chrome-browser-resources-settings-router.ts.patch b/patches/chrome-browser-resources-settings-router.ts.patch index 42e20f046a85..92097debed06 100644 --- a/patches/chrome-browser-resources-settings-router.ts.patch +++ b/patches/chrome-browser-resources-settings-router.ts.patch @@ -1,12 +1,12 @@ diff --git a/chrome/browser/resources/settings/router.ts b/chrome/browser/resources/settings/router.ts -index afe6eb3c611c80833e7ceaa32368a45cf8f2dcad..f714d77dd58abb5aafa3495c7e48ff9f71431a98 100644 +index afe6eb3c611c80833e7ceaa32368a45cf8f2dcad..7cdd33a762c5be031e45e2d97bbdd867663cb293 100644 --- a/chrome/browser/resources/settings/router.ts +++ b/chrome/browser/resources/settings/router.ts @@ -121,6 +121,7 @@ export interface SettingsRoutes { IMPORT_DATA: Route; SIGN_OUT: Route; // -+ GET_STARTED: Route; SHIELDS: Route; SOCIAL_BLOCKING: Route; EXTENSIONS: Route; EXTENSIONS_V2: Route; BRAVE_SYNC: Route; BRAVE_SYNC_SETUP: Route; BRAVE_WALLET: Route; BRAVE_WEB3: Route; BRAVE_NEW_TAB: Route; THEMES: Route; SITE_SETTINGS_AUTOPLAY: Route; SITE_SETTINGS_GOOGLE_SIGN_IN: Route; SITE_SETTINGS_LOCALHOST_ACCESS: Route; SITE_SETTINGS_BRAVE_OPEN_AI_CHAT: Route; SITE_SETTINGS_ETHEREUM: Route; SITE_SETTINGS_SOLANA: Route; SITE_SETTINGS_SHIELDS_STATUS: Route; SHIELDS_ADBLOCK: Route; SHORTCUTS: Route; BRAVE_WALLET_NETWORKS: Route; BRAVE_LEO_ASSISTANT: Route; BRAVE_CONTENT: Route; ++ GET_STARTED: Route; SHIELDS: Route; SOCIAL_BLOCKING: Route; EXTENSIONS: Route; EXTENSIONS_V2: Route; BRAVE_SYNC: Route; BRAVE_SYNC_SETUP: Route; BRAVE_WALLET: Route; BRAVE_WEB3: Route; BRAVE_NEW_TAB: Route; THEMES: Route; SITE_SETTINGS_AUTOPLAY: Route; SITE_SETTINGS_GOOGLE_SIGN_IN: Route; SITE_SETTINGS_LOCALHOST_ACCESS: Route; SITE_SETTINGS_BRAVE_OPEN_AI_CHAT: Route; SITE_SETTINGS_ETHEREUM: Route; SITE_SETTINGS_SOLANA: Route; SITE_SETTINGS_SHIELDS_STATUS: Route; SHIELDS_ADBLOCK: Route; SHORTCUTS: Route; BRAVE_WALLET_NETWORKS: Route; BRAVE_LEO_ASSISTANT: Route; BRAVE_CONTENT: Route; DEFAULT_SEARCH: Route; PRIVATE_SEARCH: Route; } /** Class for navigable routes. */