Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / ash / system / chromeos / multi_user / user_switch_util.cc
bloba5fb6e2f76661df60e4abfb11b8e6c8f72f983d0
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
3 // found in the LICENSE file.
5 #include "ash/system/chromeos/multi_user/user_switch_util.h"
7 #include "ash/shell.h"
8 #include "ash/system/chromeos/screen_security/screen_tray_item.h"
9 #include "ash/system/tray/system_tray.h"
10 #include "grit/ash_strings.h"
11 #include "ui/base/l10n/l10n_util.h"
12 #include "ui/base/resource/resource_bundle.h"
13 #include "ui/views/controls/label.h"
14 #include "ui/views/layout/grid_layout.h"
15 #include "ui/views/widget/widget.h"
16 #include "ui/views/window/dialog_delegate.h"
18 namespace ash {
20 namespace {
22 // Default width/height of the dialog.
23 const int kDefaultWidth = 500;
24 const int kDefaultHeight = 150;
26 const int kPaddingToMessage = 30;
27 const int kInset = 40;
28 const int kTopInset = 10;
30 ////////////////////////////////////////////////////////////////////////////////
31 // Dialog for multi-profiles desktop casting warning.
32 class DesktopCastingWarningView : public views::DialogDelegateView {
33 public:
34 DesktopCastingWarningView(base::Callback<void()> on_accept);
35 ~DesktopCastingWarningView() override;
37 static void ShowDialog(const base::Callback<void()> on_accept);
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;
45 // views::WidgetDelegate overrides.
46 ui::ModalType GetModalType() const override;
48 // views::View overrides.
49 gfx::Size GetPreferredSize() const override;
51 private:
52 void InitDialog();
54 const base::Callback<void()> on_switch_;
56 DISALLOW_COPY_AND_ASSIGN(DesktopCastingWarningView);
59 // The current instance of the running dialog - or NULL. This is used for
60 // unittest related functions.
61 static DesktopCastingWarningView* instance_for_test;
63 ////////////////////////////////////////////////////////////////////////////////
64 // DesktopCastingWarningView implementation.
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;
73 DesktopCastingWarningView::~DesktopCastingWarningView() {
74 DCHECK(instance_for_test);
75 instance_for_test = NULL;
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();
91 bool DesktopCastingWarningView::Accept() {
92 // Stop screen sharing and capturing.
93 SystemTray* system_tray = ash::Shell::GetInstance()->GetPrimarySystemTray();
94 if (system_tray->GetScreenShareItem()->is_started())
95 system_tray->GetScreenShareItem()->Stop();
96 if (system_tray->GetScreenCaptureItem()->is_started())
97 system_tray->GetScreenCaptureItem()->Stop();
99 on_switch_.Run();
100 return true;
103 base::string16 DesktopCastingWarningView::GetDialogButtonLabel(
104 ui::DialogButton button) const {
105 return l10n_util::GetStringUTF16(
106 button == ui::DIALOG_BUTTON_OK ?
107 IDS_DESKTOP_CASTING_ACTIVE_BUTTON_SWITCH_USER :
108 IDS_DESKTOP_CASTING_ACTIVE_BUTTON_ABORT_USER_SWITCH);
111 bool DesktopCastingWarningView::IsDialogButtonEnabled(
112 ui::DialogButton button) const {
113 return button == ui::DIALOG_BUTTON_OK || button == ui::DIALOG_BUTTON_CANCEL;
116 int DesktopCastingWarningView::GetDefaultDialogButton() const {
117 // The default should turn off the casting.
118 return ui::DIALOG_BUTTON_CANCEL;
121 ui::ModalType DesktopCastingWarningView::GetModalType() const {
122 return ui::MODAL_TYPE_SYSTEM;
125 gfx::Size DesktopCastingWarningView::GetPreferredSize() const {
126 return gfx::Size(kDefaultWidth, kDefaultHeight);
129 void DesktopCastingWarningView::InitDialog() {
130 const gfx::Insets kDialogInsets(kTopInset, kInset, kInset, kInset);
132 // Create the views and layout manager and set them up.
133 views::GridLayout* grid_layout = views::GridLayout::CreatePanel(this);
134 grid_layout->SetInsets(kDialogInsets);
136 views::ColumnSet* column_set = grid_layout->AddColumnSet(0);
137 column_set->AddColumn(views::GridLayout::FILL, views::GridLayout::FILL, 1,
138 views::GridLayout::USE_PREF, 0, 0);
140 // Title
141 views::Label* title_label_ = new views::Label(
142 l10n_util::GetStringUTF16(IDS_DESKTOP_CASTING_ACTIVE_TITLE));
143 title_label_->SetFontList(ui::ResourceBundle::GetSharedInstance().GetFontList(
144 ui::ResourceBundle::MediumBoldFont));
145 title_label_->SetHorizontalAlignment(gfx::ALIGN_LEFT);
146 grid_layout->StartRow(0, 0);
147 grid_layout->AddView(title_label_);
148 grid_layout->AddPaddingRow(0, kPaddingToMessage);
150 // Explanation string
151 views::Label* label = new views::Label(
152 l10n_util::GetStringUTF16(IDS_DESKTOP_CASTING_ACTIVE_MESSAGE));
153 label->SetFontList(ui::ResourceBundle::GetSharedInstance().GetFontList(
154 ui::ResourceBundle::MediumFont));
155 label->SetMultiLine(true);
156 label->SetHorizontalAlignment(gfx::ALIGN_LEFT);
157 label->SetAllowCharacterBreak(true);
158 grid_layout->StartRow(0, 0);
159 grid_layout->AddView(label);
161 SetLayoutManager(grid_layout);
162 Layout();
165 } // namespace
167 ////////////////////////////////////////////////////////////////////////////////
168 // Factory function.
170 void TrySwitchingActiveUser(
171 const base::Callback<void()> on_switch) {
172 // Some unit tests do not have a shell. In that case simply execute.
173 if (!ash::Shell::HasInstance()) {
174 on_switch.Run();
175 return;
177 // If neither screen sharing nor capturing is going on we can immediately
178 // switch users.
179 SystemTray* system_tray = ash::Shell::GetInstance()->GetPrimarySystemTray();
180 if (!system_tray->GetScreenShareItem()->is_started() &&
181 !system_tray->GetScreenCaptureItem()->is_started()) {
182 on_switch.Run();
183 return;
185 DesktopCastingWarningView::ShowDialog(on_switch);
188 bool TestAndTerminateDesktopCastingWarningForTest(bool accept) {
189 if (!instance_for_test)
190 return false;
191 if (accept)
192 instance_for_test->Accept();
193 delete instance_for_test->GetWidget()->GetNativeWindow();
194 CHECK(!instance_for_test);
195 return true;
198 } // namespace chromeos