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.h"
13 #include "chrome/browser/chromeos/login/user_manager.h"
14 #include "chrome/browser/profiles/profile.h"
15 #include "chrome/browser/profiles/profile_manager.h"
16 #include "chrome/common/chrome_constants.h"
17 #include "chrome/common/chrome_switches.h"
18 #include "chromeos/chromeos_switches.h"
24 base::FilePath GetSigninProfileDir() {
25 ProfileManager* profile_manager = g_browser_process->profile_manager();
26 base::FilePath user_data_dir = profile_manager->user_data_dir();
27 return user_data_dir.AppendASCII(chrome::kInitialProfile);
30 } // anonymous namespace
32 ////////////////////////////////////////////////////////////////////////////////
33 // ProfileHelper, public
35 ProfileHelper::ProfileHelper()
36 : signin_profile_clear_requested_(false) {
39 ProfileHelper::~ProfileHelper() {
40 // Checking whether UserManager is initialized covers case
41 // when ScopedTestUserManager is used.
42 if (UserManager::IsInitialized())
43 UserManager::Get()->RemoveSessionStateObserver(this);
47 Profile* ProfileHelper::GetProfileByUserIdHash(
48 const std::string& user_id_hash) {
49 ProfileManager* profile_manager = g_browser_process->profile_manager();
50 return profile_manager->GetProfile(GetProfilePathByUserIdHash(user_id_hash));
54 base::FilePath ProfileHelper::GetProfilePathByUserIdHash(
55 const std::string& user_id_hash) {
56 const CommandLine& command_line = *CommandLine::ForCurrentProcess();
57 // Fails for KioskTest.InstallAndLaunchApp test - crbug.com/238985
58 // Will probably fail for Guest session / restart after a crash -
60 // TODO(nkostylev): Remove this check once these bugs are fixed.
61 if (command_line.HasSwitch(::switches::kMultiProfiles))
62 DCHECK(!user_id_hash.empty());
63 ProfileManager* profile_manager = g_browser_process->profile_manager();
64 base::FilePath profile_path = profile_manager->user_data_dir();
65 return profile_path.Append(
66 base::FilePath(chrome::kProfileDirPrefix + user_id_hash));
70 base::FilePath ProfileHelper::GetProfileDirByLegacyLoginProfileSwitch() {
71 base::FilePath profile_dir;
72 std::string login_profile_value = CommandLine::ForCurrentProcess()->
73 GetSwitchValueASCII(chromeos::switches::kLoginProfile);
74 if (login_profile_value == chrome::kLegacyProfileDir ||
75 login_profile_value == chrome::kTestUserProfileDir) {
76 profile_dir = base::FilePath(login_profile_value);
78 profile_dir = ProfileHelper::GetUserProfileDir(login_profile_value);
84 Profile* ProfileHelper::GetSigninProfile() {
85 ProfileManager* profile_manager = g_browser_process->profile_manager();
86 return profile_manager->GetProfile(GetSigninProfileDir())->
87 GetOffTheRecordProfile();
91 std::string ProfileHelper::GetUserIdHashFromProfile(Profile* profile) {
95 // Check that profile directory starts with the correct prefix.
96 std::string profile_dir = profile->GetPath().BaseName().value();
97 std::string prefix(chrome::kProfileDirPrefix);
98 if (profile_dir.find(prefix) != 0) {
99 // This happens when creating a TestingProfile in browser tests.
100 return std::string();
103 return profile_dir.substr(prefix.length(),
104 profile_dir.length() - prefix.length());
108 base::FilePath ProfileHelper::GetUserProfileDir(
109 const std::string& user_id_hash) {
110 DCHECK(!user_id_hash.empty());
111 return base::FilePath(chrome::kProfileDirPrefix + user_id_hash);
115 bool ProfileHelper::IsSigninProfile(Profile* profile) {
116 return profile->GetPath().BaseName().value() == chrome::kInitialProfile;
120 bool ProfileHelper::IsOwnerProfile(Profile* profile) {
123 chromeos::UserManager* manager = chromeos::UserManager::Get();
124 chromeos::User* user = manager->GetUserByProfile(profile);
127 return user->email() == manager->GetOwnerEmail();
130 void ProfileHelper::ProfileStartup(Profile* profile, bool process_startup) {
131 // Initialize Chrome OS preferences like touch pad sensitivity. For the
132 // preferences to work in the guest mode, the initialization has to be
133 // done after |profile| is switched to the incognito profile (which
134 // is actually GuestSessionProfile in the guest mode). See the
135 // GetOffTheRecordProfile() call above.
136 profile->InitChromeOSPreferences();
138 // Add observer so we can see when the first profile's session restore is
139 // completed. After that, we won't need the default profile anymore.
140 if (!IsSigninProfile(profile) &&
141 UserManager::Get()->IsLoggedInAsRegularUser() &&
142 !UserManager::Get()->IsLoggedInAsStub()) {
143 chromeos::OAuth2LoginManager* login_manager =
144 chromeos::OAuth2LoginManagerFactory::GetInstance()->GetForProfile(
147 login_manager->AddObserver(this);
151 base::FilePath ProfileHelper::GetActiveUserProfileDir() {
152 return ProfileHelper::GetUserProfileDir(active_user_id_hash_);
155 void ProfileHelper::Initialize() {
156 UserManager::Get()->AddSessionStateObserver(this);
159 void ProfileHelper::ClearSigninProfile(const base::Closure& on_clear_callback) {
160 on_clear_callbacks_.push_back(on_clear_callback);
161 if (signin_profile_clear_requested_)
163 ProfileManager* profile_manager = g_browser_process->profile_manager();
164 // Check if signin profile was loaded.
165 if (!profile_manager->GetProfileByPath(GetSigninProfileDir())) {
166 OnBrowsingDataRemoverDone();
169 signin_profile_clear_requested_ = true;
170 BrowsingDataRemover* remover =
171 BrowsingDataRemover::CreateForUnboundedRange(GetSigninProfile());
172 remover->AddObserver(this);
173 remover->Remove(BrowsingDataRemover::REMOVE_SITE_DATA,
174 BrowsingDataHelper::ALL);
177 ////////////////////////////////////////////////////////////////////////////////
178 // ProfileHelper, BrowsingDataRemover::Observer implementation:
180 void ProfileHelper::OnBrowsingDataRemoverDone() {
181 signin_profile_clear_requested_ = false;
182 for (size_t i = 0; i < on_clear_callbacks_.size(); ++i) {
183 if (!on_clear_callbacks_[i].is_null())
184 on_clear_callbacks_[i].Run();
186 on_clear_callbacks_.clear();
189 ////////////////////////////////////////////////////////////////////////////////
190 // ProfileHelper, OAuth2LoginManager::Observer implementation:
192 void ProfileHelper::OnSessionRestoreStateChanged(
193 Profile* user_profile,
194 OAuth2LoginManager::SessionRestoreState state) {
195 if (state == OAuth2LoginManager::SESSION_RESTORE_DONE ||
196 state == OAuth2LoginManager::SESSION_RESTORE_FAILED ||
197 state == OAuth2LoginManager::SESSION_RESTORE_CONNECTION_FAILED) {
198 chromeos::OAuth2LoginManager* login_manager =
199 chromeos::OAuth2LoginManagerFactory::GetInstance()->
200 GetForProfile(user_profile);
201 login_manager->RemoveObserver(this);
202 ClearSigninProfile(base::Closure());
206 ////////////////////////////////////////////////////////////////////////////////
207 // ProfileHelper, UserManager::UserSessionStateObserver implementation:
209 void ProfileHelper::ActiveUserHashChanged(const std::string& hash) {
210 active_user_id_hash_ = hash;
211 base::FilePath profile_path = GetProfilePathByUserIdHash(hash);
212 VLOG(1) << "Switching to profile path: " << profile_path.value();
215 } // namespace chromeos