Skip to content

Commit 8680aaf

Browse files
committed
Show default browser dialog instead of infobar
Browser will launch window modal dialog at 2nd, 3rd, 4th, 8th and 20th launching if browser is not a default. If browser is changed from default to non-default, browser will launch dialog again with same frequency. fix brave/brave-browser#12203
1 parent 5a36d78 commit 8680aaf

12 files changed

+447
-0
lines changed

app/brave_generated_resources.grd

+15
Original file line numberDiff line numberDiff line change
@@ -929,6 +929,21 @@ By installing this extension, you are agreeing to the Google Widevine Terms of U
929929
<message name="IDS_BRAVE_AVATAR_LABEL_89" desc="The label for the avatar_bonbon_ninja.png avatar.">
930930
Avatar Bonbon Ninja
931931
</message>
932+
<!-- Brave default browser dialog -->
933+
<message name="IDS_BRAVE_DEFAULT_BROWSER_DIALOG_HEADER_TEXT" desc="The header text for default browser dialog">
934+
Set Brave as your default browser
935+
</message>
936+
<message name="IDS_BRAVE_DEFAULT_BROWSER_DIALOG_CONTENTS_TEXT" desc="The contents for default browser dialog">
937+
Brave browser is currently not set as your default browser.
938+
Set Brave as your default to keep browsing the web faster -
939+
up to 6x faster on major news sites.
940+
</message>
941+
<message name="IDS_BRAVE_DEFAULT_BROWSER_DIALOG_OK_BUTTON_LABEL" desc="The text for default browser dialog ok button">
942+
Set as default
943+
</message>
944+
<message name="IDS_BRAVE_DEFAULT_BROWSER_DIALOG_CANCEL_BUTTON_LABEL" desc="The text for default browser dialog cancel button">
945+
Not now
946+
</message>
932947
</if>
933948
<!-- P3A -->
934949
<message name="IDS_BRAVE_CONFIRM_P3A_INFO_BAR" desc="Text that shows on the info bar where user acknowledges the P3A message">

browser/brave_profile_prefs.cc

+5
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,10 @@
9999
#include "components/translate/core/browser/translate_pref_names.h"
100100
#endif
101101

102+
#if !defined(OS_ANDROID)
103+
#include "brave/browser/ui/startup/default_brave_browser_prompt.h"
104+
#endif
105+
102106
using extensions::FeatureSwitch;
103107

104108
namespace brave {
@@ -339,6 +343,7 @@ void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry) {
339343
// We can turn customization mode on when we have add-shortcut feature.
340344
registry->SetDefaultPrefValue(prefs::kNtpUseMostVisitedTiles,
341345
base::Value(true));
346+
RegisterDefaultBraveBrowserPromptPrefs(registry);
342347
#endif
343348

344349
RegisterProfilePrefsForMigration(registry);

browser/ui/BUILD.gn

+5
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ source_set("ui") {
6666
"brave_view_ids.h",
6767
"browser_commands.cc",
6868
"browser_commands.h",
69+
"browser_dialogs.h",
6970
"content_settings/brave_autoplay_blocked_image_model.cc",
7071
"content_settings/brave_autoplay_blocked_image_model.h",
7172
"content_settings/brave_autoplay_content_setting_bubble_model.cc",
@@ -74,6 +75,8 @@ source_set("ui") {
7475
"content_settings/brave_content_setting_image_models.h",
7576
"omnibox/brave_omnibox_client_impl.cc",
7677
"omnibox/brave_omnibox_client_impl.h",
78+
"startup/default_brave_browser_prompt.cc",
79+
"startup/default_brave_browser_prompt.h",
7780
"toolbar/brave_app_menu_model.cc",
7881
"toolbar/brave_app_menu_model.h",
7982
"toolbar/brave_recent_tabs_sub_menu_model.h",
@@ -118,6 +121,8 @@ source_set("ui") {
118121
sources += [
119122
"views/bookmarks/bookmark_bar_instructions_view.cc",
120123
"views/bookmarks/bookmark_bar_instructions_view.h",
124+
"views/brave_default_browser_dialog_view.cc",
125+
"views/brave_default_browser_dialog_view.h",
121126
"views/brave_layout_provider.cc",
122127
"views/brave_layout_provider.h",
123128
"views/download/brave_download_item_view.cc",

browser/ui/browser_dialogs.h

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
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+
#ifndef BRAVE_BROWSER_UI_BROWSER_DIALOGS_H_
7+
#define BRAVE_BROWSER_UI_BROWSER_DIALOGS_H_
8+
9+
class Browser;
10+
11+
namespace brave {
12+
13+
void ShowDefaultBrowserDialog(Browser* browser);
14+
15+
} // namespace brave
16+
17+
#endif // BRAVE_BROWSER_UI_BROWSER_DIALOGS_H_
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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+
#ifndef BRAVE_BROWSER_UI_STARTUP_DEFAULT_BRAVE_BROWSER_PROMPT_H_
7+
#define BRAVE_BROWSER_UI_STARTUP_DEFAULT_BRAVE_BROWSER_PROMPT_H_
8+
9+
class PrefRegistrySimple;
10+
class Profile;
11+
12+
void RegisterDefaultBraveBrowserPromptPrefs(PrefRegistrySimple* registry);
13+
14+
void ShowDefaultBraveBrowserPrompt(Profile* profile);
15+
16+
void ResetDefaultBraveBrowserPrompt(Profile* profile);
17+
18+
#endif // BRAVE_BROWSER_UI_STARTUP_DEFAULT_BRAVE_BROWSER_PROMPT_H_

0 commit comments

Comments
 (0)