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