| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/ui/startup/default_browser_prompt.h" | 5 #include "chrome/browser/ui/startup/default_browser_prompt.h" |
| 6 | 6 |
| 7 #include <limits> |
| 7 #include <string> | 8 #include <string> |
| 8 | 9 |
| 9 #include "base/location.h" | 10 #include "base/location.h" |
| 10 #include "base/macros.h" | 11 #include "base/macros.h" |
| 11 #include "base/memory/weak_ptr.h" | 12 #include "base/memory/weak_ptr.h" |
| 12 #include "base/metrics/histogram_macros.h" | |
| 13 #include "base/metrics/user_metrics_action.h" | |
| 14 #include "base/single_thread_task_runner.h" | 13 #include "base/single_thread_task_runner.h" |
| 15 #include "base/strings/string_number_conversions.h" | 14 #include "base/strings/string_number_conversions.h" |
| 16 #include "base/threading/thread_task_runner_handle.h" | |
| 17 #include "base/time/time.h" | 15 #include "base/time/time.h" |
| 18 #include "base/version.h" | 16 #include "base/version.h" |
| 19 #include "build/build_config.h" | 17 #include "build/build_config.h" |
| 20 #include "chrome/browser/browser_process.h" | 18 #include "chrome/browser/browser_process.h" |
| 21 #include "chrome/browser/infobars/infobar_service.h" | 19 #include "chrome/browser/infobars/infobar_service.h" |
| 22 #include "chrome/browser/profiles/profile.h" | 20 #include "chrome/browser/profiles/profile.h" |
| 23 #include "chrome/browser/profiles/profile_manager.h" | 21 #include "chrome/browser/profiles/profile_manager.h" |
| 24 #include "chrome/browser/ui/browser.h" | 22 #include "chrome/browser/ui/browser.h" |
| 25 #include "chrome/browser/ui/browser_finder.h" | 23 #include "chrome/browser/ui/browser_finder.h" |
| 24 #include "chrome/browser/ui/startup/default_browser_infobar_delegate.h" |
| 26 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 25 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 27 #include "chrome/common/pref_names.h" | 26 #include "chrome/common/pref_names.h" |
| 28 #include "chrome/grit/chromium_strings.h" | |
| 29 #include "chrome/grit/generated_resources.h" | |
| 30 #include "components/infobars/core/confirm_infobar_delegate.h" | |
| 31 #include "components/infobars/core/infobar.h" | |
| 32 #include "components/prefs/pref_registry_simple.h" | 27 #include "components/prefs/pref_registry_simple.h" |
| 33 #include "components/prefs/pref_service.h" | 28 #include "components/prefs/pref_service.h" |
| 34 #include "components/variations/variations_associated_data.h" | 29 #include "components/variations/variations_associated_data.h" |
| 35 #include "components/version_info/version_info.h" | 30 #include "components/version_info/version_info.h" |
| 36 #include "content/public/browser/user_metrics.h" | |
| 37 #include "grit/theme_resources.h" | |
| 38 #include "ui/base/l10n/l10n_util.h" | |
| 39 #include "ui/gfx/vector_icons_public.h" | |
| 40 | 31 |
| 41 #if defined(OS_WIN) | 32 namespace chrome { |
| 42 #include "base/win/windows_version.h" | |
| 43 #endif | |
| 44 | 33 |
| 45 namespace { | 34 namespace { |
| 46 | 35 |
| 47 // The delegate for the infobar shown when Chrome is not the default browser. | |
| 48 // Ownership of the delegate is given to the infobar itself, the lifetime of | |
| 49 // which is bound to the containing WebContents. | |
| 50 class DefaultBrowserInfoBarDelegate : public ConfirmInfoBarDelegate { | |
| 51 public: | |
| 52 // Creates a default browser infobar and delegate and adds the infobar to | |
| 53 // |infobar_service|. | |
| 54 static void Create(InfoBarService* infobar_service, Profile* profile); | |
| 55 | |
| 56 private: | |
| 57 // Possible user interactions with the default browser info bar. | |
| 58 // Do not modify the ordering as it is important for UMA. | |
| 59 enum InfoBarUserInteraction { | |
| 60 // The user clicked the "OK" (i.e., "Set as default") button. | |
| 61 ACCEPT_INFO_BAR = 0, | |
| 62 // The user clicked the "Cancel" (i.e., "Don't ask again") button. | |
| 63 CANCEL_INFO_BAR = 1, | |
| 64 // The user did not interact with the info bar. | |
| 65 IGNORE_INFO_BAR = 2, | |
| 66 NUM_INFO_BAR_USER_INTERACTION_TYPES | |
| 67 }; | |
| 68 | |
| 69 explicit DefaultBrowserInfoBarDelegate(Profile* profile); | |
| 70 ~DefaultBrowserInfoBarDelegate() override; | |
| 71 | |
| 72 void AllowExpiry() { should_expire_ = true; } | |
| 73 | |
| 74 // InfoBarDelegate: | |
| 75 Type GetInfoBarType() const override; | |
| 76 infobars::InfoBarDelegate::InfoBarIdentifier GetIdentifier() const override; | |
| 77 int GetIconId() const override; | |
| 78 gfx::VectorIconId GetVectorIconId() const override; | |
| 79 bool ShouldExpire(const NavigationDetails& details) const override; | |
| 80 void InfoBarDismissed() override; | |
| 81 | |
| 82 // ConfirmInfoBarDelegate: | |
| 83 base::string16 GetMessageText() const override; | |
| 84 base::string16 GetButtonLabel(InfoBarButton button) const override; | |
| 85 bool OKButtonTriggersUACPrompt() const override; | |
| 86 bool Accept() override; | |
| 87 bool Cancel() override; | |
| 88 | |
| 89 // The WebContents's corresponding profile. | |
| 90 Profile* profile_; | |
| 91 | |
| 92 // Whether the info-bar should be dismissed on the next navigation. | |
| 93 bool should_expire_ = false; | |
| 94 | |
| 95 // Used to delay the expiration of the info-bar. | |
| 96 base::WeakPtrFactory<DefaultBrowserInfoBarDelegate> weak_factory_; | |
| 97 | |
| 98 DISALLOW_COPY_AND_ASSIGN(DefaultBrowserInfoBarDelegate); | |
| 99 }; | |
| 100 | |
| 101 // static | |
| 102 void DefaultBrowserInfoBarDelegate::Create(InfoBarService* infobar_service, | |
| 103 Profile* profile) { | |
| 104 infobar_service->AddInfoBar(infobar_service->CreateConfirmInfoBar( | |
| 105 std::unique_ptr<ConfirmInfoBarDelegate>( | |
| 106 new DefaultBrowserInfoBarDelegate(profile)))); | |
| 107 } | |
| 108 | |
| 109 DefaultBrowserInfoBarDelegate::DefaultBrowserInfoBarDelegate(Profile* profile) | |
| 110 : ConfirmInfoBarDelegate(), profile_(profile), weak_factory_(this) { | |
| 111 // We want the info-bar to stick-around for few seconds and then be hidden | |
| 112 // on the next navigation after that. | |
| 113 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( | |
| 114 FROM_HERE, base::Bind(&DefaultBrowserInfoBarDelegate::AllowExpiry, | |
| 115 weak_factory_.GetWeakPtr()), | |
| 116 base::TimeDelta::FromSeconds(8)); | |
| 117 } | |
| 118 | |
| 119 DefaultBrowserInfoBarDelegate::~DefaultBrowserInfoBarDelegate() = default; | |
| 120 | |
| 121 infobars::InfoBarDelegate::Type DefaultBrowserInfoBarDelegate::GetInfoBarType() | |
| 122 const { | |
| 123 #if defined(OS_WIN) | |
| 124 return WARNING_TYPE; | |
| 125 #else | |
| 126 return PAGE_ACTION_TYPE; | |
| 127 #endif | |
| 128 } | |
| 129 | |
| 130 infobars::InfoBarDelegate::InfoBarIdentifier | |
| 131 DefaultBrowserInfoBarDelegate::GetIdentifier() const { | |
| 132 return DEFAULT_BROWSER_INFOBAR_DELEGATE; | |
| 133 } | |
| 134 | |
| 135 int DefaultBrowserInfoBarDelegate::GetIconId() const { | |
| 136 return IDR_PRODUCT_LOGO_32; | |
| 137 } | |
| 138 | |
| 139 gfx::VectorIconId DefaultBrowserInfoBarDelegate::GetVectorIconId() const { | |
| 140 #if defined(OS_MACOSX) || defined(OS_ANDROID) | |
| 141 return gfx::VectorIconId::VECTOR_ICON_NONE; | |
| 142 #else | |
| 143 return gfx::VectorIconId::CHROME_PRODUCT; | |
| 144 #endif | |
| 145 } | |
| 146 | |
| 147 bool DefaultBrowserInfoBarDelegate::ShouldExpire( | |
| 148 const NavigationDetails& details) const { | |
| 149 return should_expire_ && ConfirmInfoBarDelegate::ShouldExpire(details); | |
| 150 } | |
| 151 | |
| 152 void DefaultBrowserInfoBarDelegate::InfoBarDismissed() { | |
| 153 content::RecordAction( | |
| 154 base::UserMetricsAction("DefaultBrowserInfoBar_Dismiss")); | |
| 155 UMA_HISTOGRAM_ENUMERATION("DefaultBrowser.InfoBar.UserInteraction", | |
| 156 IGNORE_INFO_BAR, | |
| 157 NUM_INFO_BAR_USER_INTERACTION_TYPES); | |
| 158 } | |
| 159 | |
| 160 base::string16 DefaultBrowserInfoBarDelegate::GetMessageText() const { | |
| 161 return l10n_util::GetStringUTF16(IDS_DEFAULT_BROWSER_INFOBAR_SHORT_TEXT); | |
| 162 } | |
| 163 | |
| 164 base::string16 DefaultBrowserInfoBarDelegate::GetButtonLabel( | |
| 165 InfoBarButton button) const { | |
| 166 #if defined(OS_WIN) | |
| 167 // On Windows 10, the "OK" button opens the Windows Settings application, | |
| 168 // through which the user must make their default browser choice. | |
| 169 const int kSetAsDefaultButtonMessageId = | |
| 170 base::win::GetVersion() >= base::win::VERSION_WIN10 | |
| 171 ? IDS_DEFAULT_BROWSER_INFOBAR_OK_BUTTON_LABEL_WIN_10 | |
| 172 : IDS_DEFAULT_BROWSER_INFOBAR_OK_BUTTON_LABEL; | |
| 173 #else | |
| 174 const int kSetAsDefaultButtonMessageId = | |
| 175 IDS_DEFAULT_BROWSER_INFOBAR_OK_BUTTON_LABEL; | |
| 176 #endif | |
| 177 return l10n_util::GetStringUTF16( | |
| 178 button == BUTTON_OK ? kSetAsDefaultButtonMessageId | |
| 179 : IDS_DEFAULT_BROWSER_INFOBAR_CANCEL_BUTTON_LABEL); | |
| 180 } | |
| 181 | |
| 182 // Setting an app as the default browser doesn't require elevation directly, but | |
| 183 // it does require registering it as the protocol handler for "http", so if | |
| 184 // protocol registration in general requires elevation, this does as well. | |
| 185 bool DefaultBrowserInfoBarDelegate::OKButtonTriggersUACPrompt() const { | |
| 186 return shell_integration::IsElevationNeededForSettingDefaultProtocolClient(); | |
| 187 } | |
| 188 | |
| 189 bool DefaultBrowserInfoBarDelegate::Accept() { | |
| 190 content::RecordAction( | |
| 191 base::UserMetricsAction("DefaultBrowserInfoBar_Accept")); | |
| 192 UMA_HISTOGRAM_ENUMERATION("DefaultBrowser.InfoBar.UserInteraction", | |
| 193 ACCEPT_INFO_BAR, | |
| 194 NUM_INFO_BAR_USER_INTERACTION_TYPES); | |
| 195 // The worker pointer is reference counted. While it is running, the | |
| 196 // message loops of the FILE and UI thread will hold references to it | |
| 197 // and it will be automatically freed once all its tasks have finished. | |
| 198 scoped_refptr<shell_integration::DefaultBrowserWorker>( | |
| 199 new shell_integration::DefaultBrowserWorker( | |
| 200 shell_integration::DefaultWebClientWorkerCallback())) | |
| 201 ->StartSetAsDefault(); | |
| 202 return true; | |
| 203 } | |
| 204 | |
| 205 bool DefaultBrowserInfoBarDelegate::Cancel() { | |
| 206 chrome::DefaultBrowserPromptDeclined(profile_); | |
| 207 content::RecordAction( | |
| 208 base::UserMetricsAction("DefaultBrowserInfoBar_Cancel")); | |
| 209 UMA_HISTOGRAM_ENUMERATION("DefaultBrowser.InfoBar.UserInteraction", | |
| 210 CANCEL_INFO_BAR, | |
| 211 NUM_INFO_BAR_USER_INTERACTION_TYPES); | |
| 212 return true; | |
| 213 } | |
| 214 | |
| 215 void ResetCheckDefaultBrowserPref(const base::FilePath& profile_path) { | 36 void ResetCheckDefaultBrowserPref(const base::FilePath& profile_path) { |
| 216 Profile* profile = | 37 Profile* profile = |
| 217 g_browser_process->profile_manager()->GetProfileByPath(profile_path); | 38 g_browser_process->profile_manager()->GetProfileByPath(profile_path); |
| 218 if (profile) | 39 if (profile) |
| 219 chrome::ResetDefaultBrowserPrompt(profile); | 40 chrome::ResetDefaultBrowserPrompt(profile); |
| 220 } | 41 } |
| 221 | 42 |
| 222 void ShowPrompt() { | 43 void ShowPrompt() { |
| 223 Browser* browser = chrome::FindLastActive(); | 44 Browser* browser = chrome::FindLastActive(); |
| 224 if (!browser) | 45 if (!browser) |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 280 // default browser. | 101 // default browser. |
| 281 ResetCheckDefaultBrowserPref(profile_path); | 102 ResetCheckDefaultBrowserPref(profile_path); |
| 282 } else if (show_prompt && state == shell_integration::NOT_DEFAULT && | 103 } else if (show_prompt && state == shell_integration::NOT_DEFAULT && |
| 283 shell_integration::CanSetAsDefaultBrowser()) { | 104 shell_integration::CanSetAsDefaultBrowser()) { |
| 284 ShowPrompt(); | 105 ShowPrompt(); |
| 285 } | 106 } |
| 286 } | 107 } |
| 287 | 108 |
| 288 } // namespace | 109 } // namespace |
| 289 | 110 |
| 290 namespace chrome { | |
| 291 | |
| 292 void RegisterDefaultBrowserPromptPrefs(PrefRegistrySimple* registry) { | 111 void RegisterDefaultBrowserPromptPrefs(PrefRegistrySimple* registry) { |
| 293 registry->RegisterStringPref( | 112 registry->RegisterStringPref( |
| 294 prefs::kBrowserSuppressDefaultBrowserPrompt, std::string()); | 113 prefs::kBrowserSuppressDefaultBrowserPrompt, std::string()); |
| 295 } | 114 } |
| 296 | 115 |
| 297 void ShowDefaultBrowserPrompt(Profile* profile) { | 116 void ShowDefaultBrowserPrompt(Profile* profile) { |
| 298 // Do not check if Chrome is the default browser if there is a policy in | 117 // Do not check if Chrome is the default browser if there is a policy in |
| 299 // control of this setting. | 118 // control of this setting. |
| 300 if (g_browser_process->local_state()->IsManagedPreference( | 119 if (g_browser_process->local_state()->IsManagedPreference( |
| 301 prefs::kDefaultBrowserSettingEnabled)) { | 120 prefs::kDefaultBrowserSettingEnabled)) { |
| (...skipping 25 matching lines...) Expand all Loading... |
| 327 profile->GetPrefs()->ClearPref(prefs::kDefaultBrowserLastDeclined); | 146 profile->GetPrefs()->ClearPref(prefs::kDefaultBrowserLastDeclined); |
| 328 } | 147 } |
| 329 | 148 |
| 330 #if !defined(OS_WIN) | 149 #if !defined(OS_WIN) |
| 331 bool ShowFirstRunDefaultBrowserPrompt(Profile* profile) { | 150 bool ShowFirstRunDefaultBrowserPrompt(Profile* profile) { |
| 332 return false; | 151 return false; |
| 333 } | 152 } |
| 334 #endif | 153 #endif |
| 335 | 154 |
| 336 } // namespace chrome | 155 } // namespace chrome |
| OLD | NEW |