blob: f18fc40642bb184bd76e408b83f7c201ce032794 [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]eddcf7502012-02-09 22:43:4899// True while AppController is calling Browser::NewEmptyWindow(). We need a
[email protected]d630d7d52010-02-21 00:55:11100// 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]eddcf7502012-02-09 22:43:48121 Browser::NewEmptyWindow(profile);
[email protected]d630d7d52010-02-21 00:55:11122 }
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();
[email protected]b6b72222012-02-11 02:04:13161 CFPreferencesSetAppValue(
162 base::mac::NSToCFCast(app_mode::kLastRunAppBundlePathPrefsKey),
163 base::SysUTF8ToCFStringRef(appBundlePath.value()),
164 BaseBundleID_CFString());
[email protected]12dc3d42010-02-22 23:37:12165
166 // Sync after a delay avoid I/O contention on startup; 1500 ms is plenty.
[email protected]d04e7662010-10-10 22:24:48167 BrowserThread::PostDelayedTask(BrowserThread::FILE, FROM_HERE,
[email protected]0b8caad2011-11-28 23:33:27168 base::Bind(&PrefsSyncCallback), 1500);
[email protected]12dc3d42010-02-22 23:37:12169}
170
[email protected]d630d7d52010-02-21 00:55:11171} // anonymous namespace
172
[email protected]a529af52011-08-15 20:56:17173const AEEventClass kAECloudPrintInstallClass = 'GCPi';
174const AEEventClass kAECloudPrintUninstallClass = 'GCPu';
175
[email protected]a99fce0e2011-03-21 20:58:48176@interface AppController (Private)
[email protected]88d74942009-01-21 22:04:44177- (void)initMenuState;
[email protected]961a6932011-07-19 19:52:46178- (void)initProfileMenu;
[email protected]a99fce0e2011-03-21 20:58:48179- (void)updateConfirmToQuitPrefMenuItem:(NSMenuItem*)item;
[email protected]eef99c22010-08-17 05:55:16180- (void)registerServicesMenuTypesTo:(NSApplication*)app;
[email protected]d630d7d52010-02-21 00:55:11181- (void)openUrls:(const std::vector<GURL>&)urls;
[email protected]57750f822009-04-21 21:43:09182- (void)getUrl:(NSAppleEventDescriptor*)event
183 withReply:(NSAppleEventDescriptor*)reply;
[email protected]a529af52011-08-15 20:56:17184- (void)submitCloudPrintJob:(NSAppleEventDescriptor*)event;
185- (void)installCloudPrint:(NSAppleEventDescriptor*)event;
186- (void)uninstallCloudPrint:(NSAppleEventDescriptor*)event;
[email protected]449dd2f2009-05-27 13:04:00187- (void)windowLayeringDidChange:(NSNotification*)inNotification;
[email protected]5c593702011-06-17 18:35:24188- (void)windowChangedToProfile:(Profile*)profile;
[email protected]dd6ab1282010-07-20 01:07:32189- (void)checkForAnyKeyWindows;
[email protected]73f5b362009-08-10 23:58:21190- (BOOL)userWillWaitForInProgressDownloads:(int)downloadCount;
[email protected]09729a552009-08-03 23:21:41191- (BOOL)shouldQuitWithInProgressDownloads;
[email protected]93b59fc2010-12-21 20:00:47192- (void)executeApplication:(id)sender;
[email protected]88d74942009-01-21 22:04:44193@end
[email protected]1bcdb532009-01-16 17:47:57194
195@implementation AppController
196
[email protected]caadfca2010-01-22 21:51:21197@synthesize startupComplete = startupComplete_;
198
[email protected]cd63ef62009-05-06 19:41:37199// This method is called very early in application startup (ie, before
200// the profile is loaded or any preferences have been registered). Defer any
201// user-data initialization until -applicationDidFinishLaunching:.
[email protected]88d74942009-01-21 22:04:44202- (void)awakeFromNib {
[email protected]136140c2009-05-19 13:58:25203 // We need to register the handlers early to catch events fired on launch.
[email protected]57750f822009-04-21 21:43:09204 NSAppleEventManager* em = [NSAppleEventManager sharedAppleEventManager];
205 [em setEventHandler:self
206 andSelector:@selector(getUrl:withReply:)
207 forEventClass:kInternetEventClass
208 andEventID:kAEGetURL];
209 [em setEventHandler:self
[email protected]a529af52011-08-15 20:56:17210 andSelector:@selector(submitCloudPrintJob:)
[email protected]13aec6c42011-10-27 21:22:08211 forEventClass:cloud_print::kAECloudPrintClass
212 andEventID:cloud_print::kAECloudPrintClass];
[email protected]a529af52011-08-15 20:56:17213 // Install and uninstall handlers for virtual drivers.
214 [em setEventHandler:self
215 andSelector:@selector(installCloudPrint:)
216 forEventClass:kAECloudPrintInstallClass
217 andEventID:kAECloudPrintInstallClass];
218 [em setEventHandler:self
219 andSelector:@selector(uninstallCloudPrint:)
220 forEventClass:kAECloudPrintUninstallClass
221 andEventID:kAECloudPrintUninstallClass];
222 [em setEventHandler:self
[email protected]57750f822009-04-21 21:43:09223 andSelector:@selector(getUrl:withReply:)
224 forEventClass:'WWW!' // A particularly ancient AppleEvent that dates
225 andEventID:'OURL']; // back to the Spyglass days.
[email protected]cd63ef62009-05-06 19:41:37226
[email protected]449dd2f2009-05-27 13:04:00227 // Register for various window layering changes. We use these to update
228 // various UI elements (command-key equivalents, etc) when the frontmost
229 // window changes.
230 NSNotificationCenter* notificationCenter =
231 [NSNotificationCenter defaultCenter];
[email protected]ce560f82009-06-03 09:39:44232 [notificationCenter
[email protected]449dd2f2009-05-27 13:04:00233 addObserver:self
234 selector:@selector(windowLayeringDidChange:)
235 name:NSWindowDidBecomeKeyNotification
236 object:nil];
237 [notificationCenter
238 addObserver:self
239 selector:@selector(windowLayeringDidChange:)
240 name:NSWindowDidResignKeyNotification
241 object:nil];
242 [notificationCenter
243 addObserver:self
244 selector:@selector(windowLayeringDidChange:)
245 name:NSWindowDidBecomeMainNotification
246 object:nil];
247 [notificationCenter
248 addObserver:self
249 selector:@selector(windowLayeringDidChange:)
250 name:NSWindowDidResignMainNotification
251 object:nil];
252
[email protected]ebbe94b2012-01-18 20:11:56253 if (base::mac::IsOSLionOrLater()) {
254 [notificationCenter
255 addObserver:self
256 selector:@selector(popoverDidShow:)
257 name:NSPopoverDidShowNotification
258 object:nil];
259 [notificationCenter
260 addObserver:self
261 selector:@selector(popoverDidClose:)
262 name:NSPopoverDidCloseNotification
263 object:nil];
264 }
265
[email protected]136140c2009-05-19 13:58:25266 // Set up the command updater for when there are no windows open
267 [self initMenuState];
[email protected]961a6932011-07-19 19:52:46268
269 // Initialize the Profile menu.
270 [self initProfileMenu];
[email protected]136140c2009-05-19 13:58:25271}
272
[email protected]71c0eb92012-01-03 17:57:30273- (void)unregisterEventHandlers {
274 NSAppleEventManager* em = [NSAppleEventManager sharedAppleEventManager];
275 [em removeEventHandlerForEventClass:kInternetEventClass
276 andEventID:kAEGetURL];
277 [em removeEventHandlerForEventClass:cloud_print::kAECloudPrintClass
278 andEventID:cloud_print::kAECloudPrintClass];
279 [em removeEventHandlerForEventClass:kAECloudPrintInstallClass
280 andEventID:kAECloudPrintInstallClass];
281 [em removeEventHandlerForEventClass:kAECloudPrintUninstallClass
282 andEventID:kAECloudPrintUninstallClass];
283 [em removeEventHandlerForEventClass:'WWW!'
284 andEventID:'OURL'];
285 [[NSNotificationCenter defaultCenter] removeObserver:self];
286}
287
[email protected]7dc8c6b2010-04-09 17:02:50288// (NSApplicationDelegate protocol) This is the Apple-approved place to override
289// the default handlers.
290- (void)applicationWillFinishLaunching:(NSNotification*)notification {
[email protected]58877932010-05-25 05:43:11291 // Nothing here right now.
[email protected]7dc8c6b2010-04-09 17:02:50292}
293
294- (BOOL)tryToTerminateApplication:(NSApplication*)app {
[email protected]3076fad2010-04-28 18:11:45295 // Check for in-process downloads, and prompt the user if they really want
296 // to quit (and thus cancel downloads). Only check if we're not already
297 // shutting down, else the user might be prompted multiple times if the
298 // download isn't stopped before terminate is called again.
299 if (!browser_shutdown::IsTryingToQuit() &&
300 ![self shouldQuitWithInProgressDownloads])
301 return NO;
302
[email protected]7dc8c6b2010-04-09 17:02:50303 // TODO(viettrungluu): Remove Apple Event handlers here? (It's safe to leave
304 // them in, but I'm not sure about UX; we'd also want to disable other things
305 // though.) http://crbug.com/40861
306
[email protected]177aceb2010-11-03 16:17:41307 // Check if the user really wants to quit by employing the confirm-to-quit
308 // mechanism.
309 if (!browser_shutdown::IsTryingToQuit() &&
310 [self applicationShouldTerminate:app] != NSTerminateNow)
311 return NO;
312
[email protected]c984d9f2010-07-20 20:52:20313 size_t num_browsers = BrowserList::size();
[email protected]7dc8c6b2010-04-09 17:02:50314
[email protected]e7dd6d82010-10-07 23:21:12315 // Give any print jobs in progress time to finish.
316 if (!browser_shutdown::IsTryingToQuit())
317 g_browser_process->print_job_manager()->StopJobs(true);
318
[email protected]c984d9f2010-07-20 20:52:20319 // Initiate a shutdown (via BrowserList::CloseAllBrowsers()) if we aren't
320 // already shutting down.
[email protected]ef61b8c2012-01-20 10:54:56321 if (!browser_shutdown::IsTryingToQuit()) {
322 content::NotificationService::current()->Notify(
323 content::NOTIFICATION_APP_EXITING,
324 content::NotificationService::AllSources(),
325 content::NotificationService::NoDetails());
[email protected]c6032e82010-09-13 20:06:05326 BrowserList::CloseAllBrowsers();
[email protected]ef61b8c2012-01-20 10:54:56327 }
[email protected]7dc8c6b2010-04-09 17:02:50328
[email protected]c984d9f2010-07-20 20:52:20329 return num_browsers == 0 ? YES : NO;
[email protected]7dc8c6b2010-04-09 17:02:50330}
331
332- (void)stopTryingToTerminateApplication:(NSApplication*)app {
333 if (browser_shutdown::IsTryingToQuit()) {
334 // Reset the "trying to quit" state, so that closing all browser windows
335 // will no longer lead to termination.
336 browser_shutdown::SetTryingToQuit(false);
337
338 // TODO(viettrungluu): Were we to remove Apple Event handlers above, we
339 // would have to reinstall them here. http://crbug.com/40861
340 }
[email protected]a9e8afc2009-08-11 22:03:17341}
342
[email protected]177aceb2010-11-03 16:17:41343- (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication*)app {
[email protected]a99fce0e2011-03-21 20:58:48344 // Check if the preference is turned on.
[email protected]a585a8db2011-06-20 18:58:35345 const PrefService* prefs = [self lastProfile]->GetPrefs();
[email protected]1eaddf32011-04-05 15:09:23346 if (!prefs->GetBoolean(prefs::kConfirmToQuitEnabled)) {
347 confirm_quit::RecordHistogram(confirm_quit::kNoConfirm);
[email protected]177aceb2010-11-03 16:17:41348 return NSTerminateNow;
[email protected]1eaddf32011-04-05 15:09:23349 }
[email protected]177aceb2010-11-03 16:17:41350
351 // If the application is going to terminate as the result of a Cmd+Q
352 // invocation, use the special sauce to prevent accidental quitting.
353 // http://dev.chromium.org/developers/design-documents/confirm-to-quit-experiment
[email protected]177aceb2010-11-03 16:17:41354
[email protected]2020fd0b2010-12-10 01:16:50355 // This logic is only for keyboard-initiated quits.
[email protected]5df54fb2011-03-07 18:15:26356 if (![ConfirmQuitPanelController eventTriggersFeature:[app currentEvent]])
[email protected]2020fd0b2010-12-10 01:16:50357 return NSTerminateNow;
[email protected]177aceb2010-11-03 16:17:41358
[email protected]5df54fb2011-03-07 18:15:26359 return [[ConfirmQuitPanelController sharedController]
360 runModalLoopForApplication:app];
[email protected]177aceb2010-11-03 16:17:41361}
362
[email protected]449dd2f2009-05-27 13:04:00363// Called when the app is shutting down. Clean-up as appropriate.
[email protected]7dc8c6b2010-04-09 17:02:50364- (void)applicationWillTerminate:(NSNotification*)aNotification {
[email protected]7dc8c6b2010-04-09 17:02:50365 // There better be no browser windows left at this point.
[email protected]71c0eb92012-01-03 17:57:30366 CHECK_EQ(0u, BrowserList::size());
[email protected]3b6aa8b62009-09-15 21:36:11367
[email protected]c984d9f2010-07-20 20:52:20368 // Tell BrowserList not to keep the browser process alive. Once all the
369 // browsers get dealloc'd, it will stop the RunLoop and fall back into main().
370 BrowserList::EndKeepAlive();
[email protected]fbc947b2009-06-19 13:28:24371
[email protected]d4e18652009-11-19 21:52:16372 // Close these off if they have open windows.
[email protected]d4e18652009-11-19 21:52:16373 [aboutController_ close];
374
[email protected]71c0eb92012-01-03 17:57:30375 [self unregisterEventHandlers];
[email protected]449dd2f2009-05-27 13:04:00376}
377
[email protected]3b6aa8b62009-09-15 21:36:11378- (void)didEndMainMessageLoop {
[email protected]a585a8db2011-06-20 18:58:35379 DCHECK(!BrowserList::HasBrowserWithProfile([self lastProfile]));
380 if (!BrowserList::HasBrowserWithProfile([self lastProfile])) {
[email protected]3b6aa8b62009-09-15 21:36:11381 // As we're shutting down, we need to nuke the TabRestoreService, which
382 // will start the shutdown of the NavigationControllers and allow for
383 // proper shutdown. If we don't do this, Chrome won't shut down cleanly,
384 // and may end up crashing when some thread tries to use the IO thread (or
385 // another thread) that is no longer valid.
[email protected]a585a8db2011-06-20 18:58:35386 TabRestoreServiceFactory::ResetForProfile([self lastProfile]);
[email protected]3b6aa8b62009-09-15 21:36:11387 }
388}
389
[email protected]8cfbdbd2011-06-16 04:17:17390// If the window has a tab controller, make "close window" be cmd-shift-w,
391// otherwise leave it as the normal cmd-w. Capitalization of the key equivalent
[email protected]eddcf7502012-02-09 22:43:48392// affects whether the shift modifier is used.
[email protected]ebbe94b2012-01-18 20:11:56393- (void)adjustCloseWindowMenuItemKeyEquivalent:(BOOL)enableCloseTabShortcut {
394 [closeWindowMenuItem_ setKeyEquivalent:(enableCloseTabShortcut ? @"W" :
395 @"w")];
[email protected]8cfbdbd2011-06-16 04:17:17396 [closeWindowMenuItem_ setKeyEquivalentModifierMask:NSCommandKeyMask];
397}
398
399// If the window has a tab controller, make "close tab" take over cmd-w,
400// otherwise it shouldn't have any key-equivalent because it should be disabled.
[email protected]ebbe94b2012-01-18 20:11:56401- (void)adjustCloseTabMenuItemKeyEquivalent:(BOOL)enableCloseTabShortcut {
402 if (enableCloseTabShortcut) {
[email protected]8cfbdbd2011-06-16 04:17:17403 [closeTabMenuItem_ setKeyEquivalent:@"w"];
404 [closeTabMenuItem_ setKeyEquivalentModifierMask:NSCommandKeyMask];
405 } else {
406 [closeTabMenuItem_ setKeyEquivalent:@""];
407 [closeTabMenuItem_ setKeyEquivalentModifierMask:0];
408 }
409}
410
411// Explicitly remove any command-key equivalents from the close tab/window
412// menus so that nothing can go haywire if we get a user action during pending
413// updates.
414- (void)clearCloseMenuItemKeyEquivalents {
415 [closeTabMenuItem_ setKeyEquivalent:@""];
416 [closeTabMenuItem_ setKeyEquivalentModifierMask:0];
417 [closeWindowMenuItem_ setKeyEquivalent:@""];
418 [closeWindowMenuItem_ setKeyEquivalentModifierMask:0];
419}
420
[email protected]9f5f79b62011-10-28 00:43:45421// See if the focused window window has tabs, and adjust the key equivalents for
[email protected]8cfbdbd2011-06-16 04:17:17422// Close Tab/Close Window accordingly.
[email protected]9f5f79b62011-10-28 00:43:45423- (void)fixCloseMenuItemKeyEquivalents {
[email protected]8cfbdbd2011-06-16 04:17:17424 fileMenuUpdatePending_ = NO;
[email protected]8cfbdbd2011-06-16 04:17:17425
[email protected]9f5f79b62011-10-28 00:43:45426 NSWindow* window = [NSApp keyWindow];
427 NSWindow* mainWindow = [NSApp mainWindow];
428 if (!window || ([window parentWindow] == mainWindow)) {
429 // If the key window is a child of the main window (e.g. a bubble), the main
430 // window should be the one that handles the close menu item action.
431 // Also, there might be a small amount of time where there is no key window;
432 // in that case as well, just use our main browser window if there is one.
433 // You might think that we should just always use the main window, but the
434 // "About Chrome" window serves as a counterexample.
435 window = mainWindow;
436 }
437
438 BOOL hasTabs =
439 [[window windowController] isKindOfClass:[TabWindowController class]];
[email protected]ebbe94b2012-01-18 20:11:56440 BOOL enableCloseTabShortcut = hasTabs && !hasPopover_;
441 [self adjustCloseWindowMenuItemKeyEquivalent:enableCloseTabShortcut];
442 [self adjustCloseTabMenuItemKeyEquivalent:enableCloseTabShortcut];
[email protected]8cfbdbd2011-06-16 04:17:17443}
444
445// Fix up the "close tab/close window" command-key equivalents. We do this
446// after a delay to ensure that window layer state has been set by the time
447// we do the enabling. This should only be called on the main thread, code that
448// calls this (even as a side-effect) from other threads needs to be fixed.
[email protected]9f5f79b62011-10-28 00:43:45449- (void)delayedFixCloseMenuItemKeyEquivalents {
[email protected]8cfbdbd2011-06-16 04:17:17450 DCHECK([NSThread isMainThread]);
451 if (!fileMenuUpdatePending_) {
452 // The OS prefers keypresses to timers, so it's possible that a cmd-w
453 // can sneak in before this timer fires. In order to prevent that from
454 // having any bad consequences, just clear the keys combos altogether. They
455 // will be reset when the timer eventually fires.
456 if ([NSThread isMainThread]) {
457 fileMenuUpdatePending_ = YES;
458 [self clearCloseMenuItemKeyEquivalents];
[email protected]9f5f79b62011-10-28 00:43:45459 [self performSelector:@selector(fixCloseMenuItemKeyEquivalents)
460 withObject:nil
[email protected]8cfbdbd2011-06-16 04:17:17461 afterDelay:0];
462 } else {
463 // This shouldn't be happening, but if it does, force it to the main
464 // thread to avoid dropping the update. Don't mess with
465 // |fileMenuUpdatePending_| as it's not expected to be threadsafe and
466 // there could be a race between the selector finishing and setting the
467 // flag.
468 [self
[email protected]9f5f79b62011-10-28 00:43:45469 performSelectorOnMainThread:@selector(fixCloseMenuItemKeyEquivalents)
470 withObject:nil
[email protected]8cfbdbd2011-06-16 04:17:17471 waitUntilDone:NO];
472 }
473 }
474}
475
[email protected]449dd2f2009-05-27 13:04:00476// Called when we get a notification about the window layering changing to
477// update the UI based on the new main window.
478- (void)windowLayeringDidChange:(NSNotification*)notify {
[email protected]9f5f79b62011-10-28 00:43:45479 [self delayedFixCloseMenuItemKeyEquivalents];
[email protected]8cfbdbd2011-06-16 04:17:17480
[email protected]dd6ab1282010-07-20 01:07:32481 if ([notify name] == NSWindowDidResignKeyNotification) {
482 // If a window is closed, this notification is fired but |[NSApp keyWindow]|
483 // returns nil regardless of whether any suitable candidates for the key
484 // window remain. It seems that the new key window for the app is not set
485 // until after this notification is fired, so a check is performed after the
486 // run loop is allowed to spin.
487 [self performSelector:@selector(checkForAnyKeyWindows)
488 withObject:nil
489 afterDelay:0.0];
490 }
[email protected]5c593702011-06-17 18:35:24491
492 // If the window changed to a new BrowserWindowController, update the profile.
493 id windowController = [[notify object] windowController];
[email protected]dc251482012-01-03 18:55:21494 if ([notify name] == NSWindowDidBecomeMainNotification &&
495 [windowController isKindOfClass:[BrowserWindowController class]]) {
[email protected]5c593702011-06-17 18:35:24496 // If the profile is incognito, use the original profile.
497 Profile* newProfile = [windowController profile]->GetOriginalProfile();
498 [self windowChangedToProfile:newProfile];
[email protected]cae68b3a2011-11-21 23:29:49499 } else if (BrowserList::empty()) {
500 [self windowChangedToProfile:
501 g_browser_process->profile_manager()->GetLastUsedProfile()];
[email protected]5c593702011-06-17 18:35:24502 }
503}
504
[email protected]ebbe94b2012-01-18 20:11:56505// Called on Lion and later when a popover (e.g. dictionary) is shown.
506- (void)popoverDidShow:(NSNotification*)notify {
507 hasPopover_ = YES;
508 [self fixCloseMenuItemKeyEquivalents];
509}
510
511// Called on Lion and later when a popover (e.g. dictionary) is closed.
512- (void)popoverDidClose:(NSNotification*)notify {
513 hasPopover_ = NO;
514 [self fixCloseMenuItemKeyEquivalents];
515}
516
[email protected]5c593702011-06-17 18:35:24517// Called when the user has changed browser windows, meaning the backing profile
518// may have changed. This can cause a rebuild of the user-data menus. This is a
519// no-op if the new profile is the same as the current one. This will always be
520// the original profile and never incognito.
521- (void)windowChangedToProfile:(Profile*)profile {
522 if (lastProfile_ == profile)
523 return;
524
525 // Before tearing down the menu controller bridges, return the Cocoa menus to
526 // their initial state.
527 if (bookmarkMenuBridge_.get())
528 bookmarkMenuBridge_->ResetMenu();
529 if (historyMenuBridge_.get())
530 historyMenuBridge_->ResetMenu();
531
532 // Rebuild the menus with the new profile.
533 lastProfile_ = profile;
534
[email protected]0c165cf2011-07-09 03:56:50535 bookmarkMenuBridge_.reset(new BookmarkMenuBridge(lastProfile_,
536 [[[NSApp mainMenu] itemWithTag:IDC_BOOKMARKS_MENU] submenu]));
[email protected]dc251482012-01-03 18:55:21537 // No need to |BuildMenu| here. It is done lazily upon menu access.
[email protected]5c593702011-06-17 18:35:24538
539 historyMenuBridge_.reset(new HistoryMenuBridge(lastProfile_));
540 historyMenuBridge_->BuildMenu();
[email protected]dd6ab1282010-07-20 01:07:32541}
542
543- (void)checkForAnyKeyWindows {
544 if ([NSApp keyWindow])
545 return;
546
[email protected]ad50def52011-10-19 23:17:07547 content::NotificationService::current()->Notify(
[email protected]666641df2011-07-10 18:24:38548 content::NOTIFICATION_NO_KEY_WINDOW,
[email protected]ad50def52011-10-19 23:17:07549 content::NotificationService::AllSources(),
550 content::NotificationService::NoDetails());
[email protected]449dd2f2009-05-27 13:04:00551}
552
[email protected]414fde592009-05-21 16:14:43553// If the auto-update interval is not set, make it 5 hours.
554// This code is specific to Mac Chrome Dev Channel.
555// Placed here for 2 reasons:
556// 1) Same spot as other Pref stuff
557// 2) Try and be friendly by keeping this after app launch
558// TODO(jrg): remove once we go Beta.
559- (void)setUpdateCheckInterval {
560#if defined(GOOGLE_CHROME_BUILD)
561 CFStringRef app = (CFStringRef)@"com.google.Keystone.Agent";
562 CFStringRef checkInterval = (CFStringRef)@"checkInterval";
563 CFPropertyListRef plist = CFPreferencesCopyAppValue(checkInterval, app);
564 if (!plist) {
565 const float fiveHoursInSeconds = 5.0 * 60.0 * 60.0;
[email protected]7dc8c6b2010-04-09 17:02:50566 NSNumber* value = [NSNumber numberWithFloat:fiveHoursInSeconds];
[email protected]414fde592009-05-21 16:14:43567 CFPreferencesSetAppValue(checkInterval, value, app);
568 CFPreferencesAppSynchronize(app);
569 }
570#endif
571}
572
[email protected]136140c2009-05-19 13:58:25573// This is called after profiles have been loaded and preferences registered.
574// It is safe to access the default profile here.
575- (void)applicationDidFinishLaunching:(NSNotification*)notify {
[email protected]c984d9f2010-07-20 20:52:20576 // Notify BrowserList to keep the application running so it doesn't go away
577 // when all the browser windows get closed.
578 BrowserList::StartKeepAlive();
[email protected]136140c2009-05-19 13:58:25579
[email protected]414fde592009-05-21 16:14:43580 [self setUpdateCheckInterval];
[email protected]2bcec612009-05-14 17:50:53581
582 // Build up the encoding menu, the order of the items differs based on the
583 // current locale (see http://crbug.com/7647 for details).
584 // We need a valid g_browser_process to get the profile which is why we can't
585 // call this from awakeFromNib.
[email protected]6f91e5ae2011-03-21 19:53:14586 NSMenu* viewMenu = [[[NSApp mainMenu] itemWithTag:IDC_VIEW_MENU] submenu];
587 NSMenuItem* encodingMenuItem = [viewMenu itemWithTag:IDC_ENCODING_MENU];
588 NSMenu* encodingMenu = [encodingMenuItem submenu];
[email protected]a585a8db2011-06-20 18:58:35589 EncodingMenuControllerDelegate::BuildEncodingMenu([self lastProfile],
[email protected]6f91e5ae2011-03-21 19:53:14590 encodingMenu);
[email protected]2bcec612009-05-14 17:50:53591
[email protected]f2c20fa2009-12-01 17:42:02592 // Since Chrome is localized to more languages than the OS, tell Cocoa which
593 // menu is the Help so it can add the search item to it.
[email protected]caadfca2010-01-22 21:51:21594 if (helpMenu_ && [NSApp respondsToSelector:@selector(setHelpMenu:)])
[email protected]f2c20fa2009-12-01 17:42:02595 [NSApp setHelpMenu:helpMenu_];
[email protected]f2c20fa2009-12-01 17:42:02596
[email protected]12dc3d42010-02-22 23:37:12597 // Record the path to the (browser) app bundle; this is used by the app mode
598 // shim.
599 RecordLastRunAppBundlePath();
600
[email protected]eef99c22010-08-17 05:55:16601 // Makes "Services" menu items available.
602 [self registerServicesMenuTypesTo:[notify object]];
603
[email protected]caadfca2010-01-22 21:51:21604 startupComplete_ = YES;
605
606 // TODO(viettrungluu): This is very temporary, since this should be done "in"
607 // |BrowserMain()|, i.e., this list of startup URLs should be appended to the
608 // (probably-empty) list of URLs from the command line.
[email protected]d630d7d52010-02-21 00:55:11609 if (startupUrls_.size()) {
610 [self openUrls:startupUrls_];
611 [self clearStartupUrls];
[email protected]caadfca2010-01-22 21:51:21612 }
[email protected]aac169d2011-03-18 19:53:03613
614 const CommandLine& parsed_command_line = *CommandLine::ForCurrentProcess();
615 if (!parsed_command_line.HasSwitch(switches::kEnableExposeForTabs)) {
616 [tabposeMenuItem_ setHidden:YES];
617 }
[email protected]7c321082009-02-09 15:35:47618}
619
[email protected]a96ec6a2009-11-04 17:27:08620// This is called after profiles have been loaded and preferences registered.
621// It is safe to access the default profile here.
622- (void)applicationDidBecomeActive:(NSNotification*)notify {
[email protected]ad50def52011-10-19 23:17:07623 content::NotificationService::current()->Notify(
624 content::NOTIFICATION_APP_ACTIVATED,
625 content::NotificationService::AllSources(),
626 content::NotificationService::NoDetails());
[email protected]a96ec6a2009-11-04 17:27:08627}
628
[email protected]73f5b362009-08-10 23:58:21629// Helper function for populating and displaying the in progress downloads at
630// exit alert panel.
631- (BOOL)userWillWaitForInProgressDownloads:(int)downloadCount {
[email protected]e8b5f7882009-09-14 20:59:13632 NSString* warningText = nil;
633 NSString* explanationText = nil;
[email protected]73f5b362009-08-10 23:58:21634 NSString* waitTitle = nil;
635 NSString* exitTitle = nil;
636
637 // Set the dialog text based on whether or not there are multiple downloads.
638 if (downloadCount == 1) {
[email protected]e8b5f7882009-09-14 20:59:13639 // Dialog text: warning and explanation.
[email protected]ab3b1c112012-01-13 22:04:16640 warningText = l10n_util::GetNSString(
641 IDS_SINGLE_DOWNLOAD_REMOVE_CONFIRM_WARNING);
642 explanationText = l10n_util::GetNSString(
643 IDS_SINGLE_DOWNLOAD_REMOVE_CONFIRM_EXPLANATION);
[email protected]73f5b362009-08-10 23:58:21644
645 // Cancel download and exit button text.
[email protected]757127052010-04-27 14:42:26646 exitTitle = l10n_util::GetNSString(
647 IDS_SINGLE_DOWNLOAD_REMOVE_CONFIRM_OK_BUTTON_LABEL);
[email protected]73f5b362009-08-10 23:58:21648
649 // Wait for download button text.
[email protected]757127052010-04-27 14:42:26650 waitTitle = l10n_util::GetNSString(
651 IDS_SINGLE_DOWNLOAD_REMOVE_CONFIRM_CANCEL_BUTTON_LABEL);
[email protected]73f5b362009-08-10 23:58:21652 } else {
[email protected]e8b5f7882009-09-14 20:59:13653 // Dialog text: warning and explanation.
[email protected]757127052010-04-27 14:42:26654 warningText = l10n_util::GetNSStringF(
[email protected]ab3b1c112012-01-13 22:04:16655 IDS_MULTIPLE_DOWNLOADS_REMOVE_CONFIRM_WARNING,
[email protected]528c56d2010-07-30 19:28:44656 base::IntToString16(downloadCount));
[email protected]ab3b1c112012-01-13 22:04:16657 explanationText = l10n_util::GetNSString(
658 IDS_MULTIPLE_DOWNLOADS_REMOVE_CONFIRM_EXPLANATION);
[email protected]73f5b362009-08-10 23:58:21659
660 // Cancel downloads and exit button text.
[email protected]757127052010-04-27 14:42:26661 exitTitle = l10n_util::GetNSString(
662 IDS_MULTIPLE_DOWNLOADS_REMOVE_CONFIRM_OK_BUTTON_LABEL);
[email protected]73f5b362009-08-10 23:58:21663
664 // Wait for downloads button text.
[email protected]757127052010-04-27 14:42:26665 waitTitle = l10n_util::GetNSString(
666 IDS_MULTIPLE_DOWNLOADS_REMOVE_CONFIRM_CANCEL_BUTTON_LABEL);
[email protected]73f5b362009-08-10 23:58:21667 }
668
669 // 'waitButton' is the default choice.
[email protected]e8b5f7882009-09-14 20:59:13670 int choice = NSRunAlertPanel(warningText, explanationText,
671 waitTitle, exitTitle, nil);
[email protected]73f5b362009-08-10 23:58:21672 return choice == NSAlertDefaultReturn ? YES : NO;
673}
674
675// Check all profiles for in progress downloads, and if we find any, prompt the
[email protected]09729a552009-08-03 23:21:41676// user to see if we should continue to exit (and thus cancel the downloads), or
677// if we should wait.
678- (BOOL)shouldQuitWithInProgressDownloads {
[email protected]73f5b362009-08-10 23:58:21679 ProfileManager* profile_manager = g_browser_process->profile_manager();
680 if (!profile_manager)
681 return YES;
[email protected]09729a552009-08-03 23:21:41682
[email protected]844a1002011-04-19 11:37:21683 std::vector<Profile*> profiles(profile_manager->GetLoadedProfiles());
684 for (size_t i = 0; i < profiles.size(); ++i) {
[email protected]9bb54ee2011-10-12 17:43:35685 DownloadService* download_service =
686 DownloadServiceFactory::GetForProfile(profiles[i]);
687 DownloadManager* download_manager =
688 (download_service->HasCreatedDownloadManager() ?
689 download_service->GetDownloadManager() : NULL);
[email protected]5656f8a2011-11-17 16:12:58690 if (download_manager && download_manager->InProgressCount() > 0) {
691 int downloadCount = download_manager->InProgressCount();
[email protected]73f5b362009-08-10 23:58:21692 if ([self userWillWaitForInProgressDownloads:downloadCount]) {
693 // Create a new browser window (if necessary) and navigate to the
694 // downloads page if the user chooses to wait.
[email protected]844a1002011-04-19 11:37:21695 Browser* browser = BrowserList::FindBrowserWithProfile(profiles[i]);
[email protected]73f5b362009-08-10 23:58:21696 if (!browser) {
[email protected]844a1002011-04-19 11:37:21697 browser = Browser::Create(profiles[i]);
[email protected]73f5b362009-08-10 23:58:21698 browser->window()->Show();
699 }
700 DCHECK(browser);
701 browser->ShowDownloadsTab();
702 return NO;
703 }
[email protected]09729a552009-08-03 23:21:41704
[email protected]73f5b362009-08-10 23:58:21705 // User wants to exit.
706 return YES;
[email protected]09729a552009-08-03 23:21:41707 }
[email protected]09729a552009-08-03 23:21:41708 }
709
[email protected]73f5b362009-08-10 23:58:21710 // No profiles or active downloads found, okay to exit.
[email protected]09729a552009-08-03 23:21:41711 return YES;
712}
713
[email protected]fbc947b2009-06-19 13:28:24714// Called to determine if we should enable the "restore tab" menu item.
715// Checks with the TabRestoreService to see if there's anything there to
716// restore and returns YES if so.
717- (BOOL)canRestoreTab {
[email protected]92371eb2011-04-28 11:50:15718 TabRestoreService* service =
[email protected]a585a8db2011-06-20 18:58:35719 TabRestoreServiceFactory::GetForProfile([self lastProfile]);
[email protected]fbc947b2009-06-19 13:28:24720 return service && !service->entries().empty();
721}
722
[email protected]f6c15fa2010-06-04 20:08:21723// Returns true if there is not a modal window (either window- or application-
724// modal) blocking the active browser. Note that tab modal dialogs (HTTP auth
725// sheets) will not count as blocking the browser. But things like open/save
726// dialogs that are window modal will block the browser.
727- (BOOL)keyWindowIsNotModal {
[email protected]6a281332009-10-13 17:41:06728 Browser* browser = BrowserList::GetLastActive();
[email protected]dc76e8362010-06-11 16:29:52729 return [NSApp modalWindow] == nil && (!browser ||
[email protected]6a281332009-10-13 17:41:06730 ![[browser->window()->GetNativeHandle() attachedSheet]
[email protected]f6c15fa2010-06-04 20:08:21731 isKindOfClass:[NSWindow class]]);
[email protected]6a281332009-10-13 17:41:06732}
733
[email protected]88d74942009-01-21 22:04:44734// Called to validate menu items when there are no key windows. All the
735// items we care about have been set with the |commandDispatch:| action and
736// a target of FirstResponder in IB. If it's not one of those, let it
737// continue up the responder chain to be handled elsewhere. We pull out the
738// tag as the cross-platform constant to differentiate and dispatch the
739// various commands.
740- (BOOL)validateUserInterfaceItem:(id<NSValidatedUserInterfaceItem>)item {
741 SEL action = [item action];
742 BOOL enable = NO;
743 if (action == @selector(commandDispatch:)) {
744 NSInteger tag = [item tag];
[email protected]fbc947b2009-06-19 13:28:24745 if (menuState_->SupportsCommand(tag)) {
746 switch (tag) {
[email protected]419eb2a2009-11-11 16:26:26747 // The File Menu commands are not automatically disabled by Cocoa when a
748 // dialog sheet obscures the browser window, so we disable several of
749 // them here. We don't need to include IDC_CLOSE_WINDOW, because
750 // app_controller is only activated when there are no key windows (see
751 // function comment).
[email protected]fbc947b2009-06-19 13:28:24752 case IDC_RESTORE_TAB:
[email protected]f6c15fa2010-06-04 20:08:21753 enable = [self keyWindowIsNotModal] && [self canRestoreTab];
[email protected]6a281332009-10-13 17:41:06754 break;
[email protected]f6c15fa2010-06-04 20:08:21755 // Browser-level items that open in new tabs should not open if there's
756 // a window- or app-modal dialog.
[email protected]6a281332009-10-13 17:41:06757 case IDC_OPEN_FILE:
[email protected]6a281332009-10-13 17:41:06758 case IDC_NEW_TAB:
[email protected]f6c15fa2010-06-04 20:08:21759 case IDC_SHOW_HISTORY:
760 case IDC_SHOW_BOOKMARK_MANAGER:
761 enable = [self keyWindowIsNotModal];
762 break;
763 // Browser-level items that open in new windows.
764 case IDC_NEW_WINDOW:
765 case IDC_TASK_MANAGER:
766 // Allow the user to open a new window if there's a window-modal
767 // dialog.
768 enable = [self keyWindowIsNotModal] || ([NSApp modalWindow] == nil);
[email protected]fbc947b2009-06-19 13:28:24769 break;
[email protected]65549182012-02-25 00:45:40770 case IDC_SHOW_SYNC_SETUP: {
[email protected]a585a8db2011-06-20 18:58:35771 Profile* lastProfile = [self lastProfile];
[email protected]446e16a2010-05-03 21:23:58772 // The profile may be NULL during shutdown -- see
773 // http://code.google.com/p/chromium/issues/detail?id=43048 .
774 //
775 // TODO(akalin,viettrungluu): Figure out whether this method
[email protected]a585a8db2011-06-20 18:58:35776 // can be prevented from being called if lastProfile is
[email protected]446e16a2010-05-03 21:23:58777 // NULL.
[email protected]a585a8db2011-06-20 18:58:35778 if (!lastProfile) {
[email protected]446e16a2010-05-03 21:23:58779 LOG(WARNING)
[email protected]a585a8db2011-06-20 18:58:35780 << "NULL lastProfile detected -- not doing anything";
[email protected]446e16a2010-05-03 21:23:58781 break;
782 }
[email protected]a585a8db2011-06-20 18:58:35783 enable = lastProfile->IsSyncAccessible() &&
[email protected]d3b98c82010-07-14 07:45:59784 [self keyWindowIsNotModal];
[email protected]a585a8db2011-06-20 18:58:35785 sync_ui_util::UpdateSyncItem(item, enable, lastProfile);
[email protected]f9bc9b92009-11-24 00:55:35786 break;
[email protected]446e16a2010-05-03 21:23:58787 }
[email protected]acaa37b2011-08-31 07:19:11788 case IDC_FEEDBACK:
789 enable = NO;
790 break;
[email protected]fbc947b2009-06-19 13:28:24791 default:
[email protected]f6c15fa2010-06-04 20:08:21792 enable = menuState_->IsCommandEnabled(tag) ?
793 [self keyWindowIsNotModal] : NO;
[email protected]fbc947b2009-06-19 13:28:24794 }
795 }
[email protected]3b6aa8b62009-09-15 21:36:11796 } else if (action == @selector(terminate:)) {
[email protected]88d74942009-01-21 22:04:44797 enable = YES;
[email protected]3111f08b2009-04-30 16:01:52798 } else if (action == @selector(showPreferences:)) {
799 enable = YES;
[email protected]bde3dda2009-05-20 22:13:07800 } else if (action == @selector(orderFrontStandardAboutPanel:)) {
801 enable = YES;
[email protected]93b59fc2010-12-21 20:00:47802 } else if (action == @selector(commandFromDock:)) {
[email protected]3d4bd23f2009-10-06 03:58:38803 enable = YES;
[email protected]a99fce0e2011-03-21 20:58:48804 } else if (action == @selector(toggleConfirmToQuit:)) {
805 [self updateConfirmToQuitPrefMenuItem:static_cast<NSMenuItem*>(item)];
806 enable = YES;
[email protected]88d74942009-01-21 22:04:44807 }
808 return enable;
809}
810
[email protected]947fc0d2010-01-14 22:33:05811// Called when the user picks a menu item when there are no key windows, or when
812// there is no foreground browser window. Calls through to the browser object to
813// execute the command. This assumes that the command is supported and doesn't
[email protected]d630d7d52010-02-21 00:55:11814// check, otherwise it should have been disabled in the UI in
815// |-validateUserInterfaceItem:|.
[email protected]88d74942009-01-21 22:04:44816- (void)commandDispatch:(id)sender {
[email protected]a585a8db2011-06-20 18:58:35817 Profile* lastProfile = [self lastProfile];
[email protected]f0a51fb52009-03-05 12:46:38818
[email protected]947fc0d2010-01-14 22:33:05819 // Handle the case where we're dispatching a command from a sender that's in a
820 // browser window. This means that the command came from a background window
821 // and is getting here because the foreground window is not a browser window.
822 if ([sender respondsToSelector:@selector(window)]) {
823 id delegate = [[sender window] windowController];
824 if ([delegate isKindOfClass:[BrowserWindowController class]]) {
825 [delegate commandDispatch:sender];
826 return;
827 }
828 }
829
[email protected]88d74942009-01-21 22:04:44830 NSInteger tag = [sender tag];
831 switch (tag) {
[email protected]fbc947b2009-06-19 13:28:24832 case IDC_NEW_TAB:
[email protected]d630d7d52010-02-21 00:55:11833 // Create a new tab in an existing browser window (which we activate) if
834 // possible.
[email protected]a585a8db2011-06-20 18:58:35835 if (Browser* browser = ActivateBrowser(lastProfile)) {
[email protected]d630d7d52010-02-21 00:55:11836 browser->ExecuteCommand(IDC_NEW_TAB);
837 break;
838 }
839 // Else fall through to create new window.
[email protected]88d74942009-01-21 22:04:44840 case IDC_NEW_WINDOW:
[email protected]a585a8db2011-06-20 18:58:35841 CreateBrowser(lastProfile);
[email protected]d630d7d52010-02-21 00:55:11842 break;
[email protected]91a5b3e2009-10-30 19:32:22843 case IDC_FOCUS_LOCATION:
[email protected]a585a8db2011-06-20 18:58:35844 ActivateOrCreateBrowser(lastProfile)->ExecuteCommand(IDC_FOCUS_LOCATION);
[email protected]88d74942009-01-21 22:04:44845 break;
[email protected]cddc5242009-12-07 18:54:33846 case IDC_FOCUS_SEARCH:
[email protected]a585a8db2011-06-20 18:58:35847 ActivateOrCreateBrowser(lastProfile)->ExecuteCommand(IDC_FOCUS_SEARCH);
[email protected]cddc5242009-12-07 18:54:33848 break;
[email protected]863ff662009-01-26 20:18:18849 case IDC_NEW_INCOGNITO_WINDOW:
[email protected]eddcf7502012-02-09 22:43:48850 CreateBrowser(lastProfile->GetOffTheRecordProfile());
[email protected]863ff662009-01-26 20:18:18851 break;
[email protected]fbc947b2009-06-19 13:28:24852 case IDC_RESTORE_TAB:
[email protected]a585a8db2011-06-20 18:58:35853 Browser::OpenWindowWithRestoredTabs(lastProfile);
[email protected]fbc947b2009-06-19 13:28:24854 break;
[email protected]e19516d2009-04-28 17:15:19855 case IDC_OPEN_FILE:
[email protected]a585a8db2011-06-20 18:58:35856 CreateBrowser(lastProfile)->ExecuteCommand(IDC_OPEN_FILE);
[email protected]e19516d2009-04-28 17:15:19857 break;
[email protected]1fdff702009-10-22 00:36:18858 case IDC_CLEAR_BROWSING_DATA: {
[email protected]93e181762009-05-29 14:30:38859 // There may not be a browser open, so use the default profile.
[email protected]a585a8db2011-06-20 18:58:35860 if (Browser* browser = ActivateBrowser(lastProfile)) {
[email protected]445177652011-03-09 02:04:05861 browser->OpenClearBrowsingDataDialog();
[email protected]c8de64a2011-01-25 17:10:23862 } else {
[email protected]ddddfda2011-07-14 23:19:39863 Browser::OpenClearBrowsingDataDialogWindow(lastProfile);
[email protected]c8de64a2011-01-25 17:10:23864 }
[email protected]93e181762009-05-29 14:30:38865 break;
[email protected]1fdff702009-10-22 00:36:18866 }
[email protected]4aee5652009-11-19 18:59:21867 case IDC_IMPORT_SETTINGS: {
[email protected]a585a8db2011-06-20 18:58:35868 if (Browser* browser = ActivateBrowser(lastProfile)) {
[email protected]445177652011-03-09 02:04:05869 browser->OpenImportSettingsDialog();
[email protected]c8de64a2011-01-25 17:10:23870 } else {
[email protected]a585a8db2011-06-20 18:58:35871 Browser::OpenImportSettingsDialogWindow(lastProfile);
[email protected]c8de64a2011-01-25 17:10:23872 }
[email protected]4aee5652009-11-19 18:59:21873 break;
874 }
[email protected]af7c25e2010-01-23 05:46:45875 case IDC_SHOW_BOOKMARK_MANAGER:
[email protected]7f6f44c2011-12-14 13:23:38876 content::RecordAction(UserMetricsAction("ShowBookmarkManager"));
[email protected]a585a8db2011-06-20 18:58:35877 if (Browser* browser = ActivateBrowser(lastProfile)) {
[email protected]47e532b2010-04-23 17:46:40878 // Open a bookmark manager tab.
[email protected]90a8bf252010-03-12 00:09:47879 browser->OpenBookmarkManager();
880 } else {
[email protected]47e532b2010-04-23 17:46:40881 // No browser window, so create one for the bookmark manager tab.
[email protected]a585a8db2011-06-20 18:58:35882 Browser::OpenBookmarkManagerWindow(lastProfile);
[email protected]90a8bf252010-03-12 00:09:47883 }
[email protected]af7c25e2010-01-23 05:46:45884 break;
[email protected]91a5b3e2009-10-30 19:32:22885 case IDC_SHOW_HISTORY:
[email protected]a585a8db2011-06-20 18:58:35886 if (Browser* browser = ActivateBrowser(lastProfile))
[email protected]d630d7d52010-02-21 00:55:11887 browser->ShowHistoryTab();
888 else
[email protected]a585a8db2011-06-20 18:58:35889 Browser::OpenHistoryWindow(lastProfile);
[email protected]91a5b3e2009-10-30 19:32:22890 break;
891 case IDC_SHOW_DOWNLOADS:
[email protected]a585a8db2011-06-20 18:58:35892 if (Browser* browser = ActivateBrowser(lastProfile))
[email protected]d630d7d52010-02-21 00:55:11893 browser->ShowDownloadsTab();
894 else
[email protected]a585a8db2011-06-20 18:58:35895 Browser::OpenDownloadsWindow(lastProfile);
[email protected]91a5b3e2009-10-30 19:32:22896 break;
[email protected]bb37e6ec2009-11-18 17:59:36897 case IDC_MANAGE_EXTENSIONS:
[email protected]a585a8db2011-06-20 18:58:35898 if (Browser* browser = ActivateBrowser(lastProfile))
[email protected]d630d7d52010-02-21 00:55:11899 browser->ShowExtensionsTab();
900 else
[email protected]a585a8db2011-06-20 18:58:35901 Browser::OpenExtensionsWindow(lastProfile);
[email protected]bb37e6ec2009-11-18 17:59:36902 break;
[email protected]1fdff702009-10-22 00:36:18903 case IDC_HELP_PAGE:
[email protected]a585a8db2011-06-20 18:58:35904 if (Browser* browser = ActivateBrowser(lastProfile))
[email protected]202802e42011-07-06 11:40:46905 browser->ShowHelpTab();
[email protected]d630d7d52010-02-21 00:55:11906 else
[email protected]a585a8db2011-06-20 18:58:35907 Browser::OpenHelpWindow(lastProfile);
[email protected]1fdff702009-10-22 00:36:18908 break;
[email protected]65549182012-02-25 00:45:40909 case IDC_SHOW_SYNC_SETUP:
910 if (Browser* browser = ActivateBrowser(lastProfile))
911 browser->ShowSyncSetup();
912 else
913 Browser::OpenSyncSetupWindow(lastProfile);
[email protected]f9bc9b92009-11-24 00:55:35914 break;
[email protected]ceaaf1812010-01-20 22:13:09915 case IDC_TASK_MANAGER:
[email protected]7f6f44c2011-12-14 13:23:38916 content::RecordAction(UserMetricsAction("TaskManager"));
[email protected]adb6a84d2011-02-07 16:58:40917 TaskManagerMac::Show(false);
[email protected]ceaaf1812010-01-20 22:13:09918 break;
[email protected]4a42d272010-06-18 01:29:42919 case IDC_OPTIONS:
920 [self showPreferences:sender];
921 break;
[email protected]93b59fc2010-12-21 20:00:47922 default:
923 // Background Applications use dynamic values that must be less than the
924 // smallest value among the predefined IDC_* labels.
925 if ([sender tag] < IDC_MinimumLabelValue)
926 [self executeApplication:sender];
927 break;
[email protected]d630d7d52010-02-21 00:55:11928 }
[email protected]88d74942009-01-21 22:04:44929}
930
[email protected]93b59fc2010-12-21 20:00:47931// Run a (background) application in a new tab.
932- (void)executeApplication:(id)sender {
933 NSInteger tag = [sender tag];
[email protected]a585a8db2011-06-20 18:58:35934 Profile* profile = [self lastProfile];
[email protected]93b59fc2010-12-21 20:00:47935 DCHECK(profile);
936 BackgroundApplicationListModel applications(profile);
937 DCHECK(tag >= 0 &&
938 tag < static_cast<int>(applications.size()));
[email protected]93b59fc2010-12-21 20:00:47939 const Extension* extension = applications.GetExtension(tag);
[email protected]78d78a62011-09-29 18:24:33940 BackgroundModeManager::LaunchBackgroundApplication(profile, extension);
[email protected]93b59fc2010-12-21 20:00:47941}
942
[email protected]947fc0d2010-01-14 22:33:05943// Same as |-commandDispatch:|, but executes commands using a disposition
944// determined by the key flags. This will get called in the case where the
945// frontmost window is not a browser window, and the user has command-clicked
946// a button in a background browser window whose action is
947// |-commandDispatchUsingKeyModifiers:|
948- (void)commandDispatchUsingKeyModifiers:(id)sender {
949 DCHECK(sender);
950 if ([sender respondsToSelector:@selector(window)]) {
951 id delegate = [[sender window] windowController];
952 if ([delegate isKindOfClass:[BrowserWindowController class]]) {
953 [delegate commandDispatchUsingKeyModifiers:sender];
954 }
955 }
956}
957
[email protected]ff81e0a2009-04-13 14:58:54958// NSApplication delegate method called when someone clicks on the
959// dock icon and there are no open windows. To match standard mac
960// behavior, we should open a new window.
961- (BOOL)applicationShouldHandleReopen:(NSApplication*)theApplication
962 hasVisibleWindows:(BOOL)flag {
[email protected]70631402010-04-26 20:16:54963 // If the browser is currently trying to quit, don't do anything and return NO
964 // to prevent AppKit from doing anything.
965 // TODO(rohitrao): Remove this code when http://crbug.com/40861 is resolved.
966 if (browser_shutdown::IsTryingToQuit())
967 return NO;
968
[email protected]0236be22012-01-11 01:05:35969 // Don't do anything if there are visible tabbed or popup windows. This will
970 // cause AppKit to unminimize the most recently minimized window. If the
971 // visible windows are panels or notifications, we still need to open a new
972 // window.
973 if (flag) {
974 for (BrowserList::const_iterator iter = BrowserList::begin();
975 iter != BrowserList::end(); ++iter) {
976 Browser* browser = *iter;
977 if (browser->is_type_tabbed() || browser->is_type_popup())
978 return YES;
979 }
980 }
[email protected]ff81e0a2009-04-13 14:58:54981
[email protected]0d1210d62010-09-07 17:31:38982 // If launched as a hidden login item (due to installation of a persistent app
[email protected]eddcf7502012-02-09 22:43:48983 // or by the user, for example in System Preferences->Accounts->Login Items),
[email protected]0d1210d62010-09-07 17:31:38984 // allow session to be restored first time the user clicks on a Dock icon.
985 // Normally, it'd just open a new empty page.
986 {
[email protected]eddcf7502012-02-09 22:43:48987 static BOOL doneOnce = NO;
988 if (!doneOnce) {
989 doneOnce = YES;
990 if (base::mac::WasLaunchedAsHiddenLoginItem()) {
991 SessionService* sessionService =
992 SessionServiceFactory::GetForProfile([self lastProfile]);
993 if (sessionService &&
994 sessionService->RestoreIfNecessary(std::vector<GURL>()))
995 return NO;
[email protected]0d1210d62010-09-07 17:31:38996 }
[email protected]eddcf7502012-02-09 22:43:48997 }
[email protected]0d1210d62010-09-07 17:31:38998 }
[email protected]fc44f242012-02-14 16:54:39999
1000 // Platform apps don't use browser windows so don't do anything if there are
1001 // visible windows.
1002 const CommandLine& command_line = *CommandLine::ForCurrentProcess();
1003 if (flag && command_line.HasSwitch(switches::kAppId))
1004 return YES;
1005
[email protected]ff81e0a2009-04-13 14:58:541006 // Otherwise open a new window.
[email protected]d630d7d52010-02-21 00:55:111007 {
[email protected]0fbd70332010-06-01 19:28:341008 AutoReset<bool> auto_reset_in_run(&g_is_opening_new_window, true);
[email protected]fc44f242012-02-14 16:54:391009 int return_code;
1010 BrowserInit browser_init;
1011 browser_init.LaunchBrowser(command_line, [self lastProfile], FilePath(),
1012 BrowserInit::IS_NOT_PROCESS_STARTUP,
1013 BrowserInit::IS_NOT_FIRST_RUN, &return_code);
[email protected]d630d7d52010-02-21 00:55:111014 }
[email protected]ff81e0a2009-04-13 14:58:541015
1016 // We've handled the reopen event, so return NO to tell AppKit not
1017 // to do anything.
1018 return NO;
1019}
1020
[email protected]88d74942009-01-21 22:04:441021- (void)initMenuState {
[email protected]3111f08b2009-04-30 16:01:521022 menuState_.reset(new CommandUpdater(NULL));
[email protected]fbc947b2009-06-19 13:28:241023 menuState_->UpdateCommandEnabled(IDC_NEW_TAB, true);
[email protected]88d74942009-01-21 22:04:441024 menuState_->UpdateCommandEnabled(IDC_NEW_WINDOW, true);
[email protected]863ff662009-01-26 20:18:181025 menuState_->UpdateCommandEnabled(IDC_NEW_INCOGNITO_WINDOW, true);
[email protected]e19516d2009-04-28 17:15:191026 menuState_->UpdateCommandEnabled(IDC_OPEN_FILE, true);
[email protected]93e181762009-05-29 14:30:381027 menuState_->UpdateCommandEnabled(IDC_CLEAR_BROWSING_DATA, true);
[email protected]fbc947b2009-06-19 13:28:241028 menuState_->UpdateCommandEnabled(IDC_RESTORE_TAB, false);
[email protected]91a5b3e2009-10-30 19:32:221029 menuState_->UpdateCommandEnabled(IDC_FOCUS_LOCATION, true);
[email protected]cddc5242009-12-07 18:54:331030 menuState_->UpdateCommandEnabled(IDC_FOCUS_SEARCH, true);
[email protected]af7c25e2010-01-23 05:46:451031 menuState_->UpdateCommandEnabled(IDC_SHOW_BOOKMARK_MANAGER, true);
[email protected]91a5b3e2009-10-30 19:32:221032 menuState_->UpdateCommandEnabled(IDC_SHOW_HISTORY, true);
1033 menuState_->UpdateCommandEnabled(IDC_SHOW_DOWNLOADS, true);
[email protected]bb37e6ec2009-11-18 17:59:361034 menuState_->UpdateCommandEnabled(IDC_MANAGE_EXTENSIONS, true);
[email protected]1fdff702009-10-22 00:36:181035 menuState_->UpdateCommandEnabled(IDC_HELP_PAGE, true);
[email protected]4aee5652009-11-19 18:59:211036 menuState_->UpdateCommandEnabled(IDC_IMPORT_SETTINGS, true);
[email protected]1c41a932010-11-22 23:41:231037 menuState_->UpdateCommandEnabled(IDC_FEEDBACK, true);
[email protected]65549182012-02-25 00:45:401038 menuState_->UpdateCommandEnabled(IDC_SHOW_SYNC_SETUP, true);
[email protected]ceaaf1812010-01-20 22:13:091039 menuState_->UpdateCommandEnabled(IDC_TASK_MANAGER, true);
[email protected]88d74942009-01-21 22:04:441040}
[email protected]1bcdb532009-01-16 17:47:571041
[email protected]961a6932011-07-19 19:52:461042// Conditionally adds the Profile menu to the main menu bar.
1043- (void)initProfileMenu {
[email protected]961a6932011-07-19 19:52:461044 NSMenu* mainMenu = [NSApp mainMenu];
1045 NSMenuItem* profileMenu = [mainMenu itemWithTag:IDC_PROFILE_MAIN_MENU];
[email protected]e354ca42011-08-03 20:31:401046
1047 // On Leopard, hiding main menubar items does not work. This manifests itself
1048 // in Chromium as squished menu items <http://crbug.com/90753>. To prevent
1049 // this, remove the Profile menu on Leopard, regardless of the user's
1050 // multiprofile state.
[email protected]6b2d30bc2011-10-11 18:16:391051 if (!ProfileManager::IsMultipleProfilesEnabled() ||
1052 base::mac::IsOSLeopard()) {
[email protected]e354ca42011-08-03 20:31:401053 [mainMenu removeItem:profileMenu];
1054 return;
1055 }
1056
[email protected]6b2d30bc2011-10-11 18:16:391057 // The controller will unhide the menu if necessary.
1058 [profileMenu setHidden:YES];
[email protected]961a6932011-07-19 19:52:461059
[email protected]6b2d30bc2011-10-11 18:16:391060 profileMenuController_.reset(
1061 [[ProfileMenuController alloc] initWithMainMenuItem:profileMenu]);
[email protected]961a6932011-07-19 19:52:461062}
1063
[email protected]a99fce0e2011-03-21 20:58:481064// The Confirm to Quit preference is atypical in that the preference lives in
1065// the app menu right above the Quit menu item. This method will refresh the
1066// display of that item depending on the preference state.
1067- (void)updateConfirmToQuitPrefMenuItem:(NSMenuItem*)item {
1068 // Format the string so that the correct key equivalent is displayed.
1069 NSString* acceleratorString = [ConfirmQuitPanelController keyCommandString];
1070 NSString* title = l10n_util::GetNSStringF(IDS_CONFIRM_TO_QUIT_OPTION,
1071 base::SysNSStringToUTF16(acceleratorString));
1072 [item setTitle:title];
1073
[email protected]a585a8db2011-06-20 18:58:351074 const PrefService* prefService = [self lastProfile]->GetPrefs();
[email protected]a99fce0e2011-03-21 20:58:481075 bool enabled = prefService->GetBoolean(prefs::kConfirmToQuitEnabled);
1076 [item setState:enabled ? NSOnState : NSOffState];
1077}
1078
[email protected]eef99c22010-08-17 05:55:161079- (void)registerServicesMenuTypesTo:(NSApplication*)app {
[email protected]09ea24c2010-08-23 19:05:411080 // Note that RenderWidgetHostViewCocoa implements NSServicesRequests which
1081 // handles requests from services.
1082 NSArray* types = [NSArray arrayWithObjects:NSStringPboardType, nil];
1083 [app registerServicesMenuSendTypes:types returnTypes:types];
[email protected]eef99c22010-08-17 05:55:161084}
1085
[email protected]a585a8db2011-06-20 18:58:351086- (Profile*)lastProfile {
[email protected]7fd5a462011-06-19 16:57:231087 // Return the profile of the last-used BrowserWindowController, if available.
1088 if (lastProfile_)
1089 return lastProfile_;
1090
[email protected]056f8792011-07-20 15:40:151091 // On first launch, no profile will be stored, so use last from Local State.
[email protected]3f34599d2009-03-25 22:11:431092 if (g_browser_process->profile_manager())
[email protected]056f8792011-07-20 15:40:151093 return g_browser_process->profile_manager()->GetLastUsedProfile();
[email protected]3f34599d2009-03-25 22:11:431094
[email protected]f6314002009-04-23 01:18:131095 return NULL;
[email protected]3f34599d2009-03-25 22:11:431096}
1097
[email protected]57750f822009-04-21 21:43:091098// Various methods to open URLs that we get in a native fashion. We use
1099// BrowserInit here because on the other platforms, URLs to open come through
1100// the ProcessSingleton, and it calls BrowserInit. It's best to bottleneck the
1101// openings through that for uniform handling.
1102
[email protected]d630d7d52010-02-21 00:55:111103- (void)openUrls:(const std::vector<GURL>&)urls {
[email protected]caadfca2010-01-22 21:51:211104 // If the browser hasn't started yet, just queue up the URLs.
1105 if (!startupComplete_) {
[email protected]d630d7d52010-02-21 00:55:111106 startupUrls_.insert(startupUrls_.end(), urls.begin(), urls.end());
[email protected]136140c2009-05-19 13:58:251107 return;
1108 }
[email protected]57750f822009-04-21 21:43:091109
[email protected]0e863512009-05-28 19:45:071110 Browser* browser = BrowserList::GetLastActive();
1111 // if no browser window exists then create one with no tabs to be filled in
1112 if (!browser) {
[email protected]a585a8db2011-06-20 18:58:351113 browser = Browser::Create([self lastProfile]);
[email protected]ce560f82009-06-03 09:39:441114 browser->window()->Show();
[email protected]0e863512009-05-28 19:45:071115 }
1116
[email protected]947446b2010-10-21 03:36:311117 CommandLine dummy(CommandLine::NO_PROGRAM);
[email protected]dfa08b042011-12-28 23:07:211118 BrowserInit::IsFirstRun first_run = first_run::IsChromeFirstRun() ?
[email protected]e4a377cf2011-10-05 02:51:491119 BrowserInit::IS_FIRST_RUN : BrowserInit::IS_NOT_FIRST_RUN;
1120 BrowserInit::LaunchWithProfile launch(FilePath(), dummy, first_run);
[email protected]0e863512009-05-28 19:45:071121 launch.OpenURLsInBrowser(browser, false, urls);
[email protected]57750f822009-04-21 21:43:091122}
1123
[email protected]57750f822009-04-21 21:43:091124- (void)getUrl:(NSAppleEventDescriptor*)event
1125 withReply:(NSAppleEventDescriptor*)reply {
1126 NSString* urlStr = [[event paramDescriptorForKeyword:keyDirectObject]
1127 stringValue];
1128
1129 GURL gurl(base::SysNSStringToUTF8(urlStr));
1130 std::vector<GURL> gurlVector;
1131 gurlVector.push_back(gurl);
1132
[email protected]d630d7d52010-02-21 00:55:111133 [self openUrls:gurlVector];
[email protected]57750f822009-04-21 21:43:091134}
1135
[email protected]a529af52011-08-15 20:56:171136// Apple Event handler that receives print event from service
1137// process, gets the required data and launches Print dialog.
1138- (void)submitCloudPrintJob:(NSAppleEventDescriptor*)event {
1139 // Pull parameter list out of Apple Event.
[email protected]ebbe94b2012-01-18 20:11:561140 NSAppleEventDescriptor* paramList =
[email protected]13aec6c42011-10-27 21:22:081141 [event paramDescriptorForKeyword:cloud_print::kAECloudPrintClass];
[email protected]a529af52011-08-15 20:56:171142
1143 if (paramList != nil) {
1144 // Pull required fields out of parameter list.
1145 NSString* mime = [[paramList descriptorAtIndex:1] stringValue];
1146 NSString* inputPath = [[paramList descriptorAtIndex:2] stringValue];
1147 NSString* printTitle = [[paramList descriptorAtIndex:3] stringValue];
[email protected]e8368e92011-08-20 04:05:561148 NSString* printTicket = [[paramList descriptorAtIndex:4] stringValue];
[email protected]a529af52011-08-15 20:56:171149 // Convert the title to UTF 16 as required.
1150 string16 title16 = base::SysNSStringToUTF16(printTitle);
[email protected]e8368e92011-08-20 04:05:561151 string16 printTicket16 = base::SysNSStringToUTF16(printTicket);
[email protected]a529af52011-08-15 20:56:171152 print_dialog_cloud::CreatePrintDialogForFile(
1153 FilePath([inputPath UTF8String]), title16,
[email protected]d955fc92011-09-19 20:49:031154 printTicket16, [mime UTF8String], /*modal=*/false,
1155 /*delete_on_close=*/false);
[email protected]a529af52011-08-15 20:56:171156 }
1157}
1158
1159// Calls the helper class to install the virtual driver to the
1160// service process.
1161- (void)installCloudPrint:(NSAppleEventDescriptor*)event {
1162 cloud_print::VirtualDriverInstallHelper::SetUpInstall();
1163}
1164
1165// Calls the helper class to uninstall the virtual driver to the
1166// service process.
1167- (void)uninstallCloudPrint:(NSAppleEventDescriptor*)event {
1168 cloud_print::VirtualDriverInstallHelper::SetUpUninstall();
1169}
1170
[email protected]caadfca2010-01-22 21:51:211171- (void)application:(NSApplication*)sender
1172 openFiles:(NSArray*)filenames {
[email protected]57750f822009-04-21 21:43:091173 std::vector<GURL> gurlVector;
[email protected]caadfca2010-01-22 21:51:211174 for (NSString* file in filenames) {
1175 GURL gurl = net::FilePathToFileURL(FilePath(base::SysNSStringToUTF8(file)));
[email protected]57750f822009-04-21 21:43:091176 gurlVector.push_back(gurl);
1177 }
[email protected]caadfca2010-01-22 21:51:211178 if (!gurlVector.empty())
[email protected]d630d7d52010-02-21 00:55:111179 [self openUrls:gurlVector];
[email protected]caadfca2010-01-22 21:51:211180 else
1181 NOTREACHED() << "Nothing to open!";
[email protected]57750f822009-04-21 21:43:091182
[email protected]caadfca2010-01-22 21:51:211183 [sender replyToOpenOrPrint:NSApplicationDelegateReplySuccess];
[email protected]57750f822009-04-21 21:43:091184}
[email protected]3f34599d2009-03-25 22:11:431185
[email protected]3111f08b2009-04-30 16:01:521186// Show the preferences window, or bring it to the front if it's already
1187// visible.
1188- (IBAction)showPreferences:(id)sender {
[email protected]a585a8db2011-06-20 18:58:351189 if (Browser* browser = ActivateBrowser([self lastProfile])) {
[email protected]445177652011-03-09 02:04:051190 // Show options tab in the active browser window.
1191 browser->OpenOptionsDialog();
[email protected]4a42d272010-06-18 01:29:421192 } else {
[email protected]445177652011-03-09 02:04:051193 // No browser window, so create one for the options tab.
[email protected]a585a8db2011-06-20 18:58:351194 Browser::OpenOptionsWindow([self lastProfile]);
[email protected]4a42d272010-06-18 01:29:421195 }
[email protected]d989f07c2009-11-14 00:35:461196}
1197
[email protected]bde3dda2009-05-20 22:13:071198// Called when the about window is closed. We use this to release the
1199// window controller.
[email protected]d4e18652009-11-19 21:52:161200- (void)aboutWindowClosed:(NSNotification*)notification {
1201 NSWindow* window = [aboutController_ window];
[email protected]2ce9c89752011-02-25 18:24:341202 DCHECK_EQ([notification object], window);
[email protected]bde3dda2009-05-20 22:13:071203 [[NSNotificationCenter defaultCenter]
[email protected]49aeee52009-10-26 19:58:131204 removeObserver:self
[email protected]d4e18652009-11-19 21:52:161205 name:NSWindowWillCloseNotification
1206 object:window];
1207 // AboutWindowControllers are autoreleased in
1208 // -[AboutWindowController windowWillClose:].
1209 aboutController_ = nil;
[email protected]bde3dda2009-05-20 22:13:071210}
1211
1212- (IBAction)orderFrontStandardAboutPanel:(id)sender {
[email protected]bde3dda2009-05-20 22:13:071213 if (!aboutController_) {
[email protected]d4e18652009-11-19 21:52:161214 aboutController_ =
[email protected]a585a8db2011-06-20 18:58:351215 [[AboutWindowController alloc] initWithProfile:[self lastProfile]];
[email protected]49aeee52009-10-26 19:58:131216
[email protected]bde3dda2009-05-20 22:13:071217 // Watch for a notification of when it goes away so that we can destroy
1218 // the controller.
1219 [[NSNotificationCenter defaultCenter]
1220 addObserver:self
1221 selector:@selector(aboutWindowClosed:)
[email protected]d4e18652009-11-19 21:52:161222 name:NSWindowWillCloseNotification
1223 object:[aboutController_ window]];
[email protected]bde3dda2009-05-20 22:13:071224 }
[email protected]49aeee52009-10-26 19:58:131225
[email protected]bde3dda2009-05-20 22:13:071226 [aboutController_ showWindow:self];
[email protected]bde3dda2009-05-20 22:13:071227}
1228
[email protected]a99fce0e2011-03-21 20:58:481229- (IBAction)toggleConfirmToQuit:(id)sender {
[email protected]a585a8db2011-06-20 18:58:351230 PrefService* prefService = [self lastProfile]->GetPrefs();
[email protected]a99fce0e2011-03-21 20:58:481231 bool enabled = prefService->GetBoolean(prefs::kConfirmToQuitEnabled);
1232 prefService->SetBoolean(prefs::kConfirmToQuitEnabled, !enabled);
1233}
1234
[email protected]3d4bd23f2009-10-06 03:58:381235// Explicitly bring to the foreground when creating new windows from the dock.
[email protected]93b59fc2010-12-21 20:00:471236- (void)commandFromDock:(id)sender {
[email protected]3d4bd23f2009-10-06 03:58:381237 [NSApp activateIgnoringOtherApps:YES];
1238 [self commandDispatch:sender];
1239}
1240
[email protected]227fcee2010-06-11 19:19:371241- (NSMenu*)applicationDockMenu:(NSApplication*)sender {
[email protected]018a3962009-09-17 22:23:441242 NSMenu* dockMenu = [[[NSMenu alloc] initWithTitle: @""] autorelease];
[email protected]a585a8db2011-06-20 18:58:351243 Profile* profile = [self lastProfile];
[email protected]93b59fc2010-12-21 20:00:471244
[email protected]018a3962009-09-17 22:23:441245 NSString* titleStr = l10n_util::GetNSStringWithFixup(IDS_NEW_WINDOW_MAC);
[email protected]8c6b029d2011-01-21 22:39:511246 scoped_nsobject<NSMenuItem> item(
1247 [[NSMenuItem alloc] initWithTitle:titleStr
1248 action:@selector(commandFromDock:)
1249 keyEquivalent:@""]);
[email protected]288bfcd32009-09-14 18:14:461250 [item setTarget:self];
1251 [item setTag:IDC_NEW_WINDOW];
[email protected]018a3962009-09-17 22:23:441252 [dockMenu addItem:item];
[email protected]288bfcd32009-09-14 18:14:461253
1254 titleStr = l10n_util::GetNSStringWithFixup(IDS_NEW_INCOGNITO_WINDOW_MAC);
[email protected]018a3962009-09-17 22:23:441255 item.reset([[NSMenuItem alloc] initWithTitle:titleStr
[email protected]8c6b029d2011-01-21 22:39:511256 action:@selector(commandFromDock:)
[email protected]018a3962009-09-17 22:23:441257 keyEquivalent:@""]);
[email protected]288bfcd32009-09-14 18:14:461258 [item setTarget:self];
1259 [item setTag:IDC_NEW_INCOGNITO_WINDOW];
[email protected]018a3962009-09-17 22:23:441260 [dockMenu addItem:item];
[email protected]288bfcd32009-09-14 18:14:461261
[email protected]8c6b029d2011-01-21 22:39:511262 // TODO(rickcam): Mock out BackgroundApplicationListModel, then add unit
1263 // tests which use the mock in place of the profile-initialized model.
1264
1265 // Avoid breaking unit tests which have no profile.
1266 if (profile) {
1267 BackgroundApplicationListModel applications(profile);
1268 if (applications.size()) {
1269 int position = 0;
1270 NSString* menuStr =
1271 l10n_util::GetNSStringWithFixup(IDS_BACKGROUND_APPS_MAC);
1272 scoped_nsobject<NSMenu> appMenu([[NSMenu alloc] initWithTitle:menuStr]);
1273 for (ExtensionList::const_iterator cursor = applications.begin();
1274 cursor != applications.end();
1275 ++cursor, ++position) {
[email protected]2ce9c89752011-02-25 18:24:341276 DCHECK_EQ(applications.GetPosition(*cursor), position);
[email protected]8c6b029d2011-01-21 22:39:511277 NSString* itemStr =
1278 base::SysUTF16ToNSString(UTF8ToUTF16((*cursor)->name()));
1279 scoped_nsobject<NSMenuItem> appItem([[NSMenuItem alloc]
1280 initWithTitle:itemStr
1281 action:@selector(commandFromDock:)
1282 keyEquivalent:@""]);
1283 [appItem setTarget:self];
1284 [appItem setTag:position];
1285 [appMenu addItem:appItem];
1286 }
1287 scoped_nsobject<NSMenuItem> appMenuItem([[NSMenuItem alloc]
1288 initWithTitle:menuStr
1289 action:@selector(commandFromDock:)
1290 keyEquivalent:@""]);
1291 [appMenuItem setTarget:self];
1292 [appMenuItem setTag:position];
1293 [appMenuItem setSubmenu:appMenu];
1294 [dockMenu addItem:appMenuItem];
1295 }
1296 }
1297
[email protected]018a3962009-09-17 22:23:441298 return dockMenu;
[email protected]288bfcd32009-09-14 18:14:461299}
1300
[email protected]d630d7d52010-02-21 00:55:111301- (const std::vector<GURL>&)startupUrls {
1302 return startupUrls_;
[email protected]caadfca2010-01-22 21:51:211303}
1304
[email protected]d630d7d52010-02-21 00:55:111305- (void)clearStartupUrls {
1306 startupUrls_.clear();
[email protected]caadfca2010-01-22 21:51:211307}
1308
[email protected]7e4c69d02011-05-11 22:16:471309- (BookmarkMenuBridge*)bookmarkMenuBridge {
1310 return bookmarkMenuBridge_.get();
1311}
1312
[email protected]ebbe94b2012-01-18 20:11:561313- (void)applicationDidChangeScreenParameters:(NSNotification*)notification {
[email protected]6a455072011-11-02 01:32:281314 // During this callback the working area is not always already updated. Defer.
1315 [self performSelector:@selector(delayedPanelManagerScreenParametersUpdate)
1316 withObject:nil
1317 afterDelay:0];
1318}
1319
1320- (void)delayedPanelManagerScreenParametersUpdate {
1321 PanelManager::GetInstance()->OnDisplayChanged();
1322}
1323
[email protected]d630d7d52010-02-21 00:55:111324@end // @implementation AppController
[email protected]60ad3e22009-09-18 21:07:191325
1326//---------------------------------------------------------------------------
1327
[email protected]0934de82011-03-09 18:42:161328namespace browser {
1329
1330void ShowInstantConfirmDialog(gfx::NativeWindow parent, Profile* profile) {
1331 if (Browser* browser = ActivateBrowser(profile)) {
[email protected]b99081b2011-03-10 17:43:471332 browser->OpenInstantConfirmDialog();
[email protected]0934de82011-03-09 18:42:161333 } else {
[email protected]b99081b2011-03-10 17:43:471334 Browser::OpenInstantConfirmDialogWindow(profile);
[email protected]0934de82011-03-09 18:42:161335 }
1336}
1337
1338} // namespace browser
1339
[email protected]216e9042009-10-29 17:05:231340namespace app_controller_mac {
1341
1342bool IsOpeningNewWindow() {
1343 return g_is_opening_new_window;
1344}
1345
1346} // namespace app_controller_mac