blob: b1acdebd8d0d8fd7b6c2e47fccf72b6b8a9664cf [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]136140c2009-05-19 13:58:25215}
216
[email protected]7dc8c6b2010-04-09 17:02:50217// (NSApplicationDelegate protocol) This is the Apple-approved place to override
218// the default handlers.
219- (void)applicationWillFinishLaunching:(NSNotification*)notification {
[email protected]58877932010-05-25 05:43:11220 // Nothing here right now.
[email protected]7dc8c6b2010-04-09 17:02:50221}
222
223- (BOOL)tryToTerminateApplication:(NSApplication*)app {
[email protected]3076fad2010-04-28 18:11:45224 // Check for in-process downloads, and prompt the user if they really want
225 // to quit (and thus cancel downloads). Only check if we're not already
226 // shutting down, else the user might be prompted multiple times if the
227 // download isn't stopped before terminate is called again.
228 if (!browser_shutdown::IsTryingToQuit() &&
229 ![self shouldQuitWithInProgressDownloads])
230 return NO;
231
[email protected]7dc8c6b2010-04-09 17:02:50232 // TODO(viettrungluu): Remove Apple Event handlers here? (It's safe to leave
233 // them in, but I'm not sure about UX; we'd also want to disable other things
234 // though.) http://crbug.com/40861
235
[email protected]177aceb2010-11-03 16:17:41236 // Check if the user really wants to quit by employing the confirm-to-quit
237 // mechanism.
238 if (!browser_shutdown::IsTryingToQuit() &&
239 [self applicationShouldTerminate:app] != NSTerminateNow)
240 return NO;
241
[email protected]c984d9f2010-07-20 20:52:20242 size_t num_browsers = BrowserList::size();
[email protected]7dc8c6b2010-04-09 17:02:50243
[email protected]e7dd6d82010-10-07 23:21:12244 // Give any print jobs in progress time to finish.
245 if (!browser_shutdown::IsTryingToQuit())
246 g_browser_process->print_job_manager()->StopJobs(true);
247
[email protected]c984d9f2010-07-20 20:52:20248 // Initiate a shutdown (via BrowserList::CloseAllBrowsers()) if we aren't
249 // already shutting down.
250 if (!browser_shutdown::IsTryingToQuit())
[email protected]c6032e82010-09-13 20:06:05251 BrowserList::CloseAllBrowsers();
[email protected]7dc8c6b2010-04-09 17:02:50252
[email protected]c984d9f2010-07-20 20:52:20253 return num_browsers == 0 ? YES : NO;
[email protected]7dc8c6b2010-04-09 17:02:50254}
255
256- (void)stopTryingToTerminateApplication:(NSApplication*)app {
257 if (browser_shutdown::IsTryingToQuit()) {
258 // Reset the "trying to quit" state, so that closing all browser windows
259 // will no longer lead to termination.
260 browser_shutdown::SetTryingToQuit(false);
261
262 // TODO(viettrungluu): Were we to remove Apple Event handlers above, we
263 // would have to reinstall them here. http://crbug.com/40861
264 }
[email protected]a9e8afc2009-08-11 22:03:17265}
266
[email protected]177aceb2010-11-03 16:17:41267- (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication*)app {
[email protected]a99fce0e2011-03-21 20:58:48268 // Check if the preference is turned on.
[email protected]a585a8db2011-06-20 18:58:35269 const PrefService* prefs = [self lastProfile]->GetPrefs();
[email protected]1eaddf32011-04-05 15:09:23270 if (!prefs->GetBoolean(prefs::kConfirmToQuitEnabled)) {
271 confirm_quit::RecordHistogram(confirm_quit::kNoConfirm);
[email protected]177aceb2010-11-03 16:17:41272 return NSTerminateNow;
[email protected]1eaddf32011-04-05 15:09:23273 }
[email protected]177aceb2010-11-03 16:17:41274
275 // If the application is going to terminate as the result of a Cmd+Q
276 // invocation, use the special sauce to prevent accidental quitting.
277 // http://dev.chromium.org/developers/design-documents/confirm-to-quit-experiment
[email protected]177aceb2010-11-03 16:17:41278
[email protected]2020fd0b2010-12-10 01:16:50279 // This logic is only for keyboard-initiated quits.
[email protected]5df54fb2011-03-07 18:15:26280 if (![ConfirmQuitPanelController eventTriggersFeature:[app currentEvent]])
[email protected]2020fd0b2010-12-10 01:16:50281 return NSTerminateNow;
[email protected]177aceb2010-11-03 16:17:41282
[email protected]5df54fb2011-03-07 18:15:26283 return [[ConfirmQuitPanelController sharedController]
284 runModalLoopForApplication:app];
[email protected]177aceb2010-11-03 16:17:41285}
286
[email protected]449dd2f2009-05-27 13:04:00287// Called when the app is shutting down. Clean-up as appropriate.
[email protected]7dc8c6b2010-04-09 17:02:50288- (void)applicationWillTerminate:(NSNotification*)aNotification {
[email protected]3b6aa8b62009-09-15 21:36:11289 NSAppleEventManager* em = [NSAppleEventManager sharedAppleEventManager];
290 [em removeEventHandlerForEventClass:kInternetEventClass
291 andEventID:kAEGetURL];
292 [em removeEventHandlerForEventClass:'WWW!'
293 andEventID:'OURL'];
[email protected]3b6aa8b62009-09-15 21:36:11294
[email protected]7dc8c6b2010-04-09 17:02:50295 // There better be no browser windows left at this point.
296 CHECK_EQ(BrowserList::size(), 0u);
[email protected]3b6aa8b62009-09-15 21:36:11297
[email protected]c984d9f2010-07-20 20:52:20298 // Tell BrowserList not to keep the browser process alive. Once all the
299 // browsers get dealloc'd, it will stop the RunLoop and fall back into main().
300 BrowserList::EndKeepAlive();
[email protected]fbc947b2009-06-19 13:28:24301
[email protected]d4e18652009-11-19 21:52:16302 // Close these off if they have open windows.
[email protected]d4e18652009-11-19 21:52:16303 [aboutController_ close];
304
[email protected]449dd2f2009-05-27 13:04:00305 [[NSNotificationCenter defaultCenter] removeObserver:self];
306}
307
[email protected]3b6aa8b62009-09-15 21:36:11308- (void)didEndMainMessageLoop {
[email protected]a585a8db2011-06-20 18:58:35309 DCHECK(!BrowserList::HasBrowserWithProfile([self lastProfile]));
310 if (!BrowserList::HasBrowserWithProfile([self lastProfile])) {
[email protected]3b6aa8b62009-09-15 21:36:11311 // As we're shutting down, we need to nuke the TabRestoreService, which
312 // will start the shutdown of the NavigationControllers and allow for
313 // proper shutdown. If we don't do this, Chrome won't shut down cleanly,
314 // and may end up crashing when some thread tries to use the IO thread (or
315 // another thread) that is no longer valid.
[email protected]a585a8db2011-06-20 18:58:35316 TabRestoreServiceFactory::ResetForProfile([self lastProfile]);
[email protected]3b6aa8b62009-09-15 21:36:11317 }
318}
319
[email protected]449dd2f2009-05-27 13:04:00320// Helper routine to get the window controller if the key window is a tabbed
321// window, or nil if not. Examples of non-tabbed windows are "about" or
322// "preferences".
323- (TabWindowController*)keyWindowTabController {
324 NSWindowController* keyWindowController =
[email protected]5dbabd02009-09-17 20:44:13325 [[NSApp keyWindow] windowController];
[email protected]449dd2f2009-05-27 13:04:00326 if ([keyWindowController isKindOfClass:[TabWindowController class]])
327 return (TabWindowController*)keyWindowController;
328
329 return nil;
330}
331
[email protected]74c0c642009-09-22 21:25:50332// Helper routine to get the window controller if the main window is a tabbed
333// window, or nil if not. Examples of non-tabbed windows are "about" or
334// "preferences".
335- (TabWindowController*)mainWindowTabController {
336 NSWindowController* mainWindowController =
337 [[NSApp mainWindow] windowController];
338 if ([mainWindowController isKindOfClass:[TabWindowController class]])
339 return (TabWindowController*)mainWindowController;
340
341 return nil;
342}
343
[email protected]8cfbdbd2011-06-16 04:17:17344// If the window has a tab controller, make "close window" be cmd-shift-w,
345// otherwise leave it as the normal cmd-w. Capitalization of the key equivalent
346// affects whether the shift modifer is used.
347- (void)adjustCloseWindowMenuItemKeyEquivalent:(BOOL)hasTabs {
348 [closeWindowMenuItem_ setKeyEquivalent:(hasTabs ? @"W" : @"w")];
349 [closeWindowMenuItem_ setKeyEquivalentModifierMask:NSCommandKeyMask];
350}
351
352// If the window has a tab controller, make "close tab" take over cmd-w,
353// otherwise it shouldn't have any key-equivalent because it should be disabled.
354- (void)adjustCloseTabMenuItemKeyEquivalent:(BOOL)hasTabs {
355 if (hasTabs) {
356 [closeTabMenuItem_ setKeyEquivalent:@"w"];
357 [closeTabMenuItem_ setKeyEquivalentModifierMask:NSCommandKeyMask];
358 } else {
359 [closeTabMenuItem_ setKeyEquivalent:@""];
360 [closeTabMenuItem_ setKeyEquivalentModifierMask:0];
361 }
362}
363
364// Explicitly remove any command-key equivalents from the close tab/window
365// menus so that nothing can go haywire if we get a user action during pending
366// updates.
367- (void)clearCloseMenuItemKeyEquivalents {
368 [closeTabMenuItem_ setKeyEquivalent:@""];
369 [closeTabMenuItem_ setKeyEquivalentModifierMask:0];
370 [closeWindowMenuItem_ setKeyEquivalent:@""];
371 [closeWindowMenuItem_ setKeyEquivalentModifierMask:0];
372}
373
374// See if we have a window with tabs open, and adjust the key equivalents for
375// Close Tab/Close Window accordingly.
376- (void)fixCloseMenuItemKeyEquivalents:(NSWindow*)window {
377 fileMenuUpdatePending_ = NO;
378 TabWindowController* tabController = [self keyWindowTabController];
379 if (!tabController && ![NSApp keyWindow]) {
380 // There might be a small amount of time where there is no key window,
381 // so just use our main browser window if there is one.
382 tabController = [self mainWindowTabController];
383 }
384 BOOL hasTabs = !!tabController;
385
386 [self adjustCloseWindowMenuItemKeyEquivalent:hasTabs];
387 [self adjustCloseTabMenuItemKeyEquivalent:hasTabs];
388}
389
390// Fix up the "close tab/close window" command-key equivalents. We do this
391// after a delay to ensure that window layer state has been set by the time
392// we do the enabling. This should only be called on the main thread, code that
393// calls this (even as a side-effect) from other threads needs to be fixed.
394- (void)delayedFixCloseMenuItemKeyEquivalents:(NSNotification*)notify {
395 DCHECK([NSThread isMainThread]);
396 if (!fileMenuUpdatePending_) {
397 // The OS prefers keypresses to timers, so it's possible that a cmd-w
398 // can sneak in before this timer fires. In order to prevent that from
399 // having any bad consequences, just clear the keys combos altogether. They
400 // will be reset when the timer eventually fires.
401 if ([NSThread isMainThread]) {
402 fileMenuUpdatePending_ = YES;
403 [self clearCloseMenuItemKeyEquivalents];
404 [self performSelector:@selector(fixCloseMenuItemKeyEquivalents:)
405 withObject:[notify object]
406 afterDelay:0];
407 } else {
408 // This shouldn't be happening, but if it does, force it to the main
409 // thread to avoid dropping the update. Don't mess with
410 // |fileMenuUpdatePending_| as it's not expected to be threadsafe and
411 // there could be a race between the selector finishing and setting the
412 // flag.
413 [self
414 performSelectorOnMainThread:@selector(fixCloseMenuItemKeyEquivalents:)
415 withObject:[notify object]
416 waitUntilDone:NO];
417 }
418 }
419}
420
[email protected]449dd2f2009-05-27 13:04:00421// Called when we get a notification about the window layering changing to
422// update the UI based on the new main window.
423- (void)windowLayeringDidChange:(NSNotification*)notify {
[email protected]8cfbdbd2011-06-16 04:17:17424 [self delayedFixCloseMenuItemKeyEquivalents:notify];
425
[email protected]dd6ab1282010-07-20 01:07:32426 if ([notify name] == NSWindowDidResignKeyNotification) {
427 // If a window is closed, this notification is fired but |[NSApp keyWindow]|
428 // returns nil regardless of whether any suitable candidates for the key
429 // window remain. It seems that the new key window for the app is not set
430 // until after this notification is fired, so a check is performed after the
431 // run loop is allowed to spin.
432 [self performSelector:@selector(checkForAnyKeyWindows)
433 withObject:nil
434 afterDelay:0.0];
435 }
[email protected]5c593702011-06-17 18:35:24436
437 // If the window changed to a new BrowserWindowController, update the profile.
438 id windowController = [[notify object] windowController];
439 if ([windowController isKindOfClass:[BrowserWindowController class]]) {
440 // If the profile is incognito, use the original profile.
441 Profile* newProfile = [windowController profile]->GetOriginalProfile();
442 [self windowChangedToProfile:newProfile];
443 }
444}
445
446// Called when the user has changed browser windows, meaning the backing profile
447// may have changed. This can cause a rebuild of the user-data menus. This is a
448// no-op if the new profile is the same as the current one. This will always be
449// the original profile and never incognito.
450- (void)windowChangedToProfile:(Profile*)profile {
451 if (lastProfile_ == profile)
452 return;
453
454 // Before tearing down the menu controller bridges, return the Cocoa menus to
455 // their initial state.
456 if (bookmarkMenuBridge_.get())
457 bookmarkMenuBridge_->ResetMenu();
458 if (historyMenuBridge_.get())
459 historyMenuBridge_->ResetMenu();
460
461 // Rebuild the menus with the new profile.
462 lastProfile_ = profile;
463
464 bookmarkMenuBridge_.reset(new BookmarkMenuBridge(lastProfile_));
465 bookmarkMenuBridge_->BuildMenu();
466
467 historyMenuBridge_.reset(new HistoryMenuBridge(lastProfile_));
468 historyMenuBridge_->BuildMenu();
[email protected]dd6ab1282010-07-20 01:07:32469}
470
471- (void)checkForAnyKeyWindows {
472 if ([NSApp keyWindow])
473 return;
474
475 NotificationService::current()->Notify(
476 NotificationType::NO_KEY_WINDOW,
477 NotificationService::AllSources(),
478 NotificationService::NoDetails());
[email protected]449dd2f2009-05-27 13:04:00479}
480
[email protected]414fde592009-05-21 16:14:43481// If the auto-update interval is not set, make it 5 hours.
482// This code is specific to Mac Chrome Dev Channel.
483// Placed here for 2 reasons:
484// 1) Same spot as other Pref stuff
485// 2) Try and be friendly by keeping this after app launch
486// TODO(jrg): remove once we go Beta.
487- (void)setUpdateCheckInterval {
488#if defined(GOOGLE_CHROME_BUILD)
489 CFStringRef app = (CFStringRef)@"com.google.Keystone.Agent";
490 CFStringRef checkInterval = (CFStringRef)@"checkInterval";
491 CFPropertyListRef plist = CFPreferencesCopyAppValue(checkInterval, app);
492 if (!plist) {
493 const float fiveHoursInSeconds = 5.0 * 60.0 * 60.0;
[email protected]7dc8c6b2010-04-09 17:02:50494 NSNumber* value = [NSNumber numberWithFloat:fiveHoursInSeconds];
[email protected]414fde592009-05-21 16:14:43495 CFPreferencesSetAppValue(checkInterval, value, app);
496 CFPreferencesAppSynchronize(app);
497 }
498#endif
499}
500
[email protected]136140c2009-05-19 13:58:25501// This is called after profiles have been loaded and preferences registered.
502// It is safe to access the default profile here.
503- (void)applicationDidFinishLaunching:(NSNotification*)notify {
[email protected]c984d9f2010-07-20 20:52:20504 // Notify BrowserList to keep the application running so it doesn't go away
505 // when all the browser windows get closed.
506 BrowserList::StartKeepAlive();
[email protected]136140c2009-05-19 13:58:25507
[email protected]414fde592009-05-21 16:14:43508 [self setUpdateCheckInterval];
[email protected]2bcec612009-05-14 17:50:53509
510 // Build up the encoding menu, the order of the items differs based on the
511 // current locale (see http://crbug.com/7647 for details).
512 // We need a valid g_browser_process to get the profile which is why we can't
513 // call this from awakeFromNib.
[email protected]6f91e5ae2011-03-21 19:53:14514 NSMenu* viewMenu = [[[NSApp mainMenu] itemWithTag:IDC_VIEW_MENU] submenu];
515 NSMenuItem* encodingMenuItem = [viewMenu itemWithTag:IDC_ENCODING_MENU];
516 NSMenu* encodingMenu = [encodingMenuItem submenu];
[email protected]a585a8db2011-06-20 18:58:35517 EncodingMenuControllerDelegate::BuildEncodingMenu([self lastProfile],
[email protected]6f91e5ae2011-03-21 19:53:14518 encodingMenu);
[email protected]2bcec612009-05-14 17:50:53519
[email protected]f2c20fa2009-12-01 17:42:02520 // Since Chrome is localized to more languages than the OS, tell Cocoa which
521 // menu is the Help so it can add the search item to it.
[email protected]caadfca2010-01-22 21:51:21522 if (helpMenu_ && [NSApp respondsToSelector:@selector(setHelpMenu:)])
[email protected]f2c20fa2009-12-01 17:42:02523 [NSApp setHelpMenu:helpMenu_];
[email protected]f2c20fa2009-12-01 17:42:02524
[email protected]12dc3d42010-02-22 23:37:12525 // Record the path to the (browser) app bundle; this is used by the app mode
526 // shim.
527 RecordLastRunAppBundlePath();
528
[email protected]eef99c22010-08-17 05:55:16529 // Makes "Services" menu items available.
530 [self registerServicesMenuTypesTo:[notify object]];
531
[email protected]caadfca2010-01-22 21:51:21532 startupComplete_ = YES;
533
534 // TODO(viettrungluu): This is very temporary, since this should be done "in"
535 // |BrowserMain()|, i.e., this list of startup URLs should be appended to the
536 // (probably-empty) list of URLs from the command line.
[email protected]d630d7d52010-02-21 00:55:11537 if (startupUrls_.size()) {
538 [self openUrls:startupUrls_];
539 [self clearStartupUrls];
[email protected]caadfca2010-01-22 21:51:21540 }
[email protected]aac169d2011-03-18 19:53:03541
542 const CommandLine& parsed_command_line = *CommandLine::ForCurrentProcess();
543 if (!parsed_command_line.HasSwitch(switches::kEnableExposeForTabs)) {
544 [tabposeMenuItem_ setHidden:YES];
545 }
[email protected]7c321082009-02-09 15:35:47546}
547
[email protected]a96ec6a2009-11-04 17:27:08548// This is called after profiles have been loaded and preferences registered.
549// It is safe to access the default profile here.
550- (void)applicationDidBecomeActive:(NSNotification*)notify {
551 NotificationService::current()->Notify(NotificationType::APP_ACTIVATED,
552 NotificationService::AllSources(),
553 NotificationService::NoDetails());
554}
555
[email protected]73f5b362009-08-10 23:58:21556// Helper function for populating and displaying the in progress downloads at
557// exit alert panel.
558- (BOOL)userWillWaitForInProgressDownloads:(int)downloadCount {
[email protected]e8b5f7882009-09-14 20:59:13559 NSString* warningText = nil;
560 NSString* explanationText = nil;
[email protected]73f5b362009-08-10 23:58:21561 NSString* waitTitle = nil;
562 NSString* exitTitle = nil;
563
[email protected]757127052010-04-27 14:42:26564 string16 product_name = l10n_util::GetStringUTF16(IDS_PRODUCT_NAME);
[email protected]9dcb59662009-09-23 01:15:50565
[email protected]73f5b362009-08-10 23:58:21566 // Set the dialog text based on whether or not there are multiple downloads.
567 if (downloadCount == 1) {
[email protected]e8b5f7882009-09-14 20:59:13568 // Dialog text: warning and explanation.
[email protected]757127052010-04-27 14:42:26569 warningText = l10n_util::GetNSStringF(
570 IDS_SINGLE_DOWNLOAD_REMOVE_CONFIRM_WARNING, product_name);
571 explanationText = l10n_util::GetNSStringF(
572 IDS_SINGLE_DOWNLOAD_REMOVE_CONFIRM_EXPLANATION, product_name);
[email protected]73f5b362009-08-10 23:58:21573
574 // Cancel download and exit button text.
[email protected]757127052010-04-27 14:42:26575 exitTitle = l10n_util::GetNSString(
576 IDS_SINGLE_DOWNLOAD_REMOVE_CONFIRM_OK_BUTTON_LABEL);
[email protected]73f5b362009-08-10 23:58:21577
578 // Wait for download button text.
[email protected]757127052010-04-27 14:42:26579 waitTitle = l10n_util::GetNSString(
580 IDS_SINGLE_DOWNLOAD_REMOVE_CONFIRM_CANCEL_BUTTON_LABEL);
[email protected]73f5b362009-08-10 23:58:21581 } else {
[email protected]e8b5f7882009-09-14 20:59:13582 // Dialog text: warning and explanation.
[email protected]757127052010-04-27 14:42:26583 warningText = l10n_util::GetNSStringF(
584 IDS_MULTIPLE_DOWNLOADS_REMOVE_CONFIRM_WARNING, product_name,
[email protected]528c56d2010-07-30 19:28:44585 base::IntToString16(downloadCount));
[email protected]757127052010-04-27 14:42:26586 explanationText = l10n_util::GetNSStringF(
587 IDS_MULTIPLE_DOWNLOADS_REMOVE_CONFIRM_EXPLANATION, product_name);
[email protected]73f5b362009-08-10 23:58:21588
589 // Cancel downloads and exit button text.
[email protected]757127052010-04-27 14:42:26590 exitTitle = l10n_util::GetNSString(
591 IDS_MULTIPLE_DOWNLOADS_REMOVE_CONFIRM_OK_BUTTON_LABEL);
[email protected]73f5b362009-08-10 23:58:21592
593 // Wait for downloads button text.
[email protected]757127052010-04-27 14:42:26594 waitTitle = l10n_util::GetNSString(
595 IDS_MULTIPLE_DOWNLOADS_REMOVE_CONFIRM_CANCEL_BUTTON_LABEL);
[email protected]73f5b362009-08-10 23:58:21596 }
597
598 // 'waitButton' is the default choice.
[email protected]e8b5f7882009-09-14 20:59:13599 int choice = NSRunAlertPanel(warningText, explanationText,
600 waitTitle, exitTitle, nil);
[email protected]73f5b362009-08-10 23:58:21601 return choice == NSAlertDefaultReturn ? YES : NO;
602}
603
604// Check all profiles for in progress downloads, and if we find any, prompt the
[email protected]09729a552009-08-03 23:21:41605// user to see if we should continue to exit (and thus cancel the downloads), or
606// if we should wait.
607- (BOOL)shouldQuitWithInProgressDownloads {
[email protected]73f5b362009-08-10 23:58:21608 ProfileManager* profile_manager = g_browser_process->profile_manager();
609 if (!profile_manager)
610 return YES;
[email protected]09729a552009-08-03 23:21:41611
[email protected]844a1002011-04-19 11:37:21612 std::vector<Profile*> profiles(profile_manager->GetLoadedProfiles());
613 for (size_t i = 0; i < profiles.size(); ++i) {
614 DownloadManager* download_manager = profiles[i]->GetDownloadManager();
[email protected]73f5b362009-08-10 23:58:21615 if (download_manager && download_manager->in_progress_count() > 0) {
616 int downloadCount = download_manager->in_progress_count();
617 if ([self userWillWaitForInProgressDownloads:downloadCount]) {
618 // Create a new browser window (if necessary) and navigate to the
619 // downloads page if the user chooses to wait.
[email protected]844a1002011-04-19 11:37:21620 Browser* browser = BrowserList::FindBrowserWithProfile(profiles[i]);
[email protected]73f5b362009-08-10 23:58:21621 if (!browser) {
[email protected]844a1002011-04-19 11:37:21622 browser = Browser::Create(profiles[i]);
[email protected]73f5b362009-08-10 23:58:21623 browser->window()->Show();
624 }
625 DCHECK(browser);
626 browser->ShowDownloadsTab();
627 return NO;
628 }
[email protected]09729a552009-08-03 23:21:41629
[email protected]73f5b362009-08-10 23:58:21630 // User wants to exit.
631 return YES;
[email protected]09729a552009-08-03 23:21:41632 }
[email protected]09729a552009-08-03 23:21:41633 }
634
[email protected]73f5b362009-08-10 23:58:21635 // No profiles or active downloads found, okay to exit.
[email protected]09729a552009-08-03 23:21:41636 return YES;
637}
638
[email protected]fbc947b2009-06-19 13:28:24639// Called to determine if we should enable the "restore tab" menu item.
640// Checks with the TabRestoreService to see if there's anything there to
641// restore and returns YES if so.
642- (BOOL)canRestoreTab {
[email protected]92371eb2011-04-28 11:50:15643 TabRestoreService* service =
[email protected]a585a8db2011-06-20 18:58:35644 TabRestoreServiceFactory::GetForProfile([self lastProfile]);
[email protected]fbc947b2009-06-19 13:28:24645 return service && !service->entries().empty();
646}
647
[email protected]f6c15fa2010-06-04 20:08:21648// Returns true if there is not a modal window (either window- or application-
649// modal) blocking the active browser. Note that tab modal dialogs (HTTP auth
650// sheets) will not count as blocking the browser. But things like open/save
651// dialogs that are window modal will block the browser.
652- (BOOL)keyWindowIsNotModal {
[email protected]6a281332009-10-13 17:41:06653 Browser* browser = BrowserList::GetLastActive();
[email protected]dc76e8362010-06-11 16:29:52654 return [NSApp modalWindow] == nil && (!browser ||
[email protected]6a281332009-10-13 17:41:06655 ![[browser->window()->GetNativeHandle() attachedSheet]
[email protected]f6c15fa2010-06-04 20:08:21656 isKindOfClass:[NSWindow class]]);
[email protected]6a281332009-10-13 17:41:06657}
658
[email protected]88d74942009-01-21 22:04:44659// Called to validate menu items when there are no key windows. All the
660// items we care about have been set with the |commandDispatch:| action and
661// a target of FirstResponder in IB. If it's not one of those, let it
662// continue up the responder chain to be handled elsewhere. We pull out the
663// tag as the cross-platform constant to differentiate and dispatch the
664// various commands.
665- (BOOL)validateUserInterfaceItem:(id<NSValidatedUserInterfaceItem>)item {
666 SEL action = [item action];
667 BOOL enable = NO;
668 if (action == @selector(commandDispatch:)) {
669 NSInteger tag = [item tag];
[email protected]fbc947b2009-06-19 13:28:24670 if (menuState_->SupportsCommand(tag)) {
671 switch (tag) {
[email protected]419eb2a2009-11-11 16:26:26672 // The File Menu commands are not automatically disabled by Cocoa when a
673 // dialog sheet obscures the browser window, so we disable several of
674 // them here. We don't need to include IDC_CLOSE_WINDOW, because
675 // app_controller is only activated when there are no key windows (see
676 // function comment).
[email protected]fbc947b2009-06-19 13:28:24677 case IDC_RESTORE_TAB:
[email protected]f6c15fa2010-06-04 20:08:21678 enable = [self keyWindowIsNotModal] && [self canRestoreTab];
[email protected]6a281332009-10-13 17:41:06679 break;
[email protected]f6c15fa2010-06-04 20:08:21680 // Browser-level items that open in new tabs should not open if there's
681 // a window- or app-modal dialog.
[email protected]6a281332009-10-13 17:41:06682 case IDC_OPEN_FILE:
[email protected]6a281332009-10-13 17:41:06683 case IDC_NEW_TAB:
[email protected]f6c15fa2010-06-04 20:08:21684 case IDC_SHOW_HISTORY:
685 case IDC_SHOW_BOOKMARK_MANAGER:
686 enable = [self keyWindowIsNotModal];
687 break;
688 // Browser-level items that open in new windows.
689 case IDC_NEW_WINDOW:
690 case IDC_TASK_MANAGER:
691 // Allow the user to open a new window if there's a window-modal
692 // dialog.
693 enable = [self keyWindowIsNotModal] || ([NSApp modalWindow] == nil);
[email protected]fbc947b2009-06-19 13:28:24694 break;
[email protected]446e16a2010-05-03 21:23:58695 case IDC_SYNC_BOOKMARKS: {
[email protected]a585a8db2011-06-20 18:58:35696 Profile* lastProfile = [self lastProfile];
[email protected]446e16a2010-05-03 21:23:58697 // The profile may be NULL during shutdown -- see
698 // http://code.google.com/p/chromium/issues/detail?id=43048 .
699 //
700 // TODO(akalin,viettrungluu): Figure out whether this method
[email protected]a585a8db2011-06-20 18:58:35701 // can be prevented from being called if lastProfile is
[email protected]446e16a2010-05-03 21:23:58702 // NULL.
[email protected]a585a8db2011-06-20 18:58:35703 if (!lastProfile) {
[email protected]446e16a2010-05-03 21:23:58704 LOG(WARNING)
[email protected]a585a8db2011-06-20 18:58:35705 << "NULL lastProfile detected -- not doing anything";
[email protected]446e16a2010-05-03 21:23:58706 break;
707 }
[email protected]a585a8db2011-06-20 18:58:35708 enable = lastProfile->IsSyncAccessible() &&
[email protected]d3b98c82010-07-14 07:45:59709 [self keyWindowIsNotModal];
[email protected]a585a8db2011-06-20 18:58:35710 sync_ui_util::UpdateSyncItem(item, enable, lastProfile);
[email protected]f9bc9b92009-11-24 00:55:35711 break;
[email protected]446e16a2010-05-03 21:23:58712 }
[email protected]fbc947b2009-06-19 13:28:24713 default:
[email protected]f6c15fa2010-06-04 20:08:21714 enable = menuState_->IsCommandEnabled(tag) ?
715 [self keyWindowIsNotModal] : NO;
[email protected]fbc947b2009-06-19 13:28:24716 }
717 }
[email protected]3b6aa8b62009-09-15 21:36:11718 } else if (action == @selector(terminate:)) {
[email protected]88d74942009-01-21 22:04:44719 enable = YES;
[email protected]3111f08b2009-04-30 16:01:52720 } else if (action == @selector(showPreferences:)) {
721 enable = YES;
[email protected]bde3dda2009-05-20 22:13:07722 } else if (action == @selector(orderFrontStandardAboutPanel:)) {
723 enable = YES;
[email protected]93b59fc2010-12-21 20:00:47724 } else if (action == @selector(commandFromDock:)) {
[email protected]3d4bd23f2009-10-06 03:58:38725 enable = YES;
[email protected]a99fce0e2011-03-21 20:58:48726 } else if (action == @selector(toggleConfirmToQuit:)) {
727 [self updateConfirmToQuitPrefMenuItem:static_cast<NSMenuItem*>(item)];
728 enable = YES;
[email protected]88d74942009-01-21 22:04:44729 }
730 return enable;
731}
732
[email protected]947fc0d2010-01-14 22:33:05733// Called when the user picks a menu item when there are no key windows, or when
734// there is no foreground browser window. Calls through to the browser object to
735// execute the command. This assumes that the command is supported and doesn't
[email protected]d630d7d52010-02-21 00:55:11736// check, otherwise it should have been disabled in the UI in
737// |-validateUserInterfaceItem:|.
[email protected]88d74942009-01-21 22:04:44738- (void)commandDispatch:(id)sender {
[email protected]a585a8db2011-06-20 18:58:35739 Profile* lastProfile = [self lastProfile];
[email protected]f0a51fb52009-03-05 12:46:38740
[email protected]947fc0d2010-01-14 22:33:05741 // Handle the case where we're dispatching a command from a sender that's in a
742 // browser window. This means that the command came from a background window
743 // and is getting here because the foreground window is not a browser window.
744 if ([sender respondsToSelector:@selector(window)]) {
745 id delegate = [[sender window] windowController];
746 if ([delegate isKindOfClass:[BrowserWindowController class]]) {
747 [delegate commandDispatch:sender];
748 return;
749 }
750 }
751
[email protected]88d74942009-01-21 22:04:44752 NSInteger tag = [sender tag];
753 switch (tag) {
[email protected]fbc947b2009-06-19 13:28:24754 case IDC_NEW_TAB:
[email protected]d630d7d52010-02-21 00:55:11755 // Create a new tab in an existing browser window (which we activate) if
756 // possible.
[email protected]a585a8db2011-06-20 18:58:35757 if (Browser* browser = ActivateBrowser(lastProfile)) {
[email protected]d630d7d52010-02-21 00:55:11758 browser->ExecuteCommand(IDC_NEW_TAB);
759 break;
760 }
761 // Else fall through to create new window.
[email protected]88d74942009-01-21 22:04:44762 case IDC_NEW_WINDOW:
[email protected]a585a8db2011-06-20 18:58:35763 CreateBrowser(lastProfile);
[email protected]d630d7d52010-02-21 00:55:11764 break;
[email protected]91a5b3e2009-10-30 19:32:22765 case IDC_FOCUS_LOCATION:
[email protected]a585a8db2011-06-20 18:58:35766 ActivateOrCreateBrowser(lastProfile)->ExecuteCommand(IDC_FOCUS_LOCATION);
[email protected]88d74942009-01-21 22:04:44767 break;
[email protected]cddc5242009-12-07 18:54:33768 case IDC_FOCUS_SEARCH:
[email protected]a585a8db2011-06-20 18:58:35769 ActivateOrCreateBrowser(lastProfile)->ExecuteCommand(IDC_FOCUS_SEARCH);
[email protected]cddc5242009-12-07 18:54:33770 break;
[email protected]863ff662009-01-26 20:18:18771 case IDC_NEW_INCOGNITO_WINDOW:
[email protected]a585a8db2011-06-20 18:58:35772 Browser::OpenEmptyWindow(lastProfile->GetOffTheRecordProfile());
[email protected]863ff662009-01-26 20:18:18773 break;
[email protected]fbc947b2009-06-19 13:28:24774 case IDC_RESTORE_TAB:
[email protected]a585a8db2011-06-20 18:58:35775 Browser::OpenWindowWithRestoredTabs(lastProfile);
[email protected]fbc947b2009-06-19 13:28:24776 break;
[email protected]e19516d2009-04-28 17:15:19777 case IDC_OPEN_FILE:
[email protected]a585a8db2011-06-20 18:58:35778 CreateBrowser(lastProfile)->ExecuteCommand(IDC_OPEN_FILE);
[email protected]e19516d2009-04-28 17:15:19779 break;
[email protected]1fdff702009-10-22 00:36:18780 case IDC_CLEAR_BROWSING_DATA: {
[email protected]93e181762009-05-29 14:30:38781 // There may not be a browser open, so use the default profile.
[email protected]a585a8db2011-06-20 18:58:35782 if (Browser* browser = ActivateBrowser(lastProfile)) {
[email protected]445177652011-03-09 02:04:05783 browser->OpenClearBrowsingDataDialog();
[email protected]c8de64a2011-01-25 17:10:23784 } else {
[email protected]a585a8db2011-06-20 18:58:35785 Browser::OpenClearBrowingDataDialogWindow(lastProfile);
[email protected]c8de64a2011-01-25 17:10:23786 }
[email protected]93e181762009-05-29 14:30:38787 break;
[email protected]1fdff702009-10-22 00:36:18788 }
[email protected]4aee5652009-11-19 18:59:21789 case IDC_IMPORT_SETTINGS: {
[email protected]a585a8db2011-06-20 18:58:35790 if (Browser* browser = ActivateBrowser(lastProfile)) {
[email protected]445177652011-03-09 02:04:05791 browser->OpenImportSettingsDialog();
[email protected]c8de64a2011-01-25 17:10:23792 } else {
[email protected]a585a8db2011-06-20 18:58:35793 Browser::OpenImportSettingsDialogWindow(lastProfile);
[email protected]c8de64a2011-01-25 17:10:23794 }
[email protected]4aee5652009-11-19 18:59:21795 break;
796 }
[email protected]af7c25e2010-01-23 05:46:45797 case IDC_SHOW_BOOKMARK_MANAGER:
[email protected]afd1e522011-04-27 23:29:59798 UserMetrics::RecordAction(UserMetricsAction("ShowBookmarkManager"));
[email protected]a585a8db2011-06-20 18:58:35799 if (Browser* browser = ActivateBrowser(lastProfile)) {
[email protected]47e532b2010-04-23 17:46:40800 // Open a bookmark manager tab.
[email protected]90a8bf252010-03-12 00:09:47801 browser->OpenBookmarkManager();
802 } else {
[email protected]47e532b2010-04-23 17:46:40803 // No browser window, so create one for the bookmark manager tab.
[email protected]a585a8db2011-06-20 18:58:35804 Browser::OpenBookmarkManagerWindow(lastProfile);
[email protected]90a8bf252010-03-12 00:09:47805 }
[email protected]af7c25e2010-01-23 05:46:45806 break;
[email protected]91a5b3e2009-10-30 19:32:22807 case IDC_SHOW_HISTORY:
[email protected]a585a8db2011-06-20 18:58:35808 if (Browser* browser = ActivateBrowser(lastProfile))
[email protected]d630d7d52010-02-21 00:55:11809 browser->ShowHistoryTab();
810 else
[email protected]a585a8db2011-06-20 18:58:35811 Browser::OpenHistoryWindow(lastProfile);
[email protected]91a5b3e2009-10-30 19:32:22812 break;
813 case IDC_SHOW_DOWNLOADS:
[email protected]a585a8db2011-06-20 18:58:35814 if (Browser* browser = ActivateBrowser(lastProfile))
[email protected]d630d7d52010-02-21 00:55:11815 browser->ShowDownloadsTab();
816 else
[email protected]a585a8db2011-06-20 18:58:35817 Browser::OpenDownloadsWindow(lastProfile);
[email protected]91a5b3e2009-10-30 19:32:22818 break;
[email protected]bb37e6ec2009-11-18 17:59:36819 case IDC_MANAGE_EXTENSIONS:
[email protected]a585a8db2011-06-20 18:58:35820 if (Browser* browser = ActivateBrowser(lastProfile))
[email protected]d630d7d52010-02-21 00:55:11821 browser->ShowExtensionsTab();
822 else
[email protected]a585a8db2011-06-20 18:58:35823 Browser::OpenExtensionsWindow(lastProfile);
[email protected]bb37e6ec2009-11-18 17:59:36824 break;
[email protected]1fdff702009-10-22 00:36:18825 case IDC_HELP_PAGE:
[email protected]a585a8db2011-06-20 18:58:35826 if (Browser* browser = ActivateBrowser(lastProfile))
[email protected]202802e42011-07-06 11:40:46827 browser->ShowHelpTab();
[email protected]d630d7d52010-02-21 00:55:11828 else
[email protected]a585a8db2011-06-20 18:58:35829 Browser::OpenHelpWindow(lastProfile);
[email protected]1fdff702009-10-22 00:36:18830 break;
[email protected]1c41a932010-11-22 23:41:23831 case IDC_FEEDBACK: {
[email protected]3896b182009-11-03 16:15:32832 Browser* browser = BrowserList::GetLastActive();
[email protected]d630d7d52010-02-21 00:55:11833 TabContents* currentTab =
834 browser ? browser->GetSelectedTabContents() : NULL;
[email protected]3896b182009-11-03 16:15:32835 BugReportWindowController* controller =
836 [[BugReportWindowController alloc]
[email protected]d630d7d52010-02-21 00:55:11837 initWithTabContents:currentTab
[email protected]a585a8db2011-06-20 18:58:35838 profile:[self lastProfile]];
[email protected]3896b182009-11-03 16:15:32839 [controller runModalDialog];
840 break;
841 }
[email protected]f9bc9b92009-11-24 00:55:35842 case IDC_SYNC_BOOKMARKS:
[email protected]446e16a2010-05-03 21:23:58843 // The profile may be NULL during shutdown -- see
844 // http://code.google.com/p/chromium/issues/detail?id=43048 .
845 //
846 // TODO(akalin,viettrungluu): Figure out whether this method can
[email protected]a585a8db2011-06-20 18:58:35847 // be prevented from being called if lastProfile is NULL.
848 if (!lastProfile) {
849 LOG(WARNING) << "NULL lastProfile detected -- not doing anything";
[email protected]446e16a2010-05-03 21:23:58850 break;
851 }
[email protected]f9bc9b92009-11-24 00:55:35852 // TODO(akalin): Add a constant to denote starting sync from the
853 // main menu and use that instead of START_FROM_WRENCH.
[email protected]40f047972009-11-25 03:54:40854 sync_ui_util::OpenSyncMyBookmarksDialog(
[email protected]a585a8db2011-06-20 18:58:35855 lastProfile, ActivateBrowser(lastProfile),
[email protected]c8de64a2011-01-25 17:10:23856 ProfileSyncService::START_FROM_WRENCH);
[email protected]f9bc9b92009-11-24 00:55:35857 break;
[email protected]ceaaf1812010-01-20 22:13:09858 case IDC_TASK_MANAGER:
[email protected]afd1e522011-04-27 23:29:59859 UserMetrics::RecordAction(UserMetricsAction("TaskManager"));
[email protected]adb6a84d2011-02-07 16:58:40860 TaskManagerMac::Show(false);
[email protected]ceaaf1812010-01-20 22:13:09861 break;
[email protected]4a42d272010-06-18 01:29:42862 case IDC_OPTIONS:
863 [self showPreferences:sender];
864 break;
[email protected]93b59fc2010-12-21 20:00:47865 default:
866 // Background Applications use dynamic values that must be less than the
867 // smallest value among the predefined IDC_* labels.
868 if ([sender tag] < IDC_MinimumLabelValue)
869 [self executeApplication:sender];
870 break;
[email protected]d630d7d52010-02-21 00:55:11871 }
[email protected]88d74942009-01-21 22:04:44872}
873
[email protected]93b59fc2010-12-21 20:00:47874// Run a (background) application in a new tab.
875- (void)executeApplication:(id)sender {
876 NSInteger tag = [sender tag];
[email protected]a585a8db2011-06-20 18:58:35877 Profile* profile = [self lastProfile];
[email protected]93b59fc2010-12-21 20:00:47878 DCHECK(profile);
879 BackgroundApplicationListModel applications(profile);
880 DCHECK(tag >= 0 &&
881 tag < static_cast<int>(applications.size()));
882 Browser* browser = BrowserList::GetLastActive();
883 if (!browser) {
884 Browser::OpenEmptyWindow(profile);
885 browser = BrowserList::GetLastActive();
886 }
887 const Extension* extension = applications.GetExtension(tag);
[email protected]7e41c2b2011-05-06 10:31:16888 browser->OpenApplicationTab(profile, extension, NEW_FOREGROUND_TAB);
[email protected]93b59fc2010-12-21 20:00:47889}
890
[email protected]947fc0d2010-01-14 22:33:05891// Same as |-commandDispatch:|, but executes commands using a disposition
892// determined by the key flags. This will get called in the case where the
893// frontmost window is not a browser window, and the user has command-clicked
894// a button in a background browser window whose action is
895// |-commandDispatchUsingKeyModifiers:|
896- (void)commandDispatchUsingKeyModifiers:(id)sender {
897 DCHECK(sender);
898 if ([sender respondsToSelector:@selector(window)]) {
899 id delegate = [[sender window] windowController];
900 if ([delegate isKindOfClass:[BrowserWindowController class]]) {
901 [delegate commandDispatchUsingKeyModifiers:sender];
902 }
903 }
904}
905
[email protected]ff81e0a2009-04-13 14:58:54906// NSApplication delegate method called when someone clicks on the
907// dock icon and there are no open windows. To match standard mac
908// behavior, we should open a new window.
909- (BOOL)applicationShouldHandleReopen:(NSApplication*)theApplication
910 hasVisibleWindows:(BOOL)flag {
[email protected]70631402010-04-26 20:16:54911 // If the browser is currently trying to quit, don't do anything and return NO
912 // to prevent AppKit from doing anything.
913 // TODO(rohitrao): Remove this code when http://crbug.com/40861 is resolved.
914 if (browser_shutdown::IsTryingToQuit())
915 return NO;
916
[email protected]ff81e0a2009-04-13 14:58:54917 // Don't do anything if there are visible windows. This will cause
918 // AppKit to unminimize the most recently minimized window.
919 if (flag)
920 return YES;
921
[email protected]0d1210d62010-09-07 17:31:38922 // If launched as a hidden login item (due to installation of a persistent app
923 // or by the user, for example in System Preferenecs->Accounts->Login Items),
924 // allow session to be restored first time the user clicks on a Dock icon.
925 // Normally, it'd just open a new empty page.
926 {
927 static BOOL doneOnce = NO;
928 if (!doneOnce) {
929 doneOnce = YES;
[email protected]0378bf42011-01-01 18:20:14930 if (base::mac::WasLaunchedAsHiddenLoginItem()) {
[email protected]0d1210d62010-09-07 17:31:38931 SessionService* sessionService =
[email protected]a585a8db2011-06-20 18:58:35932 SessionServiceFactory::GetForProfile([self lastProfile]);
[email protected]0d1210d62010-09-07 17:31:38933 if (sessionService &&
934 sessionService->RestoreIfNecessary(std::vector<GURL>()))
935 return NO;
936 }
937 }
938 }
[email protected]ff81e0a2009-04-13 14:58:54939 // Otherwise open a new window.
[email protected]d630d7d52010-02-21 00:55:11940 {
[email protected]0fbd70332010-06-01 19:28:34941 AutoReset<bool> auto_reset_in_run(&g_is_opening_new_window, true);
[email protected]a585a8db2011-06-20 18:58:35942 Browser::OpenEmptyWindow([self lastProfile]);
[email protected]d630d7d52010-02-21 00:55:11943 }
[email protected]ff81e0a2009-04-13 14:58:54944
945 // We've handled the reopen event, so return NO to tell AppKit not
946 // to do anything.
947 return NO;
948}
949
[email protected]88d74942009-01-21 22:04:44950- (void)initMenuState {
[email protected]3111f08b2009-04-30 16:01:52951 menuState_.reset(new CommandUpdater(NULL));
[email protected]fbc947b2009-06-19 13:28:24952 menuState_->UpdateCommandEnabled(IDC_NEW_TAB, true);
[email protected]88d74942009-01-21 22:04:44953 menuState_->UpdateCommandEnabled(IDC_NEW_WINDOW, true);
[email protected]863ff662009-01-26 20:18:18954 menuState_->UpdateCommandEnabled(IDC_NEW_INCOGNITO_WINDOW, true);
[email protected]e19516d2009-04-28 17:15:19955 menuState_->UpdateCommandEnabled(IDC_OPEN_FILE, true);
[email protected]93e181762009-05-29 14:30:38956 menuState_->UpdateCommandEnabled(IDC_CLEAR_BROWSING_DATA, true);
[email protected]fbc947b2009-06-19 13:28:24957 menuState_->UpdateCommandEnabled(IDC_RESTORE_TAB, false);
[email protected]91a5b3e2009-10-30 19:32:22958 menuState_->UpdateCommandEnabled(IDC_FOCUS_LOCATION, true);
[email protected]cddc5242009-12-07 18:54:33959 menuState_->UpdateCommandEnabled(IDC_FOCUS_SEARCH, true);
[email protected]af7c25e2010-01-23 05:46:45960 menuState_->UpdateCommandEnabled(IDC_SHOW_BOOKMARK_MANAGER, true);
[email protected]91a5b3e2009-10-30 19:32:22961 menuState_->UpdateCommandEnabled(IDC_SHOW_HISTORY, true);
962 menuState_->UpdateCommandEnabled(IDC_SHOW_DOWNLOADS, true);
[email protected]bb37e6ec2009-11-18 17:59:36963 menuState_->UpdateCommandEnabled(IDC_MANAGE_EXTENSIONS, true);
[email protected]1fdff702009-10-22 00:36:18964 menuState_->UpdateCommandEnabled(IDC_HELP_PAGE, true);
[email protected]4aee5652009-11-19 18:59:21965 menuState_->UpdateCommandEnabled(IDC_IMPORT_SETTINGS, true);
[email protected]1c41a932010-11-22 23:41:23966 menuState_->UpdateCommandEnabled(IDC_FEEDBACK, true);
[email protected]d3b98c82010-07-14 07:45:59967 menuState_->UpdateCommandEnabled(IDC_SYNC_BOOKMARKS, true);
[email protected]ceaaf1812010-01-20 22:13:09968 menuState_->UpdateCommandEnabled(IDC_TASK_MANAGER, true);
[email protected]88d74942009-01-21 22:04:44969}
[email protected]1bcdb532009-01-16 17:47:57970
[email protected]a99fce0e2011-03-21 20:58:48971// The Confirm to Quit preference is atypical in that the preference lives in
972// the app menu right above the Quit menu item. This method will refresh the
973// display of that item depending on the preference state.
974- (void)updateConfirmToQuitPrefMenuItem:(NSMenuItem*)item {
975 // Format the string so that the correct key equivalent is displayed.
976 NSString* acceleratorString = [ConfirmQuitPanelController keyCommandString];
977 NSString* title = l10n_util::GetNSStringF(IDS_CONFIRM_TO_QUIT_OPTION,
978 base::SysNSStringToUTF16(acceleratorString));
979 [item setTitle:title];
980
[email protected]a585a8db2011-06-20 18:58:35981 const PrefService* prefService = [self lastProfile]->GetPrefs();
[email protected]a99fce0e2011-03-21 20:58:48982 bool enabled = prefService->GetBoolean(prefs::kConfirmToQuitEnabled);
983 [item setState:enabled ? NSOnState : NSOffState];
984}
985
[email protected]eef99c22010-08-17 05:55:16986- (void)registerServicesMenuTypesTo:(NSApplication*)app {
[email protected]09ea24c2010-08-23 19:05:41987 // Note that RenderWidgetHostViewCocoa implements NSServicesRequests which
988 // handles requests from services.
989 NSArray* types = [NSArray arrayWithObjects:NSStringPboardType, nil];
990 [app registerServicesMenuSendTypes:types returnTypes:types];
[email protected]eef99c22010-08-17 05:55:16991}
992
[email protected]a585a8db2011-06-20 18:58:35993- (Profile*)lastProfile {
[email protected]7fd5a462011-06-19 16:57:23994 // Return the profile of the last-used BrowserWindowController, if available.
995 if (lastProfile_)
996 return lastProfile_;
997
998 // Fallback to the default if this is the first Browser.
[email protected]3f34599d2009-03-25 22:11:43999 if (g_browser_process->profile_manager())
[email protected]844a1002011-04-19 11:37:211000 return g_browser_process->profile_manager()->GetDefaultProfile();
[email protected]3f34599d2009-03-25 22:11:431001
[email protected]f6314002009-04-23 01:18:131002 return NULL;
[email protected]3f34599d2009-03-25 22:11:431003}
1004
[email protected]57750f822009-04-21 21:43:091005// Various methods to open URLs that we get in a native fashion. We use
1006// BrowserInit here because on the other platforms, URLs to open come through
1007// the ProcessSingleton, and it calls BrowserInit. It's best to bottleneck the
1008// openings through that for uniform handling.
1009
[email protected]d630d7d52010-02-21 00:55:111010- (void)openUrls:(const std::vector<GURL>&)urls {
[email protected]caadfca2010-01-22 21:51:211011 // If the browser hasn't started yet, just queue up the URLs.
1012 if (!startupComplete_) {
[email protected]d630d7d52010-02-21 00:55:111013 startupUrls_.insert(startupUrls_.end(), urls.begin(), urls.end());
[email protected]136140c2009-05-19 13:58:251014 return;
1015 }
[email protected]57750f822009-04-21 21:43:091016
[email protected]0e863512009-05-28 19:45:071017 Browser* browser = BrowserList::GetLastActive();
1018 // if no browser window exists then create one with no tabs to be filled in
1019 if (!browser) {
[email protected]a585a8db2011-06-20 18:58:351020 browser = Browser::Create([self lastProfile]);
[email protected]ce560f82009-06-03 09:39:441021 browser->window()->Show();
[email protected]0e863512009-05-28 19:45:071022 }
1023
[email protected]947446b2010-10-21 03:36:311024 CommandLine dummy(CommandLine::NO_PROGRAM);
[email protected]f805fe82010-08-03 22:47:101025 BrowserInit::LaunchWithProfile launch(FilePath(), dummy);
[email protected]0e863512009-05-28 19:45:071026 launch.OpenURLsInBrowser(browser, false, urls);
[email protected]57750f822009-04-21 21:43:091027}
1028
[email protected]57750f822009-04-21 21:43:091029- (void)getUrl:(NSAppleEventDescriptor*)event
1030 withReply:(NSAppleEventDescriptor*)reply {
1031 NSString* urlStr = [[event paramDescriptorForKeyword:keyDirectObject]
1032 stringValue];
1033
1034 GURL gurl(base::SysNSStringToUTF8(urlStr));
1035 std::vector<GURL> gurlVector;
1036 gurlVector.push_back(gurl);
1037
[email protected]d630d7d52010-02-21 00:55:111038 [self openUrls:gurlVector];
[email protected]57750f822009-04-21 21:43:091039}
1040
[email protected]caadfca2010-01-22 21:51:211041- (void)application:(NSApplication*)sender
1042 openFiles:(NSArray*)filenames {
[email protected]57750f822009-04-21 21:43:091043 std::vector<GURL> gurlVector;
[email protected]caadfca2010-01-22 21:51:211044 for (NSString* file in filenames) {
1045 GURL gurl = net::FilePathToFileURL(FilePath(base::SysNSStringToUTF8(file)));
[email protected]57750f822009-04-21 21:43:091046 gurlVector.push_back(gurl);
1047 }
[email protected]caadfca2010-01-22 21:51:211048 if (!gurlVector.empty())
[email protected]d630d7d52010-02-21 00:55:111049 [self openUrls:gurlVector];
[email protected]caadfca2010-01-22 21:51:211050 else
1051 NOTREACHED() << "Nothing to open!";
[email protected]57750f822009-04-21 21:43:091052
[email protected]caadfca2010-01-22 21:51:211053 [sender replyToOpenOrPrint:NSApplicationDelegateReplySuccess];
[email protected]57750f822009-04-21 21:43:091054}
[email protected]3f34599d2009-03-25 22:11:431055
[email protected]3111f08b2009-04-30 16:01:521056// Show the preferences window, or bring it to the front if it's already
1057// visible.
1058- (IBAction)showPreferences:(id)sender {
[email protected]a585a8db2011-06-20 18:58:351059 if (Browser* browser = ActivateBrowser([self lastProfile])) {
[email protected]445177652011-03-09 02:04:051060 // Show options tab in the active browser window.
1061 browser->OpenOptionsDialog();
[email protected]4a42d272010-06-18 01:29:421062 } else {
[email protected]445177652011-03-09 02:04:051063 // No browser window, so create one for the options tab.
[email protected]a585a8db2011-06-20 18:58:351064 Browser::OpenOptionsWindow([self lastProfile]);
[email protected]4a42d272010-06-18 01:29:421065 }
[email protected]d989f07c2009-11-14 00:35:461066}
1067
[email protected]bde3dda2009-05-20 22:13:071068// Called when the about window is closed. We use this to release the
1069// window controller.
[email protected]d4e18652009-11-19 21:52:161070- (void)aboutWindowClosed:(NSNotification*)notification {
1071 NSWindow* window = [aboutController_ window];
[email protected]2ce9c89752011-02-25 18:24:341072 DCHECK_EQ([notification object], window);
[email protected]bde3dda2009-05-20 22:13:071073 [[NSNotificationCenter defaultCenter]
[email protected]49aeee52009-10-26 19:58:131074 removeObserver:self
[email protected]d4e18652009-11-19 21:52:161075 name:NSWindowWillCloseNotification
1076 object:window];
1077 // AboutWindowControllers are autoreleased in
1078 // -[AboutWindowController windowWillClose:].
1079 aboutController_ = nil;
[email protected]bde3dda2009-05-20 22:13:071080}
1081
1082- (IBAction)orderFrontStandardAboutPanel:(id)sender {
[email protected]bde3dda2009-05-20 22:13:071083 if (!aboutController_) {
[email protected]d4e18652009-11-19 21:52:161084 aboutController_ =
[email protected]a585a8db2011-06-20 18:58:351085 [[AboutWindowController alloc] initWithProfile:[self lastProfile]];
[email protected]49aeee52009-10-26 19:58:131086
[email protected]bde3dda2009-05-20 22:13:071087 // Watch for a notification of when it goes away so that we can destroy
1088 // the controller.
1089 [[NSNotificationCenter defaultCenter]
1090 addObserver:self
1091 selector:@selector(aboutWindowClosed:)
[email protected]d4e18652009-11-19 21:52:161092 name:NSWindowWillCloseNotification
1093 object:[aboutController_ window]];
[email protected]bde3dda2009-05-20 22:13:071094 }
[email protected]49aeee52009-10-26 19:58:131095
[email protected]bde3dda2009-05-20 22:13:071096 [aboutController_ showWindow:self];
[email protected]bde3dda2009-05-20 22:13:071097}
1098
[email protected]a99fce0e2011-03-21 20:58:481099- (IBAction)toggleConfirmToQuit:(id)sender {
[email protected]a585a8db2011-06-20 18:58:351100 PrefService* prefService = [self lastProfile]->GetPrefs();
[email protected]a99fce0e2011-03-21 20:58:481101 bool enabled = prefService->GetBoolean(prefs::kConfirmToQuitEnabled);
1102 prefService->SetBoolean(prefs::kConfirmToQuitEnabled, !enabled);
1103}
1104
[email protected]3d4bd23f2009-10-06 03:58:381105// Explicitly bring to the foreground when creating new windows from the dock.
[email protected]93b59fc2010-12-21 20:00:471106- (void)commandFromDock:(id)sender {
[email protected]3d4bd23f2009-10-06 03:58:381107 [NSApp activateIgnoringOtherApps:YES];
1108 [self commandDispatch:sender];
1109}
1110
[email protected]227fcee2010-06-11 19:19:371111- (NSMenu*)applicationDockMenu:(NSApplication*)sender {
[email protected]018a3962009-09-17 22:23:441112 NSMenu* dockMenu = [[[NSMenu alloc] initWithTitle: @""] autorelease];
[email protected]a585a8db2011-06-20 18:58:351113 Profile* profile = [self lastProfile];
[email protected]93b59fc2010-12-21 20:00:471114
[email protected]018a3962009-09-17 22:23:441115 NSString* titleStr = l10n_util::GetNSStringWithFixup(IDS_NEW_WINDOW_MAC);
[email protected]8c6b029d2011-01-21 22:39:511116 scoped_nsobject<NSMenuItem> item(
1117 [[NSMenuItem alloc] initWithTitle:titleStr
1118 action:@selector(commandFromDock:)
1119 keyEquivalent:@""]);
[email protected]288bfcd32009-09-14 18:14:461120 [item setTarget:self];
1121 [item setTag:IDC_NEW_WINDOW];
[email protected]018a3962009-09-17 22:23:441122 [dockMenu addItem:item];
[email protected]288bfcd32009-09-14 18:14:461123
1124 titleStr = l10n_util::GetNSStringWithFixup(IDS_NEW_INCOGNITO_WINDOW_MAC);
[email protected]018a3962009-09-17 22:23:441125 item.reset([[NSMenuItem alloc] initWithTitle:titleStr
[email protected]8c6b029d2011-01-21 22:39:511126 action:@selector(commandFromDock:)
[email protected]018a3962009-09-17 22:23:441127 keyEquivalent:@""]);
[email protected]288bfcd32009-09-14 18:14:461128 [item setTarget:self];
1129 [item setTag:IDC_NEW_INCOGNITO_WINDOW];
[email protected]018a3962009-09-17 22:23:441130 [dockMenu addItem:item];
[email protected]288bfcd32009-09-14 18:14:461131
[email protected]8c6b029d2011-01-21 22:39:511132 // TODO(rickcam): Mock out BackgroundApplicationListModel, then add unit
1133 // tests which use the mock in place of the profile-initialized model.
1134
1135 // Avoid breaking unit tests which have no profile.
1136 if (profile) {
1137 BackgroundApplicationListModel applications(profile);
1138 if (applications.size()) {
1139 int position = 0;
1140 NSString* menuStr =
1141 l10n_util::GetNSStringWithFixup(IDS_BACKGROUND_APPS_MAC);
1142 scoped_nsobject<NSMenu> appMenu([[NSMenu alloc] initWithTitle:menuStr]);
1143 for (ExtensionList::const_iterator cursor = applications.begin();
1144 cursor != applications.end();
1145 ++cursor, ++position) {
[email protected]2ce9c89752011-02-25 18:24:341146 DCHECK_EQ(applications.GetPosition(*cursor), position);
[email protected]8c6b029d2011-01-21 22:39:511147 NSString* itemStr =
1148 base::SysUTF16ToNSString(UTF8ToUTF16((*cursor)->name()));
1149 scoped_nsobject<NSMenuItem> appItem([[NSMenuItem alloc]
1150 initWithTitle:itemStr
1151 action:@selector(commandFromDock:)
1152 keyEquivalent:@""]);
1153 [appItem setTarget:self];
1154 [appItem setTag:position];
1155 [appMenu addItem:appItem];
1156 }
1157 scoped_nsobject<NSMenuItem> appMenuItem([[NSMenuItem alloc]
1158 initWithTitle:menuStr
1159 action:@selector(commandFromDock:)
1160 keyEquivalent:@""]);
1161 [appMenuItem setTarget:self];
1162 [appMenuItem setTag:position];
1163 [appMenuItem setSubmenu:appMenu];
1164 [dockMenu addItem:appMenuItem];
1165 }
1166 }
1167
[email protected]018a3962009-09-17 22:23:441168 return dockMenu;
[email protected]288bfcd32009-09-14 18:14:461169}
1170
[email protected]d630d7d52010-02-21 00:55:111171- (const std::vector<GURL>&)startupUrls {
1172 return startupUrls_;
[email protected]caadfca2010-01-22 21:51:211173}
1174
[email protected]d630d7d52010-02-21 00:55:111175- (void)clearStartupUrls {
1176 startupUrls_.clear();
[email protected]caadfca2010-01-22 21:51:211177}
1178
[email protected]7e4c69d02011-05-11 22:16:471179- (BookmarkMenuBridge*)bookmarkMenuBridge {
1180 return bookmarkMenuBridge_.get();
1181}
1182
[email protected]d630d7d52010-02-21 00:55:111183@end // @implementation AppController
[email protected]60ad3e22009-09-18 21:07:191184
1185//---------------------------------------------------------------------------
1186
[email protected]0934de82011-03-09 18:42:161187namespace browser {
1188
1189void ShowInstantConfirmDialog(gfx::NativeWindow parent, Profile* profile) {
1190 if (Browser* browser = ActivateBrowser(profile)) {
[email protected]b99081b2011-03-10 17:43:471191 browser->OpenInstantConfirmDialog();
[email protected]0934de82011-03-09 18:42:161192 } else {
[email protected]b99081b2011-03-10 17:43:471193 Browser::OpenInstantConfirmDialogWindow(profile);
[email protected]0934de82011-03-09 18:42:161194 }
1195}
1196
1197} // namespace browser
1198
[email protected]216e9042009-10-29 17:05:231199namespace app_controller_mac {
1200
1201bool IsOpeningNewWindow() {
1202 return g_is_opening_new_window;
1203}
1204
1205} // namespace app_controller_mac