1 // Copyright 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/profiles/profile_loader.h" 6 7 #include "base/bind.h" 8 #include "base/files/file_path.h" 9 #include "base/memory/weak_ptr.h" 10 #include "chrome/browser/lifetime/application_lifetime.h" 11 #include "chrome/browser/profiles/profile_manager.h" 12 13 ProfileLoader::ProfileLoader(ProfileManager* profile_manager) 14 : profile_manager_(profile_manager), 15 profile_load_sequence_id_(0), 16 pending_profile_loads_(0), 17 weak_factory_(this) { 18 } 19 20 ProfileLoader::~ProfileLoader() { 21 } 22 23 bool ProfileLoader::IsAnyProfileLoading() const { 24 return pending_profile_loads_ > 0; 25 } 26 27 void ProfileLoader::InvalidatePendingProfileLoads() { 28 ++profile_load_sequence_id_; 29 } 30 31 void ProfileLoader::LoadProfileInvalidatingOtherLoads( 32 const base::FilePath& profile_file_path, 33 base::Callback<void(Profile*)> callback) { 34 InvalidatePendingProfileLoads(); 35 36 Profile* profile = GetProfileByPath(profile_file_path); 37 if (profile) { 38 callback.Run(profile); 39 return; 40 } 41 42 IncrementPendingProfileLoads(); 43 CreateProfileAsync( 44 profile_file_path, 45 base::Bind(&ProfileLoader::OnProfileLoaded, 46 weak_factory_.GetWeakPtr(), 47 profile_load_sequence_id_, 48 callback), 49 string16(), string16(), std::string()); 50 } 51 52 Profile* ProfileLoader::GetProfileByPath(const base::FilePath& path) { 53 return profile_manager_->GetProfileByPath(path); 54 } 55 56 void ProfileLoader::CreateProfileAsync( 57 const base::FilePath& profile_path, 58 const ProfileManager::CreateCallback& callback, 59 const string16& name, 60 const string16& icon_url, 61 const std::string& managed_user_id) { 62 profile_manager_->CreateProfileAsync( 63 profile_path, callback, name, icon_url, managed_user_id); 64 } 65 66 void ProfileLoader::OnProfileLoaded(int profile_load_sequence_id, 67 base::Callback<void(Profile*)> callback, 68 Profile* profile, 69 Profile::CreateStatus status) { 70 switch (status) { 71 case Profile::CREATE_STATUS_CREATED: 72 break; 73 case Profile::CREATE_STATUS_INITIALIZED: 74 if (profile_load_sequence_id == profile_load_sequence_id_) 75 callback.Run(profile); 76 DecrementPendingProfileLoads(); 77 break; 78 case Profile::CREATE_STATUS_LOCAL_FAIL: 79 case Profile::CREATE_STATUS_REMOTE_FAIL: 80 case Profile::CREATE_STATUS_CANCELED: 81 DecrementPendingProfileLoads(); 82 break; 83 case Profile::MAX_CREATE_STATUS: 84 NOTREACHED(); 85 break; 86 } 87 } 88 89 void ProfileLoader::IncrementPendingProfileLoads() { 90 pending_profile_loads_++; 91 if (pending_profile_loads_ == 1) 92 chrome::StartKeepAlive(); 93 } 94 95 void ProfileLoader::DecrementPendingProfileLoads() { 96 pending_profile_loads_--; 97 if (pending_profile_loads_ == 0) 98 chrome::EndKeepAlive(); 99 } 100