Home | History | Annotate | Download | only in views
      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 
     35 using views::GridLayout;
     36 
     37 namespace first_run {
     38 
     39 bool ShowFirstRunDialog(Profile* profile) {
     40   return FirstRunDialog::Show(profile);
     41 }
     42 
     43 }  // namespace first_run
     44 
     45 // static
     46 bool 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     aura::client::DispatcherRunLoop run_loop(client, NULL);
     70     dialog->quit_runloop_ = run_loop.QuitClosure();
     71     run_loop.Run();
     72     dialog_shown = true;
     73   }
     74 #endif  // defined(GOOGLE_CHROME_BUILD)
     75 
     76   return dialog_shown;
     77 }
     78 
     79 FirstRunDialog::FirstRunDialog(Profile* profile)
     80     : profile_(profile),
     81       make_default_(NULL),
     82       report_crashes_(NULL) {
     83   GridLayout* layout = GridLayout::CreatePanel(this);
     84   SetLayoutManager(layout);
     85 
     86   const int related_y = views::kRelatedControlVerticalSpacing;
     87 
     88   views::ColumnSet* column_set = layout->AddColumnSet(0);
     89   column_set->AddColumn(GridLayout::FILL, GridLayout::CENTER, 0,
     90                         GridLayout::USE_PREF, 0, 0);
     91 
     92   layout->StartRow(0, 0);
     93   make_default_ = new views::Checkbox(l10n_util::GetStringUTF16(
     94       IDS_FR_CUSTOMIZE_DEFAULT_BROWSER));
     95   make_default_->SetChecked(true);
     96   layout->AddView(make_default_);
     97 
     98   layout->StartRowWithPadding(0, 0, 0, related_y);
     99   report_crashes_ = new views::Checkbox(l10n_util::GetStringUTF16(
    100       IDS_OPTIONS_ENABLE_LOGGING));
    101   layout->AddView(report_crashes_);
    102 }
    103 
    104 FirstRunDialog::~FirstRunDialog() {
    105 }
    106 
    107 void FirstRunDialog::Done() {
    108   CHECK(!quit_runloop_.is_null());
    109   quit_runloop_.Run();
    110 }
    111 
    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;
    117 }
    118 
    119 void FirstRunDialog::OnClosed() {
    120   first_run::SetShouldShowWelcomePage();
    121   Done();
    122 }
    123 
    124 bool 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 
    141 int FirstRunDialog::GetDialogButtons() const {
    142   return ui::DIALOG_BUTTON_OK;
    143 }
    144 
    145 void FirstRunDialog::LinkClicked(views::Link* source, int event_flags) {
    146   platform_util::OpenExternal(profile_, GURL(chrome::kLearnMoreReportingURL));
    147 }
    148