OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2013 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/logout_button/logout_confirmation_dialog_view.h" | |
6 | |
7 #include <cmath> | |
8 | |
9 #include "ash/shell.h" | |
10 #include "ash/system/logout_button/logout_button_tray.h" | |
11 #include "ash/system/tray/system_tray_delegate.h" | |
12 #include "ash/system/tray/tray_constants.h" | |
13 #include "base/location.h" | |
14 #include "grit/ash_strings.h" | |
15 #include "ui/aura/root_window.h" | |
16 #include "ui/base/l10n/l10n_util.h" | |
17 #include "ui/base/l10n/time_format.h" | |
18 #include "ui/base/ui_base_types.h" | |
19 #include "ui/gfx/text_constants.h" | |
20 #include "ui/views/border.h" | |
21 #include "ui/views/controls/label.h" | |
22 #include "ui/views/layout/fill_layout.h" | |
23 #include "ui/views/widget/widget.h" | |
24 | |
25 namespace ash { | |
26 namespace internal { | |
27 | |
28 namespace { | |
29 const int kCountdownUpdateIntervalMs = 1000; // 1 second. | |
bartfab (slow)
2013/12/18 18:26:49
Nit: Style guide: Constants like this one should n
binjin
2013/12/18 18:42:14
Done.
| |
30 } // namespace | |
31 | |
32 LogoutConfirmationDialogView::Delegate::Delegate() { | |
33 } | |
34 | |
35 LogoutConfirmationDialogView::Delegate::~Delegate() { | |
36 } | |
37 | |
38 void LogoutConfirmationDialogView::Delegate::LogoutCurrentUser() { | |
39 DCHECK(Shell::GetInstance() != NULL); | |
bartfab (slow)
2013/12/18 18:26:49
Nit 1: No need for != NULL. DCHECK(Shell::GetInsta
binjin
2013/12/18 18:42:14
Done.
| |
40 Shell::GetInstance()->system_tray_delegate()->SignOut(); | |
41 } | |
42 | |
43 base::TimeTicks LogoutConfirmationDialogView::Delegate::GetCurrentTime() const { | |
44 return base::TimeTicks::Now(); | |
45 } | |
46 | |
47 LogoutConfirmationDialogView::LogoutConfirmationDialogView( | |
48 LogoutButtonTray* owner, Delegate* delegate) : owner_(owner), | |
49 delegate_(delegate) { | |
50 text_label_ = new views::Label; | |
51 text_label_->set_border( | |
52 views::Border::CreateEmptyBorder(0, kTrayPopupPaddingHorizontal, | |
53 0, kTrayPopupPaddingHorizontal)); | |
54 text_label_->SetHorizontalAlignment(gfx::ALIGN_LEFT); | |
55 text_label_->SetMultiLine(true); | |
56 | |
57 SetLayoutManager(new views::FillLayout()); | |
58 | |
59 AddChildView(text_label_); | |
60 } | |
61 | |
62 LogoutConfirmationDialogView::~LogoutConfirmationDialogView() { | |
63 } | |
64 | |
65 bool LogoutConfirmationDialogView::Accept() { | |
66 LogoutCurrentUser(); | |
67 return true; | |
68 } | |
69 | |
70 ui::ModalType LogoutConfirmationDialogView::GetModalType() const { | |
71 return ui::MODAL_TYPE_SYSTEM; | |
72 } | |
73 | |
74 string16 LogoutConfirmationDialogView::GetWindowTitle() const { | |
75 return l10n_util::GetStringUTF16(IDS_ASH_LOGOUT_CONFIRMATION_TITLE); | |
76 } | |
77 | |
78 string16 LogoutConfirmationDialogView::GetDialogButtonLabel( | |
79 ui::DialogButton button) const { | |
80 if (button == ui::DIALOG_BUTTON_OK) | |
81 return l10n_util::GetStringUTF16(IDS_ASH_LOGOUT_CONFIRMATION_BUTTON); | |
82 return views::DialogDelegateView::GetDialogButtonLabel(button); | |
83 } | |
84 | |
85 void LogoutConfirmationDialogView::OnClosed() { | |
86 DCHECK(owner_ != NULL); | |
bartfab (slow)
2013/12/18 18:26:49
Nit 1: No need for != NULL. DCHECK(owner_) will do
binjin
2013/12/18 18:42:14
Done.
| |
87 owner_->ReleaseConfirmationDialog(); | |
88 owner_ = NULL; | |
89 timer_.Stop(); | |
90 // Nullify the delegate to prevent future activities of the dialog. | |
91 delegate_ = NULL; | |
92 } | |
93 | |
94 void LogoutConfirmationDialogView::DeleteDelegate() { | |
95 if (owner_) | |
96 owner_->ReleaseConfirmationDialog(); | |
97 delete this; | |
98 } | |
99 | |
100 void LogoutConfirmationDialogView::Show(base::TimeDelta duration) { | |
101 if (!delegate_) | |
102 return; | |
103 countdown_start_time_ = delegate_->GetCurrentTime(); | |
104 duration_ = duration; | |
105 | |
106 UpdateCountdown(); | |
107 | |
108 // For testing purpose, only actually display the dialog if an existing | |
109 // ash::Shell is available. | |
110 if (ash::Shell::HasInstance()) { | |
111 views::DialogDelegate::CreateDialogWidget( | |
112 this, ash::Shell::GetPrimaryRootWindow(), NULL); | |
113 GetWidget()->Show(); | |
114 } | |
115 | |
116 timer_.Start(FROM_HERE, | |
117 base::TimeDelta::FromMicroseconds(kCountdownUpdateIntervalMs), | |
bartfab (slow)
2013/12/18 18:26:49
FromMilliseconds, not FromMicroseconds.
binjin
2013/12/18 18:42:14
Done.
| |
118 this, | |
119 &LogoutConfirmationDialogView::UpdateCountdown); | |
120 } | |
121 | |
122 void LogoutConfirmationDialogView::UpdateDialogDuration( | |
123 base::TimeDelta duration) { | |
124 duration_ = duration; | |
125 UpdateCountdown(); | |
126 } | |
127 | |
128 void LogoutConfirmationDialogView::LogoutCurrentUser() { | |
129 if (!delegate_) | |
130 return; | |
131 delegate_->LogoutCurrentUser(); | |
132 // For testing purpose, simulate closing of all windows during session logout. | |
133 if (!ash::Shell::HasInstance()) | |
bartfab (slow)
2013/12/18 18:26:49
This should be done in the testing code (in the Mo
binjin
2013/12/18 18:42:14
Done.
| |
134 OnClosed(); | |
135 } | |
136 | |
137 void LogoutConfirmationDialogView::UpdateCountdown() { | |
138 if (!delegate_) | |
139 return; | |
140 const base::TimeDelta time_remaining = countdown_start_time_ + | |
141 duration_ - delegate_->GetCurrentTime(); | |
142 // Round the remaining time to nearest second, and use this value for | |
143 // the countdown display and actual enforcement. | |
144 int seconds_remaining = round(time_remaining.InSecondsF()); | |
145 if (seconds_remaining > 0) { | |
146 text_label_->SetText(l10n_util::GetStringFUTF16( | |
147 IDS_ASH_LOGOUT_CONFIRMATION_WARNING, | |
148 ui::TimeFormat::TimeDurationLong(base::TimeDelta::FromSeconds( | |
149 seconds_remaining)))); | |
150 } else { | |
151 text_label_->SetText(l10n_util::GetStringUTF16( | |
152 IDS_ASH_LOGOUT_CONFIRMATION_WARNING_NOW)); | |
153 timer_.Stop(); | |
154 LogoutCurrentUser(); | |
155 } | |
156 } | |
157 | |
158 } // namespace internal | |
159 } // namespace ash | |
OLD | NEW |