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/cros/network_library.h" 12 #include "chrome/browser/chromeos/login/user_manager.h" 13 #include "chrome/browser/chromeos/sms_observer.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 19 namespace chromeos { 20 21 namespace { 22 23 base::FilePath GetSigninProfileDir() { 24 ProfileManager* profile_manager = g_browser_process->profile_manager(); 25 base::FilePath user_data_dir = profile_manager->user_data_dir(); 26 return user_data_dir.AppendASCII(chrome::kInitialProfile); 27 } 28 29 } // anonymous namespace 30 31 //////////////////////////////////////////////////////////////////////////////// 32 // ProfileHelper, public 33 34 ProfileHelper::ProfileHelper() 35 : signin_profile_clear_requested_(false) { 36 } 37 38 ProfileHelper::~ProfileHelper() { 39 // Checking whether UserManager is initialized covers case 40 // when ScopedTestUserManager is used. 41 if (UserManager::IsInitialized()) { 42 UserManager::Get()->RemoveObserver(this); 43 UserManager::Get()->RemoveSessionStateObserver(this); 44 } 45 } 46 47 // static 48 Profile* ProfileHelper::GetProfileByUserIdHash( 49 const std::string& user_id_hash) { 50 ProfileManager* profile_manager = g_browser_process->profile_manager(); 51 return profile_manager->GetProfile(GetProfilePathByUserIdHash(user_id_hash)); 52 } 53 54 // static 55 base::FilePath ProfileHelper::GetProfilePathByUserIdHash( 56 const std::string& user_id_hash) { 57 const CommandLine& command_line = *CommandLine::ForCurrentProcess(); 58 // Fails for KioskTest.InstallAndLaunchApp test - crbug.com/238985 59 // Will probably fail for Guest session / restart after a crash - 60 // crbug.com/238998 61 // TODO(nkostylev): Remove this check once these bugs are fixed. 62 if (command_line.HasSwitch(switches::kMultiProfiles)) 63 DCHECK(!user_id_hash.empty()); 64 ProfileManager* profile_manager = g_browser_process->profile_manager(); 65 base::FilePath profile_path = profile_manager->user_data_dir(); 66 return profile_path.Append( 67 base::FilePath(chrome::kProfileDirPrefix + user_id_hash)); 68 } 69 70 // static 71 Profile* ProfileHelper::GetSigninProfile() { 72 ProfileManager* profile_manager = g_browser_process->profile_manager(); 73 return profile_manager->GetProfile(GetSigninProfileDir())-> 74 GetOffTheRecordProfile(); 75 } 76 77 // static 78 std::string ProfileHelper::GetUserIdHashFromProfile(Profile* profile) { 79 if (!profile) 80 return std::string(); 81 82 // Check that profile directory starts with the correct prefix. 83 std::string profile_dir = profile->GetPath().BaseName().value(); 84 std::string prefix(chrome::kProfileDirPrefix); 85 if (profile_dir.find(prefix) != 0) { 86 NOTREACHED(); 87 return std::string(); 88 } 89 90 return profile_dir.substr(prefix.length(), 91 profile_dir.length() - prefix.length()); 92 } 93 94 // static 95 bool ProfileHelper::IsSigninProfile(Profile* profile) { 96 return profile->GetPath().BaseName().value() == chrome::kInitialProfile; 97 } 98 99 // static 100 void ProfileHelper::ProfileStartup(Profile* profile, bool process_startup) { 101 // Initialize Chrome OS preferences like touch pad sensitivity. For the 102 // preferences to work in the guest mode, the initialization has to be 103 // done after |profile| is switched to the incognito profile (which 104 // is actually GuestSessionProfile in the guest mode). See the 105 // GetOffTheRecordProfile() call above. 106 profile->InitChromeOSPreferences(); 107 108 if (process_startup) { 109 static chromeos::SmsObserver* sms_observer = 110 new chromeos::SmsObserver(); 111 chromeos::NetworkLibrary::Get()-> 112 AddNetworkManagerObserver(sms_observer); 113 114 profile->SetupChromeOSEnterpriseExtensionObserver(); 115 } 116 } 117 118 base::FilePath ProfileHelper::GetActiveUserProfileDir() { 119 DCHECK(!active_user_id_hash_.empty()); 120 return base::FilePath(chrome::kProfileDirPrefix + active_user_id_hash_); 121 } 122 123 void ProfileHelper::Initialize() { 124 UserManager::Get()->AddObserver(this); 125 UserManager::Get()->AddSessionStateObserver(this); 126 } 127 128 void ProfileHelper::ClearSigninProfile(const base::Closure& on_clear_callback) { 129 on_clear_callbacks_.push_back(on_clear_callback); 130 if (signin_profile_clear_requested_) 131 return; 132 ProfileManager* profile_manager = g_browser_process->profile_manager(); 133 // Check if signin profile was loaded. 134 if (!profile_manager->GetProfileByPath(GetSigninProfileDir())) { 135 OnBrowsingDataRemoverDone(); 136 return; 137 } 138 signin_profile_clear_requested_ = true; 139 BrowsingDataRemover* remover = 140 BrowsingDataRemover::CreateForUnboundedRange(GetSigninProfile()); 141 remover->AddObserver(this); 142 remover->Remove(BrowsingDataRemover::REMOVE_SITE_DATA, 143 BrowsingDataHelper::ALL); 144 } 145 146 //////////////////////////////////////////////////////////////////////////////// 147 // ProfileHelper, BrowsingDataRemover::Observer implementation: 148 149 void ProfileHelper::OnBrowsingDataRemoverDone() { 150 signin_profile_clear_requested_ = false; 151 for (size_t i = 0; i < on_clear_callbacks_.size(); ++i) { 152 if (!on_clear_callbacks_[i].is_null()) 153 on_clear_callbacks_[i].Run(); 154 } 155 on_clear_callbacks_.clear(); 156 } 157 158 //////////////////////////////////////////////////////////////////////////////// 159 // ProfileHelper, UserManager::Observer implementation: 160 161 void ProfileHelper::MergeSessionStateChanged( 162 UserManager::MergeSessionState state) { 163 if (state == UserManager:: MERGE_STATUS_DONE) 164 ClearSigninProfile(base::Closure()); 165 } 166 167 //////////////////////////////////////////////////////////////////////////////// 168 // ProfileHelper, UserManager::UserSessionStateObserver implementation: 169 170 void ProfileHelper::ActiveUserHashChanged(const std::string& hash) { 171 active_user_id_hash_ = hash; 172 base::FilePath profile_path = GetProfilePathByUserIdHash(hash); 173 LOG(INFO) << "Switching to profile path: " << profile_path.value(); 174 } 175 176 } // namespace chromeos 177