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/timer.h" | |
tfarina
2012/03/14 22:55:34
nit: already included in the header, remove.
rkc
2012/03/14 23:19:29
Done.
| |
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" | |
tfarina
2012/03/14 22:55:34
can you remove this as well?
rkc
2012/03/14 23:19:29
Done.
| |
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/controls/label.h" | |
20 #include "ui/views/layout/grid_layout.h" | |
21 #include "ui/views/layout/layout_constants.h" | |
22 #include "ui/views/widget/widget.h" | |
23 | |
24 #if defined(USE_AURA) | |
25 #include "ash/shell.h" | |
tfarina
2012/03/14 22:55:34
What do you need these includes for?
rkc
2012/03/14 23:19:29
Already removed in the latest patch.
| |
26 #include "ui/aura/root_window.h" | |
27 #endif | |
28 | |
29 namespace { | |
30 | |
31 // Global singleton instance of our dialog class. | |
32 IdleLogoutDialogView* g_instance = NULL; | |
33 | |
34 const int kIdleLogoutDialogMaxWidth = 400; | |
35 const int kCountdownUpdateInterval = 1; // second. | |
36 | |
37 } // namespace | |
38 | |
39 //////////////////////////////////////////////////////////////////////////////// | |
40 // IdleLogoutDialog public static methods | |
tfarina
2012/03/14 22:55:34
nit: IdleLogoutDialogView
rkc
2012/03/14 23:19:29
Done.
| |
41 void IdleLogoutDialogView::ShowDialog() { | |
42 if (!g_instance) { | |
43 g_instance = new IdleLogoutDialogView(); | |
44 g_instance->Init(); | |
45 } | |
46 g_instance->Show(); | |
47 } | |
48 | |
49 void IdleLogoutDialogView::CloseDialog() { | |
50 if (g_instance) { | |
51 g_instance->set_closed(); | |
52 g_instance->Close(); | |
53 g_instance = NULL; | |
54 } | |
55 } | |
56 | |
57 //////////////////////////////////////////////////////////////////////////////// | |
58 // Overridden from views::DialogDelegateView | |
59 int IdleLogoutDialogView::GetDialogButtons() const { | |
60 return ui::DIALOG_BUTTON_NONE; | |
61 } | |
62 | |
63 ui::ModalType IdleLogoutDialogView::GetModalType() const { | |
64 return ui::MODAL_TYPE_WINDOW; | |
65 } | |
66 | |
67 string16 IdleLogoutDialogView::GetWindowTitle() const { | |
68 return l10n_util::GetStringUTF16(IDS_IDLE_LOGOUT_TITLE); | |
69 } | |
70 | |
71 views::View* IdleLogoutDialogView::GetContentsView() { | |
72 return this; | |
73 } | |
74 | |
75 void IdleLogoutDialogView::DeleteDelegate() { | |
76 // There isn't a delegate method that is called on close and is 'not' called | |
77 // async; this can cause an issue with us setting the g_instance to NULL | |
78 // 'after' another Show call has been called on it. So instead, we rely on | |
79 // CloseIdleLogoutDialog to set g_instance to NULL; in the case that we get | |
80 // closed by any other way than CloseIdleLogoutDialog, we check if our | |
81 // 'closed' state is set - if not, then we set it and set g_instance to null, | |
82 // since that means that CloseIdleLogoutDialog was never called. | |
83 if (!this->is_closed()) { | |
84 g_instance->set_closed(); | |
85 g_instance = NULL; | |
86 } | |
87 | |
88 // CallInit succeeded, hence didn't free this pointer, free it here. | |
89 if (chromeos::KioskModeHelper::Get()->is_initialized()) | |
90 delete instance_holder_; | |
91 | |
92 delete this; | |
93 } | |
94 | |
95 //////////////////////////////////////////////////////////////////////////////// | |
96 // Overridden from views::View | |
97 void IdleLogoutDialogView::Layout() { | |
tfarina
2012/03/14 22:55:34
Do you still need to override this?
rkc
2012/03/14 23:19:29
Doesn't seem like it anymore.
Done.
| |
98 GetLayoutManager()->Layout(this); | |
99 } | |
100 | |
101 //////////////////////////////////////////////////////////////////////////////// | |
102 // IdleLogoutDialog private methods | |
103 IdleLogoutDialogView::IdleLogoutDialogView() : restart_label_(NULL), | |
104 warning_label_(NULL) { | |
105 instance_holder_ = new IdleLogoutDialogView*(this); | |
106 } | |
107 | |
108 // static | |
109 void IdleLogoutDialogView::CallInit(IdleLogoutDialogView** instance_holder) { | |
110 if (*instance_holder) | |
111 (*instance_holder)->Init(); | |
112 else | |
113 // Our class is gone, free the holder memory. | |
114 delete instance_holder; | |
115 } | |
116 | |
117 void IdleLogoutDialogView::Init() { | |
118 if (!chromeos::KioskModeHelper::Get()->is_initialized()) { | |
119 chromeos::KioskModeHelper::Get()->Initialize( | |
120 base::Bind(&IdleLogoutDialogView::CallInit, instance_holder_)); | |
tfarina
2012/03/14 22:55:34
nit: indent 4 spaces only.
rkc
2012/03/14 23:19:29
Done.
| |
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 warning_label_->SizeToFit(kIdleLogoutDialogMaxWidth); | |
132 | |
133 restart_label_ = new views::Label(); | |
134 restart_label_->SetHorizontalAlignment(views::Label::ALIGN_LEFT); | |
135 restart_label_->SetFont(rb.GetFont(ui::ResourceBundle::BoldFont)); | |
136 warning_label_->SizeToFit(kIdleLogoutDialogMaxWidth); | |
tfarina
2012/03/14 22:55:34
nit: called twice? first in line 131 and then now
rkc
2012/03/14 23:19:29
Done.
| |
137 | |
138 views::GridLayout* layout = views::GridLayout::CreatePanel(this); | |
139 SetLayoutManager(layout); | |
140 | |
141 views::ColumnSet* column_set = layout->AddColumnSet(0); | |
142 column_set->AddColumn(views::GridLayout::FILL, views::GridLayout::CENTER, 1, | |
143 views::GridLayout::USE_PREF, 0, 0); | |
144 layout->StartRow(0, 0); | |
145 layout->AddView(warning_label_); | |
146 layout->AddPaddingRow(0, views::kUnrelatedControlVerticalSpacing); | |
147 layout->StartRow(0, 0); | |
148 layout->AddView(restart_label_); | |
149 } | |
150 | |
151 void IdleLogoutDialogView::Show() { | |
152 // Setup the countdown label before showing. | |
153 countdown_end_time_ = base::Time::Now() + base::TimeDelta::FromSeconds( | |
154 chromeos::KioskModeHelper::Get()->GetIdleLogoutWarningTimeout()); | |
155 UpdateCountdownTimer(); | |
156 | |
157 views::Widget::CreateWindow(this); | |
158 GetWidget()->SetAlwaysOnTop(true); | |
159 GetWidget()->Show(); | |
160 | |
161 // Update countdown every 1 second. | |
162 timer_.Start(FROM_HERE, | |
163 base::TimeDelta::FromSeconds(kCountdownUpdateInterval), | |
164 this, | |
165 &IdleLogoutDialogView::UpdateCountdownTimer); | |
166 } | |
167 | |
168 void IdleLogoutDialogView::Close() { | |
169 DCHECK(GetWidget()); | |
170 | |
171 timer_.Stop(); | |
172 GetWidget()->Close(); | |
173 } | |
174 | |
175 void IdleLogoutDialogView::UpdateCountdownTimer() { | |
176 base::TimeDelta logout_warning_time = countdown_end_time_ - | |
177 base::Time::Now(); | |
178 int64 seconds_left = (logout_warning_time.InMillisecondsF() / | |
179 base::Time::kMillisecondsPerSecond) + 0.5; | |
180 | |
181 if (seconds_left > 1) { | |
182 restart_label_->SetText(l10n_util::GetStringFUTF16( | |
183 IDS_IDLE_LOGOUT_WARNING_RESTART, | |
184 base::Int64ToString16(seconds_left))); | |
185 } else if (seconds_left > 0) { | |
186 restart_label_->SetText(l10n_util::GetStringUTF16( | |
187 IDS_IDLE_LOGOUT_WARNING_RESTART_1S)); | |
188 } else { | |
189 // Set the label - the logout probably won't be instant. | |
190 restart_label_->SetText(l10n_util::GetStringUTF16( | |
191 IDS_IDLE_LOGOUT_WARNING_RESTART_NOW)); | |
192 | |
193 // Logout the current user. | |
194 BrowserList::AttemptUserExit(); | |
195 } | |
196 } | |
OLD | NEW |