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/prefs/pref_service.h"
8#include "chrome/browser/profiles/profile.h"
9#include "chrome/browser/shell_integration.h"
10#include "chrome/browser/ui/browser.h"
11#include "chrome/browser/ui/browser_finder.h"
12#include "chrome/browser/ui/webui/set_as_default_browser_ui.h"
13#include "chrome/common/pref_names.h"
14#include "components/startup_metric_utils/startup_metric_utils.h"
15#include "content/public/browser/browser_thread.h"
16#include "content/public/browser/notification_service.h"
17#include "content/public/browser/notification_types.h"
18
19using content::BrowserThread;
20
21namespace {
22
23// Show the page prompting the user to make Chrome the default browser on
24// Windows 8 (which means becoming "the browser" in Metro mode). The page
25// will be shown at the first appropriate opportunity. It can be placed in
26// a tab or in a dialog, depending on other settings.
27class SetMetroBrowserFlowLauncher : public content::NotificationObserver {
28 public:
29  static void LaunchSoon(Profile* profile) {
30    // The instance will manage its own lifetime.
31    new SetMetroBrowserFlowLauncher(profile);
32  }
33
34 private:
35  explicit SetMetroBrowserFlowLauncher(Profile* profile)
36      : profile_(profile) {
37    registrar_.Add(this, content::NOTIFICATION_LOAD_COMPLETED_MAIN_FRAME,
38                   content::NotificationService::AllSources());
39  }
40
41  // content::NotificationObserver override:
42  virtual void Observe(int type,
43                       const content::NotificationSource& source,
44                       const content::NotificationDetails& details) OVERRIDE;
45
46  content::NotificationRegistrar registrar_;
47  Profile* profile_;
48
49  DISALLOW_COPY_AND_ASSIGN(SetMetroBrowserFlowLauncher);
50};
51
52void SetMetroBrowserFlowLauncher::Observe(
53    int type,
54    const content::NotificationSource& source,
55    const content::NotificationDetails& details) {
56  DCHECK_EQ(type, content::NOTIFICATION_LOAD_COMPLETED_MAIN_FRAME);
57  Browser* browser = chrome::FindBrowserWithWebContents(
58      content::Source<content::WebContents>(source).ptr());
59
60  if (!browser || !browser->is_type_tabbed())
61    return;
62
63  // Unregister and delete.
64  registrar_.RemoveAll();
65  SetAsDefaultBrowserUI::Show(profile_, browser);
66  delete this;
67}
68
69}  // namespace
70
71namespace chrome {
72
73bool ShowFirstRunDefaultBrowserPrompt(Profile* profile) {
74  // If the only available mode of setting the default browser requires
75  // user interaction, it means this couldn't have been done yet. Therefore,
76  // we launch the dialog and inform the caller of it.
77  bool show_status =
78      (ShellIntegration::CanSetAsDefaultBrowser() ==
79       ShellIntegration::SET_DEFAULT_INTERACTIVE) &&
80      (ShellIntegration::GetDefaultBrowser() == ShellIntegration::NOT_DEFAULT);
81
82  if (show_status) {
83    startup_metric_utils::SetNonBrowserUIDisplayed();
84    SetMetroBrowserFlowLauncher::LaunchSoon(profile);
85  }
86
87  return show_status;
88}
89
90}  // namespace chrome
91