Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / startup / default_browser_prompt.cc
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.
4
5 #include "chrome/browser/ui/startup/default_browser_prompt.h"
6
7 #include "base/memory/weak_ptr.h"
8 #include "base/message_loop/message_loop.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/version.h"
13 #include "chrome/browser/browser_process.h"
14 #include "chrome/browser/first_run/first_run.h"
15 #include "chrome/browser/infobars/infobar_service.h"
16 #include "chrome/browser/profiles/profile.h"
17 #include "chrome/browser/shell_integration.h"
18 #include "chrome/browser/ui/browser.h"
19 #include "chrome/browser/ui/browser_finder.h"
20 #include "chrome/browser/ui/tabs/tab_strip_model.h"
21 #include "chrome/common/chrome_version_info.h"
22 #include "chrome/common/pref_names.h"
23 #include "chrome/grit/chromium_strings.h"
24 #include "chrome/grit/generated_resources.h"
25 #include "chrome/installer/util/master_preferences.h"
26 #include "chrome/installer/util/master_preferences_constants.h"
27 #include "components/infobars/core/confirm_infobar_delegate.h"
28 #include "components/infobars/core/infobar.h"
29 #include "content/public/browser/browser_thread.h"
30 #include "content/public/browser/navigation_details.h"
31 #include "content/public/browser/web_contents.h"
32 #include "grit/theme_resources.h"
33 #include "ui/base/l10n/l10n_util.h"
34
35
36 namespace {
37
38 // Calls the appropriate function for setting Chrome as the default browser.
39 // This requires IO access (registry) and may result in interaction with a
40 // modal system UI.
41 void SetChromeAsDefaultBrowser(bool interactive_flow, PrefService* prefs) {
42   if (interactive_flow) {
43     UMA_HISTOGRAM_COUNTS("DefaultBrowserWarning.SetAsDefaultUI", 1);
44     if (!ShellIntegration::SetAsDefaultBrowserInteractive()) {
45       UMA_HISTOGRAM_COUNTS("DefaultBrowserWarning.SetAsDefaultUIFailed", 1);
46     } else if (ShellIntegration::GetDefaultBrowser() ==
47                ShellIntegration::NOT_DEFAULT) {
48       // If the interaction succeeded but we are still not the default browser
49       // it likely means the user simply selected another browser from the
50       // panel. We will respect this choice and write it down as 'no, thanks'.
51       UMA_HISTOGRAM_COUNTS("DefaultBrowserWarning.DontSetAsDefault", 1);
52     }
53   } else {
54     UMA_HISTOGRAM_COUNTS("DefaultBrowserWarning.SetAsDefault", 1);
55     ShellIntegration::SetAsDefaultBrowser();
56   }
57 }
58
59 // The delegate for the infobar shown when Chrome is not the default browser.
60 class DefaultBrowserInfoBarDelegate : public ConfirmInfoBarDelegate {
61  public:
62   // Creates a default browser infobar and delegate and adds the infobar to
63   // |infobar_service|.
64   static void Create(InfoBarService* infobar_service,
65                      PrefService* prefs,
66                      bool interactive_flow_required);
67
68  private:
69   DefaultBrowserInfoBarDelegate(PrefService* prefs,
70                                 bool interactive_flow_required);
71   ~DefaultBrowserInfoBarDelegate() override;
72
73   void AllowExpiry() { should_expire_ = true; }
74
75   // ConfirmInfoBarDelegate:
76   int GetIconID() const override;
77   base::string16 GetMessageText() const override;
78   base::string16 GetButtonLabel(InfoBarButton button) const override;
79   bool OKButtonTriggersUACPrompt() const override;
80   bool Accept() override;
81   bool Cancel() override;
82   bool ShouldExpireInternal(const NavigationDetails& details) const override;
83
84   // The prefs to use.
85   PrefService* prefs_;
86
87   // Whether the user clicked one of the buttons.
88   bool action_taken_;
89
90   // Whether the info-bar should be dismissed on the next navigation.
91   bool should_expire_;
92
93   // Whether changing the default application will require entering the
94   // modal-UI flow.
95   const bool interactive_flow_required_;
96
97   // Used to delay the expiration of the info-bar.
98   base::WeakPtrFactory<DefaultBrowserInfoBarDelegate> weak_factory_;
99
100   DISALLOW_COPY_AND_ASSIGN(DefaultBrowserInfoBarDelegate);
101 };
102
103 // static
104 void DefaultBrowserInfoBarDelegate::Create(InfoBarService* infobar_service,
105                                            PrefService* prefs,
106                                            bool interactive_flow_required) {
107   infobar_service->AddInfoBar(ConfirmInfoBarDelegate::CreateInfoBar(
108       scoped_ptr<ConfirmInfoBarDelegate>(new DefaultBrowserInfoBarDelegate(
109           prefs, interactive_flow_required))));
110 }
111
112 DefaultBrowserInfoBarDelegate::DefaultBrowserInfoBarDelegate(
113     PrefService* prefs,
114     bool interactive_flow_required)
115     : ConfirmInfoBarDelegate(),
116       prefs_(prefs),
117       action_taken_(false),
118       should_expire_(false),
119       interactive_flow_required_(interactive_flow_required),
120       weak_factory_(this) {
121   // We want the info-bar to stick-around for few seconds and then be hidden
122   // on the next navigation after that.
123   base::MessageLoop::current()->PostDelayedTask(
124       FROM_HERE,
125       base::Bind(&DefaultBrowserInfoBarDelegate::AllowExpiry,
126                  weak_factory_.GetWeakPtr()),
127       base::TimeDelta::FromSeconds(8));
128 }
129
130 DefaultBrowserInfoBarDelegate::~DefaultBrowserInfoBarDelegate() {
131   if (!action_taken_)
132     UMA_HISTOGRAM_COUNTS("DefaultBrowserWarning.Ignored", 1);
133 }
134
135 int DefaultBrowserInfoBarDelegate::GetIconID() const {
136   return IDR_PRODUCT_LOGO_32;
137 }
138
139 base::string16 DefaultBrowserInfoBarDelegate::GetMessageText() const {
140   return l10n_util::GetStringUTF16(IDS_DEFAULT_BROWSER_INFOBAR_SHORT_TEXT);
141 }
142
143 base::string16 DefaultBrowserInfoBarDelegate::GetButtonLabel(
144     InfoBarButton button) const {
145   return l10n_util::GetStringUTF16((button == BUTTON_OK) ?
146       IDS_SET_AS_DEFAULT_INFOBAR_BUTTON_LABEL :
147       IDS_DONT_ASK_AGAIN_INFOBAR_BUTTON_LABEL);
148 }
149
150 bool DefaultBrowserInfoBarDelegate::OKButtonTriggersUACPrompt() const {
151   return true;
152 }
153
154 bool DefaultBrowserInfoBarDelegate::Accept() {
155   action_taken_ = true;
156   content::BrowserThread::PostTask(
157       content::BrowserThread::FILE, FROM_HERE,
158       base::Bind(&SetChromeAsDefaultBrowser, interactive_flow_required_,
159                  prefs_));
160
161   return true;
162 }
163
164 bool DefaultBrowserInfoBarDelegate::Cancel() {
165   action_taken_ = true;
166   UMA_HISTOGRAM_COUNTS("DefaultBrowserWarning.DontSetAsDefault", 1);
167   // User clicked "Don't ask me again", remember that.
168   prefs_->SetBoolean(prefs::kCheckDefaultBrowser, false);
169   return true;
170 }
171
172 bool DefaultBrowserInfoBarDelegate::ShouldExpireInternal(
173     const NavigationDetails& details) const {
174   return should_expire_;
175 }
176
177 void NotifyNotDefaultBrowserCallback(chrome::HostDesktopType desktop_type) {
178   Browser* browser = chrome::FindLastActiveWithHostDesktopType(desktop_type);
179   if (!browser)
180     return;  // Reached during ui tests.
181
182   // In ChromeBot tests, there might be a race. This line appears to get
183   // called during shutdown and |tab| can be NULL.
184   content::WebContents* web_contents =
185       browser->tab_strip_model()->GetActiveWebContents();
186   if (!web_contents)
187     return;
188
189   DefaultBrowserInfoBarDelegate::Create(
190       InfoBarService::FromWebContents(web_contents),
191       Profile::FromBrowserContext(
192           web_contents->GetBrowserContext())->GetPrefs(),
193       (ShellIntegration::CanSetAsDefaultBrowser() ==
194           ShellIntegration::SET_DEFAULT_INTERACTIVE));
195 }
196
197 void CheckDefaultBrowserCallback(chrome::HostDesktopType desktop_type) {
198   if (ShellIntegration::GetDefaultBrowser() == ShellIntegration::NOT_DEFAULT) {
199     ShellIntegration::DefaultWebClientSetPermission default_change_mode =
200         ShellIntegration::CanSetAsDefaultBrowser();
201
202     if (default_change_mode != ShellIntegration::SET_DEFAULT_NOT_ALLOWED) {
203       content::BrowserThread::PostTask(
204           content::BrowserThread::UI, FROM_HERE,
205           base::Bind(&NotifyNotDefaultBrowserCallback, desktop_type));
206     }
207   }
208 }
209
210 }  // namespace
211
212 namespace chrome {
213
214 void RegisterDefaultBrowserPromptPrefs(PrefRegistrySimple* registry) {
215   registry->RegisterStringPref(
216       prefs::kBrowserSuppressDefaultBrowserPrompt, std::string());
217 }
218
219 void ShowDefaultBrowserPrompt(Profile* profile, HostDesktopType desktop_type) {
220   // We do not check if we are the default browser if:
221   // - The user said "don't ask me again" on the infobar earlier.
222   // - There is a policy in control of this setting.
223   // - The "suppress_default_browser_prompt_for_version" master preference is
224   //     set to the current version.
225   if (!profile->GetPrefs()->GetBoolean(prefs::kCheckDefaultBrowser))
226     return;
227
228   if (g_browser_process->local_state()->IsManagedPreference(
229       prefs::kDefaultBrowserSettingEnabled)) {
230     if (g_browser_process->local_state()->GetBoolean(
231         prefs::kDefaultBrowserSettingEnabled)) {
232       content::BrowserThread::PostTask(
233           content::BrowserThread::FILE, FROM_HERE,
234           base::Bind(
235               base::IgnoreResult(&ShellIntegration::SetAsDefaultBrowser)));
236     } else {
237       // TODO(pastarmovj): We can't really do anything meaningful here yet but
238       // just prevent showing the infobar.
239     }
240     return;
241   }
242
243   const std::string disable_version_string =
244       g_browser_process->local_state()->GetString(
245           prefs::kBrowserSuppressDefaultBrowserPrompt);
246   const Version disable_version(disable_version_string);
247   DCHECK(disable_version_string.empty() || disable_version.IsValid());
248   if (disable_version.IsValid()) {
249     const chrome::VersionInfo version_info;
250     if (disable_version.Equals(Version(version_info.Version())))
251       return;
252   }
253
254   content::BrowserThread::PostTask(
255       content::BrowserThread::FILE, FROM_HERE,
256       base::Bind(&CheckDefaultBrowserCallback, desktop_type));
257 }
258
259 #if !defined(OS_WIN)
260 bool ShowFirstRunDefaultBrowserPrompt(Profile* profile) {
261   return false;
262 }
263 #endif
264
265 }  // namespace chrome