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

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

Issue 11906008: Make Mac menu code obey incognito availability. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: adjust comment Created 7 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
« no previous file with comments | « chrome/browser/app_controller_mac.h ('k') | chrome/browser/ui/browser_command_controller.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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/file_path.h" 10 #include "base/file_path.h"
(...skipping 20 matching lines...) Expand all
31 #include "chrome/browser/profiles/profile_manager.h" 31 #include "chrome/browser/profiles/profile_manager.h"
32 #include "chrome/browser/service/service_process_control.h" 32 #include "chrome/browser/service/service_process_control.h"
33 #include "chrome/browser/sessions/session_restore.h" 33 #include "chrome/browser/sessions/session_restore.h"
34 #include "chrome/browser/sessions/session_service.h" 34 #include "chrome/browser/sessions/session_service.h"
35 #include "chrome/browser/sessions/session_service_factory.h" 35 #include "chrome/browser/sessions/session_service_factory.h"
36 #include "chrome/browser/sessions/tab_restore_service.h" 36 #include "chrome/browser/sessions/tab_restore_service.h"
37 #include "chrome/browser/sessions/tab_restore_service_factory.h" 37 #include "chrome/browser/sessions/tab_restore_service_factory.h"
38 #include "chrome/browser/sync/profile_sync_service.h" 38 #include "chrome/browser/sync/profile_sync_service.h"
39 #include "chrome/browser/sync/sync_ui_util.h" 39 #include "chrome/browser/sync/sync_ui_util.h"
40 #include "chrome/browser/ui/browser.h" 40 #include "chrome/browser/ui/browser.h"
41 #include "chrome/browser/ui/browser_command_controller.h"
41 #include "chrome/browser/ui/browser_commands.h" 42 #include "chrome/browser/ui/browser_commands.h"
42 #include "chrome/browser/ui/browser_finder.h" 43 #include "chrome/browser/ui/browser_finder.h"
43 #include "chrome/browser/ui/browser_list.h" 44 #include "chrome/browser/ui/browser_list.h"
44 #include "chrome/browser/ui/browser_mac.h" 45 #include "chrome/browser/ui/browser_mac.h"
45 #include "chrome/browser/ui/browser_window.h" 46 #include "chrome/browser/ui/browser_window.h"
46 #include "chrome/browser/ui/chrome_pages.h" 47 #include "chrome/browser/ui/chrome_pages.h"
47 #import "chrome/browser/ui/cocoa/bookmarks/bookmark_menu_bridge.h" 48 #import "chrome/browser/ui/cocoa/bookmarks/bookmark_menu_bridge.h"
48 #import "chrome/browser/ui/cocoa/browser_window_cocoa.h" 49 #import "chrome/browser/ui/cocoa/browser_window_cocoa.h"
49 #import "chrome/browser/ui/cocoa/browser_window_controller.h" 50 #import "chrome/browser/ui/cocoa/browser_window_controller.h"
50 #import "chrome/browser/ui/cocoa/confirm_quit.h" 51 #import "chrome/browser/ui/cocoa/confirm_quit.h"
(...skipping 464 matching lines...) Expand 10 before | Expand all | Expand 10 after
515 516
516 // Rebuild the menus with the new profile. 517 // Rebuild the menus with the new profile.
517 lastProfile_ = profile; 518 lastProfile_ = profile;
518 519
519 bookmarkMenuBridge_.reset(new BookmarkMenuBridge(lastProfile_, 520 bookmarkMenuBridge_.reset(new BookmarkMenuBridge(lastProfile_,
520 [[[NSApp mainMenu] itemWithTag:IDC_BOOKMARKS_MENU] submenu])); 521 [[[NSApp mainMenu] itemWithTag:IDC_BOOKMARKS_MENU] submenu]));
521 // No need to |BuildMenu| here. It is done lazily upon menu access. 522 // No need to |BuildMenu| here. It is done lazily upon menu access.
522 523
523 historyMenuBridge_.reset(new HistoryMenuBridge(lastProfile_)); 524 historyMenuBridge_.reset(new HistoryMenuBridge(lastProfile_));
524 historyMenuBridge_->BuildMenu(); 525 historyMenuBridge_->BuildMenu();
526
527 chrome::BrowserCommandController::
528 UpdateSharedCommandsForIncognitoAvailability(
529 menuState_.get(), lastProfile_);
530 profilePrefRegistrar_.reset(new PrefChangeRegistrar());
531 profilePrefRegistrar_->Init(lastProfile_->GetPrefs());
532 profilePrefRegistrar_->Add(
533 prefs::kIncognitoModeAvailability,
534 base::Bind(&chrome::BrowserCommandController::
535 UpdateSharedCommandsForIncognitoAvailability,
536 menuState_.get(),
537 lastProfile_));
525 } 538 }
526 539
527 - (void)checkForAnyKeyWindows { 540 - (void)checkForAnyKeyWindows {
528 if ([NSApp keyWindow]) 541 if ([NSApp keyWindow])
529 return; 542 return;
530 543
531 content::NotificationService::current()->Notify( 544 content::NotificationService::current()->Notify(
532 chrome::NOTIFICATION_NO_KEY_WINDOW, 545 chrome::NOTIFICATION_NO_KEY_WINDOW,
533 content::NotificationService::AllSources(), 546 content::NotificationService::AllSources(),
534 content::NotificationService::NoDetails()); 547 content::NotificationService::NoDetails());
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
591 // (probably-empty) list of URLs from the command line. 604 // (probably-empty) list of URLs from the command line.
592 if (startupUrls_.size()) { 605 if (startupUrls_.size()) {
593 [self openUrls:startupUrls_]; 606 [self openUrls:startupUrls_];
594 [self clearStartupUrls]; 607 [self clearStartupUrls];
595 } 608 }
596 609
597 const CommandLine& parsed_command_line = *CommandLine::ForCurrentProcess(); 610 const CommandLine& parsed_command_line = *CommandLine::ForCurrentProcess();
598 if (!parsed_command_line.HasSwitch(switches::kEnableExposeForTabs)) { 611 if (!parsed_command_line.HasSwitch(switches::kEnableExposeForTabs)) {
599 [tabposeMenuItem_ setHidden:YES]; 612 [tabposeMenuItem_ setHidden:YES];
600 } 613 }
614
615 PrefService* localState = g_browser_process->local_state();
616 if (localState) {
617 localPrefRegistrar_.Init(localState);
618 localPrefRegistrar_.Add(
619 prefs::kAllowFileSelectionDialogs,
620 base::Bind(&chrome::BrowserCommandController::UpdateOpenFileState,
621 menuState_.get()));
622 }
601 } 623 }
602 624
603 // This is called after profiles have been loaded and preferences registered. 625 // This is called after profiles have been loaded and preferences registered.
604 // It is safe to access the default profile here. 626 // It is safe to access the default profile here.
605 - (void)applicationDidBecomeActive:(NSNotification*)notify { 627 - (void)applicationDidBecomeActive:(NSNotification*)notify {
606 content::PluginService::GetInstance()->AppActivated(); 628 content::PluginService::GetInstance()->AppActivated();
607 } 629 }
608 630
609 // Helper function for populating and displaying the in progress downloads at 631 // Helper function for populating and displaying the in progress downloads at
610 // exit alert panel. 632 // exit alert panel.
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
685 707
686 // Called to determine if we should enable the "restore tab" menu item. 708 // Called to determine if we should enable the "restore tab" menu item.
687 // Checks with the TabRestoreService to see if there's anything there to 709 // Checks with the TabRestoreService to see if there's anything there to
688 // restore and returns YES if so. 710 // restore and returns YES if so.
689 - (BOOL)canRestoreTab { 711 - (BOOL)canRestoreTab {
690 TabRestoreService* service = 712 TabRestoreService* service =
691 TabRestoreServiceFactory::GetForProfile([self lastProfile]); 713 TabRestoreServiceFactory::GetForProfile([self lastProfile]);
692 return service && !service->entries().empty(); 714 return service && !service->entries().empty();
693 } 715 }
694 716
695 // Returns true if there is not a modal window (either window- or application- 717 // Returns true if there is a modal window (either window- or application-
696 // modal) blocking the active browser. Note that tab modal dialogs (HTTP auth 718 // modal) blocking the active browser. Note that tab modal dialogs (HTTP auth
697 // sheets) will not count as blocking the browser. But things like open/save 719 // sheets) will not count as blocking the browser. But things like open/save
698 // dialogs that are window modal will block the browser. 720 // dialogs that are window modal will block the browser.
699 - (BOOL)keyWindowIsNotModal { 721 - (BOOL)keyWindowIsModal {
722 if ([NSApp modalWindow])
723 return YES;
724
700 Browser* browser = chrome::GetLastActiveBrowser(); 725 Browser* browser = chrome::GetLastActiveBrowser();
701 return [NSApp modalWindow] == nil && (!browser || 726 return browser &&
702 ![[browser->window()->GetNativeWindow() attachedSheet] 727 [[browser->window()->GetNativeWindow() attachedSheet]
703 isKindOfClass:[NSWindow class]]); 728 isKindOfClass:[NSWindow class]];
704 } 729 }
705 730
706 // Called to validate menu items when there are no key windows. All the 731 // Called to validate menu items when there are no key windows. All the
707 // items we care about have been set with the |commandDispatch:| action and 732 // items we care about have been set with the |commandDispatch:| action and
708 // a target of FirstResponder in IB. If it's not one of those, let it 733 // a target of FirstResponder in IB. If it's not one of those, let it
709 // continue up the responder chain to be handled elsewhere. We pull out the 734 // continue up the responder chain to be handled elsewhere. We pull out the
710 // tag as the cross-platform constant to differentiate and dispatch the 735 // tag as the cross-platform constant to differentiate and dispatch the
711 // various commands. 736 // various commands.
712 - (BOOL)validateUserInterfaceItem:(id<NSValidatedUserInterfaceItem>)item { 737 - (BOOL)validateUserInterfaceItem:(id<NSValidatedUserInterfaceItem>)item {
713 SEL action = [item action]; 738 SEL action = [item action];
714 BOOL enable = NO; 739 BOOL enable = NO;
715 if (action == @selector(commandDispatch:)) { 740 if (action == @selector(commandDispatch:) ||
741 action == @selector(commandFromDock:)) {
716 NSInteger tag = [item tag]; 742 NSInteger tag = [item tag];
717 if (menuState_->SupportsCommand(tag)) { 743 if (menuState_ && // NULL in tests.
744 menuState_->SupportsCommand(tag)) {
718 switch (tag) { 745 switch (tag) {
719 // The File Menu commands are not automatically disabled by Cocoa when a 746 // The File Menu commands are not automatically disabled by Cocoa when a
720 // dialog sheet obscures the browser window, so we disable several of 747 // dialog sheet obscures the browser window, so we disable several of
721 // them here. We don't need to include IDC_CLOSE_WINDOW, because 748 // them here. We don't need to include IDC_CLOSE_WINDOW, because
722 // app_controller is only activated when there are no key windows (see 749 // app_controller is only activated when there are no key windows (see
723 // function comment). 750 // function comment).
724 case IDC_RESTORE_TAB: 751 case IDC_RESTORE_TAB:
725 enable = [self keyWindowIsNotModal] && [self canRestoreTab]; 752 enable = ![self keyWindowIsModal] && [self canRestoreTab];
726 break; 753 break;
727 // Browser-level items that open in new tabs should not open if there's 754 // Browser-level items that open in new tabs should not open if there's
728 // a window- or app-modal dialog. 755 // a window- or app-modal dialog.
729 case IDC_OPEN_FILE: 756 case IDC_OPEN_FILE:
730 case IDC_NEW_TAB: 757 case IDC_NEW_TAB:
731 case IDC_SHOW_HISTORY: 758 case IDC_SHOW_HISTORY:
732 case IDC_SHOW_BOOKMARK_MANAGER: 759 case IDC_SHOW_BOOKMARK_MANAGER:
733 enable = [self keyWindowIsNotModal]; 760 enable = ![self keyWindowIsModal];
734 break; 761 break;
735 // Browser-level items that open in new windows. 762 // Browser-level items that open in new windows.
736 case IDC_NEW_WINDOW:
737 case IDC_TASK_MANAGER: 763 case IDC_TASK_MANAGER:
738 // Allow the user to open a new window if there's a window-modal 764 // Allow the user to open a new window if there's a window-modal
739 // dialog. 765 // dialog.
740 enable = [self keyWindowIsNotModal] || ([NSApp modalWindow] == nil); 766 enable = ![self keyWindowIsModal];
741 break; 767 break;
742 case IDC_SHOW_SYNC_SETUP: { 768 case IDC_SHOW_SYNC_SETUP: {
743 Profile* lastProfile = [self lastProfile]; 769 Profile* lastProfile = [self lastProfile];
744 // The profile may be NULL during shutdown -- see 770 // The profile may be NULL during shutdown -- see
745 // http://code.google.com/p/chromium/issues/detail?id=43048 . 771 // http://code.google.com/p/chromium/issues/detail?id=43048 .
746 // 772 //
747 // TODO(akalin,viettrungluu): Figure out whether this method 773 // TODO(akalin,viettrungluu): Figure out whether this method
748 // can be prevented from being called if lastProfile is 774 // can be prevented from being called if lastProfile is
749 // NULL. 775 // NULL.
750 if (!lastProfile) { 776 if (!lastProfile) {
751 LOG(WARNING) 777 LOG(WARNING)
752 << "NULL lastProfile detected -- not doing anything"; 778 << "NULL lastProfile detected -- not doing anything";
753 break; 779 break;
754 } 780 }
755 enable = lastProfile->IsSyncAccessible() && 781 enable = lastProfile->IsSyncAccessible() &&
756 [self keyWindowIsNotModal]; 782 ![self keyWindowIsModal];
757 [BrowserWindowController updateSigninItem:item 783 [BrowserWindowController updateSigninItem:item
758 shouldShow:enable 784 shouldShow:enable
759 currentProfile:lastProfile]; 785 currentProfile:lastProfile];
760 break; 786 break;
761 } 787 }
762 case IDC_FEEDBACK: 788 case IDC_FEEDBACK:
763 enable = NO; 789 enable = NO;
764 break; 790 break;
765 default: 791 default:
766 enable = menuState_->IsCommandEnabled(tag) ? 792 enable = menuState_->IsCommandEnabled(tag) ?
767 [self keyWindowIsNotModal] : NO; 793 ![self keyWindowIsModal] : NO;
768 } 794 }
769 } 795 }
770 } else if (action == @selector(terminate:)) { 796 } else if (action == @selector(terminate:)) {
771 enable = YES; 797 enable = YES;
772 } else if (action == @selector(showPreferences:)) { 798 } else if (action == @selector(showPreferences:)) {
773 enable = YES; 799 enable = YES;
774 } else if (action == @selector(orderFrontStandardAboutPanel:)) { 800 } else if (action == @selector(orderFrontStandardAboutPanel:)) {
775 enable = YES; 801 enable = YES;
776 } else if (action == @selector(commandFromDock:)) { 802 } else if (action == @selector(commandFromDock:)) {
777 enable = YES; 803 enable = YES;
(...skipping 450 matching lines...) Expand 10 before | Expand all | Expand 10 after
1228 if (profilesAdded) 1254 if (profilesAdded)
1229 [dockMenu addItem:[NSMenuItem separatorItem]]; 1255 [dockMenu addItem:[NSMenuItem separatorItem]];
1230 1256
1231 NSString* titleStr = l10n_util::GetNSStringWithFixup(IDS_NEW_WINDOW_MAC); 1257 NSString* titleStr = l10n_util::GetNSStringWithFixup(IDS_NEW_WINDOW_MAC);
1232 scoped_nsobject<NSMenuItem> item( 1258 scoped_nsobject<NSMenuItem> item(
1233 [[NSMenuItem alloc] initWithTitle:titleStr 1259 [[NSMenuItem alloc] initWithTitle:titleStr
1234 action:@selector(commandFromDock:) 1260 action:@selector(commandFromDock:)
1235 keyEquivalent:@""]); 1261 keyEquivalent:@""]);
1236 [item setTarget:self]; 1262 [item setTarget:self];
1237 [item setTag:IDC_NEW_WINDOW]; 1263 [item setTag:IDC_NEW_WINDOW];
1264 [item setEnabled:[self validateUserInterfaceItem:item]];
1238 [dockMenu addItem:item]; 1265 [dockMenu addItem:item];
1239 1266
1240 titleStr = l10n_util::GetNSStringWithFixup(IDS_NEW_INCOGNITO_WINDOW_MAC); 1267 titleStr = l10n_util::GetNSStringWithFixup(IDS_NEW_INCOGNITO_WINDOW_MAC);
1241 item.reset( 1268 item.reset(
1242 [[NSMenuItem alloc] initWithTitle:titleStr 1269 [[NSMenuItem alloc] initWithTitle:titleStr
1243 action:@selector(commandFromDock:) 1270 action:@selector(commandFromDock:)
1244 keyEquivalent:@""]); 1271 keyEquivalent:@""]);
1245 [item setTarget:self]; 1272 [item setTarget:self];
1246 [item setTag:IDC_NEW_INCOGNITO_WINDOW]; 1273 [item setTag:IDC_NEW_INCOGNITO_WINDOW];
1274 [item setEnabled:[self validateUserInterfaceItem:item]];
1247 [dockMenu addItem:item]; 1275 [dockMenu addItem:item];
1248 1276
1249 // TODO(rickcam): Mock out BackgroundApplicationListModel, then add unit 1277 // TODO(rickcam): Mock out BackgroundApplicationListModel, then add unit
1250 // tests which use the mock in place of the profile-initialized model. 1278 // tests which use the mock in place of the profile-initialized model.
1251 1279
1252 // Avoid breaking unit tests which have no profile. 1280 // Avoid breaking unit tests which have no profile.
1253 if (profile) { 1281 if (profile) {
1254 BackgroundApplicationListModel applications(profile); 1282 BackgroundApplicationListModel applications(profile);
1255 if (applications.size()) { 1283 if (applications.size()) {
1256 int position = 0; 1284 int position = 0;
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after
1322 1350
1323 //--------------------------------------------------------------------------- 1351 //---------------------------------------------------------------------------
1324 1352
1325 namespace app_controller_mac { 1353 namespace app_controller_mac {
1326 1354
1327 bool IsOpeningNewWindow() { 1355 bool IsOpeningNewWindow() {
1328 return g_is_opening_new_window; 1356 return g_is_opening_new_window;
1329 } 1357 }
1330 1358
1331 } // namespace app_controller_mac 1359 } // namespace app_controller_mac
OLDNEW
« no previous file with comments | « chrome/browser/app_controller_mac.h ('k') | chrome/browser/ui/browser_command_controller.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698