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

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: test fix 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
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 21 matching lines...) Expand all
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/sync/sync_ui_util_mac.h" 40 #include "chrome/browser/sync/sync_ui_util_mac.h"
41 #include "chrome/browser/ui/browser.h" 41 #include "chrome/browser/ui/browser.h"
42 #include "chrome/browser/ui/browser_command_controller_base.h"
42 #include "chrome/browser/ui/browser_commands.h" 43 #include "chrome/browser/ui/browser_commands.h"
43 #include "chrome/browser/ui/browser_finder.h" 44 #include "chrome/browser/ui/browser_finder.h"
44 #include "chrome/browser/ui/browser_list.h" 45 #include "chrome/browser/ui/browser_list.h"
45 #include "chrome/browser/ui/browser_mac.h" 46 #include "chrome/browser/ui/browser_mac.h"
46 #include "chrome/browser/ui/browser_window.h" 47 #include "chrome/browser/ui/browser_window.h"
47 #include "chrome/browser/ui/chrome_pages.h" 48 #include "chrome/browser/ui/chrome_pages.h"
48 #import "chrome/browser/ui/cocoa/bookmarks/bookmark_menu_bridge.h" 49 #import "chrome/browser/ui/cocoa/bookmarks/bookmark_menu_bridge.h"
49 #import "chrome/browser/ui/cocoa/browser_window_cocoa.h" 50 #import "chrome/browser/ui/cocoa/browser_window_cocoa.h"
50 #import "chrome/browser/ui/cocoa/browser_window_controller.h" 51 #import "chrome/browser/ui/cocoa/browser_window_controller.h"
51 #import "chrome/browser/ui/cocoa/confirm_quit.h" 52 #import "chrome/browser/ui/cocoa/confirm_quit.h"
(...skipping 463 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::BrowserCommandControllerBase::UpdateCommandsForIncognitoAvailability(
528 menuState_.get(), lastProfile_);
529 profilePrefRegistrar_.reset(new PrefChangeRegistrar());
530 profilePrefRegistrar_->Init(lastProfile_->GetPrefs());
531 profilePrefRegistrar_->Add(
532 prefs::kIncognitoModeAvailability,
533 base::Bind(
534 &chrome::BrowserCommandControllerBase::
535 UpdateCommandsForIncognitoAvailability,
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(
621 &chrome::BrowserCommandControllerBase::UpdateOpenFileState,
622 menuState_.get()));
623 }
601 } 624 }
602 625
603 // This is called after profiles have been loaded and preferences registered. 626 // This is called after profiles have been loaded and preferences registered.
604 // It is safe to access the default profile here. 627 // It is safe to access the default profile here.
605 - (void)applicationDidBecomeActive:(NSNotification*)notify { 628 - (void)applicationDidBecomeActive:(NSNotification*)notify {
606 content::PluginService::GetInstance()->AppActivated(); 629 content::PluginService::GetInstance()->AppActivated();
607 } 630 }
608 631
609 // Helper function for populating and displaying the in progress downloads at 632 // Helper function for populating and displaying the in progress downloads at
610 // exit alert panel. 633 // exit alert panel.
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
685 708
686 // Called to determine if we should enable the "restore tab" menu item. 709 // 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 710 // Checks with the TabRestoreService to see if there's anything there to
688 // restore and returns YES if so. 711 // restore and returns YES if so.
689 - (BOOL)canRestoreTab { 712 - (BOOL)canRestoreTab {
690 TabRestoreService* service = 713 TabRestoreService* service =
691 TabRestoreServiceFactory::GetForProfile([self lastProfile]); 714 TabRestoreServiceFactory::GetForProfile([self lastProfile]);
692 return service && !service->entries().empty(); 715 return service && !service->entries().empty();
693 } 716 }
694 717
695 // Returns true if there is not a modal window (either window- or application- 718 // 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 719 // 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 720 // sheets) will not count as blocking the browser. But things like open/save
698 // dialogs that are window modal will block the browser. 721 // dialogs that are window modal will block the browser.
699 - (BOOL)keyWindowIsNotModal { 722 - (BOOL)keyWindowIsModal {
723 if ([NSApp modalWindow])
724 return YES;
725
700 Browser* browser = chrome::GetLastActiveBrowser(); 726 Browser* browser = chrome::GetLastActiveBrowser();
701 return [NSApp modalWindow] == nil && (!browser || 727 return browser &&
702 ![[browser->window()->GetNativeWindow() attachedSheet] 728 [[browser->window()->GetNativeWindow() attachedSheet]
703 isKindOfClass:[NSWindow class]]); 729 isKindOfClass:[NSWindow class]];
704 } 730 }
705 731
706 // Called to validate menu items when there are no key windows. All the 732 // 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 733 // 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 734 // 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 735 // 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 736 // tag as the cross-platform constant to differentiate and dispatch the
711 // various commands. 737 // various commands.
712 - (BOOL)validateUserInterfaceItem:(id<NSValidatedUserInterfaceItem>)item { 738 - (BOOL)validateUserInterfaceItem:(id<NSValidatedUserInterfaceItem>)item {
713 SEL action = [item action]; 739 SEL action = [item action];
714 BOOL enable = NO; 740 BOOL enable = NO;
715 if (action == @selector(commandDispatch:)) { 741 if (action == @selector(commandDispatch:) ||
742 action == @selector(commandFromDock:)) {
716 NSInteger tag = [item tag]; 743 NSInteger tag = [item tag];
717 if (menuState_->SupportsCommand(tag)) { 744 if (menuState_ && // null in tests
Robert Sesek 2013/01/15 22:27:18 .
Avi (use Gerrit) 2013/01/15 22:35:06 Done.
745 menuState_->SupportsCommand(tag)) {
718 switch (tag) { 746 switch (tag) {
719 // The File Menu commands are not automatically disabled by Cocoa when a 747 // The File Menu commands are not automatically disabled by Cocoa when a
720 // dialog sheet obscures the browser window, so we disable several of 748 // dialog sheet obscures the browser window, so we disable several of
721 // them here. We don't need to include IDC_CLOSE_WINDOW, because 749 // 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 750 // app_controller is only activated when there are no key windows (see
723 // function comment). 751 // function comment).
724 case IDC_RESTORE_TAB: 752 case IDC_RESTORE_TAB:
725 enable = [self keyWindowIsNotModal] && [self canRestoreTab]; 753 enable = ![self keyWindowIsModal] && [self canRestoreTab];
726 break; 754 break;
727 // Browser-level items that open in new tabs should not open if there's 755 // Browser-level items that open in new tabs should not open if there's
728 // a window- or app-modal dialog. 756 // a window- or app-modal dialog.
729 case IDC_OPEN_FILE: 757 case IDC_OPEN_FILE:
730 case IDC_NEW_TAB: 758 case IDC_NEW_TAB:
731 case IDC_SHOW_HISTORY: 759 case IDC_SHOW_HISTORY:
732 case IDC_SHOW_BOOKMARK_MANAGER: 760 case IDC_SHOW_BOOKMARK_MANAGER:
733 enable = [self keyWindowIsNotModal]; 761 enable = ![self keyWindowIsModal];
734 break; 762 break;
735 // Browser-level items that open in new windows. 763 // Browser-level items that open in new windows.
736 case IDC_NEW_WINDOW:
737 case IDC_TASK_MANAGER: 764 case IDC_TASK_MANAGER:
738 // Allow the user to open a new window if there's a window-modal 765 // Allow the user to open a new window if there's a window-modal
739 // dialog. 766 // dialog.
740 enable = [self keyWindowIsNotModal] || ([NSApp modalWindow] == nil); 767 enable = ![self keyWindowIsModal];
741 break; 768 break;
742 case IDC_SHOW_SYNC_SETUP: { 769 case IDC_SHOW_SYNC_SETUP: {
743 Profile* lastProfile = [self lastProfile]; 770 Profile* lastProfile = [self lastProfile];
744 // The profile may be NULL during shutdown -- see 771 // The profile may be NULL during shutdown -- see
745 // http://code.google.com/p/chromium/issues/detail?id=43048 . 772 // http://code.google.com/p/chromium/issues/detail?id=43048 .
746 // 773 //
747 // TODO(akalin,viettrungluu): Figure out whether this method 774 // TODO(akalin,viettrungluu): Figure out whether this method
748 // can be prevented from being called if lastProfile is 775 // can be prevented from being called if lastProfile is
749 // NULL. 776 // NULL.
750 if (!lastProfile) { 777 if (!lastProfile) {
751 LOG(WARNING) 778 LOG(WARNING)
752 << "NULL lastProfile detected -- not doing anything"; 779 << "NULL lastProfile detected -- not doing anything";
753 break; 780 break;
754 } 781 }
755 enable = lastProfile->IsSyncAccessible() && 782 enable = lastProfile->IsSyncAccessible() && ![self keyWindowIsModal];
756 [self keyWindowIsNotModal];
757 sync_ui_util::UpdateSyncItem(item, enable, lastProfile); 783 sync_ui_util::UpdateSyncItem(item, enable, lastProfile);
758 break; 784 break;
759 } 785 }
760 case IDC_FEEDBACK: 786 case IDC_FEEDBACK:
761 enable = NO; 787 enable = NO;
762 break; 788 break;
763 default: 789 default:
764 enable = menuState_->IsCommandEnabled(tag) ? 790 enable = menuState_->IsCommandEnabled(tag) ?
765 [self keyWindowIsNotModal] : NO; 791 ![self keyWindowIsModal] : NO;
766 } 792 }
767 } 793 }
768 } else if (action == @selector(terminate:)) { 794 } else if (action == @selector(terminate:)) {
769 enable = YES; 795 enable = YES;
770 } else if (action == @selector(showPreferences:)) { 796 } else if (action == @selector(showPreferences:)) {
771 enable = YES; 797 enable = YES;
772 } else if (action == @selector(orderFrontStandardAboutPanel:)) { 798 } else if (action == @selector(orderFrontStandardAboutPanel:)) {
773 enable = YES; 799 enable = YES;
774 } else if (action == @selector(commandFromDock:)) { 800 } else if (action == @selector(commandFromDock:)) {
775 enable = YES; 801 enable = YES;
(...skipping 448 matching lines...) Expand 10 before | Expand all | Expand 10 after
1224 if (profilesAdded) 1250 if (profilesAdded)
1225 [dockMenu addItem:[NSMenuItem separatorItem]]; 1251 [dockMenu addItem:[NSMenuItem separatorItem]];
1226 1252
1227 NSString* titleStr = l10n_util::GetNSStringWithFixup(IDS_NEW_WINDOW_MAC); 1253 NSString* titleStr = l10n_util::GetNSStringWithFixup(IDS_NEW_WINDOW_MAC);
1228 scoped_nsobject<NSMenuItem> item( 1254 scoped_nsobject<NSMenuItem> item(
1229 [[NSMenuItem alloc] initWithTitle:titleStr 1255 [[NSMenuItem alloc] initWithTitle:titleStr
1230 action:@selector(commandFromDock:) 1256 action:@selector(commandFromDock:)
1231 keyEquivalent:@""]); 1257 keyEquivalent:@""]);
1232 [item setTarget:self]; 1258 [item setTarget:self];
1233 [item setTag:IDC_NEW_WINDOW]; 1259 [item setTag:IDC_NEW_WINDOW];
1260 [item setEnabled:[self validateUserInterfaceItem:item]];
1234 [dockMenu addItem:item]; 1261 [dockMenu addItem:item];
1235 1262
1236 titleStr = l10n_util::GetNSStringWithFixup(IDS_NEW_INCOGNITO_WINDOW_MAC); 1263 titleStr = l10n_util::GetNSStringWithFixup(IDS_NEW_INCOGNITO_WINDOW_MAC);
1237 item.reset( 1264 item.reset(
1238 [[NSMenuItem alloc] initWithTitle:titleStr 1265 [[NSMenuItem alloc] initWithTitle:titleStr
1239 action:@selector(commandFromDock:) 1266 action:@selector(commandFromDock:)
1240 keyEquivalent:@""]); 1267 keyEquivalent:@""]);
1241 [item setTarget:self]; 1268 [item setTarget:self];
1242 [item setTag:IDC_NEW_INCOGNITO_WINDOW]; 1269 [item setTag:IDC_NEW_INCOGNITO_WINDOW];
1270 [item setEnabled:[self validateUserInterfaceItem:item]];
1243 [dockMenu addItem:item]; 1271 [dockMenu addItem:item];
1244 1272
1245 // TODO(rickcam): Mock out BackgroundApplicationListModel, then add unit 1273 // TODO(rickcam): Mock out BackgroundApplicationListModel, then add unit
1246 // tests which use the mock in place of the profile-initialized model. 1274 // tests which use the mock in place of the profile-initialized model.
1247 1275
1248 // Avoid breaking unit tests which have no profile. 1276 // Avoid breaking unit tests which have no profile.
1249 if (profile) { 1277 if (profile) {
1250 BackgroundApplicationListModel applications(profile); 1278 BackgroundApplicationListModel applications(profile);
1251 if (applications.size()) { 1279 if (applications.size()) {
1252 int position = 0; 1280 int position = 0;
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after
1318 1346
1319 //--------------------------------------------------------------------------- 1347 //---------------------------------------------------------------------------
1320 1348
1321 namespace app_controller_mac { 1349 namespace app_controller_mac {
1322 1350
1323 bool IsOpeningNewWindow() { 1351 bool IsOpeningNewWindow() {
1324 return g_is_opening_new_window; 1352 return g_is_opening_new_window;
1325 } 1353 }
1326 1354
1327 } // namespace app_controller_mac 1355 } // namespace app_controller_mac
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698