[email protected] | 71c0eb9 | 2012-01-03 17:57:30 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 1bcdb53 | 2009-01-16 17:47:57 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | 2c47bc1 | 2009-04-10 20:14:00 | [diff] [blame] | 5 | #import "chrome/browser/app_controller_mac.h" |
[email protected] | 1bcdb53 | 2009-01-16 17:47:57 | [diff] [blame] | 6 | |
[email protected] | 90a4b9d | 2013-07-31 12:14:40 | [diff] [blame] | 7 | #include "apps/app_shim/extension_app_shim_handler_mac.h" |
[email protected] | d630d7d5 | 2010-02-21 00:55:11 | [diff] [blame] | 8 | #include "base/auto_reset.h" |
[email protected] | 0b8caad | 2011-11-28 23:33:27 | [diff] [blame] | 9 | #include "base/bind.h" |
[email protected] | 57750f82 | 2009-04-21 21:43:09 | [diff] [blame] | 10 | #include "base/command_line.h" |
[email protected] | 5799981 | 2013-02-24 05:40:52 | [diff] [blame] | 11 | #include "base/files/file_path.h" |
[email protected] | 151c4a6 | 2011-04-22 04:15:13 | [diff] [blame] | 12 | #include "base/mac/foundation_util.h" |
[email protected] | 0378bf4 | 2011-01-01 18:20:14 | [diff] [blame] | 13 | #include "base/mac/mac_util.h" |
[email protected] | 2f30167 | 2014-06-07 00:58:52 | [diff] [blame] | 14 | #include "base/mac/sdk_forward_declarations.h" |
[email protected] | fa1e0e1 | 2013-07-18 00:10:14 | [diff] [blame] | 15 | #include "base/message_loop/message_loop.h" |
[email protected] | 3853a4c | 2013-02-11 17:15:57 | [diff] [blame] | 16 | #include "base/prefs/pref_service.h" |
[email protected] | 3ea1b18 | 2013-02-08 22:38:41 | [diff] [blame] | 17 | #include "base/strings/string_number_conversions.h" |
[email protected] | 3268d7b7 | 2013-03-28 17:41:43 | [diff] [blame] | 18 | #include "base/strings/sys_string_conversions.h" |
[email protected] | 135cb80 | 2013-06-09 16:44:20 | [diff] [blame] | 19 | #include "base/strings/utf_string_conversions.h" |
[email protected] | 1a3aba8 | 2010-11-08 23:52:54 | [diff] [blame] | 20 | #include "chrome/app/chrome_command_ids.h" |
[email protected] | a07676b2 | 2011-06-17 16:36:53 | [diff] [blame] | 21 | #include "chrome/browser/background/background_application_list_model.h" |
[email protected] | 78d78a6 | 2011-09-29 18:24:33 | [diff] [blame] | 22 | #include "chrome/browser/background/background_mode_manager.h" |
[email protected] | 5c23875 | 2009-06-13 10:29:07 | [diff] [blame] | 23 | #include "chrome/browser/browser_process.h" |
[email protected] | 41741a96 | 2009-02-18 21:51:39 | [diff] [blame] | 24 | #include "chrome/browser/browser_shutdown.h" |
[email protected] | fdf40f3e | 2013-07-11 23:55:46 | [diff] [blame] | 25 | #include "chrome/browser/chrome_notification_types.h" |
[email protected] | 2c47bc1 | 2009-04-10 20:14:00 | [diff] [blame] | 26 | #include "chrome/browser/command_updater.h" |
[email protected] | 9bb54ee | 2011-10-12 17:43:35 | [diff] [blame] | 27 | #include "chrome/browser/download/download_service.h" |
| 28 | #include "chrome/browser/download/download_service_factory.h" |
[email protected] | ccfab59 | 2013-01-15 06:24:32 | [diff] [blame] | 29 | #include "chrome/browser/extensions/extension_service.h" |
[email protected] | e4a377cf | 2011-10-05 02:51:49 | [diff] [blame] | 30 | #include "chrome/browser/first_run/first_run.h" |
[email protected] | 2e6389f | 2012-05-18 19:41:25 | [diff] [blame] | 31 | #include "chrome/browser/lifetime/application_lifetime.h" |
[email protected] | 7d1aaa6 | 2014-07-18 02:21:30 | [diff] [blame] | 32 | #include "chrome/browser/mac/mac_startup_profiler.h" |
[email protected] | 1c62b2f | 2013-06-28 00:15:00 | [diff] [blame] | 33 | #include "chrome/browser/profiles/profile_info_cache_observer.h" |
[email protected] | 8ecad5e | 2010-12-02 21:18:33 | [diff] [blame] | 34 | #include "chrome/browser/profiles/profile_manager.h" |
[email protected] | de71ae99 | 2013-07-18 03:30:38 | [diff] [blame] | 35 | #include "chrome/browser/profiles/profiles_state.h" |
[email protected] | 8d4bbdc | 2012-09-20 21:36:49 | [diff] [blame] | 36 | #include "chrome/browser/sessions/session_restore.h" |
[email protected] | d2912a2 | 2011-03-15 15:20:50 | [diff] [blame] | 37 | #include "chrome/browser/sessions/session_service.h" |
[email protected] | 92371eb | 2011-04-28 11:50:15 | [diff] [blame] | 38 | #include "chrome/browser/sessions/session_service_factory.h" |
[email protected] | fbc947b | 2009-06-19 13:28:24 | [diff] [blame] | 39 | #include "chrome/browser/sessions/tab_restore_service.h" |
[email protected] | 92371eb | 2011-04-28 11:50:15 | [diff] [blame] | 40 | #include "chrome/browser/sessions/tab_restore_service_factory.h" |
[email protected] | 074311a | 2013-02-28 23:14:09 | [diff] [blame] | 41 | #include "chrome/browser/signin/signin_manager_factory.h" |
[email protected] | 3d27d27 | 2013-07-31 03:15:16 | [diff] [blame] | 42 | #include "chrome/browser/signin/signin_promo.h" |
[email protected] | f9bc9b9 | 2009-11-24 00:55:35 | [diff] [blame] | 43 | #include "chrome/browser/sync/profile_sync_service.h" |
[email protected] | 40f04797 | 2009-11-25 03:54:40 | [diff] [blame] | 44 | #include "chrome/browser/sync/sync_ui_util.h" |
[email protected] | ae04f59 | 2010-11-18 20:41:35 | [diff] [blame] | 45 | #include "chrome/browser/ui/browser.h" |
[email protected] | 0545453 | 2013-01-22 21:09:08 | [diff] [blame] | 46 | #include "chrome/browser/ui/browser_command_controller.h" |
[email protected] | a37d4b0 | 2012-06-25 21:56:10 | [diff] [blame] | 47 | #include "chrome/browser/ui/browser_commands.h" |
[email protected] | 7108d91 | 2014-01-30 08:10:45 | [diff] [blame] | 48 | #include "chrome/browser/ui/browser_dialogs.h" |
[email protected] | d874814 | 2012-05-16 21:13:43 | [diff] [blame] | 49 | #include "chrome/browser/ui/browser_finder.h" |
[email protected] | b4207c4 | 2013-02-12 06:44:20 | [diff] [blame] | 50 | #include "chrome/browser/ui/browser_iterator.h" |
[email protected] | a13b76f | 2012-06-20 15:36:29 | [diff] [blame] | 51 | #include "chrome/browser/ui/browser_mac.h" |
[email protected] | ae04f59 | 2010-11-18 20:41:35 | [diff] [blame] | 52 | #include "chrome/browser/ui/browser_window.h" |
[email protected] | 5d9cace7 | 2012-06-21 16:07:12 | [diff] [blame] | 53 | #include "chrome/browser/ui/chrome_pages.h" |
[email protected] | edb2d03 | 2013-08-30 09:03:17 | [diff] [blame] | 54 | #import "chrome/browser/ui/cocoa/apps/app_shim_menu_controller_mac.h" |
[email protected] | 425f7a0 | 2014-04-29 01:51:44 | [diff] [blame] | 55 | #include "chrome/browser/ui/cocoa/apps/quit_with_apps_controller_mac.h" |
[email protected] | 7d79165 | 2010-12-01 16:34:49 | [diff] [blame] | 56 | #import "chrome/browser/ui/cocoa/bookmarks/bookmark_menu_bridge.h" |
| 57 | #import "chrome/browser/ui/cocoa/browser_window_cocoa.h" |
| 58 | #import "chrome/browser/ui/cocoa/browser_window_controller.h" |
[email protected] | b6366ffa | 2012-02-29 17:12:26 | [diff] [blame] | 59 | #import "chrome/browser/ui/cocoa/confirm_quit.h" |
[email protected] | 7d79165 | 2010-12-01 16:34:49 | [diff] [blame] | 60 | #import "chrome/browser/ui/cocoa/confirm_quit_panel_controller.h" |
| 61 | #import "chrome/browser/ui/cocoa/encoding_menu_controller_delegate_mac.h" |
| 62 | #import "chrome/browser/ui/cocoa/history_menu_bridge.h" |
[email protected] | 4feb37d9 | 2012-07-01 20:22:13 | [diff] [blame] | 63 | #include "chrome/browser/ui/cocoa/last_active_browser_cocoa.h" |
[email protected] | ced445e9 | 2014-04-09 21:43:59 | [diff] [blame] | 64 | #import "chrome/browser/ui/cocoa/profiles/profile_menu_controller.h" |
[email protected] | cfe1d61 | 2011-01-19 20:06:47 | [diff] [blame] | 65 | #import "chrome/browser/ui/cocoa/tabs/tab_strip_controller.h" |
| 66 | #import "chrome/browser/ui/cocoa/tabs/tab_window_controller.h" |
[email protected] | 7d79165 | 2010-12-01 16:34:49 | [diff] [blame] | 67 | #include "chrome/browser/ui/cocoa/task_manager_mac.h" |
[email protected] | ccfab59 | 2013-01-15 06:24:32 | [diff] [blame] | 68 | #include "chrome/browser/ui/extensions/application_launch.h" |
[email protected] | 3b26531 | 2013-01-17 02:49:55 | [diff] [blame] | 69 | #include "chrome/browser/ui/host_desktop.h" |
[email protected] | 4feb37d9 | 2012-07-01 20:22:13 | [diff] [blame] | 70 | #include "chrome/browser/ui/startup/startup_browser_creator.h" |
| 71 | #include "chrome/browser/ui/startup/startup_browser_creator_impl.h" |
mlerman | e29d003 | 2014-09-24 19:31:26 | [diff] [blame^] | 72 | #include "chrome/browser/ui/user_manager.h" |
[email protected] | 12dc3d4 | 2010-02-22 23:37:12 | [diff] [blame] | 73 | #include "chrome/common/chrome_paths_internal.h" |
[email protected] | f4acfae8 | 2009-09-11 00:33:38 | [diff] [blame] | 74 | #include "chrome/common/chrome_switches.h" |
[email protected] | 13aec6c4 | 2011-10-27 21:22:08 | [diff] [blame] | 75 | #include "chrome/common/cloud_print/cloud_print_class_mac.h" |
[email protected] | ccfab59 | 2013-01-15 06:24:32 | [diff] [blame] | 76 | #include "chrome/common/extensions/extension_constants.h" |
[email protected] | 6c583d3 | 2011-08-19 14:46:22 | [diff] [blame] | 77 | #include "chrome/common/mac/app_mode_common.h" |
[email protected] | a99fce0e | 2011-03-21 20:58:48 | [diff] [blame] | 78 | #include "chrome/common/pref_names.h" |
[email protected] | 024617a | 2010-08-20 05:08:05 | [diff] [blame] | 79 | #include "chrome/common/url_constants.h" |
[email protected] | af39f00 | 2014-08-22 10:18:18 | [diff] [blame] | 80 | #include "chrome/grit/chromium_strings.h" |
| 81 | #include "chrome/grit/generated_resources.h" |
[email protected] | 7fbd3b1 | 2014-04-01 11:19:16 | [diff] [blame] | 82 | #include "components/signin/core/browser/signin_manager.h" |
[email protected] | af39f00 | 2014-08-22 10:18:18 | [diff] [blame] | 83 | #include "components/signin/core/common/profile_management_switches.h" |
[email protected] | c38831a1 | 2011-10-28 12:44:49 | [diff] [blame] | 84 | #include "content/public/browser/browser_thread.h" |
[email protected] | e582fdd | 2011-12-20 16:48:17 | [diff] [blame] | 85 | #include "content/public/browser/download_manager.h" |
[email protected] | ad50def5 | 2011-10-19 23:17:07 | [diff] [blame] | 86 | #include "content/public/browser/notification_service.h" |
[email protected] | 0d6e9bd | 2011-10-18 04:29:16 | [diff] [blame] | 87 | #include "content/public/browser/notification_types.h" |
[email protected] | 5904cb4 | 2012-09-24 15:05:20 | [diff] [blame] | 88 | #include "content/public/browser/plugin_service.h" |
[email protected] | 7f6f44c | 2011-12-14 13:23:38 | [diff] [blame] | 89 | #include "content/public/browser/user_metrics.h" |
hashimoto | ad3c687 | 2014-08-29 09:46:57 | [diff] [blame] | 90 | #include "extensions/browser/app_window/app_window_registry.h" |
[email protected] | 59b0e60 | 2014-01-30 00:41:24 | [diff] [blame] | 91 | #include "extensions/browser/extension_system.h" |
[email protected] | d96cf75 | 2014-04-09 04:05:28 | [diff] [blame] | 92 | #include "net/base/filename_util.h" |
[email protected] | 2e29e223 | 2013-07-26 10:40:59 | [diff] [blame] | 93 | #include "ui/base/cocoa/focus_window_set.h" |
[email protected] | c051a1b | 2011-01-21 23:30:17 | [diff] [blame] | 94 | #include "ui/base/l10n/l10n_util.h" |
| 95 | #include "ui/base/l10n/l10n_util_mac.h" |
[email protected] | 88d7494 | 2009-01-21 22:04:44 | [diff] [blame] | 96 | |
[email protected] | e6e30ac | 2014-01-13 21:24:39 | [diff] [blame] | 97 | using base::UserMetricsAction; |
[email protected] | b441a849 | 2012-06-06 14:55:57 | [diff] [blame] | 98 | using content::BrowserContext; |
[email protected] | 631bb74 | 2011-11-02 11:29:39 | [diff] [blame] | 99 | using content::BrowserThread; |
[email protected] | e582fdd | 2011-12-20 16:48:17 | [diff] [blame] | 100 | using content::DownloadManager; |
[email protected] | 631bb74 | 2011-11-02 11:29:39 | [diff] [blame] | 101 | |
[email protected] | d630d7d5 | 2010-02-21 00:55:11 | [diff] [blame] | 102 | namespace { |
| 103 | |
[email protected] | ebbe94b | 2012-01-18 20:11:56 | [diff] [blame] | 104 | // Declare notification names from the 10.7 SDK. |
| 105 | #if !defined(MAC_OS_X_VERSION_10_7) || \ |
| 106 | MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_7 |
| 107 | NSString* NSPopoverDidShowNotification = @"NSPopoverDidShowNotification"; |
| 108 | NSString* NSPopoverDidCloseNotification = @"NSPopoverDidCloseNotification"; |
| 109 | #endif |
| 110 | |
[email protected] | d4c811e7 | 2013-10-29 21:57:55 | [diff] [blame] | 111 | // How long we allow a workspace change notification to wait to be |
| 112 | // associated with a dock activation. The animation lasts 250ms. See |
| 113 | // applicationShouldHandleReopen:hasVisibleWindows:. |
| 114 | static const int kWorkspaceChangeTimeoutMs = 500; |
| 115 | |
[email protected] | a37d4b0 | 2012-06-25 21:56:10 | [diff] [blame] | 116 | // True while AppController is calling chrome::NewEmptyWindow(). We need a |
[email protected] | fe7c487 | 2012-05-10 20:06:03 | [diff] [blame] | 117 | // global flag here, analogue to StartupBrowserCreator::InProcessStartup() |
| 118 | // because otherwise the SessionService will try to restore sessions when we |
| 119 | // make a new window while there are no other active windows. |
[email protected] | d630d7d5 | 2010-02-21 00:55:11 | [diff] [blame] | 120 | bool g_is_opening_new_window = false; |
| 121 | |
| 122 | // Activates a browser window having the given profile (the last one active) if |
| 123 | // possible and returns a pointer to the activate |Browser| or NULL if this was |
| 124 | // not possible. If the last active browser is minimized (in particular, if |
| 125 | // there are only minimized windows), it will unminimize it. |
| 126 | Browser* ActivateBrowser(Profile* profile) { |
[email protected] | 5595ce6d | 2014-07-24 04:01:13 | [diff] [blame] | 127 | Browser* browser = chrome::FindLastActiveWithProfile( |
| 128 | profile->IsGuestSession() ? profile->GetOffTheRecordProfile() : profile, |
[email protected] | 69425607 | 2012-11-29 13:40:34 | [diff] [blame] | 129 | chrome::HOST_DESKTOP_TYPE_NATIVE); |
[email protected] | d630d7d5 | 2010-02-21 00:55:11 | [diff] [blame] | 130 | if (browser) |
| 131 | browser->window()->Activate(); |
| 132 | return browser; |
| 133 | } |
| 134 | |
| 135 | // Creates an empty browser window with the given profile and returns a pointer |
| 136 | // to the new |Browser|. |
| 137 | Browser* CreateBrowser(Profile* profile) { |
| 138 | { |
[email protected] | 997ec9f | 2012-11-21 04:44:14 | [diff] [blame] | 139 | base::AutoReset<bool> auto_reset_in_run(&g_is_opening_new_window, true); |
[email protected] | a8e3c53 | 2013-02-20 06:03:41 | [diff] [blame] | 140 | chrome::NewEmptyWindow(profile, chrome::HOST_DESKTOP_TYPE_NATIVE); |
[email protected] | d630d7d5 | 2010-02-21 00:55:11 | [diff] [blame] | 141 | } |
| 142 | |
[email protected] | f2bc6e9 | 2012-10-14 02:08:38 | [diff] [blame] | 143 | Browser* browser = chrome::GetLastActiveBrowser(); |
[email protected] | d630d7d5 | 2010-02-21 00:55:11 | [diff] [blame] | 144 | CHECK(browser); |
| 145 | return browser; |
| 146 | } |
| 147 | |
| 148 | // Activates a browser window having the given profile (the last one active) if |
| 149 | // possible or creates an empty one if necessary. Returns a pointer to the |
| 150 | // activated/new |Browser|. |
| 151 | Browser* ActivateOrCreateBrowser(Profile* profile) { |
| 152 | if (Browser* browser = ActivateBrowser(profile)) |
| 153 | return browser; |
| 154 | return CreateBrowser(profile); |
| 155 | } |
| 156 | |
[email protected] | 151c4a6 | 2011-04-22 04:15:13 | [diff] [blame] | 157 | CFStringRef BaseBundleID_CFString() { |
| 158 | NSString* base_bundle_id = |
| 159 | [NSString stringWithUTF8String:base::mac::BaseBundleID()]; |
| 160 | return base::mac::NSToCFCast(base_bundle_id); |
| 161 | } |
| 162 | |
[email protected] | 0b8caad | 2011-11-28 23:33:27 | [diff] [blame] | 163 | // This callback synchronizes preferences (under "org.chromium.Chromium" or |
[email protected] | 12dc3d4 | 2010-02-22 23:37:12 | [diff] [blame] | 164 | // "com.google.Chrome"), in particular, writes them out to disk. |
[email protected] | 0b8caad | 2011-11-28 23:33:27 | [diff] [blame] | 165 | void PrefsSyncCallback() { |
| 166 | if (!CFPreferencesAppSynchronize(BaseBundleID_CFString())) |
| 167 | LOG(WARNING) << "Error recording application bundle path."; |
| 168 | } |
[email protected] | 12dc3d4 | 2010-02-22 23:37:12 | [diff] [blame] | 169 | |
| 170 | // Record the location of the application bundle (containing the main framework) |
| 171 | // from which Chromium was loaded. This is used by app mode shims to find |
| 172 | // Chromium. |
| 173 | void RecordLastRunAppBundlePath() { |
| 174 | // Going up three levels from |chrome::GetVersionedDirectory()| gives the |
| 175 | // real, user-visible app bundle directory. (The alternatives give either the |
| 176 | // framework's path or the initial app's path, which may be an app mode shim |
| 177 | // or a unit test.) |
[email protected] | bf0d26b | 2013-08-29 03:09:14 | [diff] [blame] | 178 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 179 | |
[email protected] | 1c95868 | 2013-09-17 03:38:13 | [diff] [blame] | 180 | base::FilePath app_bundle_path = |
[email protected] | 12dc3d4 | 2010-02-22 23:37:12 | [diff] [blame] | 181 | chrome::GetVersionedDirectory().DirName().DirName().DirName(); |
[email protected] | 1c95868 | 2013-09-17 03:38:13 | [diff] [blame] | 182 | base::ScopedCFTypeRef<CFStringRef> app_bundle_path_cfstring( |
| 183 | base::SysUTF8ToCFStringRef(app_bundle_path.value())); |
[email protected] | b6b7222 | 2012-02-11 02:04:13 | [diff] [blame] | 184 | CFPreferencesSetAppValue( |
| 185 | base::mac::NSToCFCast(app_mode::kLastRunAppBundlePathPrefsKey), |
[email protected] | 1c95868 | 2013-09-17 03:38:13 | [diff] [blame] | 186 | app_bundle_path_cfstring, BaseBundleID_CFString()); |
[email protected] | 12dc3d4 | 2010-02-22 23:37:12 | [diff] [blame] | 187 | |
| 188 | // Sync after a delay avoid I/O contention on startup; 1500 ms is plenty. |
[email protected] | df32e89c | 2012-05-17 17:47:34 | [diff] [blame] | 189 | BrowserThread::PostDelayedTask( |
| 190 | BrowserThread::FILE, FROM_HERE, |
| 191 | base::Bind(&PrefsSyncCallback), |
| 192 | base::TimeDelta::FromMilliseconds(1500)); |
[email protected] | 12dc3d4 | 2010-02-22 23:37:12 | [diff] [blame] | 193 | } |
| 194 | |
[email protected] | 7108d91 | 2014-01-30 08:10:45 | [diff] [blame] | 195 | bool IsProfileSignedOut(Profile* profile) { |
| 196 | // The signed out status only makes sense at the moment in the context of the |
| 197 | // --new-profile-management flag. |
| 198 | if (!switches::IsNewProfileManagement()) |
| 199 | return false; |
| 200 | ProfileInfoCache& cache = |
| 201 | g_browser_process->profile_manager()->GetProfileInfoCache(); |
| 202 | size_t profile_index = cache.GetIndexOfProfileWithPath(profile->GetPath()); |
| 203 | if (profile_index == std::string::npos) |
| 204 | return false; |
| 205 | return cache.ProfileIsSigninRequiredAtIndex(profile_index); |
| 206 | } |
| 207 | |
[email protected] | d630d7d5 | 2010-02-21 00:55:11 | [diff] [blame] | 208 | } // anonymous namespace |
| 209 | |
[email protected] | a99fce0e | 2011-03-21 20:58:48 | [diff] [blame] | 210 | @interface AppController (Private) |
[email protected] | 88d7494 | 2009-01-21 22:04:44 | [diff] [blame] | 211 | - (void)initMenuState; |
[email protected] | 961a693 | 2011-07-19 19:52:46 | [diff] [blame] | 212 | - (void)initProfileMenu; |
[email protected] | a99fce0e | 2011-03-21 20:58:48 | [diff] [blame] | 213 | - (void)updateConfirmToQuitPrefMenuItem:(NSMenuItem*)item; |
[email protected] | e0b0d1ba | 2014-04-24 03:22:29 | [diff] [blame] | 214 | - (void)updateDisplayMessageCenterPrefMenuItem:(NSMenuItem*)item; |
[email protected] | eef99c2 | 2010-08-17 05:55:16 | [diff] [blame] | 215 | - (void)registerServicesMenuTypesTo:(NSApplication*)app; |
[email protected] | d630d7d5 | 2010-02-21 00:55:11 | [diff] [blame] | 216 | - (void)openUrls:(const std::vector<GURL>&)urls; |
[email protected] | 57750f82 | 2009-04-21 21:43:09 | [diff] [blame] | 217 | - (void)getUrl:(NSAppleEventDescriptor*)event |
| 218 | withReply:(NSAppleEventDescriptor*)reply; |
[email protected] | 449dd2f | 2009-05-27 13:04:00 | [diff] [blame] | 219 | - (void)windowLayeringDidChange:(NSNotification*)inNotification; |
[email protected] | d4c811e7 | 2013-10-29 21:57:55 | [diff] [blame] | 220 | - (void)activeSpaceDidChange:(NSNotification*)inNotification; |
[email protected] | dd6ab128 | 2010-07-20 01:07:32 | [diff] [blame] | 221 | - (void)checkForAnyKeyWindows; |
[email protected] | 73f5b36 | 2009-08-10 23:58:21 | [diff] [blame] | 222 | - (BOOL)userWillWaitForInProgressDownloads:(int)downloadCount; |
[email protected] | 09729a55 | 2009-08-03 23:21:41 | [diff] [blame] | 223 | - (BOOL)shouldQuitWithInProgressDownloads; |
[email protected] | 93b59fc | 2010-12-21 20:00:47 | [diff] [blame] | 224 | - (void)executeApplication:(id)sender; |
[email protected] | 1c62b2f | 2013-06-28 00:15:00 | [diff] [blame] | 225 | - (void)profileWasRemoved:(const base::FilePath&)profilePath; |
[email protected] | 88d7494 | 2009-01-21 22:04:44 | [diff] [blame] | 226 | @end |
[email protected] | 1bcdb53 | 2009-01-16 17:47:57 | [diff] [blame] | 227 | |
[email protected] | 1c62b2f | 2013-06-28 00:15:00 | [diff] [blame] | 228 | class AppControllerProfileObserver : public ProfileInfoCacheObserver { |
| 229 | public: |
| 230 | AppControllerProfileObserver( |
| 231 | ProfileManager* profile_manager, AppController* app_controller) |
| 232 | : profile_manager_(profile_manager), |
| 233 | app_controller_(app_controller) { |
| 234 | DCHECK(profile_manager_); |
| 235 | DCHECK(app_controller_); |
| 236 | profile_manager_->GetProfileInfoCache().AddObserver(this); |
| 237 | } |
| 238 | |
| 239 | virtual ~AppControllerProfileObserver() { |
| 240 | DCHECK(profile_manager_); |
| 241 | profile_manager_->GetProfileInfoCache().RemoveObserver(this); |
| 242 | } |
| 243 | |
| 244 | private: |
| 245 | // ProfileInfoCacheObserver implementation: |
| 246 | |
| 247 | virtual void OnProfileAdded(const base::FilePath& profile_path) OVERRIDE { |
| 248 | } |
| 249 | |
[email protected] | 9692015 | 2013-12-04 21:00:16 | [diff] [blame] | 250 | virtual void OnProfileWasRemoved( |
| 251 | const base::FilePath& profile_path, |
| 252 | const base::string16& profile_name) OVERRIDE { |
[email protected] | 1c62b2f | 2013-06-28 00:15:00 | [diff] [blame] | 253 | // When a profile is deleted we need to notify the AppController, |
| 254 | // so it can correctly update its pointer to the last used profile. |
| 255 | [app_controller_ profileWasRemoved:profile_path]; |
| 256 | } |
| 257 | |
| 258 | virtual void OnProfileWillBeRemoved( |
| 259 | const base::FilePath& profile_path) OVERRIDE { |
| 260 | } |
| 261 | |
[email protected] | 9692015 | 2013-12-04 21:00:16 | [diff] [blame] | 262 | virtual void OnProfileNameChanged( |
| 263 | const base::FilePath& profile_path, |
| 264 | const base::string16& old_profile_name) OVERRIDE { |
[email protected] | 1c62b2f | 2013-06-28 00:15:00 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | virtual void OnProfileAvatarChanged( |
| 268 | const base::FilePath& profile_path) OVERRIDE { |
| 269 | } |
| 270 | |
| 271 | ProfileManager* profile_manager_; |
| 272 | |
| 273 | AppController* app_controller_; // Weak; owns us. |
| 274 | |
| 275 | DISALLOW_COPY_AND_ASSIGN(AppControllerProfileObserver); |
| 276 | }; |
| 277 | |
[email protected] | 1bcdb53 | 2009-01-16 17:47:57 | [diff] [blame] | 278 | @implementation AppController |
| 279 | |
[email protected] | caadfca | 2010-01-22 21:51:21 | [diff] [blame] | 280 | @synthesize startupComplete = startupComplete_; |
| 281 | |
[email protected] | cd63ef6 | 2009-05-06 19:41:37 | [diff] [blame] | 282 | // This method is called very early in application startup (ie, before |
| 283 | // the profile is loaded or any preferences have been registered). Defer any |
| 284 | // user-data initialization until -applicationDidFinishLaunching:. |
[email protected] | 88d7494 | 2009-01-21 22:04:44 | [diff] [blame] | 285 | - (void)awakeFromNib { |
[email protected] | 7d1aaa6 | 2014-07-18 02:21:30 | [diff] [blame] | 286 | MacStartupProfiler::GetInstance()->Profile( |
| 287 | MacStartupProfiler::AWAKE_FROM_NIB); |
[email protected] | 136140c | 2009-05-19 13:58:25 | [diff] [blame] | 288 | // We need to register the handlers early to catch events fired on launch. |
[email protected] | 57750f82 | 2009-04-21 21:43:09 | [diff] [blame] | 289 | NSAppleEventManager* em = [NSAppleEventManager sharedAppleEventManager]; |
| 290 | [em setEventHandler:self |
| 291 | andSelector:@selector(getUrl:withReply:) |
| 292 | forEventClass:kInternetEventClass |
| 293 | andEventID:kAEGetURL]; |
| 294 | [em setEventHandler:self |
| 295 | andSelector:@selector(getUrl:withReply:) |
| 296 | forEventClass:'WWW!' // A particularly ancient AppleEvent that dates |
| 297 | andEventID:'OURL']; // back to the Spyglass days. |
[email protected] | cd63ef6 | 2009-05-06 19:41:37 | [diff] [blame] | 298 | |
[email protected] | 449dd2f | 2009-05-27 13:04:00 | [diff] [blame] | 299 | // Register for various window layering changes. We use these to update |
| 300 | // various UI elements (command-key equivalents, etc) when the frontmost |
| 301 | // window changes. |
| 302 | NSNotificationCenter* notificationCenter = |
| 303 | [NSNotificationCenter defaultCenter]; |
[email protected] | ce560f8 | 2009-06-03 09:39:44 | [diff] [blame] | 304 | [notificationCenter |
[email protected] | 449dd2f | 2009-05-27 13:04:00 | [diff] [blame] | 305 | addObserver:self |
| 306 | selector:@selector(windowLayeringDidChange:) |
| 307 | name:NSWindowDidBecomeKeyNotification |
| 308 | object:nil]; |
| 309 | [notificationCenter |
| 310 | addObserver:self |
| 311 | selector:@selector(windowLayeringDidChange:) |
| 312 | name:NSWindowDidResignKeyNotification |
| 313 | object:nil]; |
| 314 | [notificationCenter |
| 315 | addObserver:self |
| 316 | selector:@selector(windowLayeringDidChange:) |
| 317 | name:NSWindowDidBecomeMainNotification |
| 318 | object:nil]; |
| 319 | [notificationCenter |
| 320 | addObserver:self |
| 321 | selector:@selector(windowLayeringDidChange:) |
| 322 | name:NSWindowDidResignMainNotification |
| 323 | object:nil]; |
| 324 | |
[email protected] | ebbe94b | 2012-01-18 20:11:56 | [diff] [blame] | 325 | if (base::mac::IsOSLionOrLater()) { |
| 326 | [notificationCenter |
| 327 | addObserver:self |
| 328 | selector:@selector(popoverDidShow:) |
| 329 | name:NSPopoverDidShowNotification |
| 330 | object:nil]; |
| 331 | [notificationCenter |
| 332 | addObserver:self |
| 333 | selector:@selector(popoverDidClose:) |
| 334 | name:NSPopoverDidCloseNotification |
| 335 | object:nil]; |
| 336 | } |
| 337 | |
[email protected] | d4c811e7 | 2013-10-29 21:57:55 | [diff] [blame] | 338 | // Register for space change notifications. |
| 339 | [[[NSWorkspace sharedWorkspace] notificationCenter] |
| 340 | addObserver:self |
| 341 | selector:@selector(activeSpaceDidChange:) |
| 342 | name:NSWorkspaceActiveSpaceDidChangeNotification |
| 343 | object:nil]; |
| 344 | |
[email protected] | 136140c | 2009-05-19 13:58:25 | [diff] [blame] | 345 | // Set up the command updater for when there are no windows open |
| 346 | [self initMenuState]; |
[email protected] | 961a693 | 2011-07-19 19:52:46 | [diff] [blame] | 347 | |
| 348 | // Initialize the Profile menu. |
| 349 | [self initProfileMenu]; |
[email protected] | 136140c | 2009-05-19 13:58:25 | [diff] [blame] | 350 | } |
| 351 | |
[email protected] | 71c0eb9 | 2012-01-03 17:57:30 | [diff] [blame] | 352 | - (void)unregisterEventHandlers { |
| 353 | NSAppleEventManager* em = [NSAppleEventManager sharedAppleEventManager]; |
| 354 | [em removeEventHandlerForEventClass:kInternetEventClass |
| 355 | andEventID:kAEGetURL]; |
| 356 | [em removeEventHandlerForEventClass:cloud_print::kAECloudPrintClass |
| 357 | andEventID:cloud_print::kAECloudPrintClass]; |
[email protected] | 71c0eb9 | 2012-01-03 17:57:30 | [diff] [blame] | 358 | [em removeEventHandlerForEventClass:'WWW!' |
| 359 | andEventID:'OURL']; |
| 360 | [[NSNotificationCenter defaultCenter] removeObserver:self]; |
[email protected] | d4c811e7 | 2013-10-29 21:57:55 | [diff] [blame] | 361 | [[[NSWorkspace sharedWorkspace] notificationCenter] removeObserver:self]; |
[email protected] | 71c0eb9 | 2012-01-03 17:57:30 | [diff] [blame] | 362 | } |
| 363 | |
[email protected] | 7dc8c6b | 2010-04-09 17:02:50 | [diff] [blame] | 364 | // (NSApplicationDelegate protocol) This is the Apple-approved place to override |
| 365 | // the default handlers. |
| 366 | - (void)applicationWillFinishLaunching:(NSNotification*)notification { |
[email protected] | 7d1aaa6 | 2014-07-18 02:21:30 | [diff] [blame] | 367 | MacStartupProfiler::GetInstance()->Profile( |
| 368 | MacStartupProfiler::WILL_FINISH_LAUNCHING); |
[email protected] | 7dc8c6b | 2010-04-09 17:02:50 | [diff] [blame] | 369 | } |
| 370 | |
[email protected] | 8a5bd5904 | 2014-07-22 04:10:06 | [diff] [blame] | 371 | - (void)applicationWillHide:(NSNotification*)notification { |
| 372 | apps::ExtensionAppShimHandler::OnChromeWillHide(); |
| 373 | } |
| 374 | |
[email protected] | 7dc8c6b | 2010-04-09 17:02:50 | [diff] [blame] | 375 | - (BOOL)tryToTerminateApplication:(NSApplication*)app { |
[email protected] | 3076fad | 2010-04-28 18:11:45 | [diff] [blame] | 376 | // Check for in-process downloads, and prompt the user if they really want |
| 377 | // to quit (and thus cancel downloads). Only check if we're not already |
| 378 | // shutting down, else the user might be prompted multiple times if the |
| 379 | // download isn't stopped before terminate is called again. |
| 380 | if (!browser_shutdown::IsTryingToQuit() && |
| 381 | ![self shouldQuitWithInProgressDownloads]) |
| 382 | return NO; |
| 383 | |
[email protected] | 7dc8c6b | 2010-04-09 17:02:50 | [diff] [blame] | 384 | // TODO(viettrungluu): Remove Apple Event handlers here? (It's safe to leave |
| 385 | // them in, but I'm not sure about UX; we'd also want to disable other things |
| 386 | // though.) http://crbug.com/40861 |
| 387 | |
[email protected] | 177aceb | 2010-11-03 16:17:41 | [diff] [blame] | 388 | // Check if the user really wants to quit by employing the confirm-to-quit |
| 389 | // mechanism. |
| 390 | if (!browser_shutdown::IsTryingToQuit() && |
| 391 | [self applicationShouldTerminate:app] != NSTerminateNow) |
| 392 | return NO; |
| 393 | |
[email protected] | 70593e5 | 2014-05-15 08:30:23 | [diff] [blame] | 394 | // Check for active apps. If quitting is prevented, only close browsers and |
| 395 | // sessions. |
dcheng | 037fdb50 | 2014-09-20 03:22:09 | [diff] [blame] | 396 | if (!browser_shutdown::IsTryingToQuit() && quitWithAppsController_.get() && |
| 397 | !quitWithAppsController_->ShouldQuit()) { |
[email protected] | 70593e5 | 2014-05-15 08:30:23 | [diff] [blame] | 398 | content::NotificationService::current()->Notify( |
| 399 | chrome::NOTIFICATION_CLOSE_ALL_BROWSERS_REQUEST, |
| 400 | content::NotificationService::AllSources(), |
| 401 | content::NotificationService::NoDetails()); |
| 402 | // This will close all browser sessions. |
| 403 | chrome::CloseAllBrowsers(); |
| 404 | return NO; |
| 405 | } |
| 406 | |
[email protected] | 0665ebe | 2013-02-13 09:53:19 | [diff] [blame] | 407 | size_t num_browsers = chrome::GetTotalBrowserCount(); |
[email protected] | 7dc8c6b | 2010-04-09 17:02:50 | [diff] [blame] | 408 | |
[email protected] | 0c95faf4 | 2013-10-28 06:27:20 | [diff] [blame] | 409 | // Initiate a shutdown (via chrome::CloseAllBrowsersAndQuit()) if we aren't |
[email protected] | c984d9f | 2010-07-20 20:52:20 | [diff] [blame] | 410 | // already shutting down. |
[email protected] | ef61b8c | 2012-01-20 10:54:56 | [diff] [blame] | 411 | if (!browser_shutdown::IsTryingToQuit()) { |
| 412 | content::NotificationService::current()->Notify( |
[email protected] | d53a08c | 2012-07-18 20:35:30 | [diff] [blame] | 413 | chrome::NOTIFICATION_CLOSE_ALL_BROWSERS_REQUEST, |
[email protected] | ef61b8c | 2012-01-20 10:54:56 | [diff] [blame] | 414 | content::NotificationService::AllSources(), |
| 415 | content::NotificationService::NoDetails()); |
[email protected] | 0c95faf4 | 2013-10-28 06:27:20 | [diff] [blame] | 416 | chrome::CloseAllBrowsersAndQuit(); |
[email protected] | ef61b8c | 2012-01-20 10:54:56 | [diff] [blame] | 417 | } |
[email protected] | 7dc8c6b | 2010-04-09 17:02:50 | [diff] [blame] | 418 | |
[email protected] | c984d9f | 2010-07-20 20:52:20 | [diff] [blame] | 419 | return num_browsers == 0 ? YES : NO; |
[email protected] | 7dc8c6b | 2010-04-09 17:02:50 | [diff] [blame] | 420 | } |
| 421 | |
| 422 | - (void)stopTryingToTerminateApplication:(NSApplication*)app { |
| 423 | if (browser_shutdown::IsTryingToQuit()) { |
| 424 | // Reset the "trying to quit" state, so that closing all browser windows |
| 425 | // will no longer lead to termination. |
| 426 | browser_shutdown::SetTryingToQuit(false); |
| 427 | |
| 428 | // TODO(viettrungluu): Were we to remove Apple Event handlers above, we |
| 429 | // would have to reinstall them here. http://crbug.com/40861 |
| 430 | } |
[email protected] | a9e8afc | 2009-08-11 22:03:17 | [diff] [blame] | 431 | } |
| 432 | |
[email protected] | 177aceb | 2010-11-03 16:17:41 | [diff] [blame] | 433 | - (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication*)app { |
[email protected] | 90a4b9d | 2013-07-31 12:14:40 | [diff] [blame] | 434 | // If there are no windows, quit immediately. |
| 435 | if (chrome::BrowserIterator().done() && |
hashimoto | ad3c687 | 2014-08-29 09:46:57 | [diff] [blame] | 436 | !extensions::AppWindowRegistry::IsAppWindowRegisteredInAnyProfile(0)) { |
[email protected] | 90a4b9d | 2013-07-31 12:14:40 | [diff] [blame] | 437 | return NSTerminateNow; |
| 438 | } |
| 439 | |
[email protected] | a99fce0e | 2011-03-21 20:58:48 | [diff] [blame] | 440 | // Check if the preference is turned on. |
[email protected] | b6366ffa | 2012-02-29 17:12:26 | [diff] [blame] | 441 | const PrefService* prefs = g_browser_process->local_state(); |
[email protected] | 1eaddf3 | 2011-04-05 15:09:23 | [diff] [blame] | 442 | if (!prefs->GetBoolean(prefs::kConfirmToQuitEnabled)) { |
| 443 | confirm_quit::RecordHistogram(confirm_quit::kNoConfirm); |
[email protected] | 177aceb | 2010-11-03 16:17:41 | [diff] [blame] | 444 | return NSTerminateNow; |
[email protected] | 1eaddf3 | 2011-04-05 15:09:23 | [diff] [blame] | 445 | } |
[email protected] | 177aceb | 2010-11-03 16:17:41 | [diff] [blame] | 446 | |
| 447 | // If the application is going to terminate as the result of a Cmd+Q |
| 448 | // invocation, use the special sauce to prevent accidental quitting. |
| 449 | // http://dev.chromium.org/developers/design-documents/confirm-to-quit-experiment |
[email protected] | 177aceb | 2010-11-03 16:17:41 | [diff] [blame] | 450 | |
[email protected] | 2020fd0b | 2010-12-10 01:16:50 | [diff] [blame] | 451 | // This logic is only for keyboard-initiated quits. |
[email protected] | 5df54fb | 2011-03-07 18:15:26 | [diff] [blame] | 452 | if (![ConfirmQuitPanelController eventTriggersFeature:[app currentEvent]]) |
[email protected] | 2020fd0b | 2010-12-10 01:16:50 | [diff] [blame] | 453 | return NSTerminateNow; |
[email protected] | 177aceb | 2010-11-03 16:17:41 | [diff] [blame] | 454 | |
[email protected] | 5df54fb | 2011-03-07 18:15:26 | [diff] [blame] | 455 | return [[ConfirmQuitPanelController sharedController] |
| 456 | runModalLoopForApplication:app]; |
[email protected] | 177aceb | 2010-11-03 16:17:41 | [diff] [blame] | 457 | } |
| 458 | |
[email protected] | 449dd2f | 2009-05-27 13:04:00 | [diff] [blame] | 459 | // Called when the app is shutting down. Clean-up as appropriate. |
[email protected] | 7dc8c6b | 2010-04-09 17:02:50 | [diff] [blame] | 460 | - (void)applicationWillTerminate:(NSNotification*)aNotification { |
[email protected] | 7dc8c6b | 2010-04-09 17:02:50 | [diff] [blame] | 461 | // There better be no browser windows left at this point. |
[email protected] | 0665ebe | 2013-02-13 09:53:19 | [diff] [blame] | 462 | CHECK_EQ(0u, chrome::GetTotalBrowserCount()); |
[email protected] | 3b6aa8b6 | 2009-09-15 21:36:11 | [diff] [blame] | 463 | |
[email protected] | c984d9f | 2010-07-20 20:52:20 | [diff] [blame] | 464 | // Tell BrowserList not to keep the browser process alive. Once all the |
| 465 | // browsers get dealloc'd, it will stop the RunLoop and fall back into main(). |
[email protected] | 9a718b2 | 2014-02-25 15:17:27 | [diff] [blame] | 466 | chrome::DecrementKeepAliveCount(); |
[email protected] | fbc947b | 2009-06-19 13:28:24 | [diff] [blame] | 467 | |
[email protected] | 02a31dd | 2013-02-05 08:40:52 | [diff] [blame] | 468 | // Reset all pref watching, as this object outlives the prefs system. |
| 469 | profilePrefRegistrar_.reset(); |
| 470 | localPrefRegistrar_.RemoveAll(); |
| 471 | |
[email protected] | 71c0eb9 | 2012-01-03 17:57:30 | [diff] [blame] | 472 | [self unregisterEventHandlers]; |
[email protected] | edb2d03 | 2013-08-30 09:03:17 | [diff] [blame] | 473 | |
| 474 | appShimMenuController_.reset(); |
[email protected] | 449dd2f | 2009-05-27 13:04:00 | [diff] [blame] | 475 | } |
| 476 | |
[email protected] | 3b6aa8b6 | 2009-09-15 21:36:11 | [diff] [blame] | 477 | - (void)didEndMainMessageLoop { |
[email protected] | c987a24 | 2013-02-28 01:17:41 | [diff] [blame] | 478 | DCHECK_EQ(0u, chrome::GetBrowserCount([self lastProfile], |
| 479 | chrome::HOST_DESKTOP_TYPE_NATIVE)); |
| 480 | if (!chrome::GetBrowserCount([self lastProfile], |
| 481 | chrome::HOST_DESKTOP_TYPE_NATIVE)) { |
[email protected] | 3b6aa8b6 | 2009-09-15 21:36:11 | [diff] [blame] | 482 | // As we're shutting down, we need to nuke the TabRestoreService, which |
| 483 | // will start the shutdown of the NavigationControllers and allow for |
| 484 | // proper shutdown. If we don't do this, Chrome won't shut down cleanly, |
| 485 | // and may end up crashing when some thread tries to use the IO thread (or |
| 486 | // another thread) that is no longer valid. |
[email protected] | a585a8db | 2011-06-20 18:58:35 | [diff] [blame] | 487 | TabRestoreServiceFactory::ResetForProfile([self lastProfile]); |
[email protected] | 3b6aa8b6 | 2009-09-15 21:36:11 | [diff] [blame] | 488 | } |
| 489 | } |
| 490 | |
[email protected] | 8cfbdbd | 2011-06-16 04:17:17 | [diff] [blame] | 491 | // If the window has a tab controller, make "close window" be cmd-shift-w, |
| 492 | // otherwise leave it as the normal cmd-w. Capitalization of the key equivalent |
[email protected] | eddcf750 | 2012-02-09 22:43:48 | [diff] [blame] | 493 | // affects whether the shift modifier is used. |
[email protected] | ebbe94b | 2012-01-18 20:11:56 | [diff] [blame] | 494 | - (void)adjustCloseWindowMenuItemKeyEquivalent:(BOOL)enableCloseTabShortcut { |
| 495 | [closeWindowMenuItem_ setKeyEquivalent:(enableCloseTabShortcut ? @"W" : |
| 496 | @"w")]; |
[email protected] | 8cfbdbd | 2011-06-16 04:17:17 | [diff] [blame] | 497 | [closeWindowMenuItem_ setKeyEquivalentModifierMask:NSCommandKeyMask]; |
| 498 | } |
| 499 | |
| 500 | // If the window has a tab controller, make "close tab" take over cmd-w, |
| 501 | // otherwise it shouldn't have any key-equivalent because it should be disabled. |
[email protected] | ebbe94b | 2012-01-18 20:11:56 | [diff] [blame] | 502 | - (void)adjustCloseTabMenuItemKeyEquivalent:(BOOL)enableCloseTabShortcut { |
| 503 | if (enableCloseTabShortcut) { |
[email protected] | 8cfbdbd | 2011-06-16 04:17:17 | [diff] [blame] | 504 | [closeTabMenuItem_ setKeyEquivalent:@"w"]; |
| 505 | [closeTabMenuItem_ setKeyEquivalentModifierMask:NSCommandKeyMask]; |
| 506 | } else { |
| 507 | [closeTabMenuItem_ setKeyEquivalent:@""]; |
| 508 | [closeTabMenuItem_ setKeyEquivalentModifierMask:0]; |
| 509 | } |
| 510 | } |
| 511 | |
| 512 | // Explicitly remove any command-key equivalents from the close tab/window |
| 513 | // menus so that nothing can go haywire if we get a user action during pending |
| 514 | // updates. |
| 515 | - (void)clearCloseMenuItemKeyEquivalents { |
| 516 | [closeTabMenuItem_ setKeyEquivalent:@""]; |
| 517 | [closeTabMenuItem_ setKeyEquivalentModifierMask:0]; |
| 518 | [closeWindowMenuItem_ setKeyEquivalent:@""]; |
| 519 | [closeWindowMenuItem_ setKeyEquivalentModifierMask:0]; |
| 520 | } |
| 521 | |
[email protected] | 9f5f79b6 | 2011-10-28 00:43:45 | [diff] [blame] | 522 | // See if the focused window window has tabs, and adjust the key equivalents for |
[email protected] | 8cfbdbd | 2011-06-16 04:17:17 | [diff] [blame] | 523 | // Close Tab/Close Window accordingly. |
[email protected] | 9f5f79b6 | 2011-10-28 00:43:45 | [diff] [blame] | 524 | - (void)fixCloseMenuItemKeyEquivalents { |
[email protected] | 8cfbdbd | 2011-06-16 04:17:17 | [diff] [blame] | 525 | fileMenuUpdatePending_ = NO; |
[email protected] | 8cfbdbd | 2011-06-16 04:17:17 | [diff] [blame] | 526 | |
[email protected] | 9f5f79b6 | 2011-10-28 00:43:45 | [diff] [blame] | 527 | NSWindow* window = [NSApp keyWindow]; |
| 528 | NSWindow* mainWindow = [NSApp mainWindow]; |
| 529 | if (!window || ([window parentWindow] == mainWindow)) { |
| 530 | // If the key window is a child of the main window (e.g. a bubble), the main |
| 531 | // window should be the one that handles the close menu item action. |
| 532 | // Also, there might be a small amount of time where there is no key window; |
| 533 | // in that case as well, just use our main browser window if there is one. |
| 534 | // You might think that we should just always use the main window, but the |
| 535 | // "About Chrome" window serves as a counterexample. |
| 536 | window = mainWindow; |
| 537 | } |
| 538 | |
| 539 | BOOL hasTabs = |
| 540 | [[window windowController] isKindOfClass:[TabWindowController class]]; |
[email protected] | ebbe94b | 2012-01-18 20:11:56 | [diff] [blame] | 541 | BOOL enableCloseTabShortcut = hasTabs && !hasPopover_; |
| 542 | [self adjustCloseWindowMenuItemKeyEquivalent:enableCloseTabShortcut]; |
| 543 | [self adjustCloseTabMenuItemKeyEquivalent:enableCloseTabShortcut]; |
[email protected] | 8cfbdbd | 2011-06-16 04:17:17 | [diff] [blame] | 544 | } |
| 545 | |
| 546 | // Fix up the "close tab/close window" command-key equivalents. We do this |
| 547 | // after a delay to ensure that window layer state has been set by the time |
| 548 | // we do the enabling. This should only be called on the main thread, code that |
| 549 | // calls this (even as a side-effect) from other threads needs to be fixed. |
[email protected] | 9f5f79b6 | 2011-10-28 00:43:45 | [diff] [blame] | 550 | - (void)delayedFixCloseMenuItemKeyEquivalents { |
[email protected] | 8cfbdbd | 2011-06-16 04:17:17 | [diff] [blame] | 551 | DCHECK([NSThread isMainThread]); |
| 552 | if (!fileMenuUpdatePending_) { |
| 553 | // The OS prefers keypresses to timers, so it's possible that a cmd-w |
| 554 | // can sneak in before this timer fires. In order to prevent that from |
| 555 | // having any bad consequences, just clear the keys combos altogether. They |
| 556 | // will be reset when the timer eventually fires. |
| 557 | if ([NSThread isMainThread]) { |
| 558 | fileMenuUpdatePending_ = YES; |
| 559 | [self clearCloseMenuItemKeyEquivalents]; |
[email protected] | 9f5f79b6 | 2011-10-28 00:43:45 | [diff] [blame] | 560 | [self performSelector:@selector(fixCloseMenuItemKeyEquivalents) |
| 561 | withObject:nil |
[email protected] | 8cfbdbd | 2011-06-16 04:17:17 | [diff] [blame] | 562 | afterDelay:0]; |
| 563 | } else { |
| 564 | // This shouldn't be happening, but if it does, force it to the main |
| 565 | // thread to avoid dropping the update. Don't mess with |
| 566 | // |fileMenuUpdatePending_| as it's not expected to be threadsafe and |
| 567 | // there could be a race between the selector finishing and setting the |
| 568 | // flag. |
| 569 | [self |
[email protected] | 9f5f79b6 | 2011-10-28 00:43:45 | [diff] [blame] | 570 | performSelectorOnMainThread:@selector(fixCloseMenuItemKeyEquivalents) |
| 571 | withObject:nil |
[email protected] | 8cfbdbd | 2011-06-16 04:17:17 | [diff] [blame] | 572 | waitUntilDone:NO]; |
| 573 | } |
| 574 | } |
| 575 | } |
| 576 | |
[email protected] | 449dd2f | 2009-05-27 13:04:00 | [diff] [blame] | 577 | // Called when we get a notification about the window layering changing to |
| 578 | // update the UI based on the new main window. |
| 579 | - (void)windowLayeringDidChange:(NSNotification*)notify { |
[email protected] | 9f5f79b6 | 2011-10-28 00:43:45 | [diff] [blame] | 580 | [self delayedFixCloseMenuItemKeyEquivalents]; |
[email protected] | 8cfbdbd | 2011-06-16 04:17:17 | [diff] [blame] | 581 | |
[email protected] | dd6ab128 | 2010-07-20 01:07:32 | [diff] [blame] | 582 | if ([notify name] == NSWindowDidResignKeyNotification) { |
| 583 | // If a window is closed, this notification is fired but |[NSApp keyWindow]| |
| 584 | // returns nil regardless of whether any suitable candidates for the key |
| 585 | // window remain. It seems that the new key window for the app is not set |
| 586 | // until after this notification is fired, so a check is performed after the |
| 587 | // run loop is allowed to spin. |
| 588 | [self performSelector:@selector(checkForAnyKeyWindows) |
| 589 | withObject:nil |
| 590 | afterDelay:0.0]; |
| 591 | } |
[email protected] | 5c59370 | 2011-06-17 18:35:24 | [diff] [blame] | 592 | |
| 593 | // If the window changed to a new BrowserWindowController, update the profile. |
| 594 | id windowController = [[notify object] windowController]; |
[email protected] | fbb0455 | 2014-01-16 15:47:38 | [diff] [blame] | 595 | if (![windowController isKindOfClass:[BrowserWindowController class]]) |
| 596 | return; |
| 597 | |
| 598 | if ([notify name] == NSWindowDidBecomeMainNotification) { |
[email protected] | 5c59370 | 2011-06-17 18:35:24 | [diff] [blame] | 599 | // If the profile is incognito, use the original profile. |
| 600 | Profile* newProfile = [windowController profile]->GetOriginalProfile(); |
| 601 | [self windowChangedToProfile:newProfile]; |
[email protected] | 0665ebe | 2013-02-13 09:53:19 | [diff] [blame] | 602 | } else if (chrome::GetTotalBrowserCount() == 0) { |
[email protected] | cae68b3a | 2011-11-21 23:29:49 | [diff] [blame] | 603 | [self windowChangedToProfile: |
| 604 | g_browser_process->profile_manager()->GetLastUsedProfile()]; |
[email protected] | 5c59370 | 2011-06-17 18:35:24 | [diff] [blame] | 605 | } |
| 606 | } |
| 607 | |
[email protected] | d4c811e7 | 2013-10-29 21:57:55 | [diff] [blame] | 608 | - (void)activeSpaceDidChange:(NSNotification*)notify { |
| 609 | if (reopenTime_.is_null() || |
| 610 | ![NSApp isActive] || |
| 611 | (base::TimeTicks::Now() - reopenTime_).InMilliseconds() > |
| 612 | kWorkspaceChangeTimeoutMs) { |
| 613 | return; |
| 614 | } |
| 615 | |
| 616 | // The last applicationShouldHandleReopen:hasVisibleWindows: call |
| 617 | // happened during a space change. Now that the change has |
| 618 | // completed, raise browser windows. |
| 619 | reopenTime_ = base::TimeTicks(); |
| 620 | std::set<NSWindow*> browserWindows; |
| 621 | for (chrome::BrowserIterator iter; !iter.done(); iter.Next()) { |
| 622 | Browser* browser = *iter; |
| 623 | browserWindows.insert(browser->window()->GetNativeWindow()); |
| 624 | } |
| 625 | if (!browserWindows.empty()) { |
[email protected] | 6c4b7960 | 2014-05-28 16:47:21 | [diff] [blame] | 626 | ui::FocusWindowSetOnCurrentSpace(browserWindows); |
[email protected] | d4c811e7 | 2013-10-29 21:57:55 | [diff] [blame] | 627 | } |
| 628 | } |
| 629 | |
[email protected] | ebbe94b | 2012-01-18 20:11:56 | [diff] [blame] | 630 | // Called on Lion and later when a popover (e.g. dictionary) is shown. |
| 631 | - (void)popoverDidShow:(NSNotification*)notify { |
| 632 | hasPopover_ = YES; |
| 633 | [self fixCloseMenuItemKeyEquivalents]; |
| 634 | } |
| 635 | |
| 636 | // Called on Lion and later when a popover (e.g. dictionary) is closed. |
| 637 | - (void)popoverDidClose:(NSNotification*)notify { |
| 638 | hasPopover_ = NO; |
| 639 | [self fixCloseMenuItemKeyEquivalents]; |
| 640 | } |
| 641 | |
[email protected] | dd6ab128 | 2010-07-20 01:07:32 | [diff] [blame] | 642 | - (void)checkForAnyKeyWindows { |
| 643 | if ([NSApp keyWindow]) |
| 644 | return; |
| 645 | |
[email protected] | ad50def5 | 2011-10-19 23:17:07 | [diff] [blame] | 646 | content::NotificationService::current()->Notify( |
[email protected] | d53a08c | 2012-07-18 20:35:30 | [diff] [blame] | 647 | chrome::NOTIFICATION_NO_KEY_WINDOW, |
[email protected] | ad50def5 | 2011-10-19 23:17:07 | [diff] [blame] | 648 | content::NotificationService::AllSources(), |
| 649 | content::NotificationService::NoDetails()); |
[email protected] | 449dd2f | 2009-05-27 13:04:00 | [diff] [blame] | 650 | } |
| 651 | |
[email protected] | 414fde59 | 2009-05-21 16:14:43 | [diff] [blame] | 652 | // If the auto-update interval is not set, make it 5 hours. |
[email protected] | 414fde59 | 2009-05-21 16:14:43 | [diff] [blame] | 653 | // Placed here for 2 reasons: |
| 654 | // 1) Same spot as other Pref stuff |
| 655 | // 2) Try and be friendly by keeping this after app launch |
[email protected] | 414fde59 | 2009-05-21 16:14:43 | [diff] [blame] | 656 | - (void)setUpdateCheckInterval { |
| 657 | #if defined(GOOGLE_CHROME_BUILD) |
[email protected] | 8be0717 | 2013-04-27 05:25:49 | [diff] [blame] | 658 | CFStringRef app = CFSTR("com.google.Keystone.Agent"); |
| 659 | CFStringRef checkInterval = CFSTR("checkInterval"); |
[email protected] | 414fde59 | 2009-05-21 16:14:43 | [diff] [blame] | 660 | CFPropertyListRef plist = CFPreferencesCopyAppValue(checkInterval, app); |
| 661 | if (!plist) { |
| 662 | const float fiveHoursInSeconds = 5.0 * 60.0 * 60.0; |
[email protected] | 7dc8c6b | 2010-04-09 17:02:50 | [diff] [blame] | 663 | NSNumber* value = [NSNumber numberWithFloat:fiveHoursInSeconds]; |
[email protected] | 414fde59 | 2009-05-21 16:14:43 | [diff] [blame] | 664 | CFPreferencesSetAppValue(checkInterval, value, app); |
| 665 | CFPreferencesAppSynchronize(app); |
| 666 | } |
| 667 | #endif |
| 668 | } |
| 669 | |
[email protected] | 66791aff | 2014-04-29 09:45:38 | [diff] [blame] | 670 | - (void)openStartupUrls { |
| 671 | // On Mac, the URLs are passed in via Cocoa, not command line. The Chrome |
| 672 | // NSApplication is created in MainMessageLoop, and then the shortcut urls |
| 673 | // are passed in via Apple events. At this point, the first browser is |
| 674 | // already loaded in PreMainMessageLoop. If we initialize NSApplication |
| 675 | // before PreMainMessageLoop to capture shortcut URL events, it may cause |
| 676 | // more problems because it relies on things created in PreMainMessageLoop |
| 677 | // and may break existing message loop design. |
| 678 | if (startupUrls_.empty()) |
| 679 | return; |
| 680 | |
| 681 | // If there's only 1 tab and the tab is NTP, close this NTP tab and open all |
| 682 | // startup urls in new tabs, because the omnibox will stay focused if we |
| 683 | // load url in NTP tab. |
| 684 | Browser* browser = chrome::GetLastActiveBrowser(); |
| 685 | int startupIndex = TabStripModel::kNoTab; |
| 686 | content::WebContents* startupContent = NULL; |
| 687 | |
| 688 | if (browser && browser->tab_strip_model()->count() == 1) { |
| 689 | startupIndex = browser->tab_strip_model()->active_index(); |
| 690 | startupContent = browser->tab_strip_model()->GetActiveWebContents(); |
| 691 | } |
| 692 | |
| 693 | if (startupUrls_.size()) { |
| 694 | [self openUrls:startupUrls_]; |
| 695 | startupUrls_.clear(); |
| 696 | } |
| 697 | |
| 698 | if (startupIndex != TabStripModel::kNoTab && |
| 699 | startupContent->GetVisibleURL() == GURL(chrome::kChromeUINewTabURL)) { |
| 700 | browser->tab_strip_model()->CloseWebContentsAt(startupIndex, |
| 701 | TabStripModel::CLOSE_NONE); |
| 702 | } |
| 703 | } |
| 704 | |
[email protected] | 136140c | 2009-05-19 13:58:25 | [diff] [blame] | 705 | // This is called after profiles have been loaded and preferences registered. |
| 706 | // It is safe to access the default profile here. |
| 707 | - (void)applicationDidFinishLaunching:(NSNotification*)notify { |
[email protected] | 7d1aaa6 | 2014-07-18 02:21:30 | [diff] [blame] | 708 | MacStartupProfiler::GetInstance()->Profile( |
| 709 | MacStartupProfiler::DID_FINISH_LAUNCHING); |
| 710 | MacStartupProfiler::GetInstance()->RecordMetrics(); |
| 711 | |
[email protected] | c984d9f | 2010-07-20 20:52:20 | [diff] [blame] | 712 | // Notify BrowserList to keep the application running so it doesn't go away |
| 713 | // when all the browser windows get closed. |
[email protected] | 9a718b2 | 2014-02-25 15:17:27 | [diff] [blame] | 714 | chrome::IncrementKeepAliveCount(); |
[email protected] | 136140c | 2009-05-19 13:58:25 | [diff] [blame] | 715 | |
[email protected] | 414fde59 | 2009-05-21 16:14:43 | [diff] [blame] | 716 | [self setUpdateCheckInterval]; |
[email protected] | 2bcec61 | 2009-05-14 17:50:53 | [diff] [blame] | 717 | |
[email protected] | edb2d03 | 2013-08-30 09:03:17 | [diff] [blame] | 718 | // Start managing the menu for app windows. This needs to be done here because |
| 719 | // main menu item titles are not yet initialized in awakeFromNib. |
[email protected] | e7dc399 | 2013-11-06 02:16:42 | [diff] [blame] | 720 | [self initAppShimMenuController]; |
[email protected] | edb2d03 | 2013-08-30 09:03:17 | [diff] [blame] | 721 | |
[email protected] | 425f7a0 | 2014-04-29 01:51:44 | [diff] [blame] | 722 | // If enabled, keep Chrome alive when apps are open instead of quitting all |
| 723 | // apps. |
| 724 | quitWithAppsController_ = new QuitWithAppsController(); |
| 725 | |
[email protected] | 2bcec61 | 2009-05-14 17:50:53 | [diff] [blame] | 726 | // Build up the encoding menu, the order of the items differs based on the |
| 727 | // current locale (see http://crbug.com/7647 for details). |
| 728 | // We need a valid g_browser_process to get the profile which is why we can't |
| 729 | // call this from awakeFromNib. |
[email protected] | 6f91e5ae | 2011-03-21 19:53:14 | [diff] [blame] | 730 | NSMenu* viewMenu = [[[NSApp mainMenu] itemWithTag:IDC_VIEW_MENU] submenu]; |
| 731 | NSMenuItem* encodingMenuItem = [viewMenu itemWithTag:IDC_ENCODING_MENU]; |
| 732 | NSMenu* encodingMenu = [encodingMenuItem submenu]; |
[email protected] | a585a8db | 2011-06-20 18:58:35 | [diff] [blame] | 733 | EncodingMenuControllerDelegate::BuildEncodingMenu([self lastProfile], |
[email protected] | 6f91e5ae | 2011-03-21 19:53:14 | [diff] [blame] | 734 | encodingMenu); |
[email protected] | 2bcec61 | 2009-05-14 17:50:53 | [diff] [blame] | 735 | |
[email protected] | 1c62b2f | 2013-06-28 00:15:00 | [diff] [blame] | 736 | // Instantiate the ProfileInfoCache observer so that we can get |
| 737 | // notified when a profile is deleted. |
| 738 | profileInfoCacheObserver_.reset(new AppControllerProfileObserver( |
| 739 | g_browser_process->profile_manager(), self)); |
| 740 | |
[email protected] | f2c20fa | 2009-12-01 17:42:02 | [diff] [blame] | 741 | // Since Chrome is localized to more languages than the OS, tell Cocoa which |
| 742 | // menu is the Help so it can add the search item to it. |
[email protected] | 7398013 | 2012-07-26 20:09:28 | [diff] [blame] | 743 | [NSApp setHelpMenu:helpMenu_]; |
[email protected] | f2c20fa | 2009-12-01 17:42:02 | [diff] [blame] | 744 | |
[email protected] | 12dc3d4 | 2010-02-22 23:37:12 | [diff] [blame] | 745 | // Record the path to the (browser) app bundle; this is used by the app mode |
[email protected] | bf0d26b | 2013-08-29 03:09:14 | [diff] [blame] | 746 | // shim. It has to be done in FILE thread because getting the path requires |
| 747 | // I/O. |
| 748 | BrowserThread::PostTask( |
| 749 | BrowserThread::FILE, FROM_HERE, |
| 750 | base::Bind(&RecordLastRunAppBundlePath)); |
[email protected] | 12dc3d4 | 2010-02-22 23:37:12 | [diff] [blame] | 751 | |
[email protected] | eef99c2 | 2010-08-17 05:55:16 | [diff] [blame] | 752 | // Makes "Services" menu items available. |
| 753 | [self registerServicesMenuTypesTo:[notify object]]; |
| 754 | |
[email protected] | caadfca | 2010-01-22 21:51:21 | [diff] [blame] | 755 | startupComplete_ = YES; |
| 756 | |
[email protected] | 66791aff | 2014-04-29 09:45:38 | [diff] [blame] | 757 | [self openStartupUrls]; |
[email protected] | aac169d | 2011-03-18 19:53:03 | [diff] [blame] | 758 | |
[email protected] | 0545453 | 2013-01-22 21:09:08 | [diff] [blame] | 759 | PrefService* localState = g_browser_process->local_state(); |
| 760 | if (localState) { |
| 761 | localPrefRegistrar_.Init(localState); |
| 762 | localPrefRegistrar_.Add( |
| 763 | prefs::kAllowFileSelectionDialogs, |
| 764 | base::Bind(&chrome::BrowserCommandController::UpdateOpenFileState, |
| 765 | menuState_.get())); |
| 766 | } |
[email protected] | 7c32108 | 2009-02-09 15:35:47 | [diff] [blame] | 767 | } |
| 768 | |
[email protected] | a96ec6a | 2009-11-04 17:27:08 | [diff] [blame] | 769 | // This is called after profiles have been loaded and preferences registered. |
| 770 | // It is safe to access the default profile here. |
| 771 | - (void)applicationDidBecomeActive:(NSNotification*)notify { |
[email protected] | 5904cb4 | 2012-09-24 15:05:20 | [diff] [blame] | 772 | content::PluginService::GetInstance()->AppActivated(); |
[email protected] | a96ec6a | 2009-11-04 17:27:08 | [diff] [blame] | 773 | } |
| 774 | |
[email protected] | 73f5b36 | 2009-08-10 23:58:21 | [diff] [blame] | 775 | // Helper function for populating and displaying the in progress downloads at |
| 776 | // exit alert panel. |
| 777 | - (BOOL)userWillWaitForInProgressDownloads:(int)downloadCount { |
[email protected] | 83a9f65 | 2012-04-25 21:03:48 | [diff] [blame] | 778 | NSString* titleText = nil; |
[email protected] | e8b5f788 | 2009-09-14 20:59:13 | [diff] [blame] | 779 | NSString* explanationText = nil; |
[email protected] | 73f5b36 | 2009-08-10 23:58:21 | [diff] [blame] | 780 | NSString* waitTitle = nil; |
| 781 | NSString* exitTitle = nil; |
| 782 | |
| 783 | // Set the dialog text based on whether or not there are multiple downloads. |
| 784 | if (downloadCount == 1) { |
[email protected] | e8b5f788 | 2009-09-14 20:59:13 | [diff] [blame] | 785 | // Dialog text: warning and explanation. |
[email protected] | 83a9f65 | 2012-04-25 21:03:48 | [diff] [blame] | 786 | titleText = l10n_util::GetNSString( |
| 787 | IDS_SINGLE_DOWNLOAD_REMOVE_CONFIRM_TITLE); |
[email protected] | ab3b1c11 | 2012-01-13 22:04:16 | [diff] [blame] | 788 | explanationText = l10n_util::GetNSString( |
| 789 | IDS_SINGLE_DOWNLOAD_REMOVE_CONFIRM_EXPLANATION); |
[email protected] | 73f5b36 | 2009-08-10 23:58:21 | [diff] [blame] | 790 | } else { |
[email protected] | e8b5f788 | 2009-09-14 20:59:13 | [diff] [blame] | 791 | // Dialog text: warning and explanation. |
mattm | 84f8c167 | 2014-09-12 22:08:07 | [diff] [blame] | 792 | titleText = l10n_util::GetNSString( |
| 793 | IDS_MULTIPLE_DOWNLOADS_REMOVE_CONFIRM_TITLE); |
[email protected] | ab3b1c11 | 2012-01-13 22:04:16 | [diff] [blame] | 794 | explanationText = l10n_util::GetNSString( |
| 795 | IDS_MULTIPLE_DOWNLOADS_REMOVE_CONFIRM_EXPLANATION); |
[email protected] | 73f5b36 | 2009-08-10 23:58:21 | [diff] [blame] | 796 | } |
[email protected] | 83a9f65 | 2012-04-25 21:03:48 | [diff] [blame] | 797 | // Cancel download and exit button text. |
| 798 | exitTitle = l10n_util::GetNSString( |
| 799 | IDS_DOWNLOAD_REMOVE_CONFIRM_OK_BUTTON_LABEL); |
| 800 | |
| 801 | // Wait for download button text. |
| 802 | waitTitle = l10n_util::GetNSString( |
| 803 | IDS_DOWNLOAD_REMOVE_CONFIRM_CANCEL_BUTTON_LABEL); |
[email protected] | 73f5b36 | 2009-08-10 23:58:21 | [diff] [blame] | 804 | |
| 805 | // 'waitButton' is the default choice. |
[email protected] | 361b47c | 2013-07-02 15:13:24 | [diff] [blame] | 806 | int choice = NSRunAlertPanel(titleText, @"%@", |
| 807 | waitTitle, exitTitle, nil, explanationText); |
[email protected] | 73f5b36 | 2009-08-10 23:58:21 | [diff] [blame] | 808 | return choice == NSAlertDefaultReturn ? YES : NO; |
| 809 | } |
| 810 | |
| 811 | // Check all profiles for in progress downloads, and if we find any, prompt the |
[email protected] | 09729a55 | 2009-08-03 23:21:41 | [diff] [blame] | 812 | // user to see if we should continue to exit (and thus cancel the downloads), or |
| 813 | // if we should wait. |
| 814 | - (BOOL)shouldQuitWithInProgressDownloads { |
[email protected] | 73f5b36 | 2009-08-10 23:58:21 | [diff] [blame] | 815 | ProfileManager* profile_manager = g_browser_process->profile_manager(); |
| 816 | if (!profile_manager) |
| 817 | return YES; |
[email protected] | 09729a55 | 2009-08-03 23:21:41 | [diff] [blame] | 818 | |
[email protected] | 844a100 | 2011-04-19 11:37:21 | [diff] [blame] | 819 | std::vector<Profile*> profiles(profile_manager->GetLoadedProfiles()); |
| 820 | for (size_t i = 0; i < profiles.size(); ++i) { |
[email protected] | 9bb54ee | 2011-10-12 17:43:35 | [diff] [blame] | 821 | DownloadService* download_service = |
[email protected] | 423939d | 2013-07-31 20:00:08 | [diff] [blame] | 822 | DownloadServiceFactory::GetForBrowserContext(profiles[i]); |
[email protected] | 9bb54ee | 2011-10-12 17:43:35 | [diff] [blame] | 823 | DownloadManager* download_manager = |
| 824 | (download_service->HasCreatedDownloadManager() ? |
[email protected] | b441a849 | 2012-06-06 14:55:57 | [diff] [blame] | 825 | BrowserContext::GetDownloadManager(profiles[i]) : NULL); |
[email protected] | 422a7d1 | 2013-10-21 12:10:42 | [diff] [blame] | 826 | if (download_manager && |
| 827 | download_manager->NonMaliciousInProgressCount() > 0) { |
| 828 | int downloadCount = download_manager->NonMaliciousInProgressCount(); |
[email protected] | 73f5b36 | 2009-08-10 23:58:21 | [diff] [blame] | 829 | if ([self userWillWaitForInProgressDownloads:downloadCount]) { |
| 830 | // Create a new browser window (if necessary) and navigate to the |
| 831 | // downloads page if the user chooses to wait. |
[email protected] | 3c6a095 | 2012-12-17 11:56:09 | [diff] [blame] | 832 | Browser* browser = chrome::FindBrowserWithProfile( |
[email protected] | 323fd712 | 2012-08-24 14:45:11 | [diff] [blame] | 833 | profiles[i], chrome::HOST_DESKTOP_TYPE_NATIVE); |
[email protected] | 73f5b36 | 2009-08-10 23:58:21 | [diff] [blame] | 834 | if (!browser) { |
[email protected] | c125cdc | 2013-02-25 19:10:21 | [diff] [blame] | 835 | browser = new Browser(Browser::CreateParams( |
| 836 | profiles[i], chrome::HOST_DESKTOP_TYPE_NATIVE)); |
[email protected] | 73f5b36 | 2009-08-10 23:58:21 | [diff] [blame] | 837 | browser->window()->Show(); |
| 838 | } |
| 839 | DCHECK(browser); |
[email protected] | 5d9cace7 | 2012-06-21 16:07:12 | [diff] [blame] | 840 | chrome::ShowDownloads(browser); |
[email protected] | 73f5b36 | 2009-08-10 23:58:21 | [diff] [blame] | 841 | return NO; |
| 842 | } |
[email protected] | 09729a55 | 2009-08-03 23:21:41 | [diff] [blame] | 843 | |
[email protected] | 73f5b36 | 2009-08-10 23:58:21 | [diff] [blame] | 844 | // User wants to exit. |
| 845 | return YES; |
[email protected] | 09729a55 | 2009-08-03 23:21:41 | [diff] [blame] | 846 | } |
[email protected] | 09729a55 | 2009-08-03 23:21:41 | [diff] [blame] | 847 | } |
| 848 | |
[email protected] | 73f5b36 | 2009-08-10 23:58:21 | [diff] [blame] | 849 | // No profiles or active downloads found, okay to exit. |
[email protected] | 09729a55 | 2009-08-03 23:21:41 | [diff] [blame] | 850 | return YES; |
| 851 | } |
| 852 | |
[email protected] | fbc947b | 2009-06-19 13:28:24 | [diff] [blame] | 853 | // Called to determine if we should enable the "restore tab" menu item. |
| 854 | // Checks with the TabRestoreService to see if there's anything there to |
| 855 | // restore and returns YES if so. |
| 856 | - (BOOL)canRestoreTab { |
[email protected] | 92371eb | 2011-04-28 11:50:15 | [diff] [blame] | 857 | TabRestoreService* service = |
[email protected] | a585a8db | 2011-06-20 18:58:35 | [diff] [blame] | 858 | TabRestoreServiceFactory::GetForProfile([self lastProfile]); |
[email protected] | fbc947b | 2009-06-19 13:28:24 | [diff] [blame] | 859 | return service && !service->entries().empty(); |
| 860 | } |
| 861 | |
[email protected] | 1c62b2f | 2013-06-28 00:15:00 | [diff] [blame] | 862 | // Called from the AppControllerProfileObserver every time a profile is deleted. |
| 863 | - (void)profileWasRemoved:(const base::FilePath&)profilePath { |
| 864 | Profile* lastProfile = [self lastProfile]; |
| 865 | |
| 866 | // If the lastProfile has been deleted, the profile manager has |
| 867 | // already loaded a new one, so the pointer needs to be updated; |
| 868 | // otherwise we will try to start up a browser window with a pointer |
| 869 | // to the old profile. |
| 870 | if (profilePath == lastProfile->GetPath()) |
| 871 | lastProfile_ = g_browser_process->profile_manager()->GetLastUsedProfile(); |
| 872 | } |
| 873 | |
[email protected] | 0545453 | 2013-01-22 21:09:08 | [diff] [blame] | 874 | // Returns true if there is a modal window (either window- or application- |
[email protected] | f6c15fa | 2010-06-04 20:08:21 | [diff] [blame] | 875 | // modal) blocking the active browser. Note that tab modal dialogs (HTTP auth |
| 876 | // sheets) will not count as blocking the browser. But things like open/save |
| 877 | // dialogs that are window modal will block the browser. |
[email protected] | 0545453 | 2013-01-22 21:09:08 | [diff] [blame] | 878 | - (BOOL)keyWindowIsModal { |
| 879 | if ([NSApp modalWindow]) |
| 880 | return YES; |
| 881 | |
[email protected] | f2bc6e9 | 2012-10-14 02:08:38 | [diff] [blame] | 882 | Browser* browser = chrome::GetLastActiveBrowser(); |
[email protected] | 0545453 | 2013-01-22 21:09:08 | [diff] [blame] | 883 | return browser && |
| 884 | [[browser->window()->GetNativeWindow() attachedSheet] |
| 885 | isKindOfClass:[NSWindow class]]; |
[email protected] | 6a28133 | 2009-10-13 17:41:06 | [diff] [blame] | 886 | } |
| 887 | |
[email protected] | 88d7494 | 2009-01-21 22:04:44 | [diff] [blame] | 888 | // Called to validate menu items when there are no key windows. All the |
| 889 | // items we care about have been set with the |commandDispatch:| action and |
| 890 | // a target of FirstResponder in IB. If it's not one of those, let it |
| 891 | // continue up the responder chain to be handled elsewhere. We pull out the |
| 892 | // tag as the cross-platform constant to differentiate and dispatch the |
| 893 | // various commands. |
| 894 | - (BOOL)validateUserInterfaceItem:(id<NSValidatedUserInterfaceItem>)item { |
| 895 | SEL action = [item action]; |
| 896 | BOOL enable = NO; |
[email protected] | 0545453 | 2013-01-22 21:09:08 | [diff] [blame] | 897 | if (action == @selector(commandDispatch:) || |
| 898 | action == @selector(commandFromDock:)) { |
[email protected] | 88d7494 | 2009-01-21 22:04:44 | [diff] [blame] | 899 | NSInteger tag = [item tag]; |
[email protected] | 0545453 | 2013-01-22 21:09:08 | [diff] [blame] | 900 | if (menuState_ && // NULL in tests. |
| 901 | menuState_->SupportsCommand(tag)) { |
[email protected] | fbc947b | 2009-06-19 13:28:24 | [diff] [blame] | 902 | switch (tag) { |
[email protected] | 419eb2a | 2009-11-11 16:26:26 | [diff] [blame] | 903 | // The File Menu commands are not automatically disabled by Cocoa when a |
| 904 | // dialog sheet obscures the browser window, so we disable several of |
| 905 | // them here. We don't need to include IDC_CLOSE_WINDOW, because |
| 906 | // app_controller is only activated when there are no key windows (see |
| 907 | // function comment). |
[email protected] | fbc947b | 2009-06-19 13:28:24 | [diff] [blame] | 908 | case IDC_RESTORE_TAB: |
[email protected] | 0545453 | 2013-01-22 21:09:08 | [diff] [blame] | 909 | enable = ![self keyWindowIsModal] && [self canRestoreTab]; |
[email protected] | 6a28133 | 2009-10-13 17:41:06 | [diff] [blame] | 910 | break; |
[email protected] | f6c15fa | 2010-06-04 20:08:21 | [diff] [blame] | 911 | // Browser-level items that open in new tabs should not open if there's |
| 912 | // a window- or app-modal dialog. |
[email protected] | 6a28133 | 2009-10-13 17:41:06 | [diff] [blame] | 913 | case IDC_OPEN_FILE: |
[email protected] | 6a28133 | 2009-10-13 17:41:06 | [diff] [blame] | 914 | case IDC_NEW_TAB: |
[email protected] | f6c15fa | 2010-06-04 20:08:21 | [diff] [blame] | 915 | case IDC_SHOW_HISTORY: |
| 916 | case IDC_SHOW_BOOKMARK_MANAGER: |
[email protected] | 0545453 | 2013-01-22 21:09:08 | [diff] [blame] | 917 | enable = ![self keyWindowIsModal]; |
[email protected] | f6c15fa | 2010-06-04 20:08:21 | [diff] [blame] | 918 | break; |
| 919 | // Browser-level items that open in new windows. |
[email protected] | f6c15fa | 2010-06-04 20:08:21 | [diff] [blame] | 920 | case IDC_TASK_MANAGER: |
| 921 | // Allow the user to open a new window if there's a window-modal |
| 922 | // dialog. |
[email protected] | 0545453 | 2013-01-22 21:09:08 | [diff] [blame] | 923 | enable = ![self keyWindowIsModal]; |
[email protected] | fbc947b | 2009-06-19 13:28:24 | [diff] [blame] | 924 | break; |
[email protected] | 6554918 | 2012-02-25 00:45:40 | [diff] [blame] | 925 | case IDC_SHOW_SYNC_SETUP: { |
[email protected] | a585a8db | 2011-06-20 18:58:35 | [diff] [blame] | 926 | Profile* lastProfile = [self lastProfile]; |
[email protected] | 446e16a | 2010-05-03 21:23:58 | [diff] [blame] | 927 | // The profile may be NULL during shutdown -- see |
| 928 | // http://code.google.com/p/chromium/issues/detail?id=43048 . |
| 929 | // |
| 930 | // TODO(akalin,viettrungluu): Figure out whether this method |
[email protected] | a585a8db | 2011-06-20 18:58:35 | [diff] [blame] | 931 | // can be prevented from being called if lastProfile is |
[email protected] | 446e16a | 2010-05-03 21:23:58 | [diff] [blame] | 932 | // NULL. |
[email protected] | a585a8db | 2011-06-20 18:58:35 | [diff] [blame] | 933 | if (!lastProfile) { |
[email protected] | 446e16a | 2010-05-03 21:23:58 | [diff] [blame] | 934 | LOG(WARNING) |
[email protected] | a585a8db | 2011-06-20 18:58:35 | [diff] [blame] | 935 | << "NULL lastProfile detected -- not doing anything"; |
[email protected] | 446e16a | 2010-05-03 21:23:58 | [diff] [blame] | 936 | break; |
| 937 | } |
[email protected] | 074311a | 2013-02-28 23:14:09 | [diff] [blame] | 938 | SigninManager* signin = SigninManagerFactory::GetForProfile( |
| 939 | lastProfile->GetOriginalProfile()); |
| 940 | enable = signin->IsSigninAllowed() && |
| 941 | ![self keyWindowIsModal]; |
[email protected] | 6bf5119 | 2013-01-18 12:35:25 | [diff] [blame] | 942 | [BrowserWindowController updateSigninItem:item |
| 943 | shouldShow:enable |
| 944 | currentProfile:lastProfile]; |
[email protected] | f9bc9b9 | 2009-11-24 00:55:35 | [diff] [blame] | 945 | break; |
[email protected] | 446e16a | 2010-05-03 21:23:58 | [diff] [blame] | 946 | } |
[email protected] | 236ad302 | 2013-09-04 03:27:43 | [diff] [blame] | 947 | #if defined(GOOGLE_CHROME_BUILD) |
[email protected] | acaa37b | 2011-08-31 07:19:11 | [diff] [blame] | 948 | case IDC_FEEDBACK: |
| 949 | enable = NO; |
| 950 | break; |
[email protected] | 236ad302 | 2013-09-04 03:27:43 | [diff] [blame] | 951 | #endif |
[email protected] | fbc947b | 2009-06-19 13:28:24 | [diff] [blame] | 952 | default: |
[email protected] | f6c15fa | 2010-06-04 20:08:21 | [diff] [blame] | 953 | enable = menuState_->IsCommandEnabled(tag) ? |
[email protected] | 0545453 | 2013-01-22 21:09:08 | [diff] [blame] | 954 | ![self keyWindowIsModal] : NO; |
[email protected] | fbc947b | 2009-06-19 13:28:24 | [diff] [blame] | 955 | } |
| 956 | } |
[email protected] | 3b6aa8b6 | 2009-09-15 21:36:11 | [diff] [blame] | 957 | } else if (action == @selector(terminate:)) { |
[email protected] | 88d7494 | 2009-01-21 22:04:44 | [diff] [blame] | 958 | enable = YES; |
[email protected] | 3111f08b | 2009-04-30 16:01:52 | [diff] [blame] | 959 | } else if (action == @selector(showPreferences:)) { |
| 960 | enable = YES; |
[email protected] | bde3dda | 2009-05-20 22:13:07 | [diff] [blame] | 961 | } else if (action == @selector(orderFrontStandardAboutPanel:)) { |
| 962 | enable = YES; |
[email protected] | 93b59fc | 2010-12-21 20:00:47 | [diff] [blame] | 963 | } else if (action == @selector(commandFromDock:)) { |
[email protected] | 3d4bd23f | 2009-10-06 03:58:38 | [diff] [blame] | 964 | enable = YES; |
[email protected] | a99fce0e | 2011-03-21 20:58:48 | [diff] [blame] | 965 | } else if (action == @selector(toggleConfirmToQuit:)) { |
| 966 | [self updateConfirmToQuitPrefMenuItem:static_cast<NSMenuItem*>(item)]; |
| 967 | enable = YES; |
[email protected] | e0b0d1ba | 2014-04-24 03:22:29 | [diff] [blame] | 968 | } else if (action == @selector(toggleDisplayMessageCenter:)) { |
| 969 | NSMenuItem* menuItem = static_cast<NSMenuItem*>(item); |
| 970 | [self updateDisplayMessageCenterPrefMenuItem:menuItem]; |
| 971 | enable = YES; |
[email protected] | 3a90b45 | 2013-02-15 04:33:48 | [diff] [blame] | 972 | } else if (action == @selector(executeApplication:)) { |
| 973 | enable = YES; |
[email protected] | 88d7494 | 2009-01-21 22:04:44 | [diff] [blame] | 974 | } |
| 975 | return enable; |
| 976 | } |
| 977 | |
[email protected] | 947fc0d | 2010-01-14 22:33:05 | [diff] [blame] | 978 | // Called when the user picks a menu item when there are no key windows, or when |
| 979 | // there is no foreground browser window. Calls through to the browser object to |
| 980 | // execute the command. This assumes that the command is supported and doesn't |
[email protected] | d630d7d5 | 2010-02-21 00:55:11 | [diff] [blame] | 981 | // check, otherwise it should have been disabled in the UI in |
| 982 | // |-validateUserInterfaceItem:|. |
[email protected] | 88d7494 | 2009-01-21 22:04:44 | [diff] [blame] | 983 | - (void)commandDispatch:(id)sender { |
[email protected] | 2a39d1a | 2014-04-10 05:46:24 | [diff] [blame] | 984 | Profile* lastProfile = [self safeLastProfileForNewWindows]; |
[email protected] | f0a51fb5 | 2009-03-05 12:46:38 | [diff] [blame] | 985 | |
[email protected] | 947fc0d | 2010-01-14 22:33:05 | [diff] [blame] | 986 | // Handle the case where we're dispatching a command from a sender that's in a |
| 987 | // browser window. This means that the command came from a background window |
| 988 | // and is getting here because the foreground window is not a browser window. |
| 989 | if ([sender respondsToSelector:@selector(window)]) { |
| 990 | id delegate = [[sender window] windowController]; |
| 991 | if ([delegate isKindOfClass:[BrowserWindowController class]]) { |
| 992 | [delegate commandDispatch:sender]; |
| 993 | return; |
| 994 | } |
| 995 | } |
| 996 | |
[email protected] | 8d4bbdc | 2012-09-20 21:36:49 | [diff] [blame] | 997 | // Ignore commands during session restore's browser creation. It uses a |
| 998 | // nested message loop and commands dispatched during this operation cause |
| 999 | // havoc. |
| 1000 | if (SessionRestore::IsRestoring(lastProfile) && |
[email protected] | b3a2509 | 2013-05-28 22:08:16 | [diff] [blame] | 1001 | base::MessageLoop::current()->IsNested()) |
[email protected] | 8d4bbdc | 2012-09-20 21:36:49 | [diff] [blame] | 1002 | return; |
| 1003 | |
[email protected] | 88d7494 | 2009-01-21 22:04:44 | [diff] [blame] | 1004 | NSInteger tag = [sender tag]; |
[email protected] | 7108d91 | 2014-01-30 08:10:45 | [diff] [blame] | 1005 | |
| 1006 | // If there are no browser windows, and we are trying to open a browser |
| 1007 | // for a locked profile, we have to show the User Manager instead as the |
| 1008 | // locked profile needs authentication. |
| 1009 | if (IsProfileSignedOut(lastProfile)) { |
mlerman | e29d003 | 2014-09-24 19:31:26 | [diff] [blame^] | 1010 | UserManager::Show(lastProfile->GetPath(), |
| 1011 | profiles::USER_MANAGER_NO_TUTORIAL, |
| 1012 | profiles::USER_MANAGER_SELECT_PROFILE_NO_ACTION); |
[email protected] | 7108d91 | 2014-01-30 08:10:45 | [diff] [blame] | 1013 | return; |
| 1014 | } |
| 1015 | |
[email protected] | 88d7494 | 2009-01-21 22:04:44 | [diff] [blame] | 1016 | switch (tag) { |
[email protected] | fbc947b | 2009-06-19 13:28:24 | [diff] [blame] | 1017 | case IDC_NEW_TAB: |
[email protected] | d630d7d5 | 2010-02-21 00:55:11 | [diff] [blame] | 1018 | // Create a new tab in an existing browser window (which we activate) if |
| 1019 | // possible. |
[email protected] | a585a8db | 2011-06-20 18:58:35 | [diff] [blame] | 1020 | if (Browser* browser = ActivateBrowser(lastProfile)) { |
[email protected] | 5d9829491 | 2012-06-27 22:57:40 | [diff] [blame] | 1021 | chrome::ExecuteCommand(browser, IDC_NEW_TAB); |
[email protected] | d630d7d5 | 2010-02-21 00:55:11 | [diff] [blame] | 1022 | break; |
| 1023 | } |
| 1024 | // Else fall through to create new window. |
[email protected] | 88d7494 | 2009-01-21 22:04:44 | [diff] [blame] | 1025 | case IDC_NEW_WINDOW: |
[email protected] | a585a8db | 2011-06-20 18:58:35 | [diff] [blame] | 1026 | CreateBrowser(lastProfile); |
[email protected] | d630d7d5 | 2010-02-21 00:55:11 | [diff] [blame] | 1027 | break; |
[email protected] | 91a5b3e | 2009-10-30 19:32:22 | [diff] [blame] | 1028 | case IDC_FOCUS_LOCATION: |
[email protected] | 5d9829491 | 2012-06-27 22:57:40 | [diff] [blame] | 1029 | chrome::ExecuteCommand(ActivateOrCreateBrowser(lastProfile), |
| 1030 | IDC_FOCUS_LOCATION); |
[email protected] | 88d7494 | 2009-01-21 22:04:44 | [diff] [blame] | 1031 | break; |
[email protected] | cddc524 | 2009-12-07 18:54:33 | [diff] [blame] | 1032 | case IDC_FOCUS_SEARCH: |
[email protected] | 5d9829491 | 2012-06-27 22:57:40 | [diff] [blame] | 1033 | chrome::ExecuteCommand(ActivateOrCreateBrowser(lastProfile), |
| 1034 | IDC_FOCUS_SEARCH); |
[email protected] | cddc524 | 2009-12-07 18:54:33 | [diff] [blame] | 1035 | break; |
[email protected] | 863ff66 | 2009-01-26 20:18:18 | [diff] [blame] | 1036 | case IDC_NEW_INCOGNITO_WINDOW: |
[email protected] | eddcf750 | 2012-02-09 22:43:48 | [diff] [blame] | 1037 | CreateBrowser(lastProfile->GetOffTheRecordProfile()); |
[email protected] | 863ff66 | 2009-01-26 20:18:18 | [diff] [blame] | 1038 | break; |
[email protected] | fbc947b | 2009-06-19 13:28:24 | [diff] [blame] | 1039 | case IDC_RESTORE_TAB: |
[email protected] | 3b26531 | 2013-01-17 02:49:55 | [diff] [blame] | 1040 | // There is only the native desktop on Mac. |
| 1041 | chrome::OpenWindowWithRestoredTabs(lastProfile, |
| 1042 | chrome::HOST_DESKTOP_TYPE_NATIVE); |
[email protected] | fbc947b | 2009-06-19 13:28:24 | [diff] [blame] | 1043 | break; |
[email protected] | e19516d | 2009-04-28 17:15:19 | [diff] [blame] | 1044 | case IDC_OPEN_FILE: |
[email protected] | 5d9829491 | 2012-06-27 22:57:40 | [diff] [blame] | 1045 | chrome::ExecuteCommand(CreateBrowser(lastProfile), IDC_OPEN_FILE); |
[email protected] | e19516d | 2009-04-28 17:15:19 | [diff] [blame] | 1046 | break; |
[email protected] | 1fdff70 | 2009-10-22 00:36:18 | [diff] [blame] | 1047 | case IDC_CLEAR_BROWSING_DATA: { |
[email protected] | 93e18176 | 2009-05-29 14:30:38 | [diff] [blame] | 1048 | // There may not be a browser open, so use the default profile. |
[email protected] | a585a8db | 2011-06-20 18:58:35 | [diff] [blame] | 1049 | if (Browser* browser = ActivateBrowser(lastProfile)) { |
[email protected] | 5d9cace7 | 2012-06-21 16:07:12 | [diff] [blame] | 1050 | chrome::ShowClearBrowsingDataDialog(browser); |
[email protected] | c8de64a | 2011-01-25 17:10:23 | [diff] [blame] | 1051 | } else { |
[email protected] | 1f0b50b | 2012-06-22 20:37:16 | [diff] [blame] | 1052 | chrome::OpenClearBrowsingDataDialogWindow(lastProfile); |
[email protected] | c8de64a | 2011-01-25 17:10:23 | [diff] [blame] | 1053 | } |
[email protected] | 93e18176 | 2009-05-29 14:30:38 | [diff] [blame] | 1054 | break; |
[email protected] | 1fdff70 | 2009-10-22 00:36:18 | [diff] [blame] | 1055 | } |
[email protected] | 4aee565 | 2009-11-19 18:59:21 | [diff] [blame] | 1056 | case IDC_IMPORT_SETTINGS: { |
[email protected] | a585a8db | 2011-06-20 18:58:35 | [diff] [blame] | 1057 | if (Browser* browser = ActivateBrowser(lastProfile)) { |
[email protected] | 5d9cace7 | 2012-06-21 16:07:12 | [diff] [blame] | 1058 | chrome::ShowImportDialog(browser); |
[email protected] | c8de64a | 2011-01-25 17:10:23 | [diff] [blame] | 1059 | } else { |
[email protected] | 1f0b50b | 2012-06-22 20:37:16 | [diff] [blame] | 1060 | chrome::OpenImportSettingsDialogWindow(lastProfile); |
[email protected] | c8de64a | 2011-01-25 17:10:23 | [diff] [blame] | 1061 | } |
[email protected] | 4aee565 | 2009-11-19 18:59:21 | [diff] [blame] | 1062 | break; |
| 1063 | } |
[email protected] | af7c25e | 2010-01-23 05:46:45 | [diff] [blame] | 1064 | case IDC_SHOW_BOOKMARK_MANAGER: |
[email protected] | 7f6f44c | 2011-12-14 13:23:38 | [diff] [blame] | 1065 | content::RecordAction(UserMetricsAction("ShowBookmarkManager")); |
[email protected] | a585a8db | 2011-06-20 18:58:35 | [diff] [blame] | 1066 | if (Browser* browser = ActivateBrowser(lastProfile)) { |
[email protected] | 5d9cace7 | 2012-06-21 16:07:12 | [diff] [blame] | 1067 | chrome::ShowBookmarkManager(browser); |
[email protected] | 90a8bf25 | 2010-03-12 00:09:47 | [diff] [blame] | 1068 | } else { |
[email protected] | 47e532b | 2010-04-23 17:46:40 | [diff] [blame] | 1069 | // No browser window, so create one for the bookmark manager tab. |
[email protected] | 1f0b50b | 2012-06-22 20:37:16 | [diff] [blame] | 1070 | chrome::OpenBookmarkManagerWindow(lastProfile); |
[email protected] | 90a8bf25 | 2010-03-12 00:09:47 | [diff] [blame] | 1071 | } |
[email protected] | af7c25e | 2010-01-23 05:46:45 | [diff] [blame] | 1072 | break; |
[email protected] | 91a5b3e | 2009-10-30 19:32:22 | [diff] [blame] | 1073 | case IDC_SHOW_HISTORY: |
[email protected] | a585a8db | 2011-06-20 18:58:35 | [diff] [blame] | 1074 | if (Browser* browser = ActivateBrowser(lastProfile)) |
[email protected] | 5d9cace7 | 2012-06-21 16:07:12 | [diff] [blame] | 1075 | chrome::ShowHistory(browser); |
[email protected] | d630d7d5 | 2010-02-21 00:55:11 | [diff] [blame] | 1076 | else |
[email protected] | 1f0b50b | 2012-06-22 20:37:16 | [diff] [blame] | 1077 | chrome::OpenHistoryWindow(lastProfile); |
[email protected] | 91a5b3e | 2009-10-30 19:32:22 | [diff] [blame] | 1078 | break; |
| 1079 | case IDC_SHOW_DOWNLOADS: |
[email protected] | a585a8db | 2011-06-20 18:58:35 | [diff] [blame] | 1080 | if (Browser* browser = ActivateBrowser(lastProfile)) |
[email protected] | 5d9cace7 | 2012-06-21 16:07:12 | [diff] [blame] | 1081 | chrome::ShowDownloads(browser); |
[email protected] | d630d7d5 | 2010-02-21 00:55:11 | [diff] [blame] | 1082 | else |
[email protected] | 1f0b50b | 2012-06-22 20:37:16 | [diff] [blame] | 1083 | chrome::OpenDownloadsWindow(lastProfile); |
[email protected] | 91a5b3e | 2009-10-30 19:32:22 | [diff] [blame] | 1084 | break; |
[email protected] | bb37e6ec | 2009-11-18 17:59:36 | [diff] [blame] | 1085 | case IDC_MANAGE_EXTENSIONS: |
[email protected] | a585a8db | 2011-06-20 18:58:35 | [diff] [blame] | 1086 | if (Browser* browser = ActivateBrowser(lastProfile)) |
[email protected] | bc9833c3 | 2013-02-28 04:05:08 | [diff] [blame] | 1087 | chrome::ShowExtensions(browser, std::string()); |
[email protected] | d630d7d5 | 2010-02-21 00:55:11 | [diff] [blame] | 1088 | else |
[email protected] | 1f0b50b | 2012-06-22 20:37:16 | [diff] [blame] | 1089 | chrome::OpenExtensionsWindow(lastProfile); |
[email protected] | bb37e6ec | 2009-11-18 17:59:36 | [diff] [blame] | 1090 | break; |
[email protected] | 2ca3561 | 2012-06-12 03:49:48 | [diff] [blame] | 1091 | case IDC_HELP_PAGE_VIA_MENU: |
[email protected] | a585a8db | 2011-06-20 18:58:35 | [diff] [blame] | 1092 | if (Browser* browser = ActivateBrowser(lastProfile)) |
[email protected] | 5d9cace7 | 2012-06-21 16:07:12 | [diff] [blame] | 1093 | chrome::ShowHelp(browser, chrome::HELP_SOURCE_MENU); |
[email protected] | d630d7d5 | 2010-02-21 00:55:11 | [diff] [blame] | 1094 | else |
[email protected] | 1f0b50b | 2012-06-22 20:37:16 | [diff] [blame] | 1095 | chrome::OpenHelpWindow(lastProfile, chrome::HELP_SOURCE_MENU); |
[email protected] | 1fdff70 | 2009-10-22 00:36:18 | [diff] [blame] | 1096 | break; |
[email protected] | 6554918 | 2012-02-25 00:45:40 | [diff] [blame] | 1097 | case IDC_SHOW_SYNC_SETUP: |
[email protected] | 3d27d27 | 2013-07-31 03:15:16 | [diff] [blame] | 1098 | if (Browser* browser = ActivateBrowser(lastProfile)) { |
| 1099 | chrome::ShowBrowserSignin(browser, signin::SOURCE_MENU); |
| 1100 | } else { |
| 1101 | chrome::OpenSyncSetupWindow(lastProfile, signin::SOURCE_MENU); |
| 1102 | } |
[email protected] | f9bc9b9 | 2009-11-24 00:55:35 | [diff] [blame] | 1103 | break; |
[email protected] | ceaaf181 | 2010-01-20 22:13:09 | [diff] [blame] | 1104 | case IDC_TASK_MANAGER: |
[email protected] | 7f6f44c | 2011-12-14 13:23:38 | [diff] [blame] | 1105 | content::RecordAction(UserMetricsAction("TaskManager")); |
[email protected] | 29c262de | 2013-06-22 15:39:38 | [diff] [blame] | 1106 | TaskManagerMac::Show(); |
[email protected] | ceaaf181 | 2010-01-20 22:13:09 | [diff] [blame] | 1107 | break; |
[email protected] | 4a42d27 | 2010-06-18 01:29:42 | [diff] [blame] | 1108 | case IDC_OPTIONS: |
| 1109 | [self showPreferences:sender]; |
| 1110 | break; |
[email protected] | d630d7d5 | 2010-02-21 00:55:11 | [diff] [blame] | 1111 | } |
[email protected] | 88d7494 | 2009-01-21 22:04:44 | [diff] [blame] | 1112 | } |
| 1113 | |
[email protected] | 93b59fc | 2010-12-21 20:00:47 | [diff] [blame] | 1114 | // Run a (background) application in a new tab. |
| 1115 | - (void)executeApplication:(id)sender { |
| 1116 | NSInteger tag = [sender tag]; |
[email protected] | a585a8db | 2011-06-20 18:58:35 | [diff] [blame] | 1117 | Profile* profile = [self lastProfile]; |
[email protected] | 93b59fc | 2010-12-21 20:00:47 | [diff] [blame] | 1118 | DCHECK(profile); |
| 1119 | BackgroundApplicationListModel applications(profile); |
| 1120 | DCHECK(tag >= 0 && |
| 1121 | tag < static_cast<int>(applications.size())); |
[email protected] | 1c321ee | 2012-05-21 03:02:34 | [diff] [blame] | 1122 | const extensions::Extension* extension = applications.GetExtension(tag); |
[email protected] | 78d78a6 | 2011-09-29 18:24:33 | [diff] [blame] | 1123 | BackgroundModeManager::LaunchBackgroundApplication(profile, extension); |
[email protected] | 93b59fc | 2010-12-21 20:00:47 | [diff] [blame] | 1124 | } |
| 1125 | |
[email protected] | 947fc0d | 2010-01-14 22:33:05 | [diff] [blame] | 1126 | // Same as |-commandDispatch:|, but executes commands using a disposition |
| 1127 | // determined by the key flags. This will get called in the case where the |
| 1128 | // frontmost window is not a browser window, and the user has command-clicked |
| 1129 | // a button in a background browser window whose action is |
| 1130 | // |-commandDispatchUsingKeyModifiers:| |
| 1131 | - (void)commandDispatchUsingKeyModifiers:(id)sender { |
| 1132 | DCHECK(sender); |
| 1133 | if ([sender respondsToSelector:@selector(window)]) { |
| 1134 | id delegate = [[sender window] windowController]; |
| 1135 | if ([delegate isKindOfClass:[BrowserWindowController class]]) { |
| 1136 | [delegate commandDispatchUsingKeyModifiers:sender]; |
| 1137 | } |
| 1138 | } |
| 1139 | } |
| 1140 | |
[email protected] | 2e29e223 | 2013-07-26 10:40:59 | [diff] [blame] | 1141 | // NSApplication delegate method called when someone clicks on the dock icon. |
| 1142 | // To match standard mac behavior, we should open a new window if there are no |
| 1143 | // browser windows. |
[email protected] | ff81e0a | 2009-04-13 14:58:54 | [diff] [blame] | 1144 | - (BOOL)applicationShouldHandleReopen:(NSApplication*)theApplication |
[email protected] | bf193bc5 | 2013-06-08 04:39:58 | [diff] [blame] | 1145 | hasVisibleWindows:(BOOL)hasVisibleWindows { |
[email protected] | 7063140 | 2010-04-26 20:16:54 | [diff] [blame] | 1146 | // If the browser is currently trying to quit, don't do anything and return NO |
| 1147 | // to prevent AppKit from doing anything. |
| 1148 | // TODO(rohitrao): Remove this code when http://crbug.com/40861 is resolved. |
| 1149 | if (browser_shutdown::IsTryingToQuit()) |
| 1150 | return NO; |
| 1151 | |
[email protected] | 2e29e223 | 2013-07-26 10:40:59 | [diff] [blame] | 1152 | // Bring all browser windows to the front. Specifically, this brings them in |
| 1153 | // front of any app windows. FocusWindowSet will also unminimize the most |
| 1154 | // recently minimized window if no windows in the set are visible. |
[email protected] | a25920ee | 2013-09-05 19:38:49 | [diff] [blame] | 1155 | // If there are any, return here. Otherwise, the windows are panels or |
| 1156 | // notifications so we still need to open a new window. |
[email protected] | bf193bc5 | 2013-06-08 04:39:58 | [diff] [blame] | 1157 | if (hasVisibleWindows) { |
[email protected] | 2e29e223 | 2013-07-26 10:40:59 | [diff] [blame] | 1158 | std::set<NSWindow*> browserWindows; |
[email protected] | b4207c4 | 2013-02-12 06:44:20 | [diff] [blame] | 1159 | for (chrome::BrowserIterator iter; !iter.done(); iter.Next()) { |
[email protected] | 0236be2 | 2012-01-11 01:05:35 | [diff] [blame] | 1160 | Browser* browser = *iter; |
[email protected] | 2e29e223 | 2013-07-26 10:40:59 | [diff] [blame] | 1161 | browserWindows.insert(browser->window()->GetNativeWindow()); |
[email protected] | 0236be2 | 2012-01-11 01:05:35 | [diff] [blame] | 1162 | } |
[email protected] | a25920ee | 2013-09-05 19:38:49 | [diff] [blame] | 1163 | if (!browserWindows.empty()) { |
[email protected] | d4c811e7 | 2013-10-29 21:57:55 | [diff] [blame] | 1164 | NSWindow* keyWindow = [NSApp keyWindow]; |
| 1165 | if (keyWindow && ![keyWindow isOnActiveSpace]) { |
| 1166 | // The key window is not on the active space. We must be mid-animation |
| 1167 | // for a space transition triggered by the dock. Delay the call to |
| 1168 | // |ui::FocusWindowSet| until the transition completes. Otherwise, the |
| 1169 | // wrong space's windows get raised, resulting in an off-screen key |
| 1170 | // window. It does not work to |ui::FocusWindowSet| twice, once here |
| 1171 | // and once in |activeSpaceDidChange:|, as that appears to break when |
| 1172 | // the omnibox is focused. |
| 1173 | // |
| 1174 | // This check relies on OS X setting the key window to a window on the |
| 1175 | // target space before calling this method. |
| 1176 | // |
| 1177 | // See http://crbug.com/309656. |
| 1178 | reopenTime_ = base::TimeTicks::Now(); |
| 1179 | } else { |
[email protected] | 6c4b7960 | 2014-05-28 16:47:21 | [diff] [blame] | 1180 | ui::FocusWindowSetOnCurrentSpace(browserWindows); |
[email protected] | d4c811e7 | 2013-10-29 21:57:55 | [diff] [blame] | 1181 | } |
| 1182 | // Return NO; we've done (or soon will do) the deminiaturize, so |
| 1183 | // AppKit shouldn't do anything. |
[email protected] | a25920ee | 2013-09-05 19:38:49 | [diff] [blame] | 1184 | return NO; |
| 1185 | } |
[email protected] | 0236be2 | 2012-01-11 01:05:35 | [diff] [blame] | 1186 | } |
[email protected] | ff81e0a | 2009-04-13 14:58:54 | [diff] [blame] | 1187 | |
[email protected] | 0d1210d6 | 2010-09-07 17:31:38 | [diff] [blame] | 1188 | // If launched as a hidden login item (due to installation of a persistent app |
[email protected] | eddcf750 | 2012-02-09 22:43:48 | [diff] [blame] | 1189 | // or by the user, for example in System Preferences->Accounts->Login Items), |
[email protected] | 0d1210d6 | 2010-09-07 17:31:38 | [diff] [blame] | 1190 | // allow session to be restored first time the user clicks on a Dock icon. |
| 1191 | // Normally, it'd just open a new empty page. |
| 1192 | { |
[email protected] | eddcf750 | 2012-02-09 22:43:48 | [diff] [blame] | 1193 | static BOOL doneOnce = NO; |
[email protected] | 70593e5 | 2014-05-15 08:30:23 | [diff] [blame] | 1194 | BOOL attemptRestore = apps::AppShimHandler::ShouldRestoreSession() || |
| 1195 | (!doneOnce && base::mac::WasLaunchedAsHiddenLoginItem()); |
| 1196 | doneOnce = YES; |
| 1197 | if (attemptRestore) { |
| 1198 | SessionService* sessionService = |
| 1199 | SessionServiceFactory::GetForProfileForSessionRestore( |
| 1200 | [self lastProfile]); |
| 1201 | if (sessionService && |
| 1202 | sessionService->RestoreIfNecessary(std::vector<GURL>())) |
| 1203 | return NO; |
[email protected] | eddcf750 | 2012-02-09 22:43:48 | [diff] [blame] | 1204 | } |
[email protected] | 0d1210d6 | 2010-09-07 17:31:38 | [diff] [blame] | 1205 | } |
[email protected] | fc44f24 | 2012-02-14 16:54:39 | [diff] [blame] | 1206 | |
[email protected] | ff81e0a | 2009-04-13 14:58:54 | [diff] [blame] | 1207 | // Otherwise open a new window. |
[email protected] | 7108d91 | 2014-01-30 08:10:45 | [diff] [blame] | 1208 | // If the last profile was locked, we have to open the User Manager, as the |
| 1209 | // profile requires authentication. Similarly, because guest mode is |
| 1210 | // implemented as forced incognito, we can't open a new guest browser either, |
| 1211 | // so we have to show the User Manager as well. |
| 1212 | Profile* lastProfile = [self lastProfile]; |
mlerman | e29d003 | 2014-09-24 19:31:26 | [diff] [blame^] | 1213 | if (lastProfile->IsGuestSession() || IsProfileSignedOut(lastProfile)) { |
| 1214 | UserManager::Show(lastProfile->GetPath(), |
| 1215 | profiles::USER_MANAGER_NO_TUTORIAL, |
| 1216 | profiles::USER_MANAGER_SELECT_PROFILE_NO_ACTION); |
| 1217 | } else { |
[email protected] | 7108d91 | 2014-01-30 08:10:45 | [diff] [blame] | 1218 | CreateBrowser(lastProfile); |
mlerman | e29d003 | 2014-09-24 19:31:26 | [diff] [blame^] | 1219 | } |
[email protected] | ff81e0a | 2009-04-13 14:58:54 | [diff] [blame] | 1220 | |
| 1221 | // We've handled the reopen event, so return NO to tell AppKit not |
| 1222 | // to do anything. |
| 1223 | return NO; |
| 1224 | } |
| 1225 | |
[email protected] | 88d7494 | 2009-01-21 22:04:44 | [diff] [blame] | 1226 | - (void)initMenuState { |
[email protected] | 3111f08b | 2009-04-30 16:01:52 | [diff] [blame] | 1227 | menuState_.reset(new CommandUpdater(NULL)); |
[email protected] | fbc947b | 2009-06-19 13:28:24 | [diff] [blame] | 1228 | menuState_->UpdateCommandEnabled(IDC_NEW_TAB, true); |
[email protected] | 88d7494 | 2009-01-21 22:04:44 | [diff] [blame] | 1229 | menuState_->UpdateCommandEnabled(IDC_NEW_WINDOW, true); |
[email protected] | 863ff66 | 2009-01-26 20:18:18 | [diff] [blame] | 1230 | menuState_->UpdateCommandEnabled(IDC_NEW_INCOGNITO_WINDOW, true); |
[email protected] | e19516d | 2009-04-28 17:15:19 | [diff] [blame] | 1231 | menuState_->UpdateCommandEnabled(IDC_OPEN_FILE, true); |
[email protected] | 93e18176 | 2009-05-29 14:30:38 | [diff] [blame] | 1232 | menuState_->UpdateCommandEnabled(IDC_CLEAR_BROWSING_DATA, true); |
[email protected] | fbc947b | 2009-06-19 13:28:24 | [diff] [blame] | 1233 | menuState_->UpdateCommandEnabled(IDC_RESTORE_TAB, false); |
[email protected] | 91a5b3e | 2009-10-30 19:32:22 | [diff] [blame] | 1234 | menuState_->UpdateCommandEnabled(IDC_FOCUS_LOCATION, true); |
[email protected] | cddc524 | 2009-12-07 18:54:33 | [diff] [blame] | 1235 | menuState_->UpdateCommandEnabled(IDC_FOCUS_SEARCH, true); |
[email protected] | af7c25e | 2010-01-23 05:46:45 | [diff] [blame] | 1236 | menuState_->UpdateCommandEnabled(IDC_SHOW_BOOKMARK_MANAGER, true); |
[email protected] | 91a5b3e | 2009-10-30 19:32:22 | [diff] [blame] | 1237 | menuState_->UpdateCommandEnabled(IDC_SHOW_HISTORY, true); |
| 1238 | menuState_->UpdateCommandEnabled(IDC_SHOW_DOWNLOADS, true); |
[email protected] | bb37e6ec | 2009-11-18 17:59:36 | [diff] [blame] | 1239 | menuState_->UpdateCommandEnabled(IDC_MANAGE_EXTENSIONS, true); |
[email protected] | 2ca3561 | 2012-06-12 03:49:48 | [diff] [blame] | 1240 | menuState_->UpdateCommandEnabled(IDC_HELP_PAGE_VIA_MENU, true); |
[email protected] | 4aee565 | 2009-11-19 18:59:21 | [diff] [blame] | 1241 | menuState_->UpdateCommandEnabled(IDC_IMPORT_SETTINGS, true); |
[email protected] | 236ad302 | 2013-09-04 03:27:43 | [diff] [blame] | 1242 | #if defined(GOOGLE_CHROME_BUILD) |
[email protected] | 1c41a93 | 2010-11-22 23:41:23 | [diff] [blame] | 1243 | menuState_->UpdateCommandEnabled(IDC_FEEDBACK, true); |
[email protected] | 236ad302 | 2013-09-04 03:27:43 | [diff] [blame] | 1244 | #endif |
[email protected] | 6554918 | 2012-02-25 00:45:40 | [diff] [blame] | 1245 | menuState_->UpdateCommandEnabled(IDC_SHOW_SYNC_SETUP, true); |
[email protected] | ceaaf181 | 2010-01-20 22:13:09 | [diff] [blame] | 1246 | menuState_->UpdateCommandEnabled(IDC_TASK_MANAGER, true); |
[email protected] | 88d7494 | 2009-01-21 22:04:44 | [diff] [blame] | 1247 | } |
[email protected] | 1bcdb53 | 2009-01-16 17:47:57 | [diff] [blame] | 1248 | |
[email protected] | 961a693 | 2011-07-19 19:52:46 | [diff] [blame] | 1249 | // Conditionally adds the Profile menu to the main menu bar. |
| 1250 | - (void)initProfileMenu { |
[email protected] | 961a693 | 2011-07-19 19:52:46 | [diff] [blame] | 1251 | NSMenu* mainMenu = [NSApp mainMenu]; |
| 1252 | NSMenuItem* profileMenu = [mainMenu itemWithTag:IDC_PROFILE_MAIN_MENU]; |
[email protected] | e354ca4 | 2011-08-03 20:31:40 | [diff] [blame] | 1253 | |
[email protected] | de71ae99 | 2013-07-18 03:30:38 | [diff] [blame] | 1254 | if (!profiles::IsMultipleProfilesEnabled()) { |
[email protected] | e354ca4 | 2011-08-03 20:31:40 | [diff] [blame] | 1255 | [mainMenu removeItem:profileMenu]; |
| 1256 | return; |
| 1257 | } |
| 1258 | |
[email protected] | 6b2d30bc | 2011-10-11 18:16:39 | [diff] [blame] | 1259 | // The controller will unhide the menu if necessary. |
| 1260 | [profileMenu setHidden:YES]; |
[email protected] | 961a693 | 2011-07-19 19:52:46 | [diff] [blame] | 1261 | |
[email protected] | 6b2d30bc | 2011-10-11 18:16:39 | [diff] [blame] | 1262 | profileMenuController_.reset( |
| 1263 | [[ProfileMenuController alloc] initWithMainMenuItem:profileMenu]); |
[email protected] | 961a693 | 2011-07-19 19:52:46 | [diff] [blame] | 1264 | } |
| 1265 | |
[email protected] | a99fce0e | 2011-03-21 20:58:48 | [diff] [blame] | 1266 | // The Confirm to Quit preference is atypical in that the preference lives in |
| 1267 | // the app menu right above the Quit menu item. This method will refresh the |
| 1268 | // display of that item depending on the preference state. |
| 1269 | - (void)updateConfirmToQuitPrefMenuItem:(NSMenuItem*)item { |
| 1270 | // Format the string so that the correct key equivalent is displayed. |
| 1271 | NSString* acceleratorString = [ConfirmQuitPanelController keyCommandString]; |
| 1272 | NSString* title = l10n_util::GetNSStringF(IDS_CONFIRM_TO_QUIT_OPTION, |
| 1273 | base::SysNSStringToUTF16(acceleratorString)); |
| 1274 | [item setTitle:title]; |
| 1275 | |
[email protected] | b6366ffa | 2012-02-29 17:12:26 | [diff] [blame] | 1276 | const PrefService* prefService = g_browser_process->local_state(); |
[email protected] | a99fce0e | 2011-03-21 20:58:48 | [diff] [blame] | 1277 | bool enabled = prefService->GetBoolean(prefs::kConfirmToQuitEnabled); |
| 1278 | [item setState:enabled ? NSOnState : NSOffState]; |
| 1279 | } |
| 1280 | |
[email protected] | e0b0d1ba | 2014-04-24 03:22:29 | [diff] [blame] | 1281 | - (void)updateDisplayMessageCenterPrefMenuItem:(NSMenuItem*)item { |
| 1282 | const PrefService* prefService = g_browser_process->local_state(); |
| 1283 | bool enabled = prefService->GetBoolean(prefs::kMessageCenterShowIcon); |
| 1284 | // The item should be checked if "show icon" is false, since the text reads |
| 1285 | // "Hide notification center icon." |
| 1286 | [item setState:enabled ? NSOffState : NSOnState]; |
| 1287 | } |
| 1288 | |
[email protected] | eef99c2 | 2010-08-17 05:55:16 | [diff] [blame] | 1289 | - (void)registerServicesMenuTypesTo:(NSApplication*)app { |
[email protected] | 09ea24c | 2010-08-23 19:05:41 | [diff] [blame] | 1290 | // Note that RenderWidgetHostViewCocoa implements NSServicesRequests which |
| 1291 | // handles requests from services. |
| 1292 | NSArray* types = [NSArray arrayWithObjects:NSStringPboardType, nil]; |
| 1293 | [app registerServicesMenuSendTypes:types returnTypes:types]; |
[email protected] | eef99c2 | 2010-08-17 05:55:16 | [diff] [blame] | 1294 | } |
| 1295 | |
[email protected] | a585a8db | 2011-06-20 18:58:35 | [diff] [blame] | 1296 | - (Profile*)lastProfile { |
[email protected] | 7fd5a46 | 2011-06-19 16:57:23 | [diff] [blame] | 1297 | // Return the profile of the last-used BrowserWindowController, if available. |
| 1298 | if (lastProfile_) |
| 1299 | return lastProfile_; |
| 1300 | |
[email protected] | fbb0455 | 2014-01-16 15:47:38 | [diff] [blame] | 1301 | // On first launch, use the logic that ChromeBrowserMain uses to determine |
| 1302 | // the initial profile. |
| 1303 | ProfileManager* profile_manager = g_browser_process->profile_manager(); |
| 1304 | if (!profile_manager) |
| 1305 | return NULL; |
[email protected] | 3f34599d | 2009-03-25 22:11:43 | [diff] [blame] | 1306 | |
[email protected] | fbb0455 | 2014-01-16 15:47:38 | [diff] [blame] | 1307 | return profile_manager->GetProfile(GetStartupProfilePath( |
| 1308 | profile_manager->user_data_dir(), |
| 1309 | *CommandLine::ForCurrentProcess())); |
[email protected] | 3f34599d | 2009-03-25 22:11:43 | [diff] [blame] | 1310 | } |
| 1311 | |
[email protected] | 2a39d1a | 2014-04-10 05:46:24 | [diff] [blame] | 1312 | - (Profile*)safeLastProfileForNewWindows { |
| 1313 | Profile* profile = [self lastProfile]; |
| 1314 | |
| 1315 | // Guest sessions must always be OffTheRecord. Use that when opening windows. |
| 1316 | if (profile->IsGuestSession()) |
| 1317 | return profile->GetOffTheRecordProfile(); |
| 1318 | |
| 1319 | return profile; |
| 1320 | } |
| 1321 | |
[email protected] | 57750f82 | 2009-04-21 21:43:09 | [diff] [blame] | 1322 | // Various methods to open URLs that we get in a native fashion. We use |
[email protected] | fe7c487 | 2012-05-10 20:06:03 | [diff] [blame] | 1323 | // StartupBrowserCreator here because on the other platforms, URLs to open come |
| 1324 | // through the ProcessSingleton, and it calls StartupBrowserCreator. It's best |
| 1325 | // to bottleneck the openings through that for uniform handling. |
[email protected] | 57750f82 | 2009-04-21 21:43:09 | [diff] [blame] | 1326 | |
[email protected] | d630d7d5 | 2010-02-21 00:55:11 | [diff] [blame] | 1327 | - (void)openUrls:(const std::vector<GURL>&)urls { |
[email protected] | caadfca | 2010-01-22 21:51:21 | [diff] [blame] | 1328 | // If the browser hasn't started yet, just queue up the URLs. |
| 1329 | if (!startupComplete_) { |
[email protected] | d630d7d5 | 2010-02-21 00:55:11 | [diff] [blame] | 1330 | startupUrls_.insert(startupUrls_.end(), urls.begin(), urls.end()); |
[email protected] | 136140c | 2009-05-19 13:58:25 | [diff] [blame] | 1331 | return; |
| 1332 | } |
[email protected] | 57750f82 | 2009-04-21 21:43:09 | [diff] [blame] | 1333 | |
[email protected] | f2bc6e9 | 2012-10-14 02:08:38 | [diff] [blame] | 1334 | Browser* browser = chrome::GetLastActiveBrowser(); |
[email protected] | 0e86351 | 2009-05-28 19:45:07 | [diff] [blame] | 1335 | // if no browser window exists then create one with no tabs to be filled in |
| 1336 | if (!browser) { |
[email protected] | c125cdc | 2013-02-25 19:10:21 | [diff] [blame] | 1337 | browser = new Browser(Browser::CreateParams( |
| 1338 | [self lastProfile], chrome::HOST_DESKTOP_TYPE_NATIVE)); |
[email protected] | ce560f8 | 2009-06-03 09:39:44 | [diff] [blame] | 1339 | browser->window()->Show(); |
[email protected] | 0e86351 | 2009-05-28 19:45:07 | [diff] [blame] | 1340 | } |
| 1341 | |
[email protected] | 947446b | 2010-10-21 03:36:31 | [diff] [blame] | 1342 | CommandLine dummy(CommandLine::NO_PROGRAM); |
[email protected] | 4feb37d9 | 2012-07-01 20:22:13 | [diff] [blame] | 1343 | chrome::startup::IsFirstRun first_run = first_run::IsChromeFirstRun() ? |
| 1344 | chrome::startup::IS_FIRST_RUN : chrome::startup::IS_NOT_FIRST_RUN; |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 1345 | StartupBrowserCreatorImpl launch(base::FilePath(), dummy, first_run); |
[email protected] | 921071f85 | 2013-05-25 03:22:08 | [diff] [blame] | 1346 | launch.OpenURLsInBrowser(browser, false, urls, browser->host_desktop_type()); |
[email protected] | 57750f82 | 2009-04-21 21:43:09 | [diff] [blame] | 1347 | } |
| 1348 | |
[email protected] | 57750f82 | 2009-04-21 21:43:09 | [diff] [blame] | 1349 | - (void)getUrl:(NSAppleEventDescriptor*)event |
| 1350 | withReply:(NSAppleEventDescriptor*)reply { |
| 1351 | NSString* urlStr = [[event paramDescriptorForKeyword:keyDirectObject] |
| 1352 | stringValue]; |
| 1353 | |
| 1354 | GURL gurl(base::SysNSStringToUTF8(urlStr)); |
| 1355 | std::vector<GURL> gurlVector; |
| 1356 | gurlVector.push_back(gurl); |
| 1357 | |
[email protected] | d630d7d5 | 2010-02-21 00:55:11 | [diff] [blame] | 1358 | [self openUrls:gurlVector]; |
[email protected] | 57750f82 | 2009-04-21 21:43:09 | [diff] [blame] | 1359 | } |
| 1360 | |
[email protected] | caadfca | 2010-01-22 21:51:21 | [diff] [blame] | 1361 | - (void)application:(NSApplication*)sender |
| 1362 | openFiles:(NSArray*)filenames { |
[email protected] | 57750f82 | 2009-04-21 21:43:09 | [diff] [blame] | 1363 | std::vector<GURL> gurlVector; |
[email protected] | caadfca | 2010-01-22 21:51:21 | [diff] [blame] | 1364 | for (NSString* file in filenames) { |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 1365 | GURL gurl = |
[email protected] | 77d8354 | 2013-12-13 08:54:29 | [diff] [blame] | 1366 | net::FilePathToFileURL(base::FilePath([file fileSystemRepresentation])); |
[email protected] | 57750f82 | 2009-04-21 21:43:09 | [diff] [blame] | 1367 | gurlVector.push_back(gurl); |
| 1368 | } |
[email protected] | caadfca | 2010-01-22 21:51:21 | [diff] [blame] | 1369 | if (!gurlVector.empty()) |
[email protected] | d630d7d5 | 2010-02-21 00:55:11 | [diff] [blame] | 1370 | [self openUrls:gurlVector]; |
[email protected] | caadfca | 2010-01-22 21:51:21 | [diff] [blame] | 1371 | else |
| 1372 | NOTREACHED() << "Nothing to open!"; |
[email protected] | 57750f82 | 2009-04-21 21:43:09 | [diff] [blame] | 1373 | |
[email protected] | caadfca | 2010-01-22 21:51:21 | [diff] [blame] | 1374 | [sender replyToOpenOrPrint:NSApplicationDelegateReplySuccess]; |
[email protected] | 57750f82 | 2009-04-21 21:43:09 | [diff] [blame] | 1375 | } |
[email protected] | 3f34599d | 2009-03-25 22:11:43 | [diff] [blame] | 1376 | |
[email protected] | 3111f08b | 2009-04-30 16:01:52 | [diff] [blame] | 1377 | // Show the preferences window, or bring it to the front if it's already |
| 1378 | // visible. |
| 1379 | - (IBAction)showPreferences:(id)sender { |
[email protected] | a585a8db | 2011-06-20 18:58:35 | [diff] [blame] | 1380 | if (Browser* browser = ActivateBrowser([self lastProfile])) { |
[email protected] | 44517765 | 2011-03-09 02:04:05 | [diff] [blame] | 1381 | // Show options tab in the active browser window. |
[email protected] | 5d9cace7 | 2012-06-21 16:07:12 | [diff] [blame] | 1382 | chrome::ShowSettings(browser); |
[email protected] | 4a42d27 | 2010-06-18 01:29:42 | [diff] [blame] | 1383 | } else { |
[email protected] | 44517765 | 2011-03-09 02:04:05 | [diff] [blame] | 1384 | // No browser window, so create one for the options tab. |
[email protected] | 2a39d1a | 2014-04-10 05:46:24 | [diff] [blame] | 1385 | chrome::OpenOptionsWindow([self safeLastProfileForNewWindows]); |
[email protected] | 4a42d27 | 2010-06-18 01:29:42 | [diff] [blame] | 1386 | } |
[email protected] | d989f07c | 2009-11-14 00:35:46 | [diff] [blame] | 1387 | } |
| 1388 | |
[email protected] | bde3dda | 2009-05-20 22:13:07 | [diff] [blame] | 1389 | - (IBAction)orderFrontStandardAboutPanel:(id)sender { |
[email protected] | a048ad2 | 2012-03-23 04:26:56 | [diff] [blame] | 1390 | if (Browser* browser = ActivateBrowser([self lastProfile])) { |
[email protected] | 5d9cace7 | 2012-06-21 16:07:12 | [diff] [blame] | 1391 | chrome::ShowAboutChrome(browser); |
[email protected] | a9fbb096 | 2012-03-21 23:25:13 | [diff] [blame] | 1392 | } else { |
[email protected] | a048ad2 | 2012-03-23 04:26:56 | [diff] [blame] | 1393 | // No browser window, so create one for the about tab. |
[email protected] | 2a39d1a | 2014-04-10 05:46:24 | [diff] [blame] | 1394 | chrome::OpenAboutWindow([self safeLastProfileForNewWindows]); |
[email protected] | bde3dda | 2009-05-20 22:13:07 | [diff] [blame] | 1395 | } |
[email protected] | bde3dda | 2009-05-20 22:13:07 | [diff] [blame] | 1396 | } |
| 1397 | |
[email protected] | a99fce0e | 2011-03-21 20:58:48 | [diff] [blame] | 1398 | - (IBAction)toggleConfirmToQuit:(id)sender { |
[email protected] | b6366ffa | 2012-02-29 17:12:26 | [diff] [blame] | 1399 | PrefService* prefService = g_browser_process->local_state(); |
[email protected] | a99fce0e | 2011-03-21 20:58:48 | [diff] [blame] | 1400 | bool enabled = prefService->GetBoolean(prefs::kConfirmToQuitEnabled); |
| 1401 | prefService->SetBoolean(prefs::kConfirmToQuitEnabled, !enabled); |
| 1402 | } |
| 1403 | |
[email protected] | e0b0d1ba | 2014-04-24 03:22:29 | [diff] [blame] | 1404 | - (IBAction)toggleDisplayMessageCenter:(id)sender { |
| 1405 | PrefService* prefService = g_browser_process->local_state(); |
| 1406 | bool enabled = prefService->GetBoolean(prefs::kMessageCenterShowIcon); |
| 1407 | prefService->SetBoolean(prefs::kMessageCenterShowIcon, !enabled); |
| 1408 | } |
| 1409 | |
[email protected] | 3d4bd23f | 2009-10-06 03:58:38 | [diff] [blame] | 1410 | // Explicitly bring to the foreground when creating new windows from the dock. |
[email protected] | 93b59fc | 2010-12-21 20:00:47 | [diff] [blame] | 1411 | - (void)commandFromDock:(id)sender { |
[email protected] | 3d4bd23f | 2009-10-06 03:58:38 | [diff] [blame] | 1412 | [NSApp activateIgnoringOtherApps:YES]; |
| 1413 | [self commandDispatch:sender]; |
| 1414 | } |
| 1415 | |
[email protected] | 227fcee | 2010-06-11 19:19:37 | [diff] [blame] | 1416 | - (NSMenu*)applicationDockMenu:(NSApplication*)sender { |
[email protected] | 018a396 | 2009-09-17 22:23:44 | [diff] [blame] | 1417 | NSMenu* dockMenu = [[[NSMenu alloc] initWithTitle: @""] autorelease]; |
[email protected] | a585a8db | 2011-06-20 18:58:35 | [diff] [blame] | 1418 | Profile* profile = [self lastProfile]; |
[email protected] | 93b59fc | 2010-12-21 20:00:47 | [diff] [blame] | 1419 | |
[email protected] | 646fadbf | 2012-09-08 01:49:01 | [diff] [blame] | 1420 | BOOL profilesAdded = [profileMenuController_ insertItemsIntoMenu:dockMenu |
| 1421 | atOffset:0 |
| 1422 | fromDock:YES]; |
| 1423 | if (profilesAdded) |
| 1424 | [dockMenu addItem:[NSMenuItem separatorItem]]; |
[email protected] | 3d272f5 | 2012-08-23 19:00:33 | [diff] [blame] | 1425 | |
[email protected] | 646fadbf | 2012-09-08 01:49:01 | [diff] [blame] | 1426 | NSString* titleStr = l10n_util::GetNSStringWithFixup(IDS_NEW_WINDOW_MAC); |
[email protected] | a852203 | 2013-06-24 22:51:46 | [diff] [blame] | 1427 | base::scoped_nsobject<NSMenuItem> item( |
[email protected] | 8c6b029d | 2011-01-21 22:39:51 | [diff] [blame] | 1428 | [[NSMenuItem alloc] initWithTitle:titleStr |
| 1429 | action:@selector(commandFromDock:) |
| 1430 | keyEquivalent:@""]); |
[email protected] | 288bfcd3 | 2009-09-14 18:14:46 | [diff] [blame] | 1431 | [item setTarget:self]; |
[email protected] | 646fadbf | 2012-09-08 01:49:01 | [diff] [blame] | 1432 | [item setTag:IDC_NEW_WINDOW]; |
[email protected] | 0545453 | 2013-01-22 21:09:08 | [diff] [blame] | 1433 | [item setEnabled:[self validateUserInterfaceItem:item]]; |
[email protected] | 646fadbf | 2012-09-08 01:49:01 | [diff] [blame] | 1434 | [dockMenu addItem:item]; |
| 1435 | |
[email protected] | 8d10a41 | 2013-08-09 19:18:00 | [diff] [blame] | 1436 | // |profile| can be NULL during unit tests. |
[email protected] | d20d043 | 2014-06-12 17:14:05 | [diff] [blame] | 1437 | if (!profile || !profile->IsSupervised()) { |
[email protected] | 8d10a41 | 2013-08-09 19:18:00 | [diff] [blame] | 1438 | titleStr = l10n_util::GetNSStringWithFixup(IDS_NEW_INCOGNITO_WINDOW_MAC); |
| 1439 | item.reset( |
| 1440 | [[NSMenuItem alloc] initWithTitle:titleStr |
| 1441 | action:@selector(commandFromDock:) |
| 1442 | keyEquivalent:@""]); |
| 1443 | [item setTarget:self]; |
| 1444 | [item setTag:IDC_NEW_INCOGNITO_WINDOW]; |
| 1445 | [item setEnabled:[self validateUserInterfaceItem:item]]; |
| 1446 | [dockMenu addItem:item]; |
| 1447 | } |
[email protected] | 288bfcd3 | 2009-09-14 18:14:46 | [diff] [blame] | 1448 | |
[email protected] | 8c6b029d | 2011-01-21 22:39:51 | [diff] [blame] | 1449 | // TODO(rickcam): Mock out BackgroundApplicationListModel, then add unit |
| 1450 | // tests which use the mock in place of the profile-initialized model. |
| 1451 | |
| 1452 | // Avoid breaking unit tests which have no profile. |
| 1453 | if (profile) { |
| 1454 | BackgroundApplicationListModel applications(profile); |
| 1455 | if (applications.size()) { |
| 1456 | int position = 0; |
| 1457 | NSString* menuStr = |
| 1458 | l10n_util::GetNSStringWithFixup(IDS_BACKGROUND_APPS_MAC); |
[email protected] | a852203 | 2013-06-24 22:51:46 | [diff] [blame] | 1459 | base::scoped_nsobject<NSMenu> appMenu( |
| 1460 | [[NSMenu alloc] initWithTitle:menuStr]); |
[email protected] | 1c321ee | 2012-05-21 03:02:34 | [diff] [blame] | 1461 | for (extensions::ExtensionList::const_iterator cursor = |
[email protected] | 3a90b45 | 2013-02-15 04:33:48 | [diff] [blame] | 1462 | applications.begin(); |
[email protected] | 8c6b029d | 2011-01-21 22:39:51 | [diff] [blame] | 1463 | cursor != applications.end(); |
| 1464 | ++cursor, ++position) { |
[email protected] | f15d9ebc | 2013-07-02 00:14:50 | [diff] [blame] | 1465 | DCHECK_EQ(applications.GetPosition(cursor->get()), position); |
[email protected] | 8c6b029d | 2011-01-21 22:39:51 | [diff] [blame] | 1466 | NSString* itemStr = |
[email protected] | 670d323 | 2013-12-24 17:58:58 | [diff] [blame] | 1467 | base::SysUTF16ToNSString(base::UTF8ToUTF16((*cursor)->name())); |
[email protected] | a852203 | 2013-06-24 22:51:46 | [diff] [blame] | 1468 | base::scoped_nsobject<NSMenuItem> appItem( |
| 1469 | [[NSMenuItem alloc] initWithTitle:itemStr |
| 1470 | action:@selector(executeApplication:) |
| 1471 | keyEquivalent:@""]); |
[email protected] | 8c6b029d | 2011-01-21 22:39:51 | [diff] [blame] | 1472 | [appItem setTarget:self]; |
| 1473 | [appItem setTag:position]; |
| 1474 | [appMenu addItem:appItem]; |
| 1475 | } |
[email protected] | 8c6b029d | 2011-01-21 22:39:51 | [diff] [blame] | 1476 | } |
| 1477 | } |
| 1478 | |
[email protected] | 018a396 | 2009-09-17 22:23:44 | [diff] [blame] | 1479 | return dockMenu; |
[email protected] | 288bfcd3 | 2009-09-14 18:14:46 | [diff] [blame] | 1480 | } |
| 1481 | |
[email protected] | d630d7d5 | 2010-02-21 00:55:11 | [diff] [blame] | 1482 | - (const std::vector<GURL>&)startupUrls { |
| 1483 | return startupUrls_; |
[email protected] | caadfca | 2010-01-22 21:51:21 | [diff] [blame] | 1484 | } |
| 1485 | |
[email protected] | 7e4c69d0 | 2011-05-11 22:16:47 | [diff] [blame] | 1486 | - (BookmarkMenuBridge*)bookmarkMenuBridge { |
| 1487 | return bookmarkMenuBridge_.get(); |
| 1488 | } |
| 1489 | |
[email protected] | 33190c8 | 2012-04-10 17:04:44 | [diff] [blame] | 1490 | - (void)addObserverForWorkAreaChange:(ui::WorkAreaWatcherObserver*)observer { |
| 1491 | workAreaChangeObservers_.AddObserver(observer); |
| 1492 | } |
| 1493 | |
| 1494 | - (void)removeObserverForWorkAreaChange:(ui::WorkAreaWatcherObserver*)observer { |
| 1495 | workAreaChangeObservers_.RemoveObserver(observer); |
| 1496 | } |
| 1497 | |
[email protected] | e7dc399 | 2013-11-06 02:16:42 | [diff] [blame] | 1498 | - (void)initAppShimMenuController { |
[email protected] | e4c63ec | 2014-01-14 15:50:54 | [diff] [blame] | 1499 | if (!appShimMenuController_) |
[email protected] | e7dc399 | 2013-11-06 02:16:42 | [diff] [blame] | 1500 | appShimMenuController_.reset([[AppShimMenuController alloc] init]); |
| 1501 | } |
| 1502 | |
noms | dd5b6a61 | 2014-09-23 15:20:16 | [diff] [blame] | 1503 | - (void)windowChangedToProfile:(Profile*)profile { |
| 1504 | if (lastProfile_ == profile) |
| 1505 | return; |
| 1506 | |
| 1507 | // Before tearing down the menu controller bridges, return the Cocoa menus to |
| 1508 | // their initial state. |
| 1509 | if (bookmarkMenuBridge_.get()) |
| 1510 | bookmarkMenuBridge_->ResetMenu(); |
| 1511 | if (historyMenuBridge_.get()) |
| 1512 | historyMenuBridge_->ResetMenu(); |
| 1513 | |
| 1514 | // Rebuild the menus with the new profile. |
| 1515 | lastProfile_ = profile; |
| 1516 | |
| 1517 | bookmarkMenuBridge_.reset(new BookmarkMenuBridge(lastProfile_, |
| 1518 | [[[NSApp mainMenu] itemWithTag:IDC_BOOKMARKS_MENU] submenu])); |
| 1519 | // No need to |BuildMenu| here. It is done lazily upon menu access. |
| 1520 | |
| 1521 | historyMenuBridge_.reset(new HistoryMenuBridge(lastProfile_)); |
| 1522 | historyMenuBridge_->BuildMenu(); |
| 1523 | |
| 1524 | chrome::BrowserCommandController:: |
| 1525 | UpdateSharedCommandsForIncognitoAvailability( |
| 1526 | menuState_.get(), lastProfile_); |
| 1527 | profilePrefRegistrar_.reset(new PrefChangeRegistrar()); |
| 1528 | profilePrefRegistrar_->Init(lastProfile_->GetPrefs()); |
| 1529 | profilePrefRegistrar_->Add( |
| 1530 | prefs::kIncognitoModeAvailability, |
| 1531 | base::Bind(&chrome::BrowserCommandController:: |
| 1532 | UpdateSharedCommandsForIncognitoAvailability, |
| 1533 | menuState_.get(), |
| 1534 | lastProfile_)); |
| 1535 | } |
| 1536 | |
[email protected] | ebbe94b | 2012-01-18 20:11:56 | [diff] [blame] | 1537 | - (void)applicationDidChangeScreenParameters:(NSNotification*)notification { |
[email protected] | 6a45507 | 2011-11-02 01:32:28 | [diff] [blame] | 1538 | // During this callback the working area is not always already updated. Defer. |
[email protected] | 33190c8 | 2012-04-10 17:04:44 | [diff] [blame] | 1539 | [self performSelector:@selector(delayedScreenParametersUpdate) |
[email protected] | 6a45507 | 2011-11-02 01:32:28 | [diff] [blame] | 1540 | withObject:nil |
| 1541 | afterDelay:0]; |
| 1542 | } |
| 1543 | |
[email protected] | 33190c8 | 2012-04-10 17:04:44 | [diff] [blame] | 1544 | - (void)delayedScreenParametersUpdate { |
| 1545 | FOR_EACH_OBSERVER(ui::WorkAreaWatcherObserver, workAreaChangeObservers_, |
| 1546 | WorkAreaChanged()); |
[email protected] | 6a45507 | 2011-11-02 01:32:28 | [diff] [blame] | 1547 | } |
| 1548 | |
[email protected] | 92747fe4 | 2014-08-18 23:25:26 | [diff] [blame] | 1549 | - (BOOL)application:(NSApplication*)application |
| 1550 | willContinueUserActivityWithType:(NSString*)userActivityType { |
| 1551 | return [userActivityType isEqualToString:NSUserActivityTypeBrowsingWeb]; |
| 1552 | } |
| 1553 | |
| 1554 | - (BOOL)application:(NSApplication*)application |
| 1555 | continueUserActivity:(NSUserActivity*)userActivity |
| 1556 | restorationHandler:(void (^)(NSArray*))restorationHandler { |
| 1557 | if (![userActivity.activityType |
| 1558 | isEqualToString:NSUserActivityTypeBrowsingWeb]) { |
| 1559 | return NO; |
| 1560 | } |
| 1561 | |
| 1562 | NSURL* url = userActivity.webPageURL; |
| 1563 | if (!url) |
| 1564 | return NO; |
| 1565 | |
| 1566 | GURL gurl(base::SysNSStringToUTF8([url absoluteString])); |
| 1567 | std::vector<GURL> gurlVector; |
| 1568 | gurlVector.push_back(gurl); |
| 1569 | |
| 1570 | [self openUrls:gurlVector]; |
| 1571 | return YES; |
| 1572 | } |
| 1573 | |
| 1574 | - (void)application:(NSApplication*)application |
| 1575 | didFailToContinueUserActivityWithType:(NSString*)userActivityType |
| 1576 | error:(NSError*)error { |
| 1577 | } |
| 1578 | |
[email protected] | d630d7d5 | 2010-02-21 00:55:11 | [diff] [blame] | 1579 | @end // @implementation AppController |
[email protected] | 60ad3e2 | 2009-09-18 21:07:19 | [diff] [blame] | 1580 | |
| 1581 | //--------------------------------------------------------------------------- |
| 1582 | |
[email protected] | 216e904 | 2009-10-29 17:05:23 | [diff] [blame] | 1583 | namespace app_controller_mac { |
| 1584 | |
| 1585 | bool IsOpeningNewWindow() { |
| 1586 | return g_is_opening_new_window; |
| 1587 | } |
| 1588 | |
| 1589 | } // namespace app_controller_mac |