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 using views::GridLayout; | |
29 | |
30 namespace { | |
31 | |
32 // Global singleton instance of our dialog class. | |
33 IdleLogoutDialog* g_instance = NULL; | |
34 // Height/Width of the logout dialog. | |
35 const int kIdleLogoutDialogWidth = 400; | |
36 const int kIdleLogoutDialogHeight = 100; | |
37 | |
38 const int kCountdownUpdateInterval = 1; // second. | |
39 | |
40 } // namespace | |
41 | |
42 //////////////////////////////////////////////////////////////////////////////// | |
43 // IdleLogoutDialog public static methods | |
44 | |
45 void IdleLogoutDialog::ShowIdleLogoutDialog() { | |
46 if (!g_instance) | |
47 g_instance = new IdleLogoutDialog(); | |
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 delete this; | |
90 } | |
91 | |
92 | |
93 //////////////////////////////////////////////////////////////////////////////// | |
94 // Overridden from views::View | |
95 void IdleLogoutDialog::Layout() { | |
96 GetLayoutManager()->Layout(this); | |
97 } | |
98 | |
99 gfx::Size IdleLogoutDialog::GetPreferredSize() { | |
100 return gfx::Size(kIdleLogoutDialogWidth, kIdleLogoutDialogHeight); | |
101 } | |
102 | |
103 //////////////////////////////////////////////////////////////////////////////// | |
104 // IdleLogoutDialog private methods | |
105 IdleLogoutDialog::IdleLogoutDialog() : restart_label_(NULL), | |
sky
2012/03/12 23:33:38
nit: wrap to next line and indent 4.
rkc
2012/03/12 23:57:51
Done.
| |
106 warning_label_(NULL) { | |
107 instance_holder_ = new IdleLogoutDialog*(this); | |
108 Init(); | |
109 } | |
110 | |
111 // Calls init if the instance it was called from has not been closed yet. | |
112 void CallInit(IdleLogoutDialog** instance_holder) { | |
113 if (*instance_holder) | |
sky
2012/03/12 23:33:38
Is instance_holder_ leaked?
rkc
2012/03/12 23:57:51
Was, fixed.
| |
114 (*instance_holder)->Init(); | |
115 } | |
116 | |
117 void IdleLogoutDialog::Init() { | |
118 if (!chromeos::KioskModeHelper::Get()->is_initialized()) { | |
119 chromeos::KioskModeHelper::Get()->Initialize( | |
120 base::Bind(&CallInit, instance_holder_)); | |
121 return; | |
122 } | |
123 | |
124 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); | |
125 | |
126 warning_label_ = new views::Label( | |
127 l10n_util::GetStringUTF16(IDS_IDLE_LOGOUT_WARNING)); | |
128 warning_label_->SetHorizontalAlignment(views::Label::ALIGN_LEFT); | |
129 warning_label_->SetMultiLine(true); | |
130 warning_label_->SetFont(rb.GetFont(ui::ResourceBundle::BaseFont)); | |
131 | |
132 restart_label_ = new views::Label(); | |
133 restart_label_->SetHorizontalAlignment(views::Label::ALIGN_LEFT); | |
134 restart_label_->SetFont(rb.GetFont(ui::ResourceBundle::BoldFont)); | |
135 | |
136 views::GridLayout* layout = views::GridLayout::CreatePanel(this); | |
137 SetLayoutManager(layout); | |
138 | |
139 views::ColumnSet* column_set = layout->AddColumnSet(0); | |
140 column_set->AddColumn(views::GridLayout::FILL, views::GridLayout::CENTER, 1, | |
141 views::GridLayout::USE_PREF, 0, 0); | |
142 layout->StartRow(0, 0); | |
143 layout->AddView(warning_label_); | |
144 layout->AddPaddingRow(0, views::kUnrelatedControlVerticalSpacing); | |
145 layout->StartRow(0, 0); | |
146 layout->AddView(restart_label_); | |
147 } | |
148 | |
149 void IdleLogoutDialog::Show() { | |
150 // Setup the countdown label before showing. | |
151 countdown_end_time_ = base::Time::Now() + base::TimeDelta::FromSeconds( | |
152 chromeos::KioskModeHelper::Get()->GetIdleLogoutWarningTimeout()); | |
153 UpdateCountdownTimer(); | |
154 | |
155 #if defined(USE_AURA) | |
156 aura::RootWindow* root_window = ash::Shell::GetRootWindow(); | |
157 views::Widget::CreateWindowWithParent(this, root_window); | |
sky
2012/03/12 23:33:38
Why are you parenting this to the root window?
rkc
2012/03/12 23:57:51
The IdleLogout can come up at any point; whether o
sky
2012/03/13 03:43:54
I believe that means it could go in from of the lo
rkc
2012/03/14 22:35:32
Done.
| |
158 GetWidget()->Show(); | |
159 #else | |
160 NOTIMPLEMENTED(); | |
161 #endif | |
162 | |
163 // Update countdown every 1 second. | |
164 timer_.Start(FROM_HERE, | |
165 base::TimeDelta::FromSeconds(kCountdownUpdateInterval), | |
166 this, | |
167 &IdleLogoutDialog::UpdateCountdownTimer); | |
168 } | |
169 | |
170 void IdleLogoutDialog::Close() { | |
171 DCHECK(GetWidget()); | |
172 | |
173 timer_.Stop(); | |
174 GetWidget()->Close(); | |
175 } | |
176 | |
177 void IdleLogoutDialog::UpdateCountdownTimer() { | |
178 base::TimeDelta logout_warning_time = countdown_end_time_ - | |
179 base::Time::Now(); | |
180 int64 seconds_left = (logout_warning_time.InMillisecondsF() / | |
181 base::Time::kMillisecondsPerSecond) + 0.5; | |
182 | |
183 if (seconds_left > 1) { | |
184 restart_label_->SetText(l10n_util::GetStringFUTF16( | |
185 IDS_IDLE_LOGOUT_WARNING_RESTART, | |
186 string16(base::Int64ToString16(seconds_left)))); | |
187 } else if (seconds_left > 0) { | |
188 restart_label_->SetText(l10n_util::GetStringUTF16( | |
189 IDS_IDLE_LOGOUT_WARNING_RESTART_1S)); | |
190 } else { | |
191 // Set the label - the logout probably won't be instant. | |
192 restart_label_->SetText(l10n_util::GetStringUTF16( | |
193 IDS_IDLE_LOGOUT_WARNING_RESTART_NOW)); | |
194 | |
195 // Logout the current user. | |
196 BrowserList::AttemptUserExit(); | |
197 } | |
198 } | |
OLD | NEW |