[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] | 09729a55 | 2009-08-03 23:21:41 | [diff] [blame] | 7 | #include "app/l10n_util.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] | 1c84c82c | 2009-07-27 15:37:25 | [diff] [blame] | 21 | #import "chrome/browser/cocoa/history_menu_bridge.h" |
[email protected] | 93e18176 | 2009-05-29 14:30:38 | [diff] [blame] | 22 | #import "chrome/browser/cocoa/clear_browsing_data_controller.h" |
[email protected] | 2bcec61 | 2009-05-14 17:50:53 | [diff] [blame] | 23 | #import "chrome/browser/cocoa/encoding_menu_controller_delegate_mac.h" |
[email protected] | 4240438 | 2009-04-30 17:59:24 | [diff] [blame] | 24 | #import "chrome/browser/cocoa/preferences_window_controller.h" |
[email protected] | 449dd2f | 2009-05-27 13:04:00 | [diff] [blame] | 25 | #import "chrome/browser/cocoa/tab_strip_controller.h" |
| 26 | #import "chrome/browser/cocoa/tab_window_controller.h" |
[email protected] | 2c47bc1 | 2009-04-10 20:14:00 | [diff] [blame] | 27 | #include "chrome/browser/command_updater.h" |
[email protected] | 09729a55 | 2009-08-03 23:21:41 | [diff] [blame] | 28 | #include "chrome/browser/download/download_manager.h" |
[email protected] | fbc947b | 2009-06-19 13:28:24 | [diff] [blame] | 29 | #include "chrome/browser/sessions/tab_restore_service.h" |
[email protected] | cd63ef6 | 2009-05-06 19:41:37 | [diff] [blame] | 30 | #include "chrome/common/pref_names.h" |
| 31 | #include "chrome/common/pref_service.h" |
[email protected] | 2c47bc1 | 2009-04-10 20:14:00 | [diff] [blame] | 32 | #include "chrome/browser/profile_manager.h" |
| 33 | #include "chrome/common/temp_scaffolding_stubs.h" |
[email protected] | 09729a55 | 2009-08-03 23:21:41 | [diff] [blame] | 34 | #include "grit/generated_resources.h" |
[email protected] | b0a2f0c | 2009-07-22 14:04:27 | [diff] [blame] | 35 | #import "xib_localizers/main_menu_localizer.h" |
[email protected] | 88d7494 | 2009-01-21 22:04:44 | [diff] [blame] | 36 | |
| 37 | @interface AppController(PRIVATE) |
| 38 | - (void)initMenuState; |
[email protected] | 136140c | 2009-05-19 13:58:25 | [diff] [blame] | 39 | - (void)openURLs:(const std::vector<GURL>&)urls; |
| 40 | - (void)openPendingURLs; |
[email protected] | 57750f82 | 2009-04-21 21:43:09 | [diff] [blame] | 41 | - (void)getUrl:(NSAppleEventDescriptor*)event |
| 42 | withReply:(NSAppleEventDescriptor*)reply; |
[email protected] | 36fe18f | 2009-04-29 20:26:20 | [diff] [blame] | 43 | - (void)openFiles:(NSAppleEventDescriptor*)event |
| 44 | withReply:(NSAppleEventDescriptor*)reply; |
[email protected] | 449dd2f | 2009-05-27 13:04:00 | [diff] [blame] | 45 | - (void)windowLayeringDidChange:(NSNotification*)inNotification; |
[email protected] | 73f5b36 | 2009-08-10 23:58:21 | [diff] [blame^] | 46 | - (BOOL)userWillWaitForInProgressDownloads:(int)downloadCount; |
[email protected] | 09729a55 | 2009-08-03 23:21:41 | [diff] [blame] | 47 | - (BOOL)shouldQuitWithInProgressDownloads; |
[email protected] | 88d7494 | 2009-01-21 22:04:44 | [diff] [blame] | 48 | @end |
[email protected] | 1bcdb53 | 2009-01-16 17:47:57 | [diff] [blame] | 49 | |
| 50 | @implementation AppController |
| 51 | |
[email protected] | cd63ef6 | 2009-05-06 19:41:37 | [diff] [blame] | 52 | // This method is called very early in application startup (ie, before |
| 53 | // the profile is loaded or any preferences have been registered). Defer any |
| 54 | // user-data initialization until -applicationDidFinishLaunching:. |
[email protected] | 88d7494 | 2009-01-21 22:04:44 | [diff] [blame] | 55 | - (void)awakeFromNib { |
[email protected] | 136140c | 2009-05-19 13:58:25 | [diff] [blame] | 56 | pendingURLs_.reset(new std::vector<GURL>()); |
[email protected] | 88d7494 | 2009-01-21 22:04:44 | [diff] [blame] | 57 | |
[email protected] | 136140c | 2009-05-19 13:58:25 | [diff] [blame] | 58 | // We need to register the handlers early to catch events fired on launch. |
[email protected] | 57750f82 | 2009-04-21 21:43:09 | [diff] [blame] | 59 | NSAppleEventManager* em = [NSAppleEventManager sharedAppleEventManager]; |
| 60 | [em setEventHandler:self |
| 61 | andSelector:@selector(getUrl:withReply:) |
| 62 | forEventClass:kInternetEventClass |
| 63 | andEventID:kAEGetURL]; |
| 64 | [em setEventHandler:self |
| 65 | andSelector:@selector(getUrl:withReply:) |
| 66 | forEventClass:'WWW!' // A particularly ancient AppleEvent that dates |
| 67 | andEventID:'OURL']; // back to the Spyglass days. |
[email protected] | 36fe18f | 2009-04-29 20:26:20 | [diff] [blame] | 68 | [em setEventHandler:self |
| 69 | andSelector:@selector(openFiles:withReply:) |
| 70 | forEventClass:kCoreEventClass |
| 71 | andEventID:kAEOpenDocuments]; |
[email protected] | cd63ef6 | 2009-05-06 19:41:37 | [diff] [blame] | 72 | |
[email protected] | 449dd2f | 2009-05-27 13:04:00 | [diff] [blame] | 73 | // Register for various window layering changes. We use these to update |
| 74 | // various UI elements (command-key equivalents, etc) when the frontmost |
| 75 | // window changes. |
| 76 | NSNotificationCenter* notificationCenter = |
| 77 | [NSNotificationCenter defaultCenter]; |
[email protected] | ce560f8 | 2009-06-03 09:39:44 | [diff] [blame] | 78 | [notificationCenter |
[email protected] | 449dd2f | 2009-05-27 13:04:00 | [diff] [blame] | 79 | addObserver:self |
| 80 | selector:@selector(windowLayeringDidChange:) |
| 81 | name:NSWindowDidBecomeKeyNotification |
| 82 | object:nil]; |
| 83 | [notificationCenter |
| 84 | addObserver:self |
| 85 | selector:@selector(windowLayeringDidChange:) |
| 86 | name:NSWindowDidResignKeyNotification |
| 87 | object:nil]; |
| 88 | [notificationCenter |
| 89 | addObserver:self |
| 90 | selector:@selector(windowLayeringDidChange:) |
| 91 | name:NSWindowDidBecomeMainNotification |
| 92 | object:nil]; |
| 93 | [notificationCenter |
| 94 | addObserver:self |
| 95 | selector:@selector(windowLayeringDidChange:) |
| 96 | name:NSWindowDidResignMainNotification |
| 97 | object:nil]; |
| 98 | |
| 99 | // Register for a notification that the number of tabs changes in windows |
| 100 | // so we can adjust the close tab/window command keys. |
| 101 | [notificationCenter |
| 102 | addObserver:self |
| 103 | selector:@selector(tabsChanged:) |
| 104 | name:kTabStripNumberOfTabsChanged |
| 105 | object:nil]; |
| 106 | |
[email protected] | 136140c | 2009-05-19 13:58:25 | [diff] [blame] | 107 | // Set up the command updater for when there are no windows open |
| 108 | [self initMenuState]; |
| 109 | } |
| 110 | |
[email protected] | 449dd2f | 2009-05-27 13:04:00 | [diff] [blame] | 111 | // Called when the app is shutting down. Clean-up as appropriate. |
| 112 | - (void)applicationWillTerminate:(NSNotification *)aNotification { |
[email protected] | fbc947b | 2009-06-19 13:28:24 | [diff] [blame] | 113 | DCHECK(!BrowserList::HasBrowserWithProfile([self defaultProfile])); |
| 114 | if (!BrowserList::HasBrowserWithProfile([self defaultProfile])) { |
| 115 | // As we're shutting down, we need to nuke the TabRestoreService, which will |
| 116 | // start the shutdown of the NavigationControllers and allow for proper |
| 117 | // shutdown. If we don't do this chrome won't shutdown cleanly, and may end |
| 118 | // up crashing when some thread tries to use the IO thread (or another |
| 119 | // thread) that is no longer valid. |
| 120 | [self defaultProfile]->ResetTabRestoreService(); |
| 121 | } |
| 122 | |
[email protected] | 449dd2f | 2009-05-27 13:04:00 | [diff] [blame] | 123 | [[NSNotificationCenter defaultCenter] removeObserver:self]; |
| 124 | } |
| 125 | |
| 126 | // Helper routine to get the window controller if the key window is a tabbed |
| 127 | // window, or nil if not. Examples of non-tabbed windows are "about" or |
| 128 | // "preferences". |
| 129 | - (TabWindowController*)keyWindowTabController { |
| 130 | NSWindowController* keyWindowController = |
| 131 | [[[NSApplication sharedApplication] keyWindow] windowController]; |
| 132 | if ([keyWindowController isKindOfClass:[TabWindowController class]]) |
| 133 | return (TabWindowController*)keyWindowController; |
| 134 | |
| 135 | return nil; |
| 136 | } |
| 137 | |
| 138 | // If the window has tabs, make "close window" be cmd-shift-w, otherwise leave |
| 139 | // it as the normal cmd-w. Capitalization of the key equivalent affects whether |
| 140 | // the shift modifer is used. |
| 141 | - (void)adjustCloseWindowMenuItemKeyEquivalent:(BOOL)inHaveTabs { |
| 142 | [closeWindowMenuItem_ setKeyEquivalent:(inHaveTabs ? @"W" : @"w")]; |
| 143 | } |
| 144 | |
| 145 | // If the window has tabs, make "close tab" take over cmd-w, otherwise it |
| 146 | // shouldn't have any key-equivalent because it should be disabled. |
| 147 | - (void)adjustCloseTabMenuItemKeyEquivalent:(BOOL)hasTabs { |
| 148 | if (hasTabs) { |
| 149 | [closeTabMenuItem_ setKeyEquivalent:@"w"]; |
| 150 | [closeTabMenuItem_ setKeyEquivalentModifierMask:NSCommandKeyMask]; |
| 151 | } else { |
| 152 | [closeTabMenuItem_ setKeyEquivalent:@""]; |
| 153 | [closeTabMenuItem_ setKeyEquivalentModifierMask:0]; |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | // See if we have a window with tabs open, and adjust the key equivalents for |
| 158 | // Close Tab/Close Window accordingly |
| 159 | - (void)fixCloseMenuItemKeyEquivalents { |
| 160 | TabWindowController* tabController = [self keyWindowTabController]; |
| 161 | BOOL windowWithMultipleTabs = |
| 162 | (tabController && [tabController numberOfTabs] > 1); |
| 163 | [self adjustCloseWindowMenuItemKeyEquivalent:windowWithMultipleTabs]; |
| 164 | [self adjustCloseTabMenuItemKeyEquivalent:windowWithMultipleTabs]; |
| 165 | fileMenuUpdatePending_ = NO; |
| 166 | } |
| 167 | |
| 168 | // Fix up the "close tab/close window" command-key equivalents. We do this |
| 169 | // after a delay to ensure that window layer state has been set by the time |
| 170 | // we do the enabling. |
| 171 | - (void)delayedFixCloseMenuItemKeyEquivalents { |
| 172 | if (!fileMenuUpdatePending_) { |
| 173 | [self performSelector:@selector(fixCloseMenuItemKeyEquivalents) |
| 174 | withObject:nil |
| 175 | afterDelay:0]; |
| 176 | fileMenuUpdatePending_ = YES; |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | // Called when we get a notification about the window layering changing to |
| 181 | // update the UI based on the new main window. |
| 182 | - (void)windowLayeringDidChange:(NSNotification*)notify { |
| 183 | [self delayedFixCloseMenuItemKeyEquivalents]; |
[email protected] | ce560f8 | 2009-06-03 09:39:44 | [diff] [blame] | 184 | |
[email protected] | 449dd2f | 2009-05-27 13:04:00 | [diff] [blame] | 185 | // TODO(pinkerton): If we have other things here, such as inspector panels |
| 186 | // that follow the contents of the selected webpage, we would update those |
| 187 | // here. |
| 188 | } |
| 189 | |
| 190 | // Called when the number of tabs changes in one of the browser windows. The |
| 191 | // object is the tab strip controller, but we don't currently care. |
| 192 | - (void)tabsChanged:(NSNotification*)notify { |
| 193 | [self delayedFixCloseMenuItemKeyEquivalents]; |
| 194 | } |
| 195 | |
[email protected] | 414fde59 | 2009-05-21 16:14:43 | [diff] [blame] | 196 | // If the auto-update interval is not set, make it 5 hours. |
| 197 | // This code is specific to Mac Chrome Dev Channel. |
| 198 | // Placed here for 2 reasons: |
| 199 | // 1) Same spot as other Pref stuff |
| 200 | // 2) Try and be friendly by keeping this after app launch |
| 201 | // TODO(jrg): remove once we go Beta. |
| 202 | - (void)setUpdateCheckInterval { |
| 203 | #if defined(GOOGLE_CHROME_BUILD) |
| 204 | CFStringRef app = (CFStringRef)@"com.google.Keystone.Agent"; |
| 205 | CFStringRef checkInterval = (CFStringRef)@"checkInterval"; |
| 206 | CFPropertyListRef plist = CFPreferencesCopyAppValue(checkInterval, app); |
| 207 | if (!plist) { |
| 208 | const float fiveHoursInSeconds = 5.0 * 60.0 * 60.0; |
| 209 | NSNumber *value = [NSNumber numberWithFloat:fiveHoursInSeconds]; |
| 210 | CFPreferencesSetAppValue(checkInterval, value, app); |
| 211 | CFPreferencesAppSynchronize(app); |
| 212 | } |
| 213 | #endif |
| 214 | } |
| 215 | |
[email protected] | 136140c | 2009-05-19 13:58:25 | [diff] [blame] | 216 | // This is called after profiles have been loaded and preferences registered. |
| 217 | // It is safe to access the default profile here. |
| 218 | - (void)applicationDidFinishLaunching:(NSNotification*)notify { |
| 219 | // Hold an extra ref to the BrowserProcess singleton so it doesn't go away |
| 220 | // when all the browser windows get closed. We'll release it on quit which |
| 221 | // will be the signal to exit. |
| 222 | DCHECK(g_browser_process); |
| 223 | g_browser_process->AddRefModule(); |
| 224 | |
[email protected] | b0a2f0c | 2009-07-22 14:04:27 | [diff] [blame] | 225 | // TODO: move this into the MainMenu.xib once we clean up the startup order |
| 226 | // dependencies so that works. http://crbug.com/17380 |
[email protected] | 7b22524 | 2009-05-20 17:59:34 | [diff] [blame] | 227 | // Create the localizer for the main menu. We can't do this in the nib |
| 228 | // because it's too early. Do it before we create any bookmark menus as well, |
| 229 | // just in case one has a title that matches any of our strings (unlikely, |
| 230 | // but technically possible). |
[email protected] | b0a2f0c | 2009-07-22 14:04:27 | [diff] [blame] | 231 | scoped_nsobject<MainMenuLocalizer> localizer( |
| 232 | [[MainMenuLocalizer alloc] initWithBundle:nil]); |
[email protected] | 7b22524 | 2009-05-20 17:59:34 | [diff] [blame] | 233 | [localizer localizeObject:[NSApplication sharedApplication] |
| 234 | recursively:YES]; |
| 235 | |
[email protected] | cd63ef6 | 2009-05-06 19:41:37 | [diff] [blame] | 236 | bookmarkMenuBridge_.reset(new BookmarkMenuBridge()); |
[email protected] | 1c84c82c | 2009-07-27 15:37:25 | [diff] [blame] | 237 | historyMenuBridge_.reset(new HistoryMenuBridge([self defaultProfile])); |
[email protected] | cd63ef6 | 2009-05-06 19:41:37 | [diff] [blame] | 238 | |
[email protected] | 414fde59 | 2009-05-21 16:14:43 | [diff] [blame] | 239 | [self setUpdateCheckInterval]; |
[email protected] | 2bcec61 | 2009-05-14 17:50:53 | [diff] [blame] | 240 | |
| 241 | // Build up the encoding menu, the order of the items differs based on the |
| 242 | // current locale (see http://crbug.com/7647 for details). |
| 243 | // We need a valid g_browser_process to get the profile which is why we can't |
| 244 | // call this from awakeFromNib. |
| 245 | EncodingMenuControllerDelegate::BuildEncodingMenu([self defaultProfile]); |
| 246 | |
[email protected] | 136140c | 2009-05-19 13:58:25 | [diff] [blame] | 247 | // Now that we're initialized we can open any URLs we've been holding onto. |
| 248 | [self openPendingURLs]; |
[email protected] | 7c32108 | 2009-02-09 15:35:47 | [diff] [blame] | 249 | } |
| 250 | |
[email protected] | 73f5b36 | 2009-08-10 23:58:21 | [diff] [blame^] | 251 | // Helper function for populating and displaying the in progress downloads at |
| 252 | // exit alert panel. |
| 253 | - (BOOL)userWillWaitForInProgressDownloads:(int)downloadCount { |
| 254 | NSString* descriptionText = nil; |
| 255 | NSString* waitTitle = nil; |
| 256 | NSString* exitTitle = nil; |
| 257 | |
| 258 | // Set the dialog text based on whether or not there are multiple downloads. |
| 259 | if (downloadCount == 1) { |
| 260 | // Dialog text. |
| 261 | descriptionText = |
| 262 | base::SysWideToNSString( |
| 263 | l10n_util::GetString(IDS_SINGLE_DOWNLOAD_REMOVE_CONFIRM_TITLE)); |
| 264 | |
| 265 | // Cancel download and exit button text. |
| 266 | exitTitle = |
| 267 | base::SysWideToNSString( |
| 268 | l10n_util::GetString( |
| 269 | IDS_SINGLE_DOWNLOAD_REMOVE_CONFIRM_OK_BUTTON_LABEL)); |
| 270 | |
| 271 | // Wait for download button text. |
| 272 | waitTitle = |
| 273 | base::SysWideToNSString( |
| 274 | l10n_util::GetString( |
| 275 | IDS_SINGLE_DOWNLOAD_REMOVE_CONFIRM_CANCEL_BUTTON_LABEL)); |
| 276 | } else { |
| 277 | // Dialog text. |
| 278 | descriptionText = |
| 279 | base::SysWideToNSString( |
| 280 | l10n_util::GetStringF(IDS_MULTIPLE_DOWNLOADS_REMOVE_CONFIRM_TITLE, |
| 281 | downloadCount)); |
| 282 | |
| 283 | // Cancel downloads and exit button text. |
| 284 | exitTitle = |
| 285 | base::SysWideToNSString( |
| 286 | l10n_util::GetString( |
| 287 | IDS_MULTIPLE_DOWNLOADS_REMOVE_CONFIRM_OK_BUTTON_LABEL)); |
| 288 | |
| 289 | // Wait for downloads button text. |
| 290 | waitTitle = |
| 291 | base::SysWideToNSString( |
| 292 | l10n_util::GetString( |
| 293 | IDS_MULTIPLE_DOWNLOADS_REMOVE_CONFIRM_CANCEL_BUTTON_LABEL)); |
| 294 | } |
| 295 | |
| 296 | // 'waitButton' is the default choice. |
| 297 | int choice = NSRunAlertPanel(nil, descriptionText, waitTitle, exitTitle, nil); |
| 298 | return choice == NSAlertDefaultReturn ? YES : NO; |
| 299 | } |
| 300 | |
| 301 | // 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] | 302 | // user to see if we should continue to exit (and thus cancel the downloads), or |
| 303 | // if we should wait. |
| 304 | - (BOOL)shouldQuitWithInProgressDownloads { |
[email protected] | 73f5b36 | 2009-08-10 23:58:21 | [diff] [blame^] | 305 | ProfileManager* profile_manager = g_browser_process->profile_manager(); |
| 306 | if (!profile_manager) |
| 307 | return YES; |
[email protected] | 09729a55 | 2009-08-03 23:21:41 | [diff] [blame] | 308 | |
[email protected] | 73f5b36 | 2009-08-10 23:58:21 | [diff] [blame^] | 309 | ProfileManager::const_iterator it = profile_manager->begin(); |
| 310 | for (; it != profile_manager->end(); ++it) { |
| 311 | Profile* profile = *it; |
[email protected] | 09729a55 | 2009-08-03 23:21:41 | [diff] [blame] | 312 | DownloadManager* download_manager = profile->GetDownloadManager(); |
[email protected] | 73f5b36 | 2009-08-10 23:58:21 | [diff] [blame^] | 313 | if (download_manager && download_manager->in_progress_count() > 0) { |
| 314 | int downloadCount = download_manager->in_progress_count(); |
| 315 | if ([self userWillWaitForInProgressDownloads:downloadCount]) { |
| 316 | // Create a new browser window (if necessary) and navigate to the |
| 317 | // downloads page if the user chooses to wait. |
| 318 | Browser* browser = BrowserList::FindBrowserWithProfile(profile); |
| 319 | if (!browser) { |
| 320 | browser = Browser::Create(profile); |
| 321 | browser->window()->Show(); |
| 322 | } |
| 323 | DCHECK(browser); |
| 324 | browser->ShowDownloadsTab(); |
| 325 | return NO; |
| 326 | } |
[email protected] | 09729a55 | 2009-08-03 23:21:41 | [diff] [blame] | 327 | |
[email protected] | 73f5b36 | 2009-08-10 23:58:21 | [diff] [blame^] | 328 | // User wants to exit. |
| 329 | return YES; |
[email protected] | 09729a55 | 2009-08-03 23:21:41 | [diff] [blame] | 330 | } |
[email protected] | 09729a55 | 2009-08-03 23:21:41 | [diff] [blame] | 331 | } |
| 332 | |
[email protected] | 73f5b36 | 2009-08-10 23:58:21 | [diff] [blame^] | 333 | // No profiles or active downloads found, okay to exit. |
[email protected] | 09729a55 | 2009-08-03 23:21:41 | [diff] [blame] | 334 | return YES; |
| 335 | } |
| 336 | |
[email protected] | 983f0fc8 | 2009-01-27 16:28:44 | [diff] [blame] | 337 | // We can't use the standard terminate: method because it will abruptly exit |
[email protected] | 1bcdb53 | 2009-01-16 17:47:57 | [diff] [blame] | 338 | // the app and leave things on the stack in an unfinalized state. We need to |
| 339 | // post a quit message to our run loop so the stack can gracefully unwind. |
[email protected] | f0a51fb5 | 2009-03-05 12:46:38 | [diff] [blame] | 340 | - (IBAction)quit:(id)sender { |
[email protected] | 1bcdb53 | 2009-01-16 17:47:57 | [diff] [blame] | 341 | // TODO(pinkerton): |
| 342 | // since we have to roll it ourselves, ask the delegate (ourselves, really) |
| 343 | // if we should terminate. For example, we might not want to if the user |
| 344 | // has ongoing downloads or multiple windows/tabs open. However, this would |
[email protected] | f0a51fb5 | 2009-03-05 12:46:38 | [diff] [blame] | 345 | // require posting UI and may require spinning up another run loop to |
[email protected] | 1bcdb53 | 2009-01-16 17:47:57 | [diff] [blame] | 346 | // handle it. If it says to continue, post the quit message, otherwise |
| 347 | // go back to normal. |
[email protected] | 8a53ee04 | 2009-01-21 16:41:33 | [diff] [blame] | 348 | |
[email protected] | 09729a55 | 2009-08-03 23:21:41 | [diff] [blame] | 349 | // Check for in-progress downloads, and prompt the user if they really want to |
| 350 | // quit (and thus cancel the downloads). |
| 351 | if (![self shouldQuitWithInProgressDownloads]) |
| 352 | return; |
| 353 | |
[email protected] | 57750f82 | 2009-04-21 21:43:09 | [diff] [blame] | 354 | NSAppleEventManager* em = [NSAppleEventManager sharedAppleEventManager]; |
| 355 | [em removeEventHandlerForEventClass:kInternetEventClass |
| 356 | andEventID:kAEGetURL]; |
| 357 | [em removeEventHandlerForEventClass:'WWW!' |
| 358 | andEventID:'OURL']; |
[email protected] | 36fe18f | 2009-04-29 20:26:20 | [diff] [blame] | 359 | [em removeEventHandlerForEventClass:kCoreEventClass |
| 360 | andEventID:kAEOpenDocuments]; |
[email protected] | 57750f82 | 2009-04-21 21:43:09 | [diff] [blame] | 361 | |
[email protected] | 41741a96 | 2009-02-18 21:51:39 | [diff] [blame] | 362 | // TODO(pinkerton): Not sure where this should live, including it here |
| 363 | // causes all sorts of asserts from the open renderers. On Windows, it |
| 364 | // lives in Browser::OnWindowClosing, but that's not appropriate on Mac |
| 365 | // since we don't shut down when we reach zero windows. |
| 366 | // browser_shutdown::OnShutdownStarting(browser_shutdown::WINDOW_CLOSE); |
| 367 | |
[email protected] | 8a53ee04 | 2009-01-21 16:41:33 | [diff] [blame] | 368 | // Close all the windows. |
[email protected] | 7c32108 | 2009-02-09 15:35:47 | [diff] [blame] | 369 | BrowserList::CloseAllBrowsers(true); |
[email protected] | f0a51fb5 | 2009-03-05 12:46:38 | [diff] [blame] | 370 | |
[email protected] | 7c32108 | 2009-02-09 15:35:47 | [diff] [blame] | 371 | // Release the reference to the browser process. Once all the browsers get |
| 372 | // dealloc'd, it will stop the RunLoop and fall back into main(). |
| 373 | g_browser_process->ReleaseModule(); |
[email protected] | 1bcdb53 | 2009-01-16 17:47:57 | [diff] [blame] | 374 | } |
| 375 | |
[email protected] | fbc947b | 2009-06-19 13:28:24 | [diff] [blame] | 376 | // Called to determine if we should enable the "restore tab" menu item. |
| 377 | // Checks with the TabRestoreService to see if there's anything there to |
| 378 | // restore and returns YES if so. |
| 379 | - (BOOL)canRestoreTab { |
| 380 | TabRestoreService* service = [self defaultProfile]->GetTabRestoreService(); |
| 381 | return service && !service->entries().empty(); |
| 382 | } |
| 383 | |
[email protected] | 88d7494 | 2009-01-21 22:04:44 | [diff] [blame] | 384 | // Called to validate menu items when there are no key windows. All the |
| 385 | // items we care about have been set with the |commandDispatch:| action and |
| 386 | // a target of FirstResponder in IB. If it's not one of those, let it |
| 387 | // continue up the responder chain to be handled elsewhere. We pull out the |
| 388 | // tag as the cross-platform constant to differentiate and dispatch the |
| 389 | // various commands. |
| 390 | - (BOOL)validateUserInterfaceItem:(id<NSValidatedUserInterfaceItem>)item { |
| 391 | SEL action = [item action]; |
| 392 | BOOL enable = NO; |
| 393 | if (action == @selector(commandDispatch:)) { |
| 394 | NSInteger tag = [item tag]; |
[email protected] | fbc947b | 2009-06-19 13:28:24 | [diff] [blame] | 395 | if (menuState_->SupportsCommand(tag)) { |
| 396 | switch (tag) { |
| 397 | case IDC_RESTORE_TAB: |
| 398 | enable = [self canRestoreTab]; |
| 399 | break; |
| 400 | default: |
| 401 | enable = menuState_->IsCommandEnabled(tag) ? YES : NO; |
| 402 | } |
| 403 | } |
[email protected] | 88d7494 | 2009-01-21 22:04:44 | [diff] [blame] | 404 | } else if (action == @selector(quit:)) { |
| 405 | enable = YES; |
[email protected] | 3111f08b | 2009-04-30 16:01:52 | [diff] [blame] | 406 | } else if (action == @selector(showPreferences:)) { |
| 407 | enable = YES; |
[email protected] | bde3dda | 2009-05-20 22:13:07 | [diff] [blame] | 408 | } else if (action == @selector(orderFrontStandardAboutPanel:)) { |
| 409 | enable = YES; |
[email protected] | 88d7494 | 2009-01-21 22:04:44 | [diff] [blame] | 410 | } |
| 411 | return enable; |
| 412 | } |
| 413 | |
| 414 | // Called when the user picks a menu item when there are no key windows. Calls |
| 415 | // through to the browser object to execute the command. This assumes that the |
| 416 | // command is supported and doesn't check, otherwise it would have been disabled |
| 417 | // in the UI in validateUserInterfaceItem:. |
| 418 | - (void)commandDispatch:(id)sender { |
[email protected] | 93e18176 | 2009-05-29 14:30:38 | [diff] [blame] | 419 | Profile* defaultProfile = [self defaultProfile]; |
[email protected] | f0a51fb5 | 2009-03-05 12:46:38 | [diff] [blame] | 420 | |
[email protected] | 88d7494 | 2009-01-21 22:04:44 | [diff] [blame] | 421 | NSInteger tag = [sender tag]; |
| 422 | switch (tag) { |
[email protected] | fbc947b | 2009-06-19 13:28:24 | [diff] [blame] | 423 | case IDC_NEW_TAB: |
[email protected] | 88d7494 | 2009-01-21 22:04:44 | [diff] [blame] | 424 | case IDC_NEW_WINDOW: |
[email protected] | 93e18176 | 2009-05-29 14:30:38 | [diff] [blame] | 425 | Browser::OpenEmptyWindow(defaultProfile); |
[email protected] | 88d7494 | 2009-01-21 22:04:44 | [diff] [blame] | 426 | break; |
[email protected] | 863ff66 | 2009-01-26 20:18:18 | [diff] [blame] | 427 | case IDC_NEW_INCOGNITO_WINDOW: |
[email protected] | 93e18176 | 2009-05-29 14:30:38 | [diff] [blame] | 428 | Browser::OpenURLOffTheRecord(defaultProfile, GURL()); |
[email protected] | 863ff66 | 2009-01-26 20:18:18 | [diff] [blame] | 429 | break; |
[email protected] | fbc947b | 2009-06-19 13:28:24 | [diff] [blame] | 430 | case IDC_RESTORE_TAB: |
| 431 | Browser::OpenWindowWithRestoredTabs(defaultProfile); |
| 432 | break; |
[email protected] | e19516d | 2009-04-28 17:15:19 | [diff] [blame] | 433 | case IDC_OPEN_FILE: |
[email protected] | 93e18176 | 2009-05-29 14:30:38 | [diff] [blame] | 434 | Browser::OpenEmptyWindow(defaultProfile); |
[email protected] | e19516d | 2009-04-28 17:15:19 | [diff] [blame] | 435 | BrowserList::GetLastActive()-> |
| 436 | ExecuteCommandWithDisposition(IDC_OPEN_FILE, CURRENT_TAB); |
| 437 | break; |
[email protected] | 93e18176 | 2009-05-29 14:30:38 | [diff] [blame] | 438 | case IDC_CLEAR_BROWSING_DATA: |
| 439 | // There may not be a browser open, so use the default profile. |
| 440 | scoped_nsobject<ClearBrowsingDataController> controller( |
| 441 | [[ClearBrowsingDataController alloc] |
| 442 | initWithProfile:defaultProfile]); |
| 443 | [controller runModalDialog]; |
| 444 | break; |
[email protected] | 88d7494 | 2009-01-21 22:04:44 | [diff] [blame] | 445 | }; |
| 446 | } |
| 447 | |
[email protected] | ff81e0a | 2009-04-13 14:58:54 | [diff] [blame] | 448 | // NSApplication delegate method called when someone clicks on the |
| 449 | // dock icon and there are no open windows. To match standard mac |
| 450 | // behavior, we should open a new window. |
| 451 | - (BOOL)applicationShouldHandleReopen:(NSApplication*)theApplication |
| 452 | hasVisibleWindows:(BOOL)flag { |
| 453 | // Don't do anything if there are visible windows. This will cause |
| 454 | // AppKit to unminimize the most recently minimized window. |
| 455 | if (flag) |
| 456 | return YES; |
| 457 | |
| 458 | // Otherwise open a new window. |
| 459 | Browser::OpenEmptyWindow([self defaultProfile]); |
| 460 | |
| 461 | // We've handled the reopen event, so return NO to tell AppKit not |
| 462 | // to do anything. |
| 463 | return NO; |
| 464 | } |
| 465 | |
[email protected] | 88d7494 | 2009-01-21 22:04:44 | [diff] [blame] | 466 | - (void)initMenuState { |
[email protected] | 3111f08b | 2009-04-30 16:01:52 | [diff] [blame] | 467 | menuState_.reset(new CommandUpdater(NULL)); |
[email protected] | fbc947b | 2009-06-19 13:28:24 | [diff] [blame] | 468 | menuState_->UpdateCommandEnabled(IDC_NEW_TAB, true); |
[email protected] | 88d7494 | 2009-01-21 22:04:44 | [diff] [blame] | 469 | menuState_->UpdateCommandEnabled(IDC_NEW_WINDOW, true); |
[email protected] | 863ff66 | 2009-01-26 20:18:18 | [diff] [blame] | 470 | menuState_->UpdateCommandEnabled(IDC_NEW_INCOGNITO_WINDOW, true); |
[email protected] | e19516d | 2009-04-28 17:15:19 | [diff] [blame] | 471 | menuState_->UpdateCommandEnabled(IDC_OPEN_FILE, true); |
[email protected] | 93e18176 | 2009-05-29 14:30:38 | [diff] [blame] | 472 | menuState_->UpdateCommandEnabled(IDC_CLEAR_BROWSING_DATA, true); |
[email protected] | fbc947b | 2009-06-19 13:28:24 | [diff] [blame] | 473 | menuState_->UpdateCommandEnabled(IDC_RESTORE_TAB, false); |
[email protected] | 88d7494 | 2009-01-21 22:04:44 | [diff] [blame] | 474 | // TODO(pinkerton): ...more to come... |
| 475 | } |
[email protected] | 1bcdb53 | 2009-01-16 17:47:57 | [diff] [blame] | 476 | |
[email protected] | 3f34599d | 2009-03-25 22:11:43 | [diff] [blame] | 477 | - (Profile*)defaultProfile { |
[email protected] | 3f34599d | 2009-03-25 22:11:43 | [diff] [blame] | 478 | // TODO(jrg): Find a better way to get the "default" profile. |
| 479 | if (g_browser_process->profile_manager()) |
[email protected] | 57750f82 | 2009-04-21 21:43:09 | [diff] [blame] | 480 | return* g_browser_process->profile_manager()->begin(); |
[email protected] | 3f34599d | 2009-03-25 22:11:43 | [diff] [blame] | 481 | |
[email protected] | f631400 | 2009-04-23 01:18:13 | [diff] [blame] | 482 | return NULL; |
[email protected] | 3f34599d | 2009-03-25 22:11:43 | [diff] [blame] | 483 | } |
| 484 | |
[email protected] | 57750f82 | 2009-04-21 21:43:09 | [diff] [blame] | 485 | // Various methods to open URLs that we get in a native fashion. We use |
| 486 | // BrowserInit here because on the other platforms, URLs to open come through |
| 487 | // the ProcessSingleton, and it calls BrowserInit. It's best to bottleneck the |
| 488 | // openings through that for uniform handling. |
| 489 | |
[email protected] | 136140c | 2009-05-19 13:58:25 | [diff] [blame] | 490 | - (void)openURLs:(const std::vector<GURL>&)urls { |
| 491 | if (pendingURLs_.get()) { |
| 492 | // too early to open; save for later |
| 493 | pendingURLs_->insert(pendingURLs_->end(), urls.begin(), urls.end()); |
| 494 | return; |
| 495 | } |
[email protected] | 57750f82 | 2009-04-21 21:43:09 | [diff] [blame] | 496 | |
[email protected] | 0e86351 | 2009-05-28 19:45:07 | [diff] [blame] | 497 | Browser* browser = BrowserList::GetLastActive(); |
| 498 | // if no browser window exists then create one with no tabs to be filled in |
| 499 | if (!browser) { |
| 500 | browser = Browser::Create([self defaultProfile]); |
[email protected] | ce560f8 | 2009-06-03 09:39:44 | [diff] [blame] | 501 | browser->window()->Show(); |
[email protected] | 0e86351 | 2009-05-28 19:45:07 | [diff] [blame] | 502 | } |
| 503 | |
[email protected] | 57750f82 | 2009-04-21 21:43:09 | [diff] [blame] | 504 | CommandLine dummy((std::wstring())); |
| 505 | BrowserInit::LaunchWithProfile launch(std::wstring(), dummy); |
[email protected] | 0e86351 | 2009-05-28 19:45:07 | [diff] [blame] | 506 | launch.OpenURLsInBrowser(browser, false, urls); |
[email protected] | 57750f82 | 2009-04-21 21:43:09 | [diff] [blame] | 507 | } |
| 508 | |
[email protected] | 136140c | 2009-05-19 13:58:25 | [diff] [blame] | 509 | - (void)openPendingURLs { |
| 510 | // Since the existence of pendingURLs_ is a flag that it's too early to |
| 511 | // open URLs, we need to reset pendingURLs_. |
| 512 | std::vector<GURL> urls; |
| 513 | swap(urls, *pendingURLs_); |
| 514 | pendingURLs_.reset(); |
| 515 | |
| 516 | if (urls.size()) |
| 517 | [self openURLs:urls]; |
| 518 | } |
[email protected] | 57750f82 | 2009-04-21 21:43:09 | [diff] [blame] | 519 | |
| 520 | - (void)getUrl:(NSAppleEventDescriptor*)event |
| 521 | withReply:(NSAppleEventDescriptor*)reply { |
| 522 | NSString* urlStr = [[event paramDescriptorForKeyword:keyDirectObject] |
| 523 | stringValue]; |
| 524 | |
| 525 | GURL gurl(base::SysNSStringToUTF8(urlStr)); |
| 526 | std::vector<GURL> gurlVector; |
| 527 | gurlVector.push_back(gurl); |
| 528 | |
[email protected] | 136140c | 2009-05-19 13:58:25 | [diff] [blame] | 529 | [self openURLs:gurlVector]; |
[email protected] | 57750f82 | 2009-04-21 21:43:09 | [diff] [blame] | 530 | } |
| 531 | |
[email protected] | 36fe18f | 2009-04-29 20:26:20 | [diff] [blame] | 532 | - (void)openFiles:(NSAppleEventDescriptor*)event |
| 533 | withReply:(NSAppleEventDescriptor*)reply { |
| 534 | // Ordinarily we'd use the NSApplication delegate method |
| 535 | // -application:openFiles:, but Cocoa tries to be smart and it sends files |
| 536 | // specified on the command line into that delegate method. That's too smart |
| 537 | // for us (our setup isn't done by the time Cocoa triggers the delegate method |
| 538 | // and we crash). Since all we want are files dropped on the app icon, and we |
| 539 | // have cross-platform code to handle the command-line files anyway, an Apple |
| 540 | // Event handler fits the bill just right. |
| 541 | NSAppleEventDescriptor* fileList = |
| 542 | [event paramDescriptorForKeyword:keyDirectObject]; |
| 543 | if (!fileList) |
| 544 | return; |
[email protected] | 57750f82 | 2009-04-21 21:43:09 | [diff] [blame] | 545 | std::vector<GURL> gurlVector; |
| 546 | |
[email protected] | 36fe18f | 2009-04-29 20:26:20 | [diff] [blame] | 547 | for (NSInteger i = 1; i <= [fileList numberOfItems]; ++i) { |
| 548 | NSAppleEventDescriptor* fileAliasDesc = [fileList descriptorAtIndex:i]; |
| 549 | if (!fileAliasDesc) |
| 550 | continue; |
| 551 | NSAppleEventDescriptor* fileURLDesc = |
| 552 | [fileAliasDesc coerceToDescriptorType:typeFileURL]; |
| 553 | if (!fileURLDesc) |
| 554 | continue; |
| 555 | NSData* fileURLData = [fileURLDesc data]; |
| 556 | if (!fileURLData) |
| 557 | continue; |
| 558 | GURL gurl(std::string((char*)[fileURLData bytes], [fileURLData length])); |
[email protected] | 57750f82 | 2009-04-21 21:43:09 | [diff] [blame] | 559 | gurlVector.push_back(gurl); |
| 560 | } |
| 561 | |
[email protected] | 136140c | 2009-05-19 13:58:25 | [diff] [blame] | 562 | [self openURLs:gurlVector]; |
[email protected] | 57750f82 | 2009-04-21 21:43:09 | [diff] [blame] | 563 | } |
[email protected] | 3f34599d | 2009-03-25 22:11:43 | [diff] [blame] | 564 | |
[email protected] | 767543d | 2009-04-30 19:23:58 | [diff] [blame] | 565 | // Called when the preferences window is closed. We use this to release the |
| 566 | // window controller. |
| 567 | - (void)prefsWindowClosed:(NSNotification*)notify { |
[email protected] | bde3dda | 2009-05-20 22:13:07 | [diff] [blame] | 568 | [[NSNotificationCenter defaultCenter] |
| 569 | removeObserver:self |
| 570 | name:kUserDoneEditingPrefsNotification |
| 571 | object:prefsController_.get()]; |
[email protected] | 767543d | 2009-04-30 19:23:58 | [diff] [blame] | 572 | prefsController_.reset(NULL); |
| 573 | } |
| 574 | |
[email protected] | 3111f08b | 2009-04-30 16:01:52 | [diff] [blame] | 575 | // Show the preferences window, or bring it to the front if it's already |
| 576 | // visible. |
| 577 | - (IBAction)showPreferences:(id)sender { |
[email protected] | 4240438 | 2009-04-30 17:59:24 | [diff] [blame] | 578 | if (!prefsController_.get()) { |
[email protected] | 4240438 | 2009-04-30 17:59:24 | [diff] [blame] | 579 | prefsController_.reset([[PreferencesWindowController alloc] |
[email protected] | cd63ef6 | 2009-05-06 19:41:37 | [diff] [blame] | 580 | initWithProfile:[self defaultProfile]]); |
[email protected] | 767543d | 2009-04-30 19:23:58 | [diff] [blame] | 581 | // Watch for a notification of when it goes away so that we can destroy |
| 582 | // the controller. |
| 583 | [[NSNotificationCenter defaultCenter] |
| 584 | addObserver:self |
| 585 | selector:@selector(prefsWindowClosed:) |
| 586 | name:kUserDoneEditingPrefsNotification |
| 587 | object:prefsController_.get()]; |
[email protected] | 4240438 | 2009-04-30 17:59:24 | [diff] [blame] | 588 | } |
| 589 | [prefsController_ showPreferences:sender]; |
[email protected] | 3111f08b | 2009-04-30 16:01:52 | [diff] [blame] | 590 | } |
| 591 | |
[email protected] | bde3dda | 2009-05-20 22:13:07 | [diff] [blame] | 592 | // Called when the about window is closed. We use this to release the |
| 593 | // window controller. |
| 594 | - (void)aboutWindowClosed:(NSNotification*)notify { |
| 595 | [[NSNotificationCenter defaultCenter] |
| 596 | removeObserver:self |
| 597 | name:kUserClosedAboutNotification |
| 598 | object:aboutController_.get()]; |
| 599 | aboutController_.reset(NULL); |
| 600 | } |
| 601 | |
| 602 | - (IBAction)orderFrontStandardAboutPanel:(id)sender { |
| 603 | #if !defined(GOOGLE_CHROME_BUILD) |
| 604 | // If not branded behave like a generic Cocoa app. |
| 605 | [NSApp orderFrontStandardAboutPanel:sender]; |
| 606 | #else |
| 607 | // Otherwise bring up our special dialog (e.g. with an auto-update button). |
| 608 | if (!aboutController_) { |
| 609 | aboutController_.reset([[AboutWindowController alloc] |
| 610 | initWithWindowNibName:@"About"]); |
| 611 | if (!aboutController_) { |
| 612 | // If we get here something is wacky. I managed to do it when |
| 613 | // testing by explicitly forcing an auto-update to an older |
| 614 | // version then trying to open the about box again (missing |
| 615 | // nib). This shouldn't be possible in general but let's try |
| 616 | // hard to not do nothing. |
| 617 | [NSApp orderFrontStandardAboutPanel:sender]; |
| 618 | return; |
| 619 | } |
| 620 | // Watch for a notification of when it goes away so that we can destroy |
| 621 | // the controller. |
| 622 | [[NSNotificationCenter defaultCenter] |
| 623 | addObserver:self |
| 624 | selector:@selector(aboutWindowClosed:) |
| 625 | name:kUserClosedAboutNotification |
| 626 | object:aboutController_.get()]; |
| 627 | } |
| 628 | if (![[aboutController_ window] isVisible]) |
| 629 | [[aboutController_ window] center]; |
| 630 | [aboutController_ showWindow:self]; |
| 631 | #endif |
| 632 | } |
| 633 | |
[email protected] | 1bcdb53 | 2009-01-16 17:47:57 | [diff] [blame] | 634 | @end |