| 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> | |
| 8 | |
| 9 #include "base/strings/string_util.h" | |
| 10 #include "build/build_config.h" | |
| 11 #include "chrome/browser/browser_process.h" | |
| 12 #include "chrome/browser/chromeos/profiles/profile_helper.h" | 7 #include "chrome/browser/chromeos/profiles/profile_helper.h" |
| 13 #include "chrome/browser/profiles/profile.h" | 8 #include "chrome/browser/profiles/profile.h" |
| 14 #include "chrome/browser/profiles/profile_manager.h" | |
| 15 #include "chrome/browser/ui/ash/multi_user/multi_user_window_manager.h" | 9 #include "chrome/browser/ui/ash/multi_user/multi_user_window_manager.h" |
| 16 #include "components/signin/core/account_id/account_id.h" | 10 #include "components/signin/core/account_id/account_id.h" |
| 17 #include "components/user_manager/user_manager.h" | 11 #include "components/user_manager/user_manager.h" |
| 18 #include "google_apis/gaia/gaia_auth_util.h" | |
| 19 | 12 |
| 20 namespace multi_user_util { | 13 namespace multi_user_util { |
| 21 | 14 |
| 22 AccountId GetAccountIdFromProfile(Profile* profile) { | 15 AccountId GetAccountIdFromProfile(Profile* profile) { |
| 23 return GetAccountIdFromEmail( | 16 // This will guarantee an nonempty AccountId be returned if a valid profile is |
| 24 profile->GetOriginalProfile()->GetProfileUserName()); | 17 // provided. |
| 18 const user_manager::User* user = |
| 19 chromeos::ProfileHelper::Get()->GetUserByProfile( |
| 20 profile->GetOriginalProfile()); |
| 21 return user ? user->GetAccountId() : EmptyAccountId(); |
| 25 } | 22 } |
| 26 | 23 |
| 27 AccountId GetAccountIdFromEmail(const std::string& email) { | 24 AccountId GetAccountIdFromEmail(const std::string& email) { |
| 28 // |email| and profile name could be empty if not yet logged in or guest mode. | 25 // |email| and profile name could be empty if not yet logged in or guest mode. |
| 29 return email.empty() ? EmptyAccountId() | 26 return email.empty() ? EmptyAccountId() |
| 30 : AccountId::FromUserEmail(gaia::CanonicalizeEmail( | 27 : AccountId::FromUserEmail(gaia::CanonicalizeEmail( |
| 31 gaia::SanitizeEmail(email))); | 28 gaia::SanitizeEmail(email))); |
| 32 } | 29 } |
| 33 | 30 |
| 34 Profile* GetProfileFromAccountId(const AccountId& account_id) { | 31 Profile* GetProfileFromAccountId(const AccountId& account_id) { |
| 35 // Unit tests can end up here without a |g_browser_process|. | |
| 36 if (!g_browser_process || !g_browser_process->profile_manager()) | |
| 37 return nullptr; | |
| 38 | |
| 39 std::vector<Profile*> profiles = | |
| 40 g_browser_process->profile_manager()->GetLoadedProfiles(); | |
| 41 | |
| 42 std::vector<Profile*>::const_iterator profile_iterator = profiles.begin(); | |
| 43 for (; profile_iterator != profiles.end(); ++profile_iterator) { | |
| 44 if (GetAccountIdFromProfile(*profile_iterator) == account_id) | |
| 45 return *profile_iterator; | |
| 46 } | |
| 47 | |
| 48 // If in a session the refresh token is revoked, GetAccountIdFromProfile() | |
| 49 // returns an empty account id which will cause the profile not being fetched | |
| 50 // properly. In this case we fall back to use GetProfileByUser() function. | |
| 51 // Note: If the refresh token is revoked because the user changes his GAIA | |
| 52 // password, we will force log out the user within 120 seconds. See crbug.com/ | |
| 53 // 587318 for more detail. | |
| 54 const user_manager::User* user = | 32 const user_manager::User* user = |
| 55 user_manager::UserManager::Get()->FindUser(account_id); | 33 user_manager::UserManager::Get()->FindUser(account_id); |
| 56 if (user) | 34 return user ? chromeos::ProfileHelper::Get()->GetProfileByUser(user) |
| 57 return chromeos::ProfileHelper::Get()->GetProfileByUser(user); | 35 : nullptr; |
| 58 | |
| 59 return nullptr; | |
| 60 } | 36 } |
| 61 | 37 |
| 62 Profile* GetProfileFromWindow(aura::Window* window) { | 38 Profile* GetProfileFromWindow(aura::Window* window) { |
| 63 chrome::MultiUserWindowManager* manager = | 39 chrome::MultiUserWindowManager* manager = |
| 64 chrome::MultiUserWindowManager::GetInstance(); | 40 chrome::MultiUserWindowManager::GetInstance(); |
| 65 // We might come here before the manager got created - or in a unit test. | 41 // We might come here before the manager got created - or in a unit test. |
| 66 if (!manager) | 42 if (!manager) |
| 67 return nullptr; | 43 return nullptr; |
| 68 const AccountId account_id = manager->GetUserPresentingWindow(window); | 44 const AccountId account_id = manager->GetUserPresentingWindow(window); |
| 69 return account_id.is_valid() ? GetProfileFromAccountId(account_id) : nullptr; | 45 return account_id.is_valid() ? GetProfileFromAccountId(account_id) : nullptr; |
| (...skipping 14 matching lines...) Expand all Loading... |
| 84 // Move the window to the current user's desktop. | 60 // Move the window to the current user's desktop. |
| 85 void MoveWindowToCurrentDesktop(aura::Window* window) { | 61 void MoveWindowToCurrentDesktop(aura::Window* window) { |
| 86 if (!chrome::MultiUserWindowManager::GetInstance()->IsWindowOnDesktopOfUser( | 62 if (!chrome::MultiUserWindowManager::GetInstance()->IsWindowOnDesktopOfUser( |
| 87 window, GetCurrentAccountId())) { | 63 window, GetCurrentAccountId())) { |
| 88 chrome::MultiUserWindowManager::GetInstance()->ShowWindowForUser( | 64 chrome::MultiUserWindowManager::GetInstance()->ShowWindowForUser( |
| 89 window, GetCurrentAccountId()); | 65 window, GetCurrentAccountId()); |
| 90 } | 66 } |
| 91 } | 67 } |
| 92 | 68 |
| 93 } // namespace multi_user_util | 69 } // namespace multi_user_util |
| OLD | NEW |