[Metrics] Make MetricsStateManager take a callback param to check if UMA is enabled.
[chromium-blink-merge.git] / chrome / browser / ui / views / first_run_dialog.cc
blob15a1169ce08a521e026d583117c8a6e41b20be83
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.
5 #include "chrome/browser/ui/views/first_run_dialog.h"
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"
30 #if defined(GOOGLE_CHROME_BUILD)
31 #include "base/prefs/pref_service.h"
32 #include "chrome/browser/browser_process.h"
33 #endif
35 using views::GridLayout;
37 namespace first_run {
39 bool ShowFirstRunDialog(Profile* profile) {
40 return FirstRunDialog::Show(profile);
43 } // namespace first_run
45 // static
46 bool FirstRunDialog::Show(Profile* profile) {
47 bool dialog_shown = false;
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);
55 if (!metrics_reporting_pref ||
56 !metrics_reporting_pref->IsManaged()) {
57 FirstRunDialog* dialog = new FirstRunDialog(profile);
58 views::DialogDelegate::CreateDialogWidget(dialog, NULL, NULL)->Show();
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|.
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;
72 #endif // defined(GOOGLE_CHROME_BUILD)
74 return dialog_shown;
77 FirstRunDialog::FirstRunDialog(Profile* profile)
78 : profile_(profile),
79 make_default_(NULL),
80 report_crashes_(NULL) {
81 GridLayout* layout = GridLayout::CreatePanel(this);
82 SetLayoutManager(layout);
84 const int related_y = views::kRelatedControlVerticalSpacing;
86 views::ColumnSet* column_set = layout->AddColumnSet(0);
87 column_set->AddColumn(GridLayout::FILL, GridLayout::CENTER, 0,
88 GridLayout::USE_PREF, 0, 0);
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_);
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_);
102 FirstRunDialog::~FirstRunDialog() {
105 void FirstRunDialog::Done() {
106 aura::Window* window = GetWidget()->GetNativeView();
107 aura::client::DispatcherClient* client =
108 aura::client::GetDispatcherClient(window->GetRootWindow());
109 client->QuitNestedMessageLoop();
112 views::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;
119 void FirstRunDialog::OnClosed() {
120 first_run::SetShouldShowWelcomePage();
121 Done();
124 bool FirstRunDialog::Accept() {
125 GetWidget()->Hide();
127 if (report_crashes_ && report_crashes_->checked()) {
128 if (GoogleUpdateSettings::SetCollectStatsConsent(true))
129 breakpad::InitCrashReporter(std::string());
130 } else {
131 GoogleUpdateSettings::SetCollectStatsConsent(false);
134 if (make_default_ && make_default_->checked())
135 ShellIntegration::SetAsDefaultBrowser();
137 Done();
138 return true;
141 int FirstRunDialog::GetDialogButtons() const {
142 return ui::DIALOG_BUTTON_OK;
145 void FirstRunDialog::LinkClicked(views::Link* source, int event_flags) {
146 platform_util::OpenExternal(profile_, GURL(chrome::kLearnMoreReportingURL));