From 8f23aa3e2a7ff981d8d0db99a4bc04818a8f435d Mon Sep 17 00:00:00 2001 From: Harry Twyford Date: Wed, 18 Mar 2020 21:47:17 +0000 Subject: [PATCH] Bug 1622195 - Disable Search Tips if the default browser prompt will be shown. r=adw Differential Revision: https://phabricator.services.mozilla.com/D67034 --HG-- extra : moz-landing-system : lando --- browser/components/BrowserGlue.jsm | 208 ++++++++++-------- .../urlbar/UrlbarProviderSearchTips.jsm | 11 + .../tests/browser-tips/browser_searchTips.js | 5 +- 3 files changed, 133 insertions(+), 91 deletions(-) diff --git a/browser/components/BrowserGlue.jsm b/browser/components/BrowserGlue.jsm index bda4b395900e..42707ab10c06 100644 --- a/browser/components/BrowserGlue.jsm +++ b/browser/components/BrowserGlue.jsm @@ -2,7 +2,11 @@ * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ -var EXPORTED_SYMBOLS = ["BrowserGlue", "ContentPermissionPrompt"]; +var EXPORTED_SYMBOLS = [ + "BrowserGlue", + "ContentPermissionPrompt", + "DefaultBrowserCheck", +]; const XULNS = "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"; @@ -2161,7 +2165,7 @@ BrowserGlue.prototype = { { task: () => { - this._checkForDefaultBrowser(); + this._maybeShowDefaultBrowserPrompt(); }, }, @@ -3428,96 +3432,15 @@ BrowserGlue.prototype = { Services.prefs.setIntPref("browser.migration.version", UI_VERSION); }, - _checkForDefaultBrowser() { - // Perform default browser checking. - if (!ShellService) { + _maybeShowDefaultBrowserPrompt() { + const willPrompt = DefaultBrowserCheck.willCheckDefaultBrowser( + /* isStartupCheck */ true + ); + if (!willPrompt) { return; } - let shouldCheck = - !AppConstants.DEBUG && ShellService.shouldCheckDefaultBrowser; - - const skipDefaultBrowserCheck = - Services.prefs.getBoolPref( - "browser.shell.skipDefaultBrowserCheckOnFirstRun" - ) && - !Services.prefs.getBoolPref( - "browser.shell.didSkipDefaultBrowserCheckOnFirstRun" - ); - - const usePromptLimit = !AppConstants.RELEASE_OR_BETA; - let promptCount = usePromptLimit - ? Services.prefs.getIntPref("browser.shell.defaultBrowserCheckCount") - : 0; - - let willRecoverSession = - SessionStartup.sessionType == SessionStartup.RECOVER_SESSION; - - // startup check, check all assoc - let isDefault = false; - let isDefaultError = false; - try { - isDefault = ShellService.isDefaultBrowser(true, false); - } catch (ex) { - isDefaultError = true; - } - - if (isDefault) { - let now = Math.floor(Date.now() / 1000).toString(); - Services.prefs.setCharPref( - "browser.shell.mostRecentDateSetAsDefault", - now - ); - } - - let willPrompt = shouldCheck && !isDefault && !willRecoverSession; - - // Skip the "Set Default Browser" check during first-run or after the - // browser has been run a few times. - if (willPrompt) { - if (skipDefaultBrowserCheck) { - Services.prefs.setBoolPref( - "browser.shell.didSkipDefaultBrowserCheckOnFirstRun", - true - ); - willPrompt = false; - } else { - promptCount++; - } - if (usePromptLimit && promptCount > 3) { - willPrompt = false; - } - } - - if (usePromptLimit && willPrompt) { - Services.prefs.setIntPref( - "browser.shell.defaultBrowserCheckCount", - promptCount - ); - } - - try { - // Report default browser status on startup to telemetry - // so we can track whether we are the default. - Services.telemetry - .getHistogramById("BROWSER_IS_USER_DEFAULT") - .add(isDefault); - Services.telemetry - .getHistogramById("BROWSER_IS_USER_DEFAULT_ERROR") - .add(isDefaultError); - Services.telemetry - .getHistogramById("BROWSER_SET_DEFAULT_ALWAYS_CHECK") - .add(shouldCheck); - Services.telemetry - .getHistogramById("BROWSER_SET_DEFAULT_DIALOG_PROMPT_RAWCOUNT") - .add(promptCount); - } catch (ex) { - /* Don't break the default prompt if telemetry is broken. */ - } - - if (willPrompt) { - DefaultBrowserCheck.prompt(BrowserWindowTracker.getTopWindow()); - } + DefaultBrowserCheck.prompt(BrowserWindowTracker.getTopWindow()); }, async _migrateMatchBucketsPrefForUI66() { @@ -4505,6 +4428,113 @@ var DefaultBrowserCheck = { delete this._notification; } }, + + /** + * Checks if the default browser check prompt will be shown. + * @param {boolean} isStartupCheck + * If true, prefs will be set and telemetry will be recorded. + * @returns {boolean} True if the default browser check prompt will be shown. + */ + willCheckDefaultBrowser(isStartupCheck) { + // Perform default browser checking. + if (!ShellService) { + return false; + } + + let shouldCheck = + !AppConstants.DEBUG && ShellService.shouldCheckDefaultBrowser; + + // Even if we shouldn't check the default browser, we still continue when + // isStartupCheck = true to set prefs and telemetry. + if (!shouldCheck && !isStartupCheck) { + return false; + } + + // Skip the "Set Default Browser" check during first-run or after the + // browser has been run a few times. + const skipDefaultBrowserCheck = + Services.prefs.getBoolPref( + "browser.shell.skipDefaultBrowserCheckOnFirstRun" + ) && + !Services.prefs.getBoolPref( + "browser.shell.didSkipDefaultBrowserCheckOnFirstRun" + ); + + const usePromptLimit = !AppConstants.RELEASE_OR_BETA; + let promptCount = usePromptLimit + ? Services.prefs.getIntPref("browser.shell.defaultBrowserCheckCount") + : 0; + + let willRecoverSession = + SessionStartup.sessionType == SessionStartup.RECOVER_SESSION; + + // Don't show the prompt if we're already the default browser. + let isDefault = false; + let isDefaultError = false; + try { + isDefault = ShellService.isDefaultBrowser(isStartupCheck, false); + } catch (ex) { + isDefaultError = true; + } + + if (isDefault && isStartupCheck) { + let now = Math.floor(Date.now() / 1000).toString(); + Services.prefs.setCharPref( + "browser.shell.mostRecentDateSetAsDefault", + now + ); + } + + let willPrompt = shouldCheck && !isDefault && !willRecoverSession; + + if (willPrompt) { + if (skipDefaultBrowserCheck) { + if (isStartupCheck) { + Services.prefs.setBoolPref( + "browser.shell.didSkipDefaultBrowserCheckOnFirstRun", + true + ); + } + willPrompt = false; + } + + if (usePromptLimit) { + promptCount++; + if (isStartupCheck) { + Services.prefs.setIntPref( + "browser.shell.defaultBrowserCheckCount", + promptCount + ); + } + if (promptCount > 3) { + willPrompt = false; + } + } + } + + if (isStartupCheck) { + try { + // Report default browser status on startup to telemetry + // so we can track whether we are the default. + Services.telemetry + .getHistogramById("BROWSER_IS_USER_DEFAULT") + .add(isDefault); + Services.telemetry + .getHistogramById("BROWSER_IS_USER_DEFAULT_ERROR") + .add(isDefaultError); + Services.telemetry + .getHistogramById("BROWSER_SET_DEFAULT_ALWAYS_CHECK") + .add(shouldCheck); + Services.telemetry + .getHistogramById("BROWSER_SET_DEFAULT_DIALOG_PROMPT_RAWCOUNT") + .add(promptCount); + } catch (ex) { + /* Don't break the default prompt if telemetry is broken. */ + } + } + + return willPrompt; + }, }; /* diff --git a/browser/components/urlbar/UrlbarProviderSearchTips.jsm b/browser/components/urlbar/UrlbarProviderSearchTips.jsm index 293fe080ca28..e41f734ca84c 100644 --- a/browser/components/urlbar/UrlbarProviderSearchTips.jsm +++ b/browser/components/urlbar/UrlbarProviderSearchTips.jsm @@ -17,6 +17,7 @@ const { XPCOMUtils } = ChromeUtils.import( XPCOMUtils.defineLazyModuleGetters(this, { AppMenuNotifications: "resource://gre/modules/AppMenuNotifications.jsm", + DefaultBrowserCheck: "resource:///modules/BrowserGlue.jsm", BrowserWindowTracker: "resource:///modules/BrowserWindowTracker.jsm", Log: "resource://gre/modules/Log.jsm", ProfileAge: "resource://gre/modules/ProfileAge.jsm", @@ -417,6 +418,16 @@ function isBrowserShowingNotification() { } } + // On startup, the default browser check normally opens after the Search Tip. + // As a result, we can't check for the prompt's presence, but we can check if + // it plans on opening. + const willPrompt = DefaultBrowserCheck.willCheckDefaultBrowser( + /* isStartupCheck */ false + ); + if (willPrompt) { + return true; + } + return false; } diff --git a/browser/components/urlbar/tests/browser-tips/browser_searchTips.js b/browser/components/urlbar/tests/browser-tips/browser_searchTips.js index b15ead419006..849414e0278b 100644 --- a/browser/components/urlbar/tests/browser-tips/browser_searchTips.js +++ b/browser/components/urlbar/tests/browser-tips/browser_searchTips.js @@ -4,8 +4,9 @@ // Tests the Search Tips feature, which displays a prompt to use the Urlbar on // the newtab page and on the user's default search engine's homepage. // Specifically, it tests that the Tips appear when they should be appearing. -// This doesn't test the max-shown-count limit because it requires restarting -// the browser. +// This doesn't test the max-shown-count limit or the restriction on tips when +// we show the default browser prompt because those require restarting the +// browser. "use strict";