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