1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/browser/ui/startup/default_browser_prompt.h"
7 #include "base/location.h"
8 #include "base/memory/weak_ptr.h"
9 #include "base/metrics/histogram.h"
10 #include "base/prefs/pref_registry_simple.h"
11 #include "base/prefs/pref_service.h"
12 #include "base/single_thread_task_runner.h"
13 #include "base/thread_task_runner_handle.h"
14 #include "base/version.h"
15 #include "chrome/browser/browser_process.h"
16 #include "chrome/browser/first_run/first_run.h"
17 #include "chrome/browser/infobars/infobar_service.h"
18 #include "chrome/browser/profiles/profile.h"
19 #include "chrome/browser/profiles/profile_manager.h"
20 #include "chrome/browser/shell_integration.h"
21 #include "chrome/browser/ui/browser.h"
22 #include "chrome/browser/ui/browser_finder.h"
23 #include "chrome/browser/ui/tabs/tab_strip_model.h"
24 #include "chrome/common/chrome_version_info.h"
25 #include "chrome/common/pref_names.h"
26 #include "chrome/grit/chromium_strings.h"
27 #include "chrome/grit/generated_resources.h"
28 #include "chrome/installer/util/master_preferences.h"
29 #include "chrome/installer/util/master_preferences_constants.h"
30 #include "components/infobars/core/confirm_infobar_delegate.h"
31 #include "components/infobars/core/infobar.h"
32 #include "content/public/browser/browser_thread.h"
33 #include "content/public/browser/navigation_details.h"
34 #include "content/public/browser/web_contents.h"
35 #include "grit/theme_resources.h"
36 #include "ui/base/l10n/l10n_util.h"
41 // Calls the appropriate function for setting Chrome as the default browser.
42 // This requires IO access (registry) and may result in interaction with a
44 void SetChromeAsDefaultBrowser(bool interactive_flow
, PrefService
* prefs
) {
45 if (interactive_flow
) {
46 UMA_HISTOGRAM_BOOLEAN("DefaultBrowserWarning.SetAsDefaultUI", true);
47 if (!ShellIntegration::SetAsDefaultBrowserInteractive()) {
48 UMA_HISTOGRAM_BOOLEAN("DefaultBrowserWarning.SetAsDefaultUIFailed", true);
49 } else if (ShellIntegration::GetDefaultBrowser() ==
50 ShellIntegration::NOT_DEFAULT
) {
51 // If the interaction succeeded but we are still not the default browser
52 // it likely means the user simply selected another browser from the
53 // panel. We will respect this choice and write it down as 'no, thanks'.
54 UMA_HISTOGRAM_BOOLEAN("DefaultBrowserWarning.DontSetAsDefault", true);
57 UMA_HISTOGRAM_BOOLEAN("DefaultBrowserWarning.SetAsDefault", true);
58 ShellIntegration::SetAsDefaultBrowser();
62 // The delegate for the infobar shown when Chrome is not the default browser.
63 class DefaultBrowserInfoBarDelegate
: public ConfirmInfoBarDelegate
{
65 // Creates a default browser infobar and delegate and adds the infobar to
67 static void Create(InfoBarService
* infobar_service
,
69 bool interactive_flow_required
);
72 DefaultBrowserInfoBarDelegate(PrefService
* prefs
,
73 bool interactive_flow_required
);
74 ~DefaultBrowserInfoBarDelegate() override
;
76 void AllowExpiry() { should_expire_
= true; }
78 // ConfirmInfoBarDelegate:
79 int GetIconID() const override
;
80 bool ShouldExpire(const NavigationDetails
& details
) const override
;
81 base::string16
GetMessageText() const override
;
82 base::string16
GetButtonLabel(InfoBarButton button
) const override
;
83 bool OKButtonTriggersUACPrompt() const override
;
84 bool Accept() override
;
85 bool Cancel() override
;
90 // Whether the user clicked one of the buttons.
93 // Whether the info-bar should be dismissed on the next navigation.
96 // Whether changing the default application will require entering the
98 const bool interactive_flow_required_
;
100 // Used to delay the expiration of the info-bar.
101 base::WeakPtrFactory
<DefaultBrowserInfoBarDelegate
> weak_factory_
;
103 DISALLOW_COPY_AND_ASSIGN(DefaultBrowserInfoBarDelegate
);
107 void DefaultBrowserInfoBarDelegate::Create(InfoBarService
* infobar_service
,
109 bool interactive_flow_required
) {
110 infobar_service
->AddInfoBar(infobar_service
->CreateConfirmInfoBar(
111 scoped_ptr
<ConfirmInfoBarDelegate
>(new DefaultBrowserInfoBarDelegate(
112 prefs
, interactive_flow_required
))));
115 DefaultBrowserInfoBarDelegate::DefaultBrowserInfoBarDelegate(
117 bool interactive_flow_required
)
118 : ConfirmInfoBarDelegate(),
120 action_taken_(false),
121 should_expire_(false),
122 interactive_flow_required_(interactive_flow_required
),
123 weak_factory_(this) {
124 // We want the info-bar to stick-around for few seconds and then be hidden
125 // on the next navigation after that.
126 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
127 FROM_HERE
, base::Bind(&DefaultBrowserInfoBarDelegate::AllowExpiry
,
128 weak_factory_
.GetWeakPtr()),
129 base::TimeDelta::FromSeconds(8));
132 DefaultBrowserInfoBarDelegate::~DefaultBrowserInfoBarDelegate() {
134 UMA_HISTOGRAM_BOOLEAN("DefaultBrowserWarning.Ignored", true);
137 int DefaultBrowserInfoBarDelegate::GetIconID() const {
138 return IDR_PRODUCT_LOGO_32
;
141 bool DefaultBrowserInfoBarDelegate::ShouldExpire(
142 const NavigationDetails
& details
) const {
143 return should_expire_
&& ConfirmInfoBarDelegate::ShouldExpire(details
);
146 base::string16
DefaultBrowserInfoBarDelegate::GetMessageText() const {
147 return l10n_util::GetStringUTF16(IDS_DEFAULT_BROWSER_INFOBAR_SHORT_TEXT
);
150 base::string16
DefaultBrowserInfoBarDelegate::GetButtonLabel(
151 InfoBarButton button
) const {
152 return l10n_util::GetStringUTF16((button
== BUTTON_OK
) ?
153 IDS_SET_AS_DEFAULT_INFOBAR_BUTTON_LABEL
:
154 IDS_DONT_ASK_AGAIN_INFOBAR_BUTTON_LABEL
);
157 bool DefaultBrowserInfoBarDelegate::OKButtonTriggersUACPrompt() const {
161 bool DefaultBrowserInfoBarDelegate::Accept() {
162 action_taken_
= true;
163 content::BrowserThread::PostTask(
164 content::BrowserThread::FILE, FROM_HERE
,
165 base::Bind(&SetChromeAsDefaultBrowser
, interactive_flow_required_
,
171 bool DefaultBrowserInfoBarDelegate::Cancel() {
172 action_taken_
= true;
173 UMA_HISTOGRAM_BOOLEAN("DefaultBrowserWarning.DontSetAsDefault", true);
174 // User clicked "Don't ask me again", remember that.
175 prefs_
->SetBoolean(prefs::kCheckDefaultBrowser
, false);
179 void NotifyNotDefaultBrowserCallback(chrome::HostDesktopType desktop_type
) {
180 Browser
* browser
= chrome::FindLastActiveWithHostDesktopType(desktop_type
);
182 return; // Reached during ui tests.
184 // In ChromeBot tests, there might be a race. This line appears to get
185 // called during shutdown and |tab| can be NULL.
186 content::WebContents
* web_contents
=
187 browser
->tab_strip_model()->GetActiveWebContents();
191 DefaultBrowserInfoBarDelegate::Create(
192 InfoBarService::FromWebContents(web_contents
),
193 Profile::FromBrowserContext(
194 web_contents
->GetBrowserContext())->GetPrefs(),
195 (ShellIntegration::CanSetAsDefaultBrowser() ==
196 ShellIntegration::SET_DEFAULT_INTERACTIVE
));
199 void ResetCheckDefaultBrowserPrefOnUIThread(
200 const base::FilePath
& profile_path
) {
201 DCHECK_CURRENTLY_ON(content::BrowserThread::UI
);
203 g_browser_process
->profile_manager()->GetProfileByPath(profile_path
);
205 profile
->GetPrefs()->SetBoolean(prefs::kCheckDefaultBrowser
, true);
208 void CheckDefaultBrowserOnFileThread(const base::FilePath
& profile_path
,
210 chrome::HostDesktopType desktop_type
) {
211 DCHECK_CURRENTLY_ON(content::BrowserThread::FILE);
212 ShellIntegration::DefaultWebClientState state
=
213 ShellIntegration::GetDefaultBrowser();
214 if (state
== ShellIntegration::IS_DEFAULT
) {
215 // Notify the user in the future if Chrome ceases to be the user's chosen
217 content::BrowserThread::PostTask(
218 content::BrowserThread::UI
, FROM_HERE
,
219 base::Bind(&ResetCheckDefaultBrowserPrefOnUIThread
, profile_path
));
220 } else if (show_prompt
&& state
== ShellIntegration::NOT_DEFAULT
) {
221 ShellIntegration::DefaultWebClientSetPermission default_change_mode
=
222 ShellIntegration::CanSetAsDefaultBrowser();
224 if (default_change_mode
!= ShellIntegration::SET_DEFAULT_NOT_ALLOWED
) {
225 content::BrowserThread::PostTask(
226 content::BrowserThread::UI
, FROM_HERE
,
227 base::Bind(&NotifyNotDefaultBrowserCallback
, desktop_type
));
236 void RegisterDefaultBrowserPromptPrefs(PrefRegistrySimple
* registry
) {
237 registry
->RegisterStringPref(
238 prefs::kBrowserSuppressDefaultBrowserPrompt
, std::string());
241 void ShowDefaultBrowserPrompt(Profile
* profile
, HostDesktopType desktop_type
) {
242 // We do not check if we are the default browser if:
243 // - There is a policy in control of this setting.
244 // We check if we are the default browser but do not prompt if:
245 // - The user said "don't ask me again" on the infobar earlier.
246 // - The "suppress_default_browser_prompt_for_version" master preference is
247 // set to the current version.
249 profile
->GetPrefs()->GetBoolean(prefs::kCheckDefaultBrowser
);
251 if (g_browser_process
->local_state()->IsManagedPreference(
252 prefs::kDefaultBrowserSettingEnabled
)) {
253 if (g_browser_process
->local_state()->GetBoolean(
254 prefs::kDefaultBrowserSettingEnabled
)) {
255 content::BrowserThread::PostTask(
256 content::BrowserThread::FILE, FROM_HERE
,
258 base::IgnoreResult(&ShellIntegration::SetAsDefaultBrowser
)));
260 // TODO(pastarmovj): We can't really do anything meaningful here yet but
261 // just prevent showing the infobar.
267 const std::string disable_version_string
=
268 g_browser_process
->local_state()->GetString(
269 prefs::kBrowserSuppressDefaultBrowserPrompt
);
270 const Version
disable_version(disable_version_string
);
271 DCHECK(disable_version_string
.empty() || disable_version
.IsValid());
272 if (disable_version
.IsValid()) {
273 const chrome::VersionInfo version_info
;
274 if (disable_version
.Equals(Version(version_info
.Version())))
279 content::BrowserThread::PostTask(
280 content::BrowserThread::FILE, FROM_HERE
,
281 base::Bind(&CheckDefaultBrowserOnFileThread
, profile
->GetPath(),
282 show_prompt
, desktop_type
));
286 bool ShowFirstRunDefaultBrowserPrompt(Profile
* profile
) {
291 } // namespace chrome