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.
4 
5 #include "chrome/browser/ui/ash/multi_user/multi_user_util.h"
6 
7 #include "ash/public/cpp/multi_user_window_manager.h"
8 #include "chrome/browser/chromeos/profiles/profile_helper.h"
9 #include "chrome/browser/profiles/profile.h"
10 #include "chrome/browser/ui/ash/multi_user/multi_user_window_manager_helper.h"
11 #include "components/account_id/account_id.h"
12 #include "components/user_manager/user_manager.h"
13 #include "google_apis/gaia/gaia_auth_util.h"
14 
15 namespace multi_user_util {
16 
GetAccountIdFromProfile(const Profile * profile)17 AccountId GetAccountIdFromProfile(const Profile* profile) {
18   // This will guarantee an nonempty AccountId be returned if a valid profile is
19   // provided.
20   const user_manager::User* user =
21       chromeos::ProfileHelper::Get()->GetUserByProfile(
22           profile->GetOriginalProfile());
23   return user ? user->GetAccountId() : EmptyAccountId();
24 }
25 
GetAccountIdFromEmail(const std::string & email)26 AccountId GetAccountIdFromEmail(const std::string& email) {
27   // |email| and profile name could be empty if not yet logged in or guest mode.
28   return email.empty() ? EmptyAccountId()
29                        : AccountId::FromUserEmail(gaia::CanonicalizeEmail(
30                              gaia::SanitizeEmail(email)));
31 }
32 
GetProfileFromAccountId(const AccountId & account_id)33 Profile* GetProfileFromAccountId(const AccountId& account_id) {
34   const user_manager::User* user =
35       user_manager::UserManager::Get()->FindUser(account_id);
36   return user ? chromeos::ProfileHelper::Get()->GetProfileByUser(user)
37               : nullptr;
38 }
39 
GetProfileFromWindow(aura::Window * window)40 Profile* GetProfileFromWindow(aura::Window* window) {
41   MultiUserWindowManagerHelper* helper =
42       MultiUserWindowManagerHelper::GetInstance();
43   // We might come here before the helper got created - or in a unit test.
44   if (!helper)
45     return nullptr;
46   const AccountId account_id =
47       MultiUserWindowManagerHelper::GetWindowManager()->GetUserPresentingWindow(
48           window);
49   return account_id.is_valid() ? GetProfileFromAccountId(account_id) : nullptr;
50 }
51 
IsProfileFromActiveUser(Profile * profile)52 bool IsProfileFromActiveUser(Profile* profile) {
53   // There may be no active user in tests.
54   const user_manager::User* active_user =
55       user_manager::UserManager::Get()->GetActiveUser();
56   if (!active_user)
57     return true;
58   return GetAccountIdFromProfile(profile) == active_user->GetAccountId();
59 }
60 
GetCurrentAccountId()61 const AccountId GetCurrentAccountId() {
62   const user_manager::User* user =
63       user_manager::UserManager::Get()->GetActiveUser();
64   // In unit tests user login phase is usually skipped.
65   return user ? user->GetAccountId() : EmptyAccountId();
66 }
67 
68 // Move the window to the current user's desktop.
MoveWindowToCurrentDesktop(aura::Window * window)69 void MoveWindowToCurrentDesktop(aura::Window* window) {
70   if (!MultiUserWindowManagerHelper::GetInstance()->IsWindowOnDesktopOfUser(
71           window, GetCurrentAccountId())) {
72     MultiUserWindowManagerHelper::GetWindowManager()->ShowWindowForUser(
73         window, GetCurrentAccountId());
74   }
75 }
76 
77 }  // namespace multi_user_util
78