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