|
| 1 | +/* Copyright (c) 2020 The Brave Authors. All rights reserved. |
| 2 | + * This Source Code Form is subject to the terms of the Mozilla Public |
| 3 | + * License, v. 2.0. If a copy of the MPL was not distributed with this file, |
| 4 | + * You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 5 | + |
| 6 | +#include "brave/browser/ui/startup/default_brave_browser_prompt.h" |
| 7 | + |
| 8 | +#include <string> |
| 9 | + |
| 10 | +#include "base/bind.h" |
| 11 | +#include "base/logging.h" |
| 12 | +#include "base/version.h" |
| 13 | +#include "brave/browser/ui/browser_dialogs.h" |
| 14 | +#include "brave/common/pref_names.h" |
| 15 | +#include "chrome/browser/browser_process.h" |
| 16 | +#include "chrome/browser/first_run/first_run.h" |
| 17 | +#include "chrome/browser/profiles/profile.h" |
| 18 | +#include "chrome/browser/profiles/profile_manager.h" |
| 19 | +#include "chrome/browser/shell_integration.h" |
| 20 | +#include "chrome/browser/ui/browser.h" |
| 21 | +#include "chrome/browser/ui/browser_list.h" |
| 22 | +#include "chrome/browser/ui/startup/default_browser_prompt.h" |
| 23 | +#include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 24 | +#include "chrome/common/pref_names.h" |
| 25 | +#include "components/prefs/pref_registry_simple.h" |
| 26 | +#include "components/prefs/pref_service.h" |
| 27 | +#include "components/version_info/version_info.h" |
| 28 | +#include "content/public/browser/web_contents.h" |
| 29 | + |
| 30 | +// Many code in this file is copied from default_browser_prompt.cc |
| 31 | +namespace { |
| 32 | + |
| 33 | +void IncreaseBrowserLaunchCount(Profile* profile) { |
| 34 | + const int current_count = |
| 35 | + profile->GetPrefs()->GetInteger(kDefaultBrowserLaunchingCount); |
| 36 | + |
| 37 | + // Don't need to record more because we don't show prompt after 20th launch. |
| 38 | + if (current_count >= 20) |
| 39 | + return; |
| 40 | + |
| 41 | + profile->GetPrefs()->SetInteger(kDefaultBrowserLaunchingCount, |
| 42 | + current_count + 1); |
| 43 | +} |
| 44 | + |
| 45 | +void ShowPrompt() { |
| 46 | + // Show the default browser request prompt in the most recently active, |
| 47 | + // visible, tabbed browser. Do not show the prompt if no such browser exists. |
| 48 | + BrowserList* browser_list = BrowserList::GetInstance(); |
| 49 | + for (auto browser_iterator = browser_list->begin_last_active(); |
| 50 | + browser_iterator != browser_list->end_last_active(); |
| 51 | + ++browser_iterator) { |
| 52 | + Browser* browser = *browser_iterator; |
| 53 | + |
| 54 | + // |browser| may be null in UI tests. Also, don't show the prompt in an app |
| 55 | + // window, which is not meant to be treated as a Chrome window. |
| 56 | + if (!browser || browser->deprecated_is_app()) |
| 57 | + continue; |
| 58 | + |
| 59 | + // In ChromeBot tests, there might be a race. This line appears to get |
| 60 | + // called during shutdown and the active web contents can be nullptr. |
| 61 | + content::WebContents* web_contents = |
| 62 | + browser->tab_strip_model()->GetActiveWebContents(); |
| 63 | + if (!web_contents || |
| 64 | + web_contents->GetVisibility() != content::Visibility::VISIBLE) { |
| 65 | + continue; |
| 66 | + } |
| 67 | + |
| 68 | + // Never show the default browser prompt over the first run promos. |
| 69 | + // TODO(pmonette): The whole logic that determines when to show the default |
| 70 | + // browser prompt is due for a refactor. ShouldShowDefaultBrowserPrompt() |
| 71 | + // should be aware of the first run promos and return false instead of |
| 72 | + // counting on the early return here. See bug crbug.com/693292. |
| 73 | + if (first_run::IsOnWelcomePage(web_contents)) |
| 74 | + continue; |
| 75 | + |
| 76 | + // Launch dialog. |
| 77 | + brave::ShowDefaultBrowserDialog(browser); |
| 78 | + break; |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +void ResetCheckDefaultBrowserPref(const base::FilePath& profile_path) { |
| 83 | + Profile* profile = |
| 84 | + g_browser_process->profile_manager()->GetProfileByPath(profile_path); |
| 85 | + if (profile) |
| 86 | + ResetDefaultBraveBrowserPrompt(profile); |
| 87 | +} |
| 88 | + |
| 89 | +void OnCheckIsDefaultBrowserFinished( |
| 90 | + const base::FilePath& profile_path, |
| 91 | + bool show_prompt, |
| 92 | + shell_integration::DefaultWebClientState state) { |
| 93 | + if (state == shell_integration::IS_DEFAULT) { |
| 94 | + // Notify the user in the future if Chrome ceases to be the user's chosen |
| 95 | + // default browser. |
| 96 | + ResetCheckDefaultBrowserPref(profile_path); |
| 97 | + } else if (show_prompt && state == shell_integration::NOT_DEFAULT && |
| 98 | + shell_integration::CanSetAsDefaultBrowser()) { |
| 99 | + // Only show the prompt if some other program is the user's default browser. |
| 100 | + // In particular, don't show it if another install mode is default (e.g., |
| 101 | + // don't prompt for Chrome Beta if stable Chrome is the default). |
| 102 | + ShowPrompt(); |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +// Returns true if the default browser prompt should be shown if Chrome is not |
| 107 | +// the user's default browser. |
| 108 | +bool ShouldShowDefaultBrowserPrompt(Profile* profile) { |
| 109 | + // Do not show the prompt if "suppress_default_browser_prompt_for_version" in |
| 110 | + // the initial preferences is set to the current version. |
| 111 | + const std::string disable_version_string = |
| 112 | + g_browser_process->local_state()->GetString( |
| 113 | + prefs::kBrowserSuppressDefaultBrowserPrompt); |
| 114 | + const base::Version disable_version(disable_version_string); |
| 115 | + DCHECK(disable_version_string.empty() || disable_version.IsValid()); |
| 116 | + if (disable_version.IsValid() && |
| 117 | + disable_version == version_info::GetVersion()) { |
| 118 | + return false; |
| 119 | + } |
| 120 | + |
| 121 | + const int current_count = |
| 122 | + profile->GetPrefs()->GetInteger(kDefaultBrowserLaunchingCount); |
| 123 | + |
| 124 | + // We only show prompt at 2nd, 3rd, 4th, 8th and 20th. |
| 125 | + // This is not called at first run. So, count 1 is second run. |
| 126 | + if (current_count == 1 || current_count == 2 || current_count == 3 || |
| 127 | + current_count == 7 || current_count == 19) |
| 128 | + return true; |
| 129 | + |
| 130 | + return false; |
| 131 | +} |
| 132 | + |
| 133 | +} // namespace |
| 134 | + |
| 135 | +void ShowDefaultBraveBrowserPrompt(Profile* profile) { |
| 136 | + // Do not check if Chrome is the default browser if there is a policy in |
| 137 | + // control of this setting. |
| 138 | + if (g_browser_process->local_state()->IsManagedPreference( |
| 139 | + prefs::kDefaultBrowserSettingEnabled)) { |
| 140 | + // Handling of the browser.default_browser_setting_enabled policy setting is |
| 141 | + // taken care of in BrowserProcessImpl. |
| 142 | + return; |
| 143 | + } |
| 144 | + |
| 145 | + PrefService* prefs = profile->GetPrefs(); |
| 146 | + // Reset preferences if kResetCheckDefaultBrowser is true. |
| 147 | + if (prefs->GetBoolean(prefs::kResetCheckDefaultBrowser)) { |
| 148 | + prefs->SetBoolean(prefs::kResetCheckDefaultBrowser, false); |
| 149 | + ResetDefaultBraveBrowserPrompt(profile); |
| 150 | + } |
| 151 | + |
| 152 | + IncreaseBrowserLaunchCount(profile); |
| 153 | + |
| 154 | + scoped_refptr<shell_integration::DefaultBrowserWorker>( |
| 155 | + new shell_integration::DefaultBrowserWorker()) |
| 156 | + ->StartCheckIsDefault( |
| 157 | + base::BindOnce(&OnCheckIsDefaultBrowserFinished, profile->GetPath(), |
| 158 | + ShouldShowDefaultBrowserPrompt(profile))); |
| 159 | +} |
| 160 | + |
| 161 | +void ResetDefaultBraveBrowserPrompt(Profile* profile) { |
| 162 | + profile->GetPrefs()->ClearPref(kDefaultBrowserLaunchingCount); |
| 163 | +} |
| 164 | + |
| 165 | +void RegisterDefaultBraveBrowserPromptPrefs(PrefRegistrySimple* registry) { |
| 166 | + registry->RegisterIntegerPref(kDefaultBrowserLaunchingCount, 0); |
| 167 | +} |
0 commit comments