first_run_dialog.cc revision effb81e5f8246d0db0270817048dc992db66e9fb
1// Copyright 2014 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/views/first_run_dialog.h"
6
7#include "chrome/browser/first_run/first_run.h"
8#include "chrome/browser/platform_util.h"
9#include "chrome/browser/shell_integration.h"
10#include "chrome/common/pref_names.h"
11#include "chrome/common/url_constants.h"
12#include "chrome/installer/util/google_update_settings.h"
13#include "components/breakpad/app/breakpad_linux.h"
14#include "grit/chromium_strings.h"
15#include "grit/generated_resources.h"
16#include "grit/locale_settings.h"
17#include "grit/theme_resources.h"
18#include "ui/aura/env.h"
19#include "ui/aura/window.h"
20#include "ui/aura/window_event_dispatcher.h"
21#include "ui/base/l10n/l10n_util.h"
22#include "ui/views/controls/button/checkbox.h"
23#include "ui/views/controls/link.h"
24#include "ui/views/layout/grid_layout.h"
25#include "ui/views/layout/layout_constants.h"
26#include "ui/views/widget/widget.h"
27#include "ui/views/window/dialog_delegate.h"
28#include "ui/wm/public/dispatcher_client.h"
29
30#if defined(GOOGLE_CHROME_BUILD)
31#include "base/prefs/pref_service.h"
32#include "chrome/browser/browser_process.h"
33#endif
34
35using views::GridLayout;
36
37namespace first_run {
38
39bool ShowFirstRunDialog(Profile* profile) {
40  return FirstRunDialog::Show(profile);
41}
42
43}  // namespace first_run
44
45// static
46bool FirstRunDialog::Show(Profile* profile) {
47  bool dialog_shown = false;
48
49#if defined(GOOGLE_CHROME_BUILD)
50  // If the metrics reporting is managed, we won't ask.
51  const PrefService::Preference* metrics_reporting_pref =
52      g_browser_process->local_state()->FindPreference(
53          prefs::kMetricsReportingEnabled);
54
55  if (!metrics_reporting_pref ||
56      !metrics_reporting_pref->IsManaged()) {
57    FirstRunDialog* dialog = new FirstRunDialog(profile);
58    views::DialogDelegate::CreateDialogWidget(dialog, NULL, NULL)->Show();
59
60    // Use the widget's window itself so that the message loop
61    // exists when the dialog is closed by some other means than
62    // |Accept|.
63    //
64    // This is the same trick used in simple_message_box_views.cc, minus the
65    // refcounting.
66    aura::Window* anchor = dialog->GetWidget()->GetNativeWindow();
67    aura::client::DispatcherClient* client =
68        aura::client::GetDispatcherClient(anchor->GetRootWindow());
69    client->RunWithDispatcher(NULL);
70    dialog_shown = true;
71  }
72#endif  // defined(GOOGLE_CHROME_BUILD)
73
74  return dialog_shown;
75}
76
77FirstRunDialog::FirstRunDialog(Profile* profile)
78    : profile_(profile),
79      make_default_(NULL),
80      report_crashes_(NULL) {
81  GridLayout* layout = GridLayout::CreatePanel(this);
82  SetLayoutManager(layout);
83
84  const int related_y = views::kRelatedControlVerticalSpacing;
85
86  views::ColumnSet* column_set = layout->AddColumnSet(0);
87  column_set->AddColumn(GridLayout::FILL, GridLayout::CENTER, 0,
88                        GridLayout::USE_PREF, 0, 0);
89
90  layout->StartRow(0, 0);
91  make_default_ = new views::Checkbox(l10n_util::GetStringUTF16(
92      IDS_FR_CUSTOMIZE_DEFAULT_BROWSER));
93  make_default_->SetChecked(true);
94  layout->AddView(make_default_);
95
96  layout->StartRowWithPadding(0, 0, 0, related_y);
97  report_crashes_ = new views::Checkbox(l10n_util::GetStringUTF16(
98      IDS_OPTIONS_ENABLE_LOGGING));
99  layout->AddView(report_crashes_);
100}
101
102FirstRunDialog::~FirstRunDialog() {
103}
104
105void FirstRunDialog::Done() {
106  aura::Window* window = GetWidget()->GetNativeView();
107  aura::client::DispatcherClient* client =
108      aura::client::GetDispatcherClient(window->GetRootWindow());
109  client->QuitNestedMessageLoop();
110}
111
112views::View* FirstRunDialog::CreateExtraView() {
113  views::Link* link = new views::Link(l10n_util::GetStringUTF16(
114      IDS_LEARN_MORE));
115  link->set_listener(this);
116  return link;
117}
118
119void FirstRunDialog::OnClosed() {
120  first_run::SetShouldShowWelcomePage();
121  Done();
122}
123
124bool FirstRunDialog::Accept() {
125  GetWidget()->Hide();
126
127  if (report_crashes_ && report_crashes_->checked()) {
128    if (GoogleUpdateSettings::SetCollectStatsConsent(true))
129      breakpad::InitCrashReporter(std::string());
130  } else {
131    GoogleUpdateSettings::SetCollectStatsConsent(false);
132  }
133
134  if (make_default_ && make_default_->checked())
135    ShellIntegration::SetAsDefaultBrowser();
136
137  Done();
138  return true;
139}
140
141int FirstRunDialog::GetDialogButtons() const {
142  return ui::DIALOG_BUTTON_OK;
143}
144
145void FirstRunDialog::LinkClicked(views::Link* source, int event_flags) {
146  platform_util::OpenExternal(profile_, GURL(chrome::kLearnMoreReportingURL));
147}
148