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