first_run_dialog.cc revision 5d1f7b1de12d16ceb2c938c56701a3e8bfa558f7
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/client/dispatcher_client.h" 19#include "ui/aura/env.h" 20#include "ui/aura/root_window.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 29#if defined(GOOGLE_CHROME_BUILD) 30#include "base/prefs/pref_service.h" 31#include "chrome/browser/browser_process.h" 32#endif 33 34using views::GridLayout; 35 36namespace first_run { 37 38bool ShowFirstRunDialog(Profile* profile) { 39 return FirstRunDialog::Show(profile); 40} 41 42} // namespace first_run 43 44// static 45bool FirstRunDialog::Show(Profile* profile) { 46 bool dialog_shown = false; 47 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 54 if (!metrics_reporting_pref || 55 !metrics_reporting_pref->IsManaged()) { 56 FirstRunDialog* dialog = new FirstRunDialog(profile); 57 views::DialogDelegate::CreateDialogWidget(dialog, NULL, NULL)->Show(); 58 59 // Use the widget's window itself so that the message loop 60 // exists when the dialog is closed by some other means than 61 // |Accept|. 62 // 63 // This is the same trick used in simple_message_box_views.cc, minus the 64 // refcounting. 65 aura::Window* anchor = dialog->GetWidget()->GetNativeWindow(); 66 aura::client::DispatcherClient* client = 67 aura::client::GetDispatcherClient(anchor->GetRootWindow()); 68 client->RunWithDispatcher(dialog, anchor); 69 dialog_shown = true; 70 } 71#endif // defined(GOOGLE_CHROME_BUILD) 72 73 return dialog_shown; 74} 75 76FirstRunDialog::FirstRunDialog(Profile* profile) 77 : profile_(profile), 78 make_default_(NULL), 79 report_crashes_(NULL), 80 should_show_dialog_(true) { 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 105views::View* FirstRunDialog::CreateExtraView() { 106 views::Link* link = new views::Link(l10n_util::GetStringUTF16( 107 IDS_LEARN_MORE)); 108 link->set_listener(this); 109 return link; 110} 111 112void FirstRunDialog::OnClosed() { 113 should_show_dialog_ = false; 114 first_run::SetShouldShowWelcomePage(); 115} 116 117bool FirstRunDialog::Accept() { 118 should_show_dialog_ = false; 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 return true; 132} 133 134int FirstRunDialog::GetDialogButtons() const { 135 return ui::DIALOG_BUTTON_OK; 136} 137 138void FirstRunDialog::LinkClicked(views::Link* source, int event_flags) { 139 platform_util::OpenExternal(profile_, GURL(chrome::kLearnMoreReportingURL)); 140} 141 142uint32_t FirstRunDialog::Dispatch(const base::NativeEvent& event) { 143 uint32_t action = POST_DISPATCH_PERFORM_DEFAULT; 144 if (!should_show_dialog_) 145 action |= POST_DISPATCH_QUIT_LOOP; 146 return action; 147} 148