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.
5 #include "chrome/browser/chromeos/profiles/profile_helper.h"
7 #include "base/callback.h"
8 #include "base/command_line.h"
9 #include "chrome/browser/browser_process.h"
10 #include "chrome/browser/browsing_data/browsing_data_helper.h"
11 #include "chrome/browser/chromeos/login/oauth2_login_manager_factory.h"
12 #include "chrome/browser/chromeos/login/user_manager.h"
13 #include "chrome/browser/profiles/profile.h"
14 #include "chrome/browser/profiles/profile_manager.h"
15 #include "chrome/common/chrome_constants.h"
16 #include "chrome/common/chrome_switches.h"
22 base::FilePath GetSigninProfileDir() {
23 ProfileManager* profile_manager = g_browser_process->profile_manager();
24 base::FilePath user_data_dir = profile_manager->user_data_dir();
25 return user_data_dir.AppendASCII(chrome::kInitialProfile);
28 } // anonymous namespace
30 ////////////////////////////////////////////////////////////////////////////////
31 // ProfileHelper, public
33 ProfileHelper::ProfileHelper()
34 : signin_profile_clear_requested_(false) {
37 ProfileHelper::~ProfileHelper() {
38 // Checking whether UserManager is initialized covers case
39 // when ScopedTestUserManager is used.
40 if (UserManager::IsInitialized())
41 UserManager::Get()->RemoveSessionStateObserver(this);
45 Profile* ProfileHelper::GetProfileByUserIdHash(
46 const std::string& user_id_hash) {
47 ProfileManager* profile_manager = g_browser_process->profile_manager();
48 return profile_manager->GetProfile(GetProfilePathByUserIdHash(user_id_hash));
52 base::FilePath ProfileHelper::GetProfilePathByUserIdHash(
53 const std::string& user_id_hash) {
54 const CommandLine& command_line = *CommandLine::ForCurrentProcess();
55 // Fails for KioskTest.InstallAndLaunchApp test - crbug.com/238985
56 // Will probably fail for Guest session / restart after a crash -
58 // TODO(nkostylev): Remove this check once these bugs are fixed.
59 if (command_line.HasSwitch(switches::kMultiProfiles))
60 DCHECK(!user_id_hash.empty());
61 ProfileManager* profile_manager = g_browser_process->profile_manager();
62 base::FilePath profile_path = profile_manager->user_data_dir();
63 return profile_path.Append(
64 base::FilePath(chrome::kProfileDirPrefix + user_id_hash));
68 Profile* ProfileHelper::GetSigninProfile() {
69 ProfileManager* profile_manager = g_browser_process->profile_manager();
70 return profile_manager->GetProfile(GetSigninProfileDir())->
71 GetOffTheRecordProfile();
75 std::string ProfileHelper::GetUserIdHashFromProfile(Profile* profile) {
79 // Check that profile directory starts with the correct prefix.
80 std::string profile_dir = profile->GetPath().BaseName().value();
81 std::string prefix(chrome::kProfileDirPrefix);
82 if (profile_dir.find(prefix) != 0) {
87 return profile_dir.substr(prefix.length(),
88 profile_dir.length() - prefix.length());
92 bool ProfileHelper::IsSigninProfile(Profile* profile) {
93 return profile->GetPath().BaseName().value() == chrome::kInitialProfile;
96 void ProfileHelper::ProfileStartup(Profile* profile, bool process_startup) {
97 // Initialize Chrome OS preferences like touch pad sensitivity. For the
98 // preferences to work in the guest mode, the initialization has to be
99 // done after |profile| is switched to the incognito profile (which
100 // is actually GuestSessionProfile in the guest mode). See the
101 // GetOffTheRecordProfile() call above.
102 profile->InitChromeOSPreferences();
104 // Add observer so we can see when the first profile's session restore is
105 // completed. After that, we won't need the default profile anymore.
106 if (!IsSigninProfile(profile) &&
107 UserManager::Get()->IsLoggedInAsRegularUser() &&
108 !UserManager::Get()->IsLoggedInAsStub()) {
109 chromeos::OAuth2LoginManager* login_manager =
110 chromeos::OAuth2LoginManagerFactory::GetInstance()->GetForProfile(
113 login_manager->AddObserver(this);
117 base::FilePath ProfileHelper::GetActiveUserProfileDir() {
118 return GetUserProfileDir(active_user_id_hash_);
121 base::FilePath ProfileHelper::GetUserProfileDir(
122 const std::string& user_id_hash) {
123 DCHECK(!user_id_hash.empty());
124 return base::FilePath(chrome::kProfileDirPrefix + user_id_hash);
127 void ProfileHelper::Initialize() {
128 UserManager::Get()->AddSessionStateObserver(this);
131 void ProfileHelper::ClearSigninProfile(const base::Closure& on_clear_callback) {
132 on_clear_callbacks_.push_back(on_clear_callback);
133 if (signin_profile_clear_requested_)
135 ProfileManager* profile_manager = g_browser_process->profile_manager();
136 // Check if signin profile was loaded.
137 if (!profile_manager->GetProfileByPath(GetSigninProfileDir())) {
138 OnBrowsingDataRemoverDone();
141 signin_profile_clear_requested_ = true;
142 BrowsingDataRemover* remover =
143 BrowsingDataRemover::CreateForUnboundedRange(GetSigninProfile());
144 remover->AddObserver(this);
145 remover->Remove(BrowsingDataRemover::REMOVE_SITE_DATA,
146 BrowsingDataHelper::ALL);
149 ////////////////////////////////////////////////////////////////////////////////
150 // ProfileHelper, BrowsingDataRemover::Observer implementation:
152 void ProfileHelper::OnBrowsingDataRemoverDone() {
153 signin_profile_clear_requested_ = false;
154 for (size_t i = 0; i < on_clear_callbacks_.size(); ++i) {
155 if (!on_clear_callbacks_[i].is_null())
156 on_clear_callbacks_[i].Run();
158 on_clear_callbacks_.clear();
161 ////////////////////////////////////////////////////////////////////////////////
162 // ProfileHelper, OAuth2LoginManager::Observer implementation:
164 void ProfileHelper::OnSessionRestoreStateChanged(
165 Profile* user_profile,
166 OAuth2LoginManager::SessionRestoreState state) {
167 if (state == OAuth2LoginManager::SESSION_RESTORE_DONE ||
168 state == OAuth2LoginManager::SESSION_RESTORE_FAILED ||
169 state == OAuth2LoginManager::SESSION_RESTORE_CONNECTION_FAILED) {
170 chromeos::OAuth2LoginManager* login_manager =
171 chromeos::OAuth2LoginManagerFactory::GetInstance()->
172 GetForProfile(user_profile);
173 login_manager->RemoveObserver(this);
174 ClearSigninProfile(base::Closure());
178 ////////////////////////////////////////////////////////////////////////////////
179 // ProfileHelper, UserManager::UserSessionStateObserver implementation:
181 void ProfileHelper::ActiveUserHashChanged(const std::string& hash) {
182 active_user_id_hash_ = hash;
183 base::FilePath profile_path = GetProfilePathByUserIdHash(hash);
184 LOG(INFO) << "Switching to profile path: " << profile_path.value();
187 } // namespace chromeos