Home | History | Annotate | Download | only in profiles
      1 // Copyright (c) 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 #ifndef CHROME_BROWSER_CHROMEOS_PROFILES_PROFILE_HELPER_H_
      6 #define CHROME_BROWSER_CHROMEOS_PROFILES_PROFILE_HELPER_H_
      7 
      8 #include <string>
      9 #include <vector>
     10 
     11 #include "base/basictypes.h"
     12 #include "base/callback_forward.h"
     13 #include "base/files/file_path.h"
     14 #include "chrome/browser/browsing_data/browsing_data_remover.h"
     15 #include "chrome/browser/chromeos/login/signin/oauth2_login_manager.h"
     16 #include "components/user_manager/user_manager.h"
     17 
     18 class Profile;
     19 class User;
     20 
     21 namespace base {
     22 class FilePath;
     23 }
     24 
     25 namespace extensions {
     26 class ExtensionGarbageCollectorChromeOSUnitTest;
     27 }
     28 
     29 namespace chromeos {
     30 
     31 // This helper class is used on Chrome OS to keep track of currently
     32 // active user profile.
     33 // Whenever active user is changed (either add another user into session or
     34 // switch between users), ActiveUserHashChanged() will be called thus
     35 // internal state |active_user_id_hash_| will be updated.
     36 // Typical use cases for using this class:
     37 // 1. Get "signin profile" which is a special type of profile that is only used
     38 //    during signin flow: GetSigninProfile()
     39 // 2. Get profile dir of an active user, used by ProfileManager:
     40 //    GetActiveUserProfileDir()
     41 // 3. Get mapping from user_id_hash to Profile instance/profile path etc.
     42 class ProfileHelper
     43     : public BrowsingDataRemover::Observer,
     44       public OAuth2LoginManager::Observer,
     45       public user_manager::UserManager::UserSessionStateObserver {
     46  public:
     47   ProfileHelper();
     48   virtual ~ProfileHelper();
     49 
     50   // Returns ProfileHelper instance. This class is not singleton and is owned
     51   // by BrowserProcess/BrowserProcessPlatformPart. This method keeps that
     52   // knowledge in one place.
     53   static ProfileHelper* Get();
     54 
     55   // Returns Profile instance that corresponds to |user_id_hash|.
     56   static Profile* GetProfileByUserIdHash(const std::string& user_id_hash);
     57 
     58   // Returns profile path that corresponds to a given |user_id_hash|.
     59   static base::FilePath GetProfilePathByUserIdHash(
     60       const std::string& user_id_hash);
     61 
     62   // Returns the path that corresponds to the sign-in profile.
     63   static base::FilePath GetSigninProfileDir();
     64 
     65   // Returns OffTheRecord profile for use during signing phase.
     66   static Profile* GetSigninProfile();
     67 
     68   // Returns user_id hash for |profile| instance or empty string if hash
     69   // could not be extracted from |profile|.
     70   static std::string GetUserIdHashFromProfile(Profile* profile);
     71 
     72   // Returns user profile dir in a format [u-user_id_hash].
     73   static base::FilePath GetUserProfileDir(const std::string& user_id_hash);
     74 
     75   // Returns true if |profile| is the signin Profile. This can be used during
     76   // construction of the signin Profile to determine if that Profile is the
     77   // signin Profile.
     78   static bool IsSigninProfile(Profile* profile);
     79 
     80   // Returns true when |profile| corresponds to owner's profile.
     81   static bool IsOwnerProfile(Profile* profile);
     82 
     83   // Returns true when |profile| corresponds to the primary user profile
     84   // of the current session.
     85   static bool IsPrimaryProfile(Profile* profile);
     86 
     87   // Initialize a bunch of services that are tied to a browser profile.
     88   // TODO(dzhioev): Investigate whether or not this method is needed.
     89   void ProfileStartup(Profile* profile, bool process_startup);
     90 
     91   // Returns active user profile dir in a format [u-$hash].
     92   base::FilePath GetActiveUserProfileDir();
     93 
     94   // Should called once after UserManager instance has been created.
     95   void Initialize();
     96 
     97   // Returns hash for active user ID which is used to identify that user profile
     98   // on Chrome OS.
     99   std::string active_user_id_hash() { return active_user_id_hash_; }
    100 
    101   // Clears site data (cookies, history, etc) for signin profile.
    102   // Callback can be empty. Not thread-safe.
    103   void ClearSigninProfile(const base::Closure& on_clear_callback);
    104 
    105   // Returns profile of the |user| if it is created and fully initialized.
    106   // Otherwise, returns NULL.
    107   Profile* GetProfileByUser(const user_manager::User* user);
    108 
    109   // DEPRECATED
    110   // Returns profile of the |user| if user's profile is created and fully
    111   // initialized. Otherwise, if some user is active, returns his profile.
    112   // Otherwise, returns signin profile.
    113   // Behaviour of this function does not correspond to its name and can be
    114   // very surprising, that's why it should not be used anymore.
    115   // Use |GetProfileByUser| instead.
    116   // TODO(dzhioev): remove this method. http://crbug.com/361528
    117   Profile* GetProfileByUserUnsafe(const user_manager::User* user);
    118 
    119   // Returns NULL if User is not created.
    120   user_manager::User* GetUserByProfile(Profile* profile);
    121 
    122   static std::string GetUserIdHashByUserIdForTesting(
    123       const std::string& user_id);
    124 
    125  private:
    126   friend class CryptohomeAuthenticatorTest;
    127   friend class DeviceSettingsTestBase;
    128   friend class extensions::ExtensionGarbageCollectorChromeOSUnitTest;
    129   friend class FakeUserManager;
    130   friend class KioskTest;
    131   friend class MockUserManager;
    132   friend class MultiProfileUserControllerTest;
    133   friend class ProfileHelperTest;
    134   friend class ProfileListChromeOSTest;
    135   friend class SessionStateDelegateChromeOSTest;
    136 
    137   // BrowsingDataRemover::Observer implementation:
    138   virtual void OnBrowsingDataRemoverDone() OVERRIDE;
    139 
    140   // OAuth2LoginManager::Observer overrides.
    141   virtual void OnSessionRestoreStateChanged(
    142       Profile* user_profile,
    143       OAuth2LoginManager::SessionRestoreState state) OVERRIDE;
    144 
    145   // user_manager::UserManager::UserSessionStateObserver implementation:
    146   virtual void ActiveUserHashChanged(const std::string& hash) OVERRIDE;
    147 
    148   // Associates |user| with profile with the same user_id,
    149   // for GetUserByProfile() testing.
    150   void SetProfileToUserMappingForTesting(user_manager::User* user);
    151 
    152   // Enables/disables testing code path in GetUserByProfile() like
    153   // always return primary user (when always_return_primary_user_for_testing is
    154   // set).
    155   static void SetProfileToUserForTestingEnabled(bool enabled);
    156 
    157   // Enables/disables testing GetUserByProfile() by always returning
    158   // primary user.
    159   static void SetAlwaysReturnPrimaryUserForTesting(bool value);
    160 
    161   // Associates |profile| with |user|, for GetProfileByUser() testing.
    162   void SetUserToProfileMappingForTesting(const user_manager::User* user,
    163                                          Profile* profile);
    164 
    165   // Identifies path to active user profile on Chrome OS.
    166   std::string active_user_id_hash_;
    167 
    168   // True if signin profile clearing now.
    169   bool signin_profile_clear_requested_;
    170 
    171   // List of callbacks called after signin profile clearance.
    172   std::vector<base::Closure> on_clear_callbacks_;
    173 
    174   // Used for testing by unit tests and FakeUserManager/MockUserManager.
    175   std::map<const user_manager::User*, Profile*> user_to_profile_for_testing_;
    176 
    177   // When this list is not empty GetUserByProfile() will find user that has
    178   // the same user_id as |profile|->GetProfileName().
    179   user_manager::UserList user_list_for_testing_;
    180 
    181   // If true testing code path is used in GetUserByProfile() even if
    182   // user_list_for_testing_ list is empty. In that case primary user will always
    183   // be returned.
    184   static bool enable_profile_to_user_testing;
    185 
    186   // If true and enable_profile_to_user_testing is true then primary user will
    187   // always be returned by GetUserByProfile().
    188   static bool always_return_primary_user_for_testing;
    189 
    190   DISALLOW_COPY_AND_ASSIGN(ProfileHelper);
    191 };
    192 
    193 } // namespace chromeos
    194 
    195 #endif  // CHROME_BROWSER_CHROMEOS_PROFILES_PROFILE_HELPER_H_
    196