e795927d6b99c051dd85822760e5916b45d6aba3
[platform/framework/web/crosswalk.git] / src / ash / system / chromeos / session / logout_confirmation_dialog.cc
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 "ash/system/chromeos/session/logout_confirmation_dialog.h"
6
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"
21
22 namespace ash {
23 namespace internal {
24
25 namespace {
26
27 const int kCountdownUpdateIntervalMs = 1000;  // 1 second.
28
29 const int kHalfSecondInMs = 500;  // Half a second.
30
31 }  // namespace
32
33 LogoutConfirmationDialog::LogoutConfirmationDialog(
34     LogoutConfirmationController* controller,
35     base::TimeTicks logout_time)
36     : controller_(controller),
37       logout_time_(logout_time) {
38   SetLayoutManager(new views::FillLayout());
39
40   label_ = new views::Label;
41   label_->SetBorder(views::Border::CreateEmptyBorder(
42       0, kTrayPopupPaddingHorizontal, 0, kTrayPopupPaddingHorizontal));
43   label_->SetHorizontalAlignment(gfx::ALIGN_LEFT);
44   label_->SetMultiLine(true);
45   AddChildView(label_);
46
47   UpdateLabel();
48
49   CreateDialogWidget(this, ash::Shell::GetPrimaryRootWindow(), NULL);
50   GetWidget()->Show();
51
52   update_timer_.Start(
53       FROM_HERE,
54       base::TimeDelta::FromMilliseconds(kCountdownUpdateIntervalMs),
55       this,
56       &LogoutConfirmationDialog::UpdateLabel);
57 }
58
59 LogoutConfirmationDialog::~LogoutConfirmationDialog() {
60 }
61
62 void LogoutConfirmationDialog::Update(base::TimeTicks logout_time) {
63   logout_time_ = logout_time;
64   UpdateLabel();
65 }
66
67 bool LogoutConfirmationDialog::Accept() {
68   logout_time_ = controller_->clock()->NowTicks();
69   UpdateLabel();
70   controller_->OnLogoutConfirmed();
71   return true;
72 }
73
74 ui::ModalType LogoutConfirmationDialog::GetModalType() const {
75   return ui::MODAL_TYPE_SYSTEM;
76 }
77
78 base::string16 LogoutConfirmationDialog::GetWindowTitle() const {
79   return l10n_util::GetStringUTF16(IDS_ASH_LOGOUT_CONFIRMATION_TITLE);
80 }
81
82 base::string16 LogoutConfirmationDialog::GetDialogButtonLabel(
83     ui::DialogButton button) const {
84   if (button == ui::DIALOG_BUTTON_OK)
85     return l10n_util::GetStringUTF16(IDS_ASH_LOGOUT_CONFIRMATION_BUTTON);
86   return views::DialogDelegateView::GetDialogButtonLabel(button);
87 }
88
89 void LogoutConfirmationDialog::OnClosed() {
90   update_timer_.Stop();
91   controller_->OnDialogClosed();
92 }
93
94 void LogoutConfirmationDialog::DeleteDelegate() {
95   delete this;
96 }
97
98 void LogoutConfirmationDialog::UpdateLabel() {
99   const base::TimeDelta time_remaining =
100       logout_time_ - controller_->clock()->NowTicks();
101   if (time_remaining >= base::TimeDelta::FromMilliseconds(kHalfSecondInMs)) {
102     label_->SetText(l10n_util::GetStringFUTF16(
103         IDS_ASH_LOGOUT_CONFIRMATION_WARNING,
104         ui::TimeFormat::Detailed(ui::TimeFormat::FORMAT_DURATION,
105                                  ui::TimeFormat::LENGTH_LONG,
106                                  10,
107                                  time_remaining)));
108   } else {
109     label_->SetText(l10n_util::GetStringUTF16(
110         IDS_ASH_LOGOUT_CONFIRMATION_WARNING_NOW));
111     update_timer_.Stop();
112   }
113 }
114
115 }  // namespace internal
116 }  // namespace ash