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_util.h"
9 #include "chrome/browser/browser_process.h"
10 #include "chrome/browser/profiles/profile.h"
11 #include "chrome/browser/profiles/profile_manager.h"
12 #include "google_apis/gaia/gaia_auth_util.h"
14 #if defined(OS_CHROMEOS)
15 #include "chrome/browser/chromeos/login/user_manager.h"
16 #include "chrome/browser/ui/ash/multi_user/multi_user_window_manager.h"
19 namespace multi_user_util
{
21 std::string
GetUserIDFromProfile(Profile
* profile
) {
22 return GetUserIDFromEmail(profile
->GetOriginalProfile()->GetProfileName());
25 std::string
GetUserIDFromEmail(const std::string
& email
) {
26 // |email| and profile name could be empty if not yet logged in or guest mode.
27 return email
.empty() ?
28 email
: gaia::CanonicalizeEmail(gaia::SanitizeEmail(email
));
31 Profile
* GetProfileFromUserID(const std::string
& user_id
) {
32 // Unit tests can end up here without a |g_browser_process|.
33 if (!g_browser_process
|| !g_browser_process
->profile_manager())
36 std::vector
<Profile
*> profiles
=
37 g_browser_process
->profile_manager()->GetLoadedProfiles();
39 std::vector
<Profile
*>::const_iterator profile_iterator
= profiles
.begin();
40 for (; profile_iterator
!= profiles
.end(); ++profile_iterator
) {
41 if (GetUserIDFromProfile(*profile_iterator
) == user_id
)
42 return *profile_iterator
;
47 Profile
* GetProfileFromWindow(aura::Window
* window
) {
48 #if defined(OS_CHROMEOS)
49 chrome::MultiUserWindowManager
* manager
=
50 chrome::MultiUserWindowManager::GetInstance();
51 // We might come here before the manager got created - or in a unit test.
54 const std::string user_id
= manager
->GetUserPresentingWindow(window
);
55 return user_id
.empty() ? NULL
:
56 multi_user_util::GetProfileFromUserID(user_id
);
62 bool IsProfileFromActiveUser(Profile
* profile
) {
63 #if defined(OS_CHROMEOS)
64 return GetUserIDFromProfile(profile
) ==
65 chromeos::UserManager::Get()->GetActiveUser()->email();
67 // In non Chrome OS configurations this will be always true since this only
68 // makes sense in separate desktop mode.
73 } // namespace multi_user_util