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

Side by Side Diff: chrome/browser/app_controller_mac.mm

Issue 14923004: [Mac] AppController needs to update its "last profile" pointer when the active profile is deleted (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix whitespace Created 7 years, 7 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
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 #import "chrome/browser/app_controller_mac.h" 5 #import "chrome/browser/app_controller_mac.h"
6 6
7 #include "base/auto_reset.h" 7 #include "base/auto_reset.h"
8 #include "base/bind.h" 8 #include "base/bind.h"
9 #include "base/command_line.h" 9 #include "base/command_line.h"
10 #include "base/files/file_path.h" 10 #include "base/files/file_path.h"
(...skipping 10 matching lines...) Expand all
21 #include "chrome/browser/browser_process.h" 21 #include "chrome/browser/browser_process.h"
22 #include "chrome/browser/browser_shutdown.h" 22 #include "chrome/browser/browser_shutdown.h"
23 #include "chrome/browser/command_updater.h" 23 #include "chrome/browser/command_updater.h"
24 #include "chrome/browser/download/download_service.h" 24 #include "chrome/browser/download/download_service.h"
25 #include "chrome/browser/download/download_service_factory.h" 25 #include "chrome/browser/download/download_service_factory.h"
26 #include "chrome/browser/extensions/extension_service.h" 26 #include "chrome/browser/extensions/extension_service.h"
27 #include "chrome/browser/extensions/extension_system.h" 27 #include "chrome/browser/extensions/extension_system.h"
28 #include "chrome/browser/first_run/first_run.h" 28 #include "chrome/browser/first_run/first_run.h"
29 #include "chrome/browser/lifetime/application_lifetime.h" 29 #include "chrome/browser/lifetime/application_lifetime.h"
30 #include "chrome/browser/printing/print_dialog_cloud.h" 30 #include "chrome/browser/printing/print_dialog_cloud.h"
31 #include "chrome/browser/profiles/profile_info_cache_observer.h"
31 #include "chrome/browser/profiles/profile_manager.h" 32 #include "chrome/browser/profiles/profile_manager.h"
32 #include "chrome/browser/service/service_process_control.h" 33 #include "chrome/browser/service/service_process_control.h"
33 #include "chrome/browser/sessions/session_restore.h" 34 #include "chrome/browser/sessions/session_restore.h"
34 #include "chrome/browser/sessions/session_service.h" 35 #include "chrome/browser/sessions/session_service.h"
35 #include "chrome/browser/sessions/session_service_factory.h" 36 #include "chrome/browser/sessions/session_service_factory.h"
36 #include "chrome/browser/sessions/tab_restore_service.h" 37 #include "chrome/browser/sessions/tab_restore_service.h"
37 #include "chrome/browser/sessions/tab_restore_service_factory.h" 38 #include "chrome/browser/sessions/tab_restore_service_factory.h"
38 #include "chrome/browser/signin/signin_manager.h" 39 #include "chrome/browser/signin/signin_manager.h"
39 #include "chrome/browser/signin/signin_manager_factory.h" 40 #include "chrome/browser/signin/signin_manager_factory.h"
40 #include "chrome/browser/sync/profile_sync_service.h" 41 #include "chrome/browser/sync/profile_sync_service.h"
(...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after
183 - (void)openUrls:(const std::vector<GURL>&)urls; 184 - (void)openUrls:(const std::vector<GURL>&)urls;
184 - (void)getUrl:(NSAppleEventDescriptor*)event 185 - (void)getUrl:(NSAppleEventDescriptor*)event
185 withReply:(NSAppleEventDescriptor*)reply; 186 withReply:(NSAppleEventDescriptor*)reply;
186 - (void)submitCloudPrintJob:(NSAppleEventDescriptor*)event; 187 - (void)submitCloudPrintJob:(NSAppleEventDescriptor*)event;
187 - (void)windowLayeringDidChange:(NSNotification*)inNotification; 188 - (void)windowLayeringDidChange:(NSNotification*)inNotification;
188 - (void)windowChangedToProfile:(Profile*)profile; 189 - (void)windowChangedToProfile:(Profile*)profile;
189 - (void)checkForAnyKeyWindows; 190 - (void)checkForAnyKeyWindows;
190 - (BOOL)userWillWaitForInProgressDownloads:(int)downloadCount; 191 - (BOOL)userWillWaitForInProgressDownloads:(int)downloadCount;
191 - (BOOL)shouldQuitWithInProgressDownloads; 192 - (BOOL)shouldQuitWithInProgressDownloads;
192 - (void)executeApplication:(id)sender; 193 - (void)executeApplication:(id)sender;
194 - (void)profileWasRemoved:(const string16&)profileName;
193 @end 195 @end
194 196
197 class AppControllerProfileObserver : public ProfileInfoCacheObserver {
198 public:
199 AppControllerProfileObserver(
200 ProfileManager* profile_manager, AppController* app_controller)
201 : profile_manager_(profile_manager),
202 app_controller_(app_controller) {
203 DCHECK(profile_manager_);
Roger Tawa OOO till Jul 10th 2013/05/14 14:38:40 Add dcheck for app_controller_ too? From code seem
noms (inactive) 2013/05/14 15:06:48 Done.
204 profile_manager_->GetProfileInfoCache().AddObserver(this);
205 }
206
207 virtual ~AppControllerProfileObserver() {
208 DCHECK(profile_manager_);
209 profile_manager_->GetProfileInfoCache().RemoveObserver(this);
210 }
211
212 private:
213 // ProfileInfoCacheObserver implementation:
214
215 virtual void OnProfileAdded(const base::FilePath& profile_path) OVERRIDE {
216 }
217
218 virtual void OnProfileWasRemoved(const base::FilePath& profile_path,
219 const string16& profile_name) OVERRIDE {
220 // When a profile is deleted we need to notify the AppController,
221 // so that it can correctly update its pointer to the last used profile.
222 [app_controller_ profileWasRemoved:profile_name];
223 }
224
225 virtual void OnProfileWillBeRemoved(
226 const base::FilePath& profile_path) OVERRIDE {
227 }
228
229 virtual void OnProfileNameChanged(const base::FilePath& profile_path,
230 const string16& old_profile_name) OVERRIDE {
231 }
232
233 virtual void OnProfileAvatarChanged(
234 const base::FilePath& profile_path) OVERRIDE {
235 }
236
237 ProfileManager* profile_manager_;
238
239 AppController* app_controller_; // Weak; owns us.
240
241 DISALLOW_COPY_AND_ASSIGN(AppControllerProfileObserver);
242 };
243
195 @implementation AppController 244 @implementation AppController
196 245
197 @synthesize startupComplete = startupComplete_; 246 @synthesize startupComplete = startupComplete_;
198 247
199 // This method is called very early in application startup (ie, before 248 // This method is called very early in application startup (ie, before
200 // the profile is loaded or any preferences have been registered). Defer any 249 // the profile is loaded or any preferences have been registered). Defer any
201 // user-data initialization until -applicationDidFinishLaunching:. 250 // user-data initialization until -applicationDidFinishLaunching:.
202 - (void)awakeFromNib { 251 - (void)awakeFromNib {
203 // We need to register the handlers early to catch events fired on launch. 252 // We need to register the handlers early to catch events fired on launch.
204 NSAppleEventManager* em = [NSAppleEventManager sharedAppleEventManager]; 253 NSAppleEventManager* em = [NSAppleEventManager sharedAppleEventManager];
(...skipping 293 matching lines...) Expand 10 before | Expand all | Expand 10 after
498 - (void)popoverDidClose:(NSNotification*)notify { 547 - (void)popoverDidClose:(NSNotification*)notify {
499 hasPopover_ = NO; 548 hasPopover_ = NO;
500 [self fixCloseMenuItemKeyEquivalents]; 549 [self fixCloseMenuItemKeyEquivalents];
501 } 550 }
502 551
503 // Called when the user has changed browser windows, meaning the backing profile 552 // Called when the user has changed browser windows, meaning the backing profile
504 // may have changed. This can cause a rebuild of the user-data menus. This is a 553 // may have changed. This can cause a rebuild of the user-data menus. This is a
505 // no-op if the new profile is the same as the current one. This will always be 554 // no-op if the new profile is the same as the current one. This will always be
506 // the original profile and never incognito. 555 // the original profile and never incognito.
507 - (void)windowChangedToProfile:(Profile*)profile { 556 - (void)windowChangedToProfile:(Profile*)profile {
508 if (lastProfile_ == profile) 557 if (lastProfile_ == profile && !lastProfileWasDeleted)
509 return; 558 return;
510 559
511 // Before tearing down the menu controller bridges, return the Cocoa menus to 560 // Before tearing down the menu controller bridges, return the Cocoa menus to
512 // their initial state. 561 // their initial state.
513 if (bookmarkMenuBridge_.get()) 562 if (bookmarkMenuBridge_.get())
514 bookmarkMenuBridge_->ResetMenu(); 563 bookmarkMenuBridge_->ResetMenu();
515 if (historyMenuBridge_.get()) 564 if (historyMenuBridge_.get())
516 historyMenuBridge_->ResetMenu(); 565 historyMenuBridge_->ResetMenu();
517 566
518 // Rebuild the menus with the new profile. 567 // Rebuild the menus with the new profile.
519 lastProfile_ = profile; 568 lastProfile_ = profile;
520 569
570 if (lastProfileWasDeleted)
571 lastProfileWasDeleted = NO;
Roger Tawa OOO till Jul 10th 2013/05/14 14:38:40 Is there a ctor where this is initialized?
noms (inactive) 2013/05/14 14:49:38 There is no constructor per se, but this is initia
572
521 bookmarkMenuBridge_.reset(new BookmarkMenuBridge(lastProfile_, 573 bookmarkMenuBridge_.reset(new BookmarkMenuBridge(lastProfile_,
522 [[[NSApp mainMenu] itemWithTag:IDC_BOOKMARKS_MENU] submenu])); 574 [[[NSApp mainMenu] itemWithTag:IDC_BOOKMARKS_MENU] submenu]));
523 // No need to |BuildMenu| here. It is done lazily upon menu access. 575 // No need to |BuildMenu| here. It is done lazily upon menu access.
524 576
525 historyMenuBridge_.reset(new HistoryMenuBridge(lastProfile_)); 577 historyMenuBridge_.reset(new HistoryMenuBridge(lastProfile_));
526 historyMenuBridge_->BuildMenu(); 578 historyMenuBridge_->BuildMenu();
527 579
528 chrome::BrowserCommandController:: 580 chrome::BrowserCommandController::
529 UpdateSharedCommandsForIncognitoAvailability( 581 UpdateSharedCommandsForIncognitoAvailability(
530 menuState_.get(), lastProfile_); 582 menuState_.get(), lastProfile_);
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
578 // Build up the encoding menu, the order of the items differs based on the 630 // Build up the encoding menu, the order of the items differs based on the
579 // current locale (see http://crbug.com/7647 for details). 631 // current locale (see http://crbug.com/7647 for details).
580 // We need a valid g_browser_process to get the profile which is why we can't 632 // We need a valid g_browser_process to get the profile which is why we can't
581 // call this from awakeFromNib. 633 // call this from awakeFromNib.
582 NSMenu* viewMenu = [[[NSApp mainMenu] itemWithTag:IDC_VIEW_MENU] submenu]; 634 NSMenu* viewMenu = [[[NSApp mainMenu] itemWithTag:IDC_VIEW_MENU] submenu];
583 NSMenuItem* encodingMenuItem = [viewMenu itemWithTag:IDC_ENCODING_MENU]; 635 NSMenuItem* encodingMenuItem = [viewMenu itemWithTag:IDC_ENCODING_MENU];
584 NSMenu* encodingMenu = [encodingMenuItem submenu]; 636 NSMenu* encodingMenu = [encodingMenuItem submenu];
585 EncodingMenuControllerDelegate::BuildEncodingMenu([self lastProfile], 637 EncodingMenuControllerDelegate::BuildEncodingMenu([self lastProfile],
586 encodingMenu); 638 encodingMenu);
587 639
640 // Instantiate the ProfileInfoCache observer so that we can get
641 // notified when a profile is deleted.
642 lastProfileWasDeleted = NO;
643 profileInfoCacheObserver_.reset(new AppControllerProfileObserver(
644 g_browser_process->profile_manager(), self));
645
588 // Since Chrome is localized to more languages than the OS, tell Cocoa which 646 // Since Chrome is localized to more languages than the OS, tell Cocoa which
589 // menu is the Help so it can add the search item to it. 647 // menu is the Help so it can add the search item to it.
590 [NSApp setHelpMenu:helpMenu_]; 648 [NSApp setHelpMenu:helpMenu_];
591 649
592 // Record the path to the (browser) app bundle; this is used by the app mode 650 // Record the path to the (browser) app bundle; this is used by the app mode
593 // shim. 651 // shim.
594 RecordLastRunAppBundlePath(); 652 RecordLastRunAppBundlePath();
595 653
596 // Makes "Services" menu items available. 654 // Makes "Services" menu items available.
597 [self registerServicesMenuTypesTo:[notify object]]; 655 [self registerServicesMenuTypesTo:[notify object]];
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after
707 765
708 // Called to determine if we should enable the "restore tab" menu item. 766 // Called to determine if we should enable the "restore tab" menu item.
709 // Checks with the TabRestoreService to see if there's anything there to 767 // Checks with the TabRestoreService to see if there's anything there to
710 // restore and returns YES if so. 768 // restore and returns YES if so.
711 - (BOOL)canRestoreTab { 769 - (BOOL)canRestoreTab {
712 TabRestoreService* service = 770 TabRestoreService* service =
713 TabRestoreServiceFactory::GetForProfile([self lastProfile]); 771 TabRestoreServiceFactory::GetForProfile([self lastProfile]);
714 return service && !service->entries().empty(); 772 return service && !service->entries().empty();
715 } 773 }
716 774
775 // Called from the AppControllerProfileObserver every time a profile is deleted.
776 - (void)profileWasRemoved:(const string16&)profileName {
777 Profile* lastProfile = [self lastProfile];
778 string16 lastProfileName = UTF8ToUTF16(lastProfile->GetPrefs()->GetString(
779 prefs::kProfileName));
780
781 // If the last profile has been deleted, we must NULL out our pointer
782 // otherwise we will try to start up a browser window with a pointer
783 // to the old profile.
784 if (profileName == lastProfileName) {
785 lastProfileWasDeleted = YES;
786 lastProfile_ = NULL;
787 }
788 }
789
717 // Returns true if there is a modal window (either window- or application- 790 // Returns true if there is a modal window (either window- or application-
718 // modal) blocking the active browser. Note that tab modal dialogs (HTTP auth 791 // modal) blocking the active browser. Note that tab modal dialogs (HTTP auth
719 // sheets) will not count as blocking the browser. But things like open/save 792 // sheets) will not count as blocking the browser. But things like open/save
720 // dialogs that are window modal will block the browser. 793 // dialogs that are window modal will block the browser.
721 - (BOOL)keyWindowIsModal { 794 - (BOOL)keyWindowIsModal {
722 if ([NSApp modalWindow]) 795 if ([NSApp modalWindow])
723 return YES; 796 return YES;
724 797
725 Browser* browser = chrome::GetLastActiveBrowser(); 798 Browser* browser = chrome::GetLastActiveBrowser();
726 return browser && 799 return browser &&
(...skipping 357 matching lines...) Expand 10 before | Expand all | Expand 10 after
1084 // handles requests from services. 1157 // handles requests from services.
1085 NSArray* types = [NSArray arrayWithObjects:NSStringPboardType, nil]; 1158 NSArray* types = [NSArray arrayWithObjects:NSStringPboardType, nil];
1086 [app registerServicesMenuSendTypes:types returnTypes:types]; 1159 [app registerServicesMenuSendTypes:types returnTypes:types];
1087 } 1160 }
1088 1161
1089 - (Profile*)lastProfile { 1162 - (Profile*)lastProfile {
1090 // Return the profile of the last-used BrowserWindowController, if available. 1163 // Return the profile of the last-used BrowserWindowController, if available.
1091 if (lastProfile_) 1164 if (lastProfile_)
1092 return lastProfile_; 1165 return lastProfile_;
1093 1166
1167 if (!g_browser_process->profile_manager())
1168 return NULL;
1169
1094 // On first launch, no profile will be stored, so use last from Local State. 1170 // On first launch, no profile will be stored, so use last from Local State.
1095 if (g_browser_process->profile_manager()) 1171 Profile* lastUsedProfile =
1096 return g_browser_process->profile_manager()->GetLastUsedProfile(); 1172 g_browser_process->profile_manager()->GetLastUsedProfile();
1097 1173
1098 return NULL; 1174 // If the last profile was deleted, we've already loaded the next active one,
1175 // so we should update the pointer; otherwise we will try to open the old one.
1176 // We unset lastProfileWasDeleted when the window is actually loaded, so that
1177 // all the profile preferences/menus are re-created.
1178 if (lastProfileWasDeleted) {
1179 lastProfile_ = lastUsedProfile;
1180 }
Roger Tawa OOO till Jul 10th 2013/05/14 14:38:40 Don't need { and }.
noms (inactive) 2013/05/14 15:06:48 Done.
1181
1182 return lastUsedProfile;
1099 } 1183 }
1100 1184
1101 // Various methods to open URLs that we get in a native fashion. We use 1185 // Various methods to open URLs that we get in a native fashion. We use
1102 // StartupBrowserCreator here because on the other platforms, URLs to open come 1186 // StartupBrowserCreator here because on the other platforms, URLs to open come
1103 // through the ProcessSingleton, and it calls StartupBrowserCreator. It's best 1187 // through the ProcessSingleton, and it calls StartupBrowserCreator. It's best
1104 // to bottleneck the openings through that for uniform handling. 1188 // to bottleneck the openings through that for uniform handling.
1105 1189
1106 - (void)openUrls:(const std::vector<GURL>&)urls { 1190 - (void)openUrls:(const std::vector<GURL>&)urls {
1107 // If the browser hasn't started yet, just queue up the URLs. 1191 // If the browser hasn't started yet, just queue up the URLs.
1108 if (!startupComplete_) { 1192 if (!startupComplete_) {
(...skipping 207 matching lines...) Expand 10 before | Expand all | Expand 10 after
1316 1400
1317 //--------------------------------------------------------------------------- 1401 //---------------------------------------------------------------------------
1318 1402
1319 namespace app_controller_mac { 1403 namespace app_controller_mac {
1320 1404
1321 bool IsOpeningNewWindow() { 1405 bool IsOpeningNewWindow() {
1322 return g_is_opening_new_window; 1406 return g_is_opening_new_window;
1323 } 1407 }
1324 1408
1325 } // namespace app_controller_mac 1409 } // namespace app_controller_mac
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698