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 "chrome/browser/chromeos/login/users/user_manager.h"
     17 
     18 class Profile;
     19 
     20 namespace base {
     21 class FilePath;
     22 }
     23 
     24 namespace chromeos {
     25 
     26 // This helper class is used on Chrome OS to keep track of currently
     27 // active user profile.
     28 // Whenever active user is changed (either add another user into session or
     29 // switch between users), ActiveUserHashChanged() will be called thus
     30 // internal state |active_user_id_hash_| will be updated.
     31 // Typical use cases for using this class:
     32 // 1. Get "signin profile" which is a special type of profile that is only used
     33 //    during signin flow: GetSigninProfile()
     34 // 2. Get profile dir of an active user, used by ProfileManager:
     35 //    GetActiveUserProfileDir()
     36 // 3. Get mapping from user_id_hash to Profile instance/profile path etc.
     37 class ProfileHelper : public BrowsingDataRemover::Observer,
     38                       public OAuth2LoginManager::Observer,
     39                       public UserManager::UserSessionStateObserver {
     40  public:
     41   ProfileHelper();
     42   virtual ~ProfileHelper();
     43 
     44   // Returns Profile instance that corresponds to |user_id_hash|.
     45   static Profile* GetProfileByUserIdHash(const std::string& user_id_hash);
     46 
     47   // Returns profile dir that corresponds to a --login-profile cmd line switch.
     48   static base::FilePath GetProfileDirByLegacyLoginProfileSwitch();
     49 
     50   // Returns profile path that corresponds to a given |user_id_hash|.
     51   static base::FilePath GetProfilePathByUserIdHash(
     52       const std::string& user_id_hash);
     53 
     54   // Returns the path that corresponds to the sign-in profile.
     55   static base::FilePath GetSigninProfileDir();
     56 
     57   // Returns OffTheRecord profile for use during signing phase.
     58   static Profile* GetSigninProfile();
     59 
     60   // Returns user_id hash for |profile| instance or empty string if hash
     61   // could not be extracted from |profile|.
     62   static std::string GetUserIdHashFromProfile(Profile* profile);
     63 
     64   // Returns user profile dir in a format [u-user_id_hash].
     65   static base::FilePath GetUserProfileDir(const std::string& user_id_hash);
     66 
     67   // Returns true if |profile| is the signin Profile. This can be used during
     68   // construction of the signin Profile to determine if that Profile is the
     69   // signin Profile.
     70   static bool IsSigninProfile(Profile* profile);
     71 
     72   // Returns true when |profile| corresponds to owner's profile.
     73   static bool IsOwnerProfile(Profile* profile);
     74 
     75   // Initialize a bunch of services that are tied to a browser profile.
     76   // TODO(dzhioev): Investigate whether or not this method is needed.
     77   void ProfileStartup(Profile* profile, bool process_startup);
     78 
     79   // Returns active user profile dir in a format [u-$hash].
     80   base::FilePath GetActiveUserProfileDir();
     81 
     82   // Should called once after UserManager instance has been created.
     83   void Initialize();
     84 
     85   // Returns hash for active user ID which is used to identify that user profile
     86   // on Chrome OS.
     87   std::string active_user_id_hash() { return active_user_id_hash_; }
     88 
     89   // Clears site data (cookies, history, etc) for signin profile.
     90   // Callback can be empty. Not thread-safe.
     91   void ClearSigninProfile(const base::Closure& on_clear_callback);
     92 
     93  private:
     94   friend class ProfileHelperTest;
     95   friend class ProfileListChromeOSTest;
     96 
     97   // BrowsingDataRemover::Observer implementation:
     98   virtual void OnBrowsingDataRemoverDone() OVERRIDE;
     99 
    100   // UserManager::Observer overrides.
    101   virtual void OnSessionRestoreStateChanged(
    102       Profile* user_profile,
    103       OAuth2LoginManager::SessionRestoreState state) OVERRIDE;
    104 
    105   // UserManager::UserSessionStateObserver implementation:
    106   virtual void ActiveUserHashChanged(const std::string& hash) OVERRIDE;
    107 
    108   // Identifies path to active user profile on Chrome OS.
    109   std::string active_user_id_hash_;
    110 
    111   // True if signin profile clearing now.
    112   bool signin_profile_clear_requested_;
    113 
    114   // List of callbacks called after signin profile clearance.
    115   std::vector<base::Closure> on_clear_callbacks_;
    116 
    117   DISALLOW_COPY_AND_ASSIGN(ProfileHelper);
    118 };
    119 
    120 } // namespace chromeos
    121 
    122 #endif  // CHROME_BROWSER_CHROMEOS_PROFILES_PROFILE_HELPER_H_
    123