Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / chromeos / profiles / profile_helper.h
index da9fccb..3dd899e 100644 (file)
 #include "base/files/file_path.h"
 #include "chrome/browser/browsing_data/browsing_data_remover.h"
 #include "chrome/browser/chromeos/login/signin/oauth2_login_manager.h"
-#include "chrome/browser/chromeos/login/users/user_manager.h"
+#include "components/user_manager/user_manager.h"
 
 class Profile;
+class User;
 
 namespace base {
 class FilePath;
 }
 
+namespace extensions {
+class ExtensionGarbageCollectorChromeOSUnitTest;
+}
+
 namespace chromeos {
 
 // This helper class is used on Chrome OS to keep track of currently
@@ -34,13 +39,19 @@ namespace chromeos {
 // 2. Get profile dir of an active user, used by ProfileManager:
 //    GetActiveUserProfileDir()
 // 3. Get mapping from user_id_hash to Profile instance/profile path etc.
-class ProfileHelper : public BrowsingDataRemover::Observer,
-                      public OAuth2LoginManager::Observer,
-                      public UserManager::UserSessionStateObserver {
+class ProfileHelper
+    : public BrowsingDataRemover::Observer,
+      public OAuth2LoginManager::Observer,
+      public user_manager::UserManager::UserSessionStateObserver {
  public:
   ProfileHelper();
   virtual ~ProfileHelper();
 
+  // Returns ProfileHelper instance. This class is not singleton and is owned
+  // by BrowserProcess/BrowserProcessPlatformPart. This method keeps that
+  // knowledge in one place.
+  static ProfileHelper* Get();
+
   // Returns Profile instance that corresponds to |user_id_hash|.
   static Profile* GetProfileByUserIdHash(const std::string& user_id_hash);
 
@@ -61,6 +72,9 @@ class ProfileHelper : public BrowsingDataRemover::Observer,
   // could not be extracted from |profile|.
   static std::string GetUserIdHashFromProfile(Profile* profile);
 
+  // Returns profile dir for the user identified by |user_id|.
+  static base::FilePath GetUserProfileDirByUserId(const std::string& user_id);
+
   // Returns user profile dir in a format [u-user_id_hash].
   static base::FilePath GetUserProfileDir(const std::string& user_id_hash);
 
@@ -72,6 +86,10 @@ class ProfileHelper : public BrowsingDataRemover::Observer,
   // Returns true when |profile| corresponds to owner's profile.
   static bool IsOwnerProfile(Profile* profile);
 
+  // Returns true when |profile| corresponds to the primary user profile
+  // of the current session.
+  static bool IsPrimaryProfile(Profile* profile);
+
   // Initialize a bunch of services that are tied to a browser profile.
   // TODO(dzhioev): Investigate whether or not this method is needed.
   void ProfileStartup(Profile* profile, bool process_startup);
@@ -90,21 +108,63 @@ class ProfileHelper : public BrowsingDataRemover::Observer,
   // Callback can be empty. Not thread-safe.
   void ClearSigninProfile(const base::Closure& on_clear_callback);
 
+  // Returns profile of the |user| if it is created and fully initialized.
+  // Otherwise, returns NULL.
+  Profile* GetProfileByUser(const user_manager::User* user);
+
+  // DEPRECATED
+  // Returns profile of the |user| if user's profile is created and fully
+  // initialized. Otherwise, if some user is active, returns his profile.
+  // Otherwise, returns signin profile.
+  // Behaviour of this function does not correspond to its name and can be
+  // very surprising, that's why it should not be used anymore.
+  // Use |GetProfileByUser| instead.
+  // TODO(dzhioev): remove this method. http://crbug.com/361528
+  Profile* GetProfileByUserUnsafe(const user_manager::User* user);
+
+  // Returns NULL if User is not created.
+  user_manager::User* GetUserByProfile(Profile* profile);
+
  private:
+  friend class DeviceSettingsTestBase;
+  friend class extensions::ExtensionGarbageCollectorChromeOSUnitTest;
+  friend class FakeUserManager;
+  friend class KioskTest;
+  friend class MockUserManager;
+  friend class MultiProfileUserControllerTest;
+  friend class ParallelAuthenticatorTest;
   friend class ProfileHelperTest;
   friend class ProfileListChromeOSTest;
+  friend class SessionStateDelegateChromeOSTest;
 
   // BrowsingDataRemover::Observer implementation:
   virtual void OnBrowsingDataRemoverDone() OVERRIDE;
 
-  // UserManager::Observer overrides.
+  // OAuth2LoginManager::Observer overrides.
   virtual void OnSessionRestoreStateChanged(
       Profile* user_profile,
       OAuth2LoginManager::SessionRestoreState state) OVERRIDE;
 
-  // UserManager::UserSessionStateObserver implementation:
+  // user_manager::UserManager::UserSessionStateObserver implementation:
   virtual void ActiveUserHashChanged(const std::string& hash) OVERRIDE;
 
+  // Associates |user| with profile with the same user_id,
+  // for GetUserByProfile() testing.
+  void SetProfileToUserMappingForTesting(user_manager::User* user);
+
+  // Enables/disables testing code path in GetUserByProfile() like
+  // always return primary user (when always_return_primary_user_for_testing is
+  // set).
+  static void SetProfileToUserForTestingEnabled(bool enabled);
+
+  // Enables/disables testing GetUserByProfile() by always returning
+  // primary user.
+  static void SetAlwaysReturnPrimaryUserForTesting(bool value);
+
+  // Associates |profile| with |user|, for GetProfileByUser() testing.
+  void SetUserToProfileMappingForTesting(const user_manager::User* user,
+                                         Profile* profile);
+
   // Identifies path to active user profile on Chrome OS.
   std::string active_user_id_hash_;
 
@@ -114,6 +174,22 @@ class ProfileHelper : public BrowsingDataRemover::Observer,
   // List of callbacks called after signin profile clearance.
   std::vector<base::Closure> on_clear_callbacks_;
 
+  // Used for testing by unit tests and FakeUserManager/MockUserManager.
+  std::map<const user_manager::User*, Profile*> user_to_profile_for_testing_;
+
+  // When this list is not empty GetUserByProfile() will find user that has
+  // the same user_id as |profile|->GetProfileName().
+  user_manager::UserList user_list_for_testing_;
+
+  // If true testing code path is used in GetUserByProfile() even if
+  // user_list_for_testing_ list is empty. In that case primary user will always
+  // be returned.
+  static bool enable_profile_to_user_testing;
+
+  // If true and enable_profile_to_user_testing is true then primary user will
+  // always be returned by GetUserByProfile().
+  static bool always_return_primary_user_for_testing;
+
   DISALLOW_COPY_AND_ASSIGN(ProfileHelper);
 };