Add ability to gather metrics to BubbleManager.
[chromium-blink-merge.git] / chrome / browser / ui / webui / set_as_default_browser_ui.cc
blob7bc754009d0078822755b6450305085d6c2fa198
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/webui/set_as_default_browser_ui.h"
7 #include "base/bind.h"
8 #include "base/bind_helpers.h"
9 #include "base/memory/weak_ptr.h"
10 #include "base/metrics/histogram.h"
11 #include "base/prefs/pref_service.h"
12 #include "chrome/browser/profiles/profile.h"
13 #include "chrome/browser/shell_integration.h"
14 #include "chrome/browser/ui/browser.h"
15 #include "chrome/browser/ui/browser_dialogs.h"
16 #include "chrome/browser/ui/browser_finder.h"
17 #include "chrome/browser/ui/browser_list.h"
18 #include "chrome/browser/ui/browser_list_observer.h"
19 #include "chrome/browser/ui/browser_window.h"
20 #include "chrome/browser/ui/chrome_pages.h"
21 #include "chrome/browser/ui/singleton_tabs.h"
22 #include "chrome/browser/ui/sync/sync_promo_ui.h"
23 #include "chrome/browser/ui/tabs/tab_strip_model.h"
24 #include "chrome/common/pref_names.h"
25 #include "chrome/common/url_constants.h"
26 #include "chrome/grit/chromium_strings.h"
27 #include "chrome/grit/generated_resources.h"
28 #include "chrome/grit/locale_settings.h"
29 #include "chrome/installer/util/install_util.h"
30 #include "content/public/browser/browser_thread.h"
31 #include "content/public/browser/web_contents.h"
32 #include "content/public/browser/web_contents_delegate.h"
33 #include "content/public/browser/web_ui.h"
34 #include "content/public/browser/web_ui_data_source.h"
35 #include "content/public/browser/web_ui_message_handler.h"
36 #include "grit/browser_resources.h"
37 #include "ui/base/l10n/l10n_font_util.h"
38 #include "ui/base/l10n/l10n_util.h"
39 #include "ui/gfx/font.h"
40 #include "ui/views/widget/widget.h"
41 #include "ui/web_dialogs/web_dialog_delegate.h"
43 using content::BrowserThread;
44 using content::WebContents;
45 using content::WebUIMessageHandler;
47 namespace {
49 const char kSetAsDefaultBrowserHistogram[] = "DefaultBrowser.InteractionResult";
51 // The enum permits registering in UMA the three possible outcomes (do not
52 // reorder these).
53 // ACCEPTED: user pressed Next and made Chrome default.
54 // DECLINED: user simply closed the dialog without making Chrome default.
55 // REGRETTED: user pressed Next but then elected a different default browser.
56 enum MakeChromeDefaultResult {
57 MAKE_CHROME_DEFAULT_ACCEPTED = 0,
58 MAKE_CHROME_DEFAULT_DECLINED = 1,
59 MAKE_CHROME_DEFAULT_REGRETTED = 2,
60 // MAKE_CHROME_DEFAULT_ACCEPTED_IMMERSE = 3, // Deprecated.
61 MAKE_CHROME_DEFAULT_MAX
64 content::WebUIDataSource* CreateSetAsDefaultBrowserUIHTMLSource() {
65 content::WebUIDataSource* data_source = content::WebUIDataSource::Create(
66 chrome::kChromeUIMetroFlowHost);
67 data_source->AddLocalizedString("page-title", IDS_METRO_FLOW_TAB_TITLE);
68 data_source->AddLocalizedString("flowTitle", IDS_METRO_FLOW_TITLE_SHORT);
69 data_source->AddLocalizedString("flowDescription",
70 IDS_METRO_FLOW_DESCRIPTION);
71 data_source->AddLocalizedString("flowNext",
72 IDS_METRO_FLOW_SET_DEFAULT);
73 data_source->AddLocalizedString("chromeLogoString",
74 IDS_METRO_FLOW_LOGO_STRING_ALT);
75 data_source->SetJsonPath("strings.js");
76 data_source->AddResourcePath("set_as_default_browser.js",
77 IDR_SET_AS_DEFAULT_BROWSER_JS);
78 data_source->SetDefaultResource(IDR_SET_AS_DEFAULT_BROWSER_HTML);
79 return data_source;
82 // A simple class serving as a delegate for passing down the result of the
83 // interaction.
84 class ResponseDelegate {
85 public:
86 virtual void SetDialogInteractionResult(MakeChromeDefaultResult result) = 0;
88 protected:
89 virtual ~ResponseDelegate() { }
92 // Event handler for SetAsDefaultBrowserUI. Capable of setting Chrome as the
93 // default browser on button click, closing itself and triggering Chrome
94 // restart.
95 class SetAsDefaultBrowserHandler
96 : public WebUIMessageHandler,
97 public base::SupportsWeakPtr<SetAsDefaultBrowserHandler>,
98 public ShellIntegration::DefaultWebClientObserver {
99 public:
100 explicit SetAsDefaultBrowserHandler(
101 const base::WeakPtr<ResponseDelegate>& response_delegate);
102 ~SetAsDefaultBrowserHandler() override;
104 // WebUIMessageHandler implementation.
105 void RegisterMessages() override;
107 // ShellIntegration::DefaultWebClientObserver implementation.
108 void SetDefaultWebClientUIState(
109 ShellIntegration::DefaultWebClientUIState state) override;
110 void OnSetAsDefaultConcluded(bool close_chrome) override;
111 bool IsInteractiveSetDefaultPermitted() override;
113 private:
114 // Handler for the 'Next' (or 'make Chrome the Metro browser') button.
115 void HandleLaunchSetDefaultBrowserFlow(const base::ListValue* args);
117 // Close this web ui.
118 void ConcludeInteraction(MakeChromeDefaultResult interaction_result);
120 scoped_refptr<ShellIntegration::DefaultBrowserWorker> default_browser_worker_;
121 bool set_default_returned_;
122 bool set_default_result_;
123 base::WeakPtr<ResponseDelegate> response_delegate_;
125 DISALLOW_COPY_AND_ASSIGN(SetAsDefaultBrowserHandler);
128 SetAsDefaultBrowserHandler::SetAsDefaultBrowserHandler(
129 const base::WeakPtr<ResponseDelegate>& response_delegate)
130 : default_browser_worker_(new ShellIntegration::DefaultBrowserWorker(this)),
131 set_default_returned_(false), set_default_result_(false),
132 response_delegate_(response_delegate) {
135 SetAsDefaultBrowserHandler::~SetAsDefaultBrowserHandler() {
136 default_browser_worker_->ObserverDestroyed();
139 void SetAsDefaultBrowserHandler::RegisterMessages() {
140 web_ui()->RegisterMessageCallback(
141 "SetAsDefaultBrowser:LaunchSetDefaultBrowserFlow",
142 base::Bind(&SetAsDefaultBrowserHandler::HandleLaunchSetDefaultBrowserFlow,
143 base::Unretained(this)));
146 void SetAsDefaultBrowserHandler::SetDefaultWebClientUIState(
147 ShellIntegration::DefaultWebClientUIState state) {
148 // The callback is expected to be invoked once the procedure has completed.
149 DCHECK_CURRENTLY_ON(BrowserThread::UI);
150 if (!set_default_returned_)
151 return;
153 if (state == ShellIntegration::STATE_NOT_DEFAULT && set_default_result_) {
154 // The operation concluded, but Chrome is still not the default.
155 // If the call has succeeded, this suggests user has decided not to make
156 // chrome the default.
157 ConcludeInteraction(MAKE_CHROME_DEFAULT_REGRETTED);
158 } else if (state == ShellIntegration::STATE_IS_DEFAULT) {
159 ConcludeInteraction(MAKE_CHROME_DEFAULT_ACCEPTED);
162 // Otherwise, keep the dialog open since the user probably didn't make a
163 // choice.
166 void SetAsDefaultBrowserHandler::OnSetAsDefaultConcluded(bool call_result) {
167 set_default_returned_ = true;
168 set_default_result_ = call_result;
171 bool SetAsDefaultBrowserHandler::IsInteractiveSetDefaultPermitted() {
172 return true;
175 void SetAsDefaultBrowserHandler::HandleLaunchSetDefaultBrowserFlow(
176 const base::ListValue* args) {
177 set_default_returned_ = false;
178 set_default_result_ = false;
179 default_browser_worker_->StartSetAsDefault();
182 void SetAsDefaultBrowserHandler::ConcludeInteraction(
183 MakeChromeDefaultResult interaction_result) {
184 DCHECK_CURRENTLY_ON(BrowserThread::UI);
186 if (response_delegate_)
187 response_delegate_->SetDialogInteractionResult(interaction_result);
189 WebContents* contents = web_ui()->GetWebContents();
191 if (contents) {
192 content::WebContentsDelegate* delegate = contents->GetDelegate();
193 if (delegate)
194 delegate->CloseContents(contents);
198 // A web dialog delegate implementation for when 'Make Chrome Metro' UI
199 // is displayed on a dialog.
200 class SetAsDefaultBrowserDialogImpl : public ui::WebDialogDelegate,
201 public ResponseDelegate,
202 public chrome::BrowserListObserver {
203 public:
204 SetAsDefaultBrowserDialogImpl(Profile* profile, Browser* browser);
205 ~SetAsDefaultBrowserDialogImpl() override;
206 // Show a modal web dialog with kChromeUIMetroFlowURL page.
207 void ShowDialog();
209 protected:
210 // Overridden from WebDialogDelegate:
211 ui::ModalType GetDialogModalType() const override;
212 base::string16 GetDialogTitle() const override;
213 GURL GetDialogContentURL() const override;
214 void GetWebUIMessageHandlers(
215 std::vector<WebUIMessageHandler*>* handlers) const override;
216 void GetDialogSize(gfx::Size* size) const override;
217 std::string GetDialogArgs() const override;
218 void OnDialogClosed(const std::string& json_retval) override;
219 void OnCloseContents(WebContents* source, bool* out_close_dialog) override;
220 bool ShouldShowDialogTitle() const override;
221 bool HandleContextMenu(const content::ContextMenuParams& params) override;
223 // Overridden from ResponseDelegate:
224 void SetDialogInteractionResult(MakeChromeDefaultResult result) override;
226 // Overridden from BrowserListObserver:
227 void OnBrowserRemoved(Browser* browser) override;
229 private:
230 Profile* profile_;
231 Browser* browser_;
232 mutable bool owns_handler_;
233 base::WeakPtrFactory<ResponseDelegate> response_delegate_ptr_factory_;
234 SetAsDefaultBrowserHandler* handler_;
235 MakeChromeDefaultResult dialog_interaction_result_;
237 DISALLOW_COPY_AND_ASSIGN(SetAsDefaultBrowserDialogImpl);
240 SetAsDefaultBrowserDialogImpl::SetAsDefaultBrowserDialogImpl(Profile* profile,
241 Browser* browser)
242 : profile_(profile),
243 browser_(browser),
244 owns_handler_(true),
245 response_delegate_ptr_factory_(this),
246 handler_(new SetAsDefaultBrowserHandler(
247 response_delegate_ptr_factory_.GetWeakPtr())),
248 dialog_interaction_result_(MAKE_CHROME_DEFAULT_DECLINED) {
249 BrowserList::AddObserver(this);
252 SetAsDefaultBrowserDialogImpl::~SetAsDefaultBrowserDialogImpl() {
253 if (browser_)
254 BrowserList::RemoveObserver(this);
255 if (owns_handler_)
256 delete handler_;
259 void SetAsDefaultBrowserDialogImpl::ShowDialog() {
260 // Use a NULL parent window to make sure that the dialog will have an item
261 // in the Windows task bar. The code below will make it highlight if the
262 // dialog is not in the foreground.
263 gfx::NativeWindow native_window = chrome::ShowWebDialog(NULL, profile_, this);
264 views::Widget* widget = views::Widget::GetWidgetForNativeWindow(
265 native_window);
266 widget->FlashFrame(true);
269 ui::ModalType SetAsDefaultBrowserDialogImpl::GetDialogModalType() const {
270 return ui::MODAL_TYPE_SYSTEM;
273 base::string16 SetAsDefaultBrowserDialogImpl::GetDialogTitle() const {
274 return l10n_util::GetStringUTF16(IDS_METRO_FLOW_TAB_TITLE);
277 GURL SetAsDefaultBrowserDialogImpl::GetDialogContentURL() const {
278 std::string url_string(chrome::kChromeUIMetroFlowURL);
279 return GURL(url_string);
282 void SetAsDefaultBrowserDialogImpl::GetWebUIMessageHandlers(
283 std::vector<WebUIMessageHandler*>* handlers) const {
284 handlers->push_back(handler_);
285 owns_handler_ = false;
288 void SetAsDefaultBrowserDialogImpl::GetDialogSize(gfx::Size* size) const {
289 PrefService* prefs = profile_->GetPrefs();
290 gfx::Font approximate_web_font(
291 prefs->GetString(prefs::kWebKitSansSerifFontFamily),
292 prefs->GetInteger(prefs::kWebKitDefaultFontSize));
294 *size = ui::GetLocalizedContentsSizeForFont(
295 IDS_METRO_FLOW_WIDTH_CHARS, IDS_METRO_FLOW_HEIGHT_LINES,
296 approximate_web_font);
299 std::string SetAsDefaultBrowserDialogImpl::GetDialogArgs() const {
300 return "[]";
303 void SetAsDefaultBrowserDialogImpl::OnDialogClosed(
304 const std::string& json_retval) {
305 // Register the user's response in UMA.
306 UMA_HISTOGRAM_ENUMERATION(kSetAsDefaultBrowserHistogram,
307 dialog_interaction_result_,
308 MAKE_CHROME_DEFAULT_MAX);
310 // If the user explicitly elected *not to* make Chrome default, we won't
311 // ask again.
312 if (dialog_interaction_result_ == MAKE_CHROME_DEFAULT_REGRETTED) {
313 PrefService* prefs = profile_->GetPrefs();
314 prefs->SetBoolean(prefs::kCheckDefaultBrowser, false);
317 // Carry on with a normal chrome session. For the purpose of surfacing this
318 // dialog the actual browser window had to remain hidden. Now it's time to
319 // show it.
320 if (browser_) {
321 BrowserWindow* window = browser_->window();
322 WebContents* contents = browser_->tab_strip_model()->GetActiveWebContents();
323 window->Show();
324 if (contents)
325 contents->SetInitialFocus();
328 delete this;
331 void SetAsDefaultBrowserDialogImpl::OnCloseContents(WebContents* source,
332 bool* out_close_dialog) {
333 *out_close_dialog = true;
336 bool SetAsDefaultBrowserDialogImpl::ShouldShowDialogTitle() const {
337 return true;
340 bool SetAsDefaultBrowserDialogImpl::HandleContextMenu(
341 const content::ContextMenuParams& params) {
342 return true;
345 void SetAsDefaultBrowserDialogImpl::SetDialogInteractionResult(
346 MakeChromeDefaultResult result) {
347 dialog_interaction_result_ = result;
350 void SetAsDefaultBrowserDialogImpl::OnBrowserRemoved(Browser* browser) {
351 if (browser_ == browser) {
352 browser_ = NULL;
353 BrowserList::RemoveObserver(this);
357 } // namespace
359 SetAsDefaultBrowserUI::SetAsDefaultBrowserUI(content::WebUI* web_ui)
360 : ui::WebDialogUI(web_ui) {
361 content::WebUIDataSource::Add(
362 Profile::FromWebUI(web_ui), CreateSetAsDefaultBrowserUIHTMLSource());
365 // static
366 void SetAsDefaultBrowserUI::Show(Profile* profile, Browser* browser) {
367 DCHECK_CURRENTLY_ON(BrowserThread::UI);
368 SetAsDefaultBrowserDialogImpl* dialog =
369 new SetAsDefaultBrowserDialogImpl(profile, browser);
370 dialog->ShowDialog();