OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2012 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 "chrome/browser/chromeos/ui/idle_logout_dialog.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/bind_helpers.h" |
| 9 #include "base/time.h" |
| 10 #include "base/timer.h" |
| 11 #include "base/string_number_conversions.h" |
| 12 #include "chrome/browser/chromeos/kiosk_mode/kiosk_mode_helper.h" |
| 13 #include "chrome/browser/ui/browser_list.h" |
| 14 #include "chrome/common/logging_chrome.h" |
| 15 #include "grit/browser_resources.h" |
| 16 #include "grit/generated_resources.h" |
| 17 #include "ui/base/l10n/l10n_util.h" |
| 18 #include "ui/base/resource/resource_bundle.h" |
| 19 #include "ui/views/layout/grid_layout.h" |
| 20 #include "ui/views/layout/layout_constants.h" |
| 21 #include "ui/views/widget/widget.h" |
| 22 |
| 23 #if defined(USE_AURA) |
| 24 #include "ash/shell.h" |
| 25 #include "ui/aura/root_window.h" |
| 26 #endif |
| 27 |
| 28 namespace { |
| 29 |
| 30 // Global singleton instance of our dialog class. |
| 31 IdleLogoutDialog* g_instance = NULL; |
| 32 // Height/Width of the logout dialog. |
| 33 const int kIdleLogoutDialogWidth = 400; |
| 34 const int kIdleLogoutDialogHeight = 100; |
| 35 |
| 36 const int kCountdownUpdateInterval = 1; // second. |
| 37 |
| 38 } // namespace |
| 39 |
| 40 //////////////////////////////////////////////////////////////////////////////// |
| 41 // IdleLogoutDialog public static methods |
| 42 |
| 43 void IdleLogoutDialog::ShowIdleLogoutDialog() { |
| 44 if (!g_instance) { |
| 45 g_instance = new IdleLogoutDialog(); |
| 46 g_instance->Init(); |
| 47 } |
| 48 g_instance->Show(); |
| 49 } |
| 50 |
| 51 void IdleLogoutDialog::CloseIdleLogoutDialog() { |
| 52 if (g_instance) { |
| 53 g_instance->set_closed(); |
| 54 g_instance->Close(); |
| 55 g_instance = NULL; |
| 56 } |
| 57 } |
| 58 |
| 59 //////////////////////////////////////////////////////////////////////////////// |
| 60 // Overridden from views::DialogDelegateView |
| 61 int IdleLogoutDialog::GetDialogButtons() const { |
| 62 return ui::DIALOG_BUTTON_NONE; |
| 63 } |
| 64 |
| 65 ui::ModalType IdleLogoutDialog::GetModalType() const { |
| 66 return ui::MODAL_TYPE_WINDOW; |
| 67 } |
| 68 |
| 69 string16 IdleLogoutDialog::GetWindowTitle() const { |
| 70 return l10n_util::GetStringUTF16(IDS_IDLE_LOGOUT_TITLE); |
| 71 } |
| 72 |
| 73 views::View* IdleLogoutDialog::GetContentsView() { |
| 74 return this; |
| 75 } |
| 76 |
| 77 void IdleLogoutDialog::DeleteDelegate() { |
| 78 // There isn't a delegate method that is called on close and is 'not' called |
| 79 // async; this can cause an issue with us setting the g_instance to NULL |
| 80 // 'after' another Show call has been called on it. So instead, we rely on |
| 81 // CloseIdleLogoutDialog to set g_instance to NULL; in the case that we get |
| 82 // closed by any other way than CloseIdleLogoutDialog, we check if our |
| 83 // 'closed' state is set - if not, then we set it and set g_instance to null, |
| 84 // since that means that CloseIdleLogoutDialog was never called. |
| 85 if (!this->is_closed()) { |
| 86 g_instance->set_closed(); |
| 87 g_instance = NULL; |
| 88 } |
| 89 |
| 90 // CallInit succeeded, hence didn't free this pointer, free it here. |
| 91 if (chromeos::KioskModeHelper::Get()->is_initialized()) |
| 92 delete instance_holder_; |
| 93 |
| 94 delete this; |
| 95 } |
| 96 |
| 97 //////////////////////////////////////////////////////////////////////////////// |
| 98 // Overridden from views::View |
| 99 void IdleLogoutDialog::Layout() { |
| 100 GetLayoutManager()->Layout(this); |
| 101 } |
| 102 |
| 103 gfx::Size IdleLogoutDialog::GetPreferredSize() { |
| 104 return gfx::Size(kIdleLogoutDialogWidth, kIdleLogoutDialogHeight); |
| 105 } |
| 106 |
| 107 //////////////////////////////////////////////////////////////////////////////// |
| 108 // IdleLogoutDialog private methods |
| 109 IdleLogoutDialog::IdleLogoutDialog() : restart_label_(NULL), |
| 110 warning_label_(NULL) { |
| 111 instance_holder_ = new IdleLogoutDialog*(this); |
| 112 } |
| 113 |
| 114 // static |
| 115 void IdleLogoutDialog::CallInit(IdleLogoutDialog** instance_holder) { |
| 116 if (*instance_holder) |
| 117 (*instance_holder)->Init(); |
| 118 else |
| 119 // Our class is gone, free the holder memory. |
| 120 delete instance_holder; |
| 121 } |
| 122 |
| 123 void IdleLogoutDialog::Init() { |
| 124 if (!chromeos::KioskModeHelper::Get()->is_initialized()) { |
| 125 chromeos::KioskModeHelper::Get()->Initialize( |
| 126 base::Bind(&IdleLogoutDialog::CallInit, instance_holder_)); |
| 127 return; |
| 128 } |
| 129 |
| 130 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); |
| 131 |
| 132 warning_label_ = new views::Label( |
| 133 l10n_util::GetStringUTF16(IDS_IDLE_LOGOUT_WARNING)); |
| 134 warning_label_->SetHorizontalAlignment(views::Label::ALIGN_LEFT); |
| 135 warning_label_->SetMultiLine(true); |
| 136 warning_label_->SetFont(rb.GetFont(ui::ResourceBundle::BaseFont)); |
| 137 |
| 138 restart_label_ = new views::Label(); |
| 139 restart_label_->SetHorizontalAlignment(views::Label::ALIGN_LEFT); |
| 140 restart_label_->SetFont(rb.GetFont(ui::ResourceBundle::BoldFont)); |
| 141 |
| 142 views::GridLayout* layout = views::GridLayout::CreatePanel(this); |
| 143 SetLayoutManager(layout); |
| 144 |
| 145 views::ColumnSet* column_set = layout->AddColumnSet(0); |
| 146 column_set->AddColumn(views::GridLayout::FILL, views::GridLayout::CENTER, 1, |
| 147 views::GridLayout::USE_PREF, 0, 0); |
| 148 layout->StartRow(0, 0); |
| 149 layout->AddView(warning_label_); |
| 150 layout->AddPaddingRow(0, views::kUnrelatedControlVerticalSpacing); |
| 151 layout->StartRow(0, 0); |
| 152 layout->AddView(restart_label_); |
| 153 } |
| 154 |
| 155 void IdleLogoutDialog::Show() { |
| 156 // Setup the countdown label before showing. |
| 157 countdown_end_time_ = base::Time::Now() + base::TimeDelta::FromSeconds( |
| 158 chromeos::KioskModeHelper::Get()->GetIdleLogoutWarningTimeout()); |
| 159 UpdateCountdownTimer(); |
| 160 |
| 161 #if defined(USE_AURA) |
| 162 aura::RootWindow* root_window = ash::Shell::GetRootWindow(); |
| 163 views::Widget::CreateWindowWithParent(this, root_window); |
| 164 GetWidget()->Show(); |
| 165 #else |
| 166 NOTIMPLEMENTED(); |
| 167 #endif |
| 168 |
| 169 // Update countdown every 1 second. |
| 170 timer_.Start(FROM_HERE, |
| 171 base::TimeDelta::FromSeconds(kCountdownUpdateInterval), |
| 172 this, |
| 173 &IdleLogoutDialog::UpdateCountdownTimer); |
| 174 } |
| 175 |
| 176 void IdleLogoutDialog::Close() { |
| 177 DCHECK(GetWidget()); |
| 178 |
| 179 timer_.Stop(); |
| 180 GetWidget()->Close(); |
| 181 } |
| 182 |
| 183 void IdleLogoutDialog::UpdateCountdownTimer() { |
| 184 base::TimeDelta logout_warning_time = countdown_end_time_ - |
| 185 base::Time::Now(); |
| 186 int64 seconds_left = (logout_warning_time.InMillisecondsF() / |
| 187 base::Time::kMillisecondsPerSecond) + 0.5; |
| 188 |
| 189 if (seconds_left > 1) { |
| 190 restart_label_->SetText(l10n_util::GetStringFUTF16( |
| 191 IDS_IDLE_LOGOUT_WARNING_RESTART, |
| 192 base::Int64ToString16(seconds_left))); |
| 193 } else if (seconds_left > 0) { |
| 194 restart_label_->SetText(l10n_util::GetStringUTF16( |
| 195 IDS_IDLE_LOGOUT_WARNING_RESTART_1S)); |
| 196 } else { |
| 197 // Set the label - the logout probably won't be instant. |
| 198 restart_label_->SetText(l10n_util::GetStringUTF16( |
| 199 IDS_IDLE_LOGOUT_WARNING_RESTART_NOW)); |
| 200 |
| 201 // Logout the current user. |
| 202 BrowserList::AttemptUserExit(); |
| 203 } |
| 204 } |
OLD | NEW |