blob: 780461cc3c0409ffbf6a15bd28100276c26582a2 [file] [log] [blame]
[email protected]c8de64a2011-01-25 17:10:231// Copyright (c) 2011 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]57750f822009-04-21 21:43:098#include "base/command_line.h"
[email protected]f805fe82010-08-03 22:47:109#include "base/file_path.h"
[email protected]151c4a62011-04-22 04:15:1310#include "base/mac/foundation_util.h"
[email protected]0378bf42011-01-01 18:20:1411#include "base/mac/mac_util.h"
[email protected]2c47bc12009-04-10 20:14:0012#include "base/message_loop.h"
[email protected]528c56d2010-07-30 19:28:4413#include "base/string_number_conversions.h"
[email protected]57750f822009-04-21 21:43:0914#include "base/sys_string_conversions.h"
[email protected]1a3aba82010-11-08 23:52:5415#include "chrome/app/chrome_command_ids.h"
[email protected]a07676b22011-06-17 16:36:5316#include "chrome/browser/background/background_application_list_model.h"
[email protected]5c238752009-06-13 10:29:0717#include "chrome/browser/browser_process.h"
[email protected]41741a962009-02-18 21:51:3918#include "chrome/browser/browser_shutdown.h"
[email protected]2c47bc12009-04-10 20:14:0019#include "chrome/browser/command_updater.h"
[email protected]09729a552009-08-03 23:21:4120#include "chrome/browser/download/download_manager.h"
[email protected]0934de82011-03-09 18:42:1621#include "chrome/browser/instant/instant_confirm_dialog.h"
[email protected]a99fce0e2011-03-21 20:58:4822#include "chrome/browser/prefs/pref_service.h"
[email protected]e7dd6d82010-10-07 23:21:1223#include "chrome/browser/printing/print_job_manager.h"
[email protected]8ecad5e2010-12-02 21:18:3324#include "chrome/browser/profiles/profile_manager.h"
[email protected]d2912a22011-03-15 15:20:5025#include "chrome/browser/sessions/session_service.h"
[email protected]92371eb2011-04-28 11:50:1526#include "chrome/browser/sessions/session_service_factory.h"
[email protected]fbc947b2009-06-19 13:28:2427#include "chrome/browser/sessions/tab_restore_service.h"
[email protected]92371eb2011-04-28 11:50:1528#include "chrome/browser/sessions/tab_restore_service_factory.h"
[email protected]f9bc9b92009-11-24 00:55:3529#include "chrome/browser/sync/profile_sync_service.h"
[email protected]40f047972009-11-25 03:54:4030#include "chrome/browser/sync/sync_ui_util.h"
31#include "chrome/browser/sync/sync_ui_util_mac.h"
[email protected]ae04f592010-11-18 20:41:3532#include "chrome/browser/ui/browser.h"
[email protected]f7002802010-11-12 19:50:2833#include "chrome/browser/ui/browser_init.h"
[email protected]ae04f592010-11-18 20:41:3534#include "chrome/browser/ui/browser_list.h"
35#include "chrome/browser/ui/browser_window.h"
[email protected]7d791652010-12-01 16:34:4936#import "chrome/browser/ui/cocoa/about_window_controller.h"
37#import "chrome/browser/ui/cocoa/bookmarks/bookmark_menu_bridge.h"
38#import "chrome/browser/ui/cocoa/browser_window_cocoa.h"
39#import "chrome/browser/ui/cocoa/browser_window_controller.h"
40#import "chrome/browser/ui/cocoa/bug_report_window_controller.h"
[email protected]7d791652010-12-01 16:34:4941#import "chrome/browser/ui/cocoa/confirm_quit_panel_controller.h"
42#import "chrome/browser/ui/cocoa/encoding_menu_controller_delegate_mac.h"
43#import "chrome/browser/ui/cocoa/history_menu_bridge.h"
[email protected]cfe1d612011-01-19 20:06:4744#import "chrome/browser/ui/cocoa/tabs/tab_strip_controller.h"
45#import "chrome/browser/ui/cocoa/tabs/tab_window_controller.h"
[email protected]7d791652010-12-01 16:34:4946#include "chrome/browser/ui/cocoa/task_manager_mac.h"
[email protected]646b9b92010-04-30 21:51:5847#include "chrome/common/app_mode_common_mac.h"
[email protected]12dc3d42010-02-22 23:37:1248#include "chrome/common/chrome_paths_internal.h"
[email protected]f4acfae82009-09-11 00:33:3849#include "chrome/common/chrome_switches.h"
[email protected]a99fce0e2011-03-21 20:58:4850#include "chrome/common/pref_names.h"
[email protected]024617a2010-08-20 05:08:0551#include "chrome/common/url_constants.h"
[email protected]7324d1d02011-03-01 05:02:1652#include "content/browser/browser_thread.h"
53#include "content/browser/tab_contents/tab_contents.h"
[email protected]afd1e522011-04-27 23:29:5954#include "content/browser/user_metrics.h"
[email protected]f20d7332011-03-08 21:11:5355#include "content/common/notification_service.h"
[email protected]9dcb59662009-09-23 01:15:5056#include "grit/chromium_strings.h"
[email protected]09729a552009-08-03 23:21:4157#include "grit/generated_resources.h"
[email protected]caadfca2010-01-22 21:51:2158#include "net/base/net_util.h"
[email protected]c051a1b2011-01-21 23:30:1759#include "ui/base/l10n/l10n_util.h"
60#include "ui/base/l10n/l10n_util_mac.h"
[email protected]ddd653f42011-03-02 15:14:3861#include "ui/base/models/accelerator_cocoa.h"
[email protected]88d74942009-01-21 22:04:4462
[email protected]f2c20fa2009-12-01 17:42:0263// 10.6 adds a public API for the Spotlight-backed search menu item in the Help
64// menu. Provide the declaration so it can be called below when building with
65// the 10.5 SDK.
66#if !defined(MAC_OS_X_VERSION_10_6) || \
67 MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_6
68@interface NSApplication (SnowLeopardSDKDeclarations)
[email protected]caadfca2010-01-22 21:51:2169- (void)setHelpMenu:(NSMenu*)helpMenu;
[email protected]f2c20fa2009-12-01 17:42:0270@end
71#endif
72
[email protected]d630d7d52010-02-21 00:55:1173namespace {
74
75// True while AppController is calling Browser::OpenEmptyWindow(). We need a
76// global flag here, analogue to BrowserInit::InProcessStartup() because
77// otherwise the SessionService will try to restore sessions when we make a new
78// window while there are no other active windows.
79bool g_is_opening_new_window = false;
80
81// Activates a browser window having the given profile (the last one active) if
82// possible and returns a pointer to the activate |Browser| or NULL if this was
83// not possible. If the last active browser is minimized (in particular, if
84// there are only minimized windows), it will unminimize it.
85Browser* ActivateBrowser(Profile* profile) {
86 Browser* browser = BrowserList::GetLastActiveWithProfile(profile);
87 if (browser)
88 browser->window()->Activate();
89 return browser;
90}
91
92// Creates an empty browser window with the given profile and returns a pointer
93// to the new |Browser|.
94Browser* CreateBrowser(Profile* profile) {
95 {
[email protected]0fbd70332010-06-01 19:28:3496 AutoReset<bool> auto_reset_in_run(&g_is_opening_new_window, true);
[email protected]d630d7d52010-02-21 00:55:1197 Browser::OpenEmptyWindow(profile);
98 }
99
100 Browser* browser = BrowserList::GetLastActive();
101 CHECK(browser);
102 return browser;
103}
104
105// Activates a browser window having the given profile (the last one active) if
106// possible or creates an empty one if necessary. Returns a pointer to the
107// activated/new |Browser|.
108Browser* ActivateOrCreateBrowser(Profile* profile) {
109 if (Browser* browser = ActivateBrowser(profile))
110 return browser;
111 return CreateBrowser(profile);
112}
113
[email protected]151c4a62011-04-22 04:15:13114CFStringRef BaseBundleID_CFString() {
115 NSString* base_bundle_id =
116 [NSString stringWithUTF8String:base::mac::BaseBundleID()];
117 return base::mac::NSToCFCast(base_bundle_id);
118}
119
[email protected]12dc3d42010-02-22 23:37:12120// This task synchronizes preferences (under "org.chromium.Chromium" or
121// "com.google.Chrome"), in particular, writes them out to disk.
122class PrefsSyncTask : public Task {
123 public:
124 PrefsSyncTask() {}
125 virtual ~PrefsSyncTask() {}
126 virtual void Run() {
[email protected]151c4a62011-04-22 04:15:13127 if (!CFPreferencesAppSynchronize(BaseBundleID_CFString()))
[email protected]12dc3d42010-02-22 23:37:12128 LOG(WARNING) << "Error recording application bundle path.";
129 }
130};
131
132// Record the location of the application bundle (containing the main framework)
133// from which Chromium was loaded. This is used by app mode shims to find
134// Chromium.
135void RecordLastRunAppBundlePath() {
136 // Going up three levels from |chrome::GetVersionedDirectory()| gives the
137 // real, user-visible app bundle directory. (The alternatives give either the
138 // framework's path or the initial app's path, which may be an app mode shim
139 // or a unit test.)
140 FilePath appBundlePath =
141 chrome::GetVersionedDirectory().DirName().DirName().DirName();
142 CFPreferencesSetAppValue(app_mode::kLastRunAppBundlePathPrefsKey,
143 base::SysUTF8ToCFStringRef(appBundlePath.value()),
[email protected]151c4a62011-04-22 04:15:13144 BaseBundleID_CFString());
[email protected]12dc3d42010-02-22 23:37:12145
146 // Sync after a delay avoid I/O contention on startup; 1500 ms is plenty.
[email protected]d04e7662010-10-10 22:24:48147 BrowserThread::PostDelayedTask(BrowserThread::FILE, FROM_HERE,
148 new PrefsSyncTask(), 1500);
[email protected]12dc3d42010-02-22 23:37:12149}
150
[email protected]d630d7d52010-02-21 00:55:11151} // anonymous namespace
152
[email protected]a99fce0e2011-03-21 20:58:48153@interface AppController (Private)
[email protected]88d74942009-01-21 22:04:44154- (void)initMenuState;
[email protected]a99fce0e2011-03-21 20:58:48155- (void)updateConfirmToQuitPrefMenuItem:(NSMenuItem*)item;
[email protected]eef99c22010-08-17 05:55:16156- (void)registerServicesMenuTypesTo:(NSApplication*)app;
[email protected]d630d7d52010-02-21 00:55:11157- (void)openUrls:(const std::vector<GURL>&)urls;
[email protected]57750f822009-04-21 21:43:09158- (void)getUrl:(NSAppleEventDescriptor*)event
159 withReply:(NSAppleEventDescriptor*)reply;
[email protected]449dd2f2009-05-27 13:04:00160- (void)windowLayeringDidChange:(NSNotification*)inNotification;
[email protected]5c593702011-06-17 18:35:24161- (void)windowChangedToProfile:(Profile*)profile;
[email protected]dd6ab1282010-07-20 01:07:32162- (void)checkForAnyKeyWindows;
[email protected]73f5b362009-08-10 23:58:21163- (BOOL)userWillWaitForInProgressDownloads:(int)downloadCount;
[email protected]09729a552009-08-03 23:21:41164- (BOOL)shouldQuitWithInProgressDownloads;
[email protected]93b59fc2010-12-21 20:00:47165- (void)executeApplication:(id)sender;
[email protected]88d74942009-01-21 22:04:44166@end
[email protected]1bcdb532009-01-16 17:47:57167
168@implementation AppController
169
[email protected]caadfca2010-01-22 21:51:21170@synthesize startupComplete = startupComplete_;
171
[email protected]cd63ef62009-05-06 19:41:37172// This method is called very early in application startup (ie, before
173// the profile is loaded or any preferences have been registered). Defer any
174// user-data initialization until -applicationDidFinishLaunching:.
[email protected]88d74942009-01-21 22:04:44175- (void)awakeFromNib {
[email protected]136140c2009-05-19 13:58:25176 // We need to register the handlers early to catch events fired on launch.
[email protected]57750f822009-04-21 21:43:09177 NSAppleEventManager* em = [NSAppleEventManager sharedAppleEventManager];
178 [em setEventHandler:self
179 andSelector:@selector(getUrl:withReply:)
180 forEventClass:kInternetEventClass
181 andEventID:kAEGetURL];
182 [em setEventHandler:self
183 andSelector:@selector(getUrl:withReply:)
184 forEventClass:'WWW!' // A particularly ancient AppleEvent that dates
185 andEventID:'OURL']; // back to the Spyglass days.
[email protected]cd63ef62009-05-06 19:41:37186
[email protected]449dd2f2009-05-27 13:04:00187 // Register for various window layering changes. We use these to update
188 // various UI elements (command-key equivalents, etc) when the frontmost
189 // window changes.
190 NSNotificationCenter* notificationCenter =
191 [NSNotificationCenter defaultCenter];
[email protected]ce560f82009-06-03 09:39:44192 [notificationCenter
[email protected]449dd2f2009-05-27 13:04:00193 addObserver:self
194 selector:@selector(windowLayeringDidChange:)
195 name:NSWindowDidBecomeKeyNotification
196 object:nil];
197 [notificationCenter
198 addObserver:self
199 selector:@selector(windowLayeringDidChange:)
200 name:NSWindowDidResignKeyNotification
201 object:nil];
202 [notificationCenter
203 addObserver:self
204 selector:@selector(windowLayeringDidChange:)
205 name:NSWindowDidBecomeMainNotification
206 object:nil];
207 [notificationCenter
208 addObserver:self
209 selector:@selector(windowLayeringDidChange:)
210 name:NSWindowDidResignMainNotification
211 object:nil];
212
[email protected]136140c2009-05-19 13:58:25213 // Set up the command updater for when there are no windows open
214 [self initMenuState];
[email protected]f4acfae82009-09-11 00:33:38215
216 // Activate (bring to foreground) if asked to do so. On
217 // Windows this logic isn't necessary since
218 // BrowserWindow::Activate() calls ::SetForegroundWindow() which is
219 // adequate. On Mac, BrowserWindow::Activate() calls -[NSWindow
220 // makeKeyAndOrderFront:] which does not activate the application
221 // itself.
222 const CommandLine& parsed_command_line = *CommandLine::ForCurrentProcess();
223 if (parsed_command_line.HasSwitch(switches::kActivateOnLaunch)) {
224 [NSApp activateIgnoringOtherApps:YES];
225 }
[email protected]136140c2009-05-19 13:58:25226}
227
[email protected]7dc8c6b2010-04-09 17:02:50228// (NSApplicationDelegate protocol) This is the Apple-approved place to override
229// the default handlers.
230- (void)applicationWillFinishLaunching:(NSNotification*)notification {
[email protected]58877932010-05-25 05:43:11231 // Nothing here right now.
[email protected]7dc8c6b2010-04-09 17:02:50232}
233
234- (BOOL)tryToTerminateApplication:(NSApplication*)app {
[email protected]3076fad2010-04-28 18:11:45235 // Check for in-process downloads, and prompt the user if they really want
236 // to quit (and thus cancel downloads). Only check if we're not already
237 // shutting down, else the user might be prompted multiple times if the
238 // download isn't stopped before terminate is called again.
239 if (!browser_shutdown::IsTryingToQuit() &&
240 ![self shouldQuitWithInProgressDownloads])
241 return NO;
242
[email protected]7dc8c6b2010-04-09 17:02:50243 // TODO(viettrungluu): Remove Apple Event handlers here? (It's safe to leave
244 // them in, but I'm not sure about UX; we'd also want to disable other things
245 // though.) http://crbug.com/40861
246
[email protected]177aceb2010-11-03 16:17:41247 // Check if the user really wants to quit by employing the confirm-to-quit
248 // mechanism.
249 if (!browser_shutdown::IsTryingToQuit() &&
250 [self applicationShouldTerminate:app] != NSTerminateNow)
251 return NO;
252
[email protected]c984d9f2010-07-20 20:52:20253 size_t num_browsers = BrowserList::size();
[email protected]7dc8c6b2010-04-09 17:02:50254
[email protected]e7dd6d82010-10-07 23:21:12255 // Give any print jobs in progress time to finish.
256 if (!browser_shutdown::IsTryingToQuit())
257 g_browser_process->print_job_manager()->StopJobs(true);
258
[email protected]c984d9f2010-07-20 20:52:20259 // Initiate a shutdown (via BrowserList::CloseAllBrowsers()) if we aren't
260 // already shutting down.
261 if (!browser_shutdown::IsTryingToQuit())
[email protected]c6032e82010-09-13 20:06:05262 BrowserList::CloseAllBrowsers();
[email protected]7dc8c6b2010-04-09 17:02:50263
[email protected]c984d9f2010-07-20 20:52:20264 return num_browsers == 0 ? YES : NO;
[email protected]7dc8c6b2010-04-09 17:02:50265}
266
267- (void)stopTryingToTerminateApplication:(NSApplication*)app {
268 if (browser_shutdown::IsTryingToQuit()) {
269 // Reset the "trying to quit" state, so that closing all browser windows
270 // will no longer lead to termination.
271 browser_shutdown::SetTryingToQuit(false);
272
273 // TODO(viettrungluu): Were we to remove Apple Event handlers above, we
274 // would have to reinstall them here. http://crbug.com/40861
275 }
[email protected]a9e8afc2009-08-11 22:03:17276}
277
[email protected]177aceb2010-11-03 16:17:41278- (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication*)app {
[email protected]a99fce0e2011-03-21 20:58:48279 // Check if the preference is turned on.
[email protected]a585a8db2011-06-20 18:58:35280 const PrefService* prefs = [self lastProfile]->GetPrefs();
[email protected]1eaddf32011-04-05 15:09:23281 if (!prefs->GetBoolean(prefs::kConfirmToQuitEnabled)) {
282 confirm_quit::RecordHistogram(confirm_quit::kNoConfirm);
[email protected]177aceb2010-11-03 16:17:41283 return NSTerminateNow;
[email protected]1eaddf32011-04-05 15:09:23284 }
[email protected]177aceb2010-11-03 16:17:41285
286 // If the application is going to terminate as the result of a Cmd+Q
287 // invocation, use the special sauce to prevent accidental quitting.
288 // http://dev.chromium.org/developers/design-documents/confirm-to-quit-experiment
[email protected]177aceb2010-11-03 16:17:41289
[email protected]2020fd0b2010-12-10 01:16:50290 // This logic is only for keyboard-initiated quits.
[email protected]5df54fb2011-03-07 18:15:26291 if (![ConfirmQuitPanelController eventTriggersFeature:[app currentEvent]])
[email protected]2020fd0b2010-12-10 01:16:50292 return NSTerminateNow;
[email protected]177aceb2010-11-03 16:17:41293
[email protected]5df54fb2011-03-07 18:15:26294 return [[ConfirmQuitPanelController sharedController]
295 runModalLoopForApplication:app];
[email protected]177aceb2010-11-03 16:17:41296}
297
[email protected]449dd2f2009-05-27 13:04:00298// Called when the app is shutting down. Clean-up as appropriate.
[email protected]7dc8c6b2010-04-09 17:02:50299- (void)applicationWillTerminate:(NSNotification*)aNotification {
[email protected]3b6aa8b62009-09-15 21:36:11300 NSAppleEventManager* em = [NSAppleEventManager sharedAppleEventManager];
301 [em removeEventHandlerForEventClass:kInternetEventClass
302 andEventID:kAEGetURL];
303 [em removeEventHandlerForEventClass:'WWW!'
304 andEventID:'OURL'];
[email protected]3b6aa8b62009-09-15 21:36:11305
[email protected]7dc8c6b2010-04-09 17:02:50306 // There better be no browser windows left at this point.
307 CHECK_EQ(BrowserList::size(), 0u);
[email protected]3b6aa8b62009-09-15 21:36:11308
[email protected]c984d9f2010-07-20 20:52:20309 // Tell BrowserList not to keep the browser process alive. Once all the
310 // browsers get dealloc'd, it will stop the RunLoop and fall back into main().
311 BrowserList::EndKeepAlive();
[email protected]fbc947b2009-06-19 13:28:24312
[email protected]d4e18652009-11-19 21:52:16313 // Close these off if they have open windows.
[email protected]d4e18652009-11-19 21:52:16314 [aboutController_ close];
315
[email protected]449dd2f2009-05-27 13:04:00316 [[NSNotificationCenter defaultCenter] removeObserver:self];
317}
318
[email protected]3b6aa8b62009-09-15 21:36:11319- (void)didEndMainMessageLoop {
[email protected]a585a8db2011-06-20 18:58:35320 DCHECK(!BrowserList::HasBrowserWithProfile([self lastProfile]));
321 if (!BrowserList::HasBrowserWithProfile([self lastProfile])) {
[email protected]3b6aa8b62009-09-15 21:36:11322 // As we're shutting down, we need to nuke the TabRestoreService, which
323 // will start the shutdown of the NavigationControllers and allow for
324 // proper shutdown. If we don't do this, Chrome won't shut down cleanly,
325 // and may end up crashing when some thread tries to use the IO thread (or
326 // another thread) that is no longer valid.
[email protected]a585a8db2011-06-20 18:58:35327 TabRestoreServiceFactory::ResetForProfile([self lastProfile]);
[email protected]3b6aa8b62009-09-15 21:36:11328 }
329}
330
[email protected]449dd2f2009-05-27 13:04:00331// Helper routine to get the window controller if the key window is a tabbed
332// window, or nil if not. Examples of non-tabbed windows are "about" or
333// "preferences".
334- (TabWindowController*)keyWindowTabController {
335 NSWindowController* keyWindowController =
[email protected]5dbabd02009-09-17 20:44:13336 [[NSApp keyWindow] windowController];
[email protected]449dd2f2009-05-27 13:04:00337 if ([keyWindowController isKindOfClass:[TabWindowController class]])
338 return (TabWindowController*)keyWindowController;
339
340 return nil;
341}
342
[email protected]74c0c642009-09-22 21:25:50343// Helper routine to get the window controller if the main window is a tabbed
344// window, or nil if not. Examples of non-tabbed windows are "about" or
345// "preferences".
346- (TabWindowController*)mainWindowTabController {
347 NSWindowController* mainWindowController =
348 [[NSApp mainWindow] windowController];
349 if ([mainWindowController isKindOfClass:[TabWindowController class]])
350 return (TabWindowController*)mainWindowController;
351
352 return nil;
353}
354
[email protected]8cfbdbd2011-06-16 04:17:17355// If the window has a tab controller, make "close window" be cmd-shift-w,
356// otherwise leave it as the normal cmd-w. Capitalization of the key equivalent
357// affects whether the shift modifer is used.
358- (void)adjustCloseWindowMenuItemKeyEquivalent:(BOOL)hasTabs {
359 [closeWindowMenuItem_ setKeyEquivalent:(hasTabs ? @"W" : @"w")];
360 [closeWindowMenuItem_ setKeyEquivalentModifierMask:NSCommandKeyMask];
361}
362
363// If the window has a tab controller, make "close tab" take over cmd-w,
364// otherwise it shouldn't have any key-equivalent because it should be disabled.
365- (void)adjustCloseTabMenuItemKeyEquivalent:(BOOL)hasTabs {
366 if (hasTabs) {
367 [closeTabMenuItem_ setKeyEquivalent:@"w"];
368 [closeTabMenuItem_ setKeyEquivalentModifierMask:NSCommandKeyMask];
369 } else {
370 [closeTabMenuItem_ setKeyEquivalent:@""];
371 [closeTabMenuItem_ setKeyEquivalentModifierMask:0];
372 }
373}
374
375// Explicitly remove any command-key equivalents from the close tab/window
376// menus so that nothing can go haywire if we get a user action during pending
377// updates.
378- (void)clearCloseMenuItemKeyEquivalents {
379 [closeTabMenuItem_ setKeyEquivalent:@""];
380 [closeTabMenuItem_ setKeyEquivalentModifierMask:0];
381 [closeWindowMenuItem_ setKeyEquivalent:@""];
382 [closeWindowMenuItem_ setKeyEquivalentModifierMask:0];
383}
384
385// See if we have a window with tabs open, and adjust the key equivalents for
386// Close Tab/Close Window accordingly.
387- (void)fixCloseMenuItemKeyEquivalents:(NSWindow*)window {
388 fileMenuUpdatePending_ = NO;
389 TabWindowController* tabController = [self keyWindowTabController];
390 if (!tabController && ![NSApp keyWindow]) {
391 // There might be a small amount of time where there is no key window,
392 // so just use our main browser window if there is one.
393 tabController = [self mainWindowTabController];
394 }
395 BOOL hasTabs = !!tabController;
396
397 [self adjustCloseWindowMenuItemKeyEquivalent:hasTabs];
398 [self adjustCloseTabMenuItemKeyEquivalent:hasTabs];
399}
400
401// Fix up the "close tab/close window" command-key equivalents. We do this
402// after a delay to ensure that window layer state has been set by the time
403// we do the enabling. This should only be called on the main thread, code that
404// calls this (even as a side-effect) from other threads needs to be fixed.
405- (void)delayedFixCloseMenuItemKeyEquivalents:(NSNotification*)notify {
406 DCHECK([NSThread isMainThread]);
407 if (!fileMenuUpdatePending_) {
408 // The OS prefers keypresses to timers, so it's possible that a cmd-w
409 // can sneak in before this timer fires. In order to prevent that from
410 // having any bad consequences, just clear the keys combos altogether. They
411 // will be reset when the timer eventually fires.
412 if ([NSThread isMainThread]) {
413 fileMenuUpdatePending_ = YES;
414 [self clearCloseMenuItemKeyEquivalents];
415 [self performSelector:@selector(fixCloseMenuItemKeyEquivalents:)
416 withObject:[notify object]
417 afterDelay:0];
418 } else {
419 // This shouldn't be happening, but if it does, force it to the main
420 // thread to avoid dropping the update. Don't mess with
421 // |fileMenuUpdatePending_| as it's not expected to be threadsafe and
422 // there could be a race between the selector finishing and setting the
423 // flag.
424 [self
425 performSelectorOnMainThread:@selector(fixCloseMenuItemKeyEquivalents:)
426 withObject:[notify object]
427 waitUntilDone:NO];
428 }
429 }
430}
431
[email protected]449dd2f2009-05-27 13:04:00432// Called when we get a notification about the window layering changing to
433// update the UI based on the new main window.
434- (void)windowLayeringDidChange:(NSNotification*)notify {
[email protected]8cfbdbd2011-06-16 04:17:17435 [self delayedFixCloseMenuItemKeyEquivalents:notify];
436
[email protected]dd6ab1282010-07-20 01:07:32437 if ([notify name] == NSWindowDidResignKeyNotification) {
438 // If a window is closed, this notification is fired but |[NSApp keyWindow]|
439 // returns nil regardless of whether any suitable candidates for the key
440 // window remain. It seems that the new key window for the app is not set
441 // until after this notification is fired, so a check is performed after the
442 // run loop is allowed to spin.
443 [self performSelector:@selector(checkForAnyKeyWindows)
444 withObject:nil
445 afterDelay:0.0];
446 }
[email protected]5c593702011-06-17 18:35:24447
448 // If the window changed to a new BrowserWindowController, update the profile.
449 id windowController = [[notify object] windowController];
450 if ([windowController isKindOfClass:[BrowserWindowController class]]) {
451 // If the profile is incognito, use the original profile.
452 Profile* newProfile = [windowController profile]->GetOriginalProfile();
453 [self windowChangedToProfile:newProfile];
454 }
455}
456
457// Called when the user has changed browser windows, meaning the backing profile
458// may have changed. This can cause a rebuild of the user-data menus. This is a
459// no-op if the new profile is the same as the current one. This will always be
460// the original profile and never incognito.
461- (void)windowChangedToProfile:(Profile*)profile {
462 if (lastProfile_ == profile)
463 return;
464
465 // Before tearing down the menu controller bridges, return the Cocoa menus to
466 // their initial state.
467 if (bookmarkMenuBridge_.get())
468 bookmarkMenuBridge_->ResetMenu();
469 if (historyMenuBridge_.get())
470 historyMenuBridge_->ResetMenu();
471
472 // Rebuild the menus with the new profile.
473 lastProfile_ = profile;
474
475 bookmarkMenuBridge_.reset(new BookmarkMenuBridge(lastProfile_));
476 bookmarkMenuBridge_->BuildMenu();
477
478 historyMenuBridge_.reset(new HistoryMenuBridge(lastProfile_));
479 historyMenuBridge_->BuildMenu();
[email protected]dd6ab1282010-07-20 01:07:32480}
481
482- (void)checkForAnyKeyWindows {
483 if ([NSApp keyWindow])
484 return;
485
486 NotificationService::current()->Notify(
487 NotificationType::NO_KEY_WINDOW,
488 NotificationService::AllSources(),
489 NotificationService::NoDetails());
[email protected]449dd2f2009-05-27 13:04:00490}
491
[email protected]414fde592009-05-21 16:14:43492// If the auto-update interval is not set, make it 5 hours.
493// This code is specific to Mac Chrome Dev Channel.
494// Placed here for 2 reasons:
495// 1) Same spot as other Pref stuff
496// 2) Try and be friendly by keeping this after app launch
497// TODO(jrg): remove once we go Beta.
498- (void)setUpdateCheckInterval {
499#if defined(GOOGLE_CHROME_BUILD)
500 CFStringRef app = (CFStringRef)@"com.google.Keystone.Agent";
501 CFStringRef checkInterval = (CFStringRef)@"checkInterval";
502 CFPropertyListRef plist = CFPreferencesCopyAppValue(checkInterval, app);
503 if (!plist) {
504 const float fiveHoursInSeconds = 5.0 * 60.0 * 60.0;
[email protected]7dc8c6b2010-04-09 17:02:50505 NSNumber* value = [NSNumber numberWithFloat:fiveHoursInSeconds];
[email protected]414fde592009-05-21 16:14:43506 CFPreferencesSetAppValue(checkInterval, value, app);
507 CFPreferencesAppSynchronize(app);
508 }
509#endif
510}
511
[email protected]136140c2009-05-19 13:58:25512// This is called after profiles have been loaded and preferences registered.
513// It is safe to access the default profile here.
514- (void)applicationDidFinishLaunching:(NSNotification*)notify {
[email protected]c984d9f2010-07-20 20:52:20515 // Notify BrowserList to keep the application running so it doesn't go away
516 // when all the browser windows get closed.
517 BrowserList::StartKeepAlive();
[email protected]136140c2009-05-19 13:58:25518
[email protected]414fde592009-05-21 16:14:43519 [self setUpdateCheckInterval];
[email protected]2bcec612009-05-14 17:50:53520
521 // Build up the encoding menu, the order of the items differs based on the
522 // current locale (see http://crbug.com/7647 for details).
523 // We need a valid g_browser_process to get the profile which is why we can't
524 // call this from awakeFromNib.
[email protected]6f91e5ae2011-03-21 19:53:14525 NSMenu* viewMenu = [[[NSApp mainMenu] itemWithTag:IDC_VIEW_MENU] submenu];
526 NSMenuItem* encodingMenuItem = [viewMenu itemWithTag:IDC_ENCODING_MENU];
527 NSMenu* encodingMenu = [encodingMenuItem submenu];
[email protected]a585a8db2011-06-20 18:58:35528 EncodingMenuControllerDelegate::BuildEncodingMenu([self lastProfile],
[email protected]6f91e5ae2011-03-21 19:53:14529 encodingMenu);
[email protected]2bcec612009-05-14 17:50:53530
[email protected]f2c20fa2009-12-01 17:42:02531 // Since Chrome is localized to more languages than the OS, tell Cocoa which
532 // menu is the Help so it can add the search item to it.
[email protected]caadfca2010-01-22 21:51:21533 if (helpMenu_ && [NSApp respondsToSelector:@selector(setHelpMenu:)])
[email protected]f2c20fa2009-12-01 17:42:02534 [NSApp setHelpMenu:helpMenu_];
[email protected]f2c20fa2009-12-01 17:42:02535
[email protected]12dc3d42010-02-22 23:37:12536 // Record the path to the (browser) app bundle; this is used by the app mode
537 // shim.
538 RecordLastRunAppBundlePath();
539
[email protected]eef99c22010-08-17 05:55:16540 // Makes "Services" menu items available.
541 [self registerServicesMenuTypesTo:[notify object]];
542
[email protected]caadfca2010-01-22 21:51:21543 startupComplete_ = YES;
544
545 // TODO(viettrungluu): This is very temporary, since this should be done "in"
546 // |BrowserMain()|, i.e., this list of startup URLs should be appended to the
547 // (probably-empty) list of URLs from the command line.
[email protected]d630d7d52010-02-21 00:55:11548 if (startupUrls_.size()) {
549 [self openUrls:startupUrls_];
550 [self clearStartupUrls];
[email protected]caadfca2010-01-22 21:51:21551 }
[email protected]aac169d2011-03-18 19:53:03552
553 const CommandLine& parsed_command_line = *CommandLine::ForCurrentProcess();
554 if (!parsed_command_line.HasSwitch(switches::kEnableExposeForTabs)) {
555 [tabposeMenuItem_ setHidden:YES];
556 }
[email protected]7c321082009-02-09 15:35:47557}
558
[email protected]a96ec6a2009-11-04 17:27:08559// This is called after profiles have been loaded and preferences registered.
560// It is safe to access the default profile here.
561- (void)applicationDidBecomeActive:(NSNotification*)notify {
562 NotificationService::current()->Notify(NotificationType::APP_ACTIVATED,
563 NotificationService::AllSources(),
564 NotificationService::NoDetails());
565}
566
[email protected]73f5b362009-08-10 23:58:21567// Helper function for populating and displaying the in progress downloads at
568// exit alert panel.
569- (BOOL)userWillWaitForInProgressDownloads:(int)downloadCount {
[email protected]e8b5f7882009-09-14 20:59:13570 NSString* warningText = nil;
571 NSString* explanationText = nil;
[email protected]73f5b362009-08-10 23:58:21572 NSString* waitTitle = nil;
573 NSString* exitTitle = nil;
574
[email protected]757127052010-04-27 14:42:26575 string16 product_name = l10n_util::GetStringUTF16(IDS_PRODUCT_NAME);
[email protected]9dcb59662009-09-23 01:15:50576
[email protected]73f5b362009-08-10 23:58:21577 // Set the dialog text based on whether or not there are multiple downloads.
578 if (downloadCount == 1) {
[email protected]e8b5f7882009-09-14 20:59:13579 // Dialog text: warning and explanation.
[email protected]757127052010-04-27 14:42:26580 warningText = l10n_util::GetNSStringF(
581 IDS_SINGLE_DOWNLOAD_REMOVE_CONFIRM_WARNING, product_name);
582 explanationText = l10n_util::GetNSStringF(
583 IDS_SINGLE_DOWNLOAD_REMOVE_CONFIRM_EXPLANATION, product_name);
[email protected]73f5b362009-08-10 23:58:21584
585 // Cancel download and exit button text.
[email protected]757127052010-04-27 14:42:26586 exitTitle = l10n_util::GetNSString(
587 IDS_SINGLE_DOWNLOAD_REMOVE_CONFIRM_OK_BUTTON_LABEL);
[email protected]73f5b362009-08-10 23:58:21588
589 // Wait for download button text.
[email protected]757127052010-04-27 14:42:26590 waitTitle = l10n_util::GetNSString(
591 IDS_SINGLE_DOWNLOAD_REMOVE_CONFIRM_CANCEL_BUTTON_LABEL);
[email protected]73f5b362009-08-10 23:58:21592 } else {
[email protected]e8b5f7882009-09-14 20:59:13593 // Dialog text: warning and explanation.
[email protected]757127052010-04-27 14:42:26594 warningText = l10n_util::GetNSStringF(
595 IDS_MULTIPLE_DOWNLOADS_REMOVE_CONFIRM_WARNING, product_name,
[email protected]528c56d2010-07-30 19:28:44596 base::IntToString16(downloadCount));
[email protected]757127052010-04-27 14:42:26597 explanationText = l10n_util::GetNSStringF(
598 IDS_MULTIPLE_DOWNLOADS_REMOVE_CONFIRM_EXPLANATION, product_name);
[email protected]73f5b362009-08-10 23:58:21599
600 // Cancel downloads and exit button text.
[email protected]757127052010-04-27 14:42:26601 exitTitle = l10n_util::GetNSString(
602 IDS_MULTIPLE_DOWNLOADS_REMOVE_CONFIRM_OK_BUTTON_LABEL);
[email protected]73f5b362009-08-10 23:58:21603
604 // Wait for downloads button text.
[email protected]757127052010-04-27 14:42:26605 waitTitle = l10n_util::GetNSString(
606 IDS_MULTIPLE_DOWNLOADS_REMOVE_CONFIRM_CANCEL_BUTTON_LABEL);
[email protected]73f5b362009-08-10 23:58:21607 }
608
609 // 'waitButton' is the default choice.
[email protected]e8b5f7882009-09-14 20:59:13610 int choice = NSRunAlertPanel(warningText, explanationText,
611 waitTitle, exitTitle, nil);
[email protected]73f5b362009-08-10 23:58:21612 return choice == NSAlertDefaultReturn ? YES : NO;
613}
614
615// Check all profiles for in progress downloads, and if we find any, prompt the
[email protected]09729a552009-08-03 23:21:41616// user to see if we should continue to exit (and thus cancel the downloads), or
617// if we should wait.
618- (BOOL)shouldQuitWithInProgressDownloads {
[email protected]73f5b362009-08-10 23:58:21619 ProfileManager* profile_manager = g_browser_process->profile_manager();
620 if (!profile_manager)
621 return YES;
[email protected]09729a552009-08-03 23:21:41622
[email protected]844a1002011-04-19 11:37:21623 std::vector<Profile*> profiles(profile_manager->GetLoadedProfiles());
624 for (size_t i = 0; i < profiles.size(); ++i) {
625 DownloadManager* download_manager = profiles[i]->GetDownloadManager();
[email protected]73f5b362009-08-10 23:58:21626 if (download_manager && download_manager->in_progress_count() > 0) {
627 int downloadCount = download_manager->in_progress_count();
628 if ([self userWillWaitForInProgressDownloads:downloadCount]) {
629 // Create a new browser window (if necessary) and navigate to the
630 // downloads page if the user chooses to wait.
[email protected]844a1002011-04-19 11:37:21631 Browser* browser = BrowserList::FindBrowserWithProfile(profiles[i]);
[email protected]73f5b362009-08-10 23:58:21632 if (!browser) {
[email protected]844a1002011-04-19 11:37:21633 browser = Browser::Create(profiles[i]);
[email protected]73f5b362009-08-10 23:58:21634 browser->window()->Show();
635 }
636 DCHECK(browser);
637 browser->ShowDownloadsTab();
638 return NO;
639 }
[email protected]09729a552009-08-03 23:21:41640
[email protected]73f5b362009-08-10 23:58:21641 // User wants to exit.
642 return YES;
[email protected]09729a552009-08-03 23:21:41643 }
[email protected]09729a552009-08-03 23:21:41644 }
645
[email protected]73f5b362009-08-10 23:58:21646 // No profiles or active downloads found, okay to exit.
[email protected]09729a552009-08-03 23:21:41647 return YES;
648}
649
[email protected]fbc947b2009-06-19 13:28:24650// Called to determine if we should enable the "restore tab" menu item.
651// Checks with the TabRestoreService to see if there's anything there to
652// restore and returns YES if so.
653- (BOOL)canRestoreTab {
[email protected]92371eb2011-04-28 11:50:15654 TabRestoreService* service =
[email protected]a585a8db2011-06-20 18:58:35655 TabRestoreServiceFactory::GetForProfile([self lastProfile]);
[email protected]fbc947b2009-06-19 13:28:24656 return service && !service->entries().empty();
657}
658
[email protected]f6c15fa2010-06-04 20:08:21659// Returns true if there is not a modal window (either window- or application-
660// modal) blocking the active browser. Note that tab modal dialogs (HTTP auth
661// sheets) will not count as blocking the browser. But things like open/save
662// dialogs that are window modal will block the browser.
663- (BOOL)keyWindowIsNotModal {
[email protected]6a281332009-10-13 17:41:06664 Browser* browser = BrowserList::GetLastActive();
[email protected]dc76e8362010-06-11 16:29:52665 return [NSApp modalWindow] == nil && (!browser ||
[email protected]6a281332009-10-13 17:41:06666 ![[browser->window()->GetNativeHandle() attachedSheet]
[email protected]f6c15fa2010-06-04 20:08:21667 isKindOfClass:[NSWindow class]]);
[email protected]6a281332009-10-13 17:41:06668}
669
[email protected]88d74942009-01-21 22:04:44670// Called to validate menu items when there are no key windows. All the
671// items we care about have been set with the |commandDispatch:| action and
672// a target of FirstResponder in IB. If it's not one of those, let it
673// continue up the responder chain to be handled elsewhere. We pull out the
674// tag as the cross-platform constant to differentiate and dispatch the
675// various commands.
676- (BOOL)validateUserInterfaceItem:(id<NSValidatedUserInterfaceItem>)item {
677 SEL action = [item action];
678 BOOL enable = NO;
679 if (action == @selector(commandDispatch:)) {
680 NSInteger tag = [item tag];
[email protected]fbc947b2009-06-19 13:28:24681 if (menuState_->SupportsCommand(tag)) {
682 switch (tag) {
[email protected]419eb2a2009-11-11 16:26:26683 // The File Menu commands are not automatically disabled by Cocoa when a
684 // dialog sheet obscures the browser window, so we disable several of
685 // them here. We don't need to include IDC_CLOSE_WINDOW, because
686 // app_controller is only activated when there are no key windows (see
687 // function comment).
[email protected]fbc947b2009-06-19 13:28:24688 case IDC_RESTORE_TAB:
[email protected]f6c15fa2010-06-04 20:08:21689 enable = [self keyWindowIsNotModal] && [self canRestoreTab];
[email protected]6a281332009-10-13 17:41:06690 break;
[email protected]f6c15fa2010-06-04 20:08:21691 // Browser-level items that open in new tabs should not open if there's
692 // a window- or app-modal dialog.
[email protected]6a281332009-10-13 17:41:06693 case IDC_OPEN_FILE:
[email protected]6a281332009-10-13 17:41:06694 case IDC_NEW_TAB:
[email protected]f6c15fa2010-06-04 20:08:21695 case IDC_SHOW_HISTORY:
696 case IDC_SHOW_BOOKMARK_MANAGER:
697 enable = [self keyWindowIsNotModal];
698 break;
699 // Browser-level items that open in new windows.
700 case IDC_NEW_WINDOW:
701 case IDC_TASK_MANAGER:
702 // Allow the user to open a new window if there's a window-modal
703 // dialog.
704 enable = [self keyWindowIsNotModal] || ([NSApp modalWindow] == nil);
[email protected]fbc947b2009-06-19 13:28:24705 break;
[email protected]446e16a2010-05-03 21:23:58706 case IDC_SYNC_BOOKMARKS: {
[email protected]a585a8db2011-06-20 18:58:35707 Profile* lastProfile = [self lastProfile];
[email protected]446e16a2010-05-03 21:23:58708 // The profile may be NULL during shutdown -- see
709 // http://code.google.com/p/chromium/issues/detail?id=43048 .
710 //
711 // TODO(akalin,viettrungluu): Figure out whether this method
[email protected]a585a8db2011-06-20 18:58:35712 // can be prevented from being called if lastProfile is
[email protected]446e16a2010-05-03 21:23:58713 // NULL.
[email protected]a585a8db2011-06-20 18:58:35714 if (!lastProfile) {
[email protected]446e16a2010-05-03 21:23:58715 LOG(WARNING)
[email protected]a585a8db2011-06-20 18:58:35716 << "NULL lastProfile detected -- not doing anything";
[email protected]446e16a2010-05-03 21:23:58717 break;
718 }
[email protected]a585a8db2011-06-20 18:58:35719 enable = lastProfile->IsSyncAccessible() &&
[email protected]d3b98c82010-07-14 07:45:59720 [self keyWindowIsNotModal];
[email protected]a585a8db2011-06-20 18:58:35721 sync_ui_util::UpdateSyncItem(item, enable, lastProfile);
[email protected]f9bc9b92009-11-24 00:55:35722 break;
[email protected]446e16a2010-05-03 21:23:58723 }
[email protected]fbc947b2009-06-19 13:28:24724 default:
[email protected]f6c15fa2010-06-04 20:08:21725 enable = menuState_->IsCommandEnabled(tag) ?
726 [self keyWindowIsNotModal] : NO;
[email protected]fbc947b2009-06-19 13:28:24727 }
728 }
[email protected]3b6aa8b62009-09-15 21:36:11729 } else if (action == @selector(terminate:)) {
[email protected]88d74942009-01-21 22:04:44730 enable = YES;
[email protected]3111f08b2009-04-30 16:01:52731 } else if (action == @selector(showPreferences:)) {
732 enable = YES;
[email protected]bde3dda2009-05-20 22:13:07733 } else if (action == @selector(orderFrontStandardAboutPanel:)) {
734 enable = YES;
[email protected]93b59fc2010-12-21 20:00:47735 } else if (action == @selector(commandFromDock:)) {
[email protected]3d4bd23f2009-10-06 03:58:38736 enable = YES;
[email protected]a99fce0e2011-03-21 20:58:48737 } else if (action == @selector(toggleConfirmToQuit:)) {
738 [self updateConfirmToQuitPrefMenuItem:static_cast<NSMenuItem*>(item)];
739 enable = YES;
[email protected]88d74942009-01-21 22:04:44740 }
741 return enable;
742}
743
[email protected]947fc0d2010-01-14 22:33:05744// Called when the user picks a menu item when there are no key windows, or when
745// there is no foreground browser window. Calls through to the browser object to
746// execute the command. This assumes that the command is supported and doesn't
[email protected]d630d7d52010-02-21 00:55:11747// check, otherwise it should have been disabled in the UI in
748// |-validateUserInterfaceItem:|.
[email protected]88d74942009-01-21 22:04:44749- (void)commandDispatch:(id)sender {
[email protected]a585a8db2011-06-20 18:58:35750 Profile* lastProfile = [self lastProfile];
[email protected]f0a51fb52009-03-05 12:46:38751
[email protected]947fc0d2010-01-14 22:33:05752 // Handle the case where we're dispatching a command from a sender that's in a
753 // browser window. This means that the command came from a background window
754 // and is getting here because the foreground window is not a browser window.
755 if ([sender respondsToSelector:@selector(window)]) {
756 id delegate = [[sender window] windowController];
757 if ([delegate isKindOfClass:[BrowserWindowController class]]) {
758 [delegate commandDispatch:sender];
759 return;
760 }
761 }
762
[email protected]88d74942009-01-21 22:04:44763 NSInteger tag = [sender tag];
764 switch (tag) {
[email protected]fbc947b2009-06-19 13:28:24765 case IDC_NEW_TAB:
[email protected]d630d7d52010-02-21 00:55:11766 // Create a new tab in an existing browser window (which we activate) if
767 // possible.
[email protected]a585a8db2011-06-20 18:58:35768 if (Browser* browser = ActivateBrowser(lastProfile)) {
[email protected]d630d7d52010-02-21 00:55:11769 browser->ExecuteCommand(IDC_NEW_TAB);
770 break;
771 }
772 // Else fall through to create new window.
[email protected]88d74942009-01-21 22:04:44773 case IDC_NEW_WINDOW:
[email protected]a585a8db2011-06-20 18:58:35774 CreateBrowser(lastProfile);
[email protected]d630d7d52010-02-21 00:55:11775 break;
[email protected]91a5b3e2009-10-30 19:32:22776 case IDC_FOCUS_LOCATION:
[email protected]a585a8db2011-06-20 18:58:35777 ActivateOrCreateBrowser(lastProfile)->ExecuteCommand(IDC_FOCUS_LOCATION);
[email protected]88d74942009-01-21 22:04:44778 break;
[email protected]cddc5242009-12-07 18:54:33779 case IDC_FOCUS_SEARCH:
[email protected]a585a8db2011-06-20 18:58:35780 ActivateOrCreateBrowser(lastProfile)->ExecuteCommand(IDC_FOCUS_SEARCH);
[email protected]cddc5242009-12-07 18:54:33781 break;
[email protected]863ff662009-01-26 20:18:18782 case IDC_NEW_INCOGNITO_WINDOW:
[email protected]a585a8db2011-06-20 18:58:35783 Browser::OpenEmptyWindow(lastProfile->GetOffTheRecordProfile());
[email protected]863ff662009-01-26 20:18:18784 break;
[email protected]fbc947b2009-06-19 13:28:24785 case IDC_RESTORE_TAB:
[email protected]a585a8db2011-06-20 18:58:35786 Browser::OpenWindowWithRestoredTabs(lastProfile);
[email protected]fbc947b2009-06-19 13:28:24787 break;
[email protected]e19516d2009-04-28 17:15:19788 case IDC_OPEN_FILE:
[email protected]a585a8db2011-06-20 18:58:35789 CreateBrowser(lastProfile)->ExecuteCommand(IDC_OPEN_FILE);
[email protected]e19516d2009-04-28 17:15:19790 break;
[email protected]1fdff702009-10-22 00:36:18791 case IDC_CLEAR_BROWSING_DATA: {
[email protected]93e181762009-05-29 14:30:38792 // There may not be a browser open, so use the default profile.
[email protected]a585a8db2011-06-20 18:58:35793 if (Browser* browser = ActivateBrowser(lastProfile)) {
[email protected]445177652011-03-09 02:04:05794 browser->OpenClearBrowsingDataDialog();
[email protected]c8de64a2011-01-25 17:10:23795 } else {
[email protected]a585a8db2011-06-20 18:58:35796 Browser::OpenClearBrowingDataDialogWindow(lastProfile);
[email protected]c8de64a2011-01-25 17:10:23797 }
[email protected]93e181762009-05-29 14:30:38798 break;
[email protected]1fdff702009-10-22 00:36:18799 }
[email protected]4aee5652009-11-19 18:59:21800 case IDC_IMPORT_SETTINGS: {
[email protected]a585a8db2011-06-20 18:58:35801 if (Browser* browser = ActivateBrowser(lastProfile)) {
[email protected]445177652011-03-09 02:04:05802 browser->OpenImportSettingsDialog();
[email protected]c8de64a2011-01-25 17:10:23803 } else {
[email protected]a585a8db2011-06-20 18:58:35804 Browser::OpenImportSettingsDialogWindow(lastProfile);
[email protected]c8de64a2011-01-25 17:10:23805 }
[email protected]4aee5652009-11-19 18:59:21806 break;
807 }
[email protected]af7c25e2010-01-23 05:46:45808 case IDC_SHOW_BOOKMARK_MANAGER:
[email protected]afd1e522011-04-27 23:29:59809 UserMetrics::RecordAction(UserMetricsAction("ShowBookmarkManager"));
[email protected]a585a8db2011-06-20 18:58:35810 if (Browser* browser = ActivateBrowser(lastProfile)) {
[email protected]47e532b2010-04-23 17:46:40811 // Open a bookmark manager tab.
[email protected]90a8bf252010-03-12 00:09:47812 browser->OpenBookmarkManager();
813 } else {
[email protected]47e532b2010-04-23 17:46:40814 // No browser window, so create one for the bookmark manager tab.
[email protected]a585a8db2011-06-20 18:58:35815 Browser::OpenBookmarkManagerWindow(lastProfile);
[email protected]90a8bf252010-03-12 00:09:47816 }
[email protected]af7c25e2010-01-23 05:46:45817 break;
[email protected]91a5b3e2009-10-30 19:32:22818 case IDC_SHOW_HISTORY:
[email protected]a585a8db2011-06-20 18:58:35819 if (Browser* browser = ActivateBrowser(lastProfile))
[email protected]d630d7d52010-02-21 00:55:11820 browser->ShowHistoryTab();
821 else
[email protected]a585a8db2011-06-20 18:58:35822 Browser::OpenHistoryWindow(lastProfile);
[email protected]91a5b3e2009-10-30 19:32:22823 break;
824 case IDC_SHOW_DOWNLOADS:
[email protected]a585a8db2011-06-20 18:58:35825 if (Browser* browser = ActivateBrowser(lastProfile))
[email protected]d630d7d52010-02-21 00:55:11826 browser->ShowDownloadsTab();
827 else
[email protected]a585a8db2011-06-20 18:58:35828 Browser::OpenDownloadsWindow(lastProfile);
[email protected]91a5b3e2009-10-30 19:32:22829 break;
[email protected]bb37e6ec2009-11-18 17:59:36830 case IDC_MANAGE_EXTENSIONS:
[email protected]a585a8db2011-06-20 18:58:35831 if (Browser* browser = ActivateBrowser(lastProfile))
[email protected]d630d7d52010-02-21 00:55:11832 browser->ShowExtensionsTab();
833 else
[email protected]a585a8db2011-06-20 18:58:35834 Browser::OpenExtensionsWindow(lastProfile);
[email protected]bb37e6ec2009-11-18 17:59:36835 break;
[email protected]1fdff702009-10-22 00:36:18836 case IDC_HELP_PAGE:
[email protected]a585a8db2011-06-20 18:58:35837 if (Browser* browser = ActivateBrowser(lastProfile))
[email protected]d630d7d52010-02-21 00:55:11838 browser->OpenHelpTab();
839 else
[email protected]a585a8db2011-06-20 18:58:35840 Browser::OpenHelpWindow(lastProfile);
[email protected]1fdff702009-10-22 00:36:18841 break;
[email protected]1c41a932010-11-22 23:41:23842 case IDC_FEEDBACK: {
[email protected]3896b182009-11-03 16:15:32843 Browser* browser = BrowserList::GetLastActive();
[email protected]d630d7d52010-02-21 00:55:11844 TabContents* currentTab =
845 browser ? browser->GetSelectedTabContents() : NULL;
[email protected]3896b182009-11-03 16:15:32846 BugReportWindowController* controller =
847 [[BugReportWindowController alloc]
[email protected]d630d7d52010-02-21 00:55:11848 initWithTabContents:currentTab
[email protected]a585a8db2011-06-20 18:58:35849 profile:[self lastProfile]];
[email protected]3896b182009-11-03 16:15:32850 [controller runModalDialog];
851 break;
852 }
[email protected]f9bc9b92009-11-24 00:55:35853 case IDC_SYNC_BOOKMARKS:
[email protected]446e16a2010-05-03 21:23:58854 // The profile may be NULL during shutdown -- see
855 // http://code.google.com/p/chromium/issues/detail?id=43048 .
856 //
857 // TODO(akalin,viettrungluu): Figure out whether this method can
[email protected]a585a8db2011-06-20 18:58:35858 // be prevented from being called if lastProfile is NULL.
859 if (!lastProfile) {
860 LOG(WARNING) << "NULL lastProfile detected -- not doing anything";
[email protected]446e16a2010-05-03 21:23:58861 break;
862 }
[email protected]f9bc9b92009-11-24 00:55:35863 // TODO(akalin): Add a constant to denote starting sync from the
864 // main menu and use that instead of START_FROM_WRENCH.
[email protected]40f047972009-11-25 03:54:40865 sync_ui_util::OpenSyncMyBookmarksDialog(
[email protected]a585a8db2011-06-20 18:58:35866 lastProfile, ActivateBrowser(lastProfile),
[email protected]c8de64a2011-01-25 17:10:23867 ProfileSyncService::START_FROM_WRENCH);
[email protected]f9bc9b92009-11-24 00:55:35868 break;
[email protected]ceaaf1812010-01-20 22:13:09869 case IDC_TASK_MANAGER:
[email protected]afd1e522011-04-27 23:29:59870 UserMetrics::RecordAction(UserMetricsAction("TaskManager"));
[email protected]adb6a84d2011-02-07 16:58:40871 TaskManagerMac::Show(false);
[email protected]ceaaf1812010-01-20 22:13:09872 break;
[email protected]4a42d272010-06-18 01:29:42873 case IDC_OPTIONS:
874 [self showPreferences:sender];
875 break;
[email protected]93b59fc2010-12-21 20:00:47876 default:
877 // Background Applications use dynamic values that must be less than the
878 // smallest value among the predefined IDC_* labels.
879 if ([sender tag] < IDC_MinimumLabelValue)
880 [self executeApplication:sender];
881 break;
[email protected]d630d7d52010-02-21 00:55:11882 }
[email protected]88d74942009-01-21 22:04:44883}
884
[email protected]93b59fc2010-12-21 20:00:47885// Run a (background) application in a new tab.
886- (void)executeApplication:(id)sender {
887 NSInteger tag = [sender tag];
[email protected]a585a8db2011-06-20 18:58:35888 Profile* profile = [self lastProfile];
[email protected]93b59fc2010-12-21 20:00:47889 DCHECK(profile);
890 BackgroundApplicationListModel applications(profile);
891 DCHECK(tag >= 0 &&
892 tag < static_cast<int>(applications.size()));
893 Browser* browser = BrowserList::GetLastActive();
894 if (!browser) {
895 Browser::OpenEmptyWindow(profile);
896 browser = BrowserList::GetLastActive();
897 }
898 const Extension* extension = applications.GetExtension(tag);
[email protected]7e41c2b2011-05-06 10:31:16899 browser->OpenApplicationTab(profile, extension, NEW_FOREGROUND_TAB);
[email protected]93b59fc2010-12-21 20:00:47900}
901
[email protected]947fc0d2010-01-14 22:33:05902// Same as |-commandDispatch:|, but executes commands using a disposition
903// determined by the key flags. This will get called in the case where the
904// frontmost window is not a browser window, and the user has command-clicked
905// a button in a background browser window whose action is
906// |-commandDispatchUsingKeyModifiers:|
907- (void)commandDispatchUsingKeyModifiers:(id)sender {
908 DCHECK(sender);
909 if ([sender respondsToSelector:@selector(window)]) {
910 id delegate = [[sender window] windowController];
911 if ([delegate isKindOfClass:[BrowserWindowController class]]) {
912 [delegate commandDispatchUsingKeyModifiers:sender];
913 }
914 }
915}
916
[email protected]ff81e0a2009-04-13 14:58:54917// NSApplication delegate method called when someone clicks on the
918// dock icon and there are no open windows. To match standard mac
919// behavior, we should open a new window.
920- (BOOL)applicationShouldHandleReopen:(NSApplication*)theApplication
921 hasVisibleWindows:(BOOL)flag {
[email protected]70631402010-04-26 20:16:54922 // If the browser is currently trying to quit, don't do anything and return NO
923 // to prevent AppKit from doing anything.
924 // TODO(rohitrao): Remove this code when http://crbug.com/40861 is resolved.
925 if (browser_shutdown::IsTryingToQuit())
926 return NO;
927
[email protected]ff81e0a2009-04-13 14:58:54928 // Don't do anything if there are visible windows. This will cause
929 // AppKit to unminimize the most recently minimized window.
930 if (flag)
931 return YES;
932
[email protected]0d1210d62010-09-07 17:31:38933 // If launched as a hidden login item (due to installation of a persistent app
934 // or by the user, for example in System Preferenecs->Accounts->Login Items),
935 // allow session to be restored first time the user clicks on a Dock icon.
936 // Normally, it'd just open a new empty page.
937 {
938 static BOOL doneOnce = NO;
939 if (!doneOnce) {
940 doneOnce = YES;
[email protected]0378bf42011-01-01 18:20:14941 if (base::mac::WasLaunchedAsHiddenLoginItem()) {
[email protected]0d1210d62010-09-07 17:31:38942 SessionService* sessionService =
[email protected]a585a8db2011-06-20 18:58:35943 SessionServiceFactory::GetForProfile([self lastProfile]);
[email protected]0d1210d62010-09-07 17:31:38944 if (sessionService &&
945 sessionService->RestoreIfNecessary(std::vector<GURL>()))
946 return NO;
947 }
948 }
949 }
[email protected]ff81e0a2009-04-13 14:58:54950 // Otherwise open a new window.
[email protected]d630d7d52010-02-21 00:55:11951 {
[email protected]0fbd70332010-06-01 19:28:34952 AutoReset<bool> auto_reset_in_run(&g_is_opening_new_window, true);
[email protected]a585a8db2011-06-20 18:58:35953 Browser::OpenEmptyWindow([self lastProfile]);
[email protected]d630d7d52010-02-21 00:55:11954 }
[email protected]ff81e0a2009-04-13 14:58:54955
956 // We've handled the reopen event, so return NO to tell AppKit not
957 // to do anything.
958 return NO;
959}
960
[email protected]88d74942009-01-21 22:04:44961- (void)initMenuState {
[email protected]3111f08b2009-04-30 16:01:52962 menuState_.reset(new CommandUpdater(NULL));
[email protected]fbc947b2009-06-19 13:28:24963 menuState_->UpdateCommandEnabled(IDC_NEW_TAB, true);
[email protected]88d74942009-01-21 22:04:44964 menuState_->UpdateCommandEnabled(IDC_NEW_WINDOW, true);
[email protected]863ff662009-01-26 20:18:18965 menuState_->UpdateCommandEnabled(IDC_NEW_INCOGNITO_WINDOW, true);
[email protected]e19516d2009-04-28 17:15:19966 menuState_->UpdateCommandEnabled(IDC_OPEN_FILE, true);
[email protected]93e181762009-05-29 14:30:38967 menuState_->UpdateCommandEnabled(IDC_CLEAR_BROWSING_DATA, true);
[email protected]fbc947b2009-06-19 13:28:24968 menuState_->UpdateCommandEnabled(IDC_RESTORE_TAB, false);
[email protected]91a5b3e2009-10-30 19:32:22969 menuState_->UpdateCommandEnabled(IDC_FOCUS_LOCATION, true);
[email protected]cddc5242009-12-07 18:54:33970 menuState_->UpdateCommandEnabled(IDC_FOCUS_SEARCH, true);
[email protected]af7c25e2010-01-23 05:46:45971 menuState_->UpdateCommandEnabled(IDC_SHOW_BOOKMARK_MANAGER, true);
[email protected]91a5b3e2009-10-30 19:32:22972 menuState_->UpdateCommandEnabled(IDC_SHOW_HISTORY, true);
973 menuState_->UpdateCommandEnabled(IDC_SHOW_DOWNLOADS, true);
[email protected]bb37e6ec2009-11-18 17:59:36974 menuState_->UpdateCommandEnabled(IDC_MANAGE_EXTENSIONS, true);
[email protected]1fdff702009-10-22 00:36:18975 menuState_->UpdateCommandEnabled(IDC_HELP_PAGE, true);
[email protected]4aee5652009-11-19 18:59:21976 menuState_->UpdateCommandEnabled(IDC_IMPORT_SETTINGS, true);
[email protected]1c41a932010-11-22 23:41:23977 menuState_->UpdateCommandEnabled(IDC_FEEDBACK, true);
[email protected]d3b98c82010-07-14 07:45:59978 menuState_->UpdateCommandEnabled(IDC_SYNC_BOOKMARKS, true);
[email protected]ceaaf1812010-01-20 22:13:09979 menuState_->UpdateCommandEnabled(IDC_TASK_MANAGER, true);
[email protected]88d74942009-01-21 22:04:44980}
[email protected]1bcdb532009-01-16 17:47:57981
[email protected]a99fce0e2011-03-21 20:58:48982// The Confirm to Quit preference is atypical in that the preference lives in
983// the app menu right above the Quit menu item. This method will refresh the
984// display of that item depending on the preference state.
985- (void)updateConfirmToQuitPrefMenuItem:(NSMenuItem*)item {
986 // Format the string so that the correct key equivalent is displayed.
987 NSString* acceleratorString = [ConfirmQuitPanelController keyCommandString];
988 NSString* title = l10n_util::GetNSStringF(IDS_CONFIRM_TO_QUIT_OPTION,
989 base::SysNSStringToUTF16(acceleratorString));
990 [item setTitle:title];
991
[email protected]a585a8db2011-06-20 18:58:35992 const PrefService* prefService = [self lastProfile]->GetPrefs();
[email protected]a99fce0e2011-03-21 20:58:48993 bool enabled = prefService->GetBoolean(prefs::kConfirmToQuitEnabled);
994 [item setState:enabled ? NSOnState : NSOffState];
995}
996
[email protected]eef99c22010-08-17 05:55:16997- (void)registerServicesMenuTypesTo:(NSApplication*)app {
[email protected]09ea24c2010-08-23 19:05:41998 // Note that RenderWidgetHostViewCocoa implements NSServicesRequests which
999 // handles requests from services.
1000 NSArray* types = [NSArray arrayWithObjects:NSStringPboardType, nil];
1001 [app registerServicesMenuSendTypes:types returnTypes:types];
[email protected]eef99c22010-08-17 05:55:161002}
1003
[email protected]a585a8db2011-06-20 18:58:351004- (Profile*)lastProfile {
[email protected]7fd5a462011-06-19 16:57:231005 // Return the profile of the last-used BrowserWindowController, if available.
1006 if (lastProfile_)
1007 return lastProfile_;
1008
1009 // Fallback to the default if this is the first Browser.
[email protected]3f34599d2009-03-25 22:11:431010 if (g_browser_process->profile_manager())
[email protected]844a1002011-04-19 11:37:211011 return g_browser_process->profile_manager()->GetDefaultProfile();
[email protected]3f34599d2009-03-25 22:11:431012
[email protected]f6314002009-04-23 01:18:131013 return NULL;
[email protected]3f34599d2009-03-25 22:11:431014}
1015
[email protected]57750f822009-04-21 21:43:091016// Various methods to open URLs that we get in a native fashion. We use
1017// BrowserInit here because on the other platforms, URLs to open come through
1018// the ProcessSingleton, and it calls BrowserInit. It's best to bottleneck the
1019// openings through that for uniform handling.
1020
[email protected]d630d7d52010-02-21 00:55:111021- (void)openUrls:(const std::vector<GURL>&)urls {
[email protected]caadfca2010-01-22 21:51:211022 // If the browser hasn't started yet, just queue up the URLs.
1023 if (!startupComplete_) {
[email protected]d630d7d52010-02-21 00:55:111024 startupUrls_.insert(startupUrls_.end(), urls.begin(), urls.end());
[email protected]136140c2009-05-19 13:58:251025 return;
1026 }
[email protected]57750f822009-04-21 21:43:091027
[email protected]0e863512009-05-28 19:45:071028 Browser* browser = BrowserList::GetLastActive();
1029 // if no browser window exists then create one with no tabs to be filled in
1030 if (!browser) {
[email protected]a585a8db2011-06-20 18:58:351031 browser = Browser::Create([self lastProfile]);
[email protected]ce560f82009-06-03 09:39:441032 browser->window()->Show();
[email protected]0e863512009-05-28 19:45:071033 }
1034
[email protected]947446b2010-10-21 03:36:311035 CommandLine dummy(CommandLine::NO_PROGRAM);
[email protected]f805fe82010-08-03 22:47:101036 BrowserInit::LaunchWithProfile launch(FilePath(), dummy);
[email protected]0e863512009-05-28 19:45:071037 launch.OpenURLsInBrowser(browser, false, urls);
[email protected]57750f822009-04-21 21:43:091038}
1039
[email protected]57750f822009-04-21 21:43:091040- (void)getUrl:(NSAppleEventDescriptor*)event
1041 withReply:(NSAppleEventDescriptor*)reply {
1042 NSString* urlStr = [[event paramDescriptorForKeyword:keyDirectObject]
1043 stringValue];
1044
1045 GURL gurl(base::SysNSStringToUTF8(urlStr));
1046 std::vector<GURL> gurlVector;
1047 gurlVector.push_back(gurl);
1048
[email protected]d630d7d52010-02-21 00:55:111049 [self openUrls:gurlVector];
[email protected]57750f822009-04-21 21:43:091050}
1051
[email protected]caadfca2010-01-22 21:51:211052- (void)application:(NSApplication*)sender
1053 openFiles:(NSArray*)filenames {
[email protected]57750f822009-04-21 21:43:091054 std::vector<GURL> gurlVector;
[email protected]caadfca2010-01-22 21:51:211055 for (NSString* file in filenames) {
1056 GURL gurl = net::FilePathToFileURL(FilePath(base::SysNSStringToUTF8(file)));
[email protected]57750f822009-04-21 21:43:091057 gurlVector.push_back(gurl);
1058 }
[email protected]caadfca2010-01-22 21:51:211059 if (!gurlVector.empty())
[email protected]d630d7d52010-02-21 00:55:111060 [self openUrls:gurlVector];
[email protected]caadfca2010-01-22 21:51:211061 else
1062 NOTREACHED() << "Nothing to open!";
[email protected]57750f822009-04-21 21:43:091063
[email protected]caadfca2010-01-22 21:51:211064 [sender replyToOpenOrPrint:NSApplicationDelegateReplySuccess];
[email protected]57750f822009-04-21 21:43:091065}
[email protected]3f34599d2009-03-25 22:11:431066
[email protected]3111f08b2009-04-30 16:01:521067// Show the preferences window, or bring it to the front if it's already
1068// visible.
1069- (IBAction)showPreferences:(id)sender {
[email protected]a585a8db2011-06-20 18:58:351070 if (Browser* browser = ActivateBrowser([self lastProfile])) {
[email protected]445177652011-03-09 02:04:051071 // Show options tab in the active browser window.
1072 browser->OpenOptionsDialog();
[email protected]4a42d272010-06-18 01:29:421073 } else {
[email protected]445177652011-03-09 02:04:051074 // No browser window, so create one for the options tab.
[email protected]a585a8db2011-06-20 18:58:351075 Browser::OpenOptionsWindow([self lastProfile]);
[email protected]4a42d272010-06-18 01:29:421076 }
[email protected]d989f07c2009-11-14 00:35:461077}
1078
[email protected]bde3dda2009-05-20 22:13:071079// Called when the about window is closed. We use this to release the
1080// window controller.
[email protected]d4e18652009-11-19 21:52:161081- (void)aboutWindowClosed:(NSNotification*)notification {
1082 NSWindow* window = [aboutController_ window];
[email protected]2ce9c89752011-02-25 18:24:341083 DCHECK_EQ([notification object], window);
[email protected]bde3dda2009-05-20 22:13:071084 [[NSNotificationCenter defaultCenter]
[email protected]49aeee52009-10-26 19:58:131085 removeObserver:self
[email protected]d4e18652009-11-19 21:52:161086 name:NSWindowWillCloseNotification
1087 object:window];
1088 // AboutWindowControllers are autoreleased in
1089 // -[AboutWindowController windowWillClose:].
1090 aboutController_ = nil;
[email protected]bde3dda2009-05-20 22:13:071091}
1092
1093- (IBAction)orderFrontStandardAboutPanel:(id)sender {
[email protected]bde3dda2009-05-20 22:13:071094 if (!aboutController_) {
[email protected]d4e18652009-11-19 21:52:161095 aboutController_ =
[email protected]a585a8db2011-06-20 18:58:351096 [[AboutWindowController alloc] initWithProfile:[self lastProfile]];
[email protected]49aeee52009-10-26 19:58:131097
[email protected]bde3dda2009-05-20 22:13:071098 // Watch for a notification of when it goes away so that we can destroy
1099 // the controller.
1100 [[NSNotificationCenter defaultCenter]
1101 addObserver:self
1102 selector:@selector(aboutWindowClosed:)
[email protected]d4e18652009-11-19 21:52:161103 name:NSWindowWillCloseNotification
1104 object:[aboutController_ window]];
[email protected]bde3dda2009-05-20 22:13:071105 }
[email protected]49aeee52009-10-26 19:58:131106
[email protected]bde3dda2009-05-20 22:13:071107 [aboutController_ showWindow:self];
[email protected]bde3dda2009-05-20 22:13:071108}
1109
[email protected]a99fce0e2011-03-21 20:58:481110- (IBAction)toggleConfirmToQuit:(id)sender {
[email protected]a585a8db2011-06-20 18:58:351111 PrefService* prefService = [self lastProfile]->GetPrefs();
[email protected]a99fce0e2011-03-21 20:58:481112 bool enabled = prefService->GetBoolean(prefs::kConfirmToQuitEnabled);
1113 prefService->SetBoolean(prefs::kConfirmToQuitEnabled, !enabled);
1114}
1115
[email protected]3d4bd23f2009-10-06 03:58:381116// Explicitly bring to the foreground when creating new windows from the dock.
[email protected]93b59fc2010-12-21 20:00:471117- (void)commandFromDock:(id)sender {
[email protected]3d4bd23f2009-10-06 03:58:381118 [NSApp activateIgnoringOtherApps:YES];
1119 [self commandDispatch:sender];
1120}
1121
[email protected]227fcee2010-06-11 19:19:371122- (NSMenu*)applicationDockMenu:(NSApplication*)sender {
[email protected]018a3962009-09-17 22:23:441123 NSMenu* dockMenu = [[[NSMenu alloc] initWithTitle: @""] autorelease];
[email protected]a585a8db2011-06-20 18:58:351124 Profile* profile = [self lastProfile];
[email protected]93b59fc2010-12-21 20:00:471125
[email protected]018a3962009-09-17 22:23:441126 NSString* titleStr = l10n_util::GetNSStringWithFixup(IDS_NEW_WINDOW_MAC);
[email protected]8c6b029d2011-01-21 22:39:511127 scoped_nsobject<NSMenuItem> item(
1128 [[NSMenuItem alloc] initWithTitle:titleStr
1129 action:@selector(commandFromDock:)
1130 keyEquivalent:@""]);
[email protected]288bfcd32009-09-14 18:14:461131 [item setTarget:self];
1132 [item setTag:IDC_NEW_WINDOW];
[email protected]018a3962009-09-17 22:23:441133 [dockMenu addItem:item];
[email protected]288bfcd32009-09-14 18:14:461134
1135 titleStr = l10n_util::GetNSStringWithFixup(IDS_NEW_INCOGNITO_WINDOW_MAC);
[email protected]018a3962009-09-17 22:23:441136 item.reset([[NSMenuItem alloc] initWithTitle:titleStr
[email protected]8c6b029d2011-01-21 22:39:511137 action:@selector(commandFromDock:)
[email protected]018a3962009-09-17 22:23:441138 keyEquivalent:@""]);
[email protected]288bfcd32009-09-14 18:14:461139 [item setTarget:self];
1140 [item setTag:IDC_NEW_INCOGNITO_WINDOW];
[email protected]018a3962009-09-17 22:23:441141 [dockMenu addItem:item];
[email protected]288bfcd32009-09-14 18:14:461142
[email protected]8c6b029d2011-01-21 22:39:511143 // TODO(rickcam): Mock out BackgroundApplicationListModel, then add unit
1144 // tests which use the mock in place of the profile-initialized model.
1145
1146 // Avoid breaking unit tests which have no profile.
1147 if (profile) {
1148 BackgroundApplicationListModel applications(profile);
1149 if (applications.size()) {
1150 int position = 0;
1151 NSString* menuStr =
1152 l10n_util::GetNSStringWithFixup(IDS_BACKGROUND_APPS_MAC);
1153 scoped_nsobject<NSMenu> appMenu([[NSMenu alloc] initWithTitle:menuStr]);
1154 for (ExtensionList::const_iterator cursor = applications.begin();
1155 cursor != applications.end();
1156 ++cursor, ++position) {
[email protected]2ce9c89752011-02-25 18:24:341157 DCHECK_EQ(applications.GetPosition(*cursor), position);
[email protected]8c6b029d2011-01-21 22:39:511158 NSString* itemStr =
1159 base::SysUTF16ToNSString(UTF8ToUTF16((*cursor)->name()));
1160 scoped_nsobject<NSMenuItem> appItem([[NSMenuItem alloc]
1161 initWithTitle:itemStr
1162 action:@selector(commandFromDock:)
1163 keyEquivalent:@""]);
1164 [appItem setTarget:self];
1165 [appItem setTag:position];
1166 [appMenu addItem:appItem];
1167 }
1168 scoped_nsobject<NSMenuItem> appMenuItem([[NSMenuItem alloc]
1169 initWithTitle:menuStr
1170 action:@selector(commandFromDock:)
1171 keyEquivalent:@""]);
1172 [appMenuItem setTarget:self];
1173 [appMenuItem setTag:position];
1174 [appMenuItem setSubmenu:appMenu];
1175 [dockMenu addItem:appMenuItem];
1176 }
1177 }
1178
[email protected]018a3962009-09-17 22:23:441179 return dockMenu;
[email protected]288bfcd32009-09-14 18:14:461180}
1181
[email protected]d630d7d52010-02-21 00:55:111182- (const std::vector<GURL>&)startupUrls {
1183 return startupUrls_;
[email protected]caadfca2010-01-22 21:51:211184}
1185
[email protected]d630d7d52010-02-21 00:55:111186- (void)clearStartupUrls {
1187 startupUrls_.clear();
[email protected]caadfca2010-01-22 21:51:211188}
1189
[email protected]7e4c69d02011-05-11 22:16:471190- (BookmarkMenuBridge*)bookmarkMenuBridge {
1191 return bookmarkMenuBridge_.get();
1192}
1193
[email protected]d630d7d52010-02-21 00:55:111194@end // @implementation AppController
[email protected]60ad3e22009-09-18 21:07:191195
1196//---------------------------------------------------------------------------
1197
[email protected]0934de82011-03-09 18:42:161198namespace browser {
1199
1200void ShowInstantConfirmDialog(gfx::NativeWindow parent, Profile* profile) {
1201 if (Browser* browser = ActivateBrowser(profile)) {
[email protected]b99081b2011-03-10 17:43:471202 browser->OpenInstantConfirmDialog();
[email protected]0934de82011-03-09 18:42:161203 } else {
[email protected]b99081b2011-03-10 17:43:471204 Browser::OpenInstantConfirmDialogWindow(profile);
[email protected]0934de82011-03-09 18:42:161205 }
1206}
1207
1208} // namespace browser
1209
[email protected]216e9042009-10-29 17:05:231210namespace app_controller_mac {
1211
1212bool IsOpeningNewWindow() {
1213 return g_is_opening_new_window;
1214}
1215
1216} // namespace app_controller_mac