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

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: drop weak pointer for owner 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() {
41 return base::TimeTicks::Now();
42 }
43
44 base::TimeDelta LogoutConfirmationDialogView::Delegate::GetUpdateInterval() {
45 return base::TimeDelta::FromSeconds(1);
46 }
47
48 LogoutConfirmationDialogView::LogoutConfirmationDialogView(
49 LogoutButtonTray* owner, Delegate* delegate) : owner_(owner),
50 delegate_(delegate) {
51 text_label_ = new views::Label;
52 text_label_->set_border(
53 views::Border::CreateEmptyBorder(0, kTrayPopupPaddingHorizontal,
54 0, kTrayPopupPaddingHorizontal));
55 text_label_->SetHorizontalAlignment(gfx::ALIGN_LEFT);
56 text_label_->SetMultiLine(true);
57
58 SetLayoutManager(new views::FillLayout());
59
60 AddChildView(text_label_);
61 }
62
63 LogoutConfirmationDialogView::~LogoutConfirmationDialogView() {
64 }
65
66 void LogoutConfirmationDialogView::DeleteDelegate() {
67 OnClosed();
68 delete this;
69 }
70
71 bool LogoutConfirmationDialogView::Accept() {
72 LogoutCurrentUser();
73 return true;
74 }
75
76 ui::ModalType LogoutConfirmationDialogView::GetModalType() const {
77 return ui::MODAL_TYPE_SYSTEM;
78 }
79
80 string16 LogoutConfirmationDialogView::GetWindowTitle() const {
81 return l10n_util::GetStringUTF16(IDS_ASH_LOGOUT_CONFIRMATION_TITLE);
82 }
83
84 string16 LogoutConfirmationDialogView::GetDialogButtonLabel(
85 ui::DialogButton button) const {
86 if (button == ui::DIALOG_BUTTON_OK)
87 return l10n_util::GetStringUTF16(IDS_ASH_LOGOUT_CONFIRMATION_BUTTON);
88 return views::DialogDelegateView::GetDialogButtonLabel(button);
89 }
90
91 void LogoutConfirmationDialogView::OnClosed() {
92 if (owner_) {
93 owner_->DeleteConfirmationDialog();
94 timer_.Stop();
95 // nullify the delegate to prevent future activities of the dialog.
96 delegate_ = NULL;
97 }
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 delegate_->GetUpdateInterval(),
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::NullifyOwner() {
129 owner_ = NULL;
130 }
131
132 void LogoutConfirmationDialogView::LogoutCurrentUser() {
133 if (!delegate_)
134 return;
135 // Backup the delegate, since closing widget will nullify it.
136 Delegate* delegate = delegate_;
137 // For testing purpose.
138 if (GetWidget()) {
139 GetWidget()->Close();
140 delegate->LogoutCurrentUser();
141 } else {
142 delegate->LogoutCurrentUser();
143 DeleteDelegate();
144 }
145 }
146
147 void LogoutConfirmationDialogView::UpdateCountdown() {
148 if (!delegate_)
149 return;
150 const base::TimeDelta time_remaining = countdown_start_time_
151 + duration_ - delegate_->GetCurrentTime();
152 // Round the remaining time to nearest second, and use this value for
153 // the countdown display and actual enforcement.
154 int seconds_remaining = round(time_remaining.InSecondsF());
155 if (seconds_remaining > 0) {
156 text_label_->SetText(l10n_util::GetStringFUTF16(
157 IDS_ASH_LOGOUT_CONFIRMATION_WARNING,
158 ui::TimeFormat::TimeDurationLong(base::TimeDelta::FromSeconds(
159 seconds_remaining))));
160 } else {
161 text_label_->SetText(l10n_util::GetStringUTF16(
162 IDS_ASH_LOGOUT_CONFIRMATION_WARNING_NOW));
163 timer_.Stop();
164 LogoutCurrentUser();
165 }
166 }
167
168 } // namespace internal
169 } // namespace ash
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698