| OLD | NEW |
| (Empty) |
| 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. | |
| 4 | |
| 5 #include "ash/system/chromeos/multi_user/user_switch_util.h" | |
| 6 | |
| 7 #include "ash/common/system/chromeos/screen_security/screen_tray_item.h" | |
| 8 #include "ash/shell.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" | |
| 17 | |
| 18 namespace ash { | |
| 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 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 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(); | |
| 98 | |
| 99 on_switch_.Run(); | |
| 100 return true; | |
| 101 } | |
| 102 | |
| 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); | |
| 109 } | |
| 110 | |
| 111 bool DesktopCastingWarningView::IsDialogButtonEnabled( | |
| 112 ui::DialogButton button) const { | |
| 113 return button == ui::DIALOG_BUTTON_OK || button == ui::DIALOG_BUTTON_CANCEL; | |
| 114 } | |
| 115 | |
| 116 int DesktopCastingWarningView::GetDefaultDialogButton() const { | |
| 117 // The default should turn off the casting. | |
| 118 return ui::DIALOG_BUTTON_CANCEL; | |
| 119 } | |
| 120 | |
| 121 ui::ModalType DesktopCastingWarningView::GetModalType() const { | |
| 122 return ui::MODAL_TYPE_SYSTEM; | |
| 123 } | |
| 124 | |
| 125 gfx::Size DesktopCastingWarningView::GetPreferredSize() const { | |
| 126 return gfx::Size(kDefaultWidth, kDefaultHeight); | |
| 127 } | |
| 128 | |
| 129 void DesktopCastingWarningView::InitDialog() { | |
| 130 const gfx::Insets kDialogInsets(kTopInset, kInset, kInset, kInset); | |
| 131 | |
| 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); | |
| 135 | |
| 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); | |
| 139 | |
| 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); | |
| 149 | |
| 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); | |
| 160 | |
| 161 SetLayoutManager(grid_layout); | |
| 162 Layout(); | |
| 163 } | |
| 164 | |
| 165 } // namespace | |
| 166 | |
| 167 //////////////////////////////////////////////////////////////////////////////// | |
| 168 // Factory function. | |
| 169 | |
| 170 void TrySwitchingActiveUser(const base::Callback<void()> on_switch) { | |
| 171 // Some unit tests do not have a shell. In that case simply execute. | |
| 172 if (!ash::Shell::HasInstance()) { | |
| 173 on_switch.Run(); | |
| 174 return; | |
| 175 } | |
| 176 // If neither screen sharing nor capturing is going on we can immediately | |
| 177 // switch users. | |
| 178 SystemTray* system_tray = ash::Shell::GetInstance()->GetPrimarySystemTray(); | |
| 179 if (!system_tray->GetScreenShareItem()->is_started() && | |
| 180 !system_tray->GetScreenCaptureItem()->is_started()) { | |
| 181 on_switch.Run(); | |
| 182 return; | |
| 183 } | |
| 184 DesktopCastingWarningView::ShowDialog(on_switch); | |
| 185 } | |
| 186 | |
| 187 bool TestAndTerminateDesktopCastingWarningForTest(bool accept) { | |
| 188 if (!instance_for_test) | |
| 189 return false; | |
| 190 if (accept) | |
| 191 instance_for_test->Accept(); | |
| 192 delete instance_for_test->GetWidget()->GetNativeWindow(); | |
| 193 CHECK(!instance_for_test); | |
| 194 return true; | |
| 195 } | |
| 196 | |
| 197 } // namespace chromeos | |
| OLD | NEW |