[email protected] | 1bcdb53 | 2009-01-16 17:47:57 | [diff] [blame] | 1 | // Copyright (c) 2009 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | 2c47bc1 | 2009-04-10 20:14:00 | [diff] [blame] | 5 | #import "chrome/browser/app_controller_mac.h" |
[email protected] | 1bcdb53 | 2009-01-16 17:47:57 | [diff] [blame] | 6 | |
[email protected] | 288bfcd3 | 2009-09-14 18:14:46 | [diff] [blame] | 7 | #include "app/l10n_util_mac.h" |
[email protected] | 57750f82 | 2009-04-21 21:43:09 | [diff] [blame] | 8 | #include "base/command_line.h" |
[email protected] | 09729a55 | 2009-08-03 23:21:41 | [diff] [blame] | 9 | #include "base/mac_util.h" |
[email protected] | 2c47bc1 | 2009-04-10 20:14:00 | [diff] [blame] | 10 | #include "base/message_loop.h" |
[email protected] | 57750f82 | 2009-04-21 21:43:09 | [diff] [blame] | 11 | #include "base/sys_string_conversions.h" |
[email protected] | 2c47bc1 | 2009-04-10 20:14:00 | [diff] [blame] | 12 | #include "chrome/app/chrome_dll_resource.h" |
| 13 | #include "chrome/browser/browser.h" |
[email protected] | 57750f82 | 2009-04-21 21:43:09 | [diff] [blame] | 14 | #include "chrome/browser/browser_init.h" |
[email protected] | 2c47bc1 | 2009-04-10 20:14:00 | [diff] [blame] | 15 | #include "chrome/browser/browser_list.h" |
[email protected] | 5c23875 | 2009-06-13 10:29:07 | [diff] [blame] | 16 | #include "chrome/browser/browser_process.h" |
[email protected] | 41741a96 | 2009-02-18 21:51:39 | [diff] [blame] | 17 | #include "chrome/browser/browser_shutdown.h" |
[email protected] | ce560f8 | 2009-06-03 09:39:44 | [diff] [blame] | 18 | #include "chrome/browser/browser_window.h" |
[email protected] | bde3dda | 2009-05-20 22:13:07 | [diff] [blame] | 19 | #import "chrome/browser/cocoa/about_window_controller.h" |
[email protected] | 3f34599d | 2009-03-25 22:11:43 | [diff] [blame] | 20 | #import "chrome/browser/cocoa/bookmark_menu_bridge.h" |
[email protected] | a9e8afc | 2009-08-11 22:03:17 | [diff] [blame] | 21 | #import "chrome/browser/cocoa/browser_window_cocoa.h" |
| 22 | #import "chrome/browser/cocoa/browser_window_controller.h" |
[email protected] | 3896b18 | 2009-11-03 16:15:32 | [diff] [blame] | 23 | #import "chrome/browser/cocoa/bug_report_window_controller.h" |
[email protected] | 93e18176 | 2009-05-29 14:30:38 | [diff] [blame] | 24 | #import "chrome/browser/cocoa/clear_browsing_data_controller.h" |
[email protected] | 2bcec61 | 2009-05-14 17:50:53 | [diff] [blame] | 25 | #import "chrome/browser/cocoa/encoding_menu_controller_delegate_mac.h" |
[email protected] | 4aee565 | 2009-11-19 18:59:21 | [diff] [blame] | 26 | #import "chrome/browser/cocoa/history_menu_bridge.h" |
| 27 | #import "chrome/browser/cocoa/import_settings_dialog.h" |
[email protected] | 4240438 | 2009-04-30 17:59:24 | [diff] [blame] | 28 | #import "chrome/browser/cocoa/preferences_window_controller.h" |
[email protected] | 449dd2f | 2009-05-27 13:04:00 | [diff] [blame] | 29 | #import "chrome/browser/cocoa/tab_strip_controller.h" |
| 30 | #import "chrome/browser/cocoa/tab_window_controller.h" |
[email protected] | ceaaf181 | 2010-01-20 22:13:09 | [diff] [blame] | 31 | #include "chrome/browser/cocoa/task_manager_mac.h" |
[email protected] | 2c47bc1 | 2009-04-10 20:14:00 | [diff] [blame] | 32 | #include "chrome/browser/command_updater.h" |
[email protected] | 09729a55 | 2009-08-03 23:21:41 | [diff] [blame] | 33 | #include "chrome/browser/download/download_manager.h" |
[email protected] | 4aee565 | 2009-11-19 18:59:21 | [diff] [blame] | 34 | #include "chrome/browser/metrics/user_metrics.h" |
[email protected] | 60ad3e2 | 2009-09-18 21:07:19 | [diff] [blame] | 35 | #include "chrome/browser/options_window.h" |
[email protected] | fbc947b | 2009-06-19 13:28:24 | [diff] [blame] | 36 | #include "chrome/browser/sessions/tab_restore_service.h" |
[email protected] | f9bc9b9 | 2009-11-24 00:55:35 | [diff] [blame] | 37 | #include "chrome/browser/sync/profile_sync_service.h" |
[email protected] | 40f04797 | 2009-11-25 03:54:40 | [diff] [blame] | 38 | #include "chrome/browser/sync/sync_ui_util.h" |
| 39 | #include "chrome/browser/sync/sync_ui_util_mac.h" |
[email protected] | 60ad3e2 | 2009-09-18 21:07:19 | [diff] [blame] | 40 | #include "chrome/browser/tab_contents/tab_contents.h" |
[email protected] | f4acfae8 | 2009-09-11 00:33:38 | [diff] [blame] | 41 | #include "chrome/common/chrome_switches.h" |
[email protected] | a96ec6a | 2009-11-04 17:27:08 | [diff] [blame] | 42 | #include "chrome/common/notification_service.h" |
[email protected] | cd63ef6 | 2009-05-06 19:41:37 | [diff] [blame] | 43 | #include "chrome/common/pref_names.h" |
| 44 | #include "chrome/common/pref_service.h" |
[email protected] | 2c47bc1 | 2009-04-10 20:14:00 | [diff] [blame] | 45 | #include "chrome/browser/profile_manager.h" |
| 46 | #include "chrome/common/temp_scaffolding_stubs.h" |
[email protected] | 9dcb5966 | 2009-09-23 01:15:50 | [diff] [blame] | 47 | #include "grit/chromium_strings.h" |
[email protected] | 09729a55 | 2009-08-03 23:21:41 | [diff] [blame] | 48 | #include "grit/generated_resources.h" |
[email protected] | caadfca | 2010-01-22 21:51:21 | [diff] [blame^] | 49 | #include "net/base/net_util.h" |
[email protected] | 88d7494 | 2009-01-21 22:04:44 | [diff] [blame] | 50 | |
[email protected] | f2c20fa | 2009-12-01 17:42:02 | [diff] [blame] | 51 | // 10.6 adds a public API for the Spotlight-backed search menu item in the Help |
| 52 | // menu. Provide the declaration so it can be called below when building with |
| 53 | // the 10.5 SDK. |
| 54 | #if !defined(MAC_OS_X_VERSION_10_6) || \ |
| 55 | MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_6 |
| 56 | @interface NSApplication (SnowLeopardSDKDeclarations) |
[email protected] | caadfca | 2010-01-22 21:51:21 | [diff] [blame^] | 57 | - (void)setHelpMenu:(NSMenu*)helpMenu; |
[email protected] | f2c20fa | 2009-12-01 17:42:02 | [diff] [blame] | 58 | @end |
| 59 | #endif |
| 60 | |
[email protected] | 88d7494 | 2009-01-21 22:04:44 | [diff] [blame] | 61 | @interface AppController(PRIVATE) |
| 62 | - (void)initMenuState; |
[email protected] | 136140c | 2009-05-19 13:58:25 | [diff] [blame] | 63 | - (void)openURLs:(const std::vector<GURL>&)urls; |
[email protected] | 57750f82 | 2009-04-21 21:43:09 | [diff] [blame] | 64 | - (void)getUrl:(NSAppleEventDescriptor*)event |
| 65 | withReply:(NSAppleEventDescriptor*)reply; |
[email protected] | 449dd2f | 2009-05-27 13:04:00 | [diff] [blame] | 66 | - (void)windowLayeringDidChange:(NSNotification*)inNotification; |
[email protected] | 73f5b36 | 2009-08-10 23:58:21 | [diff] [blame] | 67 | - (BOOL)userWillWaitForInProgressDownloads:(int)downloadCount; |
[email protected] | 09729a55 | 2009-08-03 23:21:41 | [diff] [blame] | 68 | - (BOOL)shouldQuitWithInProgressDownloads; |
[email protected] | d989f07c | 2009-11-14 00:35:46 | [diff] [blame] | 69 | - (void)showPreferencesWindow:(id)sender |
| 70 | page:(OptionsPage)page |
| 71 | profile:(Profile*)profile; |
[email protected] | 88d7494 | 2009-01-21 22:04:44 | [diff] [blame] | 72 | @end |
[email protected] | 1bcdb53 | 2009-01-16 17:47:57 | [diff] [blame] | 73 | |
[email protected] | 216e904 | 2009-10-29 17:05:23 | [diff] [blame] | 74 | // True while AppController is calling Browser::OpenEmptyWindow(). We need a |
| 75 | // global flag here, analogue to BrowserInit::InProcessStartup() because |
| 76 | // otherwise the SessionService will try to restore sessions when we make a new |
| 77 | // window while there are no other active windows. |
| 78 | static bool g_is_opening_new_window = false; |
| 79 | |
[email protected] | 1bcdb53 | 2009-01-16 17:47:57 | [diff] [blame] | 80 | @implementation AppController |
| 81 | |
[email protected] | caadfca | 2010-01-22 21:51:21 | [diff] [blame^] | 82 | @synthesize startupComplete = startupComplete_; |
| 83 | |
[email protected] | cd63ef6 | 2009-05-06 19:41:37 | [diff] [blame] | 84 | // This method is called very early in application startup (ie, before |
| 85 | // the profile is loaded or any preferences have been registered). Defer any |
| 86 | // user-data initialization until -applicationDidFinishLaunching:. |
[email protected] | 88d7494 | 2009-01-21 22:04:44 | [diff] [blame] | 87 | - (void)awakeFromNib { |
[email protected] | 136140c | 2009-05-19 13:58:25 | [diff] [blame] | 88 | // We need to register the handlers early to catch events fired on launch. |
[email protected] | 57750f82 | 2009-04-21 21:43:09 | [diff] [blame] | 89 | NSAppleEventManager* em = [NSAppleEventManager sharedAppleEventManager]; |
| 90 | [em setEventHandler:self |
| 91 | andSelector:@selector(getUrl:withReply:) |
| 92 | forEventClass:kInternetEventClass |
| 93 | andEventID:kAEGetURL]; |
| 94 | [em setEventHandler:self |
| 95 | andSelector:@selector(getUrl:withReply:) |
| 96 | forEventClass:'WWW!' // A particularly ancient AppleEvent that dates |
| 97 | andEventID:'OURL']; // back to the Spyglass days. |
[email protected] | cd63ef6 | 2009-05-06 19:41:37 | [diff] [blame] | 98 | |
[email protected] | 449dd2f | 2009-05-27 13:04:00 | [diff] [blame] | 99 | // Register for various window layering changes. We use these to update |
| 100 | // various UI elements (command-key equivalents, etc) when the frontmost |
| 101 | // window changes. |
| 102 | NSNotificationCenter* notificationCenter = |
| 103 | [NSNotificationCenter defaultCenter]; |
[email protected] | ce560f8 | 2009-06-03 09:39:44 | [diff] [blame] | 104 | [notificationCenter |
[email protected] | 449dd2f | 2009-05-27 13:04:00 | [diff] [blame] | 105 | addObserver:self |
| 106 | selector:@selector(windowLayeringDidChange:) |
| 107 | name:NSWindowDidBecomeKeyNotification |
| 108 | object:nil]; |
| 109 | [notificationCenter |
| 110 | addObserver:self |
| 111 | selector:@selector(windowLayeringDidChange:) |
| 112 | name:NSWindowDidResignKeyNotification |
| 113 | object:nil]; |
| 114 | [notificationCenter |
| 115 | addObserver:self |
| 116 | selector:@selector(windowLayeringDidChange:) |
| 117 | name:NSWindowDidBecomeMainNotification |
| 118 | object:nil]; |
| 119 | [notificationCenter |
| 120 | addObserver:self |
| 121 | selector:@selector(windowLayeringDidChange:) |
| 122 | name:NSWindowDidResignMainNotification |
| 123 | object:nil]; |
| 124 | |
| 125 | // Register for a notification that the number of tabs changes in windows |
| 126 | // so we can adjust the close tab/window command keys. |
| 127 | [notificationCenter |
| 128 | addObserver:self |
| 129 | selector:@selector(tabsChanged:) |
| 130 | name:kTabStripNumberOfTabsChanged |
| 131 | object:nil]; |
| 132 | |
[email protected] | 136140c | 2009-05-19 13:58:25 | [diff] [blame] | 133 | // Set up the command updater for when there are no windows open |
| 134 | [self initMenuState]; |
[email protected] | f4acfae8 | 2009-09-11 00:33:38 | [diff] [blame] | 135 | |
| 136 | // Activate (bring to foreground) if asked to do so. On |
| 137 | // Windows this logic isn't necessary since |
| 138 | // BrowserWindow::Activate() calls ::SetForegroundWindow() which is |
| 139 | // adequate. On Mac, BrowserWindow::Activate() calls -[NSWindow |
| 140 | // makeKeyAndOrderFront:] which does not activate the application |
| 141 | // itself. |
| 142 | const CommandLine& parsed_command_line = *CommandLine::ForCurrentProcess(); |
| 143 | if (parsed_command_line.HasSwitch(switches::kActivateOnLaunch)) { |
| 144 | [NSApp activateIgnoringOtherApps:YES]; |
| 145 | } |
[email protected] | 136140c | 2009-05-19 13:58:25 | [diff] [blame] | 146 | } |
| 147 | |
[email protected] | a9e8afc | 2009-08-11 22:03:17 | [diff] [blame] | 148 | - (NSApplicationTerminateReply)applicationShouldTerminate: |
| 149 | (NSApplication *)sender { |
[email protected] | a9e8afc | 2009-08-11 22:03:17 | [diff] [blame] | 150 | // Check for in-progress downloads, and prompt the user if they really want to |
| 151 | // quit (and thus cancel the downloads). |
| 152 | if (![self shouldQuitWithInProgressDownloads]) |
| 153 | return NSTerminateCancel; |
| 154 | |
| 155 | return NSTerminateNow; |
| 156 | } |
| 157 | |
[email protected] | 449dd2f | 2009-05-27 13:04:00 | [diff] [blame] | 158 | // Called when the app is shutting down. Clean-up as appropriate. |
| 159 | - (void)applicationWillTerminate:(NSNotification *)aNotification { |
[email protected] | 3b6aa8b6 | 2009-09-15 21:36:11 | [diff] [blame] | 160 | NSAppleEventManager* em = [NSAppleEventManager sharedAppleEventManager]; |
| 161 | [em removeEventHandlerForEventClass:kInternetEventClass |
| 162 | andEventID:kAEGetURL]; |
| 163 | [em removeEventHandlerForEventClass:'WWW!' |
| 164 | andEventID:'OURL']; |
[email protected] | 3b6aa8b6 | 2009-09-15 21:36:11 | [diff] [blame] | 165 | |
| 166 | // Close all the windows. |
| 167 | BrowserList::CloseAllBrowsers(true); |
| 168 | |
| 169 | // On Windows, this is done in Browser::OnWindowClosing, but that's not |
| 170 | // appropriate on Mac since we don't shut down when we reach zero windows. |
| 171 | browser_shutdown::OnShutdownStarting(browser_shutdown::BROWSER_EXIT); |
| 172 | |
| 173 | // Release the reference to the browser process. Once all the browsers get |
| 174 | // dealloc'd, it will stop the RunLoop and fall back into main(). |
| 175 | g_browser_process->ReleaseModule(); |
[email protected] | fbc947b | 2009-06-19 13:28:24 | [diff] [blame] | 176 | |
[email protected] | d4e1865 | 2009-11-19 21:52:16 | [diff] [blame] | 177 | // Close these off if they have open windows. |
| 178 | [prefsController_ close]; |
| 179 | [aboutController_ close]; |
| 180 | |
[email protected] | 449dd2f | 2009-05-27 13:04:00 | [diff] [blame] | 181 | [[NSNotificationCenter defaultCenter] removeObserver:self]; |
| 182 | } |
| 183 | |
[email protected] | 3b6aa8b6 | 2009-09-15 21:36:11 | [diff] [blame] | 184 | - (void)didEndMainMessageLoop { |
| 185 | DCHECK(!BrowserList::HasBrowserWithProfile([self defaultProfile])); |
| 186 | if (!BrowserList::HasBrowserWithProfile([self defaultProfile])) { |
| 187 | // As we're shutting down, we need to nuke the TabRestoreService, which |
| 188 | // will start the shutdown of the NavigationControllers and allow for |
| 189 | // proper shutdown. If we don't do this, Chrome won't shut down cleanly, |
| 190 | // and may end up crashing when some thread tries to use the IO thread (or |
| 191 | // another thread) that is no longer valid. |
| 192 | [self defaultProfile]->ResetTabRestoreService(); |
| 193 | } |
| 194 | } |
| 195 | |
[email protected] | 449dd2f | 2009-05-27 13:04:00 | [diff] [blame] | 196 | // Helper routine to get the window controller if the key window is a tabbed |
| 197 | // window, or nil if not. Examples of non-tabbed windows are "about" or |
| 198 | // "preferences". |
| 199 | - (TabWindowController*)keyWindowTabController { |
| 200 | NSWindowController* keyWindowController = |
[email protected] | 5dbabd0 | 2009-09-17 20:44:13 | [diff] [blame] | 201 | [[NSApp keyWindow] windowController]; |
[email protected] | 449dd2f | 2009-05-27 13:04:00 | [diff] [blame] | 202 | if ([keyWindowController isKindOfClass:[TabWindowController class]]) |
| 203 | return (TabWindowController*)keyWindowController; |
| 204 | |
| 205 | return nil; |
| 206 | } |
| 207 | |
[email protected] | 74c0c64 | 2009-09-22 21:25:50 | [diff] [blame] | 208 | // Helper routine to get the window controller if the main window is a tabbed |
| 209 | // window, or nil if not. Examples of non-tabbed windows are "about" or |
| 210 | // "preferences". |
| 211 | - (TabWindowController*)mainWindowTabController { |
| 212 | NSWindowController* mainWindowController = |
| 213 | [[NSApp mainWindow] windowController]; |
| 214 | if ([mainWindowController isKindOfClass:[TabWindowController class]]) |
| 215 | return (TabWindowController*)mainWindowController; |
| 216 | |
| 217 | return nil; |
| 218 | } |
| 219 | |
[email protected] | 449dd2f | 2009-05-27 13:04:00 | [diff] [blame] | 220 | // If the window has tabs, make "close window" be cmd-shift-w, otherwise leave |
| 221 | // it as the normal cmd-w. Capitalization of the key equivalent affects whether |
| 222 | // the shift modifer is used. |
| 223 | - (void)adjustCloseWindowMenuItemKeyEquivalent:(BOOL)inHaveTabs { |
| 224 | [closeWindowMenuItem_ setKeyEquivalent:(inHaveTabs ? @"W" : @"w")]; |
[email protected] | 74c0c64 | 2009-09-22 21:25:50 | [diff] [blame] | 225 | [closeWindowMenuItem_ setKeyEquivalentModifierMask:NSCommandKeyMask]; |
[email protected] | 449dd2f | 2009-05-27 13:04:00 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | // If the window has tabs, make "close tab" take over cmd-w, otherwise it |
| 229 | // shouldn't have any key-equivalent because it should be disabled. |
| 230 | - (void)adjustCloseTabMenuItemKeyEquivalent:(BOOL)hasTabs { |
| 231 | if (hasTabs) { |
| 232 | [closeTabMenuItem_ setKeyEquivalent:@"w"]; |
| 233 | [closeTabMenuItem_ setKeyEquivalentModifierMask:NSCommandKeyMask]; |
| 234 | } else { |
| 235 | [closeTabMenuItem_ setKeyEquivalent:@""]; |
| 236 | [closeTabMenuItem_ setKeyEquivalentModifierMask:0]; |
| 237 | } |
| 238 | } |
| 239 | |
[email protected] | 74c0c64 | 2009-09-22 21:25:50 | [diff] [blame] | 240 | // Explicitly remove any command-key equivalents from the close tab/window |
| 241 | // menus so that nothing can go haywire if we get a user action during pending |
| 242 | // updates. |
| 243 | - (void)clearCloseMenuItemKeyEquivalents { |
| 244 | [closeTabMenuItem_ setKeyEquivalent:@""]; |
| 245 | [closeTabMenuItem_ setKeyEquivalentModifierMask:0]; |
| 246 | [closeWindowMenuItem_ setKeyEquivalent:@""]; |
| 247 | [closeWindowMenuItem_ setKeyEquivalentModifierMask:0]; |
| 248 | } |
| 249 | |
[email protected] | 449dd2f | 2009-05-27 13:04:00 | [diff] [blame] | 250 | // See if we have a window with tabs open, and adjust the key equivalents for |
| 251 | // Close Tab/Close Window accordingly |
| 252 | - (void)fixCloseMenuItemKeyEquivalents { |
| 253 | TabWindowController* tabController = [self keyWindowTabController]; |
[email protected] | 74c0c64 | 2009-09-22 21:25:50 | [diff] [blame] | 254 | if (!tabController && ![NSApp keyWindow]) { |
| 255 | // There might be a small amount of time where there is no key window, |
| 256 | // so just use our main browser window if there is one. |
| 257 | tabController = [self mainWindowTabController]; |
| 258 | } |
[email protected] | 449dd2f | 2009-05-27 13:04:00 | [diff] [blame] | 259 | BOOL windowWithMultipleTabs = |
| 260 | (tabController && [tabController numberOfTabs] > 1); |
| 261 | [self adjustCloseWindowMenuItemKeyEquivalent:windowWithMultipleTabs]; |
| 262 | [self adjustCloseTabMenuItemKeyEquivalent:windowWithMultipleTabs]; |
| 263 | fileMenuUpdatePending_ = NO; |
| 264 | } |
| 265 | |
| 266 | // Fix up the "close tab/close window" command-key equivalents. We do this |
| 267 | // after a delay to ensure that window layer state has been set by the time |
[email protected] | f2a5c34 | 2010-01-21 17:00:29 | [diff] [blame] | 268 | // we do the enabling. This should only be called on the main thread, code that |
| 269 | // calls this (even as a side-effect) from other threads needs to be fixed. |
[email protected] | 449dd2f | 2009-05-27 13:04:00 | [diff] [blame] | 270 | - (void)delayedFixCloseMenuItemKeyEquivalents { |
[email protected] | f2a5c34 | 2010-01-21 17:00:29 | [diff] [blame] | 271 | DCHECK([NSThread isMainThread]); |
[email protected] | 449dd2f | 2009-05-27 13:04:00 | [diff] [blame] | 272 | if (!fileMenuUpdatePending_) { |
[email protected] | 74c0c64 | 2009-09-22 21:25:50 | [diff] [blame] | 273 | // The OS prefers keypresses to timers, so it's possible that a cmd-w |
| 274 | // can sneak in before this timer fires. In order to prevent that from |
| 275 | // having any bad consequences, just clear the keys combos altogether. They |
| 276 | // will be reset when the timer eventually fires. |
| 277 | [self clearCloseMenuItemKeyEquivalents]; |
[email protected] | f2a5c34 | 2010-01-21 17:00:29 | [diff] [blame] | 278 | [self performSelectorOnMainThread:@selector(fixCloseMenuItemKeyEquivalents) |
| 279 | withObject:nil |
| 280 | waitUntilDone:NO]; |
[email protected] | 449dd2f | 2009-05-27 13:04:00 | [diff] [blame] | 281 | fileMenuUpdatePending_ = YES; |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | // Called when we get a notification about the window layering changing to |
| 286 | // update the UI based on the new main window. |
| 287 | - (void)windowLayeringDidChange:(NSNotification*)notify { |
| 288 | [self delayedFixCloseMenuItemKeyEquivalents]; |
[email protected] | ce560f8 | 2009-06-03 09:39:44 | [diff] [blame] | 289 | |
[email protected] | 449dd2f | 2009-05-27 13:04:00 | [diff] [blame] | 290 | // TODO(pinkerton): If we have other things here, such as inspector panels |
| 291 | // that follow the contents of the selected webpage, we would update those |
| 292 | // here. |
| 293 | } |
| 294 | |
| 295 | // Called when the number of tabs changes in one of the browser windows. The |
| 296 | // object is the tab strip controller, but we don't currently care. |
| 297 | - (void)tabsChanged:(NSNotification*)notify { |
[email protected] | e72746b1 | 2009-10-19 14:11:51 | [diff] [blame] | 298 | // We don't need to do this on a delay, as in the method above, because the |
| 299 | // window layering isn't changing. As a result, there's no chance that a |
| 300 | // different window will sneak in as the key window and cause the problems |
| 301 | // we hacked around above by clearing the key equivalents. |
| 302 | [self fixCloseMenuItemKeyEquivalents]; |
[email protected] | 449dd2f | 2009-05-27 13:04:00 | [diff] [blame] | 303 | } |
| 304 | |
[email protected] | 414fde59 | 2009-05-21 16:14:43 | [diff] [blame] | 305 | // If the auto-update interval is not set, make it 5 hours. |
| 306 | // This code is specific to Mac Chrome Dev Channel. |
| 307 | // Placed here for 2 reasons: |
| 308 | // 1) Same spot as other Pref stuff |
| 309 | // 2) Try and be friendly by keeping this after app launch |
| 310 | // TODO(jrg): remove once we go Beta. |
| 311 | - (void)setUpdateCheckInterval { |
| 312 | #if defined(GOOGLE_CHROME_BUILD) |
| 313 | CFStringRef app = (CFStringRef)@"com.google.Keystone.Agent"; |
| 314 | CFStringRef checkInterval = (CFStringRef)@"checkInterval"; |
| 315 | CFPropertyListRef plist = CFPreferencesCopyAppValue(checkInterval, app); |
| 316 | if (!plist) { |
| 317 | const float fiveHoursInSeconds = 5.0 * 60.0 * 60.0; |
| 318 | NSNumber *value = [NSNumber numberWithFloat:fiveHoursInSeconds]; |
| 319 | CFPreferencesSetAppValue(checkInterval, value, app); |
| 320 | CFPreferencesAppSynchronize(app); |
| 321 | } |
| 322 | #endif |
| 323 | } |
| 324 | |
[email protected] | 136140c | 2009-05-19 13:58:25 | [diff] [blame] | 325 | // This is called after profiles have been loaded and preferences registered. |
| 326 | // It is safe to access the default profile here. |
| 327 | - (void)applicationDidFinishLaunching:(NSNotification*)notify { |
| 328 | // Hold an extra ref to the BrowserProcess singleton so it doesn't go away |
| 329 | // when all the browser windows get closed. We'll release it on quit which |
| 330 | // will be the signal to exit. |
| 331 | DCHECK(g_browser_process); |
| 332 | g_browser_process->AddRefModule(); |
| 333 | |
[email protected] | e557502 | 2009-08-22 00:31:22 | [diff] [blame] | 334 | bookmarkMenuBridge_.reset(new BookmarkMenuBridge([self defaultProfile])); |
[email protected] | 1c84c82c | 2009-07-27 15:37:25 | [diff] [blame] | 335 | historyMenuBridge_.reset(new HistoryMenuBridge([self defaultProfile])); |
[email protected] | cd63ef6 | 2009-05-06 19:41:37 | [diff] [blame] | 336 | |
[email protected] | 414fde59 | 2009-05-21 16:14:43 | [diff] [blame] | 337 | [self setUpdateCheckInterval]; |
[email protected] | 2bcec61 | 2009-05-14 17:50:53 | [diff] [blame] | 338 | |
| 339 | // Build up the encoding menu, the order of the items differs based on the |
| 340 | // current locale (see http://crbug.com/7647 for details). |
| 341 | // We need a valid g_browser_process to get the profile which is why we can't |
| 342 | // call this from awakeFromNib. |
[email protected] | fc34932 | 2009-09-23 22:28:23 | [diff] [blame] | 343 | NSMenu* view_menu = [[[NSApp mainMenu] itemWithTag:IDC_VIEW_MENU] submenu]; |
| 344 | NSMenuItem* encoding_menu_item = [view_menu itemWithTag:IDC_ENCODING_MENU]; |
| 345 | NSMenu *encoding_menu = [encoding_menu_item submenu]; |
| 346 | EncodingMenuControllerDelegate::BuildEncodingMenu([self defaultProfile], |
| 347 | encoding_menu); |
[email protected] | 2bcec61 | 2009-05-14 17:50:53 | [diff] [blame] | 348 | |
[email protected] | f2c20fa | 2009-12-01 17:42:02 | [diff] [blame] | 349 | // Since Chrome is localized to more languages than the OS, tell Cocoa which |
| 350 | // menu is the Help so it can add the search item to it. |
[email protected] | caadfca | 2010-01-22 21:51:21 | [diff] [blame^] | 351 | if (helpMenu_ && [NSApp respondsToSelector:@selector(setHelpMenu:)]) |
[email protected] | f2c20fa | 2009-12-01 17:42:02 | [diff] [blame] | 352 | [NSApp setHelpMenu:helpMenu_]; |
[email protected] | f2c20fa | 2009-12-01 17:42:02 | [diff] [blame] | 353 | |
[email protected] | caadfca | 2010-01-22 21:51:21 | [diff] [blame^] | 354 | startupComplete_ = YES; |
| 355 | |
| 356 | // TODO(viettrungluu): This is very temporary, since this should be done "in" |
| 357 | // |BrowserMain()|, i.e., this list of startup URLs should be appended to the |
| 358 | // (probably-empty) list of URLs from the command line. |
| 359 | if (startupURLs_.size()) { |
| 360 | [self openURLs:startupURLs_]; |
| 361 | [self clearStartupURLs]; |
| 362 | } |
[email protected] | 7c32108 | 2009-02-09 15:35:47 | [diff] [blame] | 363 | } |
| 364 | |
[email protected] | a96ec6a | 2009-11-04 17:27:08 | [diff] [blame] | 365 | // This is called after profiles have been loaded and preferences registered. |
| 366 | // It is safe to access the default profile here. |
| 367 | - (void)applicationDidBecomeActive:(NSNotification*)notify { |
| 368 | NotificationService::current()->Notify(NotificationType::APP_ACTIVATED, |
| 369 | NotificationService::AllSources(), |
| 370 | NotificationService::NoDetails()); |
| 371 | } |
| 372 | |
[email protected] | 73f5b36 | 2009-08-10 23:58:21 | [diff] [blame] | 373 | // Helper function for populating and displaying the in progress downloads at |
| 374 | // exit alert panel. |
| 375 | - (BOOL)userWillWaitForInProgressDownloads:(int)downloadCount { |
[email protected] | e8b5f788 | 2009-09-14 20:59:13 | [diff] [blame] | 376 | NSString* warningText = nil; |
| 377 | NSString* explanationText = nil; |
[email protected] | 73f5b36 | 2009-08-10 23:58:21 | [diff] [blame] | 378 | NSString* waitTitle = nil; |
| 379 | NSString* exitTitle = nil; |
| 380 | |
[email protected] | 9dcb5966 | 2009-09-23 01:15:50 | [diff] [blame] | 381 | std::wstring product_name = l10n_util::GetString(IDS_PRODUCT_NAME); |
| 382 | |
[email protected] | 73f5b36 | 2009-08-10 23:58:21 | [diff] [blame] | 383 | // Set the dialog text based on whether or not there are multiple downloads. |
| 384 | if (downloadCount == 1) { |
[email protected] | e8b5f788 | 2009-09-14 20:59:13 | [diff] [blame] | 385 | // Dialog text: warning and explanation. |
| 386 | warningText = |
[email protected] | 9dcb5966 | 2009-09-23 01:15:50 | [diff] [blame] | 387 | base::SysWideToNSString(l10n_util::GetStringF( |
| 388 | IDS_SINGLE_DOWNLOAD_REMOVE_CONFIRM_WARNING, product_name)); |
[email protected] | e8b5f788 | 2009-09-14 20:59:13 | [diff] [blame] | 389 | explanationText = |
[email protected] | 9dcb5966 | 2009-09-23 01:15:50 | [diff] [blame] | 390 | base::SysWideToNSString(l10n_util::GetStringF( |
| 391 | IDS_SINGLE_DOWNLOAD_REMOVE_CONFIRM_EXPLANATION, product_name)); |
[email protected] | 73f5b36 | 2009-08-10 23:58:21 | [diff] [blame] | 392 | |
| 393 | // Cancel download and exit button text. |
| 394 | exitTitle = |
[email protected] | e8b5f788 | 2009-09-14 20:59:13 | [diff] [blame] | 395 | base::SysWideToNSString(l10n_util::GetString( |
| 396 | IDS_SINGLE_DOWNLOAD_REMOVE_CONFIRM_OK_BUTTON_LABEL)); |
[email protected] | 73f5b36 | 2009-08-10 23:58:21 | [diff] [blame] | 397 | |
| 398 | // Wait for download button text. |
| 399 | waitTitle = |
[email protected] | e8b5f788 | 2009-09-14 20:59:13 | [diff] [blame] | 400 | base::SysWideToNSString(l10n_util::GetString( |
| 401 | IDS_SINGLE_DOWNLOAD_REMOVE_CONFIRM_CANCEL_BUTTON_LABEL)); |
[email protected] | 73f5b36 | 2009-08-10 23:58:21 | [diff] [blame] | 402 | } else { |
[email protected] | e8b5f788 | 2009-09-14 20:59:13 | [diff] [blame] | 403 | // Dialog text: warning and explanation. |
| 404 | warningText = |
| 405 | base::SysWideToNSString(l10n_util::GetStringF( |
[email protected] | 9dcb5966 | 2009-09-23 01:15:50 | [diff] [blame] | 406 | IDS_MULTIPLE_DOWNLOADS_REMOVE_CONFIRM_WARNING, product_name, |
| 407 | IntToWString(downloadCount))); |
[email protected] | e8b5f788 | 2009-09-14 20:59:13 | [diff] [blame] | 408 | explanationText = |
[email protected] | 9dcb5966 | 2009-09-23 01:15:50 | [diff] [blame] | 409 | base::SysWideToNSString(l10n_util::GetStringF( |
| 410 | IDS_MULTIPLE_DOWNLOADS_REMOVE_CONFIRM_EXPLANATION, product_name)); |
[email protected] | 73f5b36 | 2009-08-10 23:58:21 | [diff] [blame] | 411 | |
| 412 | // Cancel downloads and exit button text. |
| 413 | exitTitle = |
[email protected] | e8b5f788 | 2009-09-14 20:59:13 | [diff] [blame] | 414 | base::SysWideToNSString(l10n_util::GetString( |
| 415 | IDS_MULTIPLE_DOWNLOADS_REMOVE_CONFIRM_OK_BUTTON_LABEL)); |
[email protected] | 73f5b36 | 2009-08-10 23:58:21 | [diff] [blame] | 416 | |
| 417 | // Wait for downloads button text. |
| 418 | waitTitle = |
[email protected] | e8b5f788 | 2009-09-14 20:59:13 | [diff] [blame] | 419 | base::SysWideToNSString(l10n_util::GetString( |
| 420 | IDS_MULTIPLE_DOWNLOADS_REMOVE_CONFIRM_CANCEL_BUTTON_LABEL)); |
[email protected] | 73f5b36 | 2009-08-10 23:58:21 | [diff] [blame] | 421 | } |
| 422 | |
| 423 | // 'waitButton' is the default choice. |
[email protected] | e8b5f788 | 2009-09-14 20:59:13 | [diff] [blame] | 424 | int choice = NSRunAlertPanel(warningText, explanationText, |
| 425 | waitTitle, exitTitle, nil); |
[email protected] | 73f5b36 | 2009-08-10 23:58:21 | [diff] [blame] | 426 | return choice == NSAlertDefaultReturn ? YES : NO; |
| 427 | } |
| 428 | |
| 429 | // Check all profiles for in progress downloads, and if we find any, prompt the |
[email protected] | 09729a55 | 2009-08-03 23:21:41 | [diff] [blame] | 430 | // user to see if we should continue to exit (and thus cancel the downloads), or |
| 431 | // if we should wait. |
| 432 | - (BOOL)shouldQuitWithInProgressDownloads { |
[email protected] | 73f5b36 | 2009-08-10 23:58:21 | [diff] [blame] | 433 | ProfileManager* profile_manager = g_browser_process->profile_manager(); |
| 434 | if (!profile_manager) |
| 435 | return YES; |
[email protected] | 09729a55 | 2009-08-03 23:21:41 | [diff] [blame] | 436 | |
[email protected] | 73f5b36 | 2009-08-10 23:58:21 | [diff] [blame] | 437 | ProfileManager::const_iterator it = profile_manager->begin(); |
| 438 | for (; it != profile_manager->end(); ++it) { |
| 439 | Profile* profile = *it; |
[email protected] | 09729a55 | 2009-08-03 23:21:41 | [diff] [blame] | 440 | DownloadManager* download_manager = profile->GetDownloadManager(); |
[email protected] | 73f5b36 | 2009-08-10 23:58:21 | [diff] [blame] | 441 | if (download_manager && download_manager->in_progress_count() > 0) { |
| 442 | int downloadCount = download_manager->in_progress_count(); |
| 443 | if ([self userWillWaitForInProgressDownloads:downloadCount]) { |
| 444 | // Create a new browser window (if necessary) and navigate to the |
| 445 | // downloads page if the user chooses to wait. |
| 446 | Browser* browser = BrowserList::FindBrowserWithProfile(profile); |
| 447 | if (!browser) { |
| 448 | browser = Browser::Create(profile); |
| 449 | browser->window()->Show(); |
| 450 | } |
| 451 | DCHECK(browser); |
| 452 | browser->ShowDownloadsTab(); |
| 453 | return NO; |
| 454 | } |
[email protected] | 09729a55 | 2009-08-03 23:21:41 | [diff] [blame] | 455 | |
[email protected] | 73f5b36 | 2009-08-10 23:58:21 | [diff] [blame] | 456 | // User wants to exit. |
| 457 | return YES; |
[email protected] | 09729a55 | 2009-08-03 23:21:41 | [diff] [blame] | 458 | } |
[email protected] | 09729a55 | 2009-08-03 23:21:41 | [diff] [blame] | 459 | } |
| 460 | |
[email protected] | 73f5b36 | 2009-08-10 23:58:21 | [diff] [blame] | 461 | // No profiles or active downloads found, okay to exit. |
[email protected] | 09729a55 | 2009-08-03 23:21:41 | [diff] [blame] | 462 | return YES; |
| 463 | } |
| 464 | |
[email protected] | fbc947b | 2009-06-19 13:28:24 | [diff] [blame] | 465 | // Called to determine if we should enable the "restore tab" menu item. |
| 466 | // Checks with the TabRestoreService to see if there's anything there to |
| 467 | // restore and returns YES if so. |
| 468 | - (BOOL)canRestoreTab { |
| 469 | TabRestoreService* service = [self defaultProfile]->GetTabRestoreService(); |
| 470 | return service && !service->entries().empty(); |
| 471 | } |
| 472 | |
[email protected] | 6a28133 | 2009-10-13 17:41:06 | [diff] [blame] | 473 | // Returns true if there is no browser window, or if the active window is |
| 474 | // blocked by a modal dialog. |
| 475 | - (BOOL)keyWindowIsMissingOrBlocked { |
| 476 | Browser* browser = BrowserList::GetLastActive(); |
| 477 | return browser == NULL || |
| 478 | ![[browser->window()->GetNativeHandle() attachedSheet] |
| 479 | isKindOfClass:[NSWindow class]]; |
| 480 | } |
| 481 | |
[email protected] | 88d7494 | 2009-01-21 22:04:44 | [diff] [blame] | 482 | // Called to validate menu items when there are no key windows. All the |
| 483 | // items we care about have been set with the |commandDispatch:| action and |
| 484 | // a target of FirstResponder in IB. If it's not one of those, let it |
| 485 | // continue up the responder chain to be handled elsewhere. We pull out the |
| 486 | // tag as the cross-platform constant to differentiate and dispatch the |
| 487 | // various commands. |
| 488 | - (BOOL)validateUserInterfaceItem:(id<NSValidatedUserInterfaceItem>)item { |
| 489 | SEL action = [item action]; |
| 490 | BOOL enable = NO; |
| 491 | if (action == @selector(commandDispatch:)) { |
| 492 | NSInteger tag = [item tag]; |
[email protected] | fbc947b | 2009-06-19 13:28:24 | [diff] [blame] | 493 | if (menuState_->SupportsCommand(tag)) { |
| 494 | switch (tag) { |
[email protected] | 419eb2a | 2009-11-11 16:26:26 | [diff] [blame] | 495 | // The File Menu commands are not automatically disabled by Cocoa when a |
| 496 | // dialog sheet obscures the browser window, so we disable several of |
| 497 | // them here. We don't need to include IDC_CLOSE_WINDOW, because |
| 498 | // app_controller is only activated when there are no key windows (see |
| 499 | // function comment). |
[email protected] | fbc947b | 2009-06-19 13:28:24 | [diff] [blame] | 500 | case IDC_RESTORE_TAB: |
[email protected] | 6a28133 | 2009-10-13 17:41:06 | [diff] [blame] | 501 | enable = [self keyWindowIsMissingOrBlocked] && [self canRestoreTab]; |
| 502 | break; |
[email protected] | 6a28133 | 2009-10-13 17:41:06 | [diff] [blame] | 503 | case IDC_OPEN_FILE: |
[email protected] | 6a28133 | 2009-10-13 17:41:06 | [diff] [blame] | 504 | case IDC_NEW_TAB: |
[email protected] | 6a28133 | 2009-10-13 17:41:06 | [diff] [blame] | 505 | enable = [self keyWindowIsMissingOrBlocked]; |
[email protected] | fbc947b | 2009-06-19 13:28:24 | [diff] [blame] | 506 | break; |
[email protected] | f9bc9b9 | 2009-11-24 00:55:35 | [diff] [blame] | 507 | case IDC_SYNC_BOOKMARKS: |
| 508 | enable = ProfileSyncService::IsSyncEnabled(); |
[email protected] | 40f04797 | 2009-11-25 03:54:40 | [diff] [blame] | 509 | sync_ui_util::UpdateSyncItem(item, enable, [self defaultProfile]); |
[email protected] | f9bc9b9 | 2009-11-24 00:55:35 | [diff] [blame] | 510 | break; |
[email protected] | fbc947b | 2009-06-19 13:28:24 | [diff] [blame] | 511 | default: |
| 512 | enable = menuState_->IsCommandEnabled(tag) ? YES : NO; |
| 513 | } |
| 514 | } |
[email protected] | 3b6aa8b6 | 2009-09-15 21:36:11 | [diff] [blame] | 515 | } else if (action == @selector(terminate:)) { |
[email protected] | 88d7494 | 2009-01-21 22:04:44 | [diff] [blame] | 516 | enable = YES; |
[email protected] | 3111f08b | 2009-04-30 16:01:52 | [diff] [blame] | 517 | } else if (action == @selector(showPreferences:)) { |
| 518 | enable = YES; |
[email protected] | bde3dda | 2009-05-20 22:13:07 | [diff] [blame] | 519 | } else if (action == @selector(orderFrontStandardAboutPanel:)) { |
| 520 | enable = YES; |
[email protected] | 3d4bd23f | 2009-10-06 03:58:38 | [diff] [blame] | 521 | } else if (action == @selector(newWindowFromDock:)) { |
| 522 | enable = YES; |
[email protected] | 88d7494 | 2009-01-21 22:04:44 | [diff] [blame] | 523 | } |
| 524 | return enable; |
| 525 | } |
| 526 | |
[email protected] | 947fc0d | 2010-01-14 22:33:05 | [diff] [blame] | 527 | // Called when the user picks a menu item when there are no key windows, or when |
| 528 | // there is no foreground browser window. Calls through to the browser object to |
| 529 | // execute the command. This assumes that the command is supported and doesn't |
| 530 | // check, otherwise it would have been disabled in the UI in |
| 531 | // validateUserInterfaceItem:. |
[email protected] | 88d7494 | 2009-01-21 22:04:44 | [diff] [blame] | 532 | - (void)commandDispatch:(id)sender { |
[email protected] | 93e18176 | 2009-05-29 14:30:38 | [diff] [blame] | 533 | Profile* defaultProfile = [self defaultProfile]; |
[email protected] | f0a51fb5 | 2009-03-05 12:46:38 | [diff] [blame] | 534 | |
[email protected] | 947fc0d | 2010-01-14 22:33:05 | [diff] [blame] | 535 | // Handle the case where we're dispatching a command from a sender that's in a |
| 536 | // browser window. This means that the command came from a background window |
| 537 | // and is getting here because the foreground window is not a browser window. |
| 538 | if ([sender respondsToSelector:@selector(window)]) { |
| 539 | id delegate = [[sender window] windowController]; |
| 540 | if ([delegate isKindOfClass:[BrowserWindowController class]]) { |
| 541 | [delegate commandDispatch:sender]; |
| 542 | return; |
| 543 | } |
| 544 | } |
| 545 | |
[email protected] | 88d7494 | 2009-01-21 22:04:44 | [diff] [blame] | 546 | NSInteger tag = [sender tag]; |
| 547 | switch (tag) { |
[email protected] | fbc947b | 2009-06-19 13:28:24 | [diff] [blame] | 548 | case IDC_NEW_TAB: |
[email protected] | 88d7494 | 2009-01-21 22:04:44 | [diff] [blame] | 549 | case IDC_NEW_WINDOW: |
[email protected] | 91a5b3e | 2009-10-30 19:32:22 | [diff] [blame] | 550 | case IDC_FOCUS_LOCATION: |
[email protected] | 216e904 | 2009-10-29 17:05:23 | [diff] [blame] | 551 | g_is_opening_new_window = true; |
[email protected] | 93e18176 | 2009-05-29 14:30:38 | [diff] [blame] | 552 | Browser::OpenEmptyWindow(defaultProfile); |
[email protected] | 216e904 | 2009-10-29 17:05:23 | [diff] [blame] | 553 | g_is_opening_new_window = false; |
[email protected] | 88d7494 | 2009-01-21 22:04:44 | [diff] [blame] | 554 | break; |
[email protected] | cddc524 | 2009-12-07 18:54:33 | [diff] [blame] | 555 | case IDC_FOCUS_SEARCH: |
| 556 | g_is_opening_new_window = true; |
| 557 | Browser::OpenEmptyWindow(defaultProfile); |
| 558 | g_is_opening_new_window = false; |
| 559 | BrowserList::GetLastActive()->FocusSearch(); |
| 560 | break; |
[email protected] | 863ff66 | 2009-01-26 20:18:18 | [diff] [blame] | 561 | case IDC_NEW_INCOGNITO_WINDOW: |
[email protected] | e9e21127 | 2009-09-22 16:39:11 | [diff] [blame] | 562 | Browser::OpenEmptyWindow(defaultProfile->GetOffTheRecordProfile()); |
[email protected] | 863ff66 | 2009-01-26 20:18:18 | [diff] [blame] | 563 | break; |
[email protected] | fbc947b | 2009-06-19 13:28:24 | [diff] [blame] | 564 | case IDC_RESTORE_TAB: |
| 565 | Browser::OpenWindowWithRestoredTabs(defaultProfile); |
| 566 | break; |
[email protected] | e19516d | 2009-04-28 17:15:19 | [diff] [blame] | 567 | case IDC_OPEN_FILE: |
[email protected] | cddc524 | 2009-12-07 18:54:33 | [diff] [blame] | 568 | g_is_opening_new_window = true; |
[email protected] | 93e18176 | 2009-05-29 14:30:38 | [diff] [blame] | 569 | Browser::OpenEmptyWindow(defaultProfile); |
[email protected] | cddc524 | 2009-12-07 18:54:33 | [diff] [blame] | 570 | g_is_opening_new_window = false; |
[email protected] | e19516d | 2009-04-28 17:15:19 | [diff] [blame] | 571 | BrowserList::GetLastActive()-> |
| 572 | ExecuteCommandWithDisposition(IDC_OPEN_FILE, CURRENT_TAB); |
| 573 | break; |
[email protected] | 1fdff70 | 2009-10-22 00:36:18 | [diff] [blame] | 574 | case IDC_CLEAR_BROWSING_DATA: { |
[email protected] | 93e18176 | 2009-05-29 14:30:38 | [diff] [blame] | 575 | // There may not be a browser open, so use the default profile. |
[email protected] | 049ac83 | 2009-12-15 16:26:54 | [diff] [blame] | 576 | [ClearBrowsingDataController |
| 577 | showClearBrowsingDialogForProfile:defaultProfile]; |
[email protected] | 93e18176 | 2009-05-29 14:30:38 | [diff] [blame] | 578 | break; |
[email protected] | 1fdff70 | 2009-10-22 00:36:18 | [diff] [blame] | 579 | } |
[email protected] | 4aee565 | 2009-11-19 18:59:21 | [diff] [blame] | 580 | case IDC_IMPORT_SETTINGS: { |
| 581 | UserMetrics::RecordAction("Import_ShowDlg", defaultProfile); |
| 582 | // Note that this dialog controller cleans itself up when closed |
| 583 | // so auto-scoping it here is not necessary. |
| 584 | [[[ImportSettingsDialogController alloc] |
| 585 | initWithProfile:defaultProfile parentWindow:nil] runModalDialog]; |
| 586 | break; |
| 587 | } |
[email protected] | 91a5b3e | 2009-10-30 19:32:22 | [diff] [blame] | 588 | case IDC_SHOW_HISTORY: |
| 589 | Browser::OpenHistoryWindow(defaultProfile); |
| 590 | break; |
| 591 | case IDC_SHOW_DOWNLOADS: |
| 592 | Browser::OpenDownloadsWindow(defaultProfile); |
| 593 | break; |
[email protected] | bb37e6ec | 2009-11-18 17:59:36 | [diff] [blame] | 594 | case IDC_MANAGE_EXTENSIONS: |
| 595 | Browser::OpenExtensionsWindow(defaultProfile); |
| 596 | break; |
[email protected] | 1fdff70 | 2009-10-22 00:36:18 | [diff] [blame] | 597 | case IDC_HELP_PAGE: |
| 598 | Browser::OpenHelpWindow(defaultProfile); |
| 599 | break; |
[email protected] | 3896b18 | 2009-11-03 16:15:32 | [diff] [blame] | 600 | case IDC_REPORT_BUG: { |
| 601 | Browser* browser = BrowserList::GetLastActive(); |
| 602 | TabContents* current_tab = (browser != NULL) ? |
| 603 | browser->GetSelectedTabContents() : NULL; |
| 604 | BugReportWindowController* controller = |
| 605 | [[BugReportWindowController alloc] |
| 606 | initWithTabContents:current_tab |
| 607 | profile:[self defaultProfile]]; |
| 608 | [controller runModalDialog]; |
| 609 | break; |
| 610 | } |
[email protected] | f9bc9b9 | 2009-11-24 00:55:35 | [diff] [blame] | 611 | case IDC_SYNC_BOOKMARKS: |
| 612 | // TODO(akalin): Add a constant to denote starting sync from the |
| 613 | // main menu and use that instead of START_FROM_WRENCH. |
[email protected] | 40f04797 | 2009-11-25 03:54:40 | [diff] [blame] | 614 | sync_ui_util::OpenSyncMyBookmarksDialog( |
[email protected] | f9bc9b9 | 2009-11-24 00:55:35 | [diff] [blame] | 615 | defaultProfile, ProfileSyncService::START_FROM_WRENCH); |
| 616 | break; |
[email protected] | ceaaf181 | 2010-01-20 22:13:09 | [diff] [blame] | 617 | case IDC_TASK_MANAGER: |
| 618 | UserMetrics::RecordAction("TaskManager", defaultProfile); |
| 619 | TaskManagerMac::Show(); |
| 620 | break; |
[email protected] | 88d7494 | 2009-01-21 22:04:44 | [diff] [blame] | 621 | }; |
| 622 | } |
| 623 | |
[email protected] | 947fc0d | 2010-01-14 22:33:05 | [diff] [blame] | 624 | // Same as |-commandDispatch:|, but executes commands using a disposition |
| 625 | // determined by the key flags. This will get called in the case where the |
| 626 | // frontmost window is not a browser window, and the user has command-clicked |
| 627 | // a button in a background browser window whose action is |
| 628 | // |-commandDispatchUsingKeyModifiers:| |
| 629 | - (void)commandDispatchUsingKeyModifiers:(id)sender { |
| 630 | DCHECK(sender); |
| 631 | if ([sender respondsToSelector:@selector(window)]) { |
| 632 | id delegate = [[sender window] windowController]; |
| 633 | if ([delegate isKindOfClass:[BrowserWindowController class]]) { |
| 634 | [delegate commandDispatchUsingKeyModifiers:sender]; |
| 635 | } |
| 636 | } |
| 637 | } |
| 638 | |
[email protected] | ff81e0a | 2009-04-13 14:58:54 | [diff] [blame] | 639 | // NSApplication delegate method called when someone clicks on the |
| 640 | // dock icon and there are no open windows. To match standard mac |
| 641 | // behavior, we should open a new window. |
| 642 | - (BOOL)applicationShouldHandleReopen:(NSApplication*)theApplication |
| 643 | hasVisibleWindows:(BOOL)flag { |
| 644 | // Don't do anything if there are visible windows. This will cause |
| 645 | // AppKit to unminimize the most recently minimized window. |
| 646 | if (flag) |
| 647 | return YES; |
| 648 | |
| 649 | // Otherwise open a new window. |
[email protected] | 216e904 | 2009-10-29 17:05:23 | [diff] [blame] | 650 | g_is_opening_new_window = true; |
[email protected] | ff81e0a | 2009-04-13 14:58:54 | [diff] [blame] | 651 | Browser::OpenEmptyWindow([self defaultProfile]); |
[email protected] | 216e904 | 2009-10-29 17:05:23 | [diff] [blame] | 652 | g_is_opening_new_window = false; |
[email protected] | ff81e0a | 2009-04-13 14:58:54 | [diff] [blame] | 653 | |
| 654 | // We've handled the reopen event, so return NO to tell AppKit not |
| 655 | // to do anything. |
| 656 | return NO; |
| 657 | } |
| 658 | |
[email protected] | 88d7494 | 2009-01-21 22:04:44 | [diff] [blame] | 659 | - (void)initMenuState { |
[email protected] | 3111f08b | 2009-04-30 16:01:52 | [diff] [blame] | 660 | menuState_.reset(new CommandUpdater(NULL)); |
[email protected] | fbc947b | 2009-06-19 13:28:24 | [diff] [blame] | 661 | menuState_->UpdateCommandEnabled(IDC_NEW_TAB, true); |
[email protected] | 88d7494 | 2009-01-21 22:04:44 | [diff] [blame] | 662 | menuState_->UpdateCommandEnabled(IDC_NEW_WINDOW, true); |
[email protected] | 863ff66 | 2009-01-26 20:18:18 | [diff] [blame] | 663 | menuState_->UpdateCommandEnabled(IDC_NEW_INCOGNITO_WINDOW, true); |
[email protected] | e19516d | 2009-04-28 17:15:19 | [diff] [blame] | 664 | menuState_->UpdateCommandEnabled(IDC_OPEN_FILE, true); |
[email protected] | 93e18176 | 2009-05-29 14:30:38 | [diff] [blame] | 665 | menuState_->UpdateCommandEnabled(IDC_CLEAR_BROWSING_DATA, true); |
[email protected] | fbc947b | 2009-06-19 13:28:24 | [diff] [blame] | 666 | menuState_->UpdateCommandEnabled(IDC_RESTORE_TAB, false); |
[email protected] | 91a5b3e | 2009-10-30 19:32:22 | [diff] [blame] | 667 | menuState_->UpdateCommandEnabled(IDC_FOCUS_LOCATION, true); |
[email protected] | cddc524 | 2009-12-07 18:54:33 | [diff] [blame] | 668 | menuState_->UpdateCommandEnabled(IDC_FOCUS_SEARCH, true); |
[email protected] | 91a5b3e | 2009-10-30 19:32:22 | [diff] [blame] | 669 | menuState_->UpdateCommandEnabled(IDC_SHOW_HISTORY, true); |
| 670 | menuState_->UpdateCommandEnabled(IDC_SHOW_DOWNLOADS, true); |
[email protected] | bb37e6ec | 2009-11-18 17:59:36 | [diff] [blame] | 671 | menuState_->UpdateCommandEnabled(IDC_MANAGE_EXTENSIONS, true); |
[email protected] | 1fdff70 | 2009-10-22 00:36:18 | [diff] [blame] | 672 | menuState_->UpdateCommandEnabled(IDC_HELP_PAGE, true); |
[email protected] | 4aee565 | 2009-11-19 18:59:21 | [diff] [blame] | 673 | menuState_->UpdateCommandEnabled(IDC_IMPORT_SETTINGS, true); |
[email protected] | 3896b18 | 2009-11-03 16:15:32 | [diff] [blame] | 674 | menuState_->UpdateCommandEnabled(IDC_REPORT_BUG, true); |
[email protected] | f9bc9b9 | 2009-11-24 00:55:35 | [diff] [blame] | 675 | menuState_->UpdateCommandEnabled(IDC_SYNC_BOOKMARKS, |
| 676 | ProfileSyncService::IsSyncEnabled()); |
[email protected] | ceaaf181 | 2010-01-20 22:13:09 | [diff] [blame] | 677 | menuState_->UpdateCommandEnabled(IDC_TASK_MANAGER, true); |
[email protected] | 88d7494 | 2009-01-21 22:04:44 | [diff] [blame] | 678 | } |
[email protected] | 1bcdb53 | 2009-01-16 17:47:57 | [diff] [blame] | 679 | |
[email protected] | 3f34599d | 2009-03-25 22:11:43 | [diff] [blame] | 680 | - (Profile*)defaultProfile { |
[email protected] | 3f34599d | 2009-03-25 22:11:43 | [diff] [blame] | 681 | // TODO(jrg): Find a better way to get the "default" profile. |
| 682 | if (g_browser_process->profile_manager()) |
[email protected] | 065566b | 2010-01-20 20:38:26 | [diff] [blame] | 683 | return *g_browser_process->profile_manager()->begin(); |
[email protected] | 3f34599d | 2009-03-25 22:11:43 | [diff] [blame] | 684 | |
[email protected] | f631400 | 2009-04-23 01:18:13 | [diff] [blame] | 685 | return NULL; |
[email protected] | 3f34599d | 2009-03-25 22:11:43 | [diff] [blame] | 686 | } |
| 687 | |
[email protected] | 57750f82 | 2009-04-21 21:43:09 | [diff] [blame] | 688 | // Various methods to open URLs that we get in a native fashion. We use |
| 689 | // BrowserInit here because on the other platforms, URLs to open come through |
| 690 | // the ProcessSingleton, and it calls BrowserInit. It's best to bottleneck the |
| 691 | // openings through that for uniform handling. |
| 692 | |
[email protected] | 136140c | 2009-05-19 13:58:25 | [diff] [blame] | 693 | - (void)openURLs:(const std::vector<GURL>&)urls { |
[email protected] | caadfca | 2010-01-22 21:51:21 | [diff] [blame^] | 694 | // If the browser hasn't started yet, just queue up the URLs. |
| 695 | if (!startupComplete_) { |
| 696 | startupURLs_.insert(startupURLs_.end(), urls.begin(), urls.end()); |
[email protected] | 136140c | 2009-05-19 13:58:25 | [diff] [blame] | 697 | return; |
| 698 | } |
[email protected] | 57750f82 | 2009-04-21 21:43:09 | [diff] [blame] | 699 | |
[email protected] | 0e86351 | 2009-05-28 19:45:07 | [diff] [blame] | 700 | Browser* browser = BrowserList::GetLastActive(); |
| 701 | // if no browser window exists then create one with no tabs to be filled in |
| 702 | if (!browser) { |
| 703 | browser = Browser::Create([self defaultProfile]); |
[email protected] | ce560f8 | 2009-06-03 09:39:44 | [diff] [blame] | 704 | browser->window()->Show(); |
[email protected] | 0e86351 | 2009-05-28 19:45:07 | [diff] [blame] | 705 | } |
| 706 | |
[email protected] | 51343d5a | 2009-10-26 22:39:33 | [diff] [blame] | 707 | CommandLine dummy(CommandLine::ARGUMENTS_ONLY); |
[email protected] | 57750f82 | 2009-04-21 21:43:09 | [diff] [blame] | 708 | BrowserInit::LaunchWithProfile launch(std::wstring(), dummy); |
[email protected] | 0e86351 | 2009-05-28 19:45:07 | [diff] [blame] | 709 | launch.OpenURLsInBrowser(browser, false, urls); |
[email protected] | 57750f82 | 2009-04-21 21:43:09 | [diff] [blame] | 710 | } |
| 711 | |
[email protected] | 57750f82 | 2009-04-21 21:43:09 | [diff] [blame] | 712 | - (void)getUrl:(NSAppleEventDescriptor*)event |
| 713 | withReply:(NSAppleEventDescriptor*)reply { |
| 714 | NSString* urlStr = [[event paramDescriptorForKeyword:keyDirectObject] |
| 715 | stringValue]; |
| 716 | |
| 717 | GURL gurl(base::SysNSStringToUTF8(urlStr)); |
| 718 | std::vector<GURL> gurlVector; |
| 719 | gurlVector.push_back(gurl); |
| 720 | |
[email protected] | 136140c | 2009-05-19 13:58:25 | [diff] [blame] | 721 | [self openURLs:gurlVector]; |
[email protected] | 57750f82 | 2009-04-21 21:43:09 | [diff] [blame] | 722 | } |
| 723 | |
[email protected] | caadfca | 2010-01-22 21:51:21 | [diff] [blame^] | 724 | - (void)application:(NSApplication*)sender |
| 725 | openFiles:(NSArray*)filenames { |
[email protected] | 57750f82 | 2009-04-21 21:43:09 | [diff] [blame] | 726 | std::vector<GURL> gurlVector; |
[email protected] | caadfca | 2010-01-22 21:51:21 | [diff] [blame^] | 727 | for (NSString* file in filenames) { |
| 728 | GURL gurl = net::FilePathToFileURL(FilePath(base::SysNSStringToUTF8(file))); |
[email protected] | 57750f82 | 2009-04-21 21:43:09 | [diff] [blame] | 729 | gurlVector.push_back(gurl); |
| 730 | } |
[email protected] | caadfca | 2010-01-22 21:51:21 | [diff] [blame^] | 731 | if (!gurlVector.empty()) |
| 732 | [self openURLs:gurlVector]; |
| 733 | else |
| 734 | NOTREACHED() << "Nothing to open!"; |
[email protected] | 57750f82 | 2009-04-21 21:43:09 | [diff] [blame] | 735 | |
[email protected] | caadfca | 2010-01-22 21:51:21 | [diff] [blame^] | 736 | [sender replyToOpenOrPrint:NSApplicationDelegateReplySuccess]; |
[email protected] | 57750f82 | 2009-04-21 21:43:09 | [diff] [blame] | 737 | } |
[email protected] | 3f34599d | 2009-03-25 22:11:43 | [diff] [blame] | 738 | |
[email protected] | 767543d | 2009-04-30 19:23:58 | [diff] [blame] | 739 | // Called when the preferences window is closed. We use this to release the |
| 740 | // window controller. |
[email protected] | d4e1865 | 2009-11-19 21:52:16 | [diff] [blame] | 741 | - (void)prefsWindowClosed:(NSNotification*)notification { |
| 742 | NSWindow* window = [prefsController_ window]; |
| 743 | DCHECK([notification object] == window); |
| 744 | NSNotificationCenter* defaultCenter = [NSNotificationCenter defaultCenter]; |
| 745 | [defaultCenter removeObserver:self |
| 746 | name:NSWindowWillCloseNotification |
| 747 | object:window]; |
| 748 | // PreferencesWindowControllers are autoreleased in |
| 749 | // -[PreferencesWindowController windowWillClose:]. |
| 750 | prefsController_ = nil; |
[email protected] | 767543d | 2009-04-30 19:23:58 | [diff] [blame] | 751 | } |
| 752 | |
[email protected] | 3111f08b | 2009-04-30 16:01:52 | [diff] [blame] | 753 | // Show the preferences window, or bring it to the front if it's already |
| 754 | // visible. |
| 755 | - (IBAction)showPreferences:(id)sender { |
[email protected] | d989f07c | 2009-11-14 00:35:46 | [diff] [blame] | 756 | [self showPreferencesWindow:sender |
| 757 | page:OPTIONS_PAGE_DEFAULT |
| 758 | profile:[self defaultProfile]]; |
| 759 | } |
| 760 | |
| 761 | - (void)showPreferencesWindow:(id)sender |
| 762 | page:(OptionsPage)page |
| 763 | profile:(Profile*)profile { |
[email protected] | d4e1865 | 2009-11-19 21:52:16 | [diff] [blame] | 764 | if (prefsController_) { |
[email protected] | d989f07c | 2009-11-14 00:35:46 | [diff] [blame] | 765 | [prefsController_ switchToPage:page animate:YES]; |
| 766 | } else { |
[email protected] | d4e1865 | 2009-11-19 21:52:16 | [diff] [blame] | 767 | prefsController_ = |
| 768 | [[PreferencesWindowController alloc] initWithProfile:profile |
| 769 | initialPage:page]; |
[email protected] | 767543d | 2009-04-30 19:23:58 | [diff] [blame] | 770 | // Watch for a notification of when it goes away so that we can destroy |
| 771 | // the controller. |
| 772 | [[NSNotificationCenter defaultCenter] |
| 773 | addObserver:self |
| 774 | selector:@selector(prefsWindowClosed:) |
[email protected] | d4e1865 | 2009-11-19 21:52:16 | [diff] [blame] | 775 | name:NSWindowWillCloseNotification |
| 776 | object:[prefsController_ window]]; |
[email protected] | 4240438 | 2009-04-30 17:59:24 | [diff] [blame] | 777 | } |
| 778 | [prefsController_ showPreferences:sender]; |
[email protected] | 3111f08b | 2009-04-30 16:01:52 | [diff] [blame] | 779 | } |
| 780 | |
[email protected] | bde3dda | 2009-05-20 22:13:07 | [diff] [blame] | 781 | // Called when the about window is closed. We use this to release the |
| 782 | // window controller. |
[email protected] | d4e1865 | 2009-11-19 21:52:16 | [diff] [blame] | 783 | - (void)aboutWindowClosed:(NSNotification*)notification { |
| 784 | NSWindow* window = [aboutController_ window]; |
| 785 | DCHECK(window == [notification object]); |
[email protected] | bde3dda | 2009-05-20 22:13:07 | [diff] [blame] | 786 | [[NSNotificationCenter defaultCenter] |
[email protected] | 49aeee5 | 2009-10-26 19:58:13 | [diff] [blame] | 787 | removeObserver:self |
[email protected] | d4e1865 | 2009-11-19 21:52:16 | [diff] [blame] | 788 | name:NSWindowWillCloseNotification |
| 789 | object:window]; |
| 790 | // AboutWindowControllers are autoreleased in |
| 791 | // -[AboutWindowController windowWillClose:]. |
| 792 | aboutController_ = nil; |
[email protected] | bde3dda | 2009-05-20 22:13:07 | [diff] [blame] | 793 | } |
| 794 | |
| 795 | - (IBAction)orderFrontStandardAboutPanel:(id)sender { |
[email protected] | bde3dda | 2009-05-20 22:13:07 | [diff] [blame] | 796 | if (!aboutController_) { |
[email protected] | d4e1865 | 2009-11-19 21:52:16 | [diff] [blame] | 797 | aboutController_ = |
| 798 | [[AboutWindowController alloc] initWithProfile:[self defaultProfile]]; |
[email protected] | 49aeee5 | 2009-10-26 19:58:13 | [diff] [blame] | 799 | |
[email protected] | bde3dda | 2009-05-20 22:13:07 | [diff] [blame] | 800 | // Watch for a notification of when it goes away so that we can destroy |
| 801 | // the controller. |
| 802 | [[NSNotificationCenter defaultCenter] |
| 803 | addObserver:self |
| 804 | selector:@selector(aboutWindowClosed:) |
[email protected] | d4e1865 | 2009-11-19 21:52:16 | [diff] [blame] | 805 | name:NSWindowWillCloseNotification |
| 806 | object:[aboutController_ window]]; |
[email protected] | bde3dda | 2009-05-20 22:13:07 | [diff] [blame] | 807 | } |
[email protected] | 49aeee5 | 2009-10-26 19:58:13 | [diff] [blame] | 808 | |
[email protected] | bde3dda | 2009-05-20 22:13:07 | [diff] [blame] | 809 | [aboutController_ showWindow:self]; |
[email protected] | bde3dda | 2009-05-20 22:13:07 | [diff] [blame] | 810 | } |
| 811 | |
[email protected] | 3d4bd23f | 2009-10-06 03:58:38 | [diff] [blame] | 812 | // Explicitly bring to the foreground when creating new windows from the dock. |
| 813 | - (void)newWindowFromDock:(id)sender { |
| 814 | [NSApp activateIgnoringOtherApps:YES]; |
| 815 | [self commandDispatch:sender]; |
| 816 | } |
| 817 | |
[email protected] | 288bfcd3 | 2009-09-14 18:14:46 | [diff] [blame] | 818 | - (NSMenu*)applicationDockMenu:(id)sender { |
[email protected] | 018a396 | 2009-09-17 22:23:44 | [diff] [blame] | 819 | NSMenu* dockMenu = [[[NSMenu alloc] initWithTitle: @""] autorelease]; |
| 820 | NSString* titleStr = l10n_util::GetNSStringWithFixup(IDS_NEW_WINDOW_MAC); |
| 821 | scoped_nsobject<NSMenuItem> item([[NSMenuItem alloc] |
| 822 | initWithTitle:titleStr |
[email protected] | 3d4bd23f | 2009-10-06 03:58:38 | [diff] [blame] | 823 | action:@selector(newWindowFromDock:) |
[email protected] | 018a396 | 2009-09-17 22:23:44 | [diff] [blame] | 824 | keyEquivalent:@""]); |
[email protected] | 288bfcd3 | 2009-09-14 18:14:46 | [diff] [blame] | 825 | [item setTarget:self]; |
| 826 | [item setTag:IDC_NEW_WINDOW]; |
[email protected] | 018a396 | 2009-09-17 22:23:44 | [diff] [blame] | 827 | [dockMenu addItem:item]; |
[email protected] | 288bfcd3 | 2009-09-14 18:14:46 | [diff] [blame] | 828 | |
| 829 | titleStr = l10n_util::GetNSStringWithFixup(IDS_NEW_INCOGNITO_WINDOW_MAC); |
[email protected] | 018a396 | 2009-09-17 22:23:44 | [diff] [blame] | 830 | item.reset([[NSMenuItem alloc] initWithTitle:titleStr |
[email protected] | 3d4bd23f | 2009-10-06 03:58:38 | [diff] [blame] | 831 | action:@selector(newWindowFromDock:) |
[email protected] | 018a396 | 2009-09-17 22:23:44 | [diff] [blame] | 832 | keyEquivalent:@""]); |
[email protected] | 288bfcd3 | 2009-09-14 18:14:46 | [diff] [blame] | 833 | [item setTarget:self]; |
| 834 | [item setTag:IDC_NEW_INCOGNITO_WINDOW]; |
[email protected] | 018a396 | 2009-09-17 22:23:44 | [diff] [blame] | 835 | [dockMenu addItem:item]; |
[email protected] | 288bfcd3 | 2009-09-14 18:14:46 | [diff] [blame] | 836 | |
[email protected] | 018a396 | 2009-09-17 22:23:44 | [diff] [blame] | 837 | return dockMenu; |
[email protected] | 288bfcd3 | 2009-09-14 18:14:46 | [diff] [blame] | 838 | } |
| 839 | |
[email protected] | caadfca | 2010-01-22 21:51:21 | [diff] [blame^] | 840 | - (const std::vector<GURL>&)startupURLs { |
| 841 | return startupURLs_; |
| 842 | } |
| 843 | |
| 844 | - (void)clearStartupURLs { |
| 845 | startupURLs_.clear(); |
| 846 | } |
| 847 | |
[email protected] | 1bcdb53 | 2009-01-16 17:47:57 | [diff] [blame] | 848 | @end |
[email protected] | 60ad3e2 | 2009-09-18 21:07:19 | [diff] [blame] | 849 | |
| 850 | //--------------------------------------------------------------------------- |
| 851 | |
[email protected] | 60ad3e2 | 2009-09-18 21:07:19 | [diff] [blame] | 852 | void ShowOptionsWindow(OptionsPage page, |
| 853 | OptionsGroup highlight_group, |
| 854 | Profile* profile) { |
[email protected] | d989f07c | 2009-11-14 00:35:46 | [diff] [blame] | 855 | // TODO(akalin): Use highlight_group. |
| 856 | AppController* appController = [NSApp delegate]; |
| 857 | [appController showPreferencesWindow:nil page:page profile:profile]; |
[email protected] | 60ad3e2 | 2009-09-18 21:07:19 | [diff] [blame] | 858 | } |
[email protected] | 216e904 | 2009-10-29 17:05:23 | [diff] [blame] | 859 | |
| 860 | namespace app_controller_mac { |
| 861 | |
| 862 | bool IsOpeningNewWindow() { |
| 863 | return g_is_opening_new_window; |
| 864 | } |
| 865 | |
| 866 | } // namespace app_controller_mac |