blob: 000bc5c3c43d5aa4e1890ade6c7d9189868da153 [file] [log] [blame]
[email protected]71c0eb92012-01-03 17:57:301// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]1bcdb532009-01-16 17:47:572// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]2c47bc12009-04-10 20:14:005#import "chrome/browser/app_controller_mac.h"
[email protected]1bcdb532009-01-16 17:47:576
[email protected]d630d7d52010-02-21 00:55:117#include "base/auto_reset.h"
[email protected]0b8caad2011-11-28 23:33:278#include "base/bind.h"
[email protected]57750f822009-04-21 21:43:099#include "base/command_line.h"
[email protected]f805fe82010-08-03 22:47:1010#include "base/file_path.h"
[email protected]151c4a62011-04-22 04:15:1311#include "base/mac/foundation_util.h"
[email protected]0378bf42011-01-01 18:20:1412#include "base/mac/mac_util.h"
[email protected]2c47bc12009-04-10 20:14:0013#include "base/message_loop.h"
[email protected]528c56d2010-07-30 19:28:4414#include "base/string_number_conversions.h"
[email protected]57750f822009-04-21 21:43:0915#include "base/sys_string_conversions.h"
[email protected]061a5ea82011-08-03 10:11:5016#include "base/utf_string_conversions.h"
[email protected]1a3aba82010-11-08 23:52:5417#include "chrome/app/chrome_command_ids.h"
[email protected]a07676b22011-06-17 16:36:5318#include "chrome/browser/background/background_application_list_model.h"
[email protected]78d78a62011-09-29 18:24:3319#include "chrome/browser/background/background_mode_manager.h"
[email protected]5c238752009-06-13 10:29:0720#include "chrome/browser/browser_process.h"
[email protected]41741a962009-02-18 21:51:3921#include "chrome/browser/browser_shutdown.h"
[email protected]2c47bc12009-04-10 20:14:0022#include "chrome/browser/command_updater.h"
[email protected]9bb54ee2011-10-12 17:43:3523#include "chrome/browser/download/download_service.h"
24#include "chrome/browser/download/download_service_factory.h"
[email protected]e4a377cf2011-10-05 02:51:4925#include "chrome/browser/first_run/first_run.h"
[email protected]0934de82011-03-09 18:42:1626#include "chrome/browser/instant/instant_confirm_dialog.h"
[email protected]a99fce0e2011-03-21 20:58:4827#include "chrome/browser/prefs/pref_service.h"
[email protected]a529af52011-08-15 20:56:1728#include "chrome/browser/printing/cloud_print/virtual_driver_install_helper.h"
29#include "chrome/browser/printing/print_dialog_cloud.h"
[email protected]e7dd6d82010-10-07 23:21:1230#include "chrome/browser/printing/print_job_manager.h"
[email protected]8ecad5e2010-12-02 21:18:3331#include "chrome/browser/profiles/profile_manager.h"
[email protected]a529af52011-08-15 20:56:1732#include "chrome/browser/service/service_process_control.h"
[email protected]d2912a22011-03-15 15:20:5033#include "chrome/browser/sessions/session_service.h"
[email protected]92371eb2011-04-28 11:50:1534#include "chrome/browser/sessions/session_service_factory.h"
[email protected]fbc947b2009-06-19 13:28:2435#include "chrome/browser/sessions/tab_restore_service.h"
[email protected]92371eb2011-04-28 11:50:1536#include "chrome/browser/sessions/tab_restore_service_factory.h"
[email protected]f9bc9b92009-11-24 00:55:3537#include "chrome/browser/sync/profile_sync_service.h"
[email protected]40f047972009-11-25 03:54:4038#include "chrome/browser/sync/sync_ui_util.h"
39#include "chrome/browser/sync/sync_ui_util_mac.h"
[email protected]ae04f592010-11-18 20:41:3540#include "chrome/browser/ui/browser.h"
[email protected]f7002802010-11-12 19:50:2841#include "chrome/browser/ui/browser_init.h"
[email protected]ae04f592010-11-18 20:41:3542#include "chrome/browser/ui/browser_list.h"
43#include "chrome/browser/ui/browser_window.h"
[email protected]7d791652010-12-01 16:34:4944#import "chrome/browser/ui/cocoa/about_window_controller.h"
45#import "chrome/browser/ui/cocoa/bookmarks/bookmark_menu_bridge.h"
46#import "chrome/browser/ui/cocoa/browser_window_cocoa.h"
47#import "chrome/browser/ui/cocoa/browser_window_controller.h"
[email protected]7d791652010-12-01 16:34:4948#import "chrome/browser/ui/cocoa/confirm_quit_panel_controller.h"
49#import "chrome/browser/ui/cocoa/encoding_menu_controller_delegate_mac.h"
50#import "chrome/browser/ui/cocoa/history_menu_bridge.h"
[email protected]961a6932011-07-19 19:52:4651#import "chrome/browser/ui/cocoa/profile_menu_controller.h"
[email protected]cfe1d612011-01-19 20:06:4752#import "chrome/browser/ui/cocoa/tabs/tab_strip_controller.h"
53#import "chrome/browser/ui/cocoa/tabs/tab_window_controller.h"
[email protected]7d791652010-12-01 16:34:4954#include "chrome/browser/ui/cocoa/task_manager_mac.h"
[email protected]6a455072011-11-02 01:32:2855#include "chrome/browser/ui/panels/panel_manager.h"
[email protected]14fb7d32011-07-10 17:53:3256#include "chrome/common/chrome_notification_types.h"
[email protected]12dc3d42010-02-22 23:37:1257#include "chrome/common/chrome_paths_internal.h"
[email protected]f4acfae82009-09-11 00:33:3858#include "chrome/common/chrome_switches.h"
[email protected]13aec6c42011-10-27 21:22:0859#include "chrome/common/cloud_print/cloud_print_class_mac.h"
[email protected]6c583d32011-08-19 14:46:2260#include "chrome/common/mac/app_mode_common.h"
[email protected]a99fce0e2011-03-21 20:58:4861#include "chrome/common/pref_names.h"
[email protected]a529af52011-08-15 20:56:1762#include "chrome/common/service_messages.h"
[email protected]024617a2010-08-20 05:08:0563#include "chrome/common/url_constants.h"
[email protected]c38831a12011-10-28 12:44:4964#include "content/public/browser/browser_thread.h"
[email protected]e582fdd2011-12-20 16:48:1765#include "content/public/browser/download_manager.h"
[email protected]ad50def52011-10-19 23:17:0766#include "content/public/browser/notification_service.h"
[email protected]0d6e9bd2011-10-18 04:29:1667#include "content/public/browser/notification_types.h"
[email protected]7f6f44c2011-12-14 13:23:3868#include "content/public/browser/user_metrics.h"
[email protected]9dcb59662009-09-23 01:15:5069#include "grit/chromium_strings.h"
[email protected]09729a552009-08-03 23:21:4170#include "grit/generated_resources.h"
[email protected]caadfca2010-01-22 21:51:2171#include "net/base/net_util.h"
[email protected]2b68f812011-11-25 15:02:1172#include "ui/base/accelerators/accelerator_cocoa.h"
[email protected]c051a1b2011-01-21 23:30:1773#include "ui/base/l10n/l10n_util.h"
74#include "ui/base/l10n/l10n_util_mac.h"
[email protected]88d74942009-01-21 22:04:4475
[email protected]631bb742011-11-02 11:29:3976using content::BrowserThread;
[email protected]e582fdd2011-12-20 16:48:1777using content::DownloadManager;
[email protected]7f6f44c2011-12-14 13:23:3878using content::UserMetricsAction;
[email protected]631bb742011-11-02 11:29:3979
[email protected]f2c20fa2009-12-01 17:42:0280// 10.6 adds a public API for the Spotlight-backed search menu item in the Help
81// menu. Provide the declaration so it can be called below when building with
82// the 10.5 SDK.
83#if !defined(MAC_OS_X_VERSION_10_6) || \
84 MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_6
85@interface NSApplication (SnowLeopardSDKDeclarations)
[email protected]caadfca2010-01-22 21:51:2186- (void)setHelpMenu:(NSMenu*)helpMenu;
[email protected]f2c20fa2009-12-01 17:42:0287@end
88#endif
89
[email protected]d630d7d52010-02-21 00:55:1190namespace {
91
[email protected]ebbe94b2012-01-18 20:11:5692// Declare notification names from the 10.7 SDK.
93#if !defined(MAC_OS_X_VERSION_10_7) || \
94 MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_7
95NSString* NSPopoverDidShowNotification = @"NSPopoverDidShowNotification";
96NSString* NSPopoverDidCloseNotification = @"NSPopoverDidCloseNotification";
97#endif
98
[email protected]d630d7d52010-02-21 00:55:1199// True while AppController is calling Browser::OpenEmptyWindow(). We need a
100// global flag here, analogue to BrowserInit::InProcessStartup() because
101// otherwise the SessionService will try to restore sessions when we make a new
102// window while there are no other active windows.
103bool g_is_opening_new_window = false;
104
105// Activates a browser window having the given profile (the last one active) if
106// possible and returns a pointer to the activate |Browser| or NULL if this was
107// not possible. If the last active browser is minimized (in particular, if
108// there are only minimized windows), it will unminimize it.
109Browser* ActivateBrowser(Profile* profile) {
110 Browser* browser = BrowserList::GetLastActiveWithProfile(profile);
111 if (browser)
112 browser->window()->Activate();
113 return browser;
114}
115
116// Creates an empty browser window with the given profile and returns a pointer
117// to the new |Browser|.
118Browser* CreateBrowser(Profile* profile) {
119 {
[email protected]0fbd70332010-06-01 19:28:34120 AutoReset<bool> auto_reset_in_run(&g_is_opening_new_window, true);
[email protected]d630d7d52010-02-21 00:55:11121 Browser::OpenEmptyWindow(profile);
122 }
123
124 Browser* browser = BrowserList::GetLastActive();
125 CHECK(browser);
126 return browser;
127}
128
129// Activates a browser window having the given profile (the last one active) if
130// possible or creates an empty one if necessary. Returns a pointer to the
131// activated/new |Browser|.
132Browser* ActivateOrCreateBrowser(Profile* profile) {
133 if (Browser* browser = ActivateBrowser(profile))
134 return browser;
135 return CreateBrowser(profile);
136}
137
[email protected]151c4a62011-04-22 04:15:13138CFStringRef BaseBundleID_CFString() {
139 NSString* base_bundle_id =
140 [NSString stringWithUTF8String:base::mac::BaseBundleID()];
141 return base::mac::NSToCFCast(base_bundle_id);
142}
143
[email protected]0b8caad2011-11-28 23:33:27144// This callback synchronizes preferences (under "org.chromium.Chromium" or
[email protected]12dc3d42010-02-22 23:37:12145// "com.google.Chrome"), in particular, writes them out to disk.
[email protected]0b8caad2011-11-28 23:33:27146void PrefsSyncCallback() {
147 if (!CFPreferencesAppSynchronize(BaseBundleID_CFString()))
148 LOG(WARNING) << "Error recording application bundle path.";
149}
[email protected]12dc3d42010-02-22 23:37:12150
151// Record the location of the application bundle (containing the main framework)
152// from which Chromium was loaded. This is used by app mode shims to find
153// Chromium.
154void RecordLastRunAppBundlePath() {
155 // Going up three levels from |chrome::GetVersionedDirectory()| gives the
156 // real, user-visible app bundle directory. (The alternatives give either the
157 // framework's path or the initial app's path, which may be an app mode shim
158 // or a unit test.)
159 FilePath appBundlePath =
160 chrome::GetVersionedDirectory().DirName().DirName().DirName();
161 CFPreferencesSetAppValue(app_mode::kLastRunAppBundlePathPrefsKey,
162 base::SysUTF8ToCFStringRef(appBundlePath.value()),
[email protected]151c4a62011-04-22 04:15:13163 BaseBundleID_CFString());
[email protected]12dc3d42010-02-22 23:37:12164
165 // Sync after a delay avoid I/O contention on startup; 1500 ms is plenty.
[email protected]d04e7662010-10-10 22:24:48166 BrowserThread::PostDelayedTask(BrowserThread::FILE, FROM_HERE,
[email protected]0b8caad2011-11-28 23:33:27167 base::Bind(&PrefsSyncCallback), 1500);
[email protected]12dc3d42010-02-22 23:37:12168}
169
[email protected]d630d7d52010-02-21 00:55:11170} // anonymous namespace
171
[email protected]a529af52011-08-15 20:56:17172const AEEventClass kAECloudPrintInstallClass = 'GCPi';
173const AEEventClass kAECloudPrintUninstallClass = 'GCPu';
174
[email protected]a99fce0e2011-03-21 20:58:48175@interface AppController (Private)
[email protected]88d74942009-01-21 22:04:44176- (void)initMenuState;
[email protected]961a6932011-07-19 19:52:46177- (void)initProfileMenu;
[email protected]a99fce0e2011-03-21 20:58:48178- (void)updateConfirmToQuitPrefMenuItem:(NSMenuItem*)item;
[email protected]eef99c22010-08-17 05:55:16179- (void)registerServicesMenuTypesTo:(NSApplication*)app;
[email protected]d630d7d52010-02-21 00:55:11180- (void)openUrls:(const std::vector<GURL>&)urls;
[email protected]57750f822009-04-21 21:43:09181- (void)getUrl:(NSAppleEventDescriptor*)event
182 withReply:(NSAppleEventDescriptor*)reply;
[email protected]a529af52011-08-15 20:56:17183- (void)submitCloudPrintJob:(NSAppleEventDescriptor*)event;
184- (void)installCloudPrint:(NSAppleEventDescriptor*)event;
185- (void)uninstallCloudPrint:(NSAppleEventDescriptor*)event;
[email protected]449dd2f2009-05-27 13:04:00186- (void)windowLayeringDidChange:(NSNotification*)inNotification;
[email protected]5c593702011-06-17 18:35:24187- (void)windowChangedToProfile:(Profile*)profile;
[email protected]dd6ab1282010-07-20 01:07:32188- (void)checkForAnyKeyWindows;
[email protected]73f5b362009-08-10 23:58:21189- (BOOL)userWillWaitForInProgressDownloads:(int)downloadCount;
[email protected]09729a552009-08-03 23:21:41190- (BOOL)shouldQuitWithInProgressDownloads;
[email protected]93b59fc2010-12-21 20:00:47191- (void)executeApplication:(id)sender;
[email protected]88d74942009-01-21 22:04:44192@end
[email protected]1bcdb532009-01-16 17:47:57193
194@implementation AppController
195
[email protected]caadfca2010-01-22 21:51:21196@synthesize startupComplete = startupComplete_;
197
[email protected]cd63ef62009-05-06 19:41:37198// This method is called very early in application startup (ie, before
199// the profile is loaded or any preferences have been registered). Defer any
200// user-data initialization until -applicationDidFinishLaunching:.
[email protected]88d74942009-01-21 22:04:44201- (void)awakeFromNib {
[email protected]136140c2009-05-19 13:58:25202 // We need to register the handlers early to catch events fired on launch.
[email protected]57750f822009-04-21 21:43:09203 NSAppleEventManager* em = [NSAppleEventManager sharedAppleEventManager];
204 [em setEventHandler:self
205 andSelector:@selector(getUrl:withReply:)
206 forEventClass:kInternetEventClass
207 andEventID:kAEGetURL];
208 [em setEventHandler:self
[email protected]a529af52011-08-15 20:56:17209 andSelector:@selector(submitCloudPrintJob:)
[email protected]13aec6c42011-10-27 21:22:08210 forEventClass:cloud_print::kAECloudPrintClass
211 andEventID:cloud_print::kAECloudPrintClass];
[email protected]a529af52011-08-15 20:56:17212 // Install and uninstall handlers for virtual drivers.
213 [em setEventHandler:self
214 andSelector:@selector(installCloudPrint:)
215 forEventClass:kAECloudPrintInstallClass
216 andEventID:kAECloudPrintInstallClass];
217 [em setEventHandler:self
218 andSelector:@selector(uninstallCloudPrint:)
219 forEventClass:kAECloudPrintUninstallClass
220 andEventID:kAECloudPrintUninstallClass];
221 [em setEventHandler:self
[email protected]57750f822009-04-21 21:43:09222 andSelector:@selector(getUrl:withReply:)
223 forEventClass:'WWW!' // A particularly ancient AppleEvent that dates
224 andEventID:'OURL']; // back to the Spyglass days.
[email protected]cd63ef62009-05-06 19:41:37225
[email protected]449dd2f2009-05-27 13:04:00226 // Register for various window layering changes. We use these to update
227 // various UI elements (command-key equivalents, etc) when the frontmost
228 // window changes.
229 NSNotificationCenter* notificationCenter =
230 [NSNotificationCenter defaultCenter];
[email protected]ce560f82009-06-03 09:39:44231 [notificationCenter
[email protected]449dd2f2009-05-27 13:04:00232 addObserver:self
233 selector:@selector(windowLayeringDidChange:)
234 name:NSWindowDidBecomeKeyNotification
235 object:nil];
236 [notificationCenter
237 addObserver:self
238 selector:@selector(windowLayeringDidChange:)
239 name:NSWindowDidResignKeyNotification
240 object:nil];
241 [notificationCenter
242 addObserver:self
243 selector:@selector(windowLayeringDidChange:)
244 name:NSWindowDidBecomeMainNotification
245 object:nil];
246 [notificationCenter
247 addObserver:self
248 selector:@selector(windowLayeringDidChange:)
249 name:NSWindowDidResignMainNotification
250 object:nil];
251
[email protected]ebbe94b2012-01-18 20:11:56252 if (base::mac::IsOSLionOrLater()) {
253 [notificationCenter
254 addObserver:self
255 selector:@selector(popoverDidShow:)
256 name:NSPopoverDidShowNotification
257 object:nil];
258 [notificationCenter
259 addObserver:self
260 selector:@selector(popoverDidClose:)
261 name:NSPopoverDidCloseNotification
262 object:nil];
263 }
264
[email protected]136140c2009-05-19 13:58:25265 // Set up the command updater for when there are no windows open
266 [self initMenuState];
[email protected]961a6932011-07-19 19:52:46267
268 // Initialize the Profile menu.
269 [self initProfileMenu];
[email protected]136140c2009-05-19 13:58:25270}
271
[email protected]71c0eb92012-01-03 17:57:30272- (void)unregisterEventHandlers {
273 NSAppleEventManager* em = [NSAppleEventManager sharedAppleEventManager];
274 [em removeEventHandlerForEventClass:kInternetEventClass
275 andEventID:kAEGetURL];
276 [em removeEventHandlerForEventClass:cloud_print::kAECloudPrintClass
277 andEventID:cloud_print::kAECloudPrintClass];
278 [em removeEventHandlerForEventClass:kAECloudPrintInstallClass
279 andEventID:kAECloudPrintInstallClass];
280 [em removeEventHandlerForEventClass:kAECloudPrintUninstallClass
281 andEventID:kAECloudPrintUninstallClass];
282 [em removeEventHandlerForEventClass:'WWW!'
283 andEventID:'OURL'];
284 [[NSNotificationCenter defaultCenter] removeObserver:self];
285}
286
[email protected]7dc8c6b2010-04-09 17:02:50287// (NSApplicationDelegate protocol) This is the Apple-approved place to override
288// the default handlers.
289- (void)applicationWillFinishLaunching:(NSNotification*)notification {
[email protected]58877932010-05-25 05:43:11290 // Nothing here right now.
[email protected]7dc8c6b2010-04-09 17:02:50291}
292
293- (BOOL)tryToTerminateApplication:(NSApplication*)app {
[email protected]3076fad2010-04-28 18:11:45294 // Check for in-process downloads, and prompt the user if they really want
295 // to quit (and thus cancel downloads). Only check if we're not already
296 // shutting down, else the user might be prompted multiple times if the
297 // download isn't stopped before terminate is called again.
298 if (!browser_shutdown::IsTryingToQuit() &&
299 ![self shouldQuitWithInProgressDownloads])
300 return NO;
301
[email protected]7dc8c6b2010-04-09 17:02:50302 // TODO(viettrungluu): Remove Apple Event handlers here? (It's safe to leave
303 // them in, but I'm not sure about UX; we'd also want to disable other things
304 // though.) http://crbug.com/40861
305
[email protected]177aceb2010-11-03 16:17:41306 // Check if the user really wants to quit by employing the confirm-to-quit
307 // mechanism.
308 if (!browser_shutdown::IsTryingToQuit() &&
309 [self applicationShouldTerminate:app] != NSTerminateNow)
310 return NO;
311
[email protected]c984d9f2010-07-20 20:52:20312 size_t num_browsers = BrowserList::size();
[email protected]7dc8c6b2010-04-09 17:02:50313
[email protected]e7dd6d82010-10-07 23:21:12314 // Give any print jobs in progress time to finish.
315 if (!browser_shutdown::IsTryingToQuit())
316 g_browser_process->print_job_manager()->StopJobs(true);
317
[email protected]c984d9f2010-07-20 20:52:20318 // Initiate a shutdown (via BrowserList::CloseAllBrowsers()) if we aren't
319 // already shutting down.
320 if (!browser_shutdown::IsTryingToQuit())
[email protected]c6032e82010-09-13 20:06:05321 BrowserList::CloseAllBrowsers();
[email protected]7dc8c6b2010-04-09 17:02:50322
[email protected]c984d9f2010-07-20 20:52:20323 return num_browsers == 0 ? YES : NO;
[email protected]7dc8c6b2010-04-09 17:02:50324}
325
326- (void)stopTryingToTerminateApplication:(NSApplication*)app {
327 if (browser_shutdown::IsTryingToQuit()) {
328 // Reset the "trying to quit" state, so that closing all browser windows
329 // will no longer lead to termination.
330 browser_shutdown::SetTryingToQuit(false);
331
332 // TODO(viettrungluu): Were we to remove Apple Event handlers above, we
333 // would have to reinstall them here. http://crbug.com/40861
334 }
[email protected]a9e8afc2009-08-11 22:03:17335}
336
[email protected]177aceb2010-11-03 16:17:41337- (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication*)app {
[email protected]a99fce0e2011-03-21 20:58:48338 // Check if the preference is turned on.
[email protected]a585a8db2011-06-20 18:58:35339 const PrefService* prefs = [self lastProfile]->GetPrefs();
[email protected]1eaddf32011-04-05 15:09:23340 if (!prefs->GetBoolean(prefs::kConfirmToQuitEnabled)) {
341 confirm_quit::RecordHistogram(confirm_quit::kNoConfirm);
[email protected]177aceb2010-11-03 16:17:41342 return NSTerminateNow;
[email protected]1eaddf32011-04-05 15:09:23343 }
[email protected]177aceb2010-11-03 16:17:41344
345 // If the application is going to terminate as the result of a Cmd+Q
346 // invocation, use the special sauce to prevent accidental quitting.
347 // http://dev.chromium.org/developers/design-documents/confirm-to-quit-experiment
[email protected]177aceb2010-11-03 16:17:41348
[email protected]2020fd0b2010-12-10 01:16:50349 // This logic is only for keyboard-initiated quits.
[email protected]5df54fb2011-03-07 18:15:26350 if (![ConfirmQuitPanelController eventTriggersFeature:[app currentEvent]])
[email protected]2020fd0b2010-12-10 01:16:50351 return NSTerminateNow;
[email protected]177aceb2010-11-03 16:17:41352
[email protected]5df54fb2011-03-07 18:15:26353 return [[ConfirmQuitPanelController sharedController]
354 runModalLoopForApplication:app];
[email protected]177aceb2010-11-03 16:17:41355}
356
[email protected]449dd2f2009-05-27 13:04:00357// Called when the app is shutting down. Clean-up as appropriate.
[email protected]7dc8c6b2010-04-09 17:02:50358- (void)applicationWillTerminate:(NSNotification*)aNotification {
[email protected]7dc8c6b2010-04-09 17:02:50359 // There better be no browser windows left at this point.
[email protected]71c0eb92012-01-03 17:57:30360 CHECK_EQ(0u, BrowserList::size());
[email protected]3b6aa8b62009-09-15 21:36:11361
[email protected]c984d9f2010-07-20 20:52:20362 // Tell BrowserList not to keep the browser process alive. Once all the
363 // browsers get dealloc'd, it will stop the RunLoop and fall back into main().
364 BrowserList::EndKeepAlive();
[email protected]fbc947b2009-06-19 13:28:24365
[email protected]d4e18652009-11-19 21:52:16366 // Close these off if they have open windows.
[email protected]d4e18652009-11-19 21:52:16367 [aboutController_ close];
368
[email protected]71c0eb92012-01-03 17:57:30369 [self unregisterEventHandlers];
[email protected]449dd2f2009-05-27 13:04:00370}
371
[email protected]3b6aa8b62009-09-15 21:36:11372- (void)didEndMainMessageLoop {
[email protected]a585a8db2011-06-20 18:58:35373 DCHECK(!BrowserList::HasBrowserWithProfile([self lastProfile]));
374 if (!BrowserList::HasBrowserWithProfile([self lastProfile])) {
[email protected]3b6aa8b62009-09-15 21:36:11375 // As we're shutting down, we need to nuke the TabRestoreService, which
376 // will start the shutdown of the NavigationControllers and allow for
377 // proper shutdown. If we don't do this, Chrome won't shut down cleanly,
378 // and may end up crashing when some thread tries to use the IO thread (or
379 // another thread) that is no longer valid.
[email protected]a585a8db2011-06-20 18:58:35380 TabRestoreServiceFactory::ResetForProfile([self lastProfile]);
[email protected]3b6aa8b62009-09-15 21:36:11381 }
382}
383
[email protected]8cfbdbd2011-06-16 04:17:17384// If the window has a tab controller, make "close window" be cmd-shift-w,
385// otherwise leave it as the normal cmd-w. Capitalization of the key equivalent
386// affects whether the shift modifer is used.
[email protected]ebbe94b2012-01-18 20:11:56387- (void)adjustCloseWindowMenuItemKeyEquivalent:(BOOL)enableCloseTabShortcut {
388 [closeWindowMenuItem_ setKeyEquivalent:(enableCloseTabShortcut ? @"W" :
389 @"w")];
[email protected]8cfbdbd2011-06-16 04:17:17390 [closeWindowMenuItem_ setKeyEquivalentModifierMask:NSCommandKeyMask];
391}
392
393// If the window has a tab controller, make "close tab" take over cmd-w,
394// otherwise it shouldn't have any key-equivalent because it should be disabled.
[email protected]ebbe94b2012-01-18 20:11:56395- (void)adjustCloseTabMenuItemKeyEquivalent:(BOOL)enableCloseTabShortcut {
396 if (enableCloseTabShortcut) {
[email protected]8cfbdbd2011-06-16 04:17:17397 [closeTabMenuItem_ setKeyEquivalent:@"w"];
398 [closeTabMenuItem_ setKeyEquivalentModifierMask:NSCommandKeyMask];
399 } else {
400 [closeTabMenuItem_ setKeyEquivalent:@""];
401 [closeTabMenuItem_ setKeyEquivalentModifierMask:0];
402 }
403}
404
405// Explicitly remove any command-key equivalents from the close tab/window
406// menus so that nothing can go haywire if we get a user action during pending
407// updates.
408- (void)clearCloseMenuItemKeyEquivalents {
409 [closeTabMenuItem_ setKeyEquivalent:@""];
410 [closeTabMenuItem_ setKeyEquivalentModifierMask:0];
411 [closeWindowMenuItem_ setKeyEquivalent:@""];
412 [closeWindowMenuItem_ setKeyEquivalentModifierMask:0];
413}
414
[email protected]9f5f79b62011-10-28 00:43:45415// See if the focused window window has tabs, and adjust the key equivalents for
[email protected]8cfbdbd2011-06-16 04:17:17416// Close Tab/Close Window accordingly.
[email protected]9f5f79b62011-10-28 00:43:45417- (void)fixCloseMenuItemKeyEquivalents {
[email protected]8cfbdbd2011-06-16 04:17:17418 fileMenuUpdatePending_ = NO;
[email protected]8cfbdbd2011-06-16 04:17:17419
[email protected]9f5f79b62011-10-28 00:43:45420 NSWindow* window = [NSApp keyWindow];
421 NSWindow* mainWindow = [NSApp mainWindow];
422 if (!window || ([window parentWindow] == mainWindow)) {
423 // If the key window is a child of the main window (e.g. a bubble), the main
424 // window should be the one that handles the close menu item action.
425 // Also, there might be a small amount of time where there is no key window;
426 // in that case as well, just use our main browser window if there is one.
427 // You might think that we should just always use the main window, but the
428 // "About Chrome" window serves as a counterexample.
429 window = mainWindow;
430 }
431
432 BOOL hasTabs =
433 [[window windowController] isKindOfClass:[TabWindowController class]];
[email protected]ebbe94b2012-01-18 20:11:56434 BOOL enableCloseTabShortcut = hasTabs && !hasPopover_;
435 [self adjustCloseWindowMenuItemKeyEquivalent:enableCloseTabShortcut];
436 [self adjustCloseTabMenuItemKeyEquivalent:enableCloseTabShortcut];
[email protected]8cfbdbd2011-06-16 04:17:17437}
438
439// Fix up the "close tab/close window" command-key equivalents. We do this
440// after a delay to ensure that window layer state has been set by the time
441// we do the enabling. This should only be called on the main thread, code that
442// calls this (even as a side-effect) from other threads needs to be fixed.
[email protected]9f5f79b62011-10-28 00:43:45443- (void)delayedFixCloseMenuItemKeyEquivalents {
[email protected]8cfbdbd2011-06-16 04:17:17444 DCHECK([NSThread isMainThread]);
445 if (!fileMenuUpdatePending_) {
446 // The OS prefers keypresses to timers, so it's possible that a cmd-w
447 // can sneak in before this timer fires. In order to prevent that from
448 // having any bad consequences, just clear the keys combos altogether. They
449 // will be reset when the timer eventually fires.
450 if ([NSThread isMainThread]) {
451 fileMenuUpdatePending_ = YES;
452 [self clearCloseMenuItemKeyEquivalents];
[email protected]9f5f79b62011-10-28 00:43:45453 [self performSelector:@selector(fixCloseMenuItemKeyEquivalents)
454 withObject:nil
[email protected]8cfbdbd2011-06-16 04:17:17455 afterDelay:0];
456 } else {
457 // This shouldn't be happening, but if it does, force it to the main
458 // thread to avoid dropping the update. Don't mess with
459 // |fileMenuUpdatePending_| as it's not expected to be threadsafe and
460 // there could be a race between the selector finishing and setting the
461 // flag.
462 [self
[email protected]9f5f79b62011-10-28 00:43:45463 performSelectorOnMainThread:@selector(fixCloseMenuItemKeyEquivalents)
464 withObject:nil
[email protected]8cfbdbd2011-06-16 04:17:17465 waitUntilDone:NO];
466 }
467 }
468}
469
[email protected]449dd2f2009-05-27 13:04:00470// Called when we get a notification about the window layering changing to
471// update the UI based on the new main window.
472- (void)windowLayeringDidChange:(NSNotification*)notify {
[email protected]9f5f79b62011-10-28 00:43:45473 [self delayedFixCloseMenuItemKeyEquivalents];
[email protected]8cfbdbd2011-06-16 04:17:17474
[email protected]dd6ab1282010-07-20 01:07:32475 if ([notify name] == NSWindowDidResignKeyNotification) {
476 // If a window is closed, this notification is fired but |[NSApp keyWindow]|
477 // returns nil regardless of whether any suitable candidates for the key
478 // window remain. It seems that the new key window for the app is not set
479 // until after this notification is fired, so a check is performed after the
480 // run loop is allowed to spin.
481 [self performSelector:@selector(checkForAnyKeyWindows)
482 withObject:nil
483 afterDelay:0.0];
484 }
[email protected]5c593702011-06-17 18:35:24485
486 // If the window changed to a new BrowserWindowController, update the profile.
487 id windowController = [[notify object] windowController];
[email protected]dc251482012-01-03 18:55:21488 if ([notify name] == NSWindowDidBecomeMainNotification &&
489 [windowController isKindOfClass:[BrowserWindowController class]]) {
[email protected]5c593702011-06-17 18:35:24490 // If the profile is incognito, use the original profile.
491 Profile* newProfile = [windowController profile]->GetOriginalProfile();
492 [self windowChangedToProfile:newProfile];
[email protected]cae68b3a2011-11-21 23:29:49493 } else if (BrowserList::empty()) {
494 [self windowChangedToProfile:
495 g_browser_process->profile_manager()->GetLastUsedProfile()];
[email protected]5c593702011-06-17 18:35:24496 }
497}
498
[email protected]ebbe94b2012-01-18 20:11:56499// Called on Lion and later when a popover (e.g. dictionary) is shown.
500- (void)popoverDidShow:(NSNotification*)notify {
501 hasPopover_ = YES;
502 [self fixCloseMenuItemKeyEquivalents];
503}
504
505// Called on Lion and later when a popover (e.g. dictionary) is closed.
506- (void)popoverDidClose:(NSNotification*)notify {
507 hasPopover_ = NO;
508 [self fixCloseMenuItemKeyEquivalents];
509}
510
[email protected]5c593702011-06-17 18:35:24511// Called when the user has changed browser windows, meaning the backing profile
512// may have changed. This can cause a rebuild of the user-data menus. This is a
513// no-op if the new profile is the same as the current one. This will always be
514// the original profile and never incognito.
515- (void)windowChangedToProfile:(Profile*)profile {
516 if (lastProfile_ == profile)
517 return;
518
519 // Before tearing down the menu controller bridges, return the Cocoa menus to
520 // their initial state.
521 if (bookmarkMenuBridge_.get())
522 bookmarkMenuBridge_->ResetMenu();
523 if (historyMenuBridge_.get())
524 historyMenuBridge_->ResetMenu();
525
526 // Rebuild the menus with the new profile.
527 lastProfile_ = profile;
528
[email protected]0c165cf2011-07-09 03:56:50529 bookmarkMenuBridge_.reset(new BookmarkMenuBridge(lastProfile_,
530 [[[NSApp mainMenu] itemWithTag:IDC_BOOKMARKS_MENU] submenu]));
[email protected]dc251482012-01-03 18:55:21531 // No need to |BuildMenu| here. It is done lazily upon menu access.
[email protected]5c593702011-06-17 18:35:24532
533 historyMenuBridge_.reset(new HistoryMenuBridge(lastProfile_));
534 historyMenuBridge_->BuildMenu();
[email protected]dd6ab1282010-07-20 01:07:32535}
536
537- (void)checkForAnyKeyWindows {
538 if ([NSApp keyWindow])
539 return;
540
[email protected]ad50def52011-10-19 23:17:07541 content::NotificationService::current()->Notify(
[email protected]666641df2011-07-10 18:24:38542 content::NOTIFICATION_NO_KEY_WINDOW,
[email protected]ad50def52011-10-19 23:17:07543 content::NotificationService::AllSources(),
544 content::NotificationService::NoDetails());
[email protected]449dd2f2009-05-27 13:04:00545}
546
[email protected]414fde592009-05-21 16:14:43547// If the auto-update interval is not set, make it 5 hours.
548// This code is specific to Mac Chrome Dev Channel.
549// Placed here for 2 reasons:
550// 1) Same spot as other Pref stuff
551// 2) Try and be friendly by keeping this after app launch
552// TODO(jrg): remove once we go Beta.
553- (void)setUpdateCheckInterval {
554#if defined(GOOGLE_CHROME_BUILD)
555 CFStringRef app = (CFStringRef)@"com.google.Keystone.Agent";
556 CFStringRef checkInterval = (CFStringRef)@"checkInterval";
557 CFPropertyListRef plist = CFPreferencesCopyAppValue(checkInterval, app);
558 if (!plist) {
559 const float fiveHoursInSeconds = 5.0 * 60.0 * 60.0;
[email protected]7dc8c6b2010-04-09 17:02:50560 NSNumber* value = [NSNumber numberWithFloat:fiveHoursInSeconds];
[email protected]414fde592009-05-21 16:14:43561 CFPreferencesSetAppValue(checkInterval, value, app);
562 CFPreferencesAppSynchronize(app);
563 }
564#endif
565}
566
[email protected]136140c2009-05-19 13:58:25567// This is called after profiles have been loaded and preferences registered.
568// It is safe to access the default profile here.
569- (void)applicationDidFinishLaunching:(NSNotification*)notify {
[email protected]c984d9f2010-07-20 20:52:20570 // Notify BrowserList to keep the application running so it doesn't go away
571 // when all the browser windows get closed.
572 BrowserList::StartKeepAlive();
[email protected]136140c2009-05-19 13:58:25573
[email protected]414fde592009-05-21 16:14:43574 [self setUpdateCheckInterval];
[email protected]2bcec612009-05-14 17:50:53575
576 // Build up the encoding menu, the order of the items differs based on the
577 // current locale (see http://crbug.com/7647 for details).
578 // We need a valid g_browser_process to get the profile which is why we can't
579 // call this from awakeFromNib.
[email protected]6f91e5ae2011-03-21 19:53:14580 NSMenu* viewMenu = [[[NSApp mainMenu] itemWithTag:IDC_VIEW_MENU] submenu];
581 NSMenuItem* encodingMenuItem = [viewMenu itemWithTag:IDC_ENCODING_MENU];
582 NSMenu* encodingMenu = [encodingMenuItem submenu];
[email protected]a585a8db2011-06-20 18:58:35583 EncodingMenuControllerDelegate::BuildEncodingMenu([self lastProfile],
[email protected]6f91e5ae2011-03-21 19:53:14584 encodingMenu);
[email protected]2bcec612009-05-14 17:50:53585
[email protected]f2c20fa2009-12-01 17:42:02586 // Since Chrome is localized to more languages than the OS, tell Cocoa which
587 // menu is the Help so it can add the search item to it.
[email protected]caadfca2010-01-22 21:51:21588 if (helpMenu_ && [NSApp respondsToSelector:@selector(setHelpMenu:)])
[email protected]f2c20fa2009-12-01 17:42:02589 [NSApp setHelpMenu:helpMenu_];
[email protected]f2c20fa2009-12-01 17:42:02590
[email protected]12dc3d42010-02-22 23:37:12591 // Record the path to the (browser) app bundle; this is used by the app mode
592 // shim.
593 RecordLastRunAppBundlePath();
594
[email protected]eef99c22010-08-17 05:55:16595 // Makes "Services" menu items available.
596 [self registerServicesMenuTypesTo:[notify object]];
597
[email protected]caadfca2010-01-22 21:51:21598 startupComplete_ = YES;
599
600 // TODO(viettrungluu): This is very temporary, since this should be done "in"
601 // |BrowserMain()|, i.e., this list of startup URLs should be appended to the
602 // (probably-empty) list of URLs from the command line.
[email protected]d630d7d52010-02-21 00:55:11603 if (startupUrls_.size()) {
604 [self openUrls:startupUrls_];
605 [self clearStartupUrls];
[email protected]caadfca2010-01-22 21:51:21606 }
[email protected]aac169d2011-03-18 19:53:03607
608 const CommandLine& parsed_command_line = *CommandLine::ForCurrentProcess();
609 if (!parsed_command_line.HasSwitch(switches::kEnableExposeForTabs)) {
610 [tabposeMenuItem_ setHidden:YES];
611 }
[email protected]7c321082009-02-09 15:35:47612}
613
[email protected]a96ec6a2009-11-04 17:27:08614// This is called after profiles have been loaded and preferences registered.
615// It is safe to access the default profile here.
616- (void)applicationDidBecomeActive:(NSNotification*)notify {
[email protected]ad50def52011-10-19 23:17:07617 content::NotificationService::current()->Notify(
618 content::NOTIFICATION_APP_ACTIVATED,
619 content::NotificationService::AllSources(),
620 content::NotificationService::NoDetails());
[email protected]a96ec6a2009-11-04 17:27:08621}
622
[email protected]73f5b362009-08-10 23:58:21623// Helper function for populating and displaying the in progress downloads at
624// exit alert panel.
625- (BOOL)userWillWaitForInProgressDownloads:(int)downloadCount {
[email protected]e8b5f7882009-09-14 20:59:13626 NSString* warningText = nil;
627 NSString* explanationText = nil;
[email protected]73f5b362009-08-10 23:58:21628 NSString* waitTitle = nil;
629 NSString* exitTitle = nil;
630
631 // Set the dialog text based on whether or not there are multiple downloads.
632 if (downloadCount == 1) {
[email protected]e8b5f7882009-09-14 20:59:13633 // Dialog text: warning and explanation.
[email protected]ab3b1c112012-01-13 22:04:16634 warningText = l10n_util::GetNSString(
635 IDS_SINGLE_DOWNLOAD_REMOVE_CONFIRM_WARNING);
636 explanationText = l10n_util::GetNSString(
637 IDS_SINGLE_DOWNLOAD_REMOVE_CONFIRM_EXPLANATION);
[email protected]73f5b362009-08-10 23:58:21638
639 // Cancel download and exit button text.
[email protected]757127052010-04-27 14:42:26640 exitTitle = l10n_util::GetNSString(
641 IDS_SINGLE_DOWNLOAD_REMOVE_CONFIRM_OK_BUTTON_LABEL);
[email protected]73f5b362009-08-10 23:58:21642
643 // Wait for download button text.
[email protected]757127052010-04-27 14:42:26644 waitTitle = l10n_util::GetNSString(
645 IDS_SINGLE_DOWNLOAD_REMOVE_CONFIRM_CANCEL_BUTTON_LABEL);
[email protected]73f5b362009-08-10 23:58:21646 } else {
[email protected]e8b5f7882009-09-14 20:59:13647 // Dialog text: warning and explanation.
[email protected]757127052010-04-27 14:42:26648 warningText = l10n_util::GetNSStringF(
[email protected]ab3b1c112012-01-13 22:04:16649 IDS_MULTIPLE_DOWNLOADS_REMOVE_CONFIRM_WARNING,
[email protected]528c56d2010-07-30 19:28:44650 base::IntToString16(downloadCount));
[email protected]ab3b1c112012-01-13 22:04:16651 explanationText = l10n_util::GetNSString(
652 IDS_MULTIPLE_DOWNLOADS_REMOVE_CONFIRM_EXPLANATION);
[email protected]73f5b362009-08-10 23:58:21653
654 // Cancel downloads and exit button text.
[email protected]757127052010-04-27 14:42:26655 exitTitle = l10n_util::GetNSString(
656 IDS_MULTIPLE_DOWNLOADS_REMOVE_CONFIRM_OK_BUTTON_LABEL);
[email protected]73f5b362009-08-10 23:58:21657
658 // Wait for downloads button text.
[email protected]757127052010-04-27 14:42:26659 waitTitle = l10n_util::GetNSString(
660 IDS_MULTIPLE_DOWNLOADS_REMOVE_CONFIRM_CANCEL_BUTTON_LABEL);
[email protected]73f5b362009-08-10 23:58:21661 }
662
663 // 'waitButton' is the default choice.
[email protected]e8b5f7882009-09-14 20:59:13664 int choice = NSRunAlertPanel(warningText, explanationText,
665 waitTitle, exitTitle, nil);
[email protected]73f5b362009-08-10 23:58:21666 return choice == NSAlertDefaultReturn ? YES : NO;
667}
668
669// Check all profiles for in progress downloads, and if we find any, prompt the
[email protected]09729a552009-08-03 23:21:41670// user to see if we should continue to exit (and thus cancel the downloads), or
671// if we should wait.
672- (BOOL)shouldQuitWithInProgressDownloads {
[email protected]73f5b362009-08-10 23:58:21673 ProfileManager* profile_manager = g_browser_process->profile_manager();
674 if (!profile_manager)
675 return YES;
[email protected]09729a552009-08-03 23:21:41676
[email protected]844a1002011-04-19 11:37:21677 std::vector<Profile*> profiles(profile_manager->GetLoadedProfiles());
678 for (size_t i = 0; i < profiles.size(); ++i) {
[email protected]9bb54ee2011-10-12 17:43:35679 DownloadService* download_service =
680 DownloadServiceFactory::GetForProfile(profiles[i]);
681 DownloadManager* download_manager =
682 (download_service->HasCreatedDownloadManager() ?
683 download_service->GetDownloadManager() : NULL);
[email protected]5656f8a2011-11-17 16:12:58684 if (download_manager && download_manager->InProgressCount() > 0) {
685 int downloadCount = download_manager->InProgressCount();
[email protected]73f5b362009-08-10 23:58:21686 if ([self userWillWaitForInProgressDownloads:downloadCount]) {
687 // Create a new browser window (if necessary) and navigate to the
688 // downloads page if the user chooses to wait.
[email protected]844a1002011-04-19 11:37:21689 Browser* browser = BrowserList::FindBrowserWithProfile(profiles[i]);
[email protected]73f5b362009-08-10 23:58:21690 if (!browser) {
[email protected]844a1002011-04-19 11:37:21691 browser = Browser::Create(profiles[i]);
[email protected]73f5b362009-08-10 23:58:21692 browser->window()->Show();
693 }
694 DCHECK(browser);
695 browser->ShowDownloadsTab();
696 return NO;
697 }
[email protected]09729a552009-08-03 23:21:41698
[email protected]73f5b362009-08-10 23:58:21699 // User wants to exit.
700 return YES;
[email protected]09729a552009-08-03 23:21:41701 }
[email protected]09729a552009-08-03 23:21:41702 }
703
[email protected]73f5b362009-08-10 23:58:21704 // No profiles or active downloads found, okay to exit.
[email protected]09729a552009-08-03 23:21:41705 return YES;
706}
707
[email protected]fbc947b2009-06-19 13:28:24708// Called to determine if we should enable the "restore tab" menu item.
709// Checks with the TabRestoreService to see if there's anything there to
710// restore and returns YES if so.
711- (BOOL)canRestoreTab {
[email protected]92371eb2011-04-28 11:50:15712 TabRestoreService* service =
[email protected]a585a8db2011-06-20 18:58:35713 TabRestoreServiceFactory::GetForProfile([self lastProfile]);
[email protected]fbc947b2009-06-19 13:28:24714 return service && !service->entries().empty();
715}
716
[email protected]f6c15fa2010-06-04 20:08:21717// Returns true if there is not a modal window (either window- or application-
718// modal) blocking the active browser. Note that tab modal dialogs (HTTP auth
719// sheets) will not count as blocking the browser. But things like open/save
720// dialogs that are window modal will block the browser.
721- (BOOL)keyWindowIsNotModal {
[email protected]6a281332009-10-13 17:41:06722 Browser* browser = BrowserList::GetLastActive();
[email protected]dc76e8362010-06-11 16:29:52723 return [NSApp modalWindow] == nil && (!browser ||
[email protected]6a281332009-10-13 17:41:06724 ![[browser->window()->GetNativeHandle() attachedSheet]
[email protected]f6c15fa2010-06-04 20:08:21725 isKindOfClass:[NSWindow class]]);
[email protected]6a281332009-10-13 17:41:06726}
727
[email protected]88d74942009-01-21 22:04:44728// Called to validate menu items when there are no key windows. All the
729// items we care about have been set with the |commandDispatch:| action and
730// a target of FirstResponder in IB. If it's not one of those, let it
731// continue up the responder chain to be handled elsewhere. We pull out the
732// tag as the cross-platform constant to differentiate and dispatch the
733// various commands.
734- (BOOL)validateUserInterfaceItem:(id<NSValidatedUserInterfaceItem>)item {
735 SEL action = [item action];
736 BOOL enable = NO;
737 if (action == @selector(commandDispatch:)) {
738 NSInteger tag = [item tag];
[email protected]fbc947b2009-06-19 13:28:24739 if (menuState_->SupportsCommand(tag)) {
740 switch (tag) {
[email protected]419eb2a2009-11-11 16:26:26741 // The File Menu commands are not automatically disabled by Cocoa when a
742 // dialog sheet obscures the browser window, so we disable several of
743 // them here. We don't need to include IDC_CLOSE_WINDOW, because
744 // app_controller is only activated when there are no key windows (see
745 // function comment).
[email protected]fbc947b2009-06-19 13:28:24746 case IDC_RESTORE_TAB:
[email protected]f6c15fa2010-06-04 20:08:21747 enable = [self keyWindowIsNotModal] && [self canRestoreTab];
[email protected]6a281332009-10-13 17:41:06748 break;
[email protected]f6c15fa2010-06-04 20:08:21749 // Browser-level items that open in new tabs should not open if there's
750 // a window- or app-modal dialog.
[email protected]6a281332009-10-13 17:41:06751 case IDC_OPEN_FILE:
[email protected]6a281332009-10-13 17:41:06752 case IDC_NEW_TAB:
[email protected]f6c15fa2010-06-04 20:08:21753 case IDC_SHOW_HISTORY:
754 case IDC_SHOW_BOOKMARK_MANAGER:
755 enable = [self keyWindowIsNotModal];
756 break;
757 // Browser-level items that open in new windows.
758 case IDC_NEW_WINDOW:
759 case IDC_TASK_MANAGER:
760 // Allow the user to open a new window if there's a window-modal
761 // dialog.
762 enable = [self keyWindowIsNotModal] || ([NSApp modalWindow] == nil);
[email protected]fbc947b2009-06-19 13:28:24763 break;
[email protected]446e16a2010-05-03 21:23:58764 case IDC_SYNC_BOOKMARKS: {
[email protected]a585a8db2011-06-20 18:58:35765 Profile* lastProfile = [self lastProfile];
[email protected]446e16a2010-05-03 21:23:58766 // The profile may be NULL during shutdown -- see
767 // http://code.google.com/p/chromium/issues/detail?id=43048 .
768 //
769 // TODO(akalin,viettrungluu): Figure out whether this method
[email protected]a585a8db2011-06-20 18:58:35770 // can be prevented from being called if lastProfile is
[email protected]446e16a2010-05-03 21:23:58771 // NULL.
[email protected]a585a8db2011-06-20 18:58:35772 if (!lastProfile) {
[email protected]446e16a2010-05-03 21:23:58773 LOG(WARNING)
[email protected]a585a8db2011-06-20 18:58:35774 << "NULL lastProfile detected -- not doing anything";
[email protected]446e16a2010-05-03 21:23:58775 break;
776 }
[email protected]a585a8db2011-06-20 18:58:35777 enable = lastProfile->IsSyncAccessible() &&
[email protected]d3b98c82010-07-14 07:45:59778 [self keyWindowIsNotModal];
[email protected]a585a8db2011-06-20 18:58:35779 sync_ui_util::UpdateSyncItem(item, enable, lastProfile);
[email protected]f9bc9b92009-11-24 00:55:35780 break;
[email protected]446e16a2010-05-03 21:23:58781 }
[email protected]acaa37b2011-08-31 07:19:11782 case IDC_FEEDBACK:
783 enable = NO;
784 break;
[email protected]fbc947b2009-06-19 13:28:24785 default:
[email protected]f6c15fa2010-06-04 20:08:21786 enable = menuState_->IsCommandEnabled(tag) ?
787 [self keyWindowIsNotModal] : NO;
[email protected]fbc947b2009-06-19 13:28:24788 }
789 }
[email protected]3b6aa8b62009-09-15 21:36:11790 } else if (action == @selector(terminate:)) {
[email protected]88d74942009-01-21 22:04:44791 enable = YES;
[email protected]3111f08b2009-04-30 16:01:52792 } else if (action == @selector(showPreferences:)) {
793 enable = YES;
[email protected]bde3dda2009-05-20 22:13:07794 } else if (action == @selector(orderFrontStandardAboutPanel:)) {
795 enable = YES;
[email protected]93b59fc2010-12-21 20:00:47796 } else if (action == @selector(commandFromDock:)) {
[email protected]3d4bd23f2009-10-06 03:58:38797 enable = YES;
[email protected]a99fce0e2011-03-21 20:58:48798 } else if (action == @selector(toggleConfirmToQuit:)) {
799 [self updateConfirmToQuitPrefMenuItem:static_cast<NSMenuItem*>(item)];
800 enable = YES;
[email protected]88d74942009-01-21 22:04:44801 }
802 return enable;
803}
804
[email protected]947fc0d2010-01-14 22:33:05805// Called when the user picks a menu item when there are no key windows, or when
806// there is no foreground browser window. Calls through to the browser object to
807// execute the command. This assumes that the command is supported and doesn't
[email protected]d630d7d52010-02-21 00:55:11808// check, otherwise it should have been disabled in the UI in
809// |-validateUserInterfaceItem:|.
[email protected]88d74942009-01-21 22:04:44810- (void)commandDispatch:(id)sender {
[email protected]a585a8db2011-06-20 18:58:35811 Profile* lastProfile = [self lastProfile];
[email protected]f0a51fb52009-03-05 12:46:38812
[email protected]947fc0d2010-01-14 22:33:05813 // Handle the case where we're dispatching a command from a sender that's in a
814 // browser window. This means that the command came from a background window
815 // and is getting here because the foreground window is not a browser window.
816 if ([sender respondsToSelector:@selector(window)]) {
817 id delegate = [[sender window] windowController];
818 if ([delegate isKindOfClass:[BrowserWindowController class]]) {
819 [delegate commandDispatch:sender];
820 return;
821 }
822 }
823
[email protected]88d74942009-01-21 22:04:44824 NSInteger tag = [sender tag];
825 switch (tag) {
[email protected]fbc947b2009-06-19 13:28:24826 case IDC_NEW_TAB:
[email protected]d630d7d52010-02-21 00:55:11827 // Create a new tab in an existing browser window (which we activate) if
828 // possible.
[email protected]a585a8db2011-06-20 18:58:35829 if (Browser* browser = ActivateBrowser(lastProfile)) {
[email protected]d630d7d52010-02-21 00:55:11830 browser->ExecuteCommand(IDC_NEW_TAB);
831 break;
832 }
833 // Else fall through to create new window.
[email protected]88d74942009-01-21 22:04:44834 case IDC_NEW_WINDOW:
[email protected]a585a8db2011-06-20 18:58:35835 CreateBrowser(lastProfile);
[email protected]d630d7d52010-02-21 00:55:11836 break;
[email protected]91a5b3e2009-10-30 19:32:22837 case IDC_FOCUS_LOCATION:
[email protected]a585a8db2011-06-20 18:58:35838 ActivateOrCreateBrowser(lastProfile)->ExecuteCommand(IDC_FOCUS_LOCATION);
[email protected]88d74942009-01-21 22:04:44839 break;
[email protected]cddc5242009-12-07 18:54:33840 case IDC_FOCUS_SEARCH:
[email protected]a585a8db2011-06-20 18:58:35841 ActivateOrCreateBrowser(lastProfile)->ExecuteCommand(IDC_FOCUS_SEARCH);
[email protected]cddc5242009-12-07 18:54:33842 break;
[email protected]863ff662009-01-26 20:18:18843 case IDC_NEW_INCOGNITO_WINDOW:
[email protected]a585a8db2011-06-20 18:58:35844 Browser::OpenEmptyWindow(lastProfile->GetOffTheRecordProfile());
[email protected]863ff662009-01-26 20:18:18845 break;
[email protected]fbc947b2009-06-19 13:28:24846 case IDC_RESTORE_TAB:
[email protected]a585a8db2011-06-20 18:58:35847 Browser::OpenWindowWithRestoredTabs(lastProfile);
[email protected]fbc947b2009-06-19 13:28:24848 break;
[email protected]e19516d2009-04-28 17:15:19849 case IDC_OPEN_FILE:
[email protected]a585a8db2011-06-20 18:58:35850 CreateBrowser(lastProfile)->ExecuteCommand(IDC_OPEN_FILE);
[email protected]e19516d2009-04-28 17:15:19851 break;
[email protected]1fdff702009-10-22 00:36:18852 case IDC_CLEAR_BROWSING_DATA: {
[email protected]93e181762009-05-29 14:30:38853 // There may not be a browser open, so use the default profile.
[email protected]a585a8db2011-06-20 18:58:35854 if (Browser* browser = ActivateBrowser(lastProfile)) {
[email protected]445177652011-03-09 02:04:05855 browser->OpenClearBrowsingDataDialog();
[email protected]c8de64a2011-01-25 17:10:23856 } else {
[email protected]ddddfda2011-07-14 23:19:39857 Browser::OpenClearBrowsingDataDialogWindow(lastProfile);
[email protected]c8de64a2011-01-25 17:10:23858 }
[email protected]93e181762009-05-29 14:30:38859 break;
[email protected]1fdff702009-10-22 00:36:18860 }
[email protected]4aee5652009-11-19 18:59:21861 case IDC_IMPORT_SETTINGS: {
[email protected]a585a8db2011-06-20 18:58:35862 if (Browser* browser = ActivateBrowser(lastProfile)) {
[email protected]445177652011-03-09 02:04:05863 browser->OpenImportSettingsDialog();
[email protected]c8de64a2011-01-25 17:10:23864 } else {
[email protected]a585a8db2011-06-20 18:58:35865 Browser::OpenImportSettingsDialogWindow(lastProfile);
[email protected]c8de64a2011-01-25 17:10:23866 }
[email protected]4aee5652009-11-19 18:59:21867 break;
868 }
[email protected]af7c25e2010-01-23 05:46:45869 case IDC_SHOW_BOOKMARK_MANAGER:
[email protected]7f6f44c2011-12-14 13:23:38870 content::RecordAction(UserMetricsAction("ShowBookmarkManager"));
[email protected]a585a8db2011-06-20 18:58:35871 if (Browser* browser = ActivateBrowser(lastProfile)) {
[email protected]47e532b2010-04-23 17:46:40872 // Open a bookmark manager tab.
[email protected]90a8bf252010-03-12 00:09:47873 browser->OpenBookmarkManager();
874 } else {
[email protected]47e532b2010-04-23 17:46:40875 // No browser window, so create one for the bookmark manager tab.
[email protected]a585a8db2011-06-20 18:58:35876 Browser::OpenBookmarkManagerWindow(lastProfile);
[email protected]90a8bf252010-03-12 00:09:47877 }
[email protected]af7c25e2010-01-23 05:46:45878 break;
[email protected]91a5b3e2009-10-30 19:32:22879 case IDC_SHOW_HISTORY:
[email protected]a585a8db2011-06-20 18:58:35880 if (Browser* browser = ActivateBrowser(lastProfile))
[email protected]d630d7d52010-02-21 00:55:11881 browser->ShowHistoryTab();
882 else
[email protected]a585a8db2011-06-20 18:58:35883 Browser::OpenHistoryWindow(lastProfile);
[email protected]91a5b3e2009-10-30 19:32:22884 break;
885 case IDC_SHOW_DOWNLOADS:
[email protected]a585a8db2011-06-20 18:58:35886 if (Browser* browser = ActivateBrowser(lastProfile))
[email protected]d630d7d52010-02-21 00:55:11887 browser->ShowDownloadsTab();
888 else
[email protected]a585a8db2011-06-20 18:58:35889 Browser::OpenDownloadsWindow(lastProfile);
[email protected]91a5b3e2009-10-30 19:32:22890 break;
[email protected]bb37e6ec2009-11-18 17:59:36891 case IDC_MANAGE_EXTENSIONS:
[email protected]a585a8db2011-06-20 18:58:35892 if (Browser* browser = ActivateBrowser(lastProfile))
[email protected]d630d7d52010-02-21 00:55:11893 browser->ShowExtensionsTab();
894 else
[email protected]a585a8db2011-06-20 18:58:35895 Browser::OpenExtensionsWindow(lastProfile);
[email protected]bb37e6ec2009-11-18 17:59:36896 break;
[email protected]1fdff702009-10-22 00:36:18897 case IDC_HELP_PAGE:
[email protected]a585a8db2011-06-20 18:58:35898 if (Browser* browser = ActivateBrowser(lastProfile))
[email protected]202802e42011-07-06 11:40:46899 browser->ShowHelpTab();
[email protected]d630d7d52010-02-21 00:55:11900 else
[email protected]a585a8db2011-06-20 18:58:35901 Browser::OpenHelpWindow(lastProfile);
[email protected]1fdff702009-10-22 00:36:18902 break;
[email protected]f9bc9b92009-11-24 00:55:35903 case IDC_SYNC_BOOKMARKS:
[email protected]446e16a2010-05-03 21:23:58904 // The profile may be NULL during shutdown -- see
905 // http://code.google.com/p/chromium/issues/detail?id=43048 .
906 //
907 // TODO(akalin,viettrungluu): Figure out whether this method can
[email protected]a585a8db2011-06-20 18:58:35908 // be prevented from being called if lastProfile is NULL.
909 if (!lastProfile) {
910 LOG(WARNING) << "NULL lastProfile detected -- not doing anything";
[email protected]446e16a2010-05-03 21:23:58911 break;
912 }
[email protected]f9bc9b92009-11-24 00:55:35913 // TODO(akalin): Add a constant to denote starting sync from the
914 // main menu and use that instead of START_FROM_WRENCH.
[email protected]40f047972009-11-25 03:54:40915 sync_ui_util::OpenSyncMyBookmarksDialog(
[email protected]a585a8db2011-06-20 18:58:35916 lastProfile, ActivateBrowser(lastProfile),
[email protected]c8de64a2011-01-25 17:10:23917 ProfileSyncService::START_FROM_WRENCH);
[email protected]f9bc9b92009-11-24 00:55:35918 break;
[email protected]ceaaf1812010-01-20 22:13:09919 case IDC_TASK_MANAGER:
[email protected]7f6f44c2011-12-14 13:23:38920 content::RecordAction(UserMetricsAction("TaskManager"));
[email protected]adb6a84d2011-02-07 16:58:40921 TaskManagerMac::Show(false);
[email protected]ceaaf1812010-01-20 22:13:09922 break;
[email protected]4a42d272010-06-18 01:29:42923 case IDC_OPTIONS:
924 [self showPreferences:sender];
925 break;
[email protected]93b59fc2010-12-21 20:00:47926 default:
927 // Background Applications use dynamic values that must be less than the
928 // smallest value among the predefined IDC_* labels.
929 if ([sender tag] < IDC_MinimumLabelValue)
930 [self executeApplication:sender];
931 break;
[email protected]d630d7d52010-02-21 00:55:11932 }
[email protected]88d74942009-01-21 22:04:44933}
934
[email protected]93b59fc2010-12-21 20:00:47935// Run a (background) application in a new tab.
936- (void)executeApplication:(id)sender {
937 NSInteger tag = [sender tag];
[email protected]a585a8db2011-06-20 18:58:35938 Profile* profile = [self lastProfile];
[email protected]93b59fc2010-12-21 20:00:47939 DCHECK(profile);
940 BackgroundApplicationListModel applications(profile);
941 DCHECK(tag >= 0 &&
942 tag < static_cast<int>(applications.size()));
[email protected]93b59fc2010-12-21 20:00:47943 const Extension* extension = applications.GetExtension(tag);
[email protected]78d78a62011-09-29 18:24:33944 BackgroundModeManager::LaunchBackgroundApplication(profile, extension);
[email protected]93b59fc2010-12-21 20:00:47945}
946
[email protected]947fc0d2010-01-14 22:33:05947// Same as |-commandDispatch:|, but executes commands using a disposition
948// determined by the key flags. This will get called in the case where the
949// frontmost window is not a browser window, and the user has command-clicked
950// a button in a background browser window whose action is
951// |-commandDispatchUsingKeyModifiers:|
952- (void)commandDispatchUsingKeyModifiers:(id)sender {
953 DCHECK(sender);
954 if ([sender respondsToSelector:@selector(window)]) {
955 id delegate = [[sender window] windowController];
956 if ([delegate isKindOfClass:[BrowserWindowController class]]) {
957 [delegate commandDispatchUsingKeyModifiers:sender];
958 }
959 }
960}
961
[email protected]ff81e0a2009-04-13 14:58:54962// NSApplication delegate method called when someone clicks on the
963// dock icon and there are no open windows. To match standard mac
964// behavior, we should open a new window.
965- (BOOL)applicationShouldHandleReopen:(NSApplication*)theApplication
966 hasVisibleWindows:(BOOL)flag {
[email protected]70631402010-04-26 20:16:54967 // If the browser is currently trying to quit, don't do anything and return NO
968 // to prevent AppKit from doing anything.
969 // TODO(rohitrao): Remove this code when http://crbug.com/40861 is resolved.
970 if (browser_shutdown::IsTryingToQuit())
971 return NO;
972
[email protected]0236be22012-01-11 01:05:35973 // Don't do anything if there are visible tabbed or popup windows. This will
974 // cause AppKit to unminimize the most recently minimized window. If the
975 // visible windows are panels or notifications, we still need to open a new
976 // window.
977 if (flag) {
978 for (BrowserList::const_iterator iter = BrowserList::begin();
979 iter != BrowserList::end(); ++iter) {
980 Browser* browser = *iter;
981 if (browser->is_type_tabbed() || browser->is_type_popup())
982 return YES;
983 }
984 }
[email protected]ff81e0a2009-04-13 14:58:54985
[email protected]0d1210d62010-09-07 17:31:38986 // If launched as a hidden login item (due to installation of a persistent app
987 // or by the user, for example in System Preferenecs->Accounts->Login Items),
988 // allow session to be restored first time the user clicks on a Dock icon.
989 // Normally, it'd just open a new empty page.
990 {
991 static BOOL doneOnce = NO;
992 if (!doneOnce) {
993 doneOnce = YES;
[email protected]0378bf42011-01-01 18:20:14994 if (base::mac::WasLaunchedAsHiddenLoginItem()) {
[email protected]0d1210d62010-09-07 17:31:38995 SessionService* sessionService =
[email protected]a585a8db2011-06-20 18:58:35996 SessionServiceFactory::GetForProfile([self lastProfile]);
[email protected]0d1210d62010-09-07 17:31:38997 if (sessionService &&
998 sessionService->RestoreIfNecessary(std::vector<GURL>()))
999 return NO;
1000 }
1001 }
1002 }
[email protected]ff81e0a2009-04-13 14:58:541003 // Otherwise open a new window.
[email protected]d630d7d52010-02-21 00:55:111004 {
[email protected]0fbd70332010-06-01 19:28:341005 AutoReset<bool> auto_reset_in_run(&g_is_opening_new_window, true);
[email protected]a585a8db2011-06-20 18:58:351006 Browser::OpenEmptyWindow([self lastProfile]);
[email protected]d630d7d52010-02-21 00:55:111007 }
[email protected]ff81e0a2009-04-13 14:58:541008
1009 // We've handled the reopen event, so return NO to tell AppKit not
1010 // to do anything.
1011 return NO;
1012}
1013
[email protected]88d74942009-01-21 22:04:441014- (void)initMenuState {
[email protected]3111f08b2009-04-30 16:01:521015 menuState_.reset(new CommandUpdater(NULL));
[email protected]fbc947b2009-06-19 13:28:241016 menuState_->UpdateCommandEnabled(IDC_NEW_TAB, true);
[email protected]88d74942009-01-21 22:04:441017 menuState_->UpdateCommandEnabled(IDC_NEW_WINDOW, true);
[email protected]863ff662009-01-26 20:18:181018 menuState_->UpdateCommandEnabled(IDC_NEW_INCOGNITO_WINDOW, true);
[email protected]e19516d2009-04-28 17:15:191019 menuState_->UpdateCommandEnabled(IDC_OPEN_FILE, true);
[email protected]93e181762009-05-29 14:30:381020 menuState_->UpdateCommandEnabled(IDC_CLEAR_BROWSING_DATA, true);
[email protected]fbc947b2009-06-19 13:28:241021 menuState_->UpdateCommandEnabled(IDC_RESTORE_TAB, false);
[email protected]91a5b3e2009-10-30 19:32:221022 menuState_->UpdateCommandEnabled(IDC_FOCUS_LOCATION, true);
[email protected]cddc5242009-12-07 18:54:331023 menuState_->UpdateCommandEnabled(IDC_FOCUS_SEARCH, true);
[email protected]af7c25e2010-01-23 05:46:451024 menuState_->UpdateCommandEnabled(IDC_SHOW_BOOKMARK_MANAGER, true);
[email protected]91a5b3e2009-10-30 19:32:221025 menuState_->UpdateCommandEnabled(IDC_SHOW_HISTORY, true);
1026 menuState_->UpdateCommandEnabled(IDC_SHOW_DOWNLOADS, true);
[email protected]bb37e6ec2009-11-18 17:59:361027 menuState_->UpdateCommandEnabled(IDC_MANAGE_EXTENSIONS, true);
[email protected]1fdff702009-10-22 00:36:181028 menuState_->UpdateCommandEnabled(IDC_HELP_PAGE, true);
[email protected]4aee5652009-11-19 18:59:211029 menuState_->UpdateCommandEnabled(IDC_IMPORT_SETTINGS, true);
[email protected]1c41a932010-11-22 23:41:231030 menuState_->UpdateCommandEnabled(IDC_FEEDBACK, true);
[email protected]d3b98c82010-07-14 07:45:591031 menuState_->UpdateCommandEnabled(IDC_SYNC_BOOKMARKS, true);
[email protected]ceaaf1812010-01-20 22:13:091032 menuState_->UpdateCommandEnabled(IDC_TASK_MANAGER, true);
[email protected]88d74942009-01-21 22:04:441033}
[email protected]1bcdb532009-01-16 17:47:571034
[email protected]961a6932011-07-19 19:52:461035// Conditionally adds the Profile menu to the main menu bar.
1036- (void)initProfileMenu {
[email protected]961a6932011-07-19 19:52:461037 NSMenu* mainMenu = [NSApp mainMenu];
1038 NSMenuItem* profileMenu = [mainMenu itemWithTag:IDC_PROFILE_MAIN_MENU];
[email protected]e354ca42011-08-03 20:31:401039
1040 // On Leopard, hiding main menubar items does not work. This manifests itself
1041 // in Chromium as squished menu items <http://crbug.com/90753>. To prevent
1042 // this, remove the Profile menu on Leopard, regardless of the user's
1043 // multiprofile state.
[email protected]6b2d30bc2011-10-11 18:16:391044 if (!ProfileManager::IsMultipleProfilesEnabled() ||
1045 base::mac::IsOSLeopard()) {
[email protected]e354ca42011-08-03 20:31:401046 [mainMenu removeItem:profileMenu];
1047 return;
1048 }
1049
[email protected]6b2d30bc2011-10-11 18:16:391050 // The controller will unhide the menu if necessary.
1051 [profileMenu setHidden:YES];
[email protected]961a6932011-07-19 19:52:461052
[email protected]6b2d30bc2011-10-11 18:16:391053 profileMenuController_.reset(
1054 [[ProfileMenuController alloc] initWithMainMenuItem:profileMenu]);
[email protected]961a6932011-07-19 19:52:461055}
1056
[email protected]a99fce0e2011-03-21 20:58:481057// The Confirm to Quit preference is atypical in that the preference lives in
1058// the app menu right above the Quit menu item. This method will refresh the
1059// display of that item depending on the preference state.
1060- (void)updateConfirmToQuitPrefMenuItem:(NSMenuItem*)item {
1061 // Format the string so that the correct key equivalent is displayed.
1062 NSString* acceleratorString = [ConfirmQuitPanelController keyCommandString];
1063 NSString* title = l10n_util::GetNSStringF(IDS_CONFIRM_TO_QUIT_OPTION,
1064 base::SysNSStringToUTF16(acceleratorString));
1065 [item setTitle:title];
1066
[email protected]a585a8db2011-06-20 18:58:351067 const PrefService* prefService = [self lastProfile]->GetPrefs();
[email protected]a99fce0e2011-03-21 20:58:481068 bool enabled = prefService->GetBoolean(prefs::kConfirmToQuitEnabled);
1069 [item setState:enabled ? NSOnState : NSOffState];
1070}
1071
[email protected]eef99c22010-08-17 05:55:161072- (void)registerServicesMenuTypesTo:(NSApplication*)app {
[email protected]09ea24c2010-08-23 19:05:411073 // Note that RenderWidgetHostViewCocoa implements NSServicesRequests which
1074 // handles requests from services.
1075 NSArray* types = [NSArray arrayWithObjects:NSStringPboardType, nil];
1076 [app registerServicesMenuSendTypes:types returnTypes:types];
[email protected]eef99c22010-08-17 05:55:161077}
1078
[email protected]a585a8db2011-06-20 18:58:351079- (Profile*)lastProfile {
[email protected]7fd5a462011-06-19 16:57:231080 // Return the profile of the last-used BrowserWindowController, if available.
1081 if (lastProfile_)
1082 return lastProfile_;
1083
[email protected]056f8792011-07-20 15:40:151084 // On first launch, no profile will be stored, so use last from Local State.
[email protected]3f34599d2009-03-25 22:11:431085 if (g_browser_process->profile_manager())
[email protected]056f8792011-07-20 15:40:151086 return g_browser_process->profile_manager()->GetLastUsedProfile();
[email protected]3f34599d2009-03-25 22:11:431087
[email protected]f6314002009-04-23 01:18:131088 return NULL;
[email protected]3f34599d2009-03-25 22:11:431089}
1090
[email protected]57750f822009-04-21 21:43:091091// Various methods to open URLs that we get in a native fashion. We use
1092// BrowserInit here because on the other platforms, URLs to open come through
1093// the ProcessSingleton, and it calls BrowserInit. It's best to bottleneck the
1094// openings through that for uniform handling.
1095
[email protected]d630d7d52010-02-21 00:55:111096- (void)openUrls:(const std::vector<GURL>&)urls {
[email protected]caadfca2010-01-22 21:51:211097 // If the browser hasn't started yet, just queue up the URLs.
1098 if (!startupComplete_) {
[email protected]d630d7d52010-02-21 00:55:111099 startupUrls_.insert(startupUrls_.end(), urls.begin(), urls.end());
[email protected]136140c2009-05-19 13:58:251100 return;
1101 }
[email protected]57750f822009-04-21 21:43:091102
[email protected]0e863512009-05-28 19:45:071103 Browser* browser = BrowserList::GetLastActive();
1104 // if no browser window exists then create one with no tabs to be filled in
1105 if (!browser) {
[email protected]a585a8db2011-06-20 18:58:351106 browser = Browser::Create([self lastProfile]);
[email protected]ce560f82009-06-03 09:39:441107 browser->window()->Show();
[email protected]0e863512009-05-28 19:45:071108 }
1109
[email protected]947446b2010-10-21 03:36:311110 CommandLine dummy(CommandLine::NO_PROGRAM);
[email protected]dfa08b042011-12-28 23:07:211111 BrowserInit::IsFirstRun first_run = first_run::IsChromeFirstRun() ?
[email protected]e4a377cf2011-10-05 02:51:491112 BrowserInit::IS_FIRST_RUN : BrowserInit::IS_NOT_FIRST_RUN;
1113 BrowserInit::LaunchWithProfile launch(FilePath(), dummy, first_run);
[email protected]0e863512009-05-28 19:45:071114 launch.OpenURLsInBrowser(browser, false, urls);
[email protected]57750f822009-04-21 21:43:091115}
1116
[email protected]57750f822009-04-21 21:43:091117- (void)getUrl:(NSAppleEventDescriptor*)event
1118 withReply:(NSAppleEventDescriptor*)reply {
1119 NSString* urlStr = [[event paramDescriptorForKeyword:keyDirectObject]
1120 stringValue];
1121
1122 GURL gurl(base::SysNSStringToUTF8(urlStr));
1123 std::vector<GURL> gurlVector;
1124 gurlVector.push_back(gurl);
1125
[email protected]d630d7d52010-02-21 00:55:111126 [self openUrls:gurlVector];
[email protected]57750f822009-04-21 21:43:091127}
1128
[email protected]a529af52011-08-15 20:56:171129// Apple Event handler that receives print event from service
1130// process, gets the required data and launches Print dialog.
1131- (void)submitCloudPrintJob:(NSAppleEventDescriptor*)event {
1132 // Pull parameter list out of Apple Event.
[email protected]ebbe94b2012-01-18 20:11:561133 NSAppleEventDescriptor* paramList =
[email protected]13aec6c42011-10-27 21:22:081134 [event paramDescriptorForKeyword:cloud_print::kAECloudPrintClass];
[email protected]a529af52011-08-15 20:56:171135
1136 if (paramList != nil) {
1137 // Pull required fields out of parameter list.
1138 NSString* mime = [[paramList descriptorAtIndex:1] stringValue];
1139 NSString* inputPath = [[paramList descriptorAtIndex:2] stringValue];
1140 NSString* printTitle = [[paramList descriptorAtIndex:3] stringValue];
[email protected]e8368e92011-08-20 04:05:561141 NSString* printTicket = [[paramList descriptorAtIndex:4] stringValue];
[email protected]a529af52011-08-15 20:56:171142 // Convert the title to UTF 16 as required.
1143 string16 title16 = base::SysNSStringToUTF16(printTitle);
[email protected]e8368e92011-08-20 04:05:561144 string16 printTicket16 = base::SysNSStringToUTF16(printTicket);
[email protected]a529af52011-08-15 20:56:171145 print_dialog_cloud::CreatePrintDialogForFile(
1146 FilePath([inputPath UTF8String]), title16,
[email protected]d955fc92011-09-19 20:49:031147 printTicket16, [mime UTF8String], /*modal=*/false,
1148 /*delete_on_close=*/false);
[email protected]a529af52011-08-15 20:56:171149 }
1150}
1151
1152// Calls the helper class to install the virtual driver to the
1153// service process.
1154- (void)installCloudPrint:(NSAppleEventDescriptor*)event {
1155 cloud_print::VirtualDriverInstallHelper::SetUpInstall();
1156}
1157
1158// Calls the helper class to uninstall the virtual driver to the
1159// service process.
1160- (void)uninstallCloudPrint:(NSAppleEventDescriptor*)event {
1161 cloud_print::VirtualDriverInstallHelper::SetUpUninstall();
1162}
1163
[email protected]caadfca2010-01-22 21:51:211164- (void)application:(NSApplication*)sender
1165 openFiles:(NSArray*)filenames {
[email protected]57750f822009-04-21 21:43:091166 std::vector<GURL> gurlVector;
[email protected]caadfca2010-01-22 21:51:211167 for (NSString* file in filenames) {
1168 GURL gurl = net::FilePathToFileURL(FilePath(base::SysNSStringToUTF8(file)));
[email protected]57750f822009-04-21 21:43:091169 gurlVector.push_back(gurl);
1170 }
[email protected]caadfca2010-01-22 21:51:211171 if (!gurlVector.empty())
[email protected]d630d7d52010-02-21 00:55:111172 [self openUrls:gurlVector];
[email protected]caadfca2010-01-22 21:51:211173 else
1174 NOTREACHED() << "Nothing to open!";
[email protected]57750f822009-04-21 21:43:091175
[email protected]caadfca2010-01-22 21:51:211176 [sender replyToOpenOrPrint:NSApplicationDelegateReplySuccess];
[email protected]57750f822009-04-21 21:43:091177}
[email protected]3f34599d2009-03-25 22:11:431178
[email protected]3111f08b2009-04-30 16:01:521179// Show the preferences window, or bring it to the front if it's already
1180// visible.
1181- (IBAction)showPreferences:(id)sender {
[email protected]a585a8db2011-06-20 18:58:351182 if (Browser* browser = ActivateBrowser([self lastProfile])) {
[email protected]445177652011-03-09 02:04:051183 // Show options tab in the active browser window.
1184 browser->OpenOptionsDialog();
[email protected]4a42d272010-06-18 01:29:421185 } else {
[email protected]445177652011-03-09 02:04:051186 // No browser window, so create one for the options tab.
[email protected]a585a8db2011-06-20 18:58:351187 Browser::OpenOptionsWindow([self lastProfile]);
[email protected]4a42d272010-06-18 01:29:421188 }
[email protected]d989f07c2009-11-14 00:35:461189}
1190
[email protected]bde3dda2009-05-20 22:13:071191// Called when the about window is closed. We use this to release the
1192// window controller.
[email protected]d4e18652009-11-19 21:52:161193- (void)aboutWindowClosed:(NSNotification*)notification {
1194 NSWindow* window = [aboutController_ window];
[email protected]2ce9c89752011-02-25 18:24:341195 DCHECK_EQ([notification object], window);
[email protected]bde3dda2009-05-20 22:13:071196 [[NSNotificationCenter defaultCenter]
[email protected]49aeee52009-10-26 19:58:131197 removeObserver:self
[email protected]d4e18652009-11-19 21:52:161198 name:NSWindowWillCloseNotification
1199 object:window];
1200 // AboutWindowControllers are autoreleased in
1201 // -[AboutWindowController windowWillClose:].
1202 aboutController_ = nil;
[email protected]bde3dda2009-05-20 22:13:071203}
1204
1205- (IBAction)orderFrontStandardAboutPanel:(id)sender {
[email protected]bde3dda2009-05-20 22:13:071206 if (!aboutController_) {
[email protected]d4e18652009-11-19 21:52:161207 aboutController_ =
[email protected]a585a8db2011-06-20 18:58:351208 [[AboutWindowController alloc] initWithProfile:[self lastProfile]];
[email protected]49aeee52009-10-26 19:58:131209
[email protected]bde3dda2009-05-20 22:13:071210 // Watch for a notification of when it goes away so that we can destroy
1211 // the controller.
1212 [[NSNotificationCenter defaultCenter]
1213 addObserver:self
1214 selector:@selector(aboutWindowClosed:)
[email protected]d4e18652009-11-19 21:52:161215 name:NSWindowWillCloseNotification
1216 object:[aboutController_ window]];
[email protected]bde3dda2009-05-20 22:13:071217 }
[email protected]49aeee52009-10-26 19:58:131218
[email protected]bde3dda2009-05-20 22:13:071219 [aboutController_ showWindow:self];
[email protected]bde3dda2009-05-20 22:13:071220}
1221
[email protected]a99fce0e2011-03-21 20:58:481222- (IBAction)toggleConfirmToQuit:(id)sender {
[email protected]a585a8db2011-06-20 18:58:351223 PrefService* prefService = [self lastProfile]->GetPrefs();
[email protected]a99fce0e2011-03-21 20:58:481224 bool enabled = prefService->GetBoolean(prefs::kConfirmToQuitEnabled);
1225 prefService->SetBoolean(prefs::kConfirmToQuitEnabled, !enabled);
1226}
1227
[email protected]3d4bd23f2009-10-06 03:58:381228// Explicitly bring to the foreground when creating new windows from the dock.
[email protected]93b59fc2010-12-21 20:00:471229- (void)commandFromDock:(id)sender {
[email protected]3d4bd23f2009-10-06 03:58:381230 [NSApp activateIgnoringOtherApps:YES];
1231 [self commandDispatch:sender];
1232}
1233
[email protected]227fcee2010-06-11 19:19:371234- (NSMenu*)applicationDockMenu:(NSApplication*)sender {
[email protected]018a3962009-09-17 22:23:441235 NSMenu* dockMenu = [[[NSMenu alloc] initWithTitle: @""] autorelease];
[email protected]a585a8db2011-06-20 18:58:351236 Profile* profile = [self lastProfile];
[email protected]93b59fc2010-12-21 20:00:471237
[email protected]018a3962009-09-17 22:23:441238 NSString* titleStr = l10n_util::GetNSStringWithFixup(IDS_NEW_WINDOW_MAC);
[email protected]8c6b029d2011-01-21 22:39:511239 scoped_nsobject<NSMenuItem> item(
1240 [[NSMenuItem alloc] initWithTitle:titleStr
1241 action:@selector(commandFromDock:)
1242 keyEquivalent:@""]);
[email protected]288bfcd32009-09-14 18:14:461243 [item setTarget:self];
1244 [item setTag:IDC_NEW_WINDOW];
[email protected]018a3962009-09-17 22:23:441245 [dockMenu addItem:item];
[email protected]288bfcd32009-09-14 18:14:461246
1247 titleStr = l10n_util::GetNSStringWithFixup(IDS_NEW_INCOGNITO_WINDOW_MAC);
[email protected]018a3962009-09-17 22:23:441248 item.reset([[NSMenuItem alloc] initWithTitle:titleStr
[email protected]8c6b029d2011-01-21 22:39:511249 action:@selector(commandFromDock:)
[email protected]018a3962009-09-17 22:23:441250 keyEquivalent:@""]);
[email protected]288bfcd32009-09-14 18:14:461251 [item setTarget:self];
1252 [item setTag:IDC_NEW_INCOGNITO_WINDOW];
[email protected]018a3962009-09-17 22:23:441253 [dockMenu addItem:item];
[email protected]288bfcd32009-09-14 18:14:461254
[email protected]8c6b029d2011-01-21 22:39:511255 // TODO(rickcam): Mock out BackgroundApplicationListModel, then add unit
1256 // tests which use the mock in place of the profile-initialized model.
1257
1258 // Avoid breaking unit tests which have no profile.
1259 if (profile) {
1260 BackgroundApplicationListModel applications(profile);
1261 if (applications.size()) {
1262 int position = 0;
1263 NSString* menuStr =
1264 l10n_util::GetNSStringWithFixup(IDS_BACKGROUND_APPS_MAC);
1265 scoped_nsobject<NSMenu> appMenu([[NSMenu alloc] initWithTitle:menuStr]);
1266 for (ExtensionList::const_iterator cursor = applications.begin();
1267 cursor != applications.end();
1268 ++cursor, ++position) {
[email protected]2ce9c89752011-02-25 18:24:341269 DCHECK_EQ(applications.GetPosition(*cursor), position);
[email protected]8c6b029d2011-01-21 22:39:511270 NSString* itemStr =
1271 base::SysUTF16ToNSString(UTF8ToUTF16((*cursor)->name()));
1272 scoped_nsobject<NSMenuItem> appItem([[NSMenuItem alloc]
1273 initWithTitle:itemStr
1274 action:@selector(commandFromDock:)
1275 keyEquivalent:@""]);
1276 [appItem setTarget:self];
1277 [appItem setTag:position];
1278 [appMenu addItem:appItem];
1279 }
1280 scoped_nsobject<NSMenuItem> appMenuItem([[NSMenuItem alloc]
1281 initWithTitle:menuStr
1282 action:@selector(commandFromDock:)
1283 keyEquivalent:@""]);
1284 [appMenuItem setTarget:self];
1285 [appMenuItem setTag:position];
1286 [appMenuItem setSubmenu:appMenu];
1287 [dockMenu addItem:appMenuItem];
1288 }
1289 }
1290
[email protected]018a3962009-09-17 22:23:441291 return dockMenu;
[email protected]288bfcd32009-09-14 18:14:461292}
1293
[email protected]d630d7d52010-02-21 00:55:111294- (const std::vector<GURL>&)startupUrls {
1295 return startupUrls_;
[email protected]caadfca2010-01-22 21:51:211296}
1297
[email protected]d630d7d52010-02-21 00:55:111298- (void)clearStartupUrls {
1299 startupUrls_.clear();
[email protected]caadfca2010-01-22 21:51:211300}
1301
[email protected]7e4c69d02011-05-11 22:16:471302- (BookmarkMenuBridge*)bookmarkMenuBridge {
1303 return bookmarkMenuBridge_.get();
1304}
1305
[email protected]ebbe94b2012-01-18 20:11:561306- (void)applicationDidChangeScreenParameters:(NSNotification*)notification {
[email protected]6a455072011-11-02 01:32:281307 // During this callback the working area is not always already updated. Defer.
1308 [self performSelector:@selector(delayedPanelManagerScreenParametersUpdate)
1309 withObject:nil
1310 afterDelay:0];
1311}
1312
1313- (void)delayedPanelManagerScreenParametersUpdate {
1314 PanelManager::GetInstance()->OnDisplayChanged();
1315}
1316
[email protected]d630d7d52010-02-21 00:55:111317@end // @implementation AppController
[email protected]60ad3e22009-09-18 21:07:191318
1319//---------------------------------------------------------------------------
1320
[email protected]0934de82011-03-09 18:42:161321namespace browser {
1322
1323void ShowInstantConfirmDialog(gfx::NativeWindow parent, Profile* profile) {
1324 if (Browser* browser = ActivateBrowser(profile)) {
[email protected]b99081b2011-03-10 17:43:471325 browser->OpenInstantConfirmDialog();
[email protected]0934de82011-03-09 18:42:161326 } else {
[email protected]b99081b2011-03-10 17:43:471327 Browser::OpenInstantConfirmDialogWindow(profile);
[email protected]0934de82011-03-09 18:42:161328 }
1329}
1330
1331} // namespace browser
1332
[email protected]216e9042009-10-29 17:05:231333namespace app_controller_mac {
1334
1335bool IsOpeningNewWindow() {
1336 return g_is_opening_new_window;
1337}
1338
1339} // namespace app_controller_mac