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; | |
tfarina
2012/03/13 00:04:32
you can remove this now.
rkc
2012/03/13 00:36:38
Done.
| |
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 | |
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); | |
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.
| |
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 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.
| |
113 } | |
114 | |
115 // Calls init if the instance it was called from has not been closed yet. | |
116 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
| |
117 if (*instance_holder) | |
118 (*instance_holder)->Init(); | |
119 else | |
120 // Our class is gone, free the holder memory. | |
121 delete instance_holder; | |
122 } | |
123 | |
124 void IdleLogoutDialog::Init() { | |
125 if (!chromeos::KioskModeHelper::Get()->is_initialized()) { | |
126 chromeos::KioskModeHelper::Get()->Initialize( | |
127 base::Bind(&CallInit, instance_holder_)); | |
128 return; | |
129 } | |
130 | |
131 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); | |
132 | |
133 warning_label_ = new views::Label( | |
134 l10n_util::GetStringUTF16(IDS_IDLE_LOGOUT_WARNING)); | |
135 warning_label_->SetHorizontalAlignment(views::Label::ALIGN_LEFT); | |
136 warning_label_->SetMultiLine(true); | |
137 warning_label_->SetFont(rb.GetFont(ui::ResourceBundle::BaseFont)); | |
138 | |
139 restart_label_ = new views::Label(); | |
140 restart_label_->SetHorizontalAlignment(views::Label::ALIGN_LEFT); | |
141 restart_label_->SetFont(rb.GetFont(ui::ResourceBundle::BoldFont)); | |
142 | |
143 views::GridLayout* layout = views::GridLayout::CreatePanel(this); | |
144 SetLayoutManager(layout); | |
145 | |
146 views::ColumnSet* column_set = layout->AddColumnSet(0); | |
147 column_set->AddColumn(views::GridLayout::FILL, views::GridLayout::CENTER, 1, | |
148 views::GridLayout::USE_PREF, 0, 0); | |
149 layout->StartRow(0, 0); | |
150 layout->AddView(warning_label_); | |
151 layout->AddPaddingRow(0, views::kUnrelatedControlVerticalSpacing); | |
152 layout->StartRow(0, 0); | |
153 layout->AddView(restart_label_); | |
154 } | |
155 | |
156 void IdleLogoutDialog::Show() { | |
157 // Setup the countdown label before showing. | |
158 countdown_end_time_ = base::Time::Now() + base::TimeDelta::FromSeconds( | |
159 chromeos::KioskModeHelper::Get()->GetIdleLogoutWarningTimeout()); | |
160 UpdateCountdownTimer(); | |
161 | |
162 #if defined(USE_AURA) | |
163 aura::RootWindow* root_window = ash::Shell::GetRootWindow(); | |
164 views::Widget::CreateWindowWithParent(this, root_window); | |
165 GetWidget()->Show(); | |
166 #else | |
167 NOTIMPLEMENTED(); | |
168 #endif | |
169 | |
170 // Update countdown every 1 second. | |
171 timer_.Start(FROM_HERE, | |
172 base::TimeDelta::FromSeconds(kCountdownUpdateInterval), | |
173 this, | |
174 &IdleLogoutDialog::UpdateCountdownTimer); | |
175 } | |
176 | |
177 void IdleLogoutDialog::Close() { | |
178 DCHECK(GetWidget()); | |
179 | |
180 timer_.Stop(); | |
181 GetWidget()->Close(); | |
182 } | |
183 | |
184 void IdleLogoutDialog::UpdateCountdownTimer() { | |
185 base::TimeDelta logout_warning_time = countdown_end_time_ - | |
186 base::Time::Now(); | |
187 int64 seconds_left = (logout_warning_time.InMillisecondsF() / | |
188 base::Time::kMillisecondsPerSecond) + 0.5; | |
189 | |
190 if (seconds_left > 1) { | |
191 restart_label_->SetText(l10n_util::GetStringFUTF16( | |
192 IDS_IDLE_LOGOUT_WARNING_RESTART, | |
193 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.
| |
194 } else if (seconds_left > 0) { | |
195 restart_label_->SetText(l10n_util::GetStringUTF16( | |
196 IDS_IDLE_LOGOUT_WARNING_RESTART_1S)); | |
197 } else { | |
198 // Set the label - the logout probably won't be instant. | |
199 restart_label_->SetText(l10n_util::GetStringUTF16( | |
200 IDS_IDLE_LOGOUT_WARNING_RESTART_NOW)); | |
201 | |
202 // Logout the current user. | |
203 BrowserList::AttemptUserExit(); | |
204 } | |
205 } | |
OLD | NEW |