bartfab@chromium.org | 7b3f91ee | 2014-03-06 11:41:40 | [diff] [blame] | 1 | // Copyright 2014 The Chromium Authors. All rights reserved. |
bartfab@chromium.org | c80357d | 2013-08-14 11:54:42 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
James Cook | a35a1e2 | 2017-04-08 02:33:08 | [diff] [blame] | 5 | #include "ash/system/session/logout_button_tray.h" |
bartfab@chromium.org | c80357d | 2013-08-14 11:54:42 | [diff] [blame] | 6 | |
Mitsuru Oshima | 04b54d0 | 2017-10-09 14:22:45 | [diff] [blame] | 7 | #include <memory> |
| 8 | |
James Cook | dc04112 | 2017-08-01 14:03:22 | [diff] [blame] | 9 | #include "ash/public/cpp/ash_pref_names.h" |
tapted | e3880da | 2017-05-16 05:35:17 | [diff] [blame] | 10 | #include "ash/public/cpp/ash_typography.h" |
estade | 1b92771b | 2017-01-10 04:51:20 | [diff] [blame] | 11 | #include "ash/resources/vector_icons/vector_icons.h" |
Xiyuan Xia | e7b1954 | 2019-05-06 23:05:18 | [diff] [blame] | 12 | #include "ash/session/session_controller_impl.h" |
James Cook | 840177e | 2017-05-25 02:20:01 | [diff] [blame] | 13 | #include "ash/shelf/shelf.h" |
sky | 79fa3471 | 2017-03-20 23:46:47 | [diff] [blame] | 14 | #include "ash/shell.h" |
James Cook | a35a1e2 | 2017-04-08 02:33:08 | [diff] [blame] | 15 | #include "ash/system/session/logout_confirmation_controller.h" |
mohsen | e6704a0 | 2017-04-20 06:30:20 | [diff] [blame] | 16 | #include "ash/system/status_area_widget.h" |
James Cook | b0bf8e8 | 2017-04-09 17:01:44 | [diff] [blame] | 17 | #include "ash/system/tray/tray_constants.h" |
mohsen | e6704a0 | 2017-04-20 06:30:20 | [diff] [blame] | 18 | #include "ash/system/tray/tray_container.h" |
James Cook | b0bf8e8 | 2017-04-09 17:01:44 | [diff] [blame] | 19 | #include "ash/system/user/login_status.h" |
Sebastien Marchand | 6d0558fd | 2019-01-25 16:49:37 | [diff] [blame] | 20 | #include "base/bind.h" |
Wenzhao Zang | b06aeea | 2018-11-28 01:43:35 | [diff] [blame] | 21 | #include "base/metrics/user_metrics.h" |
James Cook | dc04112 | 2017-08-01 14:03:22 | [diff] [blame] | 22 | #include "components/prefs/pref_change_registrar.h" |
| 23 | #include "components/prefs/pref_registry_simple.h" |
| 24 | #include "components/prefs/pref_service.h" |
mohsen | e6704a0 | 2017-04-20 06:30:20 | [diff] [blame] | 25 | #include "ui/accessibility/ax_node_data.h" |
estade | ab41358 | 2016-08-03 16:37:07 | [diff] [blame] | 26 | #include "ui/gfx/color_palette.h" |
yukishiino@chromium.org | c3d71b64 | 2014-02-05 08:39:47 | [diff] [blame] | 27 | #include "ui/gfx/geometry/size.h" |
estade | 71ec8bb | 2016-08-09 00:00:47 | [diff] [blame] | 28 | #include "ui/gfx/paint_vector_icon.h" |
estade | ab41358 | 2016-08-03 16:37:07 | [diff] [blame] | 29 | #include "ui/views/controls/button/md_text_button.h" |
mohsen | e6704a0 | 2017-04-20 06:30:20 | [diff] [blame] | 30 | #include "ui/views/layout/fill_layout.h" |
bartfab@chromium.org | c80357d | 2013-08-14 11:54:42 | [diff] [blame] | 31 | |
| 32 | namespace ash { |
bartfab@chromium.org | c80357d | 2013-08-14 11:54:42 | [diff] [blame] | 33 | |
Yeol Park | ca31607 | 2019-05-22 01:47:02 | [diff] [blame^] | 34 | LogoutButtonTray::LogoutButtonTray(Shelf* shelf) : shelf_(shelf) { |
James Cook | dc04112 | 2017-08-01 14:03:22 | [diff] [blame] | 35 | DCHECK(shelf); |
Sam McNally | f4dab61 | 2017-08-16 03:06:33 | [diff] [blame] | 36 | Shell::Get()->session_controller()->AddObserver(this); |
Brett Wilson | 501d664 | 2017-12-14 17:58:18 | [diff] [blame] | 37 | SetLayoutManager(std::make_unique<views::FillLayout>()); |
Yeol Park | ca31607 | 2019-05-22 01:47:02 | [diff] [blame^] | 38 | container_ = AddChildView(std::make_unique<TrayContainer>(shelf)); |
yiyix | ee16fc4 | 2017-01-10 05:49:29 | [diff] [blame] | 39 | |
Yeol Park | ca31607 | 2019-05-22 01:47:02 | [diff] [blame^] | 40 | auto button = views::MdTextButton::Create(this, base::string16(), |
| 41 | CONTEXT_LAUNCHER_BUTTON); |
| 42 | button->SetProminent(true); |
| 43 | button->SetBgColorOverride(gfx::kGoogleRed700); |
mohsen | e6704a0 | 2017-04-20 06:30:20 | [diff] [blame] | 44 | |
Yeol Park | ca31607 | 2019-05-22 01:47:02 | [diff] [blame^] | 45 | button_ = container_->AddChildView(std::move(button)); |
mohsen | e6704a0 | 2017-04-20 06:30:20 | [diff] [blame] | 46 | SetVisible(false); |
bartfab@chromium.org | c80357d | 2013-08-14 11:54:42 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | LogoutButtonTray::~LogoutButtonTray() { |
Sam McNally | f4dab61 | 2017-08-16 03:06:33 | [diff] [blame] | 50 | Shell::Get()->session_controller()->RemoveObserver(this); |
James Cook | dc04112 | 2017-08-01 14:03:22 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | // static |
| 54 | void LogoutButtonTray::RegisterProfilePrefs(PrefRegistrySimple* registry) { |
Sam McNally | f4dab61 | 2017-08-16 03:06:33 | [diff] [blame] | 55 | registry->RegisterBooleanPref(prefs::kShowLogoutButtonInTray, false, |
| 56 | PrefRegistry::PUBLIC); |
| 57 | registry->RegisterIntegerPref(prefs::kLogoutDialogDurationMs, 20000, |
| 58 | PrefRegistry::PUBLIC); |
bartfab@chromium.org | c80357d | 2013-08-14 11:54:42 | [diff] [blame] | 59 | } |
| 60 | |
mohsen | e6704a0 | 2017-04-20 06:30:20 | [diff] [blame] | 61 | void LogoutButtonTray::UpdateAfterShelfAlignmentChange() { |
| 62 | // We must first update the button so that |container_| can lay it out |
| 63 | // correctly. |
| 64 | UpdateButtonTextAndImage(); |
| 65 | container_->UpdateAfterShelfAlignmentChange(); |
bartfab@chromium.org | c80357d | 2013-08-14 11:54:42 | [diff] [blame] | 66 | } |
| 67 | |
bartfab@chromium.org | c80357d | 2013-08-14 11:54:42 | [diff] [blame] | 68 | void LogoutButtonTray::ButtonPressed(views::Button* sender, |
| 69 | const ui::Event& event) { |
mohsen | e6704a0 | 2017-04-20 06:30:20 | [diff] [blame] | 70 | DCHECK_EQ(button_, sender); |
bruthig | a8048991 | 2016-10-27 23:20:48 | [diff] [blame] | 71 | |
bartfab@chromium.org | 7b3f91ee | 2014-03-06 11:41:40 | [diff] [blame] | 72 | if (dialog_duration_ <= base::TimeDelta()) { |
Wenzhao Zang | b06aeea | 2018-11-28 01:43:35 | [diff] [blame] | 73 | if (Shell::Get()->session_controller()->IsDemoSession()) |
| 74 | base::RecordAction(base::UserMetricsAction("DemoMode.ExitFromShelf")); |
bartfab@chromium.org | 7b3f91ee | 2014-03-06 11:41:40 | [diff] [blame] | 75 | // Sign out immediately if |dialog_duration_| is non-positive. |
Steven Bennetts | 40ab7f7 | 2017-10-04 23:32:27 | [diff] [blame] | 76 | Shell::Get()->session_controller()->RequestSignOut(); |
sky | 79fa3471 | 2017-03-20 23:46:47 | [diff] [blame] | 77 | } else if (Shell::Get()->logout_confirmation_controller()) { |
| 78 | Shell::Get()->logout_confirmation_controller()->ConfirmLogout( |
Wenzhao Zang | b06aeea | 2018-11-28 01:43:35 | [diff] [blame] | 79 | base::TimeTicks::Now() + dialog_duration_, |
| 80 | LogoutConfirmationController::Source::kShelfExitButton); |
bartfab@chromium.org | 7b3f91ee | 2014-03-06 11:41:40 | [diff] [blame] | 81 | } |
bartfab@chromium.org | c80357d | 2013-08-14 11:54:42 | [diff] [blame] | 82 | } |
| 83 | |
James Cook | dc04112 | 2017-08-01 14:03:22 | [diff] [blame] | 84 | void LogoutButtonTray::OnActiveUserPrefServiceChanged(PrefService* prefs) { |
| 85 | pref_change_registrar_.reset(); |
Mitsuru Oshima | 04b54d0 | 2017-10-09 14:22:45 | [diff] [blame] | 86 | pref_change_registrar_ = std::make_unique<PrefChangeRegistrar>(); |
James Cook | dc04112 | 2017-08-01 14:03:22 | [diff] [blame] | 87 | pref_change_registrar_->Init(prefs); |
| 88 | pref_change_registrar_->Add( |
| 89 | prefs::kShowLogoutButtonInTray, |
Qiang Xu | dadd3ad6 | 2018-03-07 17:22:45 | [diff] [blame] | 90 | base::BindRepeating(&LogoutButtonTray::UpdateShowLogoutButtonInTray, |
| 91 | base::Unretained(this))); |
James Cook | dc04112 | 2017-08-01 14:03:22 | [diff] [blame] | 92 | pref_change_registrar_->Add( |
| 93 | prefs::kLogoutDialogDurationMs, |
Qiang Xu | dadd3ad6 | 2018-03-07 17:22:45 | [diff] [blame] | 94 | base::BindRepeating(&LogoutButtonTray::UpdateLogoutDialogDuration, |
| 95 | base::Unretained(this))); |
James Cook | dc04112 | 2017-08-01 14:03:22 | [diff] [blame] | 96 | |
| 97 | // Read the initial values. |
| 98 | UpdateShowLogoutButtonInTray(); |
| 99 | UpdateLogoutDialogDuration(); |
| 100 | } |
| 101 | |
mohsen | e6704a0 | 2017-04-20 06:30:20 | [diff] [blame] | 102 | void LogoutButtonTray::GetAccessibleNodeData(ui::AXNodeData* node_data) { |
| 103 | View::GetAccessibleNodeData(node_data); |
| 104 | node_data->SetName(button_->GetText()); |
| 105 | } |
| 106 | |
Andrew Lee | 9b38f10 | 2019-05-20 23:21:29 | [diff] [blame] | 107 | const char* LogoutButtonTray::GetClassName() const { |
| 108 | return "LogoutButtonTray"; |
| 109 | } |
| 110 | |
James Cook | dc04112 | 2017-08-01 14:03:22 | [diff] [blame] | 111 | void LogoutButtonTray::UpdateShowLogoutButtonInTray() { |
| 112 | show_logout_button_in_tray_ = pref_change_registrar_->prefs()->GetBoolean( |
| 113 | prefs::kShowLogoutButtonInTray); |
bruthig | a8048991 | 2016-10-27 23:20:48 | [diff] [blame] | 114 | UpdateVisibility(); |
| 115 | } |
| 116 | |
James Cook | dc04112 | 2017-08-01 14:03:22 | [diff] [blame] | 117 | void LogoutButtonTray::UpdateLogoutDialogDuration() { |
| 118 | const int duration_ms = pref_change_registrar_->prefs()->GetInteger( |
| 119 | prefs::kLogoutDialogDurationMs); |
| 120 | dialog_duration_ = base::TimeDelta::FromMilliseconds(duration_ms); |
bruthig | a8048991 | 2016-10-27 23:20:48 | [diff] [blame] | 121 | } |
| 122 | |
mohsen | e6704a0 | 2017-04-20 06:30:20 | [diff] [blame] | 123 | void LogoutButtonTray::UpdateAfterLoginStatusChange() { |
| 124 | UpdateButtonTextAndImage(); |
bartfab@chromium.org | c80357d | 2013-08-14 11:54:42 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | void LogoutButtonTray::UpdateVisibility() { |
James Cook | 840177e | 2017-05-25 02:20:01 | [diff] [blame] | 128 | LoginStatus login_status = shelf_->GetStatusAreaWidget()->login_status(); |
bartfab@chromium.org | c80357d | 2013-08-14 11:54:42 | [diff] [blame] | 129 | SetVisible(show_logout_button_in_tray_ && |
mohsen | e6704a0 | 2017-04-20 06:30:20 | [diff] [blame] | 130 | login_status != LoginStatus::NOT_LOGGED_IN && |
| 131 | login_status != LoginStatus::LOCKED); |
bartfab@chromium.org | c80357d | 2013-08-14 11:54:42 | [diff] [blame] | 132 | } |
| 133 | |
mohsen | e6704a0 | 2017-04-20 06:30:20 | [diff] [blame] | 134 | void LogoutButtonTray::UpdateButtonTextAndImage() { |
James Cook | 840177e | 2017-05-25 02:20:01 | [diff] [blame] | 135 | LoginStatus login_status = shelf_->GetStatusAreaWidget()->login_status(); |
estade | 71ec8bb | 2016-08-09 00:00:47 | [diff] [blame] | 136 | const base::string16 title = |
| 137 | user::GetLocalizedSignOutStringForStatus(login_status, false); |
James Cook | 840177e | 2017-05-25 02:20:01 | [diff] [blame] | 138 | if (shelf_->IsHorizontalAlignment()) { |
estade | 71ec8bb | 2016-08-09 00:00:47 | [diff] [blame] | 139 | button_->SetText(title); |
mohsen | e6704a0 | 2017-04-20 06:30:20 | [diff] [blame] | 140 | button_->SetImage(views::Button::STATE_NORMAL, gfx::ImageSkia()); |
estade | 3cc64b83 | 2017-02-08 02:29:42 | [diff] [blame] | 141 | button_->SetMinSize(gfx::Size(0, kTrayItemSize)); |
estade | 71ec8bb | 2016-08-09 00:00:47 | [diff] [blame] | 142 | } else { |
| 143 | button_->SetText(base::string16()); |
| 144 | button_->SetAccessibleName(title); |
mohsen | e6704a0 | 2017-04-20 06:30:20 | [diff] [blame] | 145 | button_->SetImage(views::Button::STATE_NORMAL, |
estade | 1b92771b | 2017-01-10 04:51:20 | [diff] [blame] | 146 | gfx::CreateVectorIcon(kShelfLogoutIcon, kTrayIconColor)); |
estade | 3cc64b83 | 2017-02-08 02:29:42 | [diff] [blame] | 147 | button_->SetMinSize(gfx::Size(kTrayItemSize, kTrayItemSize)); |
estade | 71ec8bb | 2016-08-09 00:00:47 | [diff] [blame] | 148 | } |
| 149 | UpdateVisibility(); |
| 150 | } |
| 151 | |
bartfab@chromium.org | c80357d | 2013-08-14 11:54:42 | [diff] [blame] | 152 | } // namespace ash |