Infobar material design refresh: layout
[chromium-blink-merge.git] / chrome / browser / ui / ash / multi_user / multi_user_util.cc
blobe3444a283a87777a34a52d193ab08d137f71e6b2
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"
7 #include <vector>
9 #include "base/strings/string_util.h"
10 #include "chrome/browser/browser_process.h"
11 #include "chrome/browser/profiles/profile.h"
12 #include "chrome/browser/profiles/profile_manager.h"
13 #include "google_apis/gaia/gaia_auth_util.h"
15 #if defined(OS_CHROMEOS)
16 #include "chrome/browser/ui/ash/multi_user/multi_user_window_manager.h"
17 #include "components/user_manager/user_manager.h"
18 #endif
20 namespace multi_user_util {
22 std::string GetUserIDFromProfile(Profile* profile) {
23 return GetUserIDFromEmail(
24 profile->GetOriginalProfile()->GetProfileUserName());
27 std::string GetUserIDFromEmail(const std::string& email) {
28 // |email| and profile name could be empty if not yet logged in or guest mode.
29 return email.empty() ?
30 email : gaia::CanonicalizeEmail(gaia::SanitizeEmail(email));
33 Profile* GetProfileFromUserID(const std::string& user_id) {
34 // Unit tests can end up here without a |g_browser_process|.
35 if (!g_browser_process || !g_browser_process->profile_manager())
36 return NULL;
38 std::vector<Profile*> profiles =
39 g_browser_process->profile_manager()->GetLoadedProfiles();
41 std::vector<Profile*>::const_iterator profile_iterator = profiles.begin();
42 for (; profile_iterator != profiles.end(); ++profile_iterator) {
43 if (GetUserIDFromProfile(*profile_iterator) == user_id)
44 return *profile_iterator;
46 return NULL;
49 Profile* GetProfileFromWindow(aura::Window* window) {
50 #if defined(OS_CHROMEOS)
51 chrome::MultiUserWindowManager* manager =
52 chrome::MultiUserWindowManager::GetInstance();
53 // We might come here before the manager got created - or in a unit test.
54 if (!manager)
55 return NULL;
56 const std::string user_id = manager->GetUserPresentingWindow(window);
57 return user_id.empty() ? NULL :
58 multi_user_util::GetProfileFromUserID(user_id);
59 #else
60 return NULL;
61 #endif
64 bool IsProfileFromActiveUser(Profile* profile) {
65 #if defined(OS_CHROMEOS)
66 return GetUserIDFromProfile(profile) ==
67 user_manager::UserManager::Get()->GetActiveUser()->email();
68 #else
69 // In non Chrome OS configurations this will be always true since this only
70 // makes sense in separate desktop mode.
71 return true;
72 #endif
75 const std::string& GetCurrentUserId() {
76 #if defined(OS_CHROMEOS)
77 return user_manager::UserManager::Get()->GetActiveUser()->email();
78 #else
79 return base::EmptyString();
80 #endif
83 // Move the window to the current user's desktop.
84 void MoveWindowToCurrentDesktop(aura::Window* window) {
85 #if defined(OS_CHROMEOS)
86 chrome::MultiUserWindowManager::GetInstance()->ShowWindowForUser(
87 window,
88 GetCurrentUserId());
89 #endif
92 } // namespace multi_user_util