[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 | |
avi | e4d7b6f | 2015-12-26 00:59:18 | [diff] [blame] | 7 | #include <stddef.h> |
| 8 | |
Ivan Sandrk | 9669d0e | 2017-12-15 23:50:20 | [diff] [blame] | 9 | #include <memory> |
| 10 | |
Robert Sesek | 9984518 | 2017-08-24 20:53:33 | [diff] [blame] | 11 | #include "base/allocator/allocator_shim.h" |
Scott Violet | 4416579 | 2018-02-22 02:08:08 | [diff] [blame] | 12 | #include "base/allocator/buildflags.h" |
[email protected] | d630d7d5 | 2010-02-21 00:55:11 | [diff] [blame] | 13 | #include "base/auto_reset.h" |
[email protected] | 0b8caad | 2011-11-28 23:33:27 | [diff] [blame] | 14 | #include "base/bind.h" |
[email protected] | 57750f82 | 2009-04-21 21:43:09 | [diff] [blame] | 15 | #include "base/command_line.h" |
[email protected] | 5799981 | 2013-02-24 05:40:52 | [diff] [blame] | 16 | #include "base/files/file_path.h" |
[email protected] | 151c4a6 | 2011-04-22 04:15:13 | [diff] [blame] | 17 | #include "base/mac/foundation_util.h" |
[email protected] | 0378bf4 | 2011-01-01 18:20:14 | [diff] [blame] | 18 | #include "base/mac/mac_util.h" |
Robert Sesek | 9984518 | 2017-08-24 20:53:33 | [diff] [blame] | 19 | #include "base/mac/scoped_objc_class_swizzler.h" |
[email protected] | 2f30167 | 2014-06-07 00:58:52 | [diff] [blame] | 20 | #include "base/mac/sdk_forward_declarations.h" |
avi | e4d7b6f | 2015-12-26 00:59:18 | [diff] [blame] | 21 | #include "base/macros.h" |
asvitkine | aa06031 | 2016-09-01 22:44:13 | [diff] [blame] | 22 | #include "base/metrics/histogram_macros.h" |
bratell | 0a7406f | 2017-03-28 07:46:37 | [diff] [blame] | 23 | #include "base/metrics/user_metrics.h" |
gab | 7af9dc0 | 2017-05-05 13:38:54 | [diff] [blame] | 24 | #include "base/run_loop.h" |
[email protected] | 3ea1b18 | 2013-02-08 22:38:41 | [diff] [blame] | 25 | #include "base/strings/string_number_conversions.h" |
[email protected] | 3268d7b7 | 2013-03-28 17:41:43 | [diff] [blame] | 26 | #include "base/strings/sys_string_conversions.h" |
[email protected] | 135cb80 | 2013-06-09 16:44:20 | [diff] [blame] | 27 | #include "base/strings/utf_string_conversions.h" |
Gabriel Charette | 44db142 | 2018-08-06 11:19:33 | [diff] [blame] | 28 | #include "base/task/post_task.h" |
Etienne Pierre-Doray | a620e5e | 2018-12-06 19:02:10 | [diff] [blame] | 29 | #include "base/threading/scoped_blocking_call.h" |
[email protected] | 1a3aba8 | 2010-11-08 23:52:54 | [diff] [blame] | 30 | #include "chrome/app/chrome_command_ids.h" |
tapted | 63829f7 | 2014-09-24 23:50:50 | [diff] [blame] | 31 | #include "chrome/browser/apps/app_shim/extension_app_shim_handler_mac.h" |
Dominick Ng | 6ff5105 | 2018-07-06 05:30:20 | [diff] [blame] | 32 | #include "chrome/browser/apps/platform_apps/app_window_registry_util.h" |
[email protected] | a07676b2 | 2011-06-17 16:36:53 | [diff] [blame] | 33 | #include "chrome/browser/background/background_application_list_model.h" |
[email protected] | 78d78a6 | 2011-09-29 18:24:33 | [diff] [blame] | 34 | #include "chrome/browser/background/background_mode_manager.h" |
[email protected] | 5c23875 | 2009-06-13 10:29:07 | [diff] [blame] | 35 | #include "chrome/browser/browser_process.h" |
[email protected] | fdf40f3e | 2013-07-11 23:55:46 | [diff] [blame] | 36 | #include "chrome/browser/chrome_notification_types.h" |
Ivan Sandrk | 9669d0e | 2017-12-15 23:50:20 | [diff] [blame] | 37 | #include "chrome/browser/command_updater_impl.h" |
peter | ccb33e8 | 2017-05-02 19:00:44 | [diff] [blame] | 38 | #include "chrome/browser/download/download_core_service.h" |
| 39 | #include "chrome/browser/download/download_core_service_factory.h" |
[email protected] | ccfab59 | 2013-01-15 06:24:32 | [diff] [blame] | 40 | #include "chrome/browser/extensions/extension_service.h" |
[email protected] | e4a377cf | 2011-10-05 02:51:49 | [diff] [blame] | 41 | #include "chrome/browser/first_run/first_run.h" |
[email protected] | 2e6389f | 2012-05-18 19:41:25 | [diff] [blame] | 42 | #include "chrome/browser/lifetime/application_lifetime.h" |
Avi Drissman | d3092734 | 2018-05-22 15:04:27 | [diff] [blame] | 43 | #include "chrome/browser/lifetime/browser_shutdown.h" |
[email protected] | 7d1aaa6 | 2014-07-18 02:21:30 | [diff] [blame] | 44 | #include "chrome/browser/mac/mac_startup_profiler.h" |
Owen Min | 0b002e19 | 2018-07-10 15:04:18 | [diff] [blame] | 45 | #include "chrome/browser/policy/chrome_browser_policy_connector.h" |
| 46 | #include "chrome/browser/policy/machine_level_user_cloud_policy_controller.h" |
treib | 6f959801 | 2014-11-11 12:21:24 | [diff] [blame] | 47 | #include "chrome/browser/prefs/incognito_mode_prefs.h" |
lwchkg | 9c18394 | 2016-03-13 06:29:54 | [diff] [blame] | 48 | #include "chrome/browser/profiles/profile_attributes_entry.h" |
| 49 | #include "chrome/browser/profiles/profile_attributes_storage.h" |
[email protected] | 8ecad5e | 2010-12-02 21:18:33 | [diff] [blame] | 50 | #include "chrome/browser/profiles/profile_manager.h" |
[email protected] | de71ae99 | 2013-07-18 03:30:38 | [diff] [blame] | 51 | #include "chrome/browser/profiles/profiles_state.h" |
[email protected] | 8d4bbdc | 2012-09-20 21:36:49 | [diff] [blame] | 52 | #include "chrome/browser/sessions/session_restore.h" |
[email protected] | d2912a2 | 2011-03-15 15:20:50 | [diff] [blame] | 53 | #include "chrome/browser/sessions/session_service.h" |
[email protected] | 92371eb | 2011-04-28 11:50:15 | [diff] [blame] | 54 | #include "chrome/browser/sessions/session_service_factory.h" |
[email protected] | 92371eb | 2011-04-28 11:50:15 | [diff] [blame] | 55 | #include "chrome/browser/sessions/tab_restore_service_factory.h" |
[email protected] | 3d27d27 | 2013-07-31 03:15:16 | [diff] [blame] | 56 | #include "chrome/browser/signin/signin_promo.h" |
estade | 8c0780f | 2015-08-21 23:36:41 | [diff] [blame] | 57 | #include "chrome/browser/signin/signin_ui_util.h" |
[email protected] | 40f04797 | 2009-11-25 03:54:40 | [diff] [blame] | 58 | #include "chrome/browser/sync/sync_ui_util.h" |
[email protected] | ae04f59 | 2010-11-18 20:41:35 | [diff] [blame] | 59 | #include "chrome/browser/ui/browser.h" |
[email protected] | 0545453 | 2013-01-22 21:09:08 | [diff] [blame] | 60 | #include "chrome/browser/ui/browser_command_controller.h" |
[email protected] | a37d4b0 | 2012-06-25 21:56:10 | [diff] [blame] | 61 | #include "chrome/browser/ui/browser_commands.h" |
[email protected] | 7108d91 | 2014-01-30 08:10:45 | [diff] [blame] | 62 | #include "chrome/browser/ui/browser_dialogs.h" |
[email protected] | d874814 | 2012-05-16 21:13:43 | [diff] [blame] | 63 | #include "chrome/browser/ui/browser_finder.h" |
scottmg | 8abbff83 | 2016-01-28 22:57:37 | [diff] [blame] | 64 | #include "chrome/browser/ui/browser_list.h" |
[email protected] | a13b76f | 2012-06-20 15:36:29 | [diff] [blame] | 65 | #include "chrome/browser/ui/browser_mac.h" |
[email protected] | ae04f59 | 2010-11-18 20:41:35 | [diff] [blame] | 66 | #include "chrome/browser/ui/browser_window.h" |
[email protected] | 5d9cace7 | 2012-06-21 16:07:12 | [diff] [blame] | 67 | #include "chrome/browser/ui/chrome_pages.h" |
[email protected] | edb2d03 | 2013-08-30 09:03:17 | [diff] [blame] | 68 | #import "chrome/browser/ui/cocoa/apps/app_shim_menu_controller_mac.h" |
[email protected] | 425f7a0 | 2014-04-29 01:51:44 | [diff] [blame] | 69 | #include "chrome/browser/ui/cocoa/apps/quit_with_apps_controller_mac.h" |
[email protected] | 7d79165 | 2010-12-01 16:34:49 | [diff] [blame] | 70 | #import "chrome/browser/ui/cocoa/bookmarks/bookmark_menu_bridge.h" |
Tom Anderson | 6a49603 | 2018-08-30 23:38:37 | [diff] [blame] | 71 | #import "chrome/browser/ui/cocoa/confirm_quit.h" |
[email protected] | 7d79165 | 2010-12-01 16:34:49 | [diff] [blame] | 72 | #import "chrome/browser/ui/cocoa/confirm_quit_panel_controller.h" |
erikchen | 600f796 | 2014-12-12 00:17:38 | [diff] [blame] | 73 | #include "chrome/browser/ui/cocoa/handoff_active_url_observer_bridge.h" |
[email protected] | 7d79165 | 2010-12-01 16:34:49 | [diff] [blame] | 74 | #import "chrome/browser/ui/cocoa/history_menu_bridge.h" |
[email protected] | 4feb37d9 | 2012-07-01 20:22:13 | [diff] [blame] | 75 | #include "chrome/browser/ui/cocoa/last_active_browser_cocoa.h" |
[email protected] | ced445e9 | 2014-04-09 21:43:59 | [diff] [blame] | 76 | #import "chrome/browser/ui/cocoa/profiles/profile_menu_controller.h" |
Leonard Grey | 2bdd08f | 2017-10-03 18:40:52 | [diff] [blame] | 77 | #import "chrome/browser/ui/cocoa/share_menu_controller.h" |
[email protected] | ccfab59 | 2013-01-15 06:24:32 | [diff] [blame] | 78 | #include "chrome/browser/ui/extensions/application_launch.h" |
[email protected] | 4feb37d9 | 2012-07-01 20:22:13 | [diff] [blame] | 79 | #include "chrome/browser/ui/startup/startup_browser_creator.h" |
| 80 | #include "chrome/browser/ui/startup/startup_browser_creator_impl.h" |
andresantoso | 55fd251 | 2015-04-01 21:07:04 | [diff] [blame] | 81 | #include "chrome/browser/ui/tabs/tab_strip_model.h" |
mlerman | e29d003 | 2014-09-24 19:31:26 | [diff] [blame] | 82 | #include "chrome/browser/ui/user_manager.h" |
Alexey Baskakov | 2ce2ffc84 | 2018-08-06 02:57:15 | [diff] [blame] | 83 | #include "chrome/browser/web_applications/components/web_app_helpers.h" |
Alexey Baskakov | f302efe | 2018-07-28 02:02:32 | [diff] [blame] | 84 | #include "chrome/browser/web_applications/components/web_app_shortcut_mac.h" |
spqchan | 210e2ed | 2017-07-27 23:17:40 | [diff] [blame] | 85 | #include "chrome/common/chrome_features.h" |
[email protected] | 12dc3d4 | 2010-02-22 23:37:12 | [diff] [blame] | 86 | #include "chrome/common/chrome_paths_internal.h" |
[email protected] | f4acfae8 | 2009-09-11 00:33:38 | [diff] [blame] | 87 | #include "chrome/common/chrome_switches.h" |
[email protected] | 13aec6c4 | 2011-10-27 21:22:08 | [diff] [blame] | 88 | #include "chrome/common/cloud_print/cloud_print_class_mac.h" |
[email protected] | ccfab59 | 2013-01-15 06:24:32 | [diff] [blame] | 89 | #include "chrome/common/extensions/extension_constants.h" |
[email protected] | 6c583d3 | 2011-08-19 14:46:22 | [diff] [blame] | 90 | #include "chrome/common/mac/app_mode_common.h" |
[email protected] | a99fce0e | 2011-03-21 20:58:48 | [diff] [blame] | 91 | #include "chrome/common/pref_names.h" |
[email protected] | 024617a | 2010-08-20 05:08:05 | [diff] [blame] | 92 | #include "chrome/common/url_constants.h" |
[email protected] | af39f00 | 2014-08-22 10:18:18 | [diff] [blame] | 93 | #include "chrome/grit/chromium_strings.h" |
| 94 | #include "chrome/grit/generated_resources.h" |
erikchen | 600f796 | 2014-12-12 00:17:38 | [diff] [blame] | 95 | #include "components/handoff/handoff_manager.h" |
erikchen | 332265b | 2014-11-14 19:59:52 | [diff] [blame] | 96 | #include "components/handoff/handoff_utility.h" |
Michael Giuffrida | 2dbce0d1 | 2017-09-02 03:30:59 | [diff] [blame] | 97 | #include "components/keep_alive_registry/keep_alive_types.h" |
| 98 | #include "components/keep_alive_registry/scoped_keep_alive.h" |
brettw | b1fc1b8 | 2016-02-02 00:19:08 | [diff] [blame] | 99 | #include "components/prefs/pref_service.h" |
blundell | a08c5dd | 2015-09-18 06:14:16 | [diff] [blame] | 100 | #include "components/sessions/core/tab_restore_service.h" |
[email protected] | e582fdd | 2011-12-20 16:48:17 | [diff] [blame] | 101 | #include "content/public/browser/download_manager.h" |
[email protected] | ad50def5 | 2011-10-19 23:17:07 | [diff] [blame] | 102 | #include "content/public/browser/notification_service.h" |
[email protected] | 0d6e9bd | 2011-10-18 04:29:16 | [diff] [blame] | 103 | #include "content/public/browser/notification_types.h" |
[email protected] | 5904cb4 | 2012-09-24 15:05:20 | [diff] [blame] | 104 | #include "content/public/browser/plugin_service.h" |
mitchelljones | e59a161 | 2014-12-16 00:34:59 | [diff] [blame] | 105 | #include "extensions/browser/extension_registry.h" |
tapted | 574f09c | 2015-05-19 13:08:08 | [diff] [blame] | 106 | #include "extensions/browser/extension_system.h" |
[email protected] | d96cf75 | 2014-04-09 04:05:28 | [diff] [blame] | 107 | #include "net/base/filename_util.h" |
[email protected] | 2e29e223 | 2013-07-26 10:40:59 | [diff] [blame] | 108 | #include "ui/base/cocoa/focus_window_set.h" |
[email protected] | c051a1b | 2011-01-21 23:30:17 | [diff] [blame] | 109 | #include "ui/base/l10n/l10n_util.h" |
| 110 | #include "ui/base/l10n/l10n_util_mac.h" |
[email protected] | 88d7494 | 2009-01-21 22:04:44 | [diff] [blame] | 111 | |
mitchelljones | e59a161 | 2014-12-16 00:34:59 | [diff] [blame] | 112 | using apps::AppShimHandler; |
| 113 | using apps::ExtensionAppShimHandler; |
[email protected] | e6e30ac | 2014-01-13 21:24:39 | [diff] [blame] | 114 | using base::UserMetricsAction; |
[email protected] | b441a849 | 2012-06-06 14:55:57 | [diff] [blame] | 115 | using content::BrowserContext; |
[email protected] | e582fdd | 2011-12-20 16:48:17 | [diff] [blame] | 116 | using content::DownloadManager; |
[email protected] | 631bb74 | 2011-11-02 11:29:39 | [diff] [blame] | 117 | |
[email protected] | d630d7d5 | 2010-02-21 00:55:11 | [diff] [blame] | 118 | namespace { |
| 119 | |
[email protected] | d4c811e7 | 2013-10-29 21:57:55 | [diff] [blame] | 120 | // How long we allow a workspace change notification to wait to be |
| 121 | // associated with a dock activation. The animation lasts 250ms. See |
| 122 | // applicationShouldHandleReopen:hasVisibleWindows:. |
| 123 | static const int kWorkspaceChangeTimeoutMs = 500; |
| 124 | |
[email protected] | a37d4b0 | 2012-06-25 21:56:10 | [diff] [blame] | 125 | // True while AppController is calling chrome::NewEmptyWindow(). We need a |
[email protected] | fe7c487 | 2012-05-10 20:06:03 | [diff] [blame] | 126 | // global flag here, analogue to StartupBrowserCreator::InProcessStartup() |
| 127 | // because otherwise the SessionService will try to restore sessions when we |
| 128 | // make a new window while there are no other active windows. |
[email protected] | d630d7d5 | 2010-02-21 00:55:11 | [diff] [blame] | 129 | bool g_is_opening_new_window = false; |
| 130 | |
| 131 | // Activates a browser window having the given profile (the last one active) if |
| 132 | // possible and returns a pointer to the activate |Browser| or NULL if this was |
| 133 | // not possible. If the last active browser is minimized (in particular, if |
| 134 | // there are only minimized windows), it will unminimize it. |
| 135 | Browser* ActivateBrowser(Profile* profile) { |
[email protected] | 5595ce6d | 2014-07-24 04:01:13 | [diff] [blame] | 136 | Browser* browser = chrome::FindLastActiveWithProfile( |
scottmg | 5c03fe02 | 2016-02-03 01:27:24 | [diff] [blame] | 137 | profile->IsGuestSession() ? profile->GetOffTheRecordProfile() : profile); |
[email protected] | d630d7d5 | 2010-02-21 00:55:11 | [diff] [blame] | 138 | if (browser) |
| 139 | browser->window()->Activate(); |
| 140 | return browser; |
| 141 | } |
| 142 | |
| 143 | // Creates an empty browser window with the given profile and returns a pointer |
| 144 | // to the new |Browser|. |
| 145 | Browser* CreateBrowser(Profile* profile) { |
| 146 | { |
[email protected] | 997ec9f | 2012-11-21 04:44:14 | [diff] [blame] | 147 | base::AutoReset<bool> auto_reset_in_run(&g_is_opening_new_window, true); |
scottmg | 85194900 | 2016-02-09 20:09:44 | [diff] [blame] | 148 | chrome::NewEmptyWindow(profile); |
[email protected] | d630d7d5 | 2010-02-21 00:55:11 | [diff] [blame] | 149 | } |
| 150 | |
[email protected] | f2bc6e9 | 2012-10-14 02:08:38 | [diff] [blame] | 151 | Browser* browser = chrome::GetLastActiveBrowser(); |
[email protected] | d630d7d5 | 2010-02-21 00:55:11 | [diff] [blame] | 152 | CHECK(browser); |
| 153 | return browser; |
| 154 | } |
| 155 | |
| 156 | // Activates a browser window having the given profile (the last one active) if |
| 157 | // possible or creates an empty one if necessary. Returns a pointer to the |
| 158 | // activated/new |Browser|. |
| 159 | Browser* ActivateOrCreateBrowser(Profile* profile) { |
| 160 | if (Browser* browser = ActivateBrowser(profile)) |
| 161 | return browser; |
| 162 | return CreateBrowser(profile); |
| 163 | } |
| 164 | |
[email protected] | 151c4a6 | 2011-04-22 04:15:13 | [diff] [blame] | 165 | CFStringRef BaseBundleID_CFString() { |
| 166 | NSString* base_bundle_id = |
| 167 | [NSString stringWithUTF8String:base::mac::BaseBundleID()]; |
| 168 | return base::mac::NSToCFCast(base_bundle_id); |
| 169 | } |
| 170 | |
[email protected] | 12dc3d4 | 2010-02-22 23:37:12 | [diff] [blame] | 171 | // Record the location of the application bundle (containing the main framework) |
| 172 | // from which Chromium was loaded. This is used by app mode shims to find |
| 173 | // Chromium. |
| 174 | void RecordLastRunAppBundlePath() { |
| 175 | // Going up three levels from |chrome::GetVersionedDirectory()| gives the |
| 176 | // real, user-visible app bundle directory. (The alternatives give either the |
| 177 | // framework's path or the initial app's path, which may be an app mode shim |
| 178 | // or a unit test.) |
Etienne Bergeron | 436d4221 | 2019-02-26 17:15:12 | [diff] [blame] | 179 | base::ScopedBlockingCall scoped_blocking_call(FROM_HERE, |
| 180 | base::BlockingType::MAY_BLOCK); |
[email protected] | bf0d26b | 2013-08-29 03:09:14 | [diff] [blame] | 181 | |
Robert Sesek | 49937668 | 2019-05-16 23:25:48 | [diff] [blame] | 182 | // Go up five levels from the versioned sub-directory of the framework, which |
| 183 | // is at C.app/Contents/Frameworks/C.framework/Versions/V. |
| 184 | base::FilePath app_bundle_path = chrome::GetFrameworkBundlePath() |
| 185 | .DirName() |
| 186 | .DirName() |
| 187 | .DirName() |
| 188 | .DirName() |
| 189 | .DirName(); |
[email protected] | 1c95868 | 2013-09-17 03:38:13 | [diff] [blame] | 190 | base::ScopedCFTypeRef<CFStringRef> app_bundle_path_cfstring( |
| 191 | base::SysUTF8ToCFStringRef(app_bundle_path.value())); |
[email protected] | b6b7222 | 2012-02-11 02:04:13 | [diff] [blame] | 192 | CFPreferencesSetAppValue( |
| 193 | base::mac::NSToCFCast(app_mode::kLastRunAppBundlePathPrefsKey), |
[email protected] | 1c95868 | 2013-09-17 03:38:13 | [diff] [blame] | 194 | app_bundle_path_cfstring, BaseBundleID_CFString()); |
[email protected] | 12dc3d4 | 2010-02-22 23:37:12 | [diff] [blame] | 195 | } |
| 196 | |
[email protected] | 7108d91 | 2014-01-30 08:10:45 | [diff] [blame] | 197 | bool IsProfileSignedOut(Profile* profile) { |
lwchkg | 9c18394 | 2016-03-13 06:29:54 | [diff] [blame] | 198 | ProfileAttributesEntry* entry; |
| 199 | bool has_entry = |
| 200 | g_browser_process->profile_manager()->GetProfileAttributesStorage(). |
| 201 | GetProfileAttributesWithPath(profile->GetPath(), &entry); |
| 202 | return has_entry && entry->IsSigninRequired(); |
[email protected] | 7108d91 | 2014-01-30 08:10:45 | [diff] [blame] | 203 | } |
| 204 | |
erikchen | 600f796 | 2014-12-12 00:17:38 | [diff] [blame] | 205 | } // namespace |
[email protected] | d630d7d5 | 2010-02-21 00:55:11 | [diff] [blame] | 206 | |
erikchen | 600f796 | 2014-12-12 00:17:38 | [diff] [blame] | 207 | @interface AppController () <HandoffActiveURLObserverBridgeDelegate> |
[email protected] | 88d7494 | 2009-01-21 22:04:44 | [diff] [blame] | 208 | - (void)initMenuState; |
[email protected] | 961a693 | 2011-07-19 19:52:46 | [diff] [blame] | 209 | - (void)initProfileMenu; |
[email protected] | a99fce0e | 2011-03-21 20:58:48 | [diff] [blame] | 210 | - (void)updateConfirmToQuitPrefMenuItem:(NSMenuItem*)item; |
[email protected] | eef99c2 | 2010-08-17 05:55:16 | [diff] [blame] | 211 | - (void)registerServicesMenuTypesTo:(NSApplication*)app; |
[email protected] | 57750f82 | 2009-04-21 21:43:09 | [diff] [blame] | 212 | - (void)getUrl:(NSAppleEventDescriptor*)event |
| 213 | withReply:(NSAppleEventDescriptor*)reply; |
[email protected] | d4c811e7 | 2013-10-29 21:57:55 | [diff] [blame] | 214 | - (void)activeSpaceDidChange:(NSNotification*)inNotification; |
[email protected] | dd6ab128 | 2010-07-20 01:07:32 | [diff] [blame] | 215 | - (void)checkForAnyKeyWindows; |
[email protected] | 73f5b36 | 2009-08-10 23:58:21 | [diff] [blame] | 216 | - (BOOL)userWillWaitForInProgressDownloads:(int)downloadCount; |
[email protected] | 09729a55 | 2009-08-03 23:21:41 | [diff] [blame] | 217 | - (BOOL)shouldQuitWithInProgressDownloads; |
[email protected] | 93b59fc | 2010-12-21 20:00:47 | [diff] [blame] | 218 | - (void)executeApplication:(id)sender; |
[email protected] | 1c62b2f | 2013-06-28 00:15:00 | [diff] [blame] | 219 | - (void)profileWasRemoved:(const base::FilePath&)profilePath; |
erikchen | 19ee392 | 2014-10-31 19:14:22 | [diff] [blame] | 220 | |
| 221 | // Opens a tab for each GURL in |urls|. |
| 222 | - (void)openUrls:(const std::vector<GURL>&)urls; |
| 223 | |
| 224 | // This class cannot open urls until startup has finished. The urls that cannot |
| 225 | // be opened are cached in |startupUrls_|. This method must be called exactly |
| 226 | // once after startup has completed. It opens the urls in |startupUrls_|, and |
| 227 | // clears |startupUrls_|. |
| 228 | - (void)openStartupUrls; |
| 229 | |
| 230 | // Opens a tab for each GURL in |urls|. If there is exactly one tab open before |
| 231 | // this method is called, and that tab is the NTP, then this method closes the |
| 232 | // NTP after all the |urls| have been opened. |
| 233 | - (void)openUrlsReplacingNTP:(const std::vector<GURL>&)urls; |
erikchen | 600f796 | 2014-12-12 00:17:38 | [diff] [blame] | 234 | |
erikchen | 600f796 | 2014-12-12 00:17:38 | [diff] [blame] | 235 | // This method passes |handoffURL| to |handoffManager_|. |
| 236 | - (void)passURLToHandoffManager:(const GURL&)handoffURL; |
| 237 | |
| 238 | // Lazily creates the Handoff Manager. Updates the state of the Handoff |
| 239 | // Manager. This method is idempotent. This should be called: |
| 240 | // - During initialization. |
| 241 | // - When the current tab navigates to a new URL. |
| 242 | // - When the active browser changes. |
| 243 | // - When the active browser's active tab switches. |
| 244 | // |webContents| should be the new, active WebContents. |
| 245 | - (void)updateHandoffManager:(content::WebContents*)webContents; |
| 246 | |
| 247 | // Given |webContents|, extracts a GURL to be used for Handoff. This may return |
| 248 | // the empty GURL. |
| 249 | - (GURL)handoffURLFromWebContents:(content::WebContents*)webContents; |
Owen Min | 8c5d538 | 2018-08-02 17:52:43 | [diff] [blame] | 250 | |
| 251 | // Return false if Chrome startup is paused by dialog and AppController is |
| 252 | // called without any initialized Profile. |
| 253 | - (BOOL)isProfileReady; |
[email protected] | 88d7494 | 2009-01-21 22:04:44 | [diff] [blame] | 254 | @end |
[email protected] | 1bcdb53 | 2009-01-16 17:47:57 | [diff] [blame] | 255 | |
lwchkg | 9c18394 | 2016-03-13 06:29:54 | [diff] [blame] | 256 | class AppControllerProfileObserver : public ProfileAttributesStorage::Observer { |
[email protected] | 1c62b2f | 2013-06-28 00:15:00 | [diff] [blame] | 257 | public: |
| 258 | AppControllerProfileObserver( |
| 259 | ProfileManager* profile_manager, AppController* app_controller) |
| 260 | : profile_manager_(profile_manager), |
| 261 | app_controller_(app_controller) { |
| 262 | DCHECK(profile_manager_); |
| 263 | DCHECK(app_controller_); |
lwchkg | 9c18394 | 2016-03-13 06:29:54 | [diff] [blame] | 264 | profile_manager_->GetProfileAttributesStorage().AddObserver(this); |
[email protected] | 1c62b2f | 2013-06-28 00:15:00 | [diff] [blame] | 265 | } |
| 266 | |
Daniel Cheng | a542fca | 2014-10-21 09:51:29 | [diff] [blame] | 267 | ~AppControllerProfileObserver() override { |
[email protected] | 1c62b2f | 2013-06-28 00:15:00 | [diff] [blame] | 268 | DCHECK(profile_manager_); |
lwchkg | 9c18394 | 2016-03-13 06:29:54 | [diff] [blame] | 269 | profile_manager_->GetProfileAttributesStorage().RemoveObserver(this); |
[email protected] | 1c62b2f | 2013-06-28 00:15:00 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | private: |
lwchkg | 9c18394 | 2016-03-13 06:29:54 | [diff] [blame] | 273 | // ProfileAttributesStorage::Observer implementation: |
[email protected] | 1c62b2f | 2013-06-28 00:15:00 | [diff] [blame] | 274 | |
Daniel Cheng | a542fca | 2014-10-21 09:51:29 | [diff] [blame] | 275 | void OnProfileAdded(const base::FilePath& profile_path) override {} |
[email protected] | 1c62b2f | 2013-06-28 00:15:00 | [diff] [blame] | 276 | |
Daniel Cheng | a542fca | 2014-10-21 09:51:29 | [diff] [blame] | 277 | void OnProfileWasRemoved(const base::FilePath& profile_path, |
| 278 | const base::string16& profile_name) override { |
[email protected] | 1c62b2f | 2013-06-28 00:15:00 | [diff] [blame] | 279 | // When a profile is deleted we need to notify the AppController, |
| 280 | // so it can correctly update its pointer to the last used profile. |
| 281 | [app_controller_ profileWasRemoved:profile_path]; |
| 282 | } |
| 283 | |
Daniel Cheng | a542fca | 2014-10-21 09:51:29 | [diff] [blame] | 284 | void OnProfileWillBeRemoved(const base::FilePath& profile_path) override {} |
[email protected] | 1c62b2f | 2013-06-28 00:15:00 | [diff] [blame] | 285 | |
Daniel Cheng | a542fca | 2014-10-21 09:51:29 | [diff] [blame] | 286 | void OnProfileNameChanged(const base::FilePath& profile_path, |
| 287 | const base::string16& old_profile_name) override {} |
[email protected] | 1c62b2f | 2013-06-28 00:15:00 | [diff] [blame] | 288 | |
Daniel Cheng | a542fca | 2014-10-21 09:51:29 | [diff] [blame] | 289 | void OnProfileAvatarChanged(const base::FilePath& profile_path) override {} |
[email protected] | 1c62b2f | 2013-06-28 00:15:00 | [diff] [blame] | 290 | |
| 291 | ProfileManager* profile_manager_; |
| 292 | |
| 293 | AppController* app_controller_; // Weak; owns us. |
| 294 | |
| 295 | DISALLOW_COPY_AND_ASSIGN(AppControllerProfileObserver); |
| 296 | }; |
| 297 | |
Robert Sesek | 1eb8522 | 2017-08-24 23:17:09 | [diff] [blame] | 298 | #if BUILDFLAG(USE_ALLOCATOR_SHIM) |
Robert Sesek | 9984518 | 2017-08-24 20:53:33 | [diff] [blame] | 299 | // On macOS 10.12, the IME system attempts to allocate a 2^64 size buffer, |
| 300 | // which would typically cause an OOM crash. To avoid this, the problematic |
| 301 | // method is swizzled out and the make-OOM-fatal bit is disabled for the |
| 302 | // duration of the original call. https://crbug.com/654695 |
| 303 | static base::mac::ScopedObjCClassSwizzler* g_swizzle_imk_input_session; |
| 304 | |
| 305 | @interface OOMDisabledIMKInputSession : NSObject |
| 306 | @end |
| 307 | |
| 308 | @implementation OOMDisabledIMKInputSession |
| 309 | |
| 310 | - (void)_coreAttributesFromRange:(NSRange)range |
| 311 | whichAttributes:(long long)attributes |
| 312 | completionHandler:(void (^)(void))block { |
| 313 | // The allocator flag is per-process, so other threads may temporarily |
| 314 | // not have fatal OOM occur while this method executes, but it is better |
| 315 | // than crashing when using IME. |
| 316 | base::allocator::SetCallNewHandlerOnMallocFailure(false); |
| 317 | g_swizzle_imk_input_session->GetOriginalImplementation()(self, _cmd, range, |
| 318 | attributes, block); |
| 319 | base::allocator::SetCallNewHandlerOnMallocFailure(true); |
| 320 | } |
| 321 | |
| 322 | @end |
Robert Sesek | 1eb8522 | 2017-08-24 23:17:09 | [diff] [blame] | 323 | #endif // BUILDFLAG(USE_ALLOCATOR_SHIM) |
Robert Sesek | 9984518 | 2017-08-24 20:53:33 | [diff] [blame] | 324 | |
[email protected] | 1bcdb53 | 2009-01-16 17:47:57 | [diff] [blame] | 325 | @implementation AppController |
| 326 | |
[email protected] | caadfca | 2010-01-22 21:51:21 | [diff] [blame] | 327 | @synthesize startupComplete = startupComplete_; |
| 328 | |
andresantoso | 04f7bab | 2015-01-15 19:19:16 | [diff] [blame] | 329 | - (void)dealloc { |
| 330 | [[closeTabMenuItem_ menu] setDelegate:nil]; |
| 331 | [super dealloc]; |
| 332 | } |
| 333 | |
[email protected] | cd63ef6 | 2009-05-06 19:41:37 | [diff] [blame] | 334 | // This method is called very early in application startup (ie, before |
| 335 | // the profile is loaded or any preferences have been registered). Defer any |
| 336 | // user-data initialization until -applicationDidFinishLaunching:. |
Robert Sesek | 124f80f9 | 2018-07-24 20:31:29 | [diff] [blame] | 337 | - (void)mainMenuCreated { |
[email protected] | 7d1aaa6 | 2014-07-18 02:21:30 | [diff] [blame] | 338 | MacStartupProfiler::GetInstance()->Profile( |
| 339 | MacStartupProfiler::AWAKE_FROM_NIB); |
[email protected] | 136140c | 2009-05-19 13:58:25 | [diff] [blame] | 340 | // We need to register the handlers early to catch events fired on launch. |
[email protected] | 57750f82 | 2009-04-21 21:43:09 | [diff] [blame] | 341 | NSAppleEventManager* em = [NSAppleEventManager sharedAppleEventManager]; |
| 342 | [em setEventHandler:self |
| 343 | andSelector:@selector(getUrl:withReply:) |
| 344 | forEventClass:kInternetEventClass |
| 345 | andEventID:kAEGetURL]; |
| 346 | [em setEventHandler:self |
| 347 | andSelector:@selector(getUrl:withReply:) |
| 348 | forEventClass:'WWW!' // A particularly ancient AppleEvent that dates |
| 349 | andEventID:'OURL']; // back to the Spyglass days. |
[email protected] | cd63ef6 | 2009-05-06 19:41:37 | [diff] [blame] | 350 | |
[email protected] | 449dd2f | 2009-05-27 13:04:00 | [diff] [blame] | 351 | NSNotificationCenter* notificationCenter = |
| 352 | [NSNotificationCenter defaultCenter]; |
[email protected] | ce560f8 | 2009-06-03 09:39:44 | [diff] [blame] | 353 | [notificationCenter |
[email protected] | 449dd2f | 2009-05-27 13:04:00 | [diff] [blame] | 354 | addObserver:self |
andresantoso | 04f7bab | 2015-01-15 19:19:16 | [diff] [blame] | 355 | selector:@selector(windowDidResignKey:) |
[email protected] | 449dd2f | 2009-05-27 13:04:00 | [diff] [blame] | 356 | name:NSWindowDidResignKeyNotification |
| 357 | object:nil]; |
| 358 | [notificationCenter |
| 359 | addObserver:self |
andresantoso | 04f7bab | 2015-01-15 19:19:16 | [diff] [blame] | 360 | selector:@selector(windowDidBecomeMain:) |
[email protected] | 449dd2f | 2009-05-27 13:04:00 | [diff] [blame] | 361 | name:NSWindowDidBecomeMainNotification |
| 362 | object:nil]; |
| 363 | [notificationCenter |
| 364 | addObserver:self |
andresantoso | 04f7bab | 2015-01-15 19:19:16 | [diff] [blame] | 365 | selector:@selector(windowDidResignMain:) |
[email protected] | 449dd2f | 2009-05-27 13:04:00 | [diff] [blame] | 366 | name:NSWindowDidResignMainNotification |
| 367 | object:nil]; |
| 368 | |
[email protected] | d4c811e7 | 2013-10-29 21:57:55 | [diff] [blame] | 369 | // Register for space change notifications. |
| 370 | [[[NSWorkspace sharedWorkspace] notificationCenter] |
| 371 | addObserver:self |
| 372 | selector:@selector(activeSpaceDidChange:) |
| 373 | name:NSWorkspaceActiveSpaceDidChangeNotification |
| 374 | object:nil]; |
| 375 | |
jochen | bf942015 | 2015-01-13 14:12:57 | [diff] [blame] | 376 | [[[NSWorkspace sharedWorkspace] notificationCenter] |
| 377 | addObserver:self |
| 378 | selector:@selector(willPowerOff:) |
| 379 | name:NSWorkspaceWillPowerOffNotification |
| 380 | object:nil]; |
| 381 | |
Robert Sesek | 705c380 | 2018-10-24 20:58:42 | [diff] [blame] | 382 | NSMenu* fileMenu = [[[NSApp mainMenu] itemWithTag:IDC_FILE_MENU] submenu]; |
| 383 | closeTabMenuItem_ = [fileMenu itemWithTag:IDC_CLOSE_TAB]; |
| 384 | DCHECK(closeTabMenuItem_); |
| 385 | closeWindowMenuItem_ = [fileMenu itemWithTag:IDC_CLOSE_WINDOW]; |
| 386 | DCHECK(closeWindowMenuItem_); |
| 387 | |
[email protected] | 136140c | 2009-05-19 13:58:25 | [diff] [blame] | 388 | // Set up the command updater for when there are no windows open |
| 389 | [self initMenuState]; |
[email protected] | 961a693 | 2011-07-19 19:52:46 | [diff] [blame] | 390 | |
| 391 | // Initialize the Profile menu. |
| 392 | [self initProfileMenu]; |
[email protected] | 136140c | 2009-05-19 13:58:25 | [diff] [blame] | 393 | } |
| 394 | |
[email protected] | 71c0eb9 | 2012-01-03 17:57:30 | [diff] [blame] | 395 | - (void)unregisterEventHandlers { |
| 396 | NSAppleEventManager* em = [NSAppleEventManager sharedAppleEventManager]; |
| 397 | [em removeEventHandlerForEventClass:kInternetEventClass |
| 398 | andEventID:kAEGetURL]; |
| 399 | [em removeEventHandlerForEventClass:cloud_print::kAECloudPrintClass |
| 400 | andEventID:cloud_print::kAECloudPrintClass]; |
[email protected] | 71c0eb9 | 2012-01-03 17:57:30 | [diff] [blame] | 401 | [em removeEventHandlerForEventClass:'WWW!' |
| 402 | andEventID:'OURL']; |
| 403 | [[NSNotificationCenter defaultCenter] removeObserver:self]; |
[email protected] | d4c811e7 | 2013-10-29 21:57:55 | [diff] [blame] | 404 | [[[NSWorkspace sharedWorkspace] notificationCenter] removeObserver:self]; |
[email protected] | 71c0eb9 | 2012-01-03 17:57:30 | [diff] [blame] | 405 | } |
| 406 | |
[email protected] | 7dc8c6b | 2010-04-09 17:02:50 | [diff] [blame] | 407 | // (NSApplicationDelegate protocol) This is the Apple-approved place to override |
| 408 | // the default handlers. |
| 409 | - (void)applicationWillFinishLaunching:(NSNotification*)notification { |
[email protected] | 7d1aaa6 | 2014-07-18 02:21:30 | [diff] [blame] | 410 | MacStartupProfiler::GetInstance()->Profile( |
| 411 | MacStartupProfiler::WILL_FINISH_LAUNCHING); |
sdy | 5788338 | 2016-09-12 16:00:37 | [diff] [blame] | 412 | |
Erik Chen | 2d245df7 | 2017-07-15 03:16:06 | [diff] [blame] | 413 | if (@available(macOS 10.12, *)) { |
sdy | 5788338 | 2016-09-12 16:00:37 | [diff] [blame] | 414 | NSWindow.allowsAutomaticWindowTabbing = NO; |
| 415 | } |
spqchan | 210e2ed | 2017-07-27 23:17:40 | [diff] [blame] | 416 | |
| 417 | // If the OSX version supports this method, the system will automatically |
| 418 | // hide the item if there's no touch bar. However, for unsupported versions, |
spqchan | 30556c3 | 2018-09-05 23:14:08 | [diff] [blame] | 419 | // we'll have to manually remove the item from the menu. |
spqchan | 210e2ed | 2017-07-27 23:17:40 | [diff] [blame] | 420 | if (![NSApp |
spqchan | 30556c3 | 2018-09-05 23:14:08 | [diff] [blame] | 421 | respondsToSelector:@selector(toggleTouchBarCustomizationPalette:)]) { |
spqchan | 210e2ed | 2017-07-27 23:17:40 | [diff] [blame] | 422 | NSMenu* mainMenu = [NSApp mainMenu]; |
| 423 | NSMenu* viewMenu = [[mainMenu itemWithTag:IDC_VIEW_MENU] submenu]; |
| 424 | NSMenuItem* customizeItem = [viewMenu itemWithTag:IDC_CUSTOMIZE_TOUCH_BAR]; |
| 425 | if (customizeItem) |
| 426 | [viewMenu removeItem:customizeItem]; |
| 427 | } |
Leonard Grey | 2bdd08f | 2017-10-03 18:40:52 | [diff] [blame] | 428 | |
Leonard Grey | 34b5e62 | 2018-07-30 19:05:55 | [diff] [blame] | 429 | [self initShareMenu]; |
spqchan | 2c5d541e | 2017-10-25 07:07:11 | [diff] [blame] | 430 | |
| 431 | // Remove "Enable Javascript in Apple Events" if the feature is disabled. |
| 432 | if (!base::FeatureList::IsEnabled( |
| 433 | features::kAppleScriptExecuteJavaScriptMenuItem)) { |
| 434 | NSMenu* mainMenu = [NSApp mainMenu]; |
| 435 | NSMenu* viewMenu = [[mainMenu itemWithTag:IDC_VIEW_MENU] submenu]; |
| 436 | NSMenu* devMenu = [[viewMenu itemWithTag:IDC_DEVELOPER_MENU] submenu]; |
| 437 | NSMenuItem* javascriptAppleEventItem = |
| 438 | [devMenu itemWithTag:IDC_TOGGLE_JAVASCRIPT_APPLE_EVENTS]; |
| 439 | if (javascriptAppleEventItem) |
| 440 | [devMenu removeItem:javascriptAppleEventItem]; |
| 441 | } |
[email protected] | 7dc8c6b | 2010-04-09 17:02:50 | [diff] [blame] | 442 | } |
| 443 | |
[email protected] | 8a5bd5904 | 2014-07-22 04:10:06 | [diff] [blame] | 444 | - (void)applicationWillHide:(NSNotification*)notification { |
Owen Min | 8c5d538 | 2018-08-02 17:52:43 | [diff] [blame] | 445 | if (![self isProfileReady]) |
| 446 | return; |
Christopher Cameron | 027e4ad | 2018-09-21 16:52:54 | [diff] [blame] | 447 | apps::ExtensionAppShimHandler::Get()->OnChromeWillHide(); |
[email protected] | 8a5bd5904 | 2014-07-22 04:10:06 | [diff] [blame] | 448 | } |
| 449 | |
[email protected] | 7dc8c6b | 2010-04-09 17:02:50 | [diff] [blame] | 450 | - (BOOL)tryToTerminateApplication:(NSApplication*)app { |
jochen | bf942015 | 2015-01-13 14:12:57 | [diff] [blame] | 451 | // Reset this now that we've received the call to terminate. |
| 452 | BOOL isPoweringOff = isPoweringOff_; |
| 453 | isPoweringOff_ = NO; |
| 454 | |
[email protected] | 3076fad | 2010-04-28 18:11:45 | [diff] [blame] | 455 | // Check for in-process downloads, and prompt the user if they really want |
| 456 | // to quit (and thus cancel downloads). Only check if we're not already |
| 457 | // shutting down, else the user might be prompted multiple times if the |
| 458 | // download isn't stopped before terminate is called again. |
| 459 | if (!browser_shutdown::IsTryingToQuit() && |
| 460 | ![self shouldQuitWithInProgressDownloads]) |
| 461 | return NO; |
| 462 | |
[email protected] | 7dc8c6b | 2010-04-09 17:02:50 | [diff] [blame] | 463 | // TODO(viettrungluu): Remove Apple Event handlers here? (It's safe to leave |
| 464 | // them in, but I'm not sure about UX; we'd also want to disable other things |
| 465 | // though.) http://crbug.com/40861 |
| 466 | |
[email protected] | 70593e5 | 2014-05-15 08:30:23 | [diff] [blame] | 467 | // Check for active apps. If quitting is prevented, only close browsers and |
| 468 | // sessions. |
jochen | bf942015 | 2015-01-13 14:12:57 | [diff] [blame] | 469 | if (!browser_shutdown::IsTryingToQuit() && !isPoweringOff && |
| 470 | quitWithAppsController_.get() && !quitWithAppsController_->ShouldQuit()) { |
avi | 3ef9ec9e | 2014-12-22 22:50:17 | [diff] [blame] | 471 | if (base::CommandLine::ForCurrentProcess()->HasSwitch( |
mitchelljones | 3bc6ced | 2014-12-19 05:01:37 | [diff] [blame] | 472 | switches::kHostedAppQuitNotification)) { |
| 473 | return NO; |
| 474 | } |
| 475 | |
[email protected] | 70593e5 | 2014-05-15 08:30:23 | [diff] [blame] | 476 | content::NotificationService::current()->Notify( |
| 477 | chrome::NOTIFICATION_CLOSE_ALL_BROWSERS_REQUEST, |
| 478 | content::NotificationService::AllSources(), |
| 479 | content::NotificationService::NoDetails()); |
| 480 | // This will close all browser sessions. |
| 481 | chrome::CloseAllBrowsers(); |
jackhou | ae8e6e5 | 2015-05-29 06:36:46 | [diff] [blame] | 482 | |
| 483 | // At this point, the user has already chosen to cancel downloads. If we |
| 484 | // were to shut down as usual, the downloads would be cancelled in |
peter | ccb33e8 | 2017-05-02 19:00:44 | [diff] [blame] | 485 | // DownloadCoreService::Shutdown(). |
| 486 | DownloadCoreService::CancelAllDownloads(); |
jackhou | ae8e6e5 | 2015-05-29 06:36:46 | [diff] [blame] | 487 | |
[email protected] | 70593e5 | 2014-05-15 08:30:23 | [diff] [blame] | 488 | return NO; |
| 489 | } |
| 490 | |
[email protected] | 0665ebe | 2013-02-13 09:53:19 | [diff] [blame] | 491 | size_t num_browsers = chrome::GetTotalBrowserCount(); |
[email protected] | 7dc8c6b | 2010-04-09 17:02:50 | [diff] [blame] | 492 | |
[email protected] | 0c95faf4 | 2013-10-28 06:27:20 | [diff] [blame] | 493 | // Initiate a shutdown (via chrome::CloseAllBrowsersAndQuit()) if we aren't |
[email protected] | c984d9f | 2010-07-20 20:52:20 | [diff] [blame] | 494 | // already shutting down. |
[email protected] | ef61b8c | 2012-01-20 10:54:56 | [diff] [blame] | 495 | if (!browser_shutdown::IsTryingToQuit()) { |
| 496 | content::NotificationService::current()->Notify( |
[email protected] | d53a08c | 2012-07-18 20:35:30 | [diff] [blame] | 497 | chrome::NOTIFICATION_CLOSE_ALL_BROWSERS_REQUEST, |
[email protected] | ef61b8c | 2012-01-20 10:54:56 | [diff] [blame] | 498 | content::NotificationService::AllSources(), |
| 499 | content::NotificationService::NoDetails()); |
[email protected] | 0c95faf4 | 2013-10-28 06:27:20 | [diff] [blame] | 500 | chrome::CloseAllBrowsersAndQuit(); |
[email protected] | ef61b8c | 2012-01-20 10:54:56 | [diff] [blame] | 501 | } |
[email protected] | 7dc8c6b | 2010-04-09 17:02:50 | [diff] [blame] | 502 | |
[email protected] | c984d9f | 2010-07-20 20:52:20 | [diff] [blame] | 503 | return num_browsers == 0 ? YES : NO; |
[email protected] | 7dc8c6b | 2010-04-09 17:02:50 | [diff] [blame] | 504 | } |
| 505 | |
| 506 | - (void)stopTryingToTerminateApplication:(NSApplication*)app { |
| 507 | if (browser_shutdown::IsTryingToQuit()) { |
| 508 | // Reset the "trying to quit" state, so that closing all browser windows |
| 509 | // will no longer lead to termination. |
| 510 | browser_shutdown::SetTryingToQuit(false); |
| 511 | |
| 512 | // TODO(viettrungluu): Were we to remove Apple Event handlers above, we |
| 513 | // would have to reinstall them here. http://crbug.com/40861 |
| 514 | } |
[email protected] | a9e8afc | 2009-08-11 22:03:17 | [diff] [blame] | 515 | } |
| 516 | |
Avi Drissman | f19bb47 | 2018-11-29 19:51:28 | [diff] [blame] | 517 | - (BOOL)runConfirmQuitPanel { |
[email protected] | 90a4b9d | 2013-07-31 12:14:40 | [diff] [blame] | 518 | // If there are no windows, quit immediately. |
scottmg | 8abbff83 | 2016-01-28 22:57:37 | [diff] [blame] | 519 | if (BrowserList::GetInstance()->empty() && |
jackhou | 261f7f3 | 2015-01-07 00:59:03 | [diff] [blame] | 520 | !AppWindowRegistryUtil::IsAppWindowVisibleInAnyProfile(0)) { |
Avi Drissman | f19bb47 | 2018-11-29 19:51:28 | [diff] [blame] | 521 | return YES; |
[email protected] | 90a4b9d | 2013-07-31 12:14:40 | [diff] [blame] | 522 | } |
| 523 | |
[email protected] | a99fce0e | 2011-03-21 20:58:48 | [diff] [blame] | 524 | // Check if the preference is turned on. |
[email protected] | b6366ffa | 2012-02-29 17:12:26 | [diff] [blame] | 525 | const PrefService* prefs = g_browser_process->local_state(); |
[email protected] | 1eaddf3 | 2011-04-05 15:09:23 | [diff] [blame] | 526 | if (!prefs->GetBoolean(prefs::kConfirmToQuitEnabled)) { |
| 527 | confirm_quit::RecordHistogram(confirm_quit::kNoConfirm); |
Avi Drissman | f19bb47 | 2018-11-29 19:51:28 | [diff] [blame] | 528 | return YES; |
[email protected] | 1eaddf3 | 2011-04-05 15:09:23 | [diff] [blame] | 529 | } |
[email protected] | 177aceb | 2010-11-03 16:17:41 | [diff] [blame] | 530 | |
Avi Drissman | f19bb47 | 2018-11-29 19:51:28 | [diff] [blame] | 531 | // Run only for keyboard-initiated quits. |
| 532 | if ([[NSApp currentEvent] type] != NSKeyDown) |
[email protected] | 2020fd0b | 2010-12-10 01:16:50 | [diff] [blame] | 533 | return NSTerminateNow; |
[email protected] | 177aceb | 2010-11-03 16:17:41 | [diff] [blame] | 534 | |
[email protected] | 5df54fb | 2011-03-07 18:15:26 | [diff] [blame] | 535 | return [[ConfirmQuitPanelController sharedController] |
Avi Drissman | f19bb47 | 2018-11-29 19:51:28 | [diff] [blame] | 536 | runModalLoopForApplication:NSApp]; |
[email protected] | 177aceb | 2010-11-03 16:17:41 | [diff] [blame] | 537 | } |
| 538 | |
[email protected] | 449dd2f | 2009-05-27 13:04:00 | [diff] [blame] | 539 | // Called when the app is shutting down. Clean-up as appropriate. |
[email protected] | 7dc8c6b | 2010-04-09 17:02:50 | [diff] [blame] | 540 | - (void)applicationWillTerminate:(NSNotification*)aNotification { |
[email protected] | 7dc8c6b | 2010-04-09 17:02:50 | [diff] [blame] | 541 | // There better be no browser windows left at this point. |
[email protected] | 0665ebe | 2013-02-13 09:53:19 | [diff] [blame] | 542 | CHECK_EQ(0u, chrome::GetTotalBrowserCount()); |
[email protected] | 3b6aa8b6 | 2009-09-15 21:36:11 | [diff] [blame] | 543 | |
[email protected] | c984d9f | 2010-07-20 20:52:20 | [diff] [blame] | 544 | // Tell BrowserList not to keep the browser process alive. Once all the |
| 545 | // browsers get dealloc'd, it will stop the RunLoop and fall back into main(). |
dgn | 0237778 | 2016-03-12 00:58:38 | [diff] [blame] | 546 | keep_alive_.reset(); |
[email protected] | fbc947b | 2009-06-19 13:28:24 | [diff] [blame] | 547 | |
[email protected] | 02a31dd | 2013-02-05 08:40:52 | [diff] [blame] | 548 | // Reset all pref watching, as this object outlives the prefs system. |
| 549 | profilePrefRegistrar_.reset(); |
| 550 | localPrefRegistrar_.RemoveAll(); |
| 551 | |
[email protected] | 71c0eb9 | 2012-01-03 17:57:30 | [diff] [blame] | 552 | [self unregisterEventHandlers]; |
[email protected] | edb2d03 | 2013-08-30 09:03:17 | [diff] [blame] | 553 | |
| 554 | appShimMenuController_.reset(); |
lgarron | 9e6dee2 | 2014-11-18 01:03:39 | [diff] [blame] | 555 | |
avi | 8255475 | 2016-09-23 17:48:50 | [diff] [blame] | 556 | profileBookmarkMenuBridgeMap_.clear(); |
[email protected] | 449dd2f | 2009-05-27 13:04:00 | [diff] [blame] | 557 | } |
| 558 | |
[email protected] | 3b6aa8b6 | 2009-09-15 21:36:11 | [diff] [blame] | 559 | - (void)didEndMainMessageLoop { |
scottmg | 34c5dd88 | 2016-02-03 05:21:54 | [diff] [blame] | 560 | DCHECK_EQ(0u, chrome::GetBrowserCount([self lastProfile])); |
| 561 | if (!chrome::GetBrowserCount([self lastProfile])) { |
[email protected] | 3b6aa8b6 | 2009-09-15 21:36:11 | [diff] [blame] | 562 | // As we're shutting down, we need to nuke the TabRestoreService, which |
| 563 | // will start the shutdown of the NavigationControllers and allow for |
| 564 | // proper shutdown. If we don't do this, Chrome won't shut down cleanly, |
| 565 | // and may end up crashing when some thread tries to use the IO thread (or |
| 566 | // another thread) that is no longer valid. |
[email protected] | a585a8db | 2011-06-20 18:58:35 | [diff] [blame] | 567 | TabRestoreServiceFactory::ResetForProfile([self lastProfile]); |
[email protected] | 3b6aa8b6 | 2009-09-15 21:36:11 | [diff] [blame] | 568 | } |
| 569 | } |
| 570 | |
[email protected] | 8cfbdbd | 2011-06-16 04:17:17 | [diff] [blame] | 571 | // If the window has a tab controller, make "close window" be cmd-shift-w, |
| 572 | // otherwise leave it as the normal cmd-w. Capitalization of the key equivalent |
[email protected] | eddcf750 | 2012-02-09 22:43:48 | [diff] [blame] | 573 | // affects whether the shift modifier is used. |
[email protected] | ebbe94b | 2012-01-18 20:11:56 | [diff] [blame] | 574 | - (void)adjustCloseWindowMenuItemKeyEquivalent:(BOOL)enableCloseTabShortcut { |
| 575 | [closeWindowMenuItem_ setKeyEquivalent:(enableCloseTabShortcut ? @"W" : |
| 576 | @"w")]; |
[email protected] | 8cfbdbd | 2011-06-16 04:17:17 | [diff] [blame] | 577 | [closeWindowMenuItem_ setKeyEquivalentModifierMask:NSCommandKeyMask]; |
| 578 | } |
| 579 | |
| 580 | // If the window has a tab controller, make "close tab" take over cmd-w, |
| 581 | // otherwise it shouldn't have any key-equivalent because it should be disabled. |
[email protected] | ebbe94b | 2012-01-18 20:11:56 | [diff] [blame] | 582 | - (void)adjustCloseTabMenuItemKeyEquivalent:(BOOL)enableCloseTabShortcut { |
| 583 | if (enableCloseTabShortcut) { |
[email protected] | 8cfbdbd | 2011-06-16 04:17:17 | [diff] [blame] | 584 | [closeTabMenuItem_ setKeyEquivalent:@"w"]; |
| 585 | [closeTabMenuItem_ setKeyEquivalentModifierMask:NSCommandKeyMask]; |
| 586 | } else { |
| 587 | [closeTabMenuItem_ setKeyEquivalent:@""]; |
| 588 | [closeTabMenuItem_ setKeyEquivalentModifierMask:0]; |
| 589 | } |
| 590 | } |
| 591 | |
[email protected] | 9f5f79b6 | 2011-10-28 00:43:45 | [diff] [blame] | 592 | // 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] | 593 | // Close Tab/Close Window accordingly. |
andresantoso | 04f7bab | 2015-01-15 19:19:16 | [diff] [blame] | 594 | - (void)menuNeedsUpdate:(NSMenu*)menu { |
| 595 | DCHECK(menu == [closeTabMenuItem_ menu]); |
erikchen | 22be209 | 2018-06-12 15:54:00 | [diff] [blame] | 596 | [self updateMenuItemKeyEquivalents]; |
[email protected] | 8cfbdbd | 2011-06-16 04:17:17 | [diff] [blame] | 597 | } |
| 598 | |
andresantoso | 04f7bab | 2015-01-15 19:19:16 | [diff] [blame] | 599 | - (void)windowDidResignKey:(NSNotification*)notify { |
| 600 | // If a window is closed, this notification is fired but |[NSApp keyWindow]| |
| 601 | // returns nil regardless of whether any suitable candidates for the key |
| 602 | // window remain. It seems that the new key window for the app is not set |
| 603 | // until after this notification is fired, so a check is performed after the |
| 604 | // run loop is allowed to spin. |
| 605 | [self performSelector:@selector(checkForAnyKeyWindows) |
| 606 | withObject:nil |
| 607 | afterDelay:0.0]; |
[email protected] | 8cfbdbd | 2011-06-16 04:17:17 | [diff] [blame] | 608 | } |
| 609 | |
andresantoso | 04f7bab | 2015-01-15 19:19:16 | [diff] [blame] | 610 | - (void)windowDidBecomeMain:(NSNotification*)notify { |
andresantoso | 55fd251 | 2015-04-01 21:07:04 | [diff] [blame] | 611 | Browser* browser = chrome::FindBrowserWithWindow([notify object]); |
| 612 | if (browser) |
| 613 | [self windowChangedToProfile:browser->profile()->GetOriginalProfile()]; |
andresantoso | 04f7bab | 2015-01-15 19:19:16 | [diff] [blame] | 614 | } |
| 615 | |
| 616 | - (void)windowDidResignMain:(NSNotification*)notify { |
Owen Min | 8c5d538 | 2018-08-02 17:52:43 | [diff] [blame] | 617 | if (chrome::GetTotalBrowserCount() == 0 && [self isProfileReady]) { |
[email protected] | cae68b3a | 2011-11-21 23:29:49 | [diff] [blame] | 618 | [self windowChangedToProfile: |
| 619 | g_browser_process->profile_manager()->GetLastUsedProfile()]; |
[email protected] | 5c59370 | 2011-06-17 18:35:24 | [diff] [blame] | 620 | } |
| 621 | } |
| 622 | |
[email protected] | d4c811e7 | 2013-10-29 21:57:55 | [diff] [blame] | 623 | - (void)activeSpaceDidChange:(NSNotification*)notify { |
| 624 | if (reopenTime_.is_null() || |
| 625 | ![NSApp isActive] || |
| 626 | (base::TimeTicks::Now() - reopenTime_).InMilliseconds() > |
| 627 | kWorkspaceChangeTimeoutMs) { |
| 628 | return; |
| 629 | } |
| 630 | |
| 631 | // The last applicationShouldHandleReopen:hasVisibleWindows: call |
| 632 | // happened during a space change. Now that the change has |
| 633 | // completed, raise browser windows. |
| 634 | reopenTime_ = base::TimeTicks(); |
Christopher Cameron | 359c61e2 | 2018-10-18 08:06:20 | [diff] [blame] | 635 | std::set<gfx::NativeWindow> browserWindows; |
scottmg | 8abbff83 | 2016-01-28 22:57:37 | [diff] [blame] | 636 | for (auto* browser : *BrowserList::GetInstance()) |
[email protected] | d4c811e7 | 2013-10-29 21:57:55 | [diff] [blame] | 637 | browserWindows.insert(browser->window()->GetNativeWindow()); |
Christopher Cameron | 359c61e2 | 2018-10-18 08:06:20 | [diff] [blame] | 638 | if (!browserWindows.empty()) |
[email protected] | 6c4b7960 | 2014-05-28 16:47:21 | [diff] [blame] | 639 | ui::FocusWindowSetOnCurrentSpace(browserWindows); |
[email protected] | d4c811e7 | 2013-10-29 21:57:55 | [diff] [blame] | 640 | } |
| 641 | |
jochen | bf942015 | 2015-01-13 14:12:57 | [diff] [blame] | 642 | // Called when shutting down or logging out. |
| 643 | - (void)willPowerOff:(NSNotification*)notify { |
| 644 | // Don't attempt any shutdown here. Cocoa will shortly call |
| 645 | // -[BrowserCrApplication terminate:]. |
| 646 | isPoweringOff_ = YES; |
| 647 | } |
| 648 | |
[email protected] | dd6ab128 | 2010-07-20 01:07:32 | [diff] [blame] | 649 | - (void)checkForAnyKeyWindows { |
| 650 | if ([NSApp keyWindow]) |
| 651 | return; |
| 652 | |
[email protected] | ad50def5 | 2011-10-19 23:17:07 | [diff] [blame] | 653 | content::NotificationService::current()->Notify( |
[email protected] | d53a08c | 2012-07-18 20:35:30 | [diff] [blame] | 654 | chrome::NOTIFICATION_NO_KEY_WINDOW, |
[email protected] | ad50def5 | 2011-10-19 23:17:07 | [diff] [blame] | 655 | content::NotificationService::AllSources(), |
| 656 | content::NotificationService::NoDetails()); |
[email protected] | 449dd2f | 2009-05-27 13:04:00 | [diff] [blame] | 657 | } |
| 658 | |
[email protected] | 414fde59 | 2009-05-21 16:14:43 | [diff] [blame] | 659 | // If the auto-update interval is not set, make it 5 hours. |
[email protected] | 414fde59 | 2009-05-21 16:14:43 | [diff] [blame] | 660 | // Placed here for 2 reasons: |
| 661 | // 1) Same spot as other Pref stuff |
| 662 | // 2) Try and be friendly by keeping this after app launch |
[email protected] | 414fde59 | 2009-05-21 16:14:43 | [diff] [blame] | 663 | - (void)setUpdateCheckInterval { |
| 664 | #if defined(GOOGLE_CHROME_BUILD) |
[email protected] | 8be0717 | 2013-04-27 05:25:49 | [diff] [blame] | 665 | CFStringRef app = CFSTR("com.google.Keystone.Agent"); |
| 666 | CFStringRef checkInterval = CFSTR("checkInterval"); |
[email protected] | 414fde59 | 2009-05-21 16:14:43 | [diff] [blame] | 667 | CFPropertyListRef plist = CFPreferencesCopyAppValue(checkInterval, app); |
| 668 | if (!plist) { |
| 669 | const float fiveHoursInSeconds = 5.0 * 60.0 * 60.0; |
[email protected] | 7dc8c6b | 2010-04-09 17:02:50 | [diff] [blame] | 670 | NSNumber* value = [NSNumber numberWithFloat:fiveHoursInSeconds]; |
[email protected] | 414fde59 | 2009-05-21 16:14:43 | [diff] [blame] | 671 | CFPreferencesSetAppValue(checkInterval, value, app); |
| 672 | CFPreferencesAppSynchronize(app); |
| 673 | } |
| 674 | #endif |
| 675 | } |
| 676 | |
[email protected] | 66791aff | 2014-04-29 09:45:38 | [diff] [blame] | 677 | - (void)openStartupUrls { |
erikchen | 19ee392 | 2014-10-31 19:14:22 | [diff] [blame] | 678 | DCHECK(startupComplete_); |
| 679 | [self openUrlsReplacingNTP:startupUrls_]; |
| 680 | startupUrls_.clear(); |
| 681 | } |
| 682 | |
| 683 | - (void)openUrlsReplacingNTP:(const std::vector<GURL>&)urls { |
| 684 | if (urls.empty()) |
| 685 | return; |
| 686 | |
[email protected] | 66791aff | 2014-04-29 09:45:38 | [diff] [blame] | 687 | // On Mac, the URLs are passed in via Cocoa, not command line. The Chrome |
| 688 | // NSApplication is created in MainMessageLoop, and then the shortcut urls |
| 689 | // are passed in via Apple events. At this point, the first browser is |
| 690 | // already loaded in PreMainMessageLoop. If we initialize NSApplication |
| 691 | // before PreMainMessageLoop to capture shortcut URL events, it may cause |
| 692 | // more problems because it relies on things created in PreMainMessageLoop |
| 693 | // and may break existing message loop design. |
erikchen | 19ee392 | 2014-10-31 19:14:22 | [diff] [blame] | 694 | |
| 695 | // If the browser hasn't started yet, just queue up the URLs. |
| 696 | if (!startupComplete_) { |
| 697 | startupUrls_.insert(startupUrls_.end(), urls.begin(), urls.end()); |
[email protected] | 66791aff | 2014-04-29 09:45:38 | [diff] [blame] | 698 | return; |
erikchen | 19ee392 | 2014-10-31 19:14:22 | [diff] [blame] | 699 | } |
[email protected] | 66791aff | 2014-04-29 09:45:38 | [diff] [blame] | 700 | |
| 701 | // If there's only 1 tab and the tab is NTP, close this NTP tab and open all |
| 702 | // startup urls in new tabs, because the omnibox will stay focused if we |
| 703 | // load url in NTP tab. |
Christian Dullweber | 820ee5f | 2018-04-26 13:24:31 | [diff] [blame] | 704 | Profile* profile = |
| 705 | g_browser_process->profile_manager()->GetLastUsedProfileAllowedByPolicy(); |
| 706 | Browser* browser = chrome::FindLastActiveWithProfile(profile); |
| 707 | |
[email protected] | 66791aff | 2014-04-29 09:45:38 | [diff] [blame] | 708 | int startupIndex = TabStripModel::kNoTab; |
| 709 | content::WebContents* startupContent = NULL; |
| 710 | |
| 711 | if (browser && browser->tab_strip_model()->count() == 1) { |
| 712 | startupIndex = browser->tab_strip_model()->active_index(); |
| 713 | startupContent = browser->tab_strip_model()->GetActiveWebContents(); |
| 714 | } |
| 715 | |
erikchen | 19ee392 | 2014-10-31 19:14:22 | [diff] [blame] | 716 | [self openUrls:urls]; |
[email protected] | 66791aff | 2014-04-29 09:45:38 | [diff] [blame] | 717 | |
kristipark | a34b247a | 2018-03-09 19:05:34 | [diff] [blame] | 718 | // In test environments where there is no network connection, the visible NTP |
| 719 | // URL may be chrome-search://local-ntp/local-ntp.html instead of |
| 720 | // chrome://newtab/. See local_ntp_test_utils::GetFinalNtpUrl for more |
| 721 | // details. |
| 722 | // This NTP check should be replaced once https://crbug.com/624410 is fixed. |
[email protected] | 66791aff | 2014-04-29 09:45:38 | [diff] [blame] | 723 | if (startupIndex != TabStripModel::kNoTab && |
kristipark | a34b247a | 2018-03-09 19:05:34 | [diff] [blame] | 724 | (startupContent->GetVisibleURL() == chrome::kChromeUINewTabURL || |
| 725 | startupContent->GetVisibleURL() == chrome::kChromeSearchLocalNtpUrl)) { |
[email protected] | 66791aff | 2014-04-29 09:45:38 | [diff] [blame] | 726 | browser->tab_strip_model()->CloseWebContentsAt(startupIndex, |
| 727 | TabStripModel::CLOSE_NONE); |
| 728 | } |
| 729 | } |
| 730 | |
[email protected] | 136140c | 2009-05-19 13:58:25 | [diff] [blame] | 731 | // This is called after profiles have been loaded and preferences registered. |
| 732 | // It is safe to access the default profile here. |
Owen Min | 3bcf8d15 | 2018-07-16 16:50:39 | [diff] [blame] | 733 | - (void)applicationDidFinishLaunching:(NSNotification*)notify { |
| 734 | if (g_browser_process->browser_policy_connector() |
| 735 | ->machine_level_user_cloud_policy_controller() |
| 736 | ->IsEnterpriseStartupDialogShowing()) { |
| 737 | // As Chrome is not ready when the Enterprise startup dialog is being shown. |
| 738 | // Store the notification as it will be reposted when the dialog is closed. |
| 739 | return; |
| 740 | } |
| 741 | |
[email protected] | 7d1aaa6 | 2014-07-18 02:21:30 | [diff] [blame] | 742 | MacStartupProfiler::GetInstance()->Profile( |
| 743 | MacStartupProfiler::DID_FINISH_LAUNCHING); |
| 744 | MacStartupProfiler::GetInstance()->RecordMetrics(); |
| 745 | |
[email protected] | c984d9f | 2010-07-20 20:52:20 | [diff] [blame] | 746 | // Notify BrowserList to keep the application running so it doesn't go away |
| 747 | // when all the browser windows get closed. |
dgn | 0237778 | 2016-03-12 00:58:38 | [diff] [blame] | 748 | keep_alive_.reset(new ScopedKeepAlive(KeepAliveOrigin::APP_CONTROLLER, |
| 749 | KeepAliveRestartOption::DISABLED)); |
[email protected] | 136140c | 2009-05-19 13:58:25 | [diff] [blame] | 750 | |
[email protected] | 414fde59 | 2009-05-21 16:14:43 | [diff] [blame] | 751 | [self setUpdateCheckInterval]; |
[email protected] | 2bcec61 | 2009-05-14 17:50:53 | [diff] [blame] | 752 | |
[email protected] | edb2d03 | 2013-08-30 09:03:17 | [diff] [blame] | 753 | // Start managing the menu for app windows. This needs to be done here because |
| 754 | // main menu item titles are not yet initialized in awakeFromNib. |
[email protected] | e7dc399 | 2013-11-06 02:16:42 | [diff] [blame] | 755 | [self initAppShimMenuController]; |
[email protected] | edb2d03 | 2013-08-30 09:03:17 | [diff] [blame] | 756 | |
[email protected] | 425f7a0 | 2014-04-29 01:51:44 | [diff] [blame] | 757 | // If enabled, keep Chrome alive when apps are open instead of quitting all |
| 758 | // apps. |
| 759 | quitWithAppsController_ = new QuitWithAppsController(); |
| 760 | |
andresantoso | 04f7bab | 2015-01-15 19:19:16 | [diff] [blame] | 761 | // Dynamically update shortcuts for "Close Window" and "Close Tab" menu items. |
| 762 | [[closeTabMenuItem_ menu] setDelegate:self]; |
| 763 | |
lwchkg | 9c18394 | 2016-03-13 06:29:54 | [diff] [blame] | 764 | // Instantiate the ProfileAttributesStorage observer so that we can get |
[email protected] | 1c62b2f | 2013-06-28 00:15:00 | [diff] [blame] | 765 | // notified when a profile is deleted. |
lwchkg | 9c18394 | 2016-03-13 06:29:54 | [diff] [blame] | 766 | profileAttributesStorageObserver_.reset(new AppControllerProfileObserver( |
[email protected] | 1c62b2f | 2013-06-28 00:15:00 | [diff] [blame] | 767 | g_browser_process->profile_manager(), self)); |
| 768 | |
[email protected] | 12dc3d4 | 2010-02-22 23:37:12 | [diff] [blame] | 769 | // Record the path to the (browser) app bundle; this is used by the app mode |
Gabriel Charette | 66d79ed2 | 2017-06-21 17:58:22 | [diff] [blame] | 770 | // shim. |
Robert Sesek | 7f66190e | 2019-06-13 12:28:41 | [diff] [blame] | 771 | if (base::mac::AmIBundled()) { |
| 772 | base::PostTaskWithTraits(FROM_HERE, |
| 773 | {base::MayBlock(), base::TaskPriority::BEST_EFFORT, |
| 774 | base::TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN}, |
| 775 | base::BindOnce(&RecordLastRunAppBundlePath)); |
| 776 | } |
[email protected] | 12dc3d4 | 2010-02-22 23:37:12 | [diff] [blame] | 777 | |
[email protected] | eef99c2 | 2010-08-17 05:55:16 | [diff] [blame] | 778 | // Makes "Services" menu items available. |
| 779 | [self registerServicesMenuTypesTo:[notify object]]; |
| 780 | |
[email protected] | caadfca | 2010-01-22 21:51:21 | [diff] [blame] | 781 | startupComplete_ = YES; |
| 782 | |
scottmg | 5c03fe02 | 2016-02-03 01:27:24 | [diff] [blame] | 783 | Browser* browser = chrome::FindLastActive(); |
erikchen | 600f796 | 2014-12-12 00:17:38 | [diff] [blame] | 784 | content::WebContents* activeWebContents = nullptr; |
| 785 | if (browser) |
| 786 | activeWebContents = browser->tab_strip_model()->GetActiveWebContents(); |
| 787 | [self updateHandoffManager:activeWebContents]; |
[email protected] | 66791aff | 2014-04-29 09:45:38 | [diff] [blame] | 788 | [self openStartupUrls]; |
[email protected] | aac169d | 2011-03-18 19:53:03 | [diff] [blame] | 789 | |
[email protected] | 0545453 | 2013-01-22 21:09:08 | [diff] [blame] | 790 | PrefService* localState = g_browser_process->local_state(); |
| 791 | if (localState) { |
| 792 | localPrefRegistrar_.Init(localState); |
| 793 | localPrefRegistrar_.Add( |
| 794 | prefs::kAllowFileSelectionDialogs, |
| 795 | base::Bind(&chrome::BrowserCommandController::UpdateOpenFileState, |
| 796 | menuState_.get())); |
| 797 | } |
erikchen | 600f796 | 2014-12-12 00:17:38 | [diff] [blame] | 798 | |
| 799 | handoff_active_url_observer_bridge_.reset( |
| 800 | new HandoffActiveURLObserverBridge(self)); |
Robert Sesek | 9984518 | 2017-08-24 20:53:33 | [diff] [blame] | 801 | |
Robert Sesek | 1eb8522 | 2017-08-24 23:17:09 | [diff] [blame] | 802 | #if BUILDFLAG(USE_ALLOCATOR_SHIM) |
Robert Sesek | 9984518 | 2017-08-24 20:53:33 | [diff] [blame] | 803 | // Disable fatal OOM to hack around an OS bug https://crbug.com/654695. |
| 804 | if (base::mac::IsOS10_12()) { |
| 805 | g_swizzle_imk_input_session = new base::mac::ScopedObjCClassSwizzler( |
| 806 | NSClassFromString(@"IMKInputSession"), |
| 807 | [OOMDisabledIMKInputSession class], |
| 808 | @selector(_coreAttributesFromRange:whichAttributes:completionHandler:)); |
| 809 | } |
Robert Sesek | 1eb8522 | 2017-08-24 23:17:09 | [diff] [blame] | 810 | #endif |
[email protected] | 7c32108 | 2009-02-09 15:35:47 | [diff] [blame] | 811 | } |
| 812 | |
[email protected] | 73f5b36 | 2009-08-10 23:58:21 | [diff] [blame] | 813 | // Helper function for populating and displaying the in progress downloads at |
| 814 | // exit alert panel. |
| 815 | - (BOOL)userWillWaitForInProgressDownloads:(int)downloadCount { |
[email protected] | 83a9f65 | 2012-04-25 21:03:48 | [diff] [blame] | 816 | NSString* titleText = nil; |
[email protected] | e8b5f788 | 2009-09-14 20:59:13 | [diff] [blame] | 817 | NSString* explanationText = nil; |
[email protected] | 73f5b36 | 2009-08-10 23:58:21 | [diff] [blame] | 818 | NSString* waitTitle = nil; |
| 819 | NSString* exitTitle = nil; |
| 820 | |
| 821 | // Set the dialog text based on whether or not there are multiple downloads. |
jshin | ec68f03 | 2015-05-14 00:03:23 | [diff] [blame] | 822 | // Dialog text: warning and explanation. |
Bret Sepulveda | 331f10a | 2018-02-13 22:37:42 | [diff] [blame] | 823 | titleText = l10n_util::GetPluralNSStringF(IDS_ABANDON_DOWNLOAD_DIALOG_TITLE, |
| 824 | downloadCount); |
| 825 | explanationText = |
| 826 | l10n_util::GetNSString(IDS_ABANDON_DOWNLOAD_DIALOG_BROWSER_MESSAGE); |
| 827 | // "Cancel download and exit" button text. |
| 828 | exitTitle = l10n_util::GetNSString(IDS_ABANDON_DOWNLOAD_DIALOG_EXIT_BUTTON); |
[email protected] | 83a9f65 | 2012-04-25 21:03:48 | [diff] [blame] | 829 | |
Bret Sepulveda | 331f10a | 2018-02-13 22:37:42 | [diff] [blame] | 830 | // "Wait for download" button text. |
| 831 | waitTitle = |
| 832 | l10n_util::GetNSString(IDS_ABANDON_DOWNLOAD_DIALOG_CONTINUE_BUTTON); |
[email protected] | 73f5b36 | 2009-08-10 23:58:21 | [diff] [blame] | 833 | |
Elly Fong-Jones | bd6d39d | 2019-01-23 22:10:28 | [diff] [blame] | 834 | base::scoped_nsobject<NSAlert> alert([[NSAlert alloc] init]); |
| 835 | [alert setMessageText:titleText]; |
| 836 | [alert setInformativeText:explanationText]; |
| 837 | [alert addButtonWithTitle:waitTitle]; |
| 838 | [alert addButtonWithTitle:exitTitle]; |
| 839 | |
[email protected] | 73f5b36 | 2009-08-10 23:58:21 | [diff] [blame] | 840 | // 'waitButton' is the default choice. |
Elly Fong-Jones | bd6d39d | 2019-01-23 22:10:28 | [diff] [blame] | 841 | int choice = [alert runModal]; |
| 842 | return choice == NSAlertFirstButtonReturn ? YES : NO; |
[email protected] | 73f5b36 | 2009-08-10 23:58:21 | [diff] [blame] | 843 | } |
| 844 | |
| 845 | // 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] | 846 | // user to see if we should continue to exit (and thus cancel the downloads), or |
| 847 | // if we should wait. |
| 848 | - (BOOL)shouldQuitWithInProgressDownloads { |
[email protected] | 73f5b36 | 2009-08-10 23:58:21 | [diff] [blame] | 849 | ProfileManager* profile_manager = g_browser_process->profile_manager(); |
| 850 | if (!profile_manager) |
| 851 | return YES; |
[email protected] | 09729a55 | 2009-08-03 23:21:41 | [diff] [blame] | 852 | |
[email protected] | 844a100 | 2011-04-19 11:37:21 | [diff] [blame] | 853 | std::vector<Profile*> profiles(profile_manager->GetLoadedProfiles()); |
Elly Fong-Jones | 08757f1e | 2018-01-31 16:24:44 | [diff] [blame] | 854 | |
| 855 | std::vector<Profile*> added_profiles; |
| 856 | for (Profile* p : profiles) { |
| 857 | if (p->HasOffTheRecordProfile()) |
| 858 | added_profiles.push_back(p->GetOffTheRecordProfile()); |
| 859 | } |
| 860 | profiles.insert(profiles.end(), added_profiles.begin(), added_profiles.end()); |
| 861 | |
[email protected] | 844a100 | 2011-04-19 11:37:21 | [diff] [blame] | 862 | for (size_t i = 0; i < profiles.size(); ++i) { |
peter | ccb33e8 | 2017-05-02 19:00:44 | [diff] [blame] | 863 | DownloadCoreService* download_core_service = |
| 864 | DownloadCoreServiceFactory::GetForBrowserContext(profiles[i]); |
[email protected] | 9bb54ee | 2011-10-12 17:43:35 | [diff] [blame] | 865 | DownloadManager* download_manager = |
peter | ccb33e8 | 2017-05-02 19:00:44 | [diff] [blame] | 866 | (download_core_service->HasCreatedDownloadManager() |
| 867 | ? BrowserContext::GetDownloadManager(profiles[i]) |
| 868 | : NULL); |
[email protected] | 422a7d1 | 2013-10-21 12:10:42 | [diff] [blame] | 869 | if (download_manager && |
| 870 | download_manager->NonMaliciousInProgressCount() > 0) { |
| 871 | int downloadCount = download_manager->NonMaliciousInProgressCount(); |
[email protected] | 73f5b36 | 2009-08-10 23:58:21 | [diff] [blame] | 872 | if ([self userWillWaitForInProgressDownloads:downloadCount]) { |
| 873 | // Create a new browser window (if necessary) and navigate to the |
| 874 | // downloads page if the user chooses to wait. |
scottmg | 34c5dd88 | 2016-02-03 05:21:54 | [diff] [blame] | 875 | Browser* browser = chrome::FindBrowserWithProfile(profiles[i]); |
[email protected] | 73f5b36 | 2009-08-10 23:58:21 | [diff] [blame] | 876 | if (!browser) { |
erg | 7b01d69 | 2017-02-22 21:57:35 | [diff] [blame] | 877 | browser = new Browser(Browser::CreateParams(profiles[i], true)); |
[email protected] | 73f5b36 | 2009-08-10 23:58:21 | [diff] [blame] | 878 | browser->window()->Show(); |
| 879 | } |
| 880 | DCHECK(browser); |
[email protected] | 5d9cace7 | 2012-06-21 16:07:12 | [diff] [blame] | 881 | chrome::ShowDownloads(browser); |
[email protected] | 73f5b36 | 2009-08-10 23:58:21 | [diff] [blame] | 882 | return NO; |
| 883 | } |
[email protected] | 09729a55 | 2009-08-03 23:21:41 | [diff] [blame] | 884 | |
[email protected] | 73f5b36 | 2009-08-10 23:58:21 | [diff] [blame] | 885 | // User wants to exit. |
| 886 | return YES; |
[email protected] | 09729a55 | 2009-08-03 23:21:41 | [diff] [blame] | 887 | } |
[email protected] | 09729a55 | 2009-08-03 23:21:41 | [diff] [blame] | 888 | } |
| 889 | |
[email protected] | 73f5b36 | 2009-08-10 23:58:21 | [diff] [blame] | 890 | // No profiles or active downloads found, okay to exit. |
[email protected] | 09729a55 | 2009-08-03 23:21:41 | [diff] [blame] | 891 | return YES; |
| 892 | } |
| 893 | |
[email protected] | fbc947b | 2009-06-19 13:28:24 | [diff] [blame] | 894 | // Called to determine if we should enable the "restore tab" menu item. |
| 895 | // Checks with the TabRestoreService to see if there's anything there to |
| 896 | // restore and returns YES if so. |
| 897 | - (BOOL)canRestoreTab { |
blundell | 74001adc | 2015-09-18 11:04:25 | [diff] [blame] | 898 | sessions::TabRestoreService* service = |
[email protected] | a585a8db | 2011-06-20 18:58:35 | [diff] [blame] | 899 | TabRestoreServiceFactory::GetForProfile([self lastProfile]); |
[email protected] | fbc947b | 2009-06-19 13:28:24 | [diff] [blame] | 900 | return service && !service->entries().empty(); |
| 901 | } |
| 902 | |
[email protected] | 1c62b2f | 2013-06-28 00:15:00 | [diff] [blame] | 903 | // Called from the AppControllerProfileObserver every time a profile is deleted. |
| 904 | - (void)profileWasRemoved:(const base::FilePath&)profilePath { |
[email protected] | 1c62b2f | 2013-06-28 00:15:00 | [diff] [blame] | 905 | // If the lastProfile has been deleted, the profile manager has |
| 906 | // already loaded a new one, so the pointer needs to be updated; |
| 907 | // otherwise we will try to start up a browser window with a pointer |
| 908 | // to the old profile. |
bauerb | 447f643 | 2015-03-02 22:34:51 | [diff] [blame] | 909 | // In a browser test, the application is not brought to the front, so |
| 910 | // |lastProfile_| might be null. |
mlerman | 8ae56aa | 2015-04-24 13:56:27 | [diff] [blame] | 911 | if (!lastProfile_ || profilePath == lastProfile_->GetPath()) { |
| 912 | // Force windowChangedToProfile: to set the lastProfile_ and also update the |
| 913 | // relevant menuBridge objects. |
| 914 | lastProfile_ = nullptr; |
| 915 | [self windowChangedToProfile:g_browser_process->profile_manager()-> |
| 916 | GetLastUsedProfile()]; |
| 917 | } |
lgarron | 9e6dee2 | 2014-11-18 01:03:39 | [diff] [blame] | 918 | |
avi | 8255475 | 2016-09-23 17:48:50 | [diff] [blame] | 919 | profileBookmarkMenuBridgeMap_.erase(profilePath); |
[email protected] | 1c62b2f | 2013-06-28 00:15:00 | [diff] [blame] | 920 | } |
| 921 | |
[email protected] | 0545453 | 2013-01-22 21:09:08 | [diff] [blame] | 922 | // Returns true if there is a modal window (either window- or application- |
[email protected] | f6c15fa | 2010-06-04 20:08:21 | [diff] [blame] | 923 | // modal) blocking the active browser. Note that tab modal dialogs (HTTP auth |
| 924 | // sheets) will not count as blocking the browser. But things like open/save |
| 925 | // dialogs that are window modal will block the browser. |
[email protected] | 0545453 | 2013-01-22 21:09:08 | [diff] [blame] | 926 | - (BOOL)keyWindowIsModal { |
| 927 | if ([NSApp modalWindow]) |
| 928 | return YES; |
| 929 | |
[email protected] | f2bc6e9 | 2012-10-14 02:08:38 | [diff] [blame] | 930 | Browser* browser = chrome::GetLastActiveBrowser(); |
Christopher Cameron | 359c61e2 | 2018-10-18 08:06:20 | [diff] [blame] | 931 | return browser && [[browser->window()->GetNativeWindow().GetNativeNSWindow() |
| 932 | attachedSheet] isKindOfClass:[NSWindow class]]; |
[email protected] | 6a28133 | 2009-10-13 17:41:06 | [diff] [blame] | 933 | } |
| 934 | |
[email protected] | 88d7494 | 2009-01-21 22:04:44 | [diff] [blame] | 935 | // Called to validate menu items when there are no key windows. All the |
| 936 | // items we care about have been set with the |commandDispatch:| action and |
| 937 | // a target of FirstResponder in IB. If it's not one of those, let it |
| 938 | // continue up the responder chain to be handled elsewhere. We pull out the |
| 939 | // tag as the cross-platform constant to differentiate and dispatch the |
| 940 | // various commands. |
| 941 | - (BOOL)validateUserInterfaceItem:(id<NSValidatedUserInterfaceItem>)item { |
| 942 | SEL action = [item action]; |
| 943 | BOOL enable = NO; |
[email protected] | 0545453 | 2013-01-22 21:09:08 | [diff] [blame] | 944 | if (action == @selector(commandDispatch:) || |
| 945 | action == @selector(commandFromDock:)) { |
[email protected] | 88d7494 | 2009-01-21 22:04:44 | [diff] [blame] | 946 | NSInteger tag = [item tag]; |
[email protected] | 0545453 | 2013-01-22 21:09:08 | [diff] [blame] | 947 | if (menuState_ && // NULL in tests. |
| 948 | menuState_->SupportsCommand(tag)) { |
[email protected] | fbc947b | 2009-06-19 13:28:24 | [diff] [blame] | 949 | switch (tag) { |
[email protected] | 419eb2a | 2009-11-11 16:26:26 | [diff] [blame] | 950 | // The File Menu commands are not automatically disabled by Cocoa when a |
| 951 | // dialog sheet obscures the browser window, so we disable several of |
| 952 | // them here. We don't need to include IDC_CLOSE_WINDOW, because |
| 953 | // app_controller is only activated when there are no key windows (see |
| 954 | // function comment). |
[email protected] | fbc947b | 2009-06-19 13:28:24 | [diff] [blame] | 955 | case IDC_RESTORE_TAB: |
[email protected] | 0545453 | 2013-01-22 21:09:08 | [diff] [blame] | 956 | enable = ![self keyWindowIsModal] && [self canRestoreTab]; |
[email protected] | 6a28133 | 2009-10-13 17:41:06 | [diff] [blame] | 957 | break; |
[email protected] | f6c15fa | 2010-06-04 20:08:21 | [diff] [blame] | 958 | // Browser-level items that open in new tabs should not open if there's |
| 959 | // a window- or app-modal dialog. |
[email protected] | 6a28133 | 2009-10-13 17:41:06 | [diff] [blame] | 960 | case IDC_OPEN_FILE: |
[email protected] | 6a28133 | 2009-10-13 17:41:06 | [diff] [blame] | 961 | case IDC_NEW_TAB: |
[email protected] | f6c15fa | 2010-06-04 20:08:21 | [diff] [blame] | 962 | case IDC_SHOW_HISTORY: |
| 963 | case IDC_SHOW_BOOKMARK_MANAGER: |
[email protected] | 0545453 | 2013-01-22 21:09:08 | [diff] [blame] | 964 | enable = ![self keyWindowIsModal]; |
[email protected] | f6c15fa | 2010-06-04 20:08:21 | [diff] [blame] | 965 | break; |
| 966 | // Browser-level items that open in new windows. |
[email protected] | f6c15fa | 2010-06-04 20:08:21 | [diff] [blame] | 967 | case IDC_TASK_MANAGER: |
| 968 | // Allow the user to open a new window if there's a window-modal |
| 969 | // dialog. |
[email protected] | 0545453 | 2013-01-22 21:09:08 | [diff] [blame] | 970 | enable = ![self keyWindowIsModal]; |
[email protected] | fbc947b | 2009-06-19 13:28:24 | [diff] [blame] | 971 | break; |
| 972 | default: |
[email protected] | f6c15fa | 2010-06-04 20:08:21 | [diff] [blame] | 973 | enable = menuState_->IsCommandEnabled(tag) ? |
[email protected] | 0545453 | 2013-01-22 21:09:08 | [diff] [blame] | 974 | ![self keyWindowIsModal] : NO; |
[email protected] | fbc947b | 2009-06-19 13:28:24 | [diff] [blame] | 975 | } |
| 976 | } |
spqchan | 9d60fce | 2016-02-10 18:30:47 | [diff] [blame] | 977 | |
| 978 | // "Show as tab" should only appear when the current window is a popup. |
| 979 | // Since |validateUserInterfaceItem:| is called only when there are no |
| 980 | // key windows, we should just hide this. |
| 981 | // This is handled outside of the switch statement because we want to hide |
| 982 | // this regardless if the command is supported or not. |
| 983 | if (tag == IDC_SHOW_AS_TAB) { |
| 984 | NSMenuItem* menuItem = base::mac::ObjCCast<NSMenuItem>(item); |
| 985 | [menuItem setHidden:YES]; |
| 986 | } |
[email protected] | 3b6aa8b6 | 2009-09-15 21:36:11 | [diff] [blame] | 987 | } else if (action == @selector(terminate:)) { |
[email protected] | 88d7494 | 2009-01-21 22:04:44 | [diff] [blame] | 988 | enable = YES; |
[email protected] | 3111f08b | 2009-04-30 16:01:52 | [diff] [blame] | 989 | } else if (action == @selector(showPreferences:)) { |
| 990 | enable = YES; |
[email protected] | bde3dda | 2009-05-20 22:13:07 | [diff] [blame] | 991 | } else if (action == @selector(orderFrontStandardAboutPanel:)) { |
| 992 | enable = YES; |
[email protected] | 93b59fc | 2010-12-21 20:00:47 | [diff] [blame] | 993 | } else if (action == @selector(commandFromDock:)) { |
[email protected] | 3d4bd23f | 2009-10-06 03:58:38 | [diff] [blame] | 994 | enable = YES; |
[email protected] | a99fce0e | 2011-03-21 20:58:48 | [diff] [blame] | 995 | } else if (action == @selector(toggleConfirmToQuit:)) { |
| 996 | [self updateConfirmToQuitPrefMenuItem:static_cast<NSMenuItem*>(item)]; |
| 997 | enable = YES; |
[email protected] | 3a90b45 | 2013-02-15 04:33:48 | [diff] [blame] | 998 | } else if (action == @selector(executeApplication:)) { |
| 999 | enable = YES; |
[email protected] | 88d7494 | 2009-01-21 22:04:44 | [diff] [blame] | 1000 | } |
| 1001 | return enable; |
| 1002 | } |
| 1003 | |
[email protected] | 88d7494 | 2009-01-21 22:04:44 | [diff] [blame] | 1004 | - (void)commandDispatch:(id)sender { |
[email protected] | 2a39d1a | 2014-04-10 05:46:24 | [diff] [blame] | 1005 | Profile* lastProfile = [self safeLastProfileForNewWindows]; |
[email protected] | f0a51fb5 | 2009-03-05 12:46:38 | [diff] [blame] | 1006 | |
[email protected] | 947fc0d | 2010-01-14 22:33:05 | [diff] [blame] | 1007 | // Handle the case where we're dispatching a command from a sender that's in a |
| 1008 | // browser window. This means that the command came from a background window |
| 1009 | // and is getting here because the foreground window is not a browser window. |
| 1010 | if ([sender respondsToSelector:@selector(window)]) { |
| 1011 | id delegate = [[sender window] windowController]; |
andresantoso | 55fd251 | 2015-04-01 21:07:04 | [diff] [blame] | 1012 | if ([delegate respondsToSelector:@selector(commandDispatch:)]) { |
[email protected] | 947fc0d | 2010-01-14 22:33:05 | [diff] [blame] | 1013 | [delegate commandDispatch:sender]; |
| 1014 | return; |
| 1015 | } |
| 1016 | } |
| 1017 | |
[email protected] | 8d4bbdc | 2012-09-20 21:36:49 | [diff] [blame] | 1018 | // Ignore commands during session restore's browser creation. It uses a |
gab | 2998ee7 | 2017-05-05 16:23:50 | [diff] [blame] | 1019 | // nested run loop and commands dispatched during this operation cause |
[email protected] | 8d4bbdc | 2012-09-20 21:36:49 | [diff] [blame] | 1020 | // havoc. |
Owen Min | 8c5d538 | 2018-08-02 17:52:43 | [diff] [blame] | 1021 | if (lastProfile && SessionRestore::IsRestoring(lastProfile) && |
gab | 7af9dc0 | 2017-05-05 13:38:54 | [diff] [blame] | 1022 | base::RunLoop::IsNestedOnCurrentThread()) { |
[email protected] | 8d4bbdc | 2012-09-20 21:36:49 | [diff] [blame] | 1023 | return; |
gab | 7af9dc0 | 2017-05-05 13:38:54 | [diff] [blame] | 1024 | } |
[email protected] | 8d4bbdc | 2012-09-20 21:36:49 | [diff] [blame] | 1025 | |
Trent Apted | 45d9ae2 | 2017-11-09 23:12:32 | [diff] [blame] | 1026 | // If not between -applicationDidFinishLaunching: and |
| 1027 | // -applicationWillTerminate:, ignore. This can happen when events are sitting |
| 1028 | // in the event queue while the browser is shutting down. |
| 1029 | if (!keep_alive_) |
| 1030 | return; |
| 1031 | |
[email protected] | 88d7494 | 2009-01-21 22:04:44 | [diff] [blame] | 1032 | NSInteger tag = [sender tag]; |
[email protected] | 7108d91 | 2014-01-30 08:10:45 | [diff] [blame] | 1033 | |
| 1034 | // If there are no browser windows, and we are trying to open a browser |
Owen Min | 027059d | 2018-03-13 22:17:36 | [diff] [blame] | 1035 | // for a locked profile or the system profile or the guest profile but |
| 1036 | // guest mode is disabled, we have to show the User Manager instead as the |
| 1037 | // locked profile needs authentication and the system profile cannot have a |
| 1038 | // browser. |
| 1039 | const PrefService* prefService = g_browser_process->local_state(); |
| 1040 | if (IsProfileSignedOut(lastProfile) || lastProfile->IsSystemProfile() || |
| 1041 | (lastProfile->IsGuestSession() && prefService && |
| 1042 | !prefService->GetBoolean(prefs::kBrowserGuestModeEnabled))) { |
mlerman | e6e040a | 2014-10-31 00:53:21 | [diff] [blame] | 1043 | UserManager::Show(base::FilePath(), |
mlerman | e29d003 | 2014-09-24 19:31:26 | [diff] [blame] | 1044 | profiles::USER_MANAGER_SELECT_PROFILE_NO_ACTION); |
[email protected] | 7108d91 | 2014-01-30 08:10:45 | [diff] [blame] | 1045 | return; |
| 1046 | } |
| 1047 | |
[email protected] | 88d7494 | 2009-01-21 22:04:44 | [diff] [blame] | 1048 | switch (tag) { |
[email protected] | fbc947b | 2009-06-19 13:28:24 | [diff] [blame] | 1049 | case IDC_NEW_TAB: |
[email protected] | d630d7d5 | 2010-02-21 00:55:11 | [diff] [blame] | 1050 | // Create a new tab in an existing browser window (which we activate) if |
| 1051 | // possible. |
[email protected] | a585a8db | 2011-06-20 18:58:35 | [diff] [blame] | 1052 | if (Browser* browser = ActivateBrowser(lastProfile)) { |
[email protected] | 5d9829491 | 2012-06-27 22:57:40 | [diff] [blame] | 1053 | chrome::ExecuteCommand(browser, IDC_NEW_TAB); |
[email protected] | d630d7d5 | 2010-02-21 00:55:11 | [diff] [blame] | 1054 | break; |
| 1055 | } |
Nico Weber | 331a98a8 | 2018-02-06 23:26:01 | [diff] [blame] | 1056 | FALLTHROUGH; // To create new window. |
[email protected] | 88d7494 | 2009-01-21 22:04:44 | [diff] [blame] | 1057 | case IDC_NEW_WINDOW: |
[email protected] | a585a8db | 2011-06-20 18:58:35 | [diff] [blame] | 1058 | CreateBrowser(lastProfile); |
[email protected] | d630d7d5 | 2010-02-21 00:55:11 | [diff] [blame] | 1059 | break; |
[email protected] | 91a5b3e | 2009-10-30 19:32:22 | [diff] [blame] | 1060 | case IDC_FOCUS_LOCATION: |
[email protected] | 5d9829491 | 2012-06-27 22:57:40 | [diff] [blame] | 1061 | chrome::ExecuteCommand(ActivateOrCreateBrowser(lastProfile), |
| 1062 | IDC_FOCUS_LOCATION); |
[email protected] | 88d7494 | 2009-01-21 22:04:44 | [diff] [blame] | 1063 | break; |
[email protected] | cddc524 | 2009-12-07 18:54:33 | [diff] [blame] | 1064 | case IDC_FOCUS_SEARCH: |
[email protected] | 5d9829491 | 2012-06-27 22:57:40 | [diff] [blame] | 1065 | chrome::ExecuteCommand(ActivateOrCreateBrowser(lastProfile), |
| 1066 | IDC_FOCUS_SEARCH); |
[email protected] | cddc524 | 2009-12-07 18:54:33 | [diff] [blame] | 1067 | break; |
[email protected] | 863ff66 | 2009-01-26 20:18:18 | [diff] [blame] | 1068 | case IDC_NEW_INCOGNITO_WINDOW: |
[email protected] | eddcf750 | 2012-02-09 22:43:48 | [diff] [blame] | 1069 | CreateBrowser(lastProfile->GetOffTheRecordProfile()); |
[email protected] | 863ff66 | 2009-01-26 20:18:18 | [diff] [blame] | 1070 | break; |
[email protected] | fbc947b | 2009-06-19 13:28:24 | [diff] [blame] | 1071 | case IDC_RESTORE_TAB: |
scottmg | d161e6c | 2016-02-17 02:08:01 | [diff] [blame] | 1072 | chrome::OpenWindowWithRestoredTabs(lastProfile); |
[email protected] | fbc947b | 2009-06-19 13:28:24 | [diff] [blame] | 1073 | break; |
[email protected] | e19516d | 2009-04-28 17:15:19 | [diff] [blame] | 1074 | case IDC_OPEN_FILE: |
[email protected] | 5d9829491 | 2012-06-27 22:57:40 | [diff] [blame] | 1075 | chrome::ExecuteCommand(CreateBrowser(lastProfile), IDC_OPEN_FILE); |
[email protected] | e19516d | 2009-04-28 17:15:19 | [diff] [blame] | 1076 | break; |
[email protected] | 1fdff70 | 2009-10-22 00:36:18 | [diff] [blame] | 1077 | case IDC_CLEAR_BROWSING_DATA: { |
[email protected] | 93e18176 | 2009-05-29 14:30:38 | [diff] [blame] | 1078 | // There may not be a browser open, so use the default profile. |
[email protected] | a585a8db | 2011-06-20 18:58:35 | [diff] [blame] | 1079 | if (Browser* browser = ActivateBrowser(lastProfile)) { |
[email protected] | 5d9cace7 | 2012-06-21 16:07:12 | [diff] [blame] | 1080 | chrome::ShowClearBrowsingDataDialog(browser); |
[email protected] | c8de64a | 2011-01-25 17:10:23 | [diff] [blame] | 1081 | } else { |
[email protected] | 1f0b50b | 2012-06-22 20:37:16 | [diff] [blame] | 1082 | chrome::OpenClearBrowsingDataDialogWindow(lastProfile); |
[email protected] | c8de64a | 2011-01-25 17:10:23 | [diff] [blame] | 1083 | } |
[email protected] | 93e18176 | 2009-05-29 14:30:38 | [diff] [blame] | 1084 | break; |
[email protected] | 1fdff70 | 2009-10-22 00:36:18 | [diff] [blame] | 1085 | } |
[email protected] | 4aee565 | 2009-11-19 18:59:21 | [diff] [blame] | 1086 | case IDC_IMPORT_SETTINGS: { |
[email protected] | a585a8db | 2011-06-20 18:58:35 | [diff] [blame] | 1087 | if (Browser* browser = ActivateBrowser(lastProfile)) { |
[email protected] | 5d9cace7 | 2012-06-21 16:07:12 | [diff] [blame] | 1088 | chrome::ShowImportDialog(browser); |
[email protected] | c8de64a | 2011-01-25 17:10:23 | [diff] [blame] | 1089 | } else { |
[email protected] | 1f0b50b | 2012-06-22 20:37:16 | [diff] [blame] | 1090 | chrome::OpenImportSettingsDialogWindow(lastProfile); |
[email protected] | c8de64a | 2011-01-25 17:10:23 | [diff] [blame] | 1091 | } |
[email protected] | 4aee565 | 2009-11-19 18:59:21 | [diff] [blame] | 1092 | break; |
| 1093 | } |
[email protected] | af7c25e | 2010-01-23 05:46:45 | [diff] [blame] | 1094 | case IDC_SHOW_BOOKMARK_MANAGER: |
[email protected] | a585a8db | 2011-06-20 18:58:35 | [diff] [blame] | 1095 | if (Browser* browser = ActivateBrowser(lastProfile)) { |
[email protected] | 5d9cace7 | 2012-06-21 16:07:12 | [diff] [blame] | 1096 | chrome::ShowBookmarkManager(browser); |
[email protected] | 90a8bf25 | 2010-03-12 00:09:47 | [diff] [blame] | 1097 | } else { |
[email protected] | 47e532b | 2010-04-23 17:46:40 | [diff] [blame] | 1098 | // No browser window, so create one for the bookmark manager tab. |
[email protected] | 1f0b50b | 2012-06-22 20:37:16 | [diff] [blame] | 1099 | chrome::OpenBookmarkManagerWindow(lastProfile); |
[email protected] | 90a8bf25 | 2010-03-12 00:09:47 | [diff] [blame] | 1100 | } |
[email protected] | af7c25e | 2010-01-23 05:46:45 | [diff] [blame] | 1101 | break; |
[email protected] | 91a5b3e | 2009-10-30 19:32:22 | [diff] [blame] | 1102 | case IDC_SHOW_HISTORY: |
[email protected] | a585a8db | 2011-06-20 18:58:35 | [diff] [blame] | 1103 | if (Browser* browser = ActivateBrowser(lastProfile)) |
[email protected] | 5d9cace7 | 2012-06-21 16:07:12 | [diff] [blame] | 1104 | chrome::ShowHistory(browser); |
[email protected] | d630d7d5 | 2010-02-21 00:55:11 | [diff] [blame] | 1105 | else |
[email protected] | 1f0b50b | 2012-06-22 20:37:16 | [diff] [blame] | 1106 | chrome::OpenHistoryWindow(lastProfile); |
[email protected] | 91a5b3e | 2009-10-30 19:32:22 | [diff] [blame] | 1107 | break; |
| 1108 | case IDC_SHOW_DOWNLOADS: |
[email protected] | a585a8db | 2011-06-20 18:58:35 | [diff] [blame] | 1109 | if (Browser* browser = ActivateBrowser(lastProfile)) |
[email protected] | 5d9cace7 | 2012-06-21 16:07:12 | [diff] [blame] | 1110 | chrome::ShowDownloads(browser); |
[email protected] | d630d7d5 | 2010-02-21 00:55:11 | [diff] [blame] | 1111 | else |
[email protected] | 1f0b50b | 2012-06-22 20:37:16 | [diff] [blame] | 1112 | chrome::OpenDownloadsWindow(lastProfile); |
[email protected] | 91a5b3e | 2009-10-30 19:32:22 | [diff] [blame] | 1113 | break; |
[email protected] | bb37e6ec | 2009-11-18 17:59:36 | [diff] [blame] | 1114 | case IDC_MANAGE_EXTENSIONS: |
[email protected] | a585a8db | 2011-06-20 18:58:35 | [diff] [blame] | 1115 | if (Browser* browser = ActivateBrowser(lastProfile)) |
[email protected] | bc9833c3 | 2013-02-28 04:05:08 | [diff] [blame] | 1116 | chrome::ShowExtensions(browser, std::string()); |
[email protected] | d630d7d5 | 2010-02-21 00:55:11 | [diff] [blame] | 1117 | else |
[email protected] | 1f0b50b | 2012-06-22 20:37:16 | [diff] [blame] | 1118 | chrome::OpenExtensionsWindow(lastProfile); |
[email protected] | bb37e6ec | 2009-11-18 17:59:36 | [diff] [blame] | 1119 | break; |
[email protected] | 2ca3561 | 2012-06-12 03:49:48 | [diff] [blame] | 1120 | case IDC_HELP_PAGE_VIA_MENU: |
[email protected] | a585a8db | 2011-06-20 18:58:35 | [diff] [blame] | 1121 | if (Browser* browser = ActivateBrowser(lastProfile)) |
[email protected] | 5d9cace7 | 2012-06-21 16:07:12 | [diff] [blame] | 1122 | chrome::ShowHelp(browser, chrome::HELP_SOURCE_MENU); |
[email protected] | d630d7d5 | 2010-02-21 00:55:11 | [diff] [blame] | 1123 | else |
[email protected] | 1f0b50b | 2012-06-22 20:37:16 | [diff] [blame] | 1124 | chrome::OpenHelpWindow(lastProfile, chrome::HELP_SOURCE_MENU); |
[email protected] | 1fdff70 | 2009-10-22 00:36:18 | [diff] [blame] | 1125 | break; |
[email protected] | ceaaf181 | 2010-01-20 22:13:09 | [diff] [blame] | 1126 | case IDC_TASK_MANAGER: |
mlerman | e6e040a | 2014-10-31 00:53:21 | [diff] [blame] | 1127 | chrome::OpenTaskManager(NULL); |
[email protected] | ceaaf181 | 2010-01-20 22:13:09 | [diff] [blame] | 1128 | break; |
[email protected] | 4a42d27 | 2010-06-18 01:29:42 | [diff] [blame] | 1129 | case IDC_OPTIONS: |
| 1130 | [self showPreferences:sender]; |
| 1131 | break; |
[email protected] | d630d7d5 | 2010-02-21 00:55:11 | [diff] [blame] | 1132 | } |
[email protected] | 88d7494 | 2009-01-21 22:04:44 | [diff] [blame] | 1133 | } |
| 1134 | |
[email protected] | 93b59fc | 2010-12-21 20:00:47 | [diff] [blame] | 1135 | // Run a (background) application in a new tab. |
| 1136 | - (void)executeApplication:(id)sender { |
| 1137 | NSInteger tag = [sender tag]; |
[email protected] | a585a8db | 2011-06-20 18:58:35 | [diff] [blame] | 1138 | Profile* profile = [self lastProfile]; |
[email protected] | 93b59fc | 2010-12-21 20:00:47 | [diff] [blame] | 1139 | DCHECK(profile); |
| 1140 | BackgroundApplicationListModel applications(profile); |
| 1141 | DCHECK(tag >= 0 && |
| 1142 | tag < static_cast<int>(applications.size())); |
[email protected] | 1c321ee | 2012-05-21 03:02:34 | [diff] [blame] | 1143 | const extensions::Extension* extension = applications.GetExtension(tag); |
[email protected] | 78d78a6 | 2011-09-29 18:24:33 | [diff] [blame] | 1144 | BackgroundModeManager::LaunchBackgroundApplication(profile, extension); |
[email protected] | 93b59fc | 2010-12-21 20:00:47 | [diff] [blame] | 1145 | } |
| 1146 | |
[email protected] | 947fc0d | 2010-01-14 22:33:05 | [diff] [blame] | 1147 | // Same as |-commandDispatch:|, but executes commands using a disposition |
| 1148 | // determined by the key flags. This will get called in the case where the |
| 1149 | // frontmost window is not a browser window, and the user has command-clicked |
| 1150 | // a button in a background browser window whose action is |
| 1151 | // |-commandDispatchUsingKeyModifiers:| |
| 1152 | - (void)commandDispatchUsingKeyModifiers:(id)sender { |
| 1153 | DCHECK(sender); |
| 1154 | if ([sender respondsToSelector:@selector(window)]) { |
| 1155 | id delegate = [[sender window] windowController]; |
andresantoso | 55fd251 | 2015-04-01 21:07:04 | [diff] [blame] | 1156 | if ([delegate respondsToSelector: |
| 1157 | @selector(commandDispatchUsingKeyModifiers:)]) { |
[email protected] | 947fc0d | 2010-01-14 22:33:05 | [diff] [blame] | 1158 | [delegate commandDispatchUsingKeyModifiers:sender]; |
| 1159 | } |
| 1160 | } |
| 1161 | } |
| 1162 | |
[email protected] | 2e29e223 | 2013-07-26 10:40:59 | [diff] [blame] | 1163 | // NSApplication delegate method called when someone clicks on the dock icon. |
| 1164 | // To match standard mac behavior, we should open a new window if there are no |
| 1165 | // browser windows. |
[email protected] | ff81e0a | 2009-04-13 14:58:54 | [diff] [blame] | 1166 | - (BOOL)applicationShouldHandleReopen:(NSApplication*)theApplication |
[email protected] | bf193bc5 | 2013-06-08 04:39:58 | [diff] [blame] | 1167 | hasVisibleWindows:(BOOL)hasVisibleWindows { |
[email protected] | 7063140 | 2010-04-26 20:16:54 | [diff] [blame] | 1168 | // If the browser is currently trying to quit, don't do anything and return NO |
| 1169 | // to prevent AppKit from doing anything. |
| 1170 | // TODO(rohitrao): Remove this code when http://crbug.com/40861 is resolved. |
| 1171 | if (browser_shutdown::IsTryingToQuit()) |
| 1172 | return NO; |
| 1173 | |
[email protected] | 2e29e223 | 2013-07-26 10:40:59 | [diff] [blame] | 1174 | // Bring all browser windows to the front. Specifically, this brings them in |
| 1175 | // front of any app windows. FocusWindowSet will also unminimize the most |
| 1176 | // recently minimized window if no windows in the set are visible. |
[email protected] | a25920ee | 2013-09-05 19:38:49 | [diff] [blame] | 1177 | // If there are any, return here. Otherwise, the windows are panels or |
| 1178 | // notifications so we still need to open a new window. |
[email protected] | bf193bc5 | 2013-06-08 04:39:58 | [diff] [blame] | 1179 | if (hasVisibleWindows) { |
Christopher Cameron | 359c61e2 | 2018-10-18 08:06:20 | [diff] [blame] | 1180 | std::set<gfx::NativeWindow> browserWindows; |
scottmg | 8abbff83 | 2016-01-28 22:57:37 | [diff] [blame] | 1181 | for (auto* browser : *BrowserList::GetInstance()) { |
mitchelljones | e59a161 | 2014-12-16 00:34:59 | [diff] [blame] | 1182 | // When focusing Chrome, don't focus any browser windows associated with |
| 1183 | // a currently running app shim, so ignore them. |
| 1184 | if (browser && browser->is_app()) { |
mitchelljones | f0042208 | 2015-01-05 23:38:03 | [diff] [blame] | 1185 | extensions::ExtensionRegistry* registry = |
| 1186 | extensions::ExtensionRegistry::Get(browser->profile()); |
| 1187 | const extensions::Extension* extension = registry->GetExtensionById( |
Alexey Baskakov | 2ce2ffc84 | 2018-08-06 02:57:15 | [diff] [blame] | 1188 | web_app::GetAppIdFromApplicationName(browser->app_name()), |
mitchelljones | f0042208 | 2015-01-05 23:38:03 | [diff] [blame] | 1189 | extensions::ExtensionRegistry::ENABLED); |
mitchelljones | b118a0c | 2015-01-07 02:51:34 | [diff] [blame] | 1190 | if (extension && extension->is_hosted_app()) |
mitchelljones | e59a161 | 2014-12-16 00:34:59 | [diff] [blame] | 1191 | continue; |
mitchelljones | e59a161 | 2014-12-16 00:34:59 | [diff] [blame] | 1192 | } |
[email protected] | 2e29e223 | 2013-07-26 10:40:59 | [diff] [blame] | 1193 | browserWindows.insert(browser->window()->GetNativeWindow()); |
[email protected] | 0236be2 | 2012-01-11 01:05:35 | [diff] [blame] | 1194 | } |
[email protected] | a25920ee | 2013-09-05 19:38:49 | [diff] [blame] | 1195 | if (!browserWindows.empty()) { |
[email protected] | d4c811e7 | 2013-10-29 21:57:55 | [diff] [blame] | 1196 | NSWindow* keyWindow = [NSApp keyWindow]; |
| 1197 | if (keyWindow && ![keyWindow isOnActiveSpace]) { |
| 1198 | // The key window is not on the active space. We must be mid-animation |
| 1199 | // for a space transition triggered by the dock. Delay the call to |
| 1200 | // |ui::FocusWindowSet| until the transition completes. Otherwise, the |
| 1201 | // wrong space's windows get raised, resulting in an off-screen key |
| 1202 | // window. It does not work to |ui::FocusWindowSet| twice, once here |
| 1203 | // and once in |activeSpaceDidChange:|, as that appears to break when |
| 1204 | // the omnibox is focused. |
| 1205 | // |
| 1206 | // This check relies on OS X setting the key window to a window on the |
| 1207 | // target space before calling this method. |
| 1208 | // |
| 1209 | // See http://crbug.com/309656. |
| 1210 | reopenTime_ = base::TimeTicks::Now(); |
| 1211 | } else { |
[email protected] | 6c4b7960 | 2014-05-28 16:47:21 | [diff] [blame] | 1212 | ui::FocusWindowSetOnCurrentSpace(browserWindows); |
[email protected] | d4c811e7 | 2013-10-29 21:57:55 | [diff] [blame] | 1213 | } |
| 1214 | // Return NO; we've done (or soon will do) the deminiaturize, so |
| 1215 | // AppKit shouldn't do anything. |
[email protected] | a25920ee | 2013-09-05 19:38:49 | [diff] [blame] | 1216 | return NO; |
| 1217 | } |
[email protected] | 0236be2 | 2012-01-11 01:05:35 | [diff] [blame] | 1218 | } |
[email protected] | ff81e0a | 2009-04-13 14:58:54 | [diff] [blame] | 1219 | |
[email protected] | 0d1210d6 | 2010-09-07 17:31:38 | [diff] [blame] | 1220 | // 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] | 1221 | // or by the user, for example in System Preferences->Accounts->Login Items), |
[email protected] | 0d1210d6 | 2010-09-07 17:31:38 | [diff] [blame] | 1222 | // allow session to be restored first time the user clicks on a Dock icon. |
| 1223 | // Normally, it'd just open a new empty page. |
| 1224 | { |
[email protected] | eddcf750 | 2012-02-09 22:43:48 | [diff] [blame] | 1225 | static BOOL doneOnce = NO; |
[email protected] | 70593e5 | 2014-05-15 08:30:23 | [diff] [blame] | 1226 | BOOL attemptRestore = apps::AppShimHandler::ShouldRestoreSession() || |
| 1227 | (!doneOnce && base::mac::WasLaunchedAsHiddenLoginItem()); |
| 1228 | doneOnce = YES; |
| 1229 | if (attemptRestore) { |
| 1230 | SessionService* sessionService = |
| 1231 | SessionServiceFactory::GetForProfileForSessionRestore( |
| 1232 | [self lastProfile]); |
| 1233 | if (sessionService && |
Findit | 04679572 | 2018-10-03 11:44:48 | [diff] [blame] | 1234 | sessionService->RestoreIfNecessary(std::vector<GURL>())) |
[email protected] | 70593e5 | 2014-05-15 08:30:23 | [diff] [blame] | 1235 | return NO; |
[email protected] | eddcf750 | 2012-02-09 22:43:48 | [diff] [blame] | 1236 | } |
[email protected] | 0d1210d6 | 2010-09-07 17:31:38 | [diff] [blame] | 1237 | } |
[email protected] | fc44f24 | 2012-02-14 16:54:39 | [diff] [blame] | 1238 | |
[email protected] | ff81e0a | 2009-04-13 14:58:54 | [diff] [blame] | 1239 | // Otherwise open a new window. |
[email protected] | 7108d91 | 2014-01-30 08:10:45 | [diff] [blame] | 1240 | // If the last profile was locked, we have to open the User Manager, as the |
mlerman | 7831f57d | 2015-05-25 11:40:15 | [diff] [blame] | 1241 | // profile requires authentication. Similarly, because guest mode and system |
| 1242 | // profile are implemented as forced incognito, we can't open a new guest |
| 1243 | // browser either, so we have to show the User Manager as well. |
[email protected] | 7108d91 | 2014-01-30 08:10:45 | [diff] [blame] | 1244 | Profile* lastProfile = [self lastProfile]; |
mlerman | 7831f57d | 2015-05-25 11:40:15 | [diff] [blame] | 1245 | if (lastProfile->IsGuestSession() || IsProfileSignedOut(lastProfile) || |
| 1246 | lastProfile->IsSystemProfile()) { |
mlerman | e6e040a | 2014-10-31 00:53:21 | [diff] [blame] | 1247 | UserManager::Show(base::FilePath(), |
mlerman | e29d003 | 2014-09-24 19:31:26 | [diff] [blame] | 1248 | profiles::USER_MANAGER_SELECT_PROFILE_NO_ACTION); |
| 1249 | } else { |
[email protected] | 7108d91 | 2014-01-30 08:10:45 | [diff] [blame] | 1250 | CreateBrowser(lastProfile); |
mlerman | e29d003 | 2014-09-24 19:31:26 | [diff] [blame] | 1251 | } |
[email protected] | ff81e0a | 2009-04-13 14:58:54 | [diff] [blame] | 1252 | |
| 1253 | // We've handled the reopen event, so return NO to tell AppKit not |
| 1254 | // to do anything. |
| 1255 | return NO; |
| 1256 | } |
| 1257 | |
[email protected] | 88d7494 | 2009-01-21 22:04:44 | [diff] [blame] | 1258 | - (void)initMenuState { |
Ivan Sandrk | 9669d0e | 2017-12-15 23:50:20 | [diff] [blame] | 1259 | menuState_ = std::make_unique<CommandUpdaterImpl>(nullptr); |
[email protected] | fbc947b | 2009-06-19 13:28:24 | [diff] [blame] | 1260 | menuState_->UpdateCommandEnabled(IDC_NEW_TAB, true); |
[email protected] | 88d7494 | 2009-01-21 22:04:44 | [diff] [blame] | 1261 | menuState_->UpdateCommandEnabled(IDC_NEW_WINDOW, true); |
[email protected] | 863ff66 | 2009-01-26 20:18:18 | [diff] [blame] | 1262 | menuState_->UpdateCommandEnabled(IDC_NEW_INCOGNITO_WINDOW, true); |
[email protected] | e19516d | 2009-04-28 17:15:19 | [diff] [blame] | 1263 | menuState_->UpdateCommandEnabled(IDC_OPEN_FILE, true); |
[email protected] | 93e18176 | 2009-05-29 14:30:38 | [diff] [blame] | 1264 | menuState_->UpdateCommandEnabled(IDC_CLEAR_BROWSING_DATA, true); |
[email protected] | fbc947b | 2009-06-19 13:28:24 | [diff] [blame] | 1265 | menuState_->UpdateCommandEnabled(IDC_RESTORE_TAB, false); |
[email protected] | 91a5b3e | 2009-10-30 19:32:22 | [diff] [blame] | 1266 | menuState_->UpdateCommandEnabled(IDC_FOCUS_LOCATION, true); |
[email protected] | cddc524 | 2009-12-07 18:54:33 | [diff] [blame] | 1267 | menuState_->UpdateCommandEnabled(IDC_FOCUS_SEARCH, true); |
[email protected] | af7c25e | 2010-01-23 05:46:45 | [diff] [blame] | 1268 | menuState_->UpdateCommandEnabled(IDC_SHOW_BOOKMARK_MANAGER, true); |
[email protected] | 91a5b3e | 2009-10-30 19:32:22 | [diff] [blame] | 1269 | menuState_->UpdateCommandEnabled(IDC_SHOW_HISTORY, true); |
| 1270 | menuState_->UpdateCommandEnabled(IDC_SHOW_DOWNLOADS, true); |
[email protected] | bb37e6ec | 2009-11-18 17:59:36 | [diff] [blame] | 1271 | menuState_->UpdateCommandEnabled(IDC_MANAGE_EXTENSIONS, true); |
[email protected] | 2ca3561 | 2012-06-12 03:49:48 | [diff] [blame] | 1272 | menuState_->UpdateCommandEnabled(IDC_HELP_PAGE_VIA_MENU, true); |
[email protected] | 4aee565 | 2009-11-19 18:59:21 | [diff] [blame] | 1273 | menuState_->UpdateCommandEnabled(IDC_IMPORT_SETTINGS, true); |
[email protected] | 236ad302 | 2013-09-04 03:27:43 | [diff] [blame] | 1274 | #if defined(GOOGLE_CHROME_BUILD) |
[email protected] | 1c41a93 | 2010-11-22 23:41:23 | [diff] [blame] | 1275 | menuState_->UpdateCommandEnabled(IDC_FEEDBACK, true); |
[email protected] | 236ad302 | 2013-09-04 03:27:43 | [diff] [blame] | 1276 | #endif |
[email protected] | ceaaf181 | 2010-01-20 22:13:09 | [diff] [blame] | 1277 | menuState_->UpdateCommandEnabled(IDC_TASK_MANAGER, true); |
[email protected] | 88d7494 | 2009-01-21 22:04:44 | [diff] [blame] | 1278 | } |
[email protected] | 1bcdb53 | 2009-01-16 17:47:57 | [diff] [blame] | 1279 | |
[email protected] | 961a693 | 2011-07-19 19:52:46 | [diff] [blame] | 1280 | // Conditionally adds the Profile menu to the main menu bar. |
| 1281 | - (void)initProfileMenu { |
[email protected] | 961a693 | 2011-07-19 19:52:46 | [diff] [blame] | 1282 | NSMenu* mainMenu = [NSApp mainMenu]; |
| 1283 | NSMenuItem* profileMenu = [mainMenu itemWithTag:IDC_PROFILE_MAIN_MENU]; |
[email protected] | e354ca4 | 2011-08-03 20:31:40 | [diff] [blame] | 1284 | |
[email protected] | de71ae99 | 2013-07-18 03:30:38 | [diff] [blame] | 1285 | if (!profiles::IsMultipleProfilesEnabled()) { |
[email protected] | e354ca4 | 2011-08-03 20:31:40 | [diff] [blame] | 1286 | [mainMenu removeItem:profileMenu]; |
| 1287 | return; |
| 1288 | } |
| 1289 | |
[email protected] | 6b2d30bc | 2011-10-11 18:16:39 | [diff] [blame] | 1290 | // The controller will unhide the menu if necessary. |
| 1291 | [profileMenu setHidden:YES]; |
[email protected] | 961a693 | 2011-07-19 19:52:46 | [diff] [blame] | 1292 | |
[email protected] | 6b2d30bc | 2011-10-11 18:16:39 | [diff] [blame] | 1293 | profileMenuController_.reset( |
| 1294 | [[ProfileMenuController alloc] initWithMainMenuItem:profileMenu]); |
[email protected] | 961a693 | 2011-07-19 19:52:46 | [diff] [blame] | 1295 | } |
| 1296 | |
Leonard Grey | 2bdd08f | 2017-10-03 18:40:52 | [diff] [blame] | 1297 | - (void)initShareMenu { |
| 1298 | shareMenuController_.reset([[ShareMenuController alloc] init]); |
| 1299 | NSMenu* mainMenu = [NSApp mainMenu]; |
| 1300 | NSMenu* fileMenu = [[mainMenu itemWithTag:IDC_FILE_MENU] submenu]; |
| 1301 | NSString* shareMenuTitle = l10n_util::GetNSString(IDS_SHARE_MAC); |
Leonard Grey | 34b5e62 | 2018-07-30 19:05:55 | [diff] [blame] | 1302 | NSMenuItem* shareMenuItem = [fileMenu itemWithTitle:shareMenuTitle]; |
Leonard Grey | 2bdd08f | 2017-10-03 18:40:52 | [diff] [blame] | 1303 | base::scoped_nsobject<NSMenu> shareSubmenu( |
| 1304 | [[NSMenu alloc] initWithTitle:shareMenuTitle]); |
| 1305 | [shareSubmenu setDelegate:shareMenuController_]; |
| 1306 | [shareMenuItem setSubmenu:shareSubmenu]; |
Leonard Grey | 2bdd08f | 2017-10-03 18:40:52 | [diff] [blame] | 1307 | } |
| 1308 | |
[email protected] | a99fce0e | 2011-03-21 20:58:48 | [diff] [blame] | 1309 | // The Confirm to Quit preference is atypical in that the preference lives in |
| 1310 | // the app menu right above the Quit menu item. This method will refresh the |
| 1311 | // display of that item depending on the preference state. |
| 1312 | - (void)updateConfirmToQuitPrefMenuItem:(NSMenuItem*)item { |
| 1313 | // Format the string so that the correct key equivalent is displayed. |
| 1314 | NSString* acceleratorString = [ConfirmQuitPanelController keyCommandString]; |
| 1315 | NSString* title = l10n_util::GetNSStringF(IDS_CONFIRM_TO_QUIT_OPTION, |
| 1316 | base::SysNSStringToUTF16(acceleratorString)); |
| 1317 | [item setTitle:title]; |
| 1318 | |
[email protected] | b6366ffa | 2012-02-29 17:12:26 | [diff] [blame] | 1319 | const PrefService* prefService = g_browser_process->local_state(); |
[email protected] | a99fce0e | 2011-03-21 20:58:48 | [diff] [blame] | 1320 | bool enabled = prefService->GetBoolean(prefs::kConfirmToQuitEnabled); |
| 1321 | [item setState:enabled ? NSOnState : NSOffState]; |
| 1322 | } |
| 1323 | |
[email protected] | eef99c2 | 2010-08-17 05:55:16 | [diff] [blame] | 1324 | - (void)registerServicesMenuTypesTo:(NSApplication*)app { |
[email protected] | 09ea24c | 2010-08-23 19:05:41 | [diff] [blame] | 1325 | // Note that RenderWidgetHostViewCocoa implements NSServicesRequests which |
| 1326 | // handles requests from services. |
Avi Drissman | a082022 | 2018-09-01 18:02:28 | [diff] [blame] | 1327 | NSArray* types = @[ base::mac::CFToNSCast(kUTTypeUTF8PlainText) ]; |
[email protected] | 09ea24c | 2010-08-23 19:05:41 | [diff] [blame] | 1328 | [app registerServicesMenuSendTypes:types returnTypes:types]; |
[email protected] | eef99c2 | 2010-08-17 05:55:16 | [diff] [blame] | 1329 | } |
| 1330 | |
Owen Min | 8c5d538 | 2018-08-02 17:52:43 | [diff] [blame] | 1331 | // Return null if Chrome is not ready or there is no ProfileManager. |
[email protected] | a585a8db | 2011-06-20 18:58:35 | [diff] [blame] | 1332 | - (Profile*)lastProfile { |
andresantoso | 55fd251 | 2015-04-01 21:07:04 | [diff] [blame] | 1333 | // Return the profile of the last-used Browser, if available. |
[email protected] | 7fd5a46 | 2011-06-19 16:57:23 | [diff] [blame] | 1334 | if (lastProfile_) |
| 1335 | return lastProfile_; |
| 1336 | |
Owen Min | 8c5d538 | 2018-08-02 17:52:43 | [diff] [blame] | 1337 | if (![self isProfileReady]) |
| 1338 | return nullptr; |
| 1339 | |
[email protected] | fbb0455 | 2014-01-16 15:47:38 | [diff] [blame] | 1340 | // On first launch, use the logic that ChromeBrowserMain uses to determine |
| 1341 | // the initial profile. |
| 1342 | ProfileManager* profile_manager = g_browser_process->profile_manager(); |
| 1343 | if (!profile_manager) |
Owen Min | 8c5d538 | 2018-08-02 17:52:43 | [diff] [blame] | 1344 | return nullptr; |
[email protected] | 3f34599d | 2009-03-25 22:11:43 | [diff] [blame] | 1345 | |
avi | 3ef9ec9e | 2014-12-22 22:50:17 | [diff] [blame] | 1346 | return profile_manager->GetProfile( |
| 1347 | GetStartupProfilePath(profile_manager->user_data_dir(), |
| 1348 | *base::CommandLine::ForCurrentProcess())); |
[email protected] | 3f34599d | 2009-03-25 22:11:43 | [diff] [blame] | 1349 | } |
| 1350 | |
[email protected] | 2a39d1a | 2014-04-10 05:46:24 | [diff] [blame] | 1351 | - (Profile*)safeLastProfileForNewWindows { |
| 1352 | Profile* profile = [self lastProfile]; |
| 1353 | |
Owen Min | 8c5d538 | 2018-08-02 17:52:43 | [diff] [blame] | 1354 | if (!profile) |
| 1355 | return nullptr; |
| 1356 | |
[email protected] | 2a39d1a | 2014-04-10 05:46:24 | [diff] [blame] | 1357 | // Guest sessions must always be OffTheRecord. Use that when opening windows. |
| 1358 | if (profile->IsGuestSession()) |
| 1359 | return profile->GetOffTheRecordProfile(); |
| 1360 | |
| 1361 | return profile; |
| 1362 | } |
| 1363 | |
mlerman | e6e040a | 2014-10-31 00:53:21 | [diff] [blame] | 1364 | // Returns true if a browser window may be opened for the last active profile. |
| 1365 | - (bool)canOpenNewBrowser { |
| 1366 | Profile* profile = [self safeLastProfileForNewWindows]; |
| 1367 | |
| 1368 | const PrefService* prefs = g_browser_process->local_state(); |
| 1369 | return !profile->IsGuestSession() || |
| 1370 | prefs->GetBoolean(prefs::kBrowserGuestModeEnabled); |
| 1371 | } |
| 1372 | |
[email protected] | 57750f82 | 2009-04-21 21:43:09 | [diff] [blame] | 1373 | // 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] | 1374 | // StartupBrowserCreator here because on the other platforms, URLs to open come |
| 1375 | // through the ProcessSingleton, and it calls StartupBrowserCreator. It's best |
| 1376 | // to bottleneck the openings through that for uniform handling. |
[email protected] | d630d7d5 | 2010-02-21 00:55:11 | [diff] [blame] | 1377 | - (void)openUrls:(const std::vector<GURL>&)urls { |
[email protected] | caadfca | 2010-01-22 21:51:21 | [diff] [blame] | 1378 | if (!startupComplete_) { |
[email protected] | d630d7d5 | 2010-02-21 00:55:11 | [diff] [blame] | 1379 | startupUrls_.insert(startupUrls_.end(), urls.begin(), urls.end()); |
[email protected] | 136140c | 2009-05-19 13:58:25 | [diff] [blame] | 1380 | return; |
| 1381 | } |
Christian Dullweber | 820ee5f | 2018-04-26 13:24:31 | [diff] [blame] | 1382 | // Pick the last used browser from a regular profile to open the urls. |
| 1383 | Profile* profile = |
| 1384 | g_browser_process->profile_manager()->GetLastUsedProfileAllowedByPolicy(); |
| 1385 | Browser* browser = chrome::FindLastActiveWithProfile(profile); |
Scott Violet | 1f106b58 | 2017-07-12 15:49:58 | [diff] [blame] | 1386 | // if no browser window exists then create one with no tabs to be filled in |
| 1387 | if (!browser) { |
| 1388 | browser = new Browser( |
| 1389 | Browser::CreateParams([self safeLastProfileForNewWindows], true)); |
| 1390 | browser->window()->Show(); |
eugenebng | 7afbc36e | 2017-06-15 13:38:47 | [diff] [blame] | 1391 | } |
Scott Violet | 1f106b58 | 2017-07-12 15:49:58 | [diff] [blame] | 1392 | |
| 1393 | base::CommandLine dummy(base::CommandLine::NO_PROGRAM); |
| 1394 | chrome::startup::IsFirstRun first_run = |
| 1395 | first_run::IsChromeFirstRun() ? chrome::startup::IS_FIRST_RUN |
| 1396 | : chrome::startup::IS_NOT_FIRST_RUN; |
| 1397 | StartupBrowserCreatorImpl launch(base::FilePath(), dummy, first_run); |
| 1398 | launch.OpenURLsInBrowser(browser, false, urls); |
[email protected] | 57750f82 | 2009-04-21 21:43:09 | [diff] [blame] | 1399 | } |
| 1400 | |
[email protected] | 57750f82 | 2009-04-21 21:43:09 | [diff] [blame] | 1401 | - (void)getUrl:(NSAppleEventDescriptor*)event |
| 1402 | withReply:(NSAppleEventDescriptor*)reply { |
| 1403 | NSString* urlStr = [[event paramDescriptorForKeyword:keyDirectObject] |
| 1404 | stringValue]; |
| 1405 | |
| 1406 | GURL gurl(base::SysNSStringToUTF8(urlStr)); |
| 1407 | std::vector<GURL> gurlVector; |
| 1408 | gurlVector.push_back(gurl); |
| 1409 | |
erikchen | 19ee392 | 2014-10-31 19:14:22 | [diff] [blame] | 1410 | [self openUrlsReplacingNTP:gurlVector]; |
[email protected] | 57750f82 | 2009-04-21 21:43:09 | [diff] [blame] | 1411 | } |
| 1412 | |
[email protected] | caadfca | 2010-01-22 21:51:21 | [diff] [blame] | 1413 | - (void)application:(NSApplication*)sender |
| 1414 | openFiles:(NSArray*)filenames { |
[email protected] | 57750f82 | 2009-04-21 21:43:09 | [diff] [blame] | 1415 | std::vector<GURL> gurlVector; |
[email protected] | caadfca | 2010-01-22 21:51:21 | [diff] [blame] | 1416 | for (NSString* file in filenames) { |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 1417 | GURL gurl = |
[email protected] | 77d8354 | 2013-12-13 08:54:29 | [diff] [blame] | 1418 | net::FilePathToFileURL(base::FilePath([file fileSystemRepresentation])); |
[email protected] | 57750f82 | 2009-04-21 21:43:09 | [diff] [blame] | 1419 | gurlVector.push_back(gurl); |
| 1420 | } |
[email protected] | caadfca | 2010-01-22 21:51:21 | [diff] [blame] | 1421 | if (!gurlVector.empty()) |
erikchen | 19ee392 | 2014-10-31 19:14:22 | [diff] [blame] | 1422 | [self openUrlsReplacingNTP:gurlVector]; |
[email protected] | caadfca | 2010-01-22 21:51:21 | [diff] [blame] | 1423 | else |
| 1424 | NOTREACHED() << "Nothing to open!"; |
[email protected] | 57750f82 | 2009-04-21 21:43:09 | [diff] [blame] | 1425 | |
[email protected] | caadfca | 2010-01-22 21:51:21 | [diff] [blame] | 1426 | [sender replyToOpenOrPrint:NSApplicationDelegateReplySuccess]; |
[email protected] | 57750f82 | 2009-04-21 21:43:09 | [diff] [blame] | 1427 | } |
[email protected] | 3f34599d | 2009-03-25 22:11:43 | [diff] [blame] | 1428 | |
[email protected] | 3111f08b | 2009-04-30 16:01:52 | [diff] [blame] | 1429 | // Show the preferences window, or bring it to the front if it's already |
| 1430 | // visible. |
| 1431 | - (IBAction)showPreferences:(id)sender { |
[email protected] | a585a8db | 2011-06-20 18:58:35 | [diff] [blame] | 1432 | if (Browser* browser = ActivateBrowser([self lastProfile])) { |
[email protected] | 44517765 | 2011-03-09 02:04:05 | [diff] [blame] | 1433 | // Show options tab in the active browser window. |
[email protected] | 5d9cace7 | 2012-06-21 16:07:12 | [diff] [blame] | 1434 | chrome::ShowSettings(browser); |
mlerman | e6e040a | 2014-10-31 00:53:21 | [diff] [blame] | 1435 | } else if ([self canOpenNewBrowser]) { |
[email protected] | 44517765 | 2011-03-09 02:04:05 | [diff] [blame] | 1436 | // No browser window, so create one for the options tab. |
[email protected] | 2a39d1a | 2014-04-10 05:46:24 | [diff] [blame] | 1437 | chrome::OpenOptionsWindow([self safeLastProfileForNewWindows]); |
mlerman | e6e040a | 2014-10-31 00:53:21 | [diff] [blame] | 1438 | } else { |
| 1439 | // No way to create a browser, default to the User Manager. |
| 1440 | UserManager::Show(base::FilePath(), |
mlerman | e6e040a | 2014-10-31 00:53:21 | [diff] [blame] | 1441 | profiles::USER_MANAGER_SELECT_PROFILE_CHROME_SETTINGS); |
[email protected] | 4a42d27 | 2010-06-18 01:29:42 | [diff] [blame] | 1442 | } |
[email protected] | d989f07c | 2009-11-14 00:35:46 | [diff] [blame] | 1443 | } |
| 1444 | |
[email protected] | bde3dda | 2009-05-20 22:13:07 | [diff] [blame] | 1445 | - (IBAction)orderFrontStandardAboutPanel:(id)sender { |
[email protected] | a048ad2 | 2012-03-23 04:26:56 | [diff] [blame] | 1446 | if (Browser* browser = ActivateBrowser([self lastProfile])) { |
[email protected] | 5d9cace7 | 2012-06-21 16:07:12 | [diff] [blame] | 1447 | chrome::ShowAboutChrome(browser); |
mlerman | e6e040a | 2014-10-31 00:53:21 | [diff] [blame] | 1448 | } else if ([self canOpenNewBrowser]) { |
| 1449 | // No browser window, so create one for the options tab. |
[email protected] | 2a39d1a | 2014-04-10 05:46:24 | [diff] [blame] | 1450 | chrome::OpenAboutWindow([self safeLastProfileForNewWindows]); |
mlerman | e6e040a | 2014-10-31 00:53:21 | [diff] [blame] | 1451 | } else { |
| 1452 | // No way to create a browser, default to the User Manager. |
| 1453 | UserManager::Show(base::FilePath(), |
mlerman | e6e040a | 2014-10-31 00:53:21 | [diff] [blame] | 1454 | profiles::USER_MANAGER_SELECT_PROFILE_ABOUT_CHROME); |
[email protected] | bde3dda | 2009-05-20 22:13:07 | [diff] [blame] | 1455 | } |
[email protected] | bde3dda | 2009-05-20 22:13:07 | [diff] [blame] | 1456 | } |
| 1457 | |
[email protected] | a99fce0e | 2011-03-21 20:58:48 | [diff] [blame] | 1458 | - (IBAction)toggleConfirmToQuit:(id)sender { |
[email protected] | b6366ffa | 2012-02-29 17:12:26 | [diff] [blame] | 1459 | PrefService* prefService = g_browser_process->local_state(); |
[email protected] | a99fce0e | 2011-03-21 20:58:48 | [diff] [blame] | 1460 | bool enabled = prefService->GetBoolean(prefs::kConfirmToQuitEnabled); |
| 1461 | prefService->SetBoolean(prefs::kConfirmToQuitEnabled, !enabled); |
| 1462 | } |
| 1463 | |
[email protected] | 3d4bd23f | 2009-10-06 03:58:38 | [diff] [blame] | 1464 | // Explicitly bring to the foreground when creating new windows from the dock. |
[email protected] | 93b59fc | 2010-12-21 20:00:47 | [diff] [blame] | 1465 | - (void)commandFromDock:(id)sender { |
[email protected] | 3d4bd23f | 2009-10-06 03:58:38 | [diff] [blame] | 1466 | [NSApp activateIgnoringOtherApps:YES]; |
| 1467 | [self commandDispatch:sender]; |
| 1468 | } |
| 1469 | |
[email protected] | 227fcee | 2010-06-11 19:19:37 | [diff] [blame] | 1470 | - (NSMenu*)applicationDockMenu:(NSApplication*)sender { |
[email protected] | 018a396 | 2009-09-17 22:23:44 | [diff] [blame] | 1471 | NSMenu* dockMenu = [[[NSMenu alloc] initWithTitle: @""] autorelease]; |
[email protected] | a585a8db | 2011-06-20 18:58:35 | [diff] [blame] | 1472 | Profile* profile = [self lastProfile]; |
[email protected] | 93b59fc | 2010-12-21 20:00:47 | [diff] [blame] | 1473 | |
[email protected] | 646fadbf | 2012-09-08 01:49:01 | [diff] [blame] | 1474 | BOOL profilesAdded = [profileMenuController_ insertItemsIntoMenu:dockMenu |
| 1475 | atOffset:0 |
| 1476 | fromDock:YES]; |
| 1477 | if (profilesAdded) |
| 1478 | [dockMenu addItem:[NSMenuItem separatorItem]]; |
[email protected] | 3d272f5 | 2012-08-23 19:00:33 | [diff] [blame] | 1479 | |
[email protected] | 646fadbf | 2012-09-08 01:49:01 | [diff] [blame] | 1480 | NSString* titleStr = l10n_util::GetNSStringWithFixup(IDS_NEW_WINDOW_MAC); |
[email protected] | a852203 | 2013-06-24 22:51:46 | [diff] [blame] | 1481 | base::scoped_nsobject<NSMenuItem> item( |
[email protected] | 8c6b029d | 2011-01-21 22:39:51 | [diff] [blame] | 1482 | [[NSMenuItem alloc] initWithTitle:titleStr |
| 1483 | action:@selector(commandFromDock:) |
| 1484 | keyEquivalent:@""]); |
[email protected] | 288bfcd3 | 2009-09-14 18:14:46 | [diff] [blame] | 1485 | [item setTarget:self]; |
[email protected] | 646fadbf | 2012-09-08 01:49:01 | [diff] [blame] | 1486 | [item setTag:IDC_NEW_WINDOW]; |
[email protected] | 0545453 | 2013-01-22 21:09:08 | [diff] [blame] | 1487 | [item setEnabled:[self validateUserInterfaceItem:item]]; |
[email protected] | 646fadbf | 2012-09-08 01:49:01 | [diff] [blame] | 1488 | [dockMenu addItem:item]; |
| 1489 | |
[email protected] | 8d10a41 | 2013-08-09 19:18:00 | [diff] [blame] | 1490 | // |profile| can be NULL during unit tests. |
treib | 6f959801 | 2014-11-11 12:21:24 | [diff] [blame] | 1491 | if (!profile || |
| 1492 | IncognitoModePrefs::GetAvailability(profile->GetPrefs()) != |
| 1493 | IncognitoModePrefs::DISABLED) { |
[email protected] | 8d10a41 | 2013-08-09 19:18:00 | [diff] [blame] | 1494 | titleStr = l10n_util::GetNSStringWithFixup(IDS_NEW_INCOGNITO_WINDOW_MAC); |
| 1495 | item.reset( |
| 1496 | [[NSMenuItem alloc] initWithTitle:titleStr |
| 1497 | action:@selector(commandFromDock:) |
| 1498 | keyEquivalent:@""]); |
| 1499 | [item setTarget:self]; |
| 1500 | [item setTag:IDC_NEW_INCOGNITO_WINDOW]; |
| 1501 | [item setEnabled:[self validateUserInterfaceItem:item]]; |
| 1502 | [dockMenu addItem:item]; |
| 1503 | } |
[email protected] | 288bfcd3 | 2009-09-14 18:14:46 | [diff] [blame] | 1504 | |
[email protected] | 8c6b029d | 2011-01-21 22:39:51 | [diff] [blame] | 1505 | // TODO(rickcam): Mock out BackgroundApplicationListModel, then add unit |
| 1506 | // tests which use the mock in place of the profile-initialized model. |
| 1507 | |
| 1508 | // Avoid breaking unit tests which have no profile. |
| 1509 | if (profile) { |
| 1510 | BackgroundApplicationListModel applications(profile); |
| 1511 | if (applications.size()) { |
| 1512 | int position = 0; |
| 1513 | NSString* menuStr = |
| 1514 | l10n_util::GetNSStringWithFixup(IDS_BACKGROUND_APPS_MAC); |
[email protected] | a852203 | 2013-06-24 22:51:46 | [diff] [blame] | 1515 | base::scoped_nsobject<NSMenu> appMenu( |
| 1516 | [[NSMenu alloc] initWithTitle:menuStr]); |
[email protected] | 1c321ee | 2012-05-21 03:02:34 | [diff] [blame] | 1517 | for (extensions::ExtensionList::const_iterator cursor = |
[email protected] | 3a90b45 | 2013-02-15 04:33:48 | [diff] [blame] | 1518 | applications.begin(); |
[email protected] | 8c6b029d | 2011-01-21 22:39:51 | [diff] [blame] | 1519 | cursor != applications.end(); |
| 1520 | ++cursor, ++position) { |
[email protected] | f15d9ebc | 2013-07-02 00:14:50 | [diff] [blame] | 1521 | DCHECK_EQ(applications.GetPosition(cursor->get()), position); |
[email protected] | 8c6b029d | 2011-01-21 22:39:51 | [diff] [blame] | 1522 | NSString* itemStr = |
[email protected] | 670d323 | 2013-12-24 17:58:58 | [diff] [blame] | 1523 | base::SysUTF16ToNSString(base::UTF8ToUTF16((*cursor)->name())); |
[email protected] | a852203 | 2013-06-24 22:51:46 | [diff] [blame] | 1524 | base::scoped_nsobject<NSMenuItem> appItem( |
| 1525 | [[NSMenuItem alloc] initWithTitle:itemStr |
| 1526 | action:@selector(executeApplication:) |
| 1527 | keyEquivalent:@""]); |
[email protected] | 8c6b029d | 2011-01-21 22:39:51 | [diff] [blame] | 1528 | [appItem setTarget:self]; |
| 1529 | [appItem setTag:position]; |
| 1530 | [appMenu addItem:appItem]; |
| 1531 | } |
[email protected] | 8c6b029d | 2011-01-21 22:39:51 | [diff] [blame] | 1532 | } |
| 1533 | } |
| 1534 | |
[email protected] | 018a396 | 2009-09-17 22:23:44 | [diff] [blame] | 1535 | return dockMenu; |
[email protected] | 288bfcd3 | 2009-09-14 18:14:46 | [diff] [blame] | 1536 | } |
| 1537 | |
[email protected] | d630d7d5 | 2010-02-21 00:55:11 | [diff] [blame] | 1538 | - (const std::vector<GURL>&)startupUrls { |
| 1539 | return startupUrls_; |
[email protected] | caadfca | 2010-01-22 21:51:21 | [diff] [blame] | 1540 | } |
| 1541 | |
[email protected] | 7e4c69d0 | 2011-05-11 22:16:47 | [diff] [blame] | 1542 | - (BookmarkMenuBridge*)bookmarkMenuBridge { |
lgarron | 9e6dee2 | 2014-11-18 01:03:39 | [diff] [blame] | 1543 | return bookmarkMenuBridge_; |
[email protected] | 7e4c69d0 | 2011-05-11 22:16:47 | [diff] [blame] | 1544 | } |
| 1545 | |
mlerman | 8ae56aa | 2015-04-24 13:56:27 | [diff] [blame] | 1546 | - (HistoryMenuBridge*)historyMenuBridge { |
| 1547 | return historyMenuBridge_.get(); |
| 1548 | } |
| 1549 | |
[email protected] | e7dc399 | 2013-11-06 02:16:42 | [diff] [blame] | 1550 | - (void)initAppShimMenuController { |
[email protected] | e4c63ec | 2014-01-14 15:50:54 | [diff] [blame] | 1551 | if (!appShimMenuController_) |
[email protected] | e7dc399 | 2013-11-06 02:16:42 | [diff] [blame] | 1552 | appShimMenuController_.reset([[AppShimMenuController alloc] init]); |
| 1553 | } |
| 1554 | |
noms | dd5b6a61 | 2014-09-23 15:20:16 | [diff] [blame] | 1555 | - (void)windowChangedToProfile:(Profile*)profile { |
| 1556 | if (lastProfile_ == profile) |
| 1557 | return; |
| 1558 | |
lgarron | 9e6dee2 | 2014-11-18 01:03:39 | [diff] [blame] | 1559 | // Before tearing down the menu controller bridges, return the history menu to |
| 1560 | // its initial state. |
| 1561 | if (historyMenuBridge_) |
noms | dd5b6a61 | 2014-09-23 15:20:16 | [diff] [blame] | 1562 | historyMenuBridge_->ResetMenu(); |
| 1563 | |
dominickn | b80838f | 2015-09-08 23:00:13 | [diff] [blame] | 1564 | // Rebuild the menus with the new profile. The bookmarks submenu is cached to |
| 1565 | // avoid slowdowns when switching between profiles with large numbers of |
| 1566 | // bookmarks. Before caching, store whether it is hidden, make the menu item |
| 1567 | // visible, and restore its original hidden state after resetting the submenu. |
| 1568 | // This works around an apparent AppKit bug where setting a *different* NSMenu |
| 1569 | // submenu on a *hidden* menu item forces the item to become visible. |
| 1570 | // See https://crbug.com/497813 for more details. |
| 1571 | NSMenuItem* bookmarkItem = [[NSApp mainMenu] itemWithTag:IDC_BOOKMARKS_MENU]; |
| 1572 | BOOL hidden = [bookmarkItem isHidden]; |
| 1573 | [bookmarkItem setHidden:NO]; |
noms | dd5b6a61 | 2014-09-23 15:20:16 | [diff] [blame] | 1574 | lastProfile_ = profile; |
| 1575 | |
Trent Apted | cf95689a | 2017-11-22 00:01:10 | [diff] [blame] | 1576 | auto& entry = profileBookmarkMenuBridgeMap_[profile->GetPath()]; |
| 1577 | if (!entry) { |
| 1578 | // This creates a deep copy, but only the first 3 items in the root menu |
| 1579 | // are really wanted. This can probably be optimized, but lazy-loading of |
| 1580 | // the menu should reduce the impact in most flows. |
dominickn | b80838f | 2015-09-08 23:00:13 | [diff] [blame] | 1581 | base::scoped_nsobject<NSMenu> submenu([[bookmarkItem submenu] copy]); |
Trent Apted | cf95689a | 2017-11-22 00:01:10 | [diff] [blame] | 1582 | [submenu setDelegate:nil]; // The delegate is also copied. Remove it. |
| 1583 | |
| 1584 | entry = std::make_unique<BookmarkMenuBridge>(profile, submenu); |
| 1585 | |
| 1586 | // Clear bookmarks from the old profile. |
| 1587 | entry->ClearBookmarkMenu(); |
lgarron | 9e6dee2 | 2014-11-18 01:03:39 | [diff] [blame] | 1588 | } |
Trent Apted | cf95689a | 2017-11-22 00:01:10 | [diff] [blame] | 1589 | bookmarkMenuBridge_ = entry.get(); |
lgarron | 9e6dee2 | 2014-11-18 01:03:39 | [diff] [blame] | 1590 | |
noms | dd5b6a61 | 2014-09-23 15:20:16 | [diff] [blame] | 1591 | // No need to |BuildMenu| here. It is done lazily upon menu access. |
dominickn | b80838f | 2015-09-08 23:00:13 | [diff] [blame] | 1592 | [bookmarkItem setSubmenu:bookmarkMenuBridge_->BookmarkMenu()]; |
| 1593 | [bookmarkItem setHidden:hidden]; |
noms | dd5b6a61 | 2014-09-23 15:20:16 | [diff] [blame] | 1594 | |
| 1595 | historyMenuBridge_.reset(new HistoryMenuBridge(lastProfile_)); |
| 1596 | historyMenuBridge_->BuildMenu(); |
| 1597 | |
| 1598 | chrome::BrowserCommandController:: |
| 1599 | UpdateSharedCommandsForIncognitoAvailability( |
| 1600 | menuState_.get(), lastProfile_); |
| 1601 | profilePrefRegistrar_.reset(new PrefChangeRegistrar()); |
| 1602 | profilePrefRegistrar_->Init(lastProfile_->GetPrefs()); |
| 1603 | profilePrefRegistrar_->Add( |
| 1604 | prefs::kIncognitoModeAvailability, |
| 1605 | base::Bind(&chrome::BrowserCommandController:: |
| 1606 | UpdateSharedCommandsForIncognitoAvailability, |
| 1607 | menuState_.get(), |
| 1608 | lastProfile_)); |
| 1609 | } |
| 1610 | |
erikchen | 22be209 | 2018-06-12 15:54:00 | [diff] [blame] | 1611 | - (void)updateMenuItemKeyEquivalents { |
| 1612 | BOOL enableCloseTabShortcut = NO; |
| 1613 | id target = [NSApp targetForAction:@selector(performClose:)]; |
| 1614 | |
| 1615 | // |target| is an instance of NSPopover or NSWindow. |
| 1616 | // If a popover (likely the dictionary lookup popover), we want Cmd-W to |
| 1617 | // close the popover so map it to "Close Window". |
| 1618 | // Otherwise, map Cmd-W to "Close Tab" if it's a browser window. |
| 1619 | if ([target isKindOfClass:[NSWindow class]]) { |
| 1620 | NSWindow* window = target; |
| 1621 | NSWindow* mainWindow = [NSApp mainWindow]; |
| 1622 | if (!window || ([window parentWindow] == mainWindow)) { |
| 1623 | // If the target window is a child of the main window (e.g. a bubble), the |
| 1624 | // main window should be the one that handles the close menu item action. |
| 1625 | window = mainWindow; |
| 1626 | } |
| 1627 | Browser* browser = chrome::FindBrowserWithWindow(window); |
| 1628 | enableCloseTabShortcut = browser && browser->is_type_tabbed(); |
| 1629 | } |
| 1630 | |
| 1631 | [self adjustCloseWindowMenuItemKeyEquivalent:enableCloseTabShortcut]; |
| 1632 | [self adjustCloseTabMenuItemKeyEquivalent:enableCloseTabShortcut]; |
| 1633 | } |
| 1634 | |
[email protected] | 92747fe4 | 2014-08-18 23:25:26 | [diff] [blame] | 1635 | - (BOOL)application:(NSApplication*)application |
Elly Fong-Jones | 397009ea | 2019-01-25 17:41:58 | [diff] [blame] | 1636 | willContinueUserActivityWithType:(NSString*)userActivityType { |
[email protected] | 92747fe4 | 2014-08-18 23:25:26 | [diff] [blame] | 1637 | return [userActivityType isEqualToString:NSUserActivityTypeBrowsingWeb]; |
| 1638 | } |
| 1639 | |
| 1640 | - (BOOL)application:(NSApplication*)application |
| 1641 | continueUserActivity:(NSUserActivity*)userActivity |
Leonard Grey | bc00f886 | 2018-09-26 17:24:22 | [diff] [blame] | 1642 | #if !defined(MAC_OS_X_VERSION_10_14) || \ |
| 1643 | MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_14 |
Peter Collingbourne | fc74af3 | 2017-07-10 21:00:18 | [diff] [blame] | 1644 | restorationHandler:(void (^)(NSArray*))restorationHandler |
Leonard Grey | bc00f886 | 2018-09-26 17:24:22 | [diff] [blame] | 1645 | #else |
| 1646 | restorationHandler: |
| 1647 | (void (^)(NSArray<id<NSUserActivityRestoring>>*))restorationHandler |
| 1648 | #endif |
Elly Fong-Jones | 397009ea | 2019-01-25 17:41:58 | [diff] [blame] | 1649 | { |
[email protected] | 92747fe4 | 2014-08-18 23:25:26 | [diff] [blame] | 1650 | if (![userActivity.activityType |
| 1651 | isEqualToString:NSUserActivityTypeBrowsingWeb]) { |
| 1652 | return NO; |
| 1653 | } |
| 1654 | |
erikchen | 96e537d5 | 2014-10-28 23:43:17 | [diff] [blame] | 1655 | NSString* originString = base::mac::ObjCCast<NSString>( |
| 1656 | [userActivity.userInfo objectForKey:handoff::kOriginKey]); |
| 1657 | handoff::Origin origin = handoff::OriginFromString(originString); |
| 1658 | UMA_HISTOGRAM_ENUMERATION( |
| 1659 | "OSX.Handoff.Origin", origin, handoff::ORIGIN_COUNT); |
| 1660 | |
jiangj | c0c568c | 2014-12-04 19:02:12 | [diff] [blame] | 1661 | NSURL* url = userActivity.webpageURL; |
[email protected] | 92747fe4 | 2014-08-18 23:25:26 | [diff] [blame] | 1662 | if (!url) |
| 1663 | return NO; |
| 1664 | |
| 1665 | GURL gurl(base::SysNSStringToUTF8([url absoluteString])); |
| 1666 | std::vector<GURL> gurlVector; |
| 1667 | gurlVector.push_back(gurl); |
| 1668 | |
erikchen | 19ee392 | 2014-10-31 19:14:22 | [diff] [blame] | 1669 | [self openUrlsReplacingNTP:gurlVector]; |
[email protected] | 92747fe4 | 2014-08-18 23:25:26 | [diff] [blame] | 1670 | return YES; |
| 1671 | } |
| 1672 | |
| 1673 | - (void)application:(NSApplication*)application |
| 1674 | didFailToContinueUserActivityWithType:(NSString*)userActivityType |
| 1675 | error:(NSError*)error { |
| 1676 | } |
| 1677 | |
erikchen | 600f796 | 2014-12-12 00:17:38 | [diff] [blame] | 1678 | #pragma mark - Handoff Manager |
| 1679 | |
erikchen | 600f796 | 2014-12-12 00:17:38 | [diff] [blame] | 1680 | - (void)passURLToHandoffManager:(const GURL&)handoffURL { |
Elly Fong-Jones | 397009ea | 2019-01-25 17:41:58 | [diff] [blame] | 1681 | [handoffManager_ updateActiveURL:handoffURL]; |
erikchen | 600f796 | 2014-12-12 00:17:38 | [diff] [blame] | 1682 | } |
| 1683 | |
| 1684 | - (void)updateHandoffManager:(content::WebContents*)webContents { |
erikchen | 600f796 | 2014-12-12 00:17:38 | [diff] [blame] | 1685 | if (!handoffManager_) |
| 1686 | handoffManager_.reset([[HandoffManager alloc] init]); |
| 1687 | |
| 1688 | GURL handoffURL = [self handoffURLFromWebContents:webContents]; |
| 1689 | [self passURLToHandoffManager:handoffURL]; |
| 1690 | } |
| 1691 | |
| 1692 | - (GURL)handoffURLFromWebContents:(content::WebContents*)webContents { |
| 1693 | if (!webContents) |
| 1694 | return GURL(); |
| 1695 | |
| 1696 | Profile* profile = |
| 1697 | Profile::FromBrowserContext(webContents->GetBrowserContext()); |
| 1698 | if (!profile) |
| 1699 | return GURL(); |
| 1700 | |
| 1701 | // Handoff is not allowed from an incognito profile. To err on the safe side, |
| 1702 | // also disallow Handoff from a guest profile. |
Ramin Halavati | 7784def0d | 2019-05-20 09:20:46 | [diff] [blame] | 1703 | if (!profile->IsRegularProfile()) |
erikchen | 600f796 | 2014-12-12 00:17:38 | [diff] [blame] | 1704 | return GURL(); |
| 1705 | |
| 1706 | if (!webContents) |
| 1707 | return GURL(); |
| 1708 | |
| 1709 | return webContents->GetVisibleURL(); |
| 1710 | } |
| 1711 | |
Owen Min | 8c5d538 | 2018-08-02 17:52:43 | [diff] [blame] | 1712 | - (BOOL)isProfileReady { |
| 1713 | return !g_browser_process->browser_policy_connector() |
| 1714 | ->machine_level_user_cloud_policy_controller() |
| 1715 | ->IsEnterpriseStartupDialogShowing(); |
| 1716 | } |
| 1717 | |
erikchen | 600f796 | 2014-12-12 00:17:38 | [diff] [blame] | 1718 | #pragma mark - HandoffActiveURLObserverBridgeDelegate |
| 1719 | |
| 1720 | - (void)handoffActiveURLChanged:(content::WebContents*)webContents { |
| 1721 | [self updateHandoffManager:webContents]; |
| 1722 | } |
| 1723 | |
[email protected] | d630d7d5 | 2010-02-21 00:55:11 | [diff] [blame] | 1724 | @end // @implementation AppController |
[email protected] | 60ad3e2 | 2009-09-18 21:07:19 | [diff] [blame] | 1725 | |
| 1726 | //--------------------------------------------------------------------------- |
| 1727 | |
Elly Fong-Jones | 68b85b8 | 2018-04-21 00:36:54 | [diff] [blame] | 1728 | namespace { |
| 1729 | |
| 1730 | void UpdateProfileInUse(Profile* profile, Profile::CreateStatus status) { |
| 1731 | if (status == Profile::CREATE_STATUS_INITIALIZED) { |
| 1732 | AppController* controller = |
| 1733 | base::mac::ObjCCastStrict<AppController>([NSApp delegate]); |
| 1734 | [controller windowChangedToProfile:profile]; |
| 1735 | } |
| 1736 | } |
| 1737 | |
| 1738 | } // namespace |
| 1739 | |
[email protected] | 216e904 | 2009-10-29 17:05:23 | [diff] [blame] | 1740 | namespace app_controller_mac { |
| 1741 | |
| 1742 | bool IsOpeningNewWindow() { |
| 1743 | return g_is_opening_new_window; |
| 1744 | } |
| 1745 | |
Elly Fong-Jones | 68b85b8 | 2018-04-21 00:36:54 | [diff] [blame] | 1746 | void CreateGuestProfileIfNeeded() { |
| 1747 | g_browser_process->profile_manager()->CreateProfileAsync( |
| 1748 | ProfileManager::GetGuestProfilePath(), |
Marc Treib | 9212716 | 2018-08-27 14:20:03 | [diff] [blame] | 1749 | base::BindRepeating(&UpdateProfileInUse), base::string16(), |
Elly Fong-Jones | 68b85b8 | 2018-04-21 00:36:54 | [diff] [blame] | 1750 | std::string()); |
| 1751 | } |
| 1752 | |
Owen Min | 3bcf8d15 | 2018-07-16 16:50:39 | [diff] [blame] | 1753 | void EnterpriseStartupDialogClosed() { |
| 1754 | AppController* controller = |
| 1755 | base::mac::ObjCCastStrict<AppController>([NSApp delegate]); |
| 1756 | if (controller != nil) { |
| 1757 | NSNotification* notify = [NSNotification |
| 1758 | notificationWithName:NSApplicationDidFinishLaunchingNotification |
| 1759 | object:NSApp]; |
| 1760 | [controller applicationDidFinishLaunching:notify]; |
| 1761 | } |
| 1762 | } |
| 1763 | |
[email protected] | 216e904 | 2009-10-29 17:05:23 | [diff] [blame] | 1764 | } // namespace app_controller_mac |