OLD | NEW |
---|---|
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 Loading... | |
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 Loading... | |
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_); | |
204 DCHECK(app_controller_); | |
205 profile_manager_->GetProfileInfoCache().AddObserver(this); | |
206 } | |
207 | |
208 virtual ~AppControllerProfileObserver() { | |
209 DCHECK(profile_manager_); | |
210 profile_manager_->GetProfileInfoCache().RemoveObserver(this); | |
211 } | |
212 | |
213 private: | |
214 // ProfileInfoCacheObserver implementation: | |
215 | |
216 virtual void OnProfileAdded(const base::FilePath& profile_path) OVERRIDE { | |
217 } | |
218 | |
219 virtual void OnProfileWasRemoved(const base::FilePath& profile_path, | |
220 const string16& profile_name) OVERRIDE { | |
221 // When a profile is deleted we need to notify the AppController, | |
222 // so that it can correctly update its pointer to the last used profile. | |
223 [app_controller_ profileWasRemoved:profile_name]; | |
224 } | |
225 | |
226 virtual void OnProfileWillBeRemoved( | |
227 const base::FilePath& profile_path) OVERRIDE { | |
228 } | |
229 | |
230 virtual void OnProfileNameChanged(const base::FilePath& profile_path, | |
231 const string16& old_profile_name) OVERRIDE { | |
232 } | |
233 | |
234 virtual void OnProfileAvatarChanged( | |
235 const base::FilePath& profile_path) OVERRIDE { | |
236 } | |
237 | |
238 ProfileManager* profile_manager_; | |
239 | |
240 AppController* app_controller_; // Weak; owns us. | |
241 | |
242 DISALLOW_COPY_AND_ASSIGN(AppControllerProfileObserver); | |
243 }; | |
244 | |
195 @implementation AppController | 245 @implementation AppController |
196 | 246 |
197 @synthesize startupComplete = startupComplete_; | 247 @synthesize startupComplete = startupComplete_; |
198 | 248 |
199 // This method is called very early in application startup (ie, before | 249 // This method is called very early in application startup (ie, before |
200 // the profile is loaded or any preferences have been registered). Defer any | 250 // the profile is loaded or any preferences have been registered). Defer any |
201 // user-data initialization until -applicationDidFinishLaunching:. | 251 // user-data initialization until -applicationDidFinishLaunching:. |
202 - (void)awakeFromNib { | 252 - (void)awakeFromNib { |
203 // We need to register the handlers early to catch events fired on launch. | 253 // We need to register the handlers early to catch events fired on launch. |
204 NSAppleEventManager* em = [NSAppleEventManager sharedAppleEventManager]; | 254 NSAppleEventManager* em = [NSAppleEventManager sharedAppleEventManager]; |
(...skipping 293 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
498 - (void)popoverDidClose:(NSNotification*)notify { | 548 - (void)popoverDidClose:(NSNotification*)notify { |
499 hasPopover_ = NO; | 549 hasPopover_ = NO; |
500 [self fixCloseMenuItemKeyEquivalents]; | 550 [self fixCloseMenuItemKeyEquivalents]; |
501 } | 551 } |
502 | 552 |
503 // Called when the user has changed browser windows, meaning the backing profile | 553 // 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 | 554 // 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 | 555 // no-op if the new profile is the same as the current one. This will always be |
506 // the original profile and never incognito. | 556 // the original profile and never incognito. |
507 - (void)windowChangedToProfile:(Profile*)profile { | 557 - (void)windowChangedToProfile:(Profile*)profile { |
508 if (lastProfile_ == profile) | 558 if (lastProfile_ == profile && !lastProfileWasDeleted_) |
509 return; | 559 return; |
510 | 560 |
511 // Before tearing down the menu controller bridges, return the Cocoa menus to | 561 // Before tearing down the menu controller bridges, return the Cocoa menus to |
512 // their initial state. | 562 // their initial state. |
513 if (bookmarkMenuBridge_.get()) | 563 if (bookmarkMenuBridge_.get()) |
514 bookmarkMenuBridge_->ResetMenu(); | 564 bookmarkMenuBridge_->ResetMenu(); |
515 if (historyMenuBridge_.get()) | 565 if (historyMenuBridge_.get()) |
516 historyMenuBridge_->ResetMenu(); | 566 historyMenuBridge_->ResetMenu(); |
517 | 567 |
518 // Rebuild the menus with the new profile. | 568 // Rebuild the menus with the new profile. |
519 lastProfile_ = profile; | 569 lastProfile_ = profile; |
520 | 570 |
571 if (lastProfileWasDeleted_) | |
572 lastProfileWasDeleted_ = NO; | |
573 | |
521 bookmarkMenuBridge_.reset(new BookmarkMenuBridge(lastProfile_, | 574 bookmarkMenuBridge_.reset(new BookmarkMenuBridge(lastProfile_, |
522 [[[NSApp mainMenu] itemWithTag:IDC_BOOKMARKS_MENU] submenu])); | 575 [[[NSApp mainMenu] itemWithTag:IDC_BOOKMARKS_MENU] submenu])); |
523 // No need to |BuildMenu| here. It is done lazily upon menu access. | 576 // No need to |BuildMenu| here. It is done lazily upon menu access. |
524 | 577 |
525 historyMenuBridge_.reset(new HistoryMenuBridge(lastProfile_)); | 578 historyMenuBridge_.reset(new HistoryMenuBridge(lastProfile_)); |
526 historyMenuBridge_->BuildMenu(); | 579 historyMenuBridge_->BuildMenu(); |
527 | 580 |
528 chrome::BrowserCommandController:: | 581 chrome::BrowserCommandController:: |
529 UpdateSharedCommandsForIncognitoAvailability( | 582 UpdateSharedCommandsForIncognitoAvailability( |
530 menuState_.get(), lastProfile_); | 583 menuState_.get(), lastProfile_); |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
578 // Build up the encoding menu, the order of the items differs based on the | 631 // Build up the encoding menu, the order of the items differs based on the |
579 // current locale (see http://crbug.com/7647 for details). | 632 // 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 | 633 // We need a valid g_browser_process to get the profile which is why we can't |
581 // call this from awakeFromNib. | 634 // call this from awakeFromNib. |
582 NSMenu* viewMenu = [[[NSApp mainMenu] itemWithTag:IDC_VIEW_MENU] submenu]; | 635 NSMenu* viewMenu = [[[NSApp mainMenu] itemWithTag:IDC_VIEW_MENU] submenu]; |
583 NSMenuItem* encodingMenuItem = [viewMenu itemWithTag:IDC_ENCODING_MENU]; | 636 NSMenuItem* encodingMenuItem = [viewMenu itemWithTag:IDC_ENCODING_MENU]; |
584 NSMenu* encodingMenu = [encodingMenuItem submenu]; | 637 NSMenu* encodingMenu = [encodingMenuItem submenu]; |
585 EncodingMenuControllerDelegate::BuildEncodingMenu([self lastProfile], | 638 EncodingMenuControllerDelegate::BuildEncodingMenu([self lastProfile], |
586 encodingMenu); | 639 encodingMenu); |
587 | 640 |
641 // Instantiate the ProfileInfoCache observer so that we can get | |
642 // notified when a profile is deleted. | |
643 lastProfileWasDeleted_ = NO; | |
644 profileInfoCacheObserver_.reset(new AppControllerProfileObserver( | |
645 g_browser_process->profile_manager(), self)); | |
646 | |
588 // Since Chrome is localized to more languages than the OS, tell Cocoa which | 647 // 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. | 648 // menu is the Help so it can add the search item to it. |
590 [NSApp setHelpMenu:helpMenu_]; | 649 [NSApp setHelpMenu:helpMenu_]; |
591 | 650 |
592 // Record the path to the (browser) app bundle; this is used by the app mode | 651 // Record the path to the (browser) app bundle; this is used by the app mode |
593 // shim. | 652 // shim. |
594 RecordLastRunAppBundlePath(); | 653 RecordLastRunAppBundlePath(); |
595 | 654 |
596 // Makes "Services" menu items available. | 655 // Makes "Services" menu items available. |
597 [self registerServicesMenuTypesTo:[notify object]]; | 656 [self registerServicesMenuTypesTo:[notify object]]; |
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
707 | 766 |
708 // Called to determine if we should enable the "restore tab" menu item. | 767 // 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 | 768 // Checks with the TabRestoreService to see if there's anything there to |
710 // restore and returns YES if so. | 769 // restore and returns YES if so. |
711 - (BOOL)canRestoreTab { | 770 - (BOOL)canRestoreTab { |
712 TabRestoreService* service = | 771 TabRestoreService* service = |
713 TabRestoreServiceFactory::GetForProfile([self lastProfile]); | 772 TabRestoreServiceFactory::GetForProfile([self lastProfile]); |
714 return service && !service->entries().empty(); | 773 return service && !service->entries().empty(); |
715 } | 774 } |
716 | 775 |
776 // Called from the AppControllerProfileObserver every time a profile is deleted. | |
777 - (void)profileWasRemoved:(const string16&)profileName { | |
778 Profile* lastProfile = [self lastProfile]; | |
779 string16 lastProfileName = UTF8ToUTF16(lastProfile->GetPrefs()->GetString( | |
780 prefs::kProfileName)); | |
781 | |
782 // If the last profile has been deleted, we must NULL out our pointer | |
783 // otherwise we will try to start up a browser window with a pointer | |
784 // to the old profile. | |
785 if (profileName == lastProfileName) { | |
786 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
| |
787 lastProfile_ = NULL; | |
788 } | |
789 } | |
790 | |
717 // Returns true if there is a modal window (either window- or application- | 791 // 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 | 792 // 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 | 793 // sheets) will not count as blocking the browser. But things like open/save |
720 // dialogs that are window modal will block the browser. | 794 // dialogs that are window modal will block the browser. |
721 - (BOOL)keyWindowIsModal { | 795 - (BOOL)keyWindowIsModal { |
722 if ([NSApp modalWindow]) | 796 if ([NSApp modalWindow]) |
723 return YES; | 797 return YES; |
724 | 798 |
725 Browser* browser = chrome::GetLastActiveBrowser(); | 799 Browser* browser = chrome::GetLastActiveBrowser(); |
726 return browser && | 800 return browser && |
(...skipping 357 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1084 // handles requests from services. | 1158 // handles requests from services. |
1085 NSArray* types = [NSArray arrayWithObjects:NSStringPboardType, nil]; | 1159 NSArray* types = [NSArray arrayWithObjects:NSStringPboardType, nil]; |
1086 [app registerServicesMenuSendTypes:types returnTypes:types]; | 1160 [app registerServicesMenuSendTypes:types returnTypes:types]; |
1087 } | 1161 } |
1088 | 1162 |
1089 - (Profile*)lastProfile { | 1163 - (Profile*)lastProfile { |
1090 // Return the profile of the last-used BrowserWindowController, if available. | 1164 // Return the profile of the last-used BrowserWindowController, if available. |
1091 if (lastProfile_) | 1165 if (lastProfile_) |
1092 return lastProfile_; | 1166 return lastProfile_; |
1093 | 1167 |
1168 if (!g_browser_process->profile_manager()) | |
1169 return NULL; | |
1170 | |
1094 // On first launch, no profile will be stored, so use last from Local State. | 1171 // On first launch, no profile will be stored, so use last from Local State. |
1095 if (g_browser_process->profile_manager()) | 1172 Profile* lastUsedProfile = |
1096 return g_browser_process->profile_manager()->GetLastUsedProfile(); | 1173 g_browser_process->profile_manager()->GetLastUsedProfile(); |
1097 | 1174 |
1098 return NULL; | 1175 // If the last profile was deleted, we've already loaded the next active one, |
1176 // so we should update the pointer; otherwise we will try to open the old one. | |
1177 // We unset lastProfileWasDeleted_ when the window is actually loaded, so that | |
1178 // all the profile preferences/menus are re-created. | |
1179 if (lastProfileWasDeleted_) | |
1180 lastProfile_ = lastUsedProfile; | |
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 Loading... | |
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 |
OLD | NEW |