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/gtk/first_run_dialog.h"
10 #include "base/i18n/rtl.h"
11 #include "base/message_loop/message_loop.h"
12 #include "base/strings/utf_string_conversions.h"
13 #include "chrome/browser/first_run/first_run_dialog.h"
14 #include "chrome/browser/platform_util.h"
15 #include "chrome/browser/process_singleton.h"
16 #include "chrome/browser/shell_integration.h"
17 #include "chrome/browser/ui/gtk/gtk_chrome_link_button.h"
18 #include "chrome/browser/ui/gtk/gtk_util.h"
19 #include "chrome/common/pref_names.h"
20 #include "chrome/common/url_constants.h"
21 #include "chrome/installer/util/google_update_settings.h"
22 #include "components/breakpad/app/breakpad_linux.h"
23 #include "grit/chromium_strings.h"
24 #include "grit/generated_resources.h"
25 #include "grit/locale_settings.h"
26 #include "grit/theme_resources.h"
27 #include "ui/base/gtk/gtk_floating_container.h"
28 #include "ui/base/gtk/gtk_hig_constants.h"
29 #include "ui/base/l10n/l10n_util.h"
30 #include "ui/base/resource/resource_bundle.h"
32 #if defined(GOOGLE_CHROME_BUILD)
33 #include "base/prefs/pref_service.h"
34 #include "chrome/browser/browser_process.h"
39 bool ShowFirstRunDialog(Profile
* profile
) {
40 return FirstRunDialog::Show(profile
);
43 } // namespace first_run
46 bool FirstRunDialog::Show(Profile
* profile
) {
47 bool dialog_shown
= false;
48 #if defined(GOOGLE_CHROME_BUILD)
49 // If the metrics reporting is managed, we won't ask.
50 const PrefService::Preference
* metrics_reporting_pref
=
51 g_browser_process
->local_state()->FindPreference(
52 prefs::kMetricsReportingEnabled
);
53 bool show_reporting_dialog
= !metrics_reporting_pref
||
54 !metrics_reporting_pref
->IsManaged();
56 if (show_reporting_dialog
) {
57 // Object deletes itself.
58 new FirstRunDialog(profile
);
61 // TODO(port): it should be sufficient to just run the dialog:
62 // int response = gtk_dialog_run(GTK_DIALOG(dialog));
63 // but that spins a nested message loop and hoses us. :(
64 // http://code.google.com/p/chromium/issues/detail?id=12552
65 // Instead, run a loop directly here.
66 base::MessageLoop::current()->Run();
68 #endif // defined(GOOGLE_CHROME_BUILD)
72 FirstRunDialog::FirstRunDialog(Profile
* profile
)
75 report_crashes_(NULL
),
77 ShowReportingDialog();
80 FirstRunDialog::~FirstRunDialog() {
83 void FirstRunDialog::ShowReportingDialog() {
84 dialog_
= gtk_dialog_new_with_buttons(
85 l10n_util::GetStringUTF8(IDS_FIRSTRUN_DLG_TITLE
).c_str(),
87 (GtkDialogFlags
) (GTK_DIALOG_MODAL
| GTK_DIALOG_NO_SEPARATOR
),
89 gtk_util::AddButtonToDialog(dialog_
,
90 l10n_util::GetStringUTF8(IDS_FIRSTRUN_DLG_OK
).c_str(),
91 GTK_STOCK_APPLY
, GTK_RESPONSE_ACCEPT
);
92 gtk_window_set_deletable(GTK_WINDOW(dialog_
), FALSE
);
94 gtk_window_set_resizable(GTK_WINDOW(dialog_
), FALSE
);
96 g_signal_connect(dialog_
, "delete-event",
97 G_CALLBACK(gtk_widget_hide_on_delete
), NULL
);
99 GtkWidget
* content_area
= gtk_dialog_get_content_area(GTK_DIALOG(dialog_
));
101 make_default_
= gtk_check_button_new_with_label(
102 l10n_util::GetStringUTF8(IDS_FR_CUSTOMIZE_DEFAULT_BROWSER
).c_str());
103 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(make_default_
), TRUE
);
104 gtk_box_pack_start(GTK_BOX(content_area
), make_default_
, FALSE
, FALSE
, 0);
106 report_crashes_
= gtk_check_button_new();
107 GtkWidget
* check_label
= gtk_label_new(
108 l10n_util::GetStringUTF8(IDS_OPTIONS_ENABLE_LOGGING
).c_str());
109 gtk_label_set_line_wrap(GTK_LABEL(check_label
), TRUE
);
110 gtk_container_add(GTK_CONTAINER(report_crashes_
), check_label
);
111 GtkWidget
* learn_more_vbox
= gtk_vbox_new(FALSE
, 0);
112 gtk_box_pack_start(GTK_BOX(learn_more_vbox
), report_crashes_
,
115 GtkWidget
* learn_more_link
= gtk_chrome_link_button_new(
116 l10n_util::GetStringUTF8(IDS_LEARN_MORE
).c_str());
117 gtk_button_set_alignment(GTK_BUTTON(learn_more_link
), 0.0, 0.5);
118 gtk_box_pack_start(GTK_BOX(learn_more_vbox
),
119 gtk_util::IndentWidget(learn_more_link
),
121 g_signal_connect(learn_more_link
, "clicked",
122 G_CALLBACK(OnLearnMoreLinkClickedThunk
), this);
124 gtk_box_pack_start(GTK_BOX(content_area
), learn_more_vbox
, FALSE
, FALSE
, 0);
126 g_signal_connect(dialog_
, "response",
127 G_CALLBACK(OnResponseDialogThunk
), this);
128 gtk_widget_show_all(dialog_
);
131 void FirstRunDialog::OnResponseDialog(GtkWidget
* widget
, int response
) {
133 gtk_widget_hide_all(dialog_
);
135 // Check if user has opted into reporting.
136 if (report_crashes_
&&
137 gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(report_crashes_
))) {
138 if (GoogleUpdateSettings::SetCollectStatsConsent(true))
139 breakpad::InitCrashReporter(std::string());
141 GoogleUpdateSettings::SetCollectStatsConsent(false);
144 // If selected set as default browser.
146 gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(make_default_
))) {
147 ShellIntegration::SetAsDefaultBrowser();
153 void FirstRunDialog::OnLearnMoreLinkClicked(GtkButton
* button
) {
154 platform_util::OpenExternal(profile_
, GURL(chrome::kLearnMoreReportingURL
));
157 void FirstRunDialog::FirstRunDone() {
158 first_run::SetShouldShowWelcomePage();
161 gtk_widget_destroy(dialog_
);
162 base::MessageLoop::current()->Quit();