Chromium Code Reviews| Index: ash/system/logout_button/logout_confirmation_dialog_view.cc |
| diff --git a/ash/system/logout_button/logout_confirmation_dialog_view.cc b/ash/system/logout_button/logout_confirmation_dialog_view.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..9bd80ee72a8bfa0ff074a6e56164b10b82b9d58b |
| --- /dev/null |
| +++ b/ash/system/logout_button/logout_confirmation_dialog_view.cc |
| @@ -0,0 +1,167 @@ |
| +// Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "ash/system/logout_button/logout_confirmation_dialog_view.h" |
| + |
| +#include <cmath> |
| + |
| +#include "ash/shell.h" |
| +#include "ash/system/logout_button/logout_button_tray.h" |
| +#include "ash/system/tray/system_tray_delegate.h" |
| +#include "ash/system/tray/tray_constants.h" |
| +#include "base/location.h" |
| +#include "grit/ash_strings.h" |
| +#include "ui/aura/root_window.h" |
| +#include "ui/base/l10n/l10n_util.h" |
| +#include "ui/base/l10n/time_format.h" |
| +#include "ui/base/ui_base_types.h" |
| +#include "ui/gfx/text_constants.h" |
| +#include "ui/views/border.h" |
| +#include "ui/views/controls/label.h" |
| +#include "ui/views/layout/fill_layout.h" |
| +#include "ui/views/widget/widget.h" |
| + |
| +namespace ash { |
| +namespace internal { |
| + |
| +LogoutConfirmationDialogView::Delegate::Delegate() { |
| +} |
| + |
| +LogoutConfirmationDialogView::Delegate::~Delegate() { |
| +} |
| + |
| +void |
| +LogoutConfirmationDialogView::Delegate::LogoutCurrentUser() { |
| + if (Shell::HasInstance()) |
| + Shell::GetInstance()->system_tray_delegate()->SignOut(); |
| +} |
| + |
| +base::TimeTicks LogoutConfirmationDialogView::Delegate::GetCurrentTime() const { |
| + return base::TimeTicks::Now(); |
| +} |
| + |
| +base::TimeDelta LogoutConfirmationDialogView::Delegate::GetUpdateInterval() |
| + const { |
| + return base::TimeDelta::FromSeconds(1); |
| +} |
| + |
| +LogoutConfirmationDialogView::LogoutConfirmationDialogView( |
| + LogoutButtonTray* owner, Delegate* delegate) : owner_(owner), |
| + delegate_(delegate) { |
| + text_label_ = new views::Label; |
| + text_label_->set_border( |
| + views::Border::CreateEmptyBorder(0, kTrayPopupPaddingHorizontal, |
| + 0, kTrayPopupPaddingHorizontal)); |
| + text_label_->SetHorizontalAlignment(gfx::ALIGN_LEFT); |
| + text_label_->SetMultiLine(true); |
| + |
| + SetLayoutManager(new views::FillLayout()); |
| + |
| + AddChildView(text_label_); |
| +} |
| + |
| +LogoutConfirmationDialogView::~LogoutConfirmationDialogView() { |
| +} |
| + |
| +void LogoutConfirmationDialogView::DeleteDelegate() { |
| + OnClosed(); |
|
bartfab (slow)
2013/12/12 16:04:13
As discussed offline, it is sufficient to just do
binjin
2013/12/17 10:29:49
Done.
|
| + delete this; |
| +} |
| + |
| +bool LogoutConfirmationDialogView::Accept() { |
| + LogoutCurrentUser(); |
| + return true; |
| +} |
| + |
| +ui::ModalType LogoutConfirmationDialogView::GetModalType() const { |
| + return ui::MODAL_TYPE_SYSTEM; |
| +} |
| + |
| +string16 LogoutConfirmationDialogView::GetWindowTitle() const { |
| + return l10n_util::GetStringUTF16(IDS_ASH_LOGOUT_CONFIRMATION_TITLE); |
| +} |
| + |
| +string16 LogoutConfirmationDialogView::GetDialogButtonLabel( |
| + ui::DialogButton button) const { |
| + if (button == ui::DIALOG_BUTTON_OK) |
| + return l10n_util::GetStringUTF16(IDS_ASH_LOGOUT_CONFIRMATION_BUTTON); |
| + return views::DialogDelegateView::GetDialogButtonLabel(button); |
| +} |
| + |
| +void LogoutConfirmationDialogView::OnClosed() { |
| + if (owner_) { |
|
bartfab (slow)
2013/12/12 16:04:13
How can the owner_ ever be NULL?
binjin
2013/12/17 10:29:49
I'm not sure the reason, but OnClosed() is actuall
bartfab (slow)
2013/12/17 13:21:03
This is a bit worrying. It would seem that OnClose
binjin
2013/12/17 14:58:08
I checked the stack trace for both calls. here is
bartfab (slow)
2013/12/17 15:57:46
I think allowing OnClosed() to be called twice is
binjin
2013/12/18 18:13:14
Done.
|
| + owner_->DeleteConfirmationDialog(); |
| + owner_ = NULL; |
| + timer_.Stop(); |
| + // nullify the delegate to prevent future activities of the dialog. |
|
bartfab (slow)
2013/12/12 16:04:13
Nit: s/nullify/Nullify/
binjin
2013/12/17 10:29:49
Done.
|
| + delegate_ = NULL; |
| + } |
| +} |
| + |
| +void LogoutConfirmationDialogView::Show(base::TimeDelta duration) { |
| + if (!delegate_) |
| + return; |
| + countdown_start_time_ = delegate_->GetCurrentTime(); |
| + duration_ = duration; |
| + |
| + UpdateCountdown(); |
| + |
| + // For testing purpose, only actually display the dialog if an existing |
| + // ash::Shell is available. |
| + if (ash::Shell::HasInstance()) { |
| + views::DialogDelegate::CreateDialogWidget( |
| + this, ash::Shell::GetPrimaryRootWindow(), NULL); |
| + GetWidget()->Show(); |
| + } |
| + |
| + timer_.Start(FROM_HERE, |
| + delegate_->GetUpdateInterval(), |
| + this, |
| + &LogoutConfirmationDialogView::UpdateCountdown); |
| +} |
| + |
| +void LogoutConfirmationDialogView::UpdateDialogDuration( |
| + base::TimeDelta duration) { |
| + duration_ = duration; |
| + UpdateCountdown(); |
| +} |
| + |
| +void LogoutConfirmationDialogView::LogoutCurrentUser() { |
| + if (!delegate_) |
| + return; |
| + // Backup the delegate, since closing widget will nullify it. |
| + Delegate* delegate = delegate_; |
| + // For testing purpose. |
| + if (GetWidget()) { |
| + GetWidget()->Close(); |
| + delegate->LogoutCurrentUser(); |
|
bartfab (slow)
2013/12/12 16:04:13
Why did you choose this ordering? If you called Lo
binjin
2013/12/17 10:29:49
I choose this ordering because I think the session
|
| + } else { |
| + delegate->LogoutCurrentUser(); |
| + DeleteDelegate(); |
|
bartfab (slow)
2013/12/12 16:04:13
Could you call Close() here instead?
binjin
2013/12/17 10:29:49
I think the default implementation of DialogDelega
|
| + } |
| +} |
| + |
| +void LogoutConfirmationDialogView::UpdateCountdown() { |
| + if (!delegate_) |
| + return; |
| + const base::TimeDelta time_remaining = countdown_start_time_ |
| + + duration_ - delegate_->GetCurrentTime(); |
|
bartfab (slow)
2013/12/12 16:04:13
Nit: The style guide says the + should go on the p
binjin
2013/12/17 10:29:49
Done.
|
| + // Round the remaining time to nearest second, and use this value for |
| + // the countdown display and actual enforcement. |
| + int seconds_remaining = round(time_remaining.InSecondsF()); |
| + if (seconds_remaining > 0) { |
| + text_label_->SetText(l10n_util::GetStringFUTF16( |
| + IDS_ASH_LOGOUT_CONFIRMATION_WARNING, |
| + ui::TimeFormat::TimeDurationLong(base::TimeDelta::FromSeconds( |
| + seconds_remaining)))); |
| + } else { |
| + text_label_->SetText(l10n_util::GetStringUTF16( |
| + IDS_ASH_LOGOUT_CONFIRMATION_WARNING_NOW)); |
| + timer_.Stop(); |
| + LogoutCurrentUser(); |
|
bartfab (slow)
2013/12/12 16:04:13
At this point, the dialog is still open and the de
binjin
2013/12/17 10:29:49
The LogoutCurrentUser() here is LogoutConfirmation
|
| + } |
| +} |
| + |
| +} // namespace internal |
| +} // namespace ash |