1c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// Copyright (c) 2013 The Chromium Authors. All rights reserved.
2c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
3c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// found in the LICENSE file.
4c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
5c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "chrome/browser/chromeos/profiles/profile_helper.h"
6c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
7868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "base/callback.h"
8c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "base/command_line.h"
9c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "chrome/browser/browser_process.h"
10b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)#include "chrome/browser/browsing_data/browsing_data_helper.h"
11cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "chrome/browser/chromeos/login/signin/oauth2_login_manager_factory.h"
12c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "chrome/browser/profiles/profile.h"
13c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "chrome/browser/profiles/profile_manager.h"
14cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "chrome/browser/profiles/profiles_state.h"
15c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "chrome/common/chrome_constants.h"
16c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "chrome/common/chrome_switches.h"
17a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)#include "chromeos/chromeos_switches.h"
185f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)#include "components/user_manager/user.h"
196e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)#include "components/user_manager/user_manager.h"
20116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "content/public/browser/browser_thread.h"
21c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
22c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)namespace chromeos {
23c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
24eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochnamespace {
25eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
261320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci// As defined in /chromeos/dbus/cryptohome_client.cc.
271320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tuccistatic const char kUserIdHashSuffix[] = "-hash";
281320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
2923730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)bool ShouldAddProfileDirPrefix(const std::string& user_id_hash) {
3023730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)  // Do not add profile dir prefix for legacy profile dir and test
3123730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)  // user profile. The reason of not adding prefix for test user profile
3223730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)  // is to keep the promise that TestingProfile::kTestUserProfileDir and
3323730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)  // chrome::kTestUserProfileDir are always in sync. Otherwise,
3423730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)  // TestingProfile::kTestUserProfileDir needs to be dynamically calculated
3523730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)  // based on whether multi profile is enabled or not.
3623730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)  return user_id_hash != chrome::kLegacyProfileDir &&
3723730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)      user_id_hash != chrome::kTestUserProfileDir;
3823730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)}
3923730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)
40116680a4aac90f2aa7413d9095a592090648e557Ben Murdochclass UsernameHashMatcher {
41116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch public:
42116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  explicit UsernameHashMatcher(const std::string& h) : username_hash(h) {}
435f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  bool operator()(const user_manager::User* user) const {
44116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    return user->username_hash() == username_hash;
45116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  }
46116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
47116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch private:
48116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  const std::string& username_hash;
49116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch};
50116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
51eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch}  // anonymous namespace
52eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
53116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// static
54116680a4aac90f2aa7413d9095a592090648e557Ben Murdochbool ProfileHelper::enable_profile_to_user_testing = false;
55116680a4aac90f2aa7413d9095a592090648e557Ben Murdochbool ProfileHelper::always_return_primary_user_for_testing = false;
56116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
57c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)////////////////////////////////////////////////////////////////////////////////
58c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// ProfileHelper, public
59c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
60b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)ProfileHelper::ProfileHelper()
61116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    : signin_profile_clear_requested_(false) {
62c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
63c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
64c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)ProfileHelper::~ProfileHelper() {
65868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // Checking whether UserManager is initialized covers case
66868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // when ScopedTestUserManager is used.
676e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  if (user_manager::UserManager::IsInitialized())
686e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    user_manager::UserManager::Get()->RemoveSessionStateObserver(this);
69c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
70c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
71c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// static
72116680a4aac90f2aa7413d9095a592090648e557Ben MurdochProfileHelper* ProfileHelper::Get() {
73116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  return g_browser_process->platform_part()->profile_helper();
74116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch}
75116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
76116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// static
77c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)Profile* ProfileHelper::GetProfileByUserIdHash(
78c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    const std::string& user_id_hash) {
79c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  ProfileManager* profile_manager = g_browser_process->profile_manager();
80b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  return profile_manager->GetProfile(GetProfilePathByUserIdHash(user_id_hash));
81c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
82c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
83c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// static
8490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)base::FilePath ProfileHelper::GetProfilePathByUserIdHash(
8590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    const std::string& user_id_hash) {
86cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // Fails for KioskTest.InstallAndLaunchApp test - crbug.com/238985
87cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // Will probably fail for Guest session / restart after a crash -
88cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // crbug.com/238998
89cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // TODO(nkostylev): Remove this check once these bugs are fixed.
90cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  DCHECK(!user_id_hash.empty());
9190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  ProfileManager* profile_manager = g_browser_process->profile_manager();
9290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  base::FilePath profile_path = profile_manager->user_data_dir();
9323730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)
9423730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)  return profile_path.Append(GetUserProfileDir(user_id_hash));
9590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}
9690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
9790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// static
98c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdochbase::FilePath ProfileHelper::GetSigninProfileDir() {
99c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  ProfileManager* profile_manager = g_browser_process->profile_manager();
100c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  base::FilePath user_data_dir = profile_manager->user_data_dir();
101c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  return user_data_dir.AppendASCII(chrome::kInitialProfile);
102c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch}
103c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch
104c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch// static
105c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)Profile* ProfileHelper::GetSigninProfile() {
106c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  ProfileManager* profile_manager = g_browser_process->profile_manager();
107eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  return profile_manager->GetProfile(GetSigninProfileDir())->
108c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      GetOffTheRecordProfile();
109c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
110c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
111c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// static
112b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)std::string ProfileHelper::GetUserIdHashFromProfile(Profile* profile) {
113b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  if (!profile)
114b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)    return std::string();
115b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)
116b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  std::string profile_dir = profile->GetPath().BaseName().value();
11723730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)
11823730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)  // Don't strip prefix if the dir is not supposed to be prefixed.
11923730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)  if (!ShouldAddProfileDirPrefix(profile_dir))
12023730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)    return profile_dir;
12123730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)
12223730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)  // Check that profile directory starts with the correct prefix.
12390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  std::string prefix(chrome::kProfileDirPrefix);
124b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  if (profile_dir.find(prefix) != 0) {
1255d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    // This happens when creating a TestingProfile in browser tests.
126b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)    return std::string();
127b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  }
128b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)
129b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  return profile_dir.substr(prefix.length(),
130b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)                            profile_dir.length() - prefix.length());
131b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)}
132b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)
133b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)// static
134a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)base::FilePath ProfileHelper::GetUserProfileDir(
135a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    const std::string& user_id_hash) {
1361320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  CHECK(!user_id_hash.empty());
13723730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)  return ShouldAddProfileDirPrefix(user_id_hash)
13823730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)             ? base::FilePath(chrome::kProfileDirPrefix + user_id_hash)
13923730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)             : base::FilePath(user_id_hash);
140a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)}
141a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
142a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)// static
143c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)bool ProfileHelper::IsSigninProfile(Profile* profile) {
144c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  return profile->GetPath().BaseName().value() == chrome::kInitialProfile;
145c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
146c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1475d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// static
1485d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)bool ProfileHelper::IsOwnerProfile(Profile* profile) {
1495d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (!profile)
1505d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    return false;
1515f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  user_manager::User* user = ProfileHelper::Get()->GetUserByProfile(profile);
1525d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (!user)
1535d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    return false;
154116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
1556e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  return user->email() == user_manager::UserManager::Get()->GetOwnerEmail();
1565d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
1575d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1586e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)// static
1595f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)bool ProfileHelper::IsPrimaryProfile(Profile* profile) {
1605f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  if (!profile)
1615f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    return false;
1625f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  user_manager::User* user = ProfileHelper::Get()->GetUserByProfile(profile);
1635f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  if (!user)
1645f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    return false;
1656e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  return user == user_manager::UserManager::Get()->GetPrimaryUser();
1665f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)}
1675f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
168c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)void ProfileHelper::ProfileStartup(Profile* profile, bool process_startup) {
169c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // Initialize Chrome OS preferences like touch pad sensitivity. For the
170c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // preferences to work in the guest mode, the initialization has to be
171c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // done after |profile| is switched to the incognito profile (which
172c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // is actually GuestSessionProfile in the guest mode). See the
173c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // GetOffTheRecordProfile() call above.
174c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  profile->InitChromeOSPreferences();
175c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
17658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // Add observer so we can see when the first profile's session restore is
17758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // completed. After that, we won't need the default profile anymore.
17858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  if (!IsSigninProfile(profile) &&
1796e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)      user_manager::UserManager::Get()->IsLoggedInAsRegularUser() &&
1806e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)      !user_manager::UserManager::Get()->IsLoggedInAsStub()) {
18158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    chromeos::OAuth2LoginManager* login_manager =
18258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)        chromeos::OAuth2LoginManagerFactory::GetInstance()->GetForProfile(
18358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)            profile);
18458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    if (login_manager)
18558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      login_manager->AddObserver(this);
186c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
187c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
188c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
189b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)base::FilePath ProfileHelper::GetActiveUserProfileDir() {
190a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  return ProfileHelper::GetUserProfileDir(active_user_id_hash_);
191b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)}
192b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)
193c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)void ProfileHelper::Initialize() {
1946e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  user_manager::UserManager::Get()->AddSessionStateObserver(this);
195c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
196c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
197b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)void ProfileHelper::ClearSigninProfile(const base::Closure& on_clear_callback) {
198b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  on_clear_callbacks_.push_back(on_clear_callback);
199b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  if (signin_profile_clear_requested_)
200b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)    return;
201eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  ProfileManager* profile_manager = g_browser_process->profile_manager();
202eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  // Check if signin profile was loaded.
203eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  if (!profile_manager->GetProfileByPath(GetSigninProfileDir())) {
204eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    OnBrowsingDataRemoverDone();
205eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    return;
206eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  }
207b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  signin_profile_clear_requested_ = true;
208b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  BrowsingDataRemover* remover =
209b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)      BrowsingDataRemover::CreateForUnboundedRange(GetSigninProfile());
210b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  remover->AddObserver(this);
211b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  remover->Remove(BrowsingDataRemover::REMOVE_SITE_DATA,
212b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)                  BrowsingDataHelper::ALL);
213b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)}
214b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)
2155f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)Profile* ProfileHelper::GetProfileByUser(const user_manager::User* user) {
216116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // This map is non-empty only in tests.
217116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  if (!user_to_profile_for_testing_.empty()) {
2185f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    std::map<const user_manager::User*, Profile*>::const_iterator it =
219116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch        user_to_profile_for_testing_.find(user);
220116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    return it == user_to_profile_for_testing_.end() ? NULL : it->second;
221116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  }
222116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
2236e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  if (!user->is_profile_created())
2246e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    return NULL;
2256e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  Profile* profile =
2266e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)      ProfileHelper::GetProfileByUserIdHash(user->username_hash());
2276e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
2286e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  // GetActiveUserProfile() or GetProfileByUserIdHash() returns a new instance
2296e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  // of ProfileImpl(), but actually its OffTheRecordProfile() should be used.
2306e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  if (user_manager::UserManager::Get()->IsLoggedInAsGuest())
2316e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    profile = profile->GetOffTheRecordProfile();
2326e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
2336e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  return profile;
2346e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)}
2356e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
2366e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)Profile* ProfileHelper::GetProfileByUserUnsafe(const user_manager::User* user) {
2376e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  // This map is non-empty only in tests.
2386e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  if (!user_to_profile_for_testing_.empty()) {
2396e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    std::map<const user_manager::User*, Profile*>::const_iterator it =
2406e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)        user_to_profile_for_testing_.find(user);
2416e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    return it == user_to_profile_for_testing_.end() ? NULL : it->second;
2426e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  }
2436e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
244116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  Profile* profile = NULL;
2456e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  if (user->is_profile_created()) {
246116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    profile = ProfileHelper::GetProfileByUserIdHash(user->username_hash());
2476e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  } else {
2486e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    LOG(WARNING) << "ProfileHelper::GetProfileByUserUnsafe is called when "
2496e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)                    "|user|'s profile is not created. It probably means that "
2506e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)                    "something is wrong with a calling code. Please report in "
2511320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                    "http://crbug.com/361528 if you see this message. user_id: "
2521320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                 << user->email();
253116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    profile = ProfileManager::GetActiveUserProfile();
2546e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  }
255116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
256116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // GetActiveUserProfile() or GetProfileByUserIdHash() returns a new instance
257116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // of ProfileImpl(), but actually its OffTheRecordProfile() should be used.
2586e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  if (profile && user_manager::UserManager::Get()->IsLoggedInAsGuest())
259116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    profile = profile->GetOffTheRecordProfile();
260116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  return profile;
261116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch}
262116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
2635f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)user_manager::User* ProfileHelper::GetUserByProfile(Profile* profile) {
264116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // This map is non-empty only in tests.
265116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  if (enable_profile_to_user_testing || !user_list_for_testing_.empty()) {
266116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    if (always_return_primary_user_for_testing)
2675f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)      return const_cast<user_manager::User*>(
2686e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)          user_manager::UserManager::Get()->GetPrimaryUser());
269116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
270116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    const std::string& user_name = profile->GetProfileName();
2715f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    for (user_manager::UserList::const_iterator it =
2725f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)             user_list_for_testing_.begin();
273116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch         it != user_list_for_testing_.end();
274116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch         ++it) {
275116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      if ((*it)->email() == user_name)
276116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch        return *it;
277116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    }
278116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
279116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    // In case of test setup we should always default to primary user.
2805f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    return const_cast<user_manager::User*>(
2816e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)        user_manager::UserManager::Get()->GetPrimaryUser());
282116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  }
283116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
2841320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  DCHECK(!content::BrowserThread::IsThreadInitialized(
2851320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci             content::BrowserThread::UI) ||
2861320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci         content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
287116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  if (ProfileHelper::IsSigninProfile(profile))
288116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    return NULL;
289116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
2906e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  user_manager::UserManager* user_manager = user_manager::UserManager::Get();
291116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
292116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // Special case for non-CrOS tests that do create several profiles
293116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // and don't really care about mapping to the real user.
294116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // Without multi-profiles on Chrome OS such tests always got active_user_.
295116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // Now these tests will specify special flag to continue working.
296116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // In future those tests can get a proper CrOS configuration i.e. register
297116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // and login several users if they want to work with an additional profile.
298116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  if (CommandLine::ForCurrentProcess()->HasSwitch(
299116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch          switches::kIgnoreUserProfileMappingForTests)) {
300116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    return user_manager->GetActiveUser();
301116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  }
302116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
303116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  const std::string username_hash =
304116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      ProfileHelper::GetUserIdHashFromProfile(profile);
3055f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  const user_manager::UserList& users = user_manager->GetUsers();
3065f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  const user_manager::UserList::const_iterator pos = std::find_if(
307116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      users.begin(), users.end(), UsernameHashMatcher(username_hash));
308116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  if (pos != users.end())
309116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    return *pos;
310116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
311116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // Many tests do not have their users registered with UserManager and
312116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // runs here. If |active_user_| matches |profile|, returns it.
3135f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  const user_manager::User* active_user = user_manager->GetActiveUser();
314116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  return active_user &&
315116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch                 ProfileHelper::GetProfilePathByUserIdHash(
316116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch                     active_user->username_hash()) == profile->GetPath()
3175f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)             ? const_cast<user_manager::User*>(active_user)
318116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch             : NULL;
319116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch}
320116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
321b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)////////////////////////////////////////////////////////////////////////////////
322b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)// ProfileHelper, BrowsingDataRemover::Observer implementation:
323b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)
324b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)void ProfileHelper::OnBrowsingDataRemoverDone() {
325b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  signin_profile_clear_requested_ = false;
326b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  for (size_t i = 0; i < on_clear_callbacks_.size(); ++i) {
327b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)    if (!on_clear_callbacks_[i].is_null())
328b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)      on_clear_callbacks_[i].Run();
329b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  }
330b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  on_clear_callbacks_.clear();
331b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)}
332b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)
333c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)////////////////////////////////////////////////////////////////////////////////
33458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)// ProfileHelper, OAuth2LoginManager::Observer implementation:
33558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
33658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)void ProfileHelper::OnSessionRestoreStateChanged(
33758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    Profile* user_profile,
33858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    OAuth2LoginManager::SessionRestoreState state) {
3394e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  if (state == OAuth2LoginManager::SESSION_RESTORE_DONE ||
3404e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      state == OAuth2LoginManager::SESSION_RESTORE_FAILED ||
3414e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      state == OAuth2LoginManager::SESSION_RESTORE_CONNECTION_FAILED) {
34258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    chromeos::OAuth2LoginManager* login_manager =
3434e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)        chromeos::OAuth2LoginManagerFactory::GetInstance()->
3444e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)            GetForProfile(user_profile);
34558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    login_manager->RemoveObserver(this);
346868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    ClearSigninProfile(base::Closure());
34758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  }
348868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
349868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
350868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)////////////////////////////////////////////////////////////////////////////////
351c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// ProfileHelper, UserManager::UserSessionStateObserver implementation:
352c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
353c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)void ProfileHelper::ActiveUserHashChanged(const std::string& hash) {
354c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  active_user_id_hash_ = hash;
355c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
356c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
3575f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)void ProfileHelper::SetProfileToUserMappingForTesting(
3585f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    user_manager::User* user) {
359116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  user_list_for_testing_.push_back(user);
360116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch}
361116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
362116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// static
363116680a4aac90f2aa7413d9095a592090648e557Ben Murdochvoid ProfileHelper::SetProfileToUserForTestingEnabled(bool enabled) {
364116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  enable_profile_to_user_testing = enabled;
365116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch}
366116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
367116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// static
368116680a4aac90f2aa7413d9095a592090648e557Ben Murdochvoid ProfileHelper::SetAlwaysReturnPrimaryUserForTesting(bool value) {
369116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  always_return_primary_user_for_testing = true;
370116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  ProfileHelper::SetProfileToUserForTestingEnabled(true);
371116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch}
372116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
3735f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)void ProfileHelper::SetUserToProfileMappingForTesting(
3745f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    const user_manager::User* user,
3755f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    Profile* profile) {
376116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  user_to_profile_for_testing_[user] = profile;
377116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch}
378116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
3791320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci// static
3801320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tuccistd::string ProfileHelper::GetUserIdHashByUserIdForTesting(
3811320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    const std::string& user_id) {
3821320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  return user_id + kUserIdHashSuffix;
3831320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci}
3841320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
385c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}  // namespace chromeos
386