Chromium Code Reviews| Index: chrome/browser/app_controller_mac.mm |
| diff --git a/chrome/browser/app_controller_mac.mm b/chrome/browser/app_controller_mac.mm |
| index feb0f95f782cfa782706dee63350095316590490..8c84963de16dd47766092e74694cbe6eaaaa08d5 100644 |
| --- a/chrome/browser/app_controller_mac.mm |
| +++ b/chrome/browser/app_controller_mac.mm |
| @@ -28,6 +28,7 @@ |
| #include "chrome/browser/first_run/first_run.h" |
| #include "chrome/browser/lifetime/application_lifetime.h" |
| #include "chrome/browser/printing/print_dialog_cloud.h" |
| +#include "chrome/browser/profiles/profile_info_cache_observer.h" |
| #include "chrome/browser/profiles/profile_manager.h" |
| #include "chrome/browser/service/service_process_control.h" |
| #include "chrome/browser/sessions/session_restore.h" |
| @@ -190,8 +191,57 @@ void RecordLastRunAppBundlePath() { |
| - (BOOL)userWillWaitForInProgressDownloads:(int)downloadCount; |
| - (BOOL)shouldQuitWithInProgressDownloads; |
| - (void)executeApplication:(id)sender; |
| +- (void)profileWasRemoved:(const string16&)profileName; |
| @end |
| +class AppControllerProfileObserver : public ProfileInfoCacheObserver { |
| + public: |
| + AppControllerProfileObserver( |
| + ProfileManager* profile_manager, AppController* app_controller) |
| + : profile_manager_(profile_manager), |
| + app_controller_(app_controller) { |
| + DCHECK(profile_manager_); |
| + DCHECK(app_controller_); |
| + profile_manager_->GetProfileInfoCache().AddObserver(this); |
| + } |
| + |
| + virtual ~AppControllerProfileObserver() { |
| + DCHECK(profile_manager_); |
| + profile_manager_->GetProfileInfoCache().RemoveObserver(this); |
| + } |
| + |
| + private: |
| + // ProfileInfoCacheObserver implementation: |
| + |
| + virtual void OnProfileAdded(const base::FilePath& profile_path) OVERRIDE { |
| + } |
| + |
| + virtual void OnProfileWasRemoved(const base::FilePath& profile_path, |
| + const string16& profile_name) OVERRIDE { |
| + // When a profile is deleted we need to notify the AppController, |
| + // so that it can correctly update its pointer to the last used profile. |
| + [app_controller_ profileWasRemoved:profile_name]; |
| + } |
| + |
| + virtual void OnProfileWillBeRemoved( |
| + const base::FilePath& profile_path) OVERRIDE { |
| + } |
| + |
| + virtual void OnProfileNameChanged(const base::FilePath& profile_path, |
| + const string16& old_profile_name) OVERRIDE { |
| + } |
| + |
| + virtual void OnProfileAvatarChanged( |
| + const base::FilePath& profile_path) OVERRIDE { |
| + } |
| + |
| + ProfileManager* profile_manager_; |
| + |
| + AppController* app_controller_; // Weak; owns us. |
| + |
| + DISALLOW_COPY_AND_ASSIGN(AppControllerProfileObserver); |
| +}; |
| + |
| @implementation AppController |
| @synthesize startupComplete = startupComplete_; |
| @@ -505,7 +555,7 @@ void RecordLastRunAppBundlePath() { |
| // no-op if the new profile is the same as the current one. This will always be |
| // the original profile and never incognito. |
| - (void)windowChangedToProfile:(Profile*)profile { |
| - if (lastProfile_ == profile) |
| + if (lastProfile_ == profile && !lastProfileWasDeleted_) |
| return; |
| // Before tearing down the menu controller bridges, return the Cocoa menus to |
| @@ -518,6 +568,9 @@ void RecordLastRunAppBundlePath() { |
| // Rebuild the menus with the new profile. |
| lastProfile_ = profile; |
| + if (lastProfileWasDeleted_) |
| + lastProfileWasDeleted_ = NO; |
| + |
| bookmarkMenuBridge_.reset(new BookmarkMenuBridge(lastProfile_, |
| [[[NSApp mainMenu] itemWithTag:IDC_BOOKMARKS_MENU] submenu])); |
| // No need to |BuildMenu| here. It is done lazily upon menu access. |
| @@ -585,6 +638,12 @@ void RecordLastRunAppBundlePath() { |
| EncodingMenuControllerDelegate::BuildEncodingMenu([self lastProfile], |
| encodingMenu); |
| + // Instantiate the ProfileInfoCache observer so that we can get |
| + // notified when a profile is deleted. |
| + lastProfileWasDeleted_ = NO; |
| + profileInfoCacheObserver_.reset(new AppControllerProfileObserver( |
| + g_browser_process->profile_manager(), self)); |
| + |
| // Since Chrome is localized to more languages than the OS, tell Cocoa which |
| // menu is the Help so it can add the search item to it. |
| [NSApp setHelpMenu:helpMenu_]; |
| @@ -714,6 +773,21 @@ void RecordLastRunAppBundlePath() { |
| return service && !service->entries().empty(); |
| } |
| +// Called from the AppControllerProfileObserver every time a profile is deleted. |
| +- (void)profileWasRemoved:(const string16&)profileName { |
| + Profile* lastProfile = [self lastProfile]; |
| + string16 lastProfileName = UTF8ToUTF16(lastProfile->GetPrefs()->GetString( |
| + prefs::kProfileName)); |
| + |
| + // If the last profile has been deleted, we must NULL out our pointer |
| + // otherwise we will try to start up a browser window with a pointer |
| + // to the old profile. |
| + if (profileName == lastProfileName) { |
| + lastProfileWasDeleted_ = YES; |
|
Alexei Svitkine (slow)
2013/06/05 20:00:52
Instead of introducing this variable to track this
noms (inactive)
2013/06/11 19:32:17
yup, you're right. Done. On 2013/06/05 20:00:52, A
|
| + lastProfile_ = NULL; |
| + } |
| +} |
| + |
| // Returns true if there is a modal window (either window- or application- |
| // modal) blocking the active browser. Note that tab modal dialogs (HTTP auth |
| // sheets) will not count as blocking the browser. But things like open/save |
| @@ -1091,11 +1165,21 @@ void RecordLastRunAppBundlePath() { |
| if (lastProfile_) |
| return lastProfile_; |
| + if (!g_browser_process->profile_manager()) |
| + return NULL; |
| + |
| // On first launch, no profile will be stored, so use last from Local State. |
| - if (g_browser_process->profile_manager()) |
| - return g_browser_process->profile_manager()->GetLastUsedProfile(); |
| + Profile* lastUsedProfile = |
| + g_browser_process->profile_manager()->GetLastUsedProfile(); |
| + |
| + // If the last profile was deleted, we've already loaded the next active one, |
| + // so we should update the pointer; otherwise we will try to open the old one. |
| + // We unset lastProfileWasDeleted_ when the window is actually loaded, so that |
| + // all the profile preferences/menus are re-created. |
| + if (lastProfileWasDeleted_) |
| + lastProfile_ = lastUsedProfile; |
| - return NULL; |
| + return lastUsedProfile; |
| } |
| // Various methods to open URLs that we get in a native fashion. We use |