blob: fb88173530e181057948cf29247e78ac127b10f7 [file] [log] [blame]
[email protected]268942a2012-05-08 22:40:351// 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.
4
[email protected]bc4303d32012-05-10 14:58:555#include "chrome/browser/ui/startup/default_browser_prompt.h"
[email protected]268942a2012-05-08 22:40:356
pmonettefca05f32016-06-02 17:06:127#include <limits>
pmonette868ca642015-09-02 14:34:028#include <string>
9
skyostil380bb2222015-06-12 12:07:0510#include "base/location.h"
avi655876a2015-12-25 07:18:1511#include "base/macros.h"
[email protected]268942a2012-05-08 22:40:3512#include "base/memory/weak_ptr.h"
skyostil380bb2222015-06-12 12:07:0513#include "base/single_thread_task_runner.h"
grta872c102016-04-12 17:20:5714#include "base/strings/string_number_conversions.h"
grta872c102016-04-12 17:20:5715#include "base/time/time.h"
[email protected]abaca602013-03-29 01:31:2916#include "base/version.h"
avi655876a2015-12-25 07:18:1517#include "build/build_config.h"
[email protected]268942a2012-05-08 22:40:3518#include "chrome/browser/browser_process.h"
[email protected]4a8adfa02013-03-19 22:37:4619#include "chrome/browser/infobars/infobar_service.h"
[email protected]268942a2012-05-08 22:40:3520#include "chrome/browser/profiles/profile.h"
grtb0e8c812015-06-16 22:45:4221#include "chrome/browser/profiles/profile_manager.h"
[email protected]268942a2012-05-08 22:40:3522#include "chrome/browser/ui/browser.h"
[email protected]83810682012-10-12 21:10:1423#include "chrome/browser/ui/browser_finder.h"
pmonettefca05f32016-06-02 17:06:1224#include "chrome/browser/ui/startup/default_browser_infobar_delegate.h"
[email protected]617ee962013-01-29 20:49:1225#include "chrome/browser/ui/tabs/tab_strip_model.h"
[email protected]268942a2012-05-08 22:40:3526#include "chrome/common/pref_names.h"
brettwb1fc1b82016-02-02 00:19:0827#include "components/prefs/pref_registry_simple.h"
28#include "components/prefs/pref_service.h"
grta872c102016-04-12 17:20:5729#include "components/variations/variations_associated_data.h"
sdefresne9fb67692015-08-03 18:48:2230#include "components/version_info/version_info.h"
[email protected]268942a2012-05-08 22:40:3531
pmonettefca05f32016-06-02 17:06:1232namespace chrome {
grta872c102016-04-12 17:20:5733
[email protected]268942a2012-05-08 22:40:3534namespace {
35
pmonette586ab5b32016-03-07 19:50:3736void ResetCheckDefaultBrowserPref(const base::FilePath& profile_path) {
grt9c8bb5e2015-09-23 23:41:1737 Profile* profile =
pmonette586ab5b32016-03-07 19:50:3738 g_browser_process->profile_manager()->GetProfileByPath(profile_path);
grt9c8bb5e2015-09-23 23:41:1739 if (profile)
grt36fc6322016-04-07 13:28:3540 chrome::ResetDefaultBrowserPrompt(profile);
grt9c8bb5e2015-09-23 23:41:1741}
42
pmonette586ab5b32016-03-07 19:50:3743void ShowPrompt() {
scottmg5c03fe022016-02-03 01:27:2444 Browser* browser = chrome::FindLastActive();
[email protected]3a7982302012-05-25 15:53:0445 if (!browser)
46 return; // Reached during ui tests.
47
48 // In ChromeBot tests, there might be a race. This line appears to get
49 // called during shutdown and |tab| can be NULL.
[email protected]617ee962013-01-29 20:49:1250 content::WebContents* web_contents =
51 browser->tab_strip_model()->GetActiveWebContents();
[email protected]591a59f2012-10-11 01:16:2352 if (!web_contents)
[email protected]3a7982302012-05-25 15:53:0453 return;
54
[email protected]0be09932013-01-08 02:03:5055 DefaultBrowserInfoBarDelegate::Create(
grt36fc6322016-04-07 13:28:3556 InfoBarService::FromWebContents(web_contents), browser->profile());
[email protected]83810682012-10-12 21:10:1457}
58
grta872c102016-04-12 17:20:5759// Returns true if the default browser prompt should be shown if Chrome is not
60// the user's default browser.
61bool ShouldShowDefaultBrowserPrompt(Profile* profile) {
62 // Do not show the prompt if the "suppress_default_browser_prompt_for_version"
63 // master preference is set to the current version.
64 const std::string disable_version_string =
65 g_browser_process->local_state()->GetString(
66 prefs::kBrowserSuppressDefaultBrowserPrompt);
pwnalla9cfc0f2016-08-30 23:17:2367 const base::Version disable_version(disable_version_string);
grta872c102016-04-12 17:20:5768 DCHECK(disable_version_string.empty() || disable_version.IsValid());
69 if (disable_version.IsValid() &&
pwnalla9cfc0f2016-08-30 23:17:2370 disable_version == base::Version(version_info::GetVersionNumber())) {
grta872c102016-04-12 17:20:5771 return false;
72 }
73
74 // Do not show if the prompt period has yet to pass since the user previously
75 // dismissed the infobar.
76 int64_t last_dismissed_value =
77 profile->GetPrefs()->GetInt64(prefs::kDefaultBrowserLastDeclined);
78 if (last_dismissed_value) {
79 int period_days = 0;
80 base::StringToInt(variations::GetVariationParamValue(
81 "DefaultBrowserInfobar", "RefreshPeriodDays"),
82 &period_days);
83 if (period_days <= 0 || period_days == std::numeric_limits<int>::max())
84 return false; // Failed to parse a reasonable period.
85 base::Time show_on_or_after =
86 base::Time::FromInternalValue(last_dismissed_value) +
87 base::TimeDelta::FromDays(period_days);
88 if (base::Time::Now() < show_on_or_after)
89 return false;
90 }
91
92 return true;
93}
94
pmonette586ab5b32016-03-07 19:50:3795void OnCheckIsDefaultBrowserFinished(
96 const base::FilePath& profile_path,
97 bool show_prompt,
pmonetteb9204142016-03-08 20:02:4498 shell_integration::DefaultWebClientState state) {
99 if (state == shell_integration::IS_DEFAULT) {
pmonette586ab5b32016-03-07 19:50:37100 // Notify the user in the future if Chrome ceases to be the user's chosen
101 // default browser.
102 ResetCheckDefaultBrowserPref(profile_path);
pmonetteb9204142016-03-08 20:02:44103 } else if (show_prompt && state == shell_integration::NOT_DEFAULT &&
pmonette32a5cfb42016-04-11 22:04:44104 shell_integration::CanSetAsDefaultBrowser()) {
pmonette586ab5b32016-03-07 19:50:37105 ShowPrompt();
106 }
107}
108
[email protected]83810682012-10-12 21:10:14109} // namespace
110
[email protected]abaca602013-03-29 01:31:29111void RegisterDefaultBrowserPromptPrefs(PrefRegistrySimple* registry) {
112 registry->RegisterStringPref(
113 prefs::kBrowserSuppressDefaultBrowserPrompt, std::string());
114}
115
scottmgb711bab12016-02-10 21:39:11116void ShowDefaultBrowserPrompt(Profile* profile) {
grt22a6f882015-09-20 23:36:24117 // Do not check if Chrome is the default browser if there is a policy in
118 // control of this setting.
119 if (g_browser_process->local_state()->IsManagedPreference(
120 prefs::kDefaultBrowserSettingEnabled)) {
121 // Handling of the browser.default_browser_setting_enabled policy setting is
122 // taken care of in BrowserProcessImpl.
123 return;
124 }
125
pmonettecff89e92016-01-12 01:18:55126 PrefService* prefs = profile->GetPrefs();
127 // Reset preferences if kResetCheckDefaultBrowser is true.
128 if (prefs->GetBoolean(prefs::kResetCheckDefaultBrowser)) {
129 prefs->SetBoolean(prefs::kResetCheckDefaultBrowser, false);
grt36fc6322016-04-07 13:28:35130 ResetDefaultBrowserPrompt(profile);
pmonettecff89e92016-01-12 01:18:55131 }
132
pmonette9fa59e882016-02-10 00:12:19133 scoped_refptr<shell_integration::DefaultBrowserWorker>(
grta872c102016-04-12 17:20:57134 new shell_integration::DefaultBrowserWorker(
135 base::Bind(&OnCheckIsDefaultBrowserFinished, profile->GetPath(),
136 ShouldShowDefaultBrowserPrompt(profile))))
grt9c8bb5e2015-09-23 23:41:17137 ->StartCheckIsDefault();
[email protected]83810682012-10-12 21:10:14138}
139
grt36fc6322016-04-07 13:28:35140void DefaultBrowserPromptDeclined(Profile* profile) {
grta872c102016-04-12 17:20:57141 profile->GetPrefs()->SetInt64(prefs::kDefaultBrowserLastDeclined,
142 base::Time::Now().ToInternalValue());
grt36fc6322016-04-07 13:28:35143}
144
145void ResetDefaultBrowserPrompt(Profile* profile) {
grta872c102016-04-12 17:20:57146 profile->GetPrefs()->ClearPref(prefs::kDefaultBrowserLastDeclined);
grt36fc6322016-04-07 13:28:35147}
148
[email protected]83810682012-10-12 21:10:14149#if !defined(OS_WIN)
150bool ShowFirstRunDefaultBrowserPrompt(Profile* profile) {
151 return false;
152}
153#endif
154
[email protected]4feb37d92012-07-01 20:22:13155} // namespace chrome