profile_helper.cc revision 58537e28ecd584eab876aee8be7156509866d23a
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#include "chrome/browser/chromeos/profiles/profile_helper.h"
6
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"
17
18namespace chromeos {
19
20namespace {
21
22base::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);
26}
27
28}  // anonymous namespace
29
30////////////////////////////////////////////////////////////////////////////////
31// ProfileHelper, public
32
33ProfileHelper::ProfileHelper()
34  : signin_profile_clear_requested_(false) {
35}
36
37ProfileHelper::~ProfileHelper() {
38  // Checking whether UserManager is initialized covers case
39  // when ScopedTestUserManager is used.
40  if (UserManager::IsInitialized())
41    UserManager::Get()->RemoveSessionStateObserver(this);
42}
43
44// static
45Profile* 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));
49}
50
51// static
52base::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 -
57  // crbug.com/238998
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));
65}
66
67// static
68Profile* ProfileHelper::GetSigninProfile() {
69  ProfileManager* profile_manager = g_browser_process->profile_manager();
70  return profile_manager->GetProfile(GetSigninProfileDir())->
71      GetOffTheRecordProfile();
72}
73
74// static
75std::string ProfileHelper::GetUserIdHashFromProfile(Profile* profile) {
76  if (!profile)
77    return std::string();
78
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) {
83    NOTREACHED();
84    return std::string();
85  }
86
87  return profile_dir.substr(prefix.length(),
88                            profile_dir.length() - prefix.length());
89}
90
91// static
92bool ProfileHelper::IsSigninProfile(Profile* profile) {
93  return profile->GetPath().BaseName().value() == chrome::kInitialProfile;
94}
95
96void 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();
103
104  if (process_startup)
105    profile->SetupChromeOSEnterpriseExtensionObserver();
106
107  // Add observer so we can see when the first profile's session restore is
108  // completed. After that, we won't need the default profile anymore.
109  if (!IsSigninProfile(profile) &&
110      UserManager::Get()->IsLoggedInAsRegularUser() &&
111      !UserManager::Get()->IsLoggedInAsStub()) {
112    chromeos::OAuth2LoginManager* login_manager =
113        chromeos::OAuth2LoginManagerFactory::GetInstance()->GetForProfile(
114            profile);
115    if (login_manager)
116      login_manager->AddObserver(this);
117  }
118}
119
120base::FilePath ProfileHelper::GetActiveUserProfileDir() {
121  DCHECK(!active_user_id_hash_.empty());
122  return base::FilePath(chrome::kProfileDirPrefix + active_user_id_hash_);
123}
124
125void ProfileHelper::Initialize() {
126  UserManager::Get()->AddSessionStateObserver(this);
127}
128
129void ProfileHelper::ClearSigninProfile(const base::Closure& on_clear_callback) {
130  on_clear_callbacks_.push_back(on_clear_callback);
131  if (signin_profile_clear_requested_)
132    return;
133  ProfileManager* profile_manager = g_browser_process->profile_manager();
134  // Check if signin profile was loaded.
135  if (!profile_manager->GetProfileByPath(GetSigninProfileDir())) {
136    OnBrowsingDataRemoverDone();
137    return;
138  }
139  signin_profile_clear_requested_ = true;
140  BrowsingDataRemover* remover =
141      BrowsingDataRemover::CreateForUnboundedRange(GetSigninProfile());
142  remover->AddObserver(this);
143  remover->Remove(BrowsingDataRemover::REMOVE_SITE_DATA,
144                  BrowsingDataHelper::ALL);
145}
146
147////////////////////////////////////////////////////////////////////////////////
148// ProfileHelper, BrowsingDataRemover::Observer implementation:
149
150void ProfileHelper::OnBrowsingDataRemoverDone() {
151  signin_profile_clear_requested_ = false;
152  for (size_t i = 0; i < on_clear_callbacks_.size(); ++i) {
153    if (!on_clear_callbacks_[i].is_null())
154      on_clear_callbacks_[i].Run();
155  }
156  on_clear_callbacks_.clear();
157}
158
159////////////////////////////////////////////////////////////////////////////////
160// ProfileHelper, OAuth2LoginManager::Observer implementation:
161
162void ProfileHelper::OnSessionRestoreStateChanged(
163    Profile* user_profile,
164    OAuth2LoginManager::SessionRestoreState state) {
165  if (state ==  OAuth2LoginManager::SESSION_RESTORE_DONE ||
166      state ==  OAuth2LoginManager::SESSION_RESTORE_FAILED) {
167    chromeos::OAuth2LoginManager* login_manager =
168        chromeos::OAuth2LoginManagerFactory::GetInstance()->GetForProfile(
169            user_profile);
170    login_manager->RemoveObserver(this);
171    ClearSigninProfile(base::Closure());
172  }
173}
174
175////////////////////////////////////////////////////////////////////////////////
176// ProfileHelper, UserManager::UserSessionStateObserver implementation:
177
178void ProfileHelper::ActiveUserHashChanged(const std::string& hash) {
179  active_user_id_hash_ = hash;
180  base::FilePath profile_path = GetProfilePathByUserIdHash(hash);
181  LOG(INFO) << "Switching to profile path: " << profile_path.value();
182}
183
184}  // namespace chromeos
185