Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(9)

Side by Side Diff: ash/system/logout_button/logout_confirmation_dialog_view.cc

Issue 40053002: Implements the dialog view for logout button tray in public sessions. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: more fixes Created 7 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 LogoutConfirmationDialogView::Delegate::Delegate() {
29 }
30
31 LogoutConfirmationDialogView::Delegate::~Delegate() {
32 }
33
34 void
35 LogoutConfirmationDialogView::Delegate::LogoutCurrentUser() {
36 if (Shell::HasInstance())
37 Shell::GetInstance()->system_tray_delegate()->SignOut();
38 }
39
40 base::TimeTicks LogoutConfirmationDialogView::Delegate::GetCurrentTime() const {
41 return base::TimeTicks::Now();
42 }
43
44 base::TimeDelta LogoutConfirmationDialogView::Delegate::GetUpdateInterval()
45 const {
46 return base::TimeDelta::FromSeconds(1);
47 }
48
49 LogoutConfirmationDialogView::LogoutConfirmationDialogView(
50 LogoutButtonTray* owner, Delegate* delegate) : owner_(owner),
51 delegate_(delegate) {
52 text_label_ = new views::Label;
53 text_label_->set_border(
54 views::Border::CreateEmptyBorder(0, kTrayPopupPaddingHorizontal,
55 0, kTrayPopupPaddingHorizontal));
56 text_label_->SetHorizontalAlignment(gfx::ALIGN_LEFT);
57 text_label_->SetMultiLine(true);
58
59 SetLayoutManager(new views::FillLayout());
60
61 AddChildView(text_label_);
62 }
63
64 LogoutConfirmationDialogView::~LogoutConfirmationDialogView() {
65 }
66
67 void LogoutConfirmationDialogView::DeleteDelegate() {
68 // For testing purpose. In tests DeleteDelegate() is called directly,
69 // instead of GetWidget()->Close(). Since OnClosed() is not called in tests,
70 // some of the clean up work had to be done here.
71 if (owner_)
72 owner_->ReleaseConfirmationDialog();
73 delete this;
74 }
75
76 bool LogoutConfirmationDialogView::Accept() {
77 LogoutCurrentUser();
78 return true;
79 }
80
81 ui::ModalType LogoutConfirmationDialogView::GetModalType() const {
82 return ui::MODAL_TYPE_SYSTEM;
83 }
84
85 string16 LogoutConfirmationDialogView::GetWindowTitle() const {
86 return l10n_util::GetStringUTF16(IDS_ASH_LOGOUT_CONFIRMATION_TITLE);
87 }
88
89 string16 LogoutConfirmationDialogView::GetDialogButtonLabel(
90 ui::DialogButton button) const {
91 if (button == ui::DIALOG_BUTTON_OK)
92 return l10n_util::GetStringUTF16(IDS_ASH_LOGOUT_CONFIRMATION_BUTTON);
93 return views::DialogDelegateView::GetDialogButtonLabel(button);
94 }
95
96 void LogoutConfirmationDialogView::OnClosed() {
97 if (owner_) {
98 owner_->ReleaseConfirmationDialog();
99 owner_ = NULL;
100 timer_.Stop();
101 // Nullify the delegate to prevent future activities of the dialog.
102 delegate_ = NULL;
103 }
104 }
105
106 void LogoutConfirmationDialogView::Show(base::TimeDelta duration) {
107 if (!delegate_)
108 return;
109 countdown_start_time_ = delegate_->GetCurrentTime();
110 duration_ = duration;
111
112 UpdateCountdown();
113
114 // For testing purpose, only actually display the dialog if an existing
115 // ash::Shell is available.
116 if (ash::Shell::HasInstance()) {
117 views::DialogDelegate::CreateDialogWidget(
118 this, ash::Shell::GetPrimaryRootWindow(), NULL);
119 GetWidget()->Show();
120 }
121
122 timer_.Start(FROM_HERE,
123 delegate_->GetUpdateInterval(),
124 this,
125 &LogoutConfirmationDialogView::UpdateCountdown);
126 }
127
128 void LogoutConfirmationDialogView::UpdateDialogDuration(
129 base::TimeDelta duration) {
130 duration_ = duration;
131 UpdateCountdown();
132 }
133
134 void LogoutConfirmationDialogView::LogoutCurrentUser() {
135 if (!delegate_)
136 return;
137 // Backup the delegate, since closing widget will nullify it.
138 Delegate* delegate = delegate_;
139 // For testing purpose.
bartfab (slow) 2013/12/17 13:21:03 Nit: Could you rephrase the comment so that it is
binjin 2013/12/17 14:58:08 Done.
140 if (GetWidget()) {
141 GetWidget()->Close();
142 delegate->LogoutCurrentUser();
143 } else {
144 delegate->LogoutCurrentUser();
145 DeleteDelegate();
146 }
147 }
148
149 void LogoutConfirmationDialogView::UpdateCountdown() {
150 if (!delegate_)
151 return;
152 const base::TimeDelta time_remaining = countdown_start_time_ +
153 duration_ - delegate_->GetCurrentTime();
154 // Round the remaining time to nearest second, and use this value for
155 // the countdown display and actual enforcement.
156 int seconds_remaining = round(time_remaining.InSecondsF());
157 if (seconds_remaining > 0) {
158 text_label_->SetText(l10n_util::GetStringFUTF16(
159 IDS_ASH_LOGOUT_CONFIRMATION_WARNING,
160 ui::TimeFormat::TimeDurationLong(base::TimeDelta::FromSeconds(
161 seconds_remaining))));
162 } else {
163 text_label_->SetText(l10n_util::GetStringUTF16(
164 IDS_ASH_LOGOUT_CONFIRMATION_WARNING_NOW));
165 timer_.Stop();
166 LogoutCurrentUser();
167 }
168 }
169
170 } // namespace internal
171 } // namespace ash
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698