Chromium Code Reviews| Index: chrome/browser/chromeos/ui/idle_logout_dialog.cc |
| diff --git a/chrome/browser/chromeos/ui/idle_logout_dialog.cc b/chrome/browser/chromeos/ui/idle_logout_dialog.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..f55e5bfca1b3b36e625d78527964e2aa68e19178 |
| --- /dev/null |
| +++ b/chrome/browser/chromeos/ui/idle_logout_dialog.cc |
| @@ -0,0 +1,205 @@ |
| +// Copyright (c) 2012 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 "chrome/browser/chromeos/ui/idle_logout_dialog.h" |
| + |
| +#include "base/bind.h" |
| +#include "base/bind_helpers.h" |
| +#include "base/time.h" |
| +#include "base/timer.h" |
| +#include "base/string_number_conversions.h" |
| +#include "chrome/browser/chromeos/kiosk_mode/kiosk_mode_helper.h" |
| +#include "chrome/browser/ui/browser_list.h" |
| +#include "chrome/common/logging_chrome.h" |
| +#include "grit/browser_resources.h" |
| +#include "grit/generated_resources.h" |
| +#include "ui/base/l10n/l10n_util.h" |
| +#include "ui/base/resource/resource_bundle.h" |
| +#include "ui/views/layout/grid_layout.h" |
| +#include "ui/views/layout/layout_constants.h" |
| +#include "ui/views/widget/widget.h" |
| + |
| +#if defined(USE_AURA) |
| +#include "ash/shell.h" |
| +#include "ui/aura/root_window.h" |
| +#endif |
| + |
| +using views::GridLayout; |
|
tfarina
2012/03/13 00:04:32
you can remove this now.
rkc
2012/03/13 00:36:38
Done.
|
| + |
| +namespace { |
| + |
| +// Global singleton instance of our dialog class. |
| +IdleLogoutDialog* g_instance = NULL; |
| +// Height/Width of the logout dialog. |
| +const int kIdleLogoutDialogWidth = 400; |
| +const int kIdleLogoutDialogHeight = 100; |
| + |
| +const int kCountdownUpdateInterval = 1; // second. |
| + |
| +} // namespace |
| + |
| +//////////////////////////////////////////////////////////////////////////////// |
| +// IdleLogoutDialog public static methods |
| + |
| +void IdleLogoutDialog::ShowIdleLogoutDialog() { |
| + if (!g_instance) |
| + g_instance = new IdleLogoutDialog(); |
| + g_instance->Show(); |
| +} |
| + |
| +void IdleLogoutDialog::CloseIdleLogoutDialog() { |
| + if (g_instance) { |
| + g_instance->set_closed(); |
| + g_instance->Close(); |
| + g_instance = NULL; |
| + } |
| +} |
| + |
| +//////////////////////////////////////////////////////////////////////////////// |
| +// Overridden from views::DialogDelegateView |
| +int IdleLogoutDialog::GetDialogButtons() const { |
| + return ui::DIALOG_BUTTON_NONE; |
| +} |
| + |
| +ui::ModalType IdleLogoutDialog::GetModalType() const { |
| + return ui::MODAL_TYPE_WINDOW; |
| +} |
| + |
| +string16 IdleLogoutDialog::GetWindowTitle() const { |
| + return l10n_util::GetStringUTF16(IDS_IDLE_LOGOUT_TITLE); |
| +} |
| + |
| +views::View* IdleLogoutDialog::GetContentsView() { |
| + return this; |
| +} |
| + |
| +void IdleLogoutDialog::DeleteDelegate() { |
| + // There isn't a delegate method that is called on close and is 'not' called |
| + // async; this can cause an issue with us setting the g_instance to NULL |
| + // 'after' another Show call has been called on it. So instead, we rely on |
| + // CloseIdleLogoutDialog to set g_instance to NULL; in the case that we get |
| + // closed by any other way than CloseIdleLogoutDialog, we check if our |
| + // 'closed' state is set - if not, then we set it and set g_instance to null, |
| + // since that means that CloseIdleLogoutDialog was never called. |
| + if (!this->is_closed()) { |
| + g_instance->set_closed(); |
| + g_instance = NULL; |
| + } |
| + |
| + // CallInit succeeded, hence didn't free this pointer, free it here. |
| + if (chromeos::KioskModeHelper::Get()->is_initialized()) |
| + delete instance_holder_; |
| + |
| + delete this; |
| +} |
| + |
| +//////////////////////////////////////////////////////////////////////////////// |
| +// Overridden from views::View |
| +void IdleLogoutDialog::Layout() { |
| + GetLayoutManager()->Layout(this); |
|
tfarina
2012/03/13 00:04:32
Do you really need to override this? Doesn't the d
rkc
2012/03/13 00:36:38
Nope.
|
| +} |
| + |
| +gfx::Size IdleLogoutDialog::GetPreferredSize() { |
| + return gfx::Size(kIdleLogoutDialogWidth, kIdleLogoutDialogHeight); |
| +} |
| + |
| +//////////////////////////////////////////////////////////////////////////////// |
| +// IdleLogoutDialog private methods |
| +IdleLogoutDialog::IdleLogoutDialog() : restart_label_(NULL), |
| + warning_label_(NULL) { |
| + instance_holder_ = new IdleLogoutDialog*(this); |
| + Init(); |
|
tfarina
2012/03/13 00:04:32
nit: Can you call Init() outside of the ctor? Not
rkc
2012/03/13 00:36:38
Done.
|
| +} |
| + |
| +// Calls init if the instance it was called from has not been closed yet. |
| +void CallInit(IdleLogoutDialog** instance_holder) { |
|
tfarina
2012/03/13 00:04:32
Can you move this to the unnamed namespace above?
rkc
2012/03/13 00:36:38
Made the function a static method; didn't seem to
|
| + if (*instance_holder) |
| + (*instance_holder)->Init(); |
| + else |
| + // Our class is gone, free the holder memory. |
| + delete instance_holder; |
| +} |
| + |
| +void IdleLogoutDialog::Init() { |
| + if (!chromeos::KioskModeHelper::Get()->is_initialized()) { |
| + chromeos::KioskModeHelper::Get()->Initialize( |
| + base::Bind(&CallInit, instance_holder_)); |
| + return; |
| + } |
| + |
| + ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); |
| + |
| + warning_label_ = new views::Label( |
| + l10n_util::GetStringUTF16(IDS_IDLE_LOGOUT_WARNING)); |
| + warning_label_->SetHorizontalAlignment(views::Label::ALIGN_LEFT); |
| + warning_label_->SetMultiLine(true); |
| + warning_label_->SetFont(rb.GetFont(ui::ResourceBundle::BaseFont)); |
| + |
| + restart_label_ = new views::Label(); |
| + restart_label_->SetHorizontalAlignment(views::Label::ALIGN_LEFT); |
| + restart_label_->SetFont(rb.GetFont(ui::ResourceBundle::BoldFont)); |
| + |
| + views::GridLayout* layout = views::GridLayout::CreatePanel(this); |
| + SetLayoutManager(layout); |
| + |
| + views::ColumnSet* column_set = layout->AddColumnSet(0); |
| + column_set->AddColumn(views::GridLayout::FILL, views::GridLayout::CENTER, 1, |
| + views::GridLayout::USE_PREF, 0, 0); |
| + layout->StartRow(0, 0); |
| + layout->AddView(warning_label_); |
| + layout->AddPaddingRow(0, views::kUnrelatedControlVerticalSpacing); |
| + layout->StartRow(0, 0); |
| + layout->AddView(restart_label_); |
| +} |
| + |
| +void IdleLogoutDialog::Show() { |
| + // Setup the countdown label before showing. |
| + countdown_end_time_ = base::Time::Now() + base::TimeDelta::FromSeconds( |
| + chromeos::KioskModeHelper::Get()->GetIdleLogoutWarningTimeout()); |
| + UpdateCountdownTimer(); |
| + |
| +#if defined(USE_AURA) |
| + aura::RootWindow* root_window = ash::Shell::GetRootWindow(); |
| + views::Widget::CreateWindowWithParent(this, root_window); |
| + GetWidget()->Show(); |
| +#else |
| + NOTIMPLEMENTED(); |
| +#endif |
| + |
| + // Update countdown every 1 second. |
| + timer_.Start(FROM_HERE, |
| + base::TimeDelta::FromSeconds(kCountdownUpdateInterval), |
| + this, |
| + &IdleLogoutDialog::UpdateCountdownTimer); |
| +} |
| + |
| +void IdleLogoutDialog::Close() { |
| + DCHECK(GetWidget()); |
| + |
| + timer_.Stop(); |
| + GetWidget()->Close(); |
| +} |
| + |
| +void IdleLogoutDialog::UpdateCountdownTimer() { |
| + base::TimeDelta logout_warning_time = countdown_end_time_ - |
| + base::Time::Now(); |
| + int64 seconds_left = (logout_warning_time.InMillisecondsF() / |
| + base::Time::kMillisecondsPerSecond) + 0.5; |
| + |
| + if (seconds_left > 1) { |
| + restart_label_->SetText(l10n_util::GetStringFUTF16( |
| + IDS_IDLE_LOGOUT_WARNING_RESTART, |
| + string16(base::Int64ToString16(seconds_left)))); |
|
tfarina
2012/03/13 00:04:32
nit: isn't Int64ToString16 already returning a str
rkc
2012/03/13 00:36:38
Done.
|
| + } else if (seconds_left > 0) { |
| + restart_label_->SetText(l10n_util::GetStringUTF16( |
| + IDS_IDLE_LOGOUT_WARNING_RESTART_1S)); |
| + } else { |
| + // Set the label - the logout probably won't be instant. |
| + restart_label_->SetText(l10n_util::GetStringUTF16( |
| + IDS_IDLE_LOGOUT_WARNING_RESTART_NOW)); |
| + |
| + // Logout the current user. |
| + BrowserList::AttemptUserExit(); |
| + } |
| +} |