OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/profiles/profile_loader.h" | 5 #include "chrome/browser/ui/app_list/profile_loader.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/files/file_path.h" | 8 #include "base/files/file_path.h" |
9 #include "base/memory/weak_ptr.h" | 9 #include "base/memory/weak_ptr.h" |
10 #include "chrome/browser/lifetime/application_lifetime.h" | 10 #include "chrome/browser/lifetime/application_lifetime.h" |
11 #include "chrome/browser/profiles/profile_manager.h" | 11 #include "chrome/browser/profiles/profile_manager.h" |
12 | 12 |
13 ProfileLoader::ProfileLoader(ProfileManager* profile_manager) | 13 ProfileLoader::ProfileLoader(ProfileStore* profile_store, |
14 : profile_manager_(profile_manager), | 14 scoped_ptr<KeepAliveService> keep_alive_service) |
| 15 : profile_store_(profile_store), |
| 16 keep_alive_service_(keep_alive_service.Pass()), |
15 profile_load_sequence_id_(0), | 17 profile_load_sequence_id_(0), |
16 pending_profile_loads_(0), | 18 pending_profile_loads_(0), |
17 weak_factory_(this) { | 19 weak_factory_(this) { |
18 } | 20 } |
19 | 21 |
20 ProfileLoader::~ProfileLoader() { | 22 ProfileLoader::~ProfileLoader() { |
21 } | 23 } |
22 | 24 |
23 bool ProfileLoader::IsAnyProfileLoading() const { | 25 bool ProfileLoader::IsAnyProfileLoading() const { |
24 return pending_profile_loads_ > 0; | 26 return pending_profile_loads_ > 0; |
25 } | 27 } |
26 | 28 |
27 void ProfileLoader::InvalidatePendingProfileLoads() { | 29 void ProfileLoader::InvalidatePendingProfileLoads() { |
28 ++profile_load_sequence_id_; | 30 ++profile_load_sequence_id_; |
29 } | 31 } |
30 | 32 |
31 void ProfileLoader::LoadProfileInvalidatingOtherLoads( | 33 void ProfileLoader::LoadProfileInvalidatingOtherLoads( |
32 const base::FilePath& profile_file_path, | 34 const base::FilePath& profile_file_path, |
33 base::Callback<void(Profile*)> callback) { | 35 base::Callback<void(Profile*)> callback) { |
34 InvalidatePendingProfileLoads(); | 36 InvalidatePendingProfileLoads(); |
35 | 37 |
36 Profile* profile = GetProfileByPath(profile_file_path); | 38 Profile* profile = profile_store_->GetProfileByPath(profile_file_path); |
37 if (profile) { | 39 if (profile) { |
38 callback.Run(profile); | 40 callback.Run(profile); |
39 return; | 41 return; |
40 } | 42 } |
41 | 43 |
42 IncrementPendingProfileLoads(); | 44 IncrementPendingProfileLoads(); |
43 CreateProfileAsync( | 45 profile_store_->LoadProfileAsync( |
44 profile_file_path, | 46 profile_file_path, |
45 base::Bind(&ProfileLoader::OnProfileLoaded, | 47 base::Bind(&ProfileLoader::OnProfileLoaded, |
46 weak_factory_.GetWeakPtr(), | 48 weak_factory_.GetWeakPtr(), |
47 profile_load_sequence_id_, | 49 profile_load_sequence_id_, |
48 callback), | 50 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 } | 51 } |
65 | 52 |
66 void ProfileLoader::OnProfileLoaded(int profile_load_sequence_id, | 53 void ProfileLoader::OnProfileLoaded(int profile_load_sequence_id, |
67 base::Callback<void(Profile*)> callback, | 54 base::Callback<void(Profile*)> callback, |
68 Profile* profile, | 55 Profile* profile) { |
69 Profile::CreateStatus status) { | 56 DecrementPendingProfileLoads(); |
70 switch (status) { | 57 if (profile_load_sequence_id == profile_load_sequence_id_) |
71 case Profile::CREATE_STATUS_CREATED: | 58 callback.Run(profile); |
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 } | 59 } |
88 | 60 |
89 void ProfileLoader::IncrementPendingProfileLoads() { | 61 void ProfileLoader::IncrementPendingProfileLoads() { |
90 pending_profile_loads_++; | 62 pending_profile_loads_++; |
91 if (pending_profile_loads_ == 1) | 63 if (pending_profile_loads_ == 1) |
92 chrome::StartKeepAlive(); | 64 keep_alive_service_->EnsureKeepAlive(); |
93 } | 65 } |
94 | 66 |
95 void ProfileLoader::DecrementPendingProfileLoads() { | 67 void ProfileLoader::DecrementPendingProfileLoads() { |
96 pending_profile_loads_--; | 68 pending_profile_loads_--; |
97 if (pending_profile_loads_ == 0) | 69 if (pending_profile_loads_ == 0) |
98 chrome::EndKeepAlive(); | 70 keep_alive_service_->FreeKeepAlive(); |
99 } | 71 } |
OLD | NEW |