15d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Copyright 2014 The Chromium Authors. All rights reserved.
25d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// found in the LICENSE file.
45d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
55d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "chrome/browser/ui/views/first_run_dialog.h"
65d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
703b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)#include "base/message_loop/message_loop.h"
803b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)#include "base/run_loop.h"
95d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "chrome/browser/first_run/first_run.h"
105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "chrome/browser/platform_util.h"
115d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "chrome/browser/shell_integration.h"
125d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "chrome/common/pref_names.h"
135d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "chrome/common/url_constants.h"
141320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "chrome/grit/chromium_strings.h"
151320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "chrome/grit/generated_resources.h"
161320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "chrome/grit/locale_settings.h"
175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "chrome/installer/util/google_update_settings.h"
181320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "components/crash/app/breakpad_linux.h"
191320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "grit/components_strings.h"
205d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "ui/aura/env.h"
21a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "ui/aura/window.h"
22a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "ui/aura/window_event_dispatcher.h"
235d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "ui/base/l10n/l10n_util.h"
245d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "ui/views/controls/button/checkbox.h"
255d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "ui/views/controls/link.h"
265d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "ui/views/layout/grid_layout.h"
275d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "ui/views/layout/layout_constants.h"
285d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "ui/views/widget/widget.h"
295d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "ui/views/window/dialog_delegate.h"
305d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
315d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#if defined(GOOGLE_CHROME_BUILD)
325d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "base/prefs/pref_service.h"
335d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "chrome/browser/browser_process.h"
345d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#endif
355d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
365d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)using views::GridLayout;
375d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
385d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)namespace first_run {
395d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
405d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)bool ShowFirstRunDialog(Profile* profile) {
415d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  return FirstRunDialog::Show(profile);
425d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
435d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
445d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}  // namespace first_run
455d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
465d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// static
475d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)bool FirstRunDialog::Show(Profile* profile) {
485d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  bool dialog_shown = false;
495d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
505d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#if defined(GOOGLE_CHROME_BUILD)
515d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // If the metrics reporting is managed, we won't ask.
525d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  const PrefService::Preference* metrics_reporting_pref =
535d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      g_browser_process->local_state()->FindPreference(
545d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)          prefs::kMetricsReportingEnabled);
555d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
565d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (!metrics_reporting_pref ||
575d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      !metrics_reporting_pref->IsManaged()) {
585d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    FirstRunDialog* dialog = new FirstRunDialog(profile);
595d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    views::DialogDelegate::CreateDialogWidget(dialog, NULL, NULL)->Show();
605d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
6103b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)    base::MessageLoopForUI* loop = base::MessageLoopForUI::current();
6203b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)    base::MessageLoopForUI::ScopedNestableTaskAllower allow_nested(loop);
6303b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)    base::RunLoop run_loop;
64cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    dialog->quit_runloop_ = run_loop.QuitClosure();
65cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    run_loop.Run();
665d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    dialog_shown = true;
675d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
685d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#endif  // defined(GOOGLE_CHROME_BUILD)
695d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
705d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  return dialog_shown;
715d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
725d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
735d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)FirstRunDialog::FirstRunDialog(Profile* profile)
745d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    : profile_(profile),
755d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      make_default_(NULL),
76a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      report_crashes_(NULL) {
775d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  GridLayout* layout = GridLayout::CreatePanel(this);
785d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  SetLayoutManager(layout);
795d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
805d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  const int related_y = views::kRelatedControlVerticalSpacing;
815d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
825d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  views::ColumnSet* column_set = layout->AddColumnSet(0);
835d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  column_set->AddColumn(GridLayout::FILL, GridLayout::CENTER, 0,
845d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                        GridLayout::USE_PREF, 0, 0);
855d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
865d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  layout->StartRow(0, 0);
875d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  make_default_ = new views::Checkbox(l10n_util::GetStringUTF16(
885d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      IDS_FR_CUSTOMIZE_DEFAULT_BROWSER));
895d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  make_default_->SetChecked(true);
905d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  layout->AddView(make_default_);
915d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
925d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  layout->StartRowWithPadding(0, 0, 0, related_y);
935d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  report_crashes_ = new views::Checkbox(l10n_util::GetStringUTF16(
945d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      IDS_OPTIONS_ENABLE_LOGGING));
955d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  layout->AddView(report_crashes_);
965d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
975d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
985d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)FirstRunDialog::~FirstRunDialog() {
995d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
1005d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
101a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)void FirstRunDialog::Done() {
102cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  CHECK(!quit_runloop_.is_null());
103cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  quit_runloop_.Run();
104a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)}
105a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
1065d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)views::View* FirstRunDialog::CreateExtraView() {
1075d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  views::Link* link = new views::Link(l10n_util::GetStringUTF16(
1085d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      IDS_LEARN_MORE));
1095d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  link->set_listener(this);
1105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  return link;
1115d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
1125d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1135d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)void FirstRunDialog::OnClosed() {
1145d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  first_run::SetShouldShowWelcomePage();
115a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  Done();
1165d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
1175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1185d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)bool FirstRunDialog::Accept() {
1195d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  GetWidget()->Hide();
1205d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1215d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (report_crashes_ && report_crashes_->checked()) {
1225d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    if (GoogleUpdateSettings::SetCollectStatsConsent(true))
1235d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      breakpad::InitCrashReporter(std::string());
1245d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  } else {
1255d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    GoogleUpdateSettings::SetCollectStatsConsent(false);
1265d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
1275d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1285d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (make_default_ && make_default_->checked())
1295d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    ShellIntegration::SetAsDefaultBrowser();
1305d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
131a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  Done();
1325d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  return true;
1335d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
1345d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1355d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)int FirstRunDialog::GetDialogButtons() const {
1365d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  return ui::DIALOG_BUTTON_OK;
1375d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
1385d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1395d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)void FirstRunDialog::LinkClicked(views::Link* source, int event_flags) {
1405d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  platform_util::OpenExternal(profile_, GURL(chrome::kLearnMoreReportingURL));
1415d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
142