OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/ui/ash/multi_user/user_switch_util.h" | 5 #include "chrome/browser/ui/ash/multi_user/user_switch_util.h" |
6 | 6 |
7 #include "ash/common/strings/grit/ash_strings.h" | 7 #include "ash/common/strings/grit/ash_strings.h" |
8 #include "ash/common/system/chromeos/screen_security/screen_tray_item.h" | 8 #include "ash/common/system/chromeos/screen_security/screen_tray_item.h" |
9 #include "ash/common/system/tray/system_tray.h" | 9 #include "ash/common/system/tray/system_tray.h" |
10 #include "ash/common/wm/overview/window_selector_controller.h" | 10 #include "ash/common/wm/overview/window_selector_controller.h" |
11 #include "ash/common/wm_shell.h" | 11 #include "ash/common/wm_shell.h" |
12 #include "ash/shell.h" | 12 #include "ash/shell.h" |
| 13 #include "chrome/browser/ui/simple_message_box.h" |
13 #include "ui/base/l10n/l10n_util.h" | 14 #include "ui/base/l10n/l10n_util.h" |
14 #include "ui/base/resource/resource_bundle.h" | |
15 #include "ui/views/controls/label.h" | |
16 #include "ui/views/layout/grid_layout.h" | |
17 #include "ui/views/widget/widget.h" | |
18 #include "ui/views/window/dialog_delegate.h" | |
19 | |
20 namespace { | |
21 | |
22 // Default width/height of the dialog. | |
23 const int kDefaultWidth = 500; | |
24 const int kDefaultHeight = 150; | |
25 | |
26 const int kPaddingToMessage = 30; | |
27 const int kInset = 40; | |
28 const int kTopInset = 10; | |
29 | |
30 //////////////////////////////////////////////////////////////////////////////// | |
31 // Dialog for multi-profiles desktop casting warning. | |
32 class DesktopCastingWarningView : public views::DialogDelegateView { | |
33 public: | |
34 explicit DesktopCastingWarningView(base::Callback<void()> on_accept); | |
35 ~DesktopCastingWarningView() override; | |
36 | |
37 static void ShowDialog(const base::Callback<void()> on_accept); | |
38 | |
39 // views::DialogDelegate overrides. | |
40 bool Accept() override; | |
41 base::string16 GetDialogButtonLabel(ui::DialogButton button) const override; | |
42 bool IsDialogButtonEnabled(ui::DialogButton button) const override; | |
43 int GetDefaultDialogButton() const override; | |
44 | |
45 // views::WidgetDelegate overrides. | |
46 ui::ModalType GetModalType() const override; | |
47 | |
48 // views::View overrides. | |
49 gfx::Size GetPreferredSize() const override; | |
50 | |
51 private: | |
52 void InitDialog(); | |
53 | |
54 const base::Callback<void()> on_switch_; | |
55 | |
56 DISALLOW_COPY_AND_ASSIGN(DesktopCastingWarningView); | |
57 }; | |
58 | |
59 // The current instance of the running dialog - or NULL. This is used for | |
60 // unittest related functions. | |
61 static DesktopCastingWarningView* instance_for_test; | |
62 | |
63 //////////////////////////////////////////////////////////////////////////////// | |
64 // DesktopCastingWarningView implementation. | |
65 | |
66 DesktopCastingWarningView::DesktopCastingWarningView( | |
67 const base::Callback<void()> on_switch) | |
68 : on_switch_(on_switch) { | |
69 DCHECK(!instance_for_test); | |
70 instance_for_test = this; | |
71 } | |
72 | |
73 DesktopCastingWarningView::~DesktopCastingWarningView() { | |
74 DCHECK(instance_for_test); | |
75 instance_for_test = NULL; | |
76 } | |
77 | |
78 // static | |
79 void DesktopCastingWarningView::ShowDialog( | |
80 const base::Callback<void()> on_accept) { | |
81 DesktopCastingWarningView* dialog_view = | |
82 new DesktopCastingWarningView(on_accept); | |
83 views::DialogDelegate::CreateDialogWidget( | |
84 dialog_view, ash::Shell::GetTargetRootWindow(), NULL); | |
85 dialog_view->InitDialog(); | |
86 views::Widget* widget = dialog_view->GetWidget(); | |
87 DCHECK(widget); | |
88 widget->Show(); | |
89 } | |
90 | |
91 bool DesktopCastingWarningView::Accept() { | |
92 // Stop screen sharing and capturing. | |
93 ash::SystemTray* system_tray = | |
94 ash::Shell::GetInstance()->GetPrimarySystemTray(); | |
95 if (system_tray->GetScreenShareItem()->is_started()) | |
96 system_tray->GetScreenShareItem()->Stop(); | |
97 if (system_tray->GetScreenCaptureItem()->is_started()) | |
98 system_tray->GetScreenCaptureItem()->Stop(); | |
99 | |
100 on_switch_.Run(); | |
101 return true; | |
102 } | |
103 | |
104 base::string16 DesktopCastingWarningView::GetDialogButtonLabel( | |
105 ui::DialogButton button) const { | |
106 return l10n_util::GetStringUTF16( | |
107 button == ui::DIALOG_BUTTON_OK | |
108 ? IDS_DESKTOP_CASTING_ACTIVE_BUTTON_SWITCH_USER | |
109 : IDS_DESKTOP_CASTING_ACTIVE_BUTTON_ABORT_USER_SWITCH); | |
110 } | |
111 | |
112 bool DesktopCastingWarningView::IsDialogButtonEnabled( | |
113 ui::DialogButton button) const { | |
114 return button == ui::DIALOG_BUTTON_OK || button == ui::DIALOG_BUTTON_CANCEL; | |
115 } | |
116 | |
117 int DesktopCastingWarningView::GetDefaultDialogButton() const { | |
118 // The default should turn off the casting. | |
119 return ui::DIALOG_BUTTON_CANCEL; | |
120 } | |
121 | |
122 ui::ModalType DesktopCastingWarningView::GetModalType() const { | |
123 return ui::MODAL_TYPE_SYSTEM; | |
124 } | |
125 | |
126 gfx::Size DesktopCastingWarningView::GetPreferredSize() const { | |
127 return gfx::Size(kDefaultWidth, kDefaultHeight); | |
128 } | |
129 | |
130 void DesktopCastingWarningView::InitDialog() { | |
131 const gfx::Insets kDialogInsets(kTopInset, kInset, kInset, kInset); | |
132 | |
133 // Create the views and layout manager and set them up. | |
134 views::GridLayout* grid_layout = views::GridLayout::CreatePanel(this); | |
135 grid_layout->SetInsets(kDialogInsets); | |
136 | |
137 views::ColumnSet* column_set = grid_layout->AddColumnSet(0); | |
138 column_set->AddColumn(views::GridLayout::FILL, views::GridLayout::FILL, 1, | |
139 views::GridLayout::USE_PREF, 0, 0); | |
140 | |
141 // Title | |
142 views::Label* title_label_ = new views::Label( | |
143 l10n_util::GetStringUTF16(IDS_DESKTOP_CASTING_ACTIVE_TITLE)); | |
144 title_label_->SetFontList(ui::ResourceBundle::GetSharedInstance().GetFontList( | |
145 ui::ResourceBundle::MediumBoldFont)); | |
146 title_label_->SetHorizontalAlignment(gfx::ALIGN_LEFT); | |
147 grid_layout->StartRow(0, 0); | |
148 grid_layout->AddView(title_label_); | |
149 grid_layout->AddPaddingRow(0, kPaddingToMessage); | |
150 | |
151 // Explanation string | |
152 views::Label* label = new views::Label( | |
153 l10n_util::GetStringUTF16(IDS_DESKTOP_CASTING_ACTIVE_MESSAGE)); | |
154 label->SetFontList(ui::ResourceBundle::GetSharedInstance().GetFontList( | |
155 ui::ResourceBundle::MediumFont)); | |
156 label->SetMultiLine(true); | |
157 label->SetHorizontalAlignment(gfx::ALIGN_LEFT); | |
158 label->SetAllowCharacterBreak(true); | |
159 grid_layout->StartRow(0, 0); | |
160 grid_layout->AddView(label); | |
161 | |
162 SetLayoutManager(grid_layout); | |
163 Layout(); | |
164 } | |
165 | |
166 } // namespace | |
167 | |
168 //////////////////////////////////////////////////////////////////////////////// | |
169 // Factory function. | |
170 | 15 |
171 void TrySwitchingActiveUser(const base::Callback<void()> on_switch) { | 16 void TrySwitchingActiveUser(const base::Callback<void()> on_switch) { |
172 // Some unit tests do not have a shell. In that case simply execute. | 17 // Some unit tests do not have a shell. In that case simply execute. |
173 if (!ash::Shell::HasInstance()) { | 18 if (!ash::Shell::HasInstance()) { |
174 on_switch.Run(); | 19 on_switch.Run(); |
175 return; | 20 return; |
176 } | 21 } |
177 | 22 |
178 // Cancel overview mode when switching user profiles. | 23 // Cancel overview mode when switching user profiles. |
179 ash::WindowSelectorController* controller = | 24 ash::WindowSelectorController* controller = |
180 ash::WmShell::Get()->window_selector_controller(); | 25 ash::WmShell::Get()->window_selector_controller(); |
181 if (controller->IsSelecting()) | 26 if (controller->IsSelecting()) |
182 controller->ToggleOverview(); | 27 controller->ToggleOverview(); |
183 | 28 |
184 // If neither screen sharing nor capturing is going on we can immediately | 29 // If neither screen sharing nor capturing is going on we can immediately |
185 // switch users. | 30 // switch users. |
186 ash::SystemTray* system_tray = | 31 ash::SystemTray* system_tray = |
187 ash::Shell::GetInstance()->GetPrimarySystemTray(); | 32 ash::Shell::GetInstance()->GetPrimarySystemTray(); |
188 if (!system_tray->GetScreenShareItem()->is_started() && | 33 if (!system_tray->GetScreenShareItem()->is_started() && |
189 !system_tray->GetScreenCaptureItem()->is_started()) { | 34 !system_tray->GetScreenCaptureItem()->is_started()) { |
190 on_switch.Run(); | 35 on_switch.Run(); |
191 return; | 36 return; |
192 } | 37 } |
193 DesktopCastingWarningView::ShowDialog(on_switch); | 38 if (chrome::ShowQuestionMessageBox( |
| 39 nullptr, l10n_util::GetStringUTF16(IDS_DESKTOP_CASTING_ACTIVE_TITLE), |
| 40 l10n_util::GetStringUTF16(IDS_DESKTOP_CASTING_ACTIVE_MESSAGE)) == |
| 41 chrome::MESSAGE_BOX_RESULT_YES) { |
| 42 // Stop screen sharing and capturing. |
| 43 ash::SystemTray* system_tray = |
| 44 ash::Shell::GetInstance()->GetPrimarySystemTray(); |
| 45 if (system_tray->GetScreenShareItem()->is_started()) |
| 46 system_tray->GetScreenShareItem()->Stop(); |
| 47 if (system_tray->GetScreenCaptureItem()->is_started()) |
| 48 system_tray->GetScreenCaptureItem()->Stop(); |
| 49 |
| 50 on_switch.Run(); |
| 51 } |
194 } | 52 } |
195 | |
196 bool TestAndTerminateDesktopCastingWarningForTest(bool accept) { | |
197 if (!instance_for_test) | |
198 return false; | |
199 if (accept) | |
200 instance_for_test->Accept(); | |
201 delete instance_for_test->GetWidget()->GetNativeWindow(); | |
202 CHECK(!instance_for_test); | |
203 return true; | |
204 } | |
OLD | NEW |