1 // Copyright 2013 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 "chrome/browser/ui/ash/multi_user/multi_user_context_menu.h"
7 #include "ash/multi_profile_uma.h"
8 #include "ash/session/session_state_delegate.h"
10 #include "base/bind.h"
11 #include "base/callback.h"
12 #include "base/prefs/pref_service.h"
13 #include "base/strings/utf_string_conversions.h"
14 #include "chrome/app/chrome_command_ids.h"
15 #include "chrome/browser/profiles/profile.h"
16 #include "chrome/browser/profiles/profile_manager.h"
17 #include "chrome/browser/ui/ash/multi_user/multi_user_util.h"
18 #include "chrome/browser/ui/ash/multi_user/multi_user_warning_dialog.h"
19 #include "chrome/browser/ui/ash/multi_user/multi_user_window_manager.h"
20 #include "chrome/common/pref_names.h"
21 #include "chrome/grit/generated_resources.h"
22 #include "components/user_manager/user.h"
23 #include "components/user_manager/user_manager.h"
24 #include "ui/aura/window.h"
25 #include "ui/base/l10n/l10n_util.h"
26 #include "ui/base/models/simple_menu_model.h"
32 class MultiUserContextMenuChromeos
: public ui::SimpleMenuModel
,
33 public ui::SimpleMenuModel::Delegate
{
35 explicit MultiUserContextMenuChromeos(aura::Window
* window
);
36 ~MultiUserContextMenuChromeos() override
{}
38 // SimpleMenuModel::Delegate:
39 bool IsCommandIdChecked(int command_id
) const override
{ return false; }
40 bool IsCommandIdEnabled(int command_id
) const override
{ return true; }
41 bool GetAcceleratorForCommandId(int command_id
,
42 ui::Accelerator
* accelerator
) override
{
45 void ExecuteCommand(int command_id
, int event_flags
) override
;
48 // The window for which this menu is.
49 aura::Window
* window_
;
51 DISALLOW_COPY_AND_ASSIGN(MultiUserContextMenuChromeos
);
54 MultiUserContextMenuChromeos::MultiUserContextMenuChromeos(aura::Window
* window
)
55 : ui::SimpleMenuModel(this),
59 void MultiUserContextMenuChromeos::ExecuteCommand(int command_id
,
61 ExecuteVisitDesktopCommand(command_id
, window_
);
64 } // namespace chromeos
66 scoped_ptr
<ui::MenuModel
> CreateMultiUserContextMenu(aura::Window
* window
) {
67 scoped_ptr
<ui::MenuModel
> model
;
68 ash::SessionStateDelegate
* delegate
=
69 ash::Shell::GetInstance()->session_state_delegate();
73 int logged_in_users
= delegate
->NumberOfLoggedInUsers();
74 if (logged_in_users
> 1) {
75 // If this window is not owned, we don't show the menu addition.
76 chrome::MultiUserWindowManager
* manager
=
77 chrome::MultiUserWindowManager::GetInstance();
78 const std::string user_id
= manager
->GetWindowOwner(window
);
79 if (user_id
.empty() || !window
)
81 chromeos::MultiUserContextMenuChromeos
* menu
=
82 new chromeos::MultiUserContextMenuChromeos(window
);
84 for (int user_index
= 1; user_index
< logged_in_users
; ++user_index
) {
85 const user_manager::UserInfo
* user_info
=
86 delegate
->GetUserInfo(user_index
);
87 menu
->AddItem(user_index
== 1 ? IDC_VISIT_DESKTOP_OF_LRU_USER_2
88 : IDC_VISIT_DESKTOP_OF_LRU_USER_3
,
89 l10n_util::GetStringFUTF16(
90 IDS_VISIT_DESKTOP_OF_LRU_USER
,
91 user_info
->GetDisplayName(),
92 base::ASCIIToUTF16(user_info
->GetEmail())));
98 void OnAcceptTeleportWarning(
99 const std::string user_id
, aura::Window
* window_
, bool no_show_again
) {
100 PrefService
* pref
= ProfileManager::GetActiveUserProfile()->GetPrefs();
101 pref
->SetBoolean(prefs::kMultiProfileWarningShowDismissed
, no_show_again
);
103 ash::MultiProfileUMA::RecordTeleportAction(
104 ash::MultiProfileUMA::TELEPORT_WINDOW_CAPTION_MENU
);
106 chrome::MultiUserWindowManager::GetInstance()->ShowWindowForUser(window_
,
110 void ExecuteVisitDesktopCommand(int command_id
, aura::Window
* window
) {
111 switch (command_id
) {
112 case IDC_VISIT_DESKTOP_OF_LRU_USER_2
:
113 case IDC_VISIT_DESKTOP_OF_LRU_USER_3
: {
114 // When running the multi user mode on Chrome OS, windows can "visit"
115 // another user's desktop.
116 const std::string
& user_id
=
117 ash::Shell::GetInstance()
118 ->session_state_delegate()
119 ->GetUserInfo(IDC_VISIT_DESKTOP_OF_LRU_USER_2
== command_id
? 1
122 base::Callback
<void(bool)> on_accept
=
123 base::Bind(&OnAcceptTeleportWarning
, user_id
, window
);
125 // Don't show warning dialog if any logged in user in multi-profiles
126 // session dismissed it.
127 const user_manager::UserList logged_in_users
=
128 user_manager::UserManager::Get()->GetLoggedInUsers();
129 for (user_manager::UserList::const_iterator it
= logged_in_users
.begin();
130 it
!= logged_in_users
.end();
132 if (multi_user_util::GetProfileFromUserID(
133 multi_user_util::GetUserIDFromEmail((*it
)->email()))->GetPrefs()->
134 GetBoolean(prefs::kMultiProfileWarningShowDismissed
)) {
135 bool active_user_show_option
=
136 ProfileManager::GetActiveUserProfile()->
137 GetPrefs()->GetBoolean(prefs::kMultiProfileWarningShowDismissed
);
138 on_accept
.Run(active_user_show_option
);
142 chromeos::ShowMultiprofilesWarningDialog(on_accept
);