This source file includes following definitions.
- ShouldAddProfileDirPrefix
- GetProfileByUserIdHash
- GetProfilePathByUserIdHash
- GetProfileDirByLegacyLoginProfileSwitch
- GetSigninProfileDir
- GetSigninProfile
- GetUserIdHashFromProfile
- GetUserProfileDir
- IsSigninProfile
- IsOwnerProfile
- ProfileStartup
- GetActiveUserProfileDir
- Initialize
- ClearSigninProfile
- OnBrowsingDataRemoverDone
- OnSessionRestoreStateChanged
- ActiveUserHashChanged
#include "chrome/browser/chromeos/profiles/profile_helper.h"
#include "base/callback.h"
#include "base/command_line.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/browsing_data/browsing_data_helper.h"
#include "chrome/browser/chromeos/login/oauth2_login_manager_factory.h"
#include "chrome/browser/chromeos/login/user.h"
#include "chrome/browser/chromeos/login/user_manager.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "chrome/common/chrome_constants.h"
#include "chrome/common/chrome_switches.h"
#include "chromeos/chromeos_switches.h"
namespace chromeos {
namespace {
bool ShouldAddProfileDirPrefix(const std::string& user_id_hash) {
return user_id_hash != chrome::kLegacyProfileDir &&
user_id_hash != chrome::kTestUserProfileDir;
}
}
ProfileHelper::ProfileHelper()
: signin_profile_clear_requested_(false) {
}
ProfileHelper::~ProfileHelper() {
if (UserManager::IsInitialized())
UserManager::Get()->RemoveSessionStateObserver(this);
}
Profile* ProfileHelper::GetProfileByUserIdHash(
const std::string& user_id_hash) {
ProfileManager* profile_manager = g_browser_process->profile_manager();
return profile_manager->GetProfile(GetProfilePathByUserIdHash(user_id_hash));
}
base::FilePath ProfileHelper::GetProfilePathByUserIdHash(
const std::string& user_id_hash) {
ProfileManager* profile_manager = g_browser_process->profile_manager();
base::FilePath profile_path = profile_manager->user_data_dir();
return profile_path.Append(GetUserProfileDir(user_id_hash));
}
base::FilePath ProfileHelper::GetProfileDirByLegacyLoginProfileSwitch() {
const std::string login_profile_value =
CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
chromeos::switches::kLoginProfile);
return ProfileHelper::GetUserProfileDir(login_profile_value);
}
base::FilePath ProfileHelper::GetSigninProfileDir() {
ProfileManager* profile_manager = g_browser_process->profile_manager();
base::FilePath user_data_dir = profile_manager->user_data_dir();
return user_data_dir.AppendASCII(chrome::kInitialProfile);
}
Profile* ProfileHelper::GetSigninProfile() {
ProfileManager* profile_manager = g_browser_process->profile_manager();
return profile_manager->GetProfile(GetSigninProfileDir())->
GetOffTheRecordProfile();
}
std::string ProfileHelper::GetUserIdHashFromProfile(Profile* profile) {
if (!profile)
return std::string();
std::string profile_dir = profile->GetPath().BaseName().value();
if (!ShouldAddProfileDirPrefix(profile_dir))
return profile_dir;
std::string prefix(chrome::kProfileDirPrefix);
if (profile_dir.find(prefix) != 0) {
return std::string();
}
return profile_dir.substr(prefix.length(),
profile_dir.length() - prefix.length());
}
base::FilePath ProfileHelper::GetUserProfileDir(
const std::string& user_id_hash) {
DCHECK(!user_id_hash.empty());
return ShouldAddProfileDirPrefix(user_id_hash)
? base::FilePath(chrome::kProfileDirPrefix + user_id_hash)
: base::FilePath(user_id_hash);
}
bool ProfileHelper::IsSigninProfile(Profile* profile) {
return profile->GetPath().BaseName().value() == chrome::kInitialProfile;
}
bool ProfileHelper::IsOwnerProfile(Profile* profile) {
if (!profile)
return false;
chromeos::UserManager* manager = chromeos::UserManager::Get();
chromeos::User* user = manager->GetUserByProfile(profile);
if (!user)
return false;
return user->email() == manager->GetOwnerEmail();
}
void ProfileHelper::ProfileStartup(Profile* profile, bool process_startup) {
profile->InitChromeOSPreferences();
if (!IsSigninProfile(profile) &&
UserManager::Get()->IsLoggedInAsRegularUser() &&
!UserManager::Get()->IsLoggedInAsStub()) {
chromeos::OAuth2LoginManager* login_manager =
chromeos::OAuth2LoginManagerFactory::GetInstance()->GetForProfile(
profile);
if (login_manager)
login_manager->AddObserver(this);
}
}
base::FilePath ProfileHelper::GetActiveUserProfileDir() {
return ProfileHelper::GetUserProfileDir(active_user_id_hash_);
}
void ProfileHelper::Initialize() {
UserManager::Get()->AddSessionStateObserver(this);
}
void ProfileHelper::ClearSigninProfile(const base::Closure& on_clear_callback) {
on_clear_callbacks_.push_back(on_clear_callback);
if (signin_profile_clear_requested_)
return;
ProfileManager* profile_manager = g_browser_process->profile_manager();
if (!profile_manager->GetProfileByPath(GetSigninProfileDir())) {
OnBrowsingDataRemoverDone();
return;
}
signin_profile_clear_requested_ = true;
BrowsingDataRemover* remover =
BrowsingDataRemover::CreateForUnboundedRange(GetSigninProfile());
remover->AddObserver(this);
remover->Remove(BrowsingDataRemover::REMOVE_SITE_DATA,
BrowsingDataHelper::ALL);
}
void ProfileHelper::OnBrowsingDataRemoverDone() {
signin_profile_clear_requested_ = false;
for (size_t i = 0; i < on_clear_callbacks_.size(); ++i) {
if (!on_clear_callbacks_[i].is_null())
on_clear_callbacks_[i].Run();
}
on_clear_callbacks_.clear();
}
void ProfileHelper::OnSessionRestoreStateChanged(
Profile* user_profile,
OAuth2LoginManager::SessionRestoreState state) {
if (state == OAuth2LoginManager::SESSION_RESTORE_DONE ||
state == OAuth2LoginManager::SESSION_RESTORE_FAILED ||
state == OAuth2LoginManager::SESSION_RESTORE_CONNECTION_FAILED) {
chromeos::OAuth2LoginManager* login_manager =
chromeos::OAuth2LoginManagerFactory::GetInstance()->
GetForProfile(user_profile);
login_manager->RemoveObserver(this);
ClearSigninProfile(base::Closure());
}
}
void ProfileHelper::ActiveUserHashChanged(const std::string& hash) {
active_user_id_hash_ = hash;
base::FilePath profile_path = GetProfilePathByUserIdHash(hash);
VLOG(1) << "Switching to profile path: " << profile_path.value();
}
}