Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / ash / system / chromeos / session / logout_confirmation_dialog.cc
blob80a71e84248a1dfda9e6b56a231dcaef55168f49
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 "ash/system/chromeos/session/logout_confirmation_dialog.h"
7 #include "ash/shell.h"
8 #include "ash/system/chromeos/session/logout_confirmation_controller.h"
9 #include "ash/system/tray/tray_constants.h"
10 #include "base/location.h"
11 #include "base/time/tick_clock.h"
12 #include "grit/ash_strings.h"
13 #include "ui/base/l10n/l10n_util.h"
14 #include "ui/base/l10n/time_format.h"
15 #include "ui/base/ui_base_types.h"
16 #include "ui/gfx/text_constants.h"
17 #include "ui/views/border.h"
18 #include "ui/views/controls/label.h"
19 #include "ui/views/layout/fill_layout.h"
20 #include "ui/views/widget/widget.h"
22 namespace ash {
23 namespace {
25 const int kCountdownUpdateIntervalMs = 1000; // 1 second.
27 const int kHalfSecondInMs = 500; // Half a second.
29 } // namespace
31 LogoutConfirmationDialog::LogoutConfirmationDialog(
32 LogoutConfirmationController* controller,
33 base::TimeTicks logout_time)
34 : controller_(controller),
35 logout_time_(logout_time) {
36 SetLayoutManager(new views::FillLayout());
38 label_ = new views::Label;
39 label_->SetBorder(views::Border::CreateEmptyBorder(
40 0, kTrayPopupPaddingHorizontal, 0, kTrayPopupPaddingHorizontal));
41 label_->SetHorizontalAlignment(gfx::ALIGN_LEFT);
42 label_->SetMultiLine(true);
43 AddChildView(label_);
45 UpdateLabel();
47 CreateDialogWidget(this, ash::Shell::GetPrimaryRootWindow(), NULL);
48 GetWidget()->Show();
50 update_timer_.Start(
51 FROM_HERE,
52 base::TimeDelta::FromMilliseconds(kCountdownUpdateIntervalMs),
53 this,
54 &LogoutConfirmationDialog::UpdateLabel);
57 LogoutConfirmationDialog::~LogoutConfirmationDialog() {
60 void LogoutConfirmationDialog::Update(base::TimeTicks logout_time) {
61 logout_time_ = logout_time;
62 UpdateLabel();
65 bool LogoutConfirmationDialog::Accept() {
66 logout_time_ = controller_->clock()->NowTicks();
67 UpdateLabel();
68 controller_->OnLogoutConfirmed();
69 return true;
72 ui::ModalType LogoutConfirmationDialog::GetModalType() const {
73 return ui::MODAL_TYPE_SYSTEM;
76 base::string16 LogoutConfirmationDialog::GetWindowTitle() const {
77 return l10n_util::GetStringUTF16(IDS_ASH_LOGOUT_CONFIRMATION_TITLE);
80 base::string16 LogoutConfirmationDialog::GetDialogButtonLabel(
81 ui::DialogButton button) const {
82 if (button == ui::DIALOG_BUTTON_OK)
83 return l10n_util::GetStringUTF16(IDS_ASH_LOGOUT_CONFIRMATION_BUTTON);
84 return views::DialogDelegateView::GetDialogButtonLabel(button);
87 void LogoutConfirmationDialog::OnClosed() {
88 update_timer_.Stop();
89 controller_->OnDialogClosed();
92 void LogoutConfirmationDialog::DeleteDelegate() {
93 delete this;
96 void LogoutConfirmationDialog::UpdateLabel() {
97 const base::TimeDelta time_remaining =
98 logout_time_ - controller_->clock()->NowTicks();
99 if (time_remaining >= base::TimeDelta::FromMilliseconds(kHalfSecondInMs)) {
100 label_->SetText(l10n_util::GetStringFUTF16(
101 IDS_ASH_LOGOUT_CONFIRMATION_WARNING,
102 ui::TimeFormat::Detailed(ui::TimeFormat::FORMAT_DURATION,
103 ui::TimeFormat::LENGTH_LONG,
105 time_remaining)));
106 } else {
107 label_->SetText(l10n_util::GetStringUTF16(
108 IDS_ASH_LOGOUT_CONFIRMATION_WARNING_NOW));
109 update_timer_.Stop();
113 } // namespace ash