[email protected] | 7dc8c6b | 2010-04-09 17:02:50 | [diff] [blame] | 1 | // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
[email protected] | 1bcdb53 | 2009-01-16 17:47:57 | [diff] [blame] | 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] | 0ccf461 | 2010-07-31 07:11:36 | [diff] [blame] | 7 | #include "app/l10n_util.h" |
[email protected] | 288bfcd3 | 2009-09-14 18:14:46 | [diff] [blame] | 8 | #include "app/l10n_util_mac.h" |
[email protected] | d630d7d5 | 2010-02-21 00:55:11 | [diff] [blame] | 9 | #include "base/auto_reset.h" |
[email protected] | 57750f82 | 2009-04-21 21:43:09 | [diff] [blame] | 10 | #include "base/command_line.h" |
[email protected] | f805fe8 | 2010-08-03 22:47:10 | [diff] [blame] | 11 | #include "base/file_path.h" |
[email protected] | 09729a55 | 2009-08-03 23:21:41 | [diff] [blame] | 12 | #include "base/mac_util.h" |
[email protected] | 2c47bc1 | 2009-04-10 20:14:00 | [diff] [blame] | 13 | #include "base/message_loop.h" |
[email protected] | 528c56d | 2010-07-30 19:28:44 | [diff] [blame] | 14 | #include "base/string_number_conversions.h" |
[email protected] | 57750f82 | 2009-04-21 21:43:09 | [diff] [blame] | 15 | #include "base/sys_string_conversions.h" |
[email protected] | 2c47bc1 | 2009-04-10 20:14:00 | [diff] [blame] | 16 | #include "chrome/app/chrome_dll_resource.h" |
| 17 | #include "chrome/browser/browser.h" |
[email protected] | 57750f82 | 2009-04-21 21:43:09 | [diff] [blame] | 18 | #include "chrome/browser/browser_init.h" |
[email protected] | 2c47bc1 | 2009-04-10 20:14:00 | [diff] [blame] | 19 | #include "chrome/browser/browser_list.h" |
[email protected] | 5c23875 | 2009-06-13 10:29:07 | [diff] [blame] | 20 | #include "chrome/browser/browser_process.h" |
[email protected] | 41741a96 | 2009-02-18 21:51:39 | [diff] [blame] | 21 | #include "chrome/browser/browser_shutdown.h" |
[email protected] | ce560f8 | 2009-06-03 09:39:44 | [diff] [blame] | 22 | #include "chrome/browser/browser_window.h" |
[email protected] | 12dc3d4 | 2010-02-22 23:37:12 | [diff] [blame] | 23 | #include "chrome/browser/chrome_thread.h" |
[email protected] | bde3dda | 2009-05-20 22:13:07 | [diff] [blame] | 24 | #import "chrome/browser/cocoa/about_window_controller.h" |
[email protected] | 3f34599d | 2009-03-25 22:11:43 | [diff] [blame] | 25 | #import "chrome/browser/cocoa/bookmark_menu_bridge.h" |
[email protected] | a9e8afc | 2009-08-11 22:03:17 | [diff] [blame] | 26 | #import "chrome/browser/cocoa/browser_window_cocoa.h" |
| 27 | #import "chrome/browser/cocoa/browser_window_controller.h" |
[email protected] | 3896b18 | 2009-11-03 16:15:32 | [diff] [blame] | 28 | #import "chrome/browser/cocoa/bug_report_window_controller.h" |
[email protected] | 93e18176 | 2009-05-29 14:30:38 | [diff] [blame] | 29 | #import "chrome/browser/cocoa/clear_browsing_data_controller.h" |
[email protected] | 2bcec61 | 2009-05-14 17:50:53 | [diff] [blame] | 30 | #import "chrome/browser/cocoa/encoding_menu_controller_delegate_mac.h" |
[email protected] | 4aee565 | 2009-11-19 18:59:21 | [diff] [blame] | 31 | #import "chrome/browser/cocoa/history_menu_bridge.h" |
| 32 | #import "chrome/browser/cocoa/import_settings_dialog.h" |
[email protected] | 4240438 | 2009-04-30 17:59:24 | [diff] [blame] | 33 | #import "chrome/browser/cocoa/preferences_window_controller.h" |
[email protected] | 449dd2f | 2009-05-27 13:04:00 | [diff] [blame] | 34 | #import "chrome/browser/cocoa/tab_strip_controller.h" |
| 35 | #import "chrome/browser/cocoa/tab_window_controller.h" |
[email protected] | ceaaf181 | 2010-01-20 22:13:09 | [diff] [blame] | 36 | #include "chrome/browser/cocoa/task_manager_mac.h" |
[email protected] | 2c47bc1 | 2009-04-10 20:14:00 | [diff] [blame] | 37 | #include "chrome/browser/command_updater.h" |
[email protected] | 09729a55 | 2009-08-03 23:21:41 | [diff] [blame] | 38 | #include "chrome/browser/download/download_manager.h" |
[email protected] | 4aee565 | 2009-11-19 18:59:21 | [diff] [blame] | 39 | #include "chrome/browser/metrics/user_metrics.h" |
[email protected] | 60ad3e2 | 2009-09-18 21:07:19 | [diff] [blame] | 40 | #include "chrome/browser/options_window.h" |
[email protected] | 052313b | 2010-02-19 09:43:08 | [diff] [blame] | 41 | #include "chrome/browser/pref_service.h" |
| 42 | #include "chrome/browser/profile_manager.h" |
[email protected] | fbc947b | 2009-06-19 13:28:24 | [diff] [blame] | 43 | #include "chrome/browser/sessions/tab_restore_service.h" |
[email protected] | f9bc9b9 | 2009-11-24 00:55:35 | [diff] [blame] | 44 | #include "chrome/browser/sync/profile_sync_service.h" |
[email protected] | 40f04797 | 2009-11-25 03:54:40 | [diff] [blame] | 45 | #include "chrome/browser/sync/sync_ui_util.h" |
| 46 | #include "chrome/browser/sync/sync_ui_util_mac.h" |
[email protected] | 60ad3e2 | 2009-09-18 21:07:19 | [diff] [blame] | 47 | #include "chrome/browser/tab_contents/tab_contents.h" |
[email protected] | 646b9b9 | 2010-04-30 21:51:58 | [diff] [blame] | 48 | #include "chrome/common/app_mode_common_mac.h" |
[email protected] | 12dc3d4 | 2010-02-22 23:37:12 | [diff] [blame] | 49 | #include "chrome/common/chrome_paths_internal.h" |
[email protected] | f4acfae8 | 2009-09-11 00:33:38 | [diff] [blame] | 50 | #include "chrome/common/chrome_switches.h" |
[email protected] | a96ec6a | 2009-11-04 17:27:08 | [diff] [blame] | 51 | #include "chrome/common/notification_service.h" |
[email protected] | cd63ef6 | 2009-05-06 19:41:37 | [diff] [blame] | 52 | #include "chrome/common/pref_names.h" |
[email protected] | 024617a | 2010-08-20 05:08:05 | [diff] [blame] | 53 | #include "chrome/common/url_constants.h" |
[email protected] | 9dcb5966 | 2009-09-23 01:15:50 | [diff] [blame] | 54 | #include "grit/chromium_strings.h" |
[email protected] | 09729a55 | 2009-08-03 23:21:41 | [diff] [blame] | 55 | #include "grit/generated_resources.h" |
[email protected] | caadfca | 2010-01-22 21:51:21 | [diff] [blame] | 56 | #include "net/base/net_util.h" |
[email protected] | 88d7494 | 2009-01-21 22:04:44 | [diff] [blame] | 57 | |
[email protected] | f2c20fa | 2009-12-01 17:42:02 | [diff] [blame] | 58 | // 10.6 adds a public API for the Spotlight-backed search menu item in the Help |
| 59 | // menu. Provide the declaration so it can be called below when building with |
| 60 | // the 10.5 SDK. |
| 61 | #if !defined(MAC_OS_X_VERSION_10_6) || \ |
| 62 | MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_6 |
| 63 | @interface NSApplication (SnowLeopardSDKDeclarations) |
[email protected] | caadfca | 2010-01-22 21:51:21 | [diff] [blame] | 64 | - (void)setHelpMenu:(NSMenu*)helpMenu; |
[email protected] | f2c20fa | 2009-12-01 17:42:02 | [diff] [blame] | 65 | @end |
| 66 | #endif |
| 67 | |
[email protected] | d630d7d5 | 2010-02-21 00:55:11 | [diff] [blame] | 68 | namespace { |
| 69 | |
| 70 | // True while AppController is calling Browser::OpenEmptyWindow(). We need a |
| 71 | // global flag here, analogue to BrowserInit::InProcessStartup() because |
| 72 | // otherwise the SessionService will try to restore sessions when we make a new |
| 73 | // window while there are no other active windows. |
| 74 | bool g_is_opening_new_window = false; |
| 75 | |
| 76 | // Activates a browser window having the given profile (the last one active) if |
| 77 | // possible and returns a pointer to the activate |Browser| or NULL if this was |
| 78 | // not possible. If the last active browser is minimized (in particular, if |
| 79 | // there are only minimized windows), it will unminimize it. |
| 80 | Browser* ActivateBrowser(Profile* profile) { |
| 81 | Browser* browser = BrowserList::GetLastActiveWithProfile(profile); |
| 82 | if (browser) |
| 83 | browser->window()->Activate(); |
| 84 | return browser; |
| 85 | } |
| 86 | |
| 87 | // Creates an empty browser window with the given profile and returns a pointer |
| 88 | // to the new |Browser|. |
| 89 | Browser* CreateBrowser(Profile* profile) { |
| 90 | { |
[email protected] | 0fbd7033 | 2010-06-01 19:28:34 | [diff] [blame] | 91 | AutoReset<bool> auto_reset_in_run(&g_is_opening_new_window, true); |
[email protected] | d630d7d5 | 2010-02-21 00:55:11 | [diff] [blame] | 92 | Browser::OpenEmptyWindow(profile); |
| 93 | } |
| 94 | |
| 95 | Browser* browser = BrowserList::GetLastActive(); |
| 96 | CHECK(browser); |
| 97 | return browser; |
| 98 | } |
| 99 | |
| 100 | // Activates a browser window having the given profile (the last one active) if |
| 101 | // possible or creates an empty one if necessary. Returns a pointer to the |
| 102 | // activated/new |Browser|. |
| 103 | Browser* ActivateOrCreateBrowser(Profile* profile) { |
| 104 | if (Browser* browser = ActivateBrowser(profile)) |
| 105 | return browser; |
| 106 | return CreateBrowser(profile); |
| 107 | } |
| 108 | |
[email protected] | 12dc3d4 | 2010-02-22 23:37:12 | [diff] [blame] | 109 | // This task synchronizes preferences (under "org.chromium.Chromium" or |
| 110 | // "com.google.Chrome"), in particular, writes them out to disk. |
| 111 | class PrefsSyncTask : public Task { |
| 112 | public: |
| 113 | PrefsSyncTask() {} |
| 114 | virtual ~PrefsSyncTask() {} |
| 115 | virtual void Run() { |
| 116 | if (!CFPreferencesAppSynchronize(app_mode::kAppPrefsID)) |
| 117 | LOG(WARNING) << "Error recording application bundle path."; |
| 118 | } |
| 119 | }; |
| 120 | |
| 121 | // Record the location of the application bundle (containing the main framework) |
| 122 | // from which Chromium was loaded. This is used by app mode shims to find |
| 123 | // Chromium. |
| 124 | void RecordLastRunAppBundlePath() { |
| 125 | // Going up three levels from |chrome::GetVersionedDirectory()| gives the |
| 126 | // real, user-visible app bundle directory. (The alternatives give either the |
| 127 | // framework's path or the initial app's path, which may be an app mode shim |
| 128 | // or a unit test.) |
| 129 | FilePath appBundlePath = |
| 130 | chrome::GetVersionedDirectory().DirName().DirName().DirName(); |
| 131 | CFPreferencesSetAppValue(app_mode::kLastRunAppBundlePathPrefsKey, |
| 132 | base::SysUTF8ToCFStringRef(appBundlePath.value()), |
| 133 | app_mode::kAppPrefsID); |
| 134 | |
| 135 | // Sync after a delay avoid I/O contention on startup; 1500 ms is plenty. |
| 136 | ChromeThread::PostDelayedTask(ChromeThread::FILE, FROM_HERE, |
| 137 | new PrefsSyncTask(), 1500); |
| 138 | } |
| 139 | |
[email protected] | d630d7d5 | 2010-02-21 00:55:11 | [diff] [blame] | 140 | } // anonymous namespace |
| 141 | |
| 142 | @interface AppController(Private) |
[email protected] | 88d7494 | 2009-01-21 22:04:44 | [diff] [blame] | 143 | - (void)initMenuState; |
[email protected] | eef99c2 | 2010-08-17 05:55:16 | [diff] [blame] | 144 | - (void)registerServicesMenuTypesTo:(NSApplication*)app; |
[email protected] | d630d7d5 | 2010-02-21 00:55:11 | [diff] [blame] | 145 | - (void)openUrls:(const std::vector<GURL>&)urls; |
[email protected] | 57750f82 | 2009-04-21 21:43:09 | [diff] [blame] | 146 | - (void)getUrl:(NSAppleEventDescriptor*)event |
| 147 | withReply:(NSAppleEventDescriptor*)reply; |
[email protected] | 449dd2f | 2009-05-27 13:04:00 | [diff] [blame] | 148 | - (void)windowLayeringDidChange:(NSNotification*)inNotification; |
[email protected] | dd6ab128 | 2010-07-20 01:07:32 | [diff] [blame] | 149 | - (void)checkForAnyKeyWindows; |
[email protected] | 73f5b36 | 2009-08-10 23:58:21 | [diff] [blame] | 150 | - (BOOL)userWillWaitForInProgressDownloads:(int)downloadCount; |
[email protected] | 09729a55 | 2009-08-03 23:21:41 | [diff] [blame] | 151 | - (BOOL)shouldQuitWithInProgressDownloads; |
[email protected] | d989f07c | 2009-11-14 00:35:46 | [diff] [blame] | 152 | - (void)showPreferencesWindow:(id)sender |
| 153 | page:(OptionsPage)page |
| 154 | profile:(Profile*)profile; |
[email protected] | 88d7494 | 2009-01-21 22:04:44 | [diff] [blame] | 155 | @end |
[email protected] | 1bcdb53 | 2009-01-16 17:47:57 | [diff] [blame] | 156 | |
| 157 | @implementation AppController |
| 158 | |
[email protected] | caadfca | 2010-01-22 21:51:21 | [diff] [blame] | 159 | @synthesize startupComplete = startupComplete_; |
| 160 | |
[email protected] | cd63ef6 | 2009-05-06 19:41:37 | [diff] [blame] | 161 | // This method is called very early in application startup (ie, before |
| 162 | // the profile is loaded or any preferences have been registered). Defer any |
| 163 | // user-data initialization until -applicationDidFinishLaunching:. |
[email protected] | 88d7494 | 2009-01-21 22:04:44 | [diff] [blame] | 164 | - (void)awakeFromNib { |
[email protected] | 136140c | 2009-05-19 13:58:25 | [diff] [blame] | 165 | // We need to register the handlers early to catch events fired on launch. |
[email protected] | 57750f82 | 2009-04-21 21:43:09 | [diff] [blame] | 166 | NSAppleEventManager* em = [NSAppleEventManager sharedAppleEventManager]; |
| 167 | [em setEventHandler:self |
| 168 | andSelector:@selector(getUrl:withReply:) |
| 169 | forEventClass:kInternetEventClass |
| 170 | andEventID:kAEGetURL]; |
| 171 | [em setEventHandler:self |
| 172 | andSelector:@selector(getUrl:withReply:) |
| 173 | forEventClass:'WWW!' // A particularly ancient AppleEvent that dates |
| 174 | andEventID:'OURL']; // back to the Spyglass days. |
[email protected] | cd63ef6 | 2009-05-06 19:41:37 | [diff] [blame] | 175 | |
[email protected] | 449dd2f | 2009-05-27 13:04:00 | [diff] [blame] | 176 | // Register for various window layering changes. We use these to update |
| 177 | // various UI elements (command-key equivalents, etc) when the frontmost |
| 178 | // window changes. |
| 179 | NSNotificationCenter* notificationCenter = |
| 180 | [NSNotificationCenter defaultCenter]; |
[email protected] | ce560f8 | 2009-06-03 09:39:44 | [diff] [blame] | 181 | [notificationCenter |
[email protected] | 449dd2f | 2009-05-27 13:04:00 | [diff] [blame] | 182 | addObserver:self |
| 183 | selector:@selector(windowLayeringDidChange:) |
| 184 | name:NSWindowDidBecomeKeyNotification |
| 185 | object:nil]; |
| 186 | [notificationCenter |
| 187 | addObserver:self |
| 188 | selector:@selector(windowLayeringDidChange:) |
| 189 | name:NSWindowDidResignKeyNotification |
| 190 | object:nil]; |
| 191 | [notificationCenter |
| 192 | addObserver:self |
| 193 | selector:@selector(windowLayeringDidChange:) |
| 194 | name:NSWindowDidBecomeMainNotification |
| 195 | object:nil]; |
| 196 | [notificationCenter |
| 197 | addObserver:self |
| 198 | selector:@selector(windowLayeringDidChange:) |
| 199 | name:NSWindowDidResignMainNotification |
| 200 | object:nil]; |
| 201 | |
| 202 | // Register for a notification that the number of tabs changes in windows |
| 203 | // so we can adjust the close tab/window command keys. |
| 204 | [notificationCenter |
| 205 | addObserver:self |
| 206 | selector:@selector(tabsChanged:) |
| 207 | name:kTabStripNumberOfTabsChanged |
| 208 | object:nil]; |
| 209 | |
[email protected] | 136140c | 2009-05-19 13:58:25 | [diff] [blame] | 210 | // Set up the command updater for when there are no windows open |
| 211 | [self initMenuState]; |
[email protected] | f4acfae8 | 2009-09-11 00:33:38 | [diff] [blame] | 212 | |
| 213 | // Activate (bring to foreground) if asked to do so. On |
| 214 | // Windows this logic isn't necessary since |
| 215 | // BrowserWindow::Activate() calls ::SetForegroundWindow() which is |
| 216 | // adequate. On Mac, BrowserWindow::Activate() calls -[NSWindow |
| 217 | // makeKeyAndOrderFront:] which does not activate the application |
| 218 | // itself. |
| 219 | const CommandLine& parsed_command_line = *CommandLine::ForCurrentProcess(); |
| 220 | if (parsed_command_line.HasSwitch(switches::kActivateOnLaunch)) { |
| 221 | [NSApp activateIgnoringOtherApps:YES]; |
| 222 | } |
[email protected] | c1d0d81 | 2010-07-27 20:21:55 | [diff] [blame] | 223 | |
| 224 | if (!parsed_command_line.HasSwitch(switches::kEnableExposeForTabs)) { |
| 225 | [tabposeMenuItem_ setHidden:YES]; |
| 226 | } |
[email protected] | 136140c | 2009-05-19 13:58:25 | [diff] [blame] | 227 | } |
| 228 | |
[email protected] | 7dc8c6b | 2010-04-09 17:02:50 | [diff] [blame] | 229 | // (NSApplicationDelegate protocol) This is the Apple-approved place to override |
| 230 | // the default handlers. |
| 231 | - (void)applicationWillFinishLaunching:(NSNotification*)notification { |
[email protected] | 5887793 | 2010-05-25 05:43:11 | [diff] [blame] | 232 | // Nothing here right now. |
[email protected] | 7dc8c6b | 2010-04-09 17:02:50 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | - (BOOL)tryToTerminateApplication:(NSApplication*)app { |
[email protected] | 3076fad | 2010-04-28 18:11:45 | [diff] [blame] | 236 | // Check for in-process downloads, and prompt the user if they really want |
| 237 | // to quit (and thus cancel downloads). Only check if we're not already |
| 238 | // shutting down, else the user might be prompted multiple times if the |
| 239 | // download isn't stopped before terminate is called again. |
| 240 | if (!browser_shutdown::IsTryingToQuit() && |
| 241 | ![self shouldQuitWithInProgressDownloads]) |
| 242 | return NO; |
| 243 | |
[email protected] | 7dc8c6b | 2010-04-09 17:02:50 | [diff] [blame] | 244 | // TODO(viettrungluu): Remove Apple Event handlers here? (It's safe to leave |
| 245 | // them in, but I'm not sure about UX; we'd also want to disable other things |
| 246 | // though.) http://crbug.com/40861 |
| 247 | |
[email protected] | c984d9f | 2010-07-20 20:52:20 | [diff] [blame] | 248 | size_t num_browsers = BrowserList::size(); |
[email protected] | 7dc8c6b | 2010-04-09 17:02:50 | [diff] [blame] | 249 | |
[email protected] | c984d9f | 2010-07-20 20:52:20 | [diff] [blame] | 250 | // Initiate a shutdown (via BrowserList::CloseAllBrowsers()) if we aren't |
| 251 | // already shutting down. |
| 252 | if (!browser_shutdown::IsTryingToQuit()) |
| 253 | BrowserList::CloseAllBrowsers(true); |
[email protected] | 7dc8c6b | 2010-04-09 17:02:50 | [diff] [blame] | 254 | |
[email protected] | c984d9f | 2010-07-20 20:52:20 | [diff] [blame] | 255 | return num_browsers == 0 ? YES : NO; |
[email protected] | 7dc8c6b | 2010-04-09 17:02:50 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | - (void)stopTryingToTerminateApplication:(NSApplication*)app { |
| 259 | if (browser_shutdown::IsTryingToQuit()) { |
| 260 | // Reset the "trying to quit" state, so that closing all browser windows |
| 261 | // will no longer lead to termination. |
| 262 | browser_shutdown::SetTryingToQuit(false); |
| 263 | |
| 264 | // TODO(viettrungluu): Were we to remove Apple Event handlers above, we |
| 265 | // would have to reinstall them here. http://crbug.com/40861 |
| 266 | } |
[email protected] | a9e8afc | 2009-08-11 22:03:17 | [diff] [blame] | 267 | } |
| 268 | |
[email protected] | 449dd2f | 2009-05-27 13:04:00 | [diff] [blame] | 269 | // Called when the app is shutting down. Clean-up as appropriate. |
[email protected] | 7dc8c6b | 2010-04-09 17:02:50 | [diff] [blame] | 270 | - (void)applicationWillTerminate:(NSNotification*)aNotification { |
[email protected] | 3b6aa8b6 | 2009-09-15 21:36:11 | [diff] [blame] | 271 | NSAppleEventManager* em = [NSAppleEventManager sharedAppleEventManager]; |
| 272 | [em removeEventHandlerForEventClass:kInternetEventClass |
| 273 | andEventID:kAEGetURL]; |
| 274 | [em removeEventHandlerForEventClass:'WWW!' |
| 275 | andEventID:'OURL']; |
[email protected] | 3b6aa8b6 | 2009-09-15 21:36:11 | [diff] [blame] | 276 | |
[email protected] | 7dc8c6b | 2010-04-09 17:02:50 | [diff] [blame] | 277 | // There better be no browser windows left at this point. |
| 278 | CHECK_EQ(BrowserList::size(), 0u); |
[email protected] | 3b6aa8b6 | 2009-09-15 21:36:11 | [diff] [blame] | 279 | |
[email protected] | c984d9f | 2010-07-20 20:52:20 | [diff] [blame] | 280 | // Tell BrowserList not to keep the browser process alive. Once all the |
| 281 | // browsers get dealloc'd, it will stop the RunLoop and fall back into main(). |
| 282 | BrowserList::EndKeepAlive(); |
[email protected] | fbc947b | 2009-06-19 13:28:24 | [diff] [blame] | 283 | |
[email protected] | d4e1865 | 2009-11-19 21:52:16 | [diff] [blame] | 284 | // Close these off if they have open windows. |
| 285 | [prefsController_ close]; |
| 286 | [aboutController_ close]; |
| 287 | |
[email protected] | 449dd2f | 2009-05-27 13:04:00 | [diff] [blame] | 288 | [[NSNotificationCenter defaultCenter] removeObserver:self]; |
| 289 | } |
| 290 | |
[email protected] | 3b6aa8b6 | 2009-09-15 21:36:11 | [diff] [blame] | 291 | - (void)didEndMainMessageLoop { |
| 292 | DCHECK(!BrowserList::HasBrowserWithProfile([self defaultProfile])); |
| 293 | if (!BrowserList::HasBrowserWithProfile([self defaultProfile])) { |
| 294 | // As we're shutting down, we need to nuke the TabRestoreService, which |
| 295 | // will start the shutdown of the NavigationControllers and allow for |
| 296 | // proper shutdown. If we don't do this, Chrome won't shut down cleanly, |
| 297 | // and may end up crashing when some thread tries to use the IO thread (or |
| 298 | // another thread) that is no longer valid. |
| 299 | [self defaultProfile]->ResetTabRestoreService(); |
| 300 | } |
| 301 | } |
| 302 | |
[email protected] | 449dd2f | 2009-05-27 13:04:00 | [diff] [blame] | 303 | // Helper routine to get the window controller if the key window is a tabbed |
| 304 | // window, or nil if not. Examples of non-tabbed windows are "about" or |
| 305 | // "preferences". |
| 306 | - (TabWindowController*)keyWindowTabController { |
| 307 | NSWindowController* keyWindowController = |
[email protected] | 5dbabd0 | 2009-09-17 20:44:13 | [diff] [blame] | 308 | [[NSApp keyWindow] windowController]; |
[email protected] | 449dd2f | 2009-05-27 13:04:00 | [diff] [blame] | 309 | if ([keyWindowController isKindOfClass:[TabWindowController class]]) |
| 310 | return (TabWindowController*)keyWindowController; |
| 311 | |
| 312 | return nil; |
| 313 | } |
| 314 | |
[email protected] | 74c0c64 | 2009-09-22 21:25:50 | [diff] [blame] | 315 | // Helper routine to get the window controller if the main window is a tabbed |
| 316 | // window, or nil if not. Examples of non-tabbed windows are "about" or |
| 317 | // "preferences". |
| 318 | - (TabWindowController*)mainWindowTabController { |
| 319 | NSWindowController* mainWindowController = |
| 320 | [[NSApp mainWindow] windowController]; |
| 321 | if ([mainWindowController isKindOfClass:[TabWindowController class]]) |
| 322 | return (TabWindowController*)mainWindowController; |
| 323 | |
| 324 | return nil; |
| 325 | } |
| 326 | |
[email protected] | 449dd2f | 2009-05-27 13:04:00 | [diff] [blame] | 327 | // If the window has tabs, make "close window" be cmd-shift-w, otherwise leave |
| 328 | // it as the normal cmd-w. Capitalization of the key equivalent affects whether |
| 329 | // the shift modifer is used. |
| 330 | - (void)adjustCloseWindowMenuItemKeyEquivalent:(BOOL)inHaveTabs { |
| 331 | [closeWindowMenuItem_ setKeyEquivalent:(inHaveTabs ? @"W" : @"w")]; |
[email protected] | 74c0c64 | 2009-09-22 21:25:50 | [diff] [blame] | 332 | [closeWindowMenuItem_ setKeyEquivalentModifierMask:NSCommandKeyMask]; |
[email protected] | 449dd2f | 2009-05-27 13:04:00 | [diff] [blame] | 333 | } |
| 334 | |
| 335 | // If the window has tabs, make "close tab" take over cmd-w, otherwise it |
| 336 | // shouldn't have any key-equivalent because it should be disabled. |
| 337 | - (void)adjustCloseTabMenuItemKeyEquivalent:(BOOL)hasTabs { |
| 338 | if (hasTabs) { |
| 339 | [closeTabMenuItem_ setKeyEquivalent:@"w"]; |
| 340 | [closeTabMenuItem_ setKeyEquivalentModifierMask:NSCommandKeyMask]; |
| 341 | } else { |
| 342 | [closeTabMenuItem_ setKeyEquivalent:@""]; |
| 343 | [closeTabMenuItem_ setKeyEquivalentModifierMask:0]; |
| 344 | } |
| 345 | } |
| 346 | |
[email protected] | 74c0c64 | 2009-09-22 21:25:50 | [diff] [blame] | 347 | // Explicitly remove any command-key equivalents from the close tab/window |
| 348 | // menus so that nothing can go haywire if we get a user action during pending |
| 349 | // updates. |
| 350 | - (void)clearCloseMenuItemKeyEquivalents { |
| 351 | [closeTabMenuItem_ setKeyEquivalent:@""]; |
| 352 | [closeTabMenuItem_ setKeyEquivalentModifierMask:0]; |
| 353 | [closeWindowMenuItem_ setKeyEquivalent:@""]; |
| 354 | [closeWindowMenuItem_ setKeyEquivalentModifierMask:0]; |
| 355 | } |
| 356 | |
[email protected] | 449dd2f | 2009-05-27 13:04:00 | [diff] [blame] | 357 | // See if we have a window with tabs open, and adjust the key equivalents for |
[email protected] | 668c911c | 2010-03-08 17:05:25 | [diff] [blame] | 358 | // Close Tab/Close Window accordingly. |
[email protected] | 449dd2f | 2009-05-27 13:04:00 | [diff] [blame] | 359 | - (void)fixCloseMenuItemKeyEquivalents { |
[email protected] | 668c911c | 2010-03-08 17:05:25 | [diff] [blame] | 360 | fileMenuUpdatePending_ = NO; |
[email protected] | 449dd2f | 2009-05-27 13:04:00 | [diff] [blame] | 361 | TabWindowController* tabController = [self keyWindowTabController]; |
[email protected] | 74c0c64 | 2009-09-22 21:25:50 | [diff] [blame] | 362 | if (!tabController && ![NSApp keyWindow]) { |
| 363 | // There might be a small amount of time where there is no key window, |
| 364 | // so just use our main browser window if there is one. |
| 365 | tabController = [self mainWindowTabController]; |
| 366 | } |
[email protected] | 449dd2f | 2009-05-27 13:04:00 | [diff] [blame] | 367 | BOOL windowWithMultipleTabs = |
| 368 | (tabController && [tabController numberOfTabs] > 1); |
| 369 | [self adjustCloseWindowMenuItemKeyEquivalent:windowWithMultipleTabs]; |
| 370 | [self adjustCloseTabMenuItemKeyEquivalent:windowWithMultipleTabs]; |
[email protected] | 449dd2f | 2009-05-27 13:04:00 | [diff] [blame] | 371 | } |
| 372 | |
| 373 | // Fix up the "close tab/close window" command-key equivalents. We do this |
| 374 | // 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] | 375 | // we do the enabling. This should only be called on the main thread, code that |
| 376 | // 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] | 377 | - (void)delayedFixCloseMenuItemKeyEquivalents { |
[email protected] | f2a5c34 | 2010-01-21 17:00:29 | [diff] [blame] | 378 | DCHECK([NSThread isMainThread]); |
[email protected] | 449dd2f | 2009-05-27 13:04:00 | [diff] [blame] | 379 | if (!fileMenuUpdatePending_) { |
[email protected] | 74c0c64 | 2009-09-22 21:25:50 | [diff] [blame] | 380 | // The OS prefers keypresses to timers, so it's possible that a cmd-w |
| 381 | // can sneak in before this timer fires. In order to prevent that from |
| 382 | // having any bad consequences, just clear the keys combos altogether. They |
| 383 | // will be reset when the timer eventually fires. |
[email protected] | 668c911c | 2010-03-08 17:05:25 | [diff] [blame] | 384 | if ([NSThread isMainThread]) { |
| 385 | fileMenuUpdatePending_ = YES; |
| 386 | [self clearCloseMenuItemKeyEquivalents]; |
| 387 | [self performSelector:@selector(fixCloseMenuItemKeyEquivalents) |
| 388 | withObject:nil |
| 389 | afterDelay:0]; |
| 390 | } else { |
| 391 | // This shouldn't be happening, but if it does, force it to the main |
| 392 | // thread to avoid dropping the update. Don't mess with |
| 393 | // |fileMenuUpdatePending_| as it's not expected to be threadsafe and |
| 394 | // there could be a race between the selector finishing and setting the |
| 395 | // flag. |
| 396 | [self |
| 397 | performSelectorOnMainThread:@selector(fixCloseMenuItemKeyEquivalents) |
[email protected] | f2a5c34 | 2010-01-21 17:00:29 | [diff] [blame] | 398 | withObject:nil |
| 399 | waitUntilDone:NO]; |
[email protected] | 668c911c | 2010-03-08 17:05:25 | [diff] [blame] | 400 | } |
[email protected] | 449dd2f | 2009-05-27 13:04:00 | [diff] [blame] | 401 | } |
| 402 | } |
| 403 | |
| 404 | // Called when we get a notification about the window layering changing to |
| 405 | // update the UI based on the new main window. |
| 406 | - (void)windowLayeringDidChange:(NSNotification*)notify { |
| 407 | [self delayedFixCloseMenuItemKeyEquivalents]; |
[email protected] | dd6ab128 | 2010-07-20 01:07:32 | [diff] [blame] | 408 | |
| 409 | if ([notify name] == NSWindowDidResignKeyNotification) { |
| 410 | // If a window is closed, this notification is fired but |[NSApp keyWindow]| |
| 411 | // returns nil regardless of whether any suitable candidates for the key |
| 412 | // window remain. It seems that the new key window for the app is not set |
| 413 | // until after this notification is fired, so a check is performed after the |
| 414 | // run loop is allowed to spin. |
| 415 | [self performSelector:@selector(checkForAnyKeyWindows) |
| 416 | withObject:nil |
| 417 | afterDelay:0.0]; |
| 418 | } |
| 419 | } |
| 420 | |
| 421 | - (void)checkForAnyKeyWindows { |
| 422 | if ([NSApp keyWindow]) |
| 423 | return; |
| 424 | |
| 425 | NotificationService::current()->Notify( |
| 426 | NotificationType::NO_KEY_WINDOW, |
| 427 | NotificationService::AllSources(), |
| 428 | NotificationService::NoDetails()); |
[email protected] | 449dd2f | 2009-05-27 13:04:00 | [diff] [blame] | 429 | } |
| 430 | |
| 431 | // Called when the number of tabs changes in one of the browser windows. The |
| 432 | // object is the tab strip controller, but we don't currently care. |
| 433 | - (void)tabsChanged:(NSNotification*)notify { |
[email protected] | e72746b1 | 2009-10-19 14:11:51 | [diff] [blame] | 434 | // We don't need to do this on a delay, as in the method above, because the |
| 435 | // window layering isn't changing. As a result, there's no chance that a |
| 436 | // different window will sneak in as the key window and cause the problems |
| 437 | // we hacked around above by clearing the key equivalents. |
| 438 | [self fixCloseMenuItemKeyEquivalents]; |
[email protected] | 449dd2f | 2009-05-27 13:04:00 | [diff] [blame] | 439 | } |
| 440 | |
[email protected] | 414fde59 | 2009-05-21 16:14:43 | [diff] [blame] | 441 | // If the auto-update interval is not set, make it 5 hours. |
| 442 | // This code is specific to Mac Chrome Dev Channel. |
| 443 | // Placed here for 2 reasons: |
| 444 | // 1) Same spot as other Pref stuff |
| 445 | // 2) Try and be friendly by keeping this after app launch |
| 446 | // TODO(jrg): remove once we go Beta. |
| 447 | - (void)setUpdateCheckInterval { |
| 448 | #if defined(GOOGLE_CHROME_BUILD) |
| 449 | CFStringRef app = (CFStringRef)@"com.google.Keystone.Agent"; |
| 450 | CFStringRef checkInterval = (CFStringRef)@"checkInterval"; |
| 451 | CFPropertyListRef plist = CFPreferencesCopyAppValue(checkInterval, app); |
| 452 | if (!plist) { |
| 453 | const float fiveHoursInSeconds = 5.0 * 60.0 * 60.0; |
[email protected] | 7dc8c6b | 2010-04-09 17:02:50 | [diff] [blame] | 454 | NSNumber* value = [NSNumber numberWithFloat:fiveHoursInSeconds]; |
[email protected] | 414fde59 | 2009-05-21 16:14:43 | [diff] [blame] | 455 | CFPreferencesSetAppValue(checkInterval, value, app); |
| 456 | CFPreferencesAppSynchronize(app); |
| 457 | } |
| 458 | #endif |
| 459 | } |
| 460 | |
[email protected] | 136140c | 2009-05-19 13:58:25 | [diff] [blame] | 461 | // This is called after profiles have been loaded and preferences registered. |
| 462 | // It is safe to access the default profile here. |
| 463 | - (void)applicationDidFinishLaunching:(NSNotification*)notify { |
[email protected] | c984d9f | 2010-07-20 20:52:20 | [diff] [blame] | 464 | // Notify BrowserList to keep the application running so it doesn't go away |
| 465 | // when all the browser windows get closed. |
| 466 | BrowserList::StartKeepAlive(); |
[email protected] | 136140c | 2009-05-19 13:58:25 | [diff] [blame] | 467 | |
[email protected] | e557502 | 2009-08-22 00:31:22 | [diff] [blame] | 468 | bookmarkMenuBridge_.reset(new BookmarkMenuBridge([self defaultProfile])); |
[email protected] | 1c84c82c | 2009-07-27 15:37:25 | [diff] [blame] | 469 | historyMenuBridge_.reset(new HistoryMenuBridge([self defaultProfile])); |
[email protected] | cd63ef6 | 2009-05-06 19:41:37 | [diff] [blame] | 470 | |
[email protected] | 414fde59 | 2009-05-21 16:14:43 | [diff] [blame] | 471 | [self setUpdateCheckInterval]; |
[email protected] | 2bcec61 | 2009-05-14 17:50:53 | [diff] [blame] | 472 | |
| 473 | // Build up the encoding menu, the order of the items differs based on the |
| 474 | // current locale (see http://crbug.com/7647 for details). |
| 475 | // We need a valid g_browser_process to get the profile which is why we can't |
| 476 | // call this from awakeFromNib. |
[email protected] | fc34932 | 2009-09-23 22:28:23 | [diff] [blame] | 477 | NSMenu* view_menu = [[[NSApp mainMenu] itemWithTag:IDC_VIEW_MENU] submenu]; |
| 478 | NSMenuItem* encoding_menu_item = [view_menu itemWithTag:IDC_ENCODING_MENU]; |
[email protected] | 7dc8c6b | 2010-04-09 17:02:50 | [diff] [blame] | 479 | NSMenu* encoding_menu = [encoding_menu_item submenu]; |
[email protected] | fc34932 | 2009-09-23 22:28:23 | [diff] [blame] | 480 | EncodingMenuControllerDelegate::BuildEncodingMenu([self defaultProfile], |
| 481 | encoding_menu); |
[email protected] | 2bcec61 | 2009-05-14 17:50:53 | [diff] [blame] | 482 | |
[email protected] | f2c20fa | 2009-12-01 17:42:02 | [diff] [blame] | 483 | // Since Chrome is localized to more languages than the OS, tell Cocoa which |
| 484 | // menu is the Help so it can add the search item to it. |
[email protected] | caadfca | 2010-01-22 21:51:21 | [diff] [blame] | 485 | if (helpMenu_ && [NSApp respondsToSelector:@selector(setHelpMenu:)]) |
[email protected] | f2c20fa | 2009-12-01 17:42:02 | [diff] [blame] | 486 | [NSApp setHelpMenu:helpMenu_]; |
[email protected] | f2c20fa | 2009-12-01 17:42:02 | [diff] [blame] | 487 | |
[email protected] | 12dc3d4 | 2010-02-22 23:37:12 | [diff] [blame] | 488 | // Record the path to the (browser) app bundle; this is used by the app mode |
| 489 | // shim. |
| 490 | RecordLastRunAppBundlePath(); |
| 491 | |
[email protected] | eef99c2 | 2010-08-17 05:55:16 | [diff] [blame] | 492 | // Makes "Services" menu items available. |
| 493 | [self registerServicesMenuTypesTo:[notify object]]; |
| 494 | |
[email protected] | caadfca | 2010-01-22 21:51:21 | [diff] [blame] | 495 | startupComplete_ = YES; |
| 496 | |
| 497 | // TODO(viettrungluu): This is very temporary, since this should be done "in" |
| 498 | // |BrowserMain()|, i.e., this list of startup URLs should be appended to the |
| 499 | // (probably-empty) list of URLs from the command line. |
[email protected] | d630d7d5 | 2010-02-21 00:55:11 | [diff] [blame] | 500 | if (startupUrls_.size()) { |
| 501 | [self openUrls:startupUrls_]; |
| 502 | [self clearStartupUrls]; |
[email protected] | caadfca | 2010-01-22 21:51:21 | [diff] [blame] | 503 | } |
[email protected] | 7c32108 | 2009-02-09 15:35:47 | [diff] [blame] | 504 | } |
| 505 | |
[email protected] | a96ec6a | 2009-11-04 17:27:08 | [diff] [blame] | 506 | // This is called after profiles have been loaded and preferences registered. |
| 507 | // It is safe to access the default profile here. |
| 508 | - (void)applicationDidBecomeActive:(NSNotification*)notify { |
| 509 | NotificationService::current()->Notify(NotificationType::APP_ACTIVATED, |
| 510 | NotificationService::AllSources(), |
| 511 | NotificationService::NoDetails()); |
| 512 | } |
| 513 | |
[email protected] | 73f5b36 | 2009-08-10 23:58:21 | [diff] [blame] | 514 | // Helper function for populating and displaying the in progress downloads at |
| 515 | // exit alert panel. |
| 516 | - (BOOL)userWillWaitForInProgressDownloads:(int)downloadCount { |
[email protected] | e8b5f788 | 2009-09-14 20:59:13 | [diff] [blame] | 517 | NSString* warningText = nil; |
| 518 | NSString* explanationText = nil; |
[email protected] | 73f5b36 | 2009-08-10 23:58:21 | [diff] [blame] | 519 | NSString* waitTitle = nil; |
| 520 | NSString* exitTitle = nil; |
| 521 | |
[email protected] | 75712705 | 2010-04-27 14:42:26 | [diff] [blame] | 522 | string16 product_name = l10n_util::GetStringUTF16(IDS_PRODUCT_NAME); |
[email protected] | 9dcb5966 | 2009-09-23 01:15:50 | [diff] [blame] | 523 | |
[email protected] | 73f5b36 | 2009-08-10 23:58:21 | [diff] [blame] | 524 | // Set the dialog text based on whether or not there are multiple downloads. |
| 525 | if (downloadCount == 1) { |
[email protected] | e8b5f788 | 2009-09-14 20:59:13 | [diff] [blame] | 526 | // Dialog text: warning and explanation. |
[email protected] | 75712705 | 2010-04-27 14:42:26 | [diff] [blame] | 527 | warningText = l10n_util::GetNSStringF( |
| 528 | IDS_SINGLE_DOWNLOAD_REMOVE_CONFIRM_WARNING, product_name); |
| 529 | explanationText = l10n_util::GetNSStringF( |
| 530 | IDS_SINGLE_DOWNLOAD_REMOVE_CONFIRM_EXPLANATION, product_name); |
[email protected] | 73f5b36 | 2009-08-10 23:58:21 | [diff] [blame] | 531 | |
| 532 | // Cancel download and exit button text. |
[email protected] | 75712705 | 2010-04-27 14:42:26 | [diff] [blame] | 533 | exitTitle = l10n_util::GetNSString( |
| 534 | IDS_SINGLE_DOWNLOAD_REMOVE_CONFIRM_OK_BUTTON_LABEL); |
[email protected] | 73f5b36 | 2009-08-10 23:58:21 | [diff] [blame] | 535 | |
| 536 | // Wait for download button text. |
[email protected] | 75712705 | 2010-04-27 14:42:26 | [diff] [blame] | 537 | waitTitle = l10n_util::GetNSString( |
| 538 | IDS_SINGLE_DOWNLOAD_REMOVE_CONFIRM_CANCEL_BUTTON_LABEL); |
[email protected] | 73f5b36 | 2009-08-10 23:58:21 | [diff] [blame] | 539 | } else { |
[email protected] | e8b5f788 | 2009-09-14 20:59:13 | [diff] [blame] | 540 | // Dialog text: warning and explanation. |
[email protected] | 75712705 | 2010-04-27 14:42:26 | [diff] [blame] | 541 | warningText = l10n_util::GetNSStringF( |
| 542 | IDS_MULTIPLE_DOWNLOADS_REMOVE_CONFIRM_WARNING, product_name, |
[email protected] | 528c56d | 2010-07-30 19:28:44 | [diff] [blame] | 543 | base::IntToString16(downloadCount)); |
[email protected] | 75712705 | 2010-04-27 14:42:26 | [diff] [blame] | 544 | explanationText = l10n_util::GetNSStringF( |
| 545 | IDS_MULTIPLE_DOWNLOADS_REMOVE_CONFIRM_EXPLANATION, product_name); |
[email protected] | 73f5b36 | 2009-08-10 23:58:21 | [diff] [blame] | 546 | |
| 547 | // Cancel downloads and exit button text. |
[email protected] | 75712705 | 2010-04-27 14:42:26 | [diff] [blame] | 548 | exitTitle = l10n_util::GetNSString( |
| 549 | IDS_MULTIPLE_DOWNLOADS_REMOVE_CONFIRM_OK_BUTTON_LABEL); |
[email protected] | 73f5b36 | 2009-08-10 23:58:21 | [diff] [blame] | 550 | |
| 551 | // Wait for downloads button text. |
[email protected] | 75712705 | 2010-04-27 14:42:26 | [diff] [blame] | 552 | waitTitle = l10n_util::GetNSString( |
| 553 | IDS_MULTIPLE_DOWNLOADS_REMOVE_CONFIRM_CANCEL_BUTTON_LABEL); |
[email protected] | 73f5b36 | 2009-08-10 23:58:21 | [diff] [blame] | 554 | } |
| 555 | |
| 556 | // 'waitButton' is the default choice. |
[email protected] | e8b5f788 | 2009-09-14 20:59:13 | [diff] [blame] | 557 | int choice = NSRunAlertPanel(warningText, explanationText, |
| 558 | waitTitle, exitTitle, nil); |
[email protected] | 73f5b36 | 2009-08-10 23:58:21 | [diff] [blame] | 559 | return choice == NSAlertDefaultReturn ? YES : NO; |
| 560 | } |
| 561 | |
| 562 | // 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] | 563 | // user to see if we should continue to exit (and thus cancel the downloads), or |
| 564 | // if we should wait. |
| 565 | - (BOOL)shouldQuitWithInProgressDownloads { |
[email protected] | 73f5b36 | 2009-08-10 23:58:21 | [diff] [blame] | 566 | ProfileManager* profile_manager = g_browser_process->profile_manager(); |
| 567 | if (!profile_manager) |
| 568 | return YES; |
[email protected] | 09729a55 | 2009-08-03 23:21:41 | [diff] [blame] | 569 | |
[email protected] | 73f5b36 | 2009-08-10 23:58:21 | [diff] [blame] | 570 | ProfileManager::const_iterator it = profile_manager->begin(); |
| 571 | for (; it != profile_manager->end(); ++it) { |
| 572 | Profile* profile = *it; |
[email protected] | 09729a55 | 2009-08-03 23:21:41 | [diff] [blame] | 573 | DownloadManager* download_manager = profile->GetDownloadManager(); |
[email protected] | 73f5b36 | 2009-08-10 23:58:21 | [diff] [blame] | 574 | if (download_manager && download_manager->in_progress_count() > 0) { |
| 575 | int downloadCount = download_manager->in_progress_count(); |
| 576 | if ([self userWillWaitForInProgressDownloads:downloadCount]) { |
| 577 | // Create a new browser window (if necessary) and navigate to the |
| 578 | // downloads page if the user chooses to wait. |
| 579 | Browser* browser = BrowserList::FindBrowserWithProfile(profile); |
| 580 | if (!browser) { |
| 581 | browser = Browser::Create(profile); |
| 582 | browser->window()->Show(); |
| 583 | } |
| 584 | DCHECK(browser); |
| 585 | browser->ShowDownloadsTab(); |
| 586 | return NO; |
| 587 | } |
[email protected] | 09729a55 | 2009-08-03 23:21:41 | [diff] [blame] | 588 | |
[email protected] | 73f5b36 | 2009-08-10 23:58:21 | [diff] [blame] | 589 | // User wants to exit. |
| 590 | return YES; |
[email protected] | 09729a55 | 2009-08-03 23:21:41 | [diff] [blame] | 591 | } |
[email protected] | 09729a55 | 2009-08-03 23:21:41 | [diff] [blame] | 592 | } |
| 593 | |
[email protected] | 73f5b36 | 2009-08-10 23:58:21 | [diff] [blame] | 594 | // No profiles or active downloads found, okay to exit. |
[email protected] | 09729a55 | 2009-08-03 23:21:41 | [diff] [blame] | 595 | return YES; |
| 596 | } |
| 597 | |
[email protected] | fbc947b | 2009-06-19 13:28:24 | [diff] [blame] | 598 | // Called to determine if we should enable the "restore tab" menu item. |
| 599 | // Checks with the TabRestoreService to see if there's anything there to |
| 600 | // restore and returns YES if so. |
| 601 | - (BOOL)canRestoreTab { |
| 602 | TabRestoreService* service = [self defaultProfile]->GetTabRestoreService(); |
| 603 | return service && !service->entries().empty(); |
| 604 | } |
| 605 | |
[email protected] | f6c15fa | 2010-06-04 20:08:21 | [diff] [blame] | 606 | // Returns true if there is not a modal window (either window- or application- |
| 607 | // modal) blocking the active browser. Note that tab modal dialogs (HTTP auth |
| 608 | // sheets) will not count as blocking the browser. But things like open/save |
| 609 | // dialogs that are window modal will block the browser. |
| 610 | - (BOOL)keyWindowIsNotModal { |
[email protected] | 6a28133 | 2009-10-13 17:41:06 | [diff] [blame] | 611 | Browser* browser = BrowserList::GetLastActive(); |
[email protected] | dc76e836 | 2010-06-11 16:29:52 | [diff] [blame] | 612 | return [NSApp modalWindow] == nil && (!browser || |
[email protected] | 6a28133 | 2009-10-13 17:41:06 | [diff] [blame] | 613 | ![[browser->window()->GetNativeHandle() attachedSheet] |
[email protected] | f6c15fa | 2010-06-04 20:08:21 | [diff] [blame] | 614 | isKindOfClass:[NSWindow class]]); |
[email protected] | 6a28133 | 2009-10-13 17:41:06 | [diff] [blame] | 615 | } |
| 616 | |
[email protected] | 88d7494 | 2009-01-21 22:04:44 | [diff] [blame] | 617 | // Called to validate menu items when there are no key windows. All the |
| 618 | // items we care about have been set with the |commandDispatch:| action and |
| 619 | // a target of FirstResponder in IB. If it's not one of those, let it |
| 620 | // continue up the responder chain to be handled elsewhere. We pull out the |
| 621 | // tag as the cross-platform constant to differentiate and dispatch the |
| 622 | // various commands. |
| 623 | - (BOOL)validateUserInterfaceItem:(id<NSValidatedUserInterfaceItem>)item { |
| 624 | SEL action = [item action]; |
| 625 | BOOL enable = NO; |
| 626 | if (action == @selector(commandDispatch:)) { |
| 627 | NSInteger tag = [item tag]; |
[email protected] | fbc947b | 2009-06-19 13:28:24 | [diff] [blame] | 628 | if (menuState_->SupportsCommand(tag)) { |
| 629 | switch (tag) { |
[email protected] | 419eb2a | 2009-11-11 16:26:26 | [diff] [blame] | 630 | // The File Menu commands are not automatically disabled by Cocoa when a |
| 631 | // dialog sheet obscures the browser window, so we disable several of |
| 632 | // them here. We don't need to include IDC_CLOSE_WINDOW, because |
| 633 | // app_controller is only activated when there are no key windows (see |
| 634 | // function comment). |
[email protected] | fbc947b | 2009-06-19 13:28:24 | [diff] [blame] | 635 | case IDC_RESTORE_TAB: |
[email protected] | f6c15fa | 2010-06-04 20:08:21 | [diff] [blame] | 636 | enable = [self keyWindowIsNotModal] && [self canRestoreTab]; |
[email protected] | 6a28133 | 2009-10-13 17:41:06 | [diff] [blame] | 637 | break; |
[email protected] | f6c15fa | 2010-06-04 20:08:21 | [diff] [blame] | 638 | // Browser-level items that open in new tabs should not open if there's |
| 639 | // a window- or app-modal dialog. |
[email protected] | 6a28133 | 2009-10-13 17:41:06 | [diff] [blame] | 640 | case IDC_OPEN_FILE: |
[email protected] | 6a28133 | 2009-10-13 17:41:06 | [diff] [blame] | 641 | case IDC_NEW_TAB: |
[email protected] | f6c15fa | 2010-06-04 20:08:21 | [diff] [blame] | 642 | case IDC_SHOW_HISTORY: |
| 643 | case IDC_SHOW_BOOKMARK_MANAGER: |
| 644 | enable = [self keyWindowIsNotModal]; |
| 645 | break; |
| 646 | // Browser-level items that open in new windows. |
| 647 | case IDC_NEW_WINDOW: |
| 648 | case IDC_TASK_MANAGER: |
| 649 | // Allow the user to open a new window if there's a window-modal |
| 650 | // dialog. |
| 651 | enable = [self keyWindowIsNotModal] || ([NSApp modalWindow] == nil); |
[email protected] | fbc947b | 2009-06-19 13:28:24 | [diff] [blame] | 652 | break; |
[email protected] | 446e16a | 2010-05-03 21:23:58 | [diff] [blame] | 653 | case IDC_SYNC_BOOKMARKS: { |
| 654 | Profile* defaultProfile = [self defaultProfile]; |
| 655 | // The profile may be NULL during shutdown -- see |
| 656 | // http://code.google.com/p/chromium/issues/detail?id=43048 . |
| 657 | // |
| 658 | // TODO(akalin,viettrungluu): Figure out whether this method |
| 659 | // can be prevented from being called if defaultProfile is |
| 660 | // NULL. |
| 661 | if (!defaultProfile) { |
| 662 | LOG(WARNING) |
| 663 | << "NULL defaultProfile detected -- not doing anything"; |
| 664 | break; |
| 665 | } |
[email protected] | d3b98c8 | 2010-07-14 07:45:59 | [diff] [blame] | 666 | enable = defaultProfile->IsSyncAccessible() && |
| 667 | [self keyWindowIsNotModal]; |
[email protected] | 446e16a | 2010-05-03 21:23:58 | [diff] [blame] | 668 | sync_ui_util::UpdateSyncItem(item, enable, defaultProfile); |
[email protected] | f9bc9b9 | 2009-11-24 00:55:35 | [diff] [blame] | 669 | break; |
[email protected] | 446e16a | 2010-05-03 21:23:58 | [diff] [blame] | 670 | } |
[email protected] | fbc947b | 2009-06-19 13:28:24 | [diff] [blame] | 671 | default: |
[email protected] | f6c15fa | 2010-06-04 20:08:21 | [diff] [blame] | 672 | enable = menuState_->IsCommandEnabled(tag) ? |
| 673 | [self keyWindowIsNotModal] : NO; |
[email protected] | fbc947b | 2009-06-19 13:28:24 | [diff] [blame] | 674 | } |
| 675 | } |
[email protected] | 3b6aa8b6 | 2009-09-15 21:36:11 | [diff] [blame] | 676 | } else if (action == @selector(terminate:)) { |
[email protected] | 88d7494 | 2009-01-21 22:04:44 | [diff] [blame] | 677 | enable = YES; |
[email protected] | 3111f08b | 2009-04-30 16:01:52 | [diff] [blame] | 678 | } else if (action == @selector(showPreferences:)) { |
| 679 | enable = YES; |
[email protected] | bde3dda | 2009-05-20 22:13:07 | [diff] [blame] | 680 | } else if (action == @selector(orderFrontStandardAboutPanel:)) { |
| 681 | enable = YES; |
[email protected] | 3d4bd23f | 2009-10-06 03:58:38 | [diff] [blame] | 682 | } else if (action == @selector(newWindowFromDock:)) { |
| 683 | enable = YES; |
[email protected] | 88d7494 | 2009-01-21 22:04:44 | [diff] [blame] | 684 | } |
| 685 | return enable; |
| 686 | } |
| 687 | |
[email protected] | 947fc0d | 2010-01-14 22:33:05 | [diff] [blame] | 688 | // Called when the user picks a menu item when there are no key windows, or when |
| 689 | // there is no foreground browser window. Calls through to the browser object to |
| 690 | // execute the command. This assumes that the command is supported and doesn't |
[email protected] | d630d7d5 | 2010-02-21 00:55:11 | [diff] [blame] | 691 | // check, otherwise it should have been disabled in the UI in |
| 692 | // |-validateUserInterfaceItem:|. |
[email protected] | 88d7494 | 2009-01-21 22:04:44 | [diff] [blame] | 693 | - (void)commandDispatch:(id)sender { |
[email protected] | 93e18176 | 2009-05-29 14:30:38 | [diff] [blame] | 694 | Profile* defaultProfile = [self defaultProfile]; |
[email protected] | f0a51fb5 | 2009-03-05 12:46:38 | [diff] [blame] | 695 | |
[email protected] | 947fc0d | 2010-01-14 22:33:05 | [diff] [blame] | 696 | // Handle the case where we're dispatching a command from a sender that's in a |
| 697 | // browser window. This means that the command came from a background window |
| 698 | // and is getting here because the foreground window is not a browser window. |
| 699 | if ([sender respondsToSelector:@selector(window)]) { |
| 700 | id delegate = [[sender window] windowController]; |
| 701 | if ([delegate isKindOfClass:[BrowserWindowController class]]) { |
| 702 | [delegate commandDispatch:sender]; |
| 703 | return; |
| 704 | } |
| 705 | } |
| 706 | |
[email protected] | 88d7494 | 2009-01-21 22:04:44 | [diff] [blame] | 707 | NSInteger tag = [sender tag]; |
| 708 | switch (tag) { |
[email protected] | fbc947b | 2009-06-19 13:28:24 | [diff] [blame] | 709 | case IDC_NEW_TAB: |
[email protected] | d630d7d5 | 2010-02-21 00:55:11 | [diff] [blame] | 710 | // Create a new tab in an existing browser window (which we activate) if |
| 711 | // possible. |
| 712 | if (Browser* browser = ActivateBrowser(defaultProfile)) { |
| 713 | browser->ExecuteCommand(IDC_NEW_TAB); |
| 714 | break; |
| 715 | } |
| 716 | // Else fall through to create new window. |
[email protected] | 88d7494 | 2009-01-21 22:04:44 | [diff] [blame] | 717 | case IDC_NEW_WINDOW: |
[email protected] | d630d7d5 | 2010-02-21 00:55:11 | [diff] [blame] | 718 | CreateBrowser(defaultProfile); |
| 719 | break; |
[email protected] | 91a5b3e | 2009-10-30 19:32:22 | [diff] [blame] | 720 | case IDC_FOCUS_LOCATION: |
[email protected] | d630d7d5 | 2010-02-21 00:55:11 | [diff] [blame] | 721 | ActivateOrCreateBrowser(defaultProfile)-> |
| 722 | ExecuteCommand(IDC_FOCUS_LOCATION); |
[email protected] | 88d7494 | 2009-01-21 22:04:44 | [diff] [blame] | 723 | break; |
[email protected] | cddc524 | 2009-12-07 18:54:33 | [diff] [blame] | 724 | case IDC_FOCUS_SEARCH: |
[email protected] | d630d7d5 | 2010-02-21 00:55:11 | [diff] [blame] | 725 | ActivateOrCreateBrowser(defaultProfile)->ExecuteCommand(IDC_FOCUS_SEARCH); |
[email protected] | cddc524 | 2009-12-07 18:54:33 | [diff] [blame] | 726 | break; |
[email protected] | 863ff66 | 2009-01-26 20:18:18 | [diff] [blame] | 727 | case IDC_NEW_INCOGNITO_WINDOW: |
[email protected] | e9e21127 | 2009-09-22 16:39:11 | [diff] [blame] | 728 | Browser::OpenEmptyWindow(defaultProfile->GetOffTheRecordProfile()); |
[email protected] | 863ff66 | 2009-01-26 20:18:18 | [diff] [blame] | 729 | break; |
[email protected] | fbc947b | 2009-06-19 13:28:24 | [diff] [blame] | 730 | case IDC_RESTORE_TAB: |
| 731 | Browser::OpenWindowWithRestoredTabs(defaultProfile); |
| 732 | break; |
[email protected] | e19516d | 2009-04-28 17:15:19 | [diff] [blame] | 733 | case IDC_OPEN_FILE: |
[email protected] | d630d7d5 | 2010-02-21 00:55:11 | [diff] [blame] | 734 | CreateBrowser(defaultProfile)->ExecuteCommand(IDC_OPEN_FILE); |
[email protected] | e19516d | 2009-04-28 17:15:19 | [diff] [blame] | 735 | break; |
[email protected] | 1fdff70 | 2009-10-22 00:36:18 | [diff] [blame] | 736 | case IDC_CLEAR_BROWSING_DATA: { |
[email protected] | 93e18176 | 2009-05-29 14:30:38 | [diff] [blame] | 737 | // There may not be a browser open, so use the default profile. |
[email protected] | 049ac83 | 2009-12-15 16:26:54 | [diff] [blame] | 738 | [ClearBrowsingDataController |
| 739 | showClearBrowsingDialogForProfile:defaultProfile]; |
[email protected] | 93e18176 | 2009-05-29 14:30:38 | [diff] [blame] | 740 | break; |
[email protected] | 1fdff70 | 2009-10-22 00:36:18 | [diff] [blame] | 741 | } |
[email protected] | 4aee565 | 2009-11-19 18:59:21 | [diff] [blame] | 742 | case IDC_IMPORT_SETTINGS: { |
[email protected] | 89622004 | 2010-03-23 18:14:28 | [diff] [blame] | 743 | UserMetrics::RecordAction(UserMetricsAction("Import_ShowDlg"), |
| 744 | defaultProfile); |
[email protected] | 2989cce1 | 2010-02-01 10:56:53 | [diff] [blame] | 745 | [ImportSettingsDialogController |
| 746 | showImportSettingsDialogForProfile:defaultProfile]; |
[email protected] | 4aee565 | 2009-11-19 18:59:21 | [diff] [blame] | 747 | break; |
| 748 | } |
[email protected] | af7c25e | 2010-01-23 05:46:45 | [diff] [blame] | 749 | case IDC_SHOW_BOOKMARK_MANAGER: |
[email protected] | 89622004 | 2010-03-23 18:14:28 | [diff] [blame] | 750 | UserMetrics::RecordAction(UserMetricsAction("ShowBookmarkManager"), |
| 751 | defaultProfile); |
[email protected] | 90a8bf25 | 2010-03-12 00:09:47 | [diff] [blame] | 752 | if (Browser* browser = ActivateBrowser(defaultProfile)) { |
[email protected] | 47e532b | 2010-04-23 17:46:40 | [diff] [blame] | 753 | // Open a bookmark manager tab. |
[email protected] | 90a8bf25 | 2010-03-12 00:09:47 | [diff] [blame] | 754 | browser->OpenBookmarkManager(); |
| 755 | } else { |
[email protected] | 47e532b | 2010-04-23 17:46:40 | [diff] [blame] | 756 | // No browser window, so create one for the bookmark manager tab. |
| 757 | Browser::OpenBookmarkManagerWindow(defaultProfile); |
[email protected] | 90a8bf25 | 2010-03-12 00:09:47 | [diff] [blame] | 758 | } |
[email protected] | af7c25e | 2010-01-23 05:46:45 | [diff] [blame] | 759 | break; |
[email protected] | 91a5b3e | 2009-10-30 19:32:22 | [diff] [blame] | 760 | case IDC_SHOW_HISTORY: |
[email protected] | d630d7d5 | 2010-02-21 00:55:11 | [diff] [blame] | 761 | if (Browser* browser = ActivateBrowser(defaultProfile)) |
| 762 | browser->ShowHistoryTab(); |
| 763 | else |
| 764 | Browser::OpenHistoryWindow(defaultProfile); |
[email protected] | 91a5b3e | 2009-10-30 19:32:22 | [diff] [blame] | 765 | break; |
| 766 | case IDC_SHOW_DOWNLOADS: |
[email protected] | d630d7d5 | 2010-02-21 00:55:11 | [diff] [blame] | 767 | if (Browser* browser = ActivateBrowser(defaultProfile)) |
| 768 | browser->ShowDownloadsTab(); |
| 769 | else |
| 770 | Browser::OpenDownloadsWindow(defaultProfile); |
[email protected] | 91a5b3e | 2009-10-30 19:32:22 | [diff] [blame] | 771 | break; |
[email protected] | bb37e6ec | 2009-11-18 17:59:36 | [diff] [blame] | 772 | case IDC_MANAGE_EXTENSIONS: |
[email protected] | d630d7d5 | 2010-02-21 00:55:11 | [diff] [blame] | 773 | if (Browser* browser = ActivateBrowser(defaultProfile)) |
| 774 | browser->ShowExtensionsTab(); |
| 775 | else |
| 776 | Browser::OpenExtensionsWindow(defaultProfile); |
[email protected] | bb37e6ec | 2009-11-18 17:59:36 | [diff] [blame] | 777 | break; |
[email protected] | 1fdff70 | 2009-10-22 00:36:18 | [diff] [blame] | 778 | case IDC_HELP_PAGE: |
[email protected] | d630d7d5 | 2010-02-21 00:55:11 | [diff] [blame] | 779 | if (Browser* browser = ActivateBrowser(defaultProfile)) |
| 780 | browser->OpenHelpTab(); |
| 781 | else |
| 782 | Browser::OpenHelpWindow(defaultProfile); |
[email protected] | 1fdff70 | 2009-10-22 00:36:18 | [diff] [blame] | 783 | break; |
[email protected] | 3896b18 | 2009-11-03 16:15:32 | [diff] [blame] | 784 | case IDC_REPORT_BUG: { |
| 785 | Browser* browser = BrowserList::GetLastActive(); |
[email protected] | d630d7d5 | 2010-02-21 00:55:11 | [diff] [blame] | 786 | TabContents* currentTab = |
| 787 | browser ? browser->GetSelectedTabContents() : NULL; |
[email protected] | 3896b18 | 2009-11-03 16:15:32 | [diff] [blame] | 788 | BugReportWindowController* controller = |
| 789 | [[BugReportWindowController alloc] |
[email protected] | d630d7d5 | 2010-02-21 00:55:11 | [diff] [blame] | 790 | initWithTabContents:currentTab |
| 791 | profile:[self defaultProfile]]; |
[email protected] | 3896b18 | 2009-11-03 16:15:32 | [diff] [blame] | 792 | [controller runModalDialog]; |
| 793 | break; |
| 794 | } |
[email protected] | f9bc9b9 | 2009-11-24 00:55:35 | [diff] [blame] | 795 | case IDC_SYNC_BOOKMARKS: |
[email protected] | 446e16a | 2010-05-03 21:23:58 | [diff] [blame] | 796 | // The profile may be NULL during shutdown -- see |
| 797 | // http://code.google.com/p/chromium/issues/detail?id=43048 . |
| 798 | // |
| 799 | // TODO(akalin,viettrungluu): Figure out whether this method can |
| 800 | // be prevented from being called if defaultProfile is NULL. |
| 801 | if (!defaultProfile) { |
| 802 | LOG(WARNING) << "NULL defaultProfile detected -- not doing anything"; |
| 803 | break; |
| 804 | } |
[email protected] | f9bc9b9 | 2009-11-24 00:55:35 | [diff] [blame] | 805 | // TODO(akalin): Add a constant to denote starting sync from the |
| 806 | // main menu and use that instead of START_FROM_WRENCH. |
[email protected] | 40f04797 | 2009-11-25 03:54:40 | [diff] [blame] | 807 | sync_ui_util::OpenSyncMyBookmarksDialog( |
[email protected] | f9bc9b9 | 2009-11-24 00:55:35 | [diff] [blame] | 808 | defaultProfile, ProfileSyncService::START_FROM_WRENCH); |
| 809 | break; |
[email protected] | ceaaf181 | 2010-01-20 22:13:09 | [diff] [blame] | 810 | case IDC_TASK_MANAGER: |
[email protected] | 89622004 | 2010-03-23 18:14:28 | [diff] [blame] | 811 | UserMetrics::RecordAction(UserMetricsAction("TaskManager"), |
| 812 | defaultProfile); |
[email protected] | ceaaf181 | 2010-01-20 22:13:09 | [diff] [blame] | 813 | TaskManagerMac::Show(); |
| 814 | break; |
[email protected] | 4a42d27 | 2010-06-18 01:29:42 | [diff] [blame] | 815 | case IDC_OPTIONS: |
| 816 | [self showPreferences:sender]; |
| 817 | break; |
[email protected] | d630d7d5 | 2010-02-21 00:55:11 | [diff] [blame] | 818 | } |
[email protected] | 88d7494 | 2009-01-21 22:04:44 | [diff] [blame] | 819 | } |
| 820 | |
[email protected] | 947fc0d | 2010-01-14 22:33:05 | [diff] [blame] | 821 | // Same as |-commandDispatch:|, but executes commands using a disposition |
| 822 | // determined by the key flags. This will get called in the case where the |
| 823 | // frontmost window is not a browser window, and the user has command-clicked |
| 824 | // a button in a background browser window whose action is |
| 825 | // |-commandDispatchUsingKeyModifiers:| |
| 826 | - (void)commandDispatchUsingKeyModifiers:(id)sender { |
| 827 | DCHECK(sender); |
| 828 | if ([sender respondsToSelector:@selector(window)]) { |
| 829 | id delegate = [[sender window] windowController]; |
| 830 | if ([delegate isKindOfClass:[BrowserWindowController class]]) { |
| 831 | [delegate commandDispatchUsingKeyModifiers:sender]; |
| 832 | } |
| 833 | } |
| 834 | } |
| 835 | |
[email protected] | ff81e0a | 2009-04-13 14:58:54 | [diff] [blame] | 836 | // NSApplication delegate method called when someone clicks on the |
| 837 | // dock icon and there are no open windows. To match standard mac |
| 838 | // behavior, we should open a new window. |
| 839 | - (BOOL)applicationShouldHandleReopen:(NSApplication*)theApplication |
| 840 | hasVisibleWindows:(BOOL)flag { |
[email protected] | 7063140 | 2010-04-26 20:16:54 | [diff] [blame] | 841 | // If the browser is currently trying to quit, don't do anything and return NO |
| 842 | // to prevent AppKit from doing anything. |
| 843 | // TODO(rohitrao): Remove this code when http://crbug.com/40861 is resolved. |
| 844 | if (browser_shutdown::IsTryingToQuit()) |
| 845 | return NO; |
| 846 | |
[email protected] | ff81e0a | 2009-04-13 14:58:54 | [diff] [blame] | 847 | // Don't do anything if there are visible windows. This will cause |
| 848 | // AppKit to unminimize the most recently minimized window. |
| 849 | if (flag) |
| 850 | return YES; |
| 851 | |
| 852 | // Otherwise open a new window. |
[email protected] | d630d7d5 | 2010-02-21 00:55:11 | [diff] [blame] | 853 | { |
[email protected] | 0fbd7033 | 2010-06-01 19:28:34 | [diff] [blame] | 854 | AutoReset<bool> auto_reset_in_run(&g_is_opening_new_window, true); |
[email protected] | d630d7d5 | 2010-02-21 00:55:11 | [diff] [blame] | 855 | Browser::OpenEmptyWindow([self defaultProfile]); |
| 856 | } |
[email protected] | ff81e0a | 2009-04-13 14:58:54 | [diff] [blame] | 857 | |
| 858 | // We've handled the reopen event, so return NO to tell AppKit not |
| 859 | // to do anything. |
| 860 | return NO; |
| 861 | } |
| 862 | |
[email protected] | 88d7494 | 2009-01-21 22:04:44 | [diff] [blame] | 863 | - (void)initMenuState { |
[email protected] | 3111f08b | 2009-04-30 16:01:52 | [diff] [blame] | 864 | menuState_.reset(new CommandUpdater(NULL)); |
[email protected] | fbc947b | 2009-06-19 13:28:24 | [diff] [blame] | 865 | menuState_->UpdateCommandEnabled(IDC_NEW_TAB, true); |
[email protected] | 88d7494 | 2009-01-21 22:04:44 | [diff] [blame] | 866 | menuState_->UpdateCommandEnabled(IDC_NEW_WINDOW, true); |
[email protected] | 863ff66 | 2009-01-26 20:18:18 | [diff] [blame] | 867 | menuState_->UpdateCommandEnabled(IDC_NEW_INCOGNITO_WINDOW, true); |
[email protected] | e19516d | 2009-04-28 17:15:19 | [diff] [blame] | 868 | menuState_->UpdateCommandEnabled(IDC_OPEN_FILE, true); |
[email protected] | 93e18176 | 2009-05-29 14:30:38 | [diff] [blame] | 869 | menuState_->UpdateCommandEnabled(IDC_CLEAR_BROWSING_DATA, true); |
[email protected] | fbc947b | 2009-06-19 13:28:24 | [diff] [blame] | 870 | menuState_->UpdateCommandEnabled(IDC_RESTORE_TAB, false); |
[email protected] | 91a5b3e | 2009-10-30 19:32:22 | [diff] [blame] | 871 | menuState_->UpdateCommandEnabled(IDC_FOCUS_LOCATION, true); |
[email protected] | cddc524 | 2009-12-07 18:54:33 | [diff] [blame] | 872 | menuState_->UpdateCommandEnabled(IDC_FOCUS_SEARCH, true); |
[email protected] | af7c25e | 2010-01-23 05:46:45 | [diff] [blame] | 873 | menuState_->UpdateCommandEnabled(IDC_SHOW_BOOKMARK_MANAGER, true); |
[email protected] | 91a5b3e | 2009-10-30 19:32:22 | [diff] [blame] | 874 | menuState_->UpdateCommandEnabled(IDC_SHOW_HISTORY, true); |
| 875 | menuState_->UpdateCommandEnabled(IDC_SHOW_DOWNLOADS, true); |
[email protected] | bb37e6ec | 2009-11-18 17:59:36 | [diff] [blame] | 876 | menuState_->UpdateCommandEnabled(IDC_MANAGE_EXTENSIONS, true); |
[email protected] | 1fdff70 | 2009-10-22 00:36:18 | [diff] [blame] | 877 | menuState_->UpdateCommandEnabled(IDC_HELP_PAGE, true); |
[email protected] | 4aee565 | 2009-11-19 18:59:21 | [diff] [blame] | 878 | menuState_->UpdateCommandEnabled(IDC_IMPORT_SETTINGS, true); |
[email protected] | 3896b18 | 2009-11-03 16:15:32 | [diff] [blame] | 879 | menuState_->UpdateCommandEnabled(IDC_REPORT_BUG, true); |
[email protected] | d3b98c8 | 2010-07-14 07:45:59 | [diff] [blame] | 880 | menuState_->UpdateCommandEnabled(IDC_SYNC_BOOKMARKS, true); |
[email protected] | ceaaf181 | 2010-01-20 22:13:09 | [diff] [blame] | 881 | menuState_->UpdateCommandEnabled(IDC_TASK_MANAGER, true); |
[email protected] | 88d7494 | 2009-01-21 22:04:44 | [diff] [blame] | 882 | } |
[email protected] | 1bcdb53 | 2009-01-16 17:47:57 | [diff] [blame] | 883 | |
[email protected] | eef99c2 | 2010-08-17 05:55:16 | [diff] [blame] | 884 | - (void)registerServicesMenuTypesTo:(NSApplication*)app { |
[email protected] | 09ea24c | 2010-08-23 19:05:41 | [diff] [blame^] | 885 | // Note that RenderWidgetHostViewCocoa implements NSServicesRequests which |
| 886 | // handles requests from services. |
| 887 | NSArray* types = [NSArray arrayWithObjects:NSStringPboardType, nil]; |
| 888 | [app registerServicesMenuSendTypes:types returnTypes:types]; |
[email protected] | eef99c2 | 2010-08-17 05:55:16 | [diff] [blame] | 889 | } |
| 890 | |
[email protected] | 3f34599d | 2009-03-25 22:11:43 | [diff] [blame] | 891 | - (Profile*)defaultProfile { |
[email protected] | 3f34599d | 2009-03-25 22:11:43 | [diff] [blame] | 892 | // TODO(jrg): Find a better way to get the "default" profile. |
| 893 | if (g_browser_process->profile_manager()) |
[email protected] | 065566b | 2010-01-20 20:38:26 | [diff] [blame] | 894 | return *g_browser_process->profile_manager()->begin(); |
[email protected] | 3f34599d | 2009-03-25 22:11:43 | [diff] [blame] | 895 | |
[email protected] | f631400 | 2009-04-23 01:18:13 | [diff] [blame] | 896 | return NULL; |
[email protected] | 3f34599d | 2009-03-25 22:11:43 | [diff] [blame] | 897 | } |
| 898 | |
[email protected] | 57750f82 | 2009-04-21 21:43:09 | [diff] [blame] | 899 | // Various methods to open URLs that we get in a native fashion. We use |
| 900 | // BrowserInit here because on the other platforms, URLs to open come through |
| 901 | // the ProcessSingleton, and it calls BrowserInit. It's best to bottleneck the |
| 902 | // openings through that for uniform handling. |
| 903 | |
[email protected] | d630d7d5 | 2010-02-21 00:55:11 | [diff] [blame] | 904 | - (void)openUrls:(const std::vector<GURL>&)urls { |
[email protected] | caadfca | 2010-01-22 21:51:21 | [diff] [blame] | 905 | // If the browser hasn't started yet, just queue up the URLs. |
| 906 | if (!startupComplete_) { |
[email protected] | d630d7d5 | 2010-02-21 00:55:11 | [diff] [blame] | 907 | startupUrls_.insert(startupUrls_.end(), urls.begin(), urls.end()); |
[email protected] | 136140c | 2009-05-19 13:58:25 | [diff] [blame] | 908 | return; |
| 909 | } |
[email protected] | 57750f82 | 2009-04-21 21:43:09 | [diff] [blame] | 910 | |
[email protected] | 0e86351 | 2009-05-28 19:45:07 | [diff] [blame] | 911 | Browser* browser = BrowserList::GetLastActive(); |
| 912 | // if no browser window exists then create one with no tabs to be filled in |
| 913 | if (!browser) { |
| 914 | browser = Browser::Create([self defaultProfile]); |
[email protected] | ce560f8 | 2009-06-03 09:39:44 | [diff] [blame] | 915 | browser->window()->Show(); |
[email protected] | 0e86351 | 2009-05-28 19:45:07 | [diff] [blame] | 916 | } |
| 917 | |
[email protected] | 51343d5a | 2009-10-26 22:39:33 | [diff] [blame] | 918 | CommandLine dummy(CommandLine::ARGUMENTS_ONLY); |
[email protected] | f805fe8 | 2010-08-03 22:47:10 | [diff] [blame] | 919 | BrowserInit::LaunchWithProfile launch(FilePath(), dummy); |
[email protected] | 0e86351 | 2009-05-28 19:45:07 | [diff] [blame] | 920 | launch.OpenURLsInBrowser(browser, false, urls); |
[email protected] | 57750f82 | 2009-04-21 21:43:09 | [diff] [blame] | 921 | } |
| 922 | |
[email protected] | 57750f82 | 2009-04-21 21:43:09 | [diff] [blame] | 923 | - (void)getUrl:(NSAppleEventDescriptor*)event |
| 924 | withReply:(NSAppleEventDescriptor*)reply { |
| 925 | NSString* urlStr = [[event paramDescriptorForKeyword:keyDirectObject] |
| 926 | stringValue]; |
| 927 | |
| 928 | GURL gurl(base::SysNSStringToUTF8(urlStr)); |
| 929 | std::vector<GURL> gurlVector; |
| 930 | gurlVector.push_back(gurl); |
| 931 | |
[email protected] | d630d7d5 | 2010-02-21 00:55:11 | [diff] [blame] | 932 | [self openUrls:gurlVector]; |
[email protected] | 57750f82 | 2009-04-21 21:43:09 | [diff] [blame] | 933 | } |
| 934 | |
[email protected] | caadfca | 2010-01-22 21:51:21 | [diff] [blame] | 935 | - (void)application:(NSApplication*)sender |
| 936 | openFiles:(NSArray*)filenames { |
[email protected] | 57750f82 | 2009-04-21 21:43:09 | [diff] [blame] | 937 | std::vector<GURL> gurlVector; |
[email protected] | caadfca | 2010-01-22 21:51:21 | [diff] [blame] | 938 | for (NSString* file in filenames) { |
| 939 | GURL gurl = net::FilePathToFileURL(FilePath(base::SysNSStringToUTF8(file))); |
[email protected] | 57750f82 | 2009-04-21 21:43:09 | [diff] [blame] | 940 | gurlVector.push_back(gurl); |
| 941 | } |
[email protected] | caadfca | 2010-01-22 21:51:21 | [diff] [blame] | 942 | if (!gurlVector.empty()) |
[email protected] | d630d7d5 | 2010-02-21 00:55:11 | [diff] [blame] | 943 | [self openUrls:gurlVector]; |
[email protected] | caadfca | 2010-01-22 21:51:21 | [diff] [blame] | 944 | else |
| 945 | NOTREACHED() << "Nothing to open!"; |
[email protected] | 57750f82 | 2009-04-21 21:43:09 | [diff] [blame] | 946 | |
[email protected] | caadfca | 2010-01-22 21:51:21 | [diff] [blame] | 947 | [sender replyToOpenOrPrint:NSApplicationDelegateReplySuccess]; |
[email protected] | 57750f82 | 2009-04-21 21:43:09 | [diff] [blame] | 948 | } |
[email protected] | 3f34599d | 2009-03-25 22:11:43 | [diff] [blame] | 949 | |
[email protected] | 767543d | 2009-04-30 19:23:58 | [diff] [blame] | 950 | // Called when the preferences window is closed. We use this to release the |
| 951 | // window controller. |
[email protected] | d4e1865 | 2009-11-19 21:52:16 | [diff] [blame] | 952 | - (void)prefsWindowClosed:(NSNotification*)notification { |
| 953 | NSWindow* window = [prefsController_ window]; |
| 954 | DCHECK([notification object] == window); |
| 955 | NSNotificationCenter* defaultCenter = [NSNotificationCenter defaultCenter]; |
| 956 | [defaultCenter removeObserver:self |
| 957 | name:NSWindowWillCloseNotification |
| 958 | object:window]; |
| 959 | // PreferencesWindowControllers are autoreleased in |
| 960 | // -[PreferencesWindowController windowWillClose:]. |
| 961 | prefsController_ = nil; |
[email protected] | 767543d | 2009-04-30 19:23:58 | [diff] [blame] | 962 | } |
| 963 | |
[email protected] | 3111f08b | 2009-04-30 16:01:52 | [diff] [blame] | 964 | // Show the preferences window, or bring it to the front if it's already |
| 965 | // visible. |
| 966 | - (IBAction)showPreferences:(id)sender { |
[email protected] | 4a42d27 | 2010-06-18 01:29:42 | [diff] [blame] | 967 | const CommandLine& parsed_command_line = *CommandLine::ForCurrentProcess(); |
| 968 | if (parsed_command_line.HasSwitch(switches::kEnableTabbedOptions)) { |
| 969 | if (Browser* browser = ActivateBrowser([self defaultProfile])) { |
| 970 | // Show options tab in the active browser window. |
[email protected] | 024617a | 2010-08-20 05:08:05 | [diff] [blame] | 971 | browser->ShowOptionsTab(chrome::kDefaultOptionsSubPage); |
[email protected] | 4a42d27 | 2010-06-18 01:29:42 | [diff] [blame] | 972 | } else { |
| 973 | // No browser window, so create one for the options tab. |
| 974 | Browser::OpenOptionsWindow([self defaultProfile]); |
| 975 | } |
| 976 | } else { |
| 977 | [self showPreferencesWindow:sender |
| 978 | page:OPTIONS_PAGE_DEFAULT |
| 979 | profile:[self defaultProfile]]; |
| 980 | } |
[email protected] | d989f07c | 2009-11-14 00:35:46 | [diff] [blame] | 981 | } |
| 982 | |
| 983 | - (void)showPreferencesWindow:(id)sender |
| 984 | page:(OptionsPage)page |
| 985 | profile:(Profile*)profile { |
[email protected] | d4e1865 | 2009-11-19 21:52:16 | [diff] [blame] | 986 | if (prefsController_) { |
[email protected] | d989f07c | 2009-11-14 00:35:46 | [diff] [blame] | 987 | [prefsController_ switchToPage:page animate:YES]; |
| 988 | } else { |
[email protected] | d4e1865 | 2009-11-19 21:52:16 | [diff] [blame] | 989 | prefsController_ = |
| 990 | [[PreferencesWindowController alloc] initWithProfile:profile |
| 991 | initialPage:page]; |
[email protected] | 767543d | 2009-04-30 19:23:58 | [diff] [blame] | 992 | // Watch for a notification of when it goes away so that we can destroy |
| 993 | // the controller. |
| 994 | [[NSNotificationCenter defaultCenter] |
| 995 | addObserver:self |
| 996 | selector:@selector(prefsWindowClosed:) |
[email protected] | d4e1865 | 2009-11-19 21:52:16 | [diff] [blame] | 997 | name:NSWindowWillCloseNotification |
| 998 | object:[prefsController_ window]]; |
[email protected] | 4240438 | 2009-04-30 17:59:24 | [diff] [blame] | 999 | } |
| 1000 | [prefsController_ showPreferences:sender]; |
[email protected] | 3111f08b | 2009-04-30 16:01:52 | [diff] [blame] | 1001 | } |
| 1002 | |
[email protected] | bde3dda | 2009-05-20 22:13:07 | [diff] [blame] | 1003 | // Called when the about window is closed. We use this to release the |
| 1004 | // window controller. |
[email protected] | d4e1865 | 2009-11-19 21:52:16 | [diff] [blame] | 1005 | - (void)aboutWindowClosed:(NSNotification*)notification { |
| 1006 | NSWindow* window = [aboutController_ window]; |
| 1007 | DCHECK(window == [notification object]); |
[email protected] | bde3dda | 2009-05-20 22:13:07 | [diff] [blame] | 1008 | [[NSNotificationCenter defaultCenter] |
[email protected] | 49aeee5 | 2009-10-26 19:58:13 | [diff] [blame] | 1009 | removeObserver:self |
[email protected] | d4e1865 | 2009-11-19 21:52:16 | [diff] [blame] | 1010 | name:NSWindowWillCloseNotification |
| 1011 | object:window]; |
| 1012 | // AboutWindowControllers are autoreleased in |
| 1013 | // -[AboutWindowController windowWillClose:]. |
| 1014 | aboutController_ = nil; |
[email protected] | bde3dda | 2009-05-20 22:13:07 | [diff] [blame] | 1015 | } |
| 1016 | |
| 1017 | - (IBAction)orderFrontStandardAboutPanel:(id)sender { |
[email protected] | bde3dda | 2009-05-20 22:13:07 | [diff] [blame] | 1018 | if (!aboutController_) { |
[email protected] | d4e1865 | 2009-11-19 21:52:16 | [diff] [blame] | 1019 | aboutController_ = |
| 1020 | [[AboutWindowController alloc] initWithProfile:[self defaultProfile]]; |
[email protected] | 49aeee5 | 2009-10-26 19:58:13 | [diff] [blame] | 1021 | |
[email protected] | bde3dda | 2009-05-20 22:13:07 | [diff] [blame] | 1022 | // Watch for a notification of when it goes away so that we can destroy |
| 1023 | // the controller. |
| 1024 | [[NSNotificationCenter defaultCenter] |
| 1025 | addObserver:self |
| 1026 | selector:@selector(aboutWindowClosed:) |
[email protected] | d4e1865 | 2009-11-19 21:52:16 | [diff] [blame] | 1027 | name:NSWindowWillCloseNotification |
| 1028 | object:[aboutController_ window]]; |
[email protected] | bde3dda | 2009-05-20 22:13:07 | [diff] [blame] | 1029 | } |
[email protected] | 49aeee5 | 2009-10-26 19:58:13 | [diff] [blame] | 1030 | |
[email protected] | bde3dda | 2009-05-20 22:13:07 | [diff] [blame] | 1031 | [aboutController_ showWindow:self]; |
[email protected] | bde3dda | 2009-05-20 22:13:07 | [diff] [blame] | 1032 | } |
| 1033 | |
[email protected] | 3d4bd23f | 2009-10-06 03:58:38 | [diff] [blame] | 1034 | // Explicitly bring to the foreground when creating new windows from the dock. |
| 1035 | - (void)newWindowFromDock:(id)sender { |
| 1036 | [NSApp activateIgnoringOtherApps:YES]; |
| 1037 | [self commandDispatch:sender]; |
| 1038 | } |
| 1039 | |
[email protected] | 227fcee | 2010-06-11 19:19:37 | [diff] [blame] | 1040 | - (NSMenu*)applicationDockMenu:(NSApplication*)sender { |
[email protected] | 018a396 | 2009-09-17 22:23:44 | [diff] [blame] | 1041 | NSMenu* dockMenu = [[[NSMenu alloc] initWithTitle: @""] autorelease]; |
| 1042 | NSString* titleStr = l10n_util::GetNSStringWithFixup(IDS_NEW_WINDOW_MAC); |
| 1043 | scoped_nsobject<NSMenuItem> item([[NSMenuItem alloc] |
| 1044 | initWithTitle:titleStr |
[email protected] | 3d4bd23f | 2009-10-06 03:58:38 | [diff] [blame] | 1045 | action:@selector(newWindowFromDock:) |
[email protected] | 018a396 | 2009-09-17 22:23:44 | [diff] [blame] | 1046 | keyEquivalent:@""]); |
[email protected] | 288bfcd3 | 2009-09-14 18:14:46 | [diff] [blame] | 1047 | [item setTarget:self]; |
| 1048 | [item setTag:IDC_NEW_WINDOW]; |
[email protected] | 018a396 | 2009-09-17 22:23:44 | [diff] [blame] | 1049 | [dockMenu addItem:item]; |
[email protected] | 288bfcd3 | 2009-09-14 18:14:46 | [diff] [blame] | 1050 | |
| 1051 | titleStr = l10n_util::GetNSStringWithFixup(IDS_NEW_INCOGNITO_WINDOW_MAC); |
[email protected] | 018a396 | 2009-09-17 22:23:44 | [diff] [blame] | 1052 | item.reset([[NSMenuItem alloc] initWithTitle:titleStr |
[email protected] | 3d4bd23f | 2009-10-06 03:58:38 | [diff] [blame] | 1053 | action:@selector(newWindowFromDock:) |
[email protected] | 018a396 | 2009-09-17 22:23:44 | [diff] [blame] | 1054 | keyEquivalent:@""]); |
[email protected] | 288bfcd3 | 2009-09-14 18:14:46 | [diff] [blame] | 1055 | [item setTarget:self]; |
| 1056 | [item setTag:IDC_NEW_INCOGNITO_WINDOW]; |
[email protected] | 018a396 | 2009-09-17 22:23:44 | [diff] [blame] | 1057 | [dockMenu addItem:item]; |
[email protected] | 288bfcd3 | 2009-09-14 18:14:46 | [diff] [blame] | 1058 | |
[email protected] | 018a396 | 2009-09-17 22:23:44 | [diff] [blame] | 1059 | return dockMenu; |
[email protected] | 288bfcd3 | 2009-09-14 18:14:46 | [diff] [blame] | 1060 | } |
| 1061 | |
[email protected] | d630d7d5 | 2010-02-21 00:55:11 | [diff] [blame] | 1062 | - (const std::vector<GURL>&)startupUrls { |
| 1063 | return startupUrls_; |
[email protected] | caadfca | 2010-01-22 21:51:21 | [diff] [blame] | 1064 | } |
| 1065 | |
[email protected] | d630d7d5 | 2010-02-21 00:55:11 | [diff] [blame] | 1066 | - (void)clearStartupUrls { |
| 1067 | startupUrls_.clear(); |
[email protected] | caadfca | 2010-01-22 21:51:21 | [diff] [blame] | 1068 | } |
| 1069 | |
[email protected] | d630d7d5 | 2010-02-21 00:55:11 | [diff] [blame] | 1070 | @end // @implementation AppController |
[email protected] | 60ad3e2 | 2009-09-18 21:07:19 | [diff] [blame] | 1071 | |
| 1072 | //--------------------------------------------------------------------------- |
| 1073 | |
[email protected] | 60ad3e2 | 2009-09-18 21:07:19 | [diff] [blame] | 1074 | void ShowOptionsWindow(OptionsPage page, |
| 1075 | OptionsGroup highlight_group, |
| 1076 | Profile* profile) { |
[email protected] | d989f07c | 2009-11-14 00:35:46 | [diff] [blame] | 1077 | // TODO(akalin): Use highlight_group. |
| 1078 | AppController* appController = [NSApp delegate]; |
| 1079 | [appController showPreferencesWindow:nil page:page profile:profile]; |
[email protected] | 60ad3e2 | 2009-09-18 21:07:19 | [diff] [blame] | 1080 | } |
[email protected] | 216e904 | 2009-10-29 17:05:23 | [diff] [blame] | 1081 | |
| 1082 | namespace app_controller_mac { |
| 1083 | |
| 1084 | bool IsOpeningNewWindow() { |
| 1085 | return g_is_opening_new_window; |
| 1086 | } |
| 1087 | |
| 1088 | } // namespace app_controller_mac |