| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/ui/ash/multi_user/multi_user_util.h" | 5 #include "chrome/browser/ui/ash/multi_user/multi_user_util.h" |
| 6 | 6 |
| 7 #include <vector> | 7 #include <vector> |
| 8 | 8 |
| 9 #include "base/strings/string_util.h" | 9 #include "base/strings/string_util.h" |
| 10 #include "chrome/browser/browser_process.h" | 10 #include "chrome/browser/browser_process.h" |
| 11 #include "chrome/browser/profiles/profile.h" | 11 #include "chrome/browser/profiles/profile.h" |
| 12 #include "chrome/browser/profiles/profile_manager.h" | 12 #include "chrome/browser/profiles/profile_manager.h" |
| 13 #include "components/signin/core/account_id/account_id.h" |
| 13 #include "google_apis/gaia/gaia_auth_util.h" | 14 #include "google_apis/gaia/gaia_auth_util.h" |
| 14 | 15 |
| 15 #if defined(OS_CHROMEOS) | 16 #if defined(OS_CHROMEOS) |
| 16 #include "chrome/browser/ui/ash/multi_user/multi_user_window_manager.h" | 17 #include "chrome/browser/ui/ash/multi_user/multi_user_window_manager.h" |
| 17 #include "components/user_manager/user_manager.h" | 18 #include "components/user_manager/user_manager.h" |
| 18 #endif | 19 #endif |
| 19 | 20 |
| 20 namespace multi_user_util { | 21 namespace multi_user_util { |
| 21 | 22 |
| 22 std::string GetUserIDFromProfile(Profile* profile) { | 23 AccountId GetAccountIdFromProfile(Profile* profile) { |
| 23 return GetUserIDFromEmail( | 24 return GetAccountIdFromEmail( |
| 24 profile->GetOriginalProfile()->GetProfileUserName()); | 25 profile->GetOriginalProfile()->GetProfileUserName()); |
| 25 } | 26 } |
| 26 | 27 |
| 27 std::string GetUserIDFromEmail(const std::string& email) { | 28 AccountId GetAccountIdFromEmail(const std::string& email) { |
| 28 // |email| and profile name could be empty if not yet logged in or guest mode. | 29 // |email| and profile name could be empty if not yet logged in or guest mode. |
| 29 return email.empty() ? | 30 return email.empty() ? EmptyAccountId() |
| 30 email : gaia::CanonicalizeEmail(gaia::SanitizeEmail(email)); | 31 : AccountId::FromUserEmail(gaia::CanonicalizeEmail( |
| 32 gaia::SanitizeEmail(email))); |
| 31 } | 33 } |
| 32 | 34 |
| 33 Profile* GetProfileFromUserID(const std::string& user_id) { | 35 Profile* GetProfileFromAccountId(const AccountId& account_id) { |
| 34 // Unit tests can end up here without a |g_browser_process|. | 36 // Unit tests can end up here without a |g_browser_process|. |
| 35 if (!g_browser_process || !g_browser_process->profile_manager()) | 37 if (!g_browser_process || !g_browser_process->profile_manager()) |
| 36 return NULL; | 38 return NULL; |
| 37 | 39 |
| 38 std::vector<Profile*> profiles = | 40 std::vector<Profile*> profiles = |
| 39 g_browser_process->profile_manager()->GetLoadedProfiles(); | 41 g_browser_process->profile_manager()->GetLoadedProfiles(); |
| 40 | 42 |
| 41 std::vector<Profile*>::const_iterator profile_iterator = profiles.begin(); | 43 std::vector<Profile*>::const_iterator profile_iterator = profiles.begin(); |
| 42 for (; profile_iterator != profiles.end(); ++profile_iterator) { | 44 for (; profile_iterator != profiles.end(); ++profile_iterator) { |
| 43 if (GetUserIDFromProfile(*profile_iterator) == user_id) | 45 if (GetAccountIdFromProfile(*profile_iterator) == account_id) |
| 44 return *profile_iterator; | 46 return *profile_iterator; |
| 45 } | 47 } |
| 46 return NULL; | 48 return NULL; |
| 47 } | 49 } |
| 48 | 50 |
| 49 Profile* GetProfileFromWindow(aura::Window* window) { | 51 Profile* GetProfileFromWindow(aura::Window* window) { |
| 50 #if defined(OS_CHROMEOS) | 52 #if defined(OS_CHROMEOS) |
| 51 chrome::MultiUserWindowManager* manager = | 53 chrome::MultiUserWindowManager* manager = |
| 52 chrome::MultiUserWindowManager::GetInstance(); | 54 chrome::MultiUserWindowManager::GetInstance(); |
| 53 // We might come here before the manager got created - or in a unit test. | 55 // We might come here before the manager got created - or in a unit test. |
| 54 if (!manager) | 56 if (!manager) |
| 55 return NULL; | 57 return nullptr; |
| 56 const std::string user_id = manager->GetUserPresentingWindow(window); | 58 const AccountId account_id = manager->GetUserPresentingWindow(window); |
| 57 return user_id.empty() ? NULL : | 59 return account_id.is_valid() ? GetProfileFromAccountId(account_id) : nullptr; |
| 58 multi_user_util::GetProfileFromUserID(user_id); | |
| 59 #else | 60 #else |
| 60 return NULL; | 61 return nullptr; |
| 61 #endif | 62 #endif |
| 62 } | 63 } |
| 63 | 64 |
| 64 bool IsProfileFromActiveUser(Profile* profile) { | 65 bool IsProfileFromActiveUser(Profile* profile) { |
| 65 #if defined(OS_CHROMEOS) | 66 #if defined(OS_CHROMEOS) |
| 66 return GetUserIDFromProfile(profile) == | 67 return GetAccountIdFromProfile(profile) == |
| 67 user_manager::UserManager::Get()->GetActiveUser()->email(); | 68 user_manager::UserManager::Get()->GetActiveUser()->GetAccountId(); |
| 68 #else | 69 #else |
| 69 // In non Chrome OS configurations this will be always true since this only | 70 // In non Chrome OS configurations this will be always true since this only |
| 70 // makes sense in separate desktop mode. | 71 // makes sense in separate desktop mode. |
| 71 return true; | 72 return true; |
| 72 #endif | 73 #endif |
| 73 } | 74 } |
| 74 | 75 |
| 75 const std::string& GetCurrentUserId() { | 76 const AccountId GetCurrentAccountId() { |
| 76 #if defined(OS_CHROMEOS) | 77 #if defined(OS_CHROMEOS) |
| 77 return user_manager::UserManager::Get()->GetActiveUser()->email(); | 78 const user_manager::User* user = |
| 78 #else | 79 user_manager::UserManager::Get()->GetActiveUser(); |
| 79 return base::EmptyString(); | 80 // In unit tests user login phase is usually skipped. |
| 81 if (user) |
| 82 return user->GetAccountId(); |
| 80 #endif | 83 #endif |
| 84 return EmptyAccountId(); |
| 81 } | 85 } |
| 82 | 86 |
| 83 // Move the window to the current user's desktop. | 87 // Move the window to the current user's desktop. |
| 84 void MoveWindowToCurrentDesktop(aura::Window* window) { | 88 void MoveWindowToCurrentDesktop(aura::Window* window) { |
| 85 #if defined(OS_CHROMEOS) | 89 #if defined(OS_CHROMEOS) |
| 86 if (!chrome::MultiUserWindowManager::GetInstance()->IsWindowOnDesktopOfUser( | 90 if (!chrome::MultiUserWindowManager::GetInstance()->IsWindowOnDesktopOfUser( |
| 87 window, GetCurrentUserId())) { | 91 window, GetCurrentAccountId())) { |
| 88 chrome::MultiUserWindowManager::GetInstance()->ShowWindowForUser( | 92 chrome::MultiUserWindowManager::GetInstance()->ShowWindowForUser( |
| 89 window, GetCurrentUserId()); | 93 window, GetCurrentAccountId()); |
| 90 } | 94 } |
| 91 #endif | 95 #endif |
| 92 } | 96 } |
| 93 | 97 |
| 94 } // namespace multi_user_util | 98 } // namespace multi_user_util |
| OLD | NEW |