Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(572)

Side by Side Diff: chrome/browser/profiles/profile_manager.cc

Issue 9087009: Restore all profiles which were active when restoring the last open pages. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix. Created 8 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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 <set> 5 #include <set>
6 6
7 #include "chrome/browser/profiles/profile_manager.h" 7 #include "chrome/browser/profiles/profile_manager.h"
8 8
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/command_line.h" 10 #include "base/command_line.h"
(...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after
174 ProfileManager* profile_manager = g_browser_process->profile_manager(); 174 ProfileManager* profile_manager = g_browser_process->profile_manager();
175 return profile_manager->GetDefaultProfile(profile_manager->user_data_dir_); 175 return profile_manager->GetDefaultProfile(profile_manager->user_data_dir_);
176 } 176 }
177 177
178 // static 178 // static
179 Profile* ProfileManager::GetLastUsedProfile() { 179 Profile* ProfileManager::GetLastUsedProfile() {
180 ProfileManager* profile_manager = g_browser_process->profile_manager(); 180 ProfileManager* profile_manager = g_browser_process->profile_manager();
181 return profile_manager->GetLastUsedProfile(profile_manager->user_data_dir_); 181 return profile_manager->GetLastUsedProfile(profile_manager->user_data_dir_);
182 } 182 }
183 183
184 // static
185 std::vector<Profile*> ProfileManager::GetLastActiveProfiles() {
186 ProfileManager* profile_manager = g_browser_process->profile_manager();
187 return profile_manager->GetLastActiveProfiles(
188 profile_manager->user_data_dir_);
189 }
190
184 ProfileManager::ProfileManager(const FilePath& user_data_dir) 191 ProfileManager::ProfileManager(const FilePath& user_data_dir)
185 : user_data_dir_(user_data_dir), 192 : user_data_dir_(user_data_dir),
186 logged_in_(false), 193 logged_in_(false),
187 will_import_(false) { 194 will_import_(false),
195 shutdown_started_(false) {
188 BrowserList::AddObserver(this); 196 BrowserList::AddObserver(this);
189 #if defined(OS_CHROMEOS) 197 #if defined(OS_CHROMEOS)
190 registrar_.Add( 198 registrar_.Add(
191 this, 199 this,
192 chrome::NOTIFICATION_LOGIN_USER_CHANGED, 200 chrome::NOTIFICATION_LOGIN_USER_CHANGED,
193 content::NotificationService::AllSources()); 201 content::NotificationService::AllSources());
194 #endif 202 #endif
203 registrar_.Add(
204 this,
205 chrome::NOTIFICATION_BROWSER_OPENED,
206 content::NotificationService::AllSources());
207 registrar_.Add(
208 this,
209 chrome::NOTIFICATION_BROWSER_CLOSED,
210 content::NotificationService::AllSources());
211 registrar_.Add(
212 this,
213 content::NOTIFICATION_APP_EXITING,
214 content::NotificationService::AllSources());
195 } 215 }
196 216
197 ProfileManager::~ProfileManager() { 217 ProfileManager::~ProfileManager() {
198 BrowserList::RemoveObserver(this); 218 BrowserList::RemoveObserver(this);
199 #if defined(OS_WIN) 219 #if defined(OS_WIN)
200 if (profile_shortcut_manager_.get()) 220 if (profile_shortcut_manager_.get())
201 profile_info_cache_->RemoveObserver(profile_shortcut_manager_.get()); 221 profile_info_cache_->RemoveObserver(profile_shortcut_manager_.get());
202 #endif 222 #endif
203 } 223 }
204 224
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
248 DCHECK(local_state); 268 DCHECK(local_state);
249 269
250 if (local_state->HasPrefPath(prefs::kProfileLastUsed)) 270 if (local_state->HasPrefPath(prefs::kProfileLastUsed))
251 last_profile_used = local_state->GetString(prefs::kProfileLastUsed); 271 last_profile_used = local_state->GetString(prefs::kProfileLastUsed);
252 last_used_profile_dir = last_profile_used.empty() ? 272 last_used_profile_dir = last_profile_used.empty() ?
253 last_used_profile_dir.AppendASCII(chrome::kInitialProfile) : 273 last_used_profile_dir.AppendASCII(chrome::kInitialProfile) :
254 last_used_profile_dir.AppendASCII(last_profile_used); 274 last_used_profile_dir.AppendASCII(last_profile_used);
255 return GetProfile(last_used_profile_dir); 275 return GetProfile(last_used_profile_dir);
256 } 276 }
257 277
278 std::vector<Profile*> ProfileManager::GetLastActiveProfiles(
279 const FilePath& user_data_dir) {
280 PrefService* local_state = g_browser_process->local_state();
281 DCHECK(local_state);
282
283 std::vector<Profile*> to_return;
284 if (local_state->HasPrefPath(prefs::kProfilesLastActive)) {
285 const ListValue* profile_list =
286 local_state->GetList(prefs::kProfilesLastActive);
287 if (profile_list) {
288 ListValue::const_iterator it;
289 std::string profile;
290 for (it = profile_list->begin(); it != profile_list->end(); ++it) {
291 if (!(*it)->GetAsString(&profile) || profile.empty()) {
292 LOG(WARNING) << "Invalid entry in " << prefs::kProfilesLastActive;
293 continue;
294 }
295 to_return.push_back(GetProfile(user_data_dir.AppendASCII(profile)));
296 }
297 }
298 }
299 return to_return;
300 }
301
258 Profile* ProfileManager::GetDefaultProfile(const FilePath& user_data_dir) { 302 Profile* ProfileManager::GetDefaultProfile(const FilePath& user_data_dir) {
259 FilePath default_profile_dir(user_data_dir); 303 FilePath default_profile_dir(user_data_dir);
260 default_profile_dir = default_profile_dir.Append(GetInitialProfileDir()); 304 default_profile_dir = default_profile_dir.Append(GetInitialProfileDir());
261 #if defined(OS_CHROMEOS) 305 #if defined(OS_CHROMEOS)
262 if (!logged_in_) { 306 if (!logged_in_) {
263 Profile* profile = GetProfile(default_profile_dir); 307 Profile* profile = GetProfile(default_profile_dir);
264 // For cros, return the OTR profile so we never accidentally keep 308 // For cros, return the OTR profile so we never accidentally keep
265 // user data in an unencrypted profile. But doing this makes 309 // user data in an unencrypted profile. But doing this makes
266 // many of the browser and ui tests fail. We do return the OTR profile 310 // many of the browser and ui tests fail. We do return the OTR profile
267 // if the login-profile switch is passed so that we can test this. 311 // if the login-profile switch is passed so that we can test this.
(...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after
406 if (type == chrome::NOTIFICATION_LOGIN_USER_CHANGED) { 450 if (type == chrome::NOTIFICATION_LOGIN_USER_CHANGED) {
407 const CommandLine& command_line = *CommandLine::ForCurrentProcess(); 451 const CommandLine& command_line = *CommandLine::ForCurrentProcess();
408 if (!command_line.HasSwitch(switches::kTestType)) { 452 if (!command_line.HasSwitch(switches::kTestType)) {
409 // If we don't have a mounted profile directory we're in trouble. 453 // If we don't have a mounted profile directory we're in trouble.
410 // TODO(davemoore) Once we have better api this check should ensure that 454 // TODO(davemoore) Once we have better api this check should ensure that
411 // our profile directory is the one that's mounted, and that it's mounted 455 // our profile directory is the one that's mounted, and that it's mounted
412 // as the current user. 456 // as the current user.
413 CHECK(chromeos::CrosLibrary::Get()->GetCryptohomeLibrary()->IsMounted()); 457 CHECK(chromeos::CrosLibrary::Get()->GetCryptohomeLibrary()->IsMounted());
414 } 458 }
415 logged_in_ = true; 459 logged_in_ = true;
460 return;
416 } 461 }
417 #endif 462 #endif
463 if (shutdown_started_)
464 return;
465
466 bool update_active_profiles = false;
467 switch (type) {
468 case content::NOTIFICATION_APP_EXITING: {
469 // Ignore any browsers closing from now on.
470 shutdown_started_ = true;
471 break;
472 }
473 case chrome::NOTIFICATION_BROWSER_OPENED: {
474 Browser* browser = content::Source<Browser>(source).ptr();
475 DCHECK(browser);
476 Profile* profile = browser->profile();
477 DCHECK(profile);
478 if (++browser_counts_[profile] == 1) {
479 active_profiles_.push_back(profile);
480 update_active_profiles = true;
481 }
482 break;
483 }
484 case chrome::NOTIFICATION_BROWSER_CLOSED: {
485 Browser* browser = content::Source<Browser>(source).ptr();
486 DCHECK(browser);
487 Profile* profile = browser->profile();
488 DCHECK(profile);
489 if (--browser_counts_[profile] == 0) {
490 active_profiles_.erase(
491 std::remove(active_profiles_.begin(), active_profiles_.end(),
492 profile),
493 active_profiles_.end());
494 update_active_profiles = true;
495 }
496 break;
497 }
498 default:
Miranda Callahan 2012/01/09 14:38:55 default block should also have braces.
marja 2012/01/10 14:12:03 Done.
499 NOTREACHED();
500 break;
501 }
502 if (update_active_profiles) {
503 PrefService* local_state = g_browser_process->local_state();
504 DCHECK(local_state);
505 ListPrefUpdate update(local_state, prefs::kProfilesLastActive);
506 ListValue* profile_list = update.Get();
507
508 profile_list->Clear();
509 std::vector<Profile*>::const_iterator it;
510 for (it = active_profiles_.begin(); it != active_profiles_.end(); ++it) {
511 profile_list->Append(
512 new StringValue((*it)->GetPath().BaseName().MaybeAsASCII()));
513 }
514 }
418 } 515 }
419 516
420 void ProfileManager::SetWillImport() { 517 void ProfileManager::SetWillImport() {
421 will_import_ = true; 518 will_import_ = true;
422 } 519 }
423 520
424 void ProfileManager::OnImportFinished(Profile* profile) { 521 void ProfileManager::OnImportFinished(Profile* profile) {
425 will_import_ = false; 522 will_import_ = false;
426 DCHECK(profile); 523 DCHECK(profile);
427 content::NotificationService::current()->Notify( 524 content::NotificationService::current()->Notify(
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after
546 FilePath new_path = profile_manager->GenerateNextProfileDirectoryPath(); 643 FilePath new_path = profile_manager->GenerateNextProfileDirectoryPath();
547 644
548 profile_manager->CreateProfileAsync(new_path, 645 profile_manager->CreateProfileAsync(new_path,
549 base::Bind(&OnOpenWindowForNewProfile)); 646 base::Bind(&OnOpenWindowForNewProfile));
550 } 647 }
551 648
552 // static 649 // static
553 void ProfileManager::RegisterPrefs(PrefService* prefs) { 650 void ProfileManager::RegisterPrefs(PrefService* prefs) {
554 prefs->RegisterStringPref(prefs::kProfileLastUsed, ""); 651 prefs->RegisterStringPref(prefs::kProfileLastUsed, "");
555 prefs->RegisterIntegerPref(prefs::kProfilesNumCreated, 1); 652 prefs->RegisterIntegerPref(prefs::kProfilesNumCreated, 1);
653 prefs->RegisterListPref(prefs::kProfilesLastActive);
556 } 654 }
557 655
558 size_t ProfileManager::GetNumberOfProfiles() { 656 size_t ProfileManager::GetNumberOfProfiles() {
559 return GetProfileInfoCache().GetNumberOfProfiles(); 657 return GetProfileInfoCache().GetNumberOfProfiles();
560 } 658 }
561 659
562 bool ProfileManager::CompareProfilePathAndName( 660 bool ProfileManager::CompareProfilePathAndName(
563 const ProfileManager::ProfilePathAndName& pair1, 661 const ProfileManager::ProfilePathAndName& pair1,
564 const ProfileManager::ProfilePathAndName& pair2) { 662 const ProfileManager::ProfilePathAndName& pair2) {
565 int name_compare = pair1.second.compare(pair2.second); 663 int name_compare = pair1.second.compare(pair2.second);
(...skipping 176 matching lines...) Expand 10 before | Expand all | Expand 10 after
742 profile_info_cache_->RemoveObserver(profile_shortcut_manager_.get()); 840 profile_info_cache_->RemoveObserver(profile_shortcut_manager_.get());
743 } 841 }
744 #endif 842 #endif
745 843
746 void ProfileManager::RunCallbacks(const std::vector<CreateCallback>& callbacks, 844 void ProfileManager::RunCallbacks(const std::vector<CreateCallback>& callbacks,
747 Profile* profile, 845 Profile* profile,
748 Profile::CreateStatus status) { 846 Profile::CreateStatus status) {
749 for (size_t i = 0; i < callbacks.size(); ++i) 847 for (size_t i = 0; i < callbacks.size(); ++i)
750 callbacks[i].Run(profile, status); 848 callbacks[i].Run(profile, status);
751 } 849 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698