Home | History | Annotate | Download | only in multi_user
      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 <vector>
      8 
      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"
     14 
     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
     19 
     20 namespace multi_user_util {
     21 
     22 std::string GetUserIDFromProfile(Profile* profile) {
     23   return GetUserIDFromEmail(profile->GetOriginalProfile()->GetProfileName());
     24 }
     25 
     26 std::string GetUserIDFromEmail(const std::string& email) {
     27   // |email| and profile name could be empty if not yet logged in or guest mode.
     28   return email.empty() ?
     29       email : gaia::CanonicalizeEmail(gaia::SanitizeEmail(email));
     30 }
     31 
     32 Profile* GetProfileFromUserID(const std::string& user_id) {
     33   // Unit tests can end up here without a |g_browser_process|.
     34   if (!g_browser_process || !g_browser_process->profile_manager())
     35     return NULL;
     36 
     37   std::vector<Profile*> profiles =
     38       g_browser_process->profile_manager()->GetLoadedProfiles();
     39 
     40   std::vector<Profile*>::const_iterator profile_iterator = profiles.begin();
     41   for (; profile_iterator != profiles.end(); ++profile_iterator) {
     42     if (GetUserIDFromProfile(*profile_iterator) == user_id)
     43       return *profile_iterator;
     44   }
     45   return NULL;
     46 }
     47 
     48 Profile* GetProfileFromWindow(aura::Window* window) {
     49 #if defined(OS_CHROMEOS)
     50   chrome::MultiUserWindowManager* manager =
     51       chrome::MultiUserWindowManager::GetInstance();
     52   // We might come here before the manager got created - or in a unit test.
     53   if (!manager)
     54     return NULL;
     55   const std::string user_id = manager->GetUserPresentingWindow(window);
     56   return user_id.empty() ? NULL :
     57                            multi_user_util::GetProfileFromUserID(user_id);
     58 #else
     59   return NULL;
     60 #endif
     61 }
     62 
     63 bool IsProfileFromActiveUser(Profile* profile) {
     64 #if defined(OS_CHROMEOS)
     65   return GetUserIDFromProfile(profile) ==
     66          user_manager::UserManager::Get()->GetActiveUser()->email();
     67 #else
     68   // In non Chrome OS configurations this will be always true since this only
     69   // makes sense in separate desktop mode.
     70   return true;
     71 #endif
     72 }
     73 
     74 const std::string& GetCurrentUserId() {
     75 #if defined(OS_CHROMEOS)
     76   return user_manager::UserManager::Get()->GetActiveUser()->email();
     77 #else
     78   return base::EmptyString();
     79 #endif
     80 }
     81 
     82 // Move the window to the current user's desktop.
     83 void MoveWindowToCurrentDesktop(aura::Window* window) {
     84 #if defined(OS_CHROMEOS)
     85   chrome::MultiUserWindowManager::GetInstance()->ShowWindowForUser(
     86       window,
     87       GetCurrentUserId());
     88 #endif
     89 }
     90 
     91 }  // namespace multi_user_util
     92