blob: 8493d1b2233b4d645b31e17bd2b948d043d14f04 [file] [log] [blame]
[email protected]71c0eb92012-01-03 17:57:301// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]1bcdb532009-01-16 17:47:572// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]2c47bc12009-04-10 20:14:005#import "chrome/browser/app_controller_mac.h"
[email protected]1bcdb532009-01-16 17:47:576
avie4d7b6f2015-12-26 00:59:187#include <stddef.h>
8
Ivan Sandrk9669d0e2017-12-15 23:50:209#include <memory>
10
Robert Sesek99845182017-08-24 20:53:3311#include "base/allocator/allocator_shim.h"
Scott Violet44165792018-02-22 02:08:0812#include "base/allocator/buildflags.h"
[email protected]d630d7d52010-02-21 00:55:1113#include "base/auto_reset.h"
[email protected]0b8caad2011-11-28 23:33:2714#include "base/bind.h"
[email protected]57750f822009-04-21 21:43:0915#include "base/command_line.h"
[email protected]57999812013-02-24 05:40:5216#include "base/files/file_path.h"
[email protected]151c4a62011-04-22 04:15:1317#include "base/mac/foundation_util.h"
[email protected]0378bf42011-01-01 18:20:1418#include "base/mac/mac_util.h"
Robert Sesek99845182017-08-24 20:53:3319#include "base/mac/scoped_objc_class_swizzler.h"
avie4d7b6f2015-12-26 00:59:1820#include "base/macros.h"
asvitkineaa060312016-09-01 22:44:1321#include "base/metrics/histogram_macros.h"
bratell0a7406f2017-03-28 07:46:3722#include "base/metrics/user_metrics.h"
gab7af9dc02017-05-05 13:38:5423#include "base/run_loop.h"
[email protected]3ea1b182013-02-08 22:38:4124#include "base/strings/string_number_conversions.h"
[email protected]3268d7b72013-03-28 17:41:4325#include "base/strings/sys_string_conversions.h"
[email protected]135cb802013-06-09 16:44:2026#include "base/strings/utf_string_conversions.h"
Gabriel Charette44db1422018-08-06 11:19:3327#include "base/task/post_task.h"
Gabriel Charette055039132020-02-26 23:02:0628#include "base/task/thread_pool.h"
Etienne Pierre-Doraya620e5e2018-12-06 19:02:1029#include "base/threading/scoped_blocking_call.h"
Nico Webereaa08412019-08-14 01:24:3730#include "build/branding_buildflags.h"
[email protected]1a3aba82010-11-08 23:52:5431#include "chrome/app/chrome_command_ids.h"
Christopher Cameron48dd57db2020-03-17 00:49:2632#include "chrome/browser/apps/app_shim/app_shim_manager_mac.h"
Christopher Cameron4d503112019-08-23 19:10:2833#include "chrome/browser/apps/app_shim/app_shim_termination_manager.h"
Dominick Ng6ff51052018-07-06 05:30:2034#include "chrome/browser/apps/platform_apps/app_window_registry_util.h"
[email protected]a07676b22011-06-17 16:36:5335#include "chrome/browser/background/background_application_list_model.h"
[email protected]78d78a62011-09-29 18:24:3336#include "chrome/browser/background/background_mode_manager.h"
[email protected]5c238752009-06-13 10:29:0737#include "chrome/browser/browser_process.h"
[email protected]fdf40f3e2013-07-11 23:55:4638#include "chrome/browser/chrome_notification_types.h"
Ivan Sandrk9669d0e2017-12-15 23:50:2039#include "chrome/browser/command_updater_impl.h"
peterccb33e82017-05-02 19:00:4440#include "chrome/browser/download/download_core_service.h"
41#include "chrome/browser/download/download_core_service_factory.h"
[email protected]ccfab592013-01-15 06:24:3242#include "chrome/browser/extensions/extension_service.h"
[email protected]e4a377cf2011-10-05 02:51:4943#include "chrome/browser/first_run/first_run.h"
[email protected]2e6389f2012-05-18 19:41:2544#include "chrome/browser/lifetime/application_lifetime.h"
Avi Drissmand30927342018-05-22 15:04:2745#include "chrome/browser/lifetime/browser_shutdown.h"
[email protected]7d1aaa62014-07-18 02:21:3046#include "chrome/browser/mac/mac_startup_profiler.h"
Owen Min0b002e192018-07-10 15:04:1847#include "chrome/browser/policy/chrome_browser_policy_connector.h"
treib6f9598012014-11-11 12:21:2448#include "chrome/browser/prefs/incognito_mode_prefs.h"
lwchkg9c183942016-03-13 06:29:5449#include "chrome/browser/profiles/profile_attributes_entry.h"
50#include "chrome/browser/profiles/profile_attributes_storage.h"
[email protected]8ecad5e2010-12-02 21:18:3351#include "chrome/browser/profiles/profile_manager.h"
[email protected]de71ae992013-07-18 03:30:3852#include "chrome/browser/profiles/profiles_state.h"
[email protected]8d4bbdc2012-09-20 21:36:4953#include "chrome/browser/sessions/session_restore.h"
[email protected]d2912a22011-03-15 15:20:5054#include "chrome/browser/sessions/session_service.h"
[email protected]92371eb2011-04-28 11:50:1555#include "chrome/browser/sessions/session_service_factory.h"
[email protected]92371eb2011-04-28 11:50:1556#include "chrome/browser/sessions/tab_restore_service_factory.h"
[email protected]3d27d272013-07-31 03:15:1657#include "chrome/browser/signin/signin_promo.h"
estade8c0780f2015-08-21 23:36:4158#include "chrome/browser/signin/signin_ui_util.h"
[email protected]40f047972009-11-25 03:54:4059#include "chrome/browser/sync/sync_ui_util.h"
[email protected]ae04f592010-11-18 20:41:3560#include "chrome/browser/ui/browser.h"
[email protected]05454532013-01-22 21:09:0861#include "chrome/browser/ui/browser_command_controller.h"
[email protected]a37d4b02012-06-25 21:56:1062#include "chrome/browser/ui/browser_commands.h"
[email protected]7108d912014-01-30 08:10:4563#include "chrome/browser/ui/browser_dialogs.h"
[email protected]d8748142012-05-16 21:13:4364#include "chrome/browser/ui/browser_finder.h"
scottmg8abbff832016-01-28 22:57:3765#include "chrome/browser/ui/browser_list.h"
[email protected]a13b76f2012-06-20 15:36:2966#include "chrome/browser/ui/browser_mac.h"
[email protected]ae04f592010-11-18 20:41:3567#include "chrome/browser/ui/browser_window.h"
[email protected]5d9cace72012-06-21 16:07:1268#include "chrome/browser/ui/chrome_pages.h"
[email protected]edb2d032013-08-30 09:03:1769#import "chrome/browser/ui/cocoa/apps/app_shim_menu_controller_mac.h"
[email protected]425f7a02014-04-29 01:51:4470#include "chrome/browser/ui/cocoa/apps/quit_with_apps_controller_mac.h"
[email protected]7d791652010-12-01 16:34:4971#import "chrome/browser/ui/cocoa/bookmarks/bookmark_menu_bridge.h"
Tom Anderson6a496032018-08-30 23:38:3772#import "chrome/browser/ui/cocoa/confirm_quit.h"
[email protected]7d791652010-12-01 16:34:4973#import "chrome/browser/ui/cocoa/confirm_quit_panel_controller.h"
erikchen600f7962014-12-12 00:17:3874#include "chrome/browser/ui/cocoa/handoff_active_url_observer_bridge.h"
[email protected]7d791652010-12-01 16:34:4975#import "chrome/browser/ui/cocoa/history_menu_bridge.h"
[email protected]4feb37d92012-07-01 20:22:1376#include "chrome/browser/ui/cocoa/last_active_browser_cocoa.h"
[email protected]ced445e92014-04-09 21:43:5977#import "chrome/browser/ui/cocoa/profiles/profile_menu_controller.h"
Leonard Grey2bdd08f2017-10-03 18:40:5278#import "chrome/browser/ui/cocoa/share_menu_controller.h"
Elly Fong-Jones0d121842019-08-12 22:03:4079#import "chrome/browser/ui/cocoa/tab_menu_bridge.h"
[email protected]ccfab592013-01-15 06:24:3280#include "chrome/browser/ui/extensions/application_launch.h"
[email protected]4feb37d92012-07-01 20:22:1381#include "chrome/browser/ui/startup/startup_browser_creator.h"
82#include "chrome/browser/ui/startup/startup_browser_creator_impl.h"
andresantoso55fd2512015-04-01 21:07:0483#include "chrome/browser/ui/tabs/tab_strip_model.h"
mlermane29d0032014-09-24 19:31:2684#include "chrome/browser/ui/user_manager.h"
Alexey Baskakov2ce2ffc842018-08-06 02:57:1585#include "chrome/browser/web_applications/components/web_app_helpers.h"
Alexey Baskakovf302efe2018-07-28 02:02:3286#include "chrome/browser/web_applications/components/web_app_shortcut_mac.h"
[email protected]12dc3d42010-02-22 23:37:1287#include "chrome/common/chrome_paths_internal.h"
[email protected]f4acfae82009-09-11 00:33:3888#include "chrome/common/chrome_switches.h"
[email protected]ccfab592013-01-15 06:24:3289#include "chrome/common/extensions/extension_constants.h"
[email protected]6c583d32011-08-19 14:46:2290#include "chrome/common/mac/app_mode_common.h"
[email protected]a99fce0e2011-03-21 20:58:4891#include "chrome/common/pref_names.h"
[email protected]024617a2010-08-20 05:08:0592#include "chrome/common/url_constants.h"
[email protected]af39f002014-08-22 10:18:1893#include "chrome/grit/chromium_strings.h"
94#include "chrome/grit/generated_resources.h"
Guillaume Jenkins711bf012020-07-31 19:49:4095#include "components/enterprise/browser/controller/chrome_browser_cloud_management_controller.h"
erikchen600f7962014-12-12 00:17:3896#include "components/handoff/handoff_manager.h"
erikchen332265b2014-11-14 19:59:5297#include "components/handoff/handoff_utility.h"
Michael Giuffrida2dbce0d12017-09-02 03:30:5998#include "components/keep_alive_registry/keep_alive_types.h"
99#include "components/keep_alive_registry/scoped_keep_alive.h"
brettwb1fc1b82016-02-02 00:19:08100#include "components/prefs/pref_service.h"
blundella08c5dd2015-09-18 06:14:16101#include "components/sessions/core/tab_restore_service.h"
[email protected]e582fdd2011-12-20 16:48:17102#include "content/public/browser/download_manager.h"
[email protected]ad50def52011-10-19 23:17:07103#include "content/public/browser/notification_service.h"
[email protected]0d6e9bd2011-10-18 04:29:16104#include "content/public/browser/notification_types.h"
[email protected]5904cb42012-09-24 15:05:20105#include "content/public/browser/plugin_service.h"
mitchelljonese59a1612014-12-16 00:34:59106#include "extensions/browser/extension_registry.h"
tapted574f09c2015-05-19 13:08:08107#include "extensions/browser/extension_system.h"
[email protected]d96cf752014-04-09 04:05:28108#include "net/base/filename_util.h"
Avi Drissmand4887fb22020-04-14 19:16:25109#include "net/base/mac/url_conversions.h"
[email protected]2e29e2232013-07-26 10:40:59110#include "ui/base/cocoa/focus_window_set.h"
[email protected]c051a1b2011-01-21 23:30:17111#include "ui/base/l10n/l10n_util.h"
112#include "ui/base/l10n/l10n_util_mac.h"
[email protected]88d74942009-01-21 22:04:44113
Christopher Cameron48dd57db2020-03-17 00:49:26114using apps::AppShimManager;
[email protected]e6e30ac2014-01-13 21:24:39115using base::UserMetricsAction;
[email protected]b441a8492012-06-06 14:55:57116using content::BrowserContext;
[email protected]e582fdd2011-12-20 16:48:17117using content::DownloadManager;
[email protected]631bb742011-11-02 11:29:39118
[email protected]d630d7d52010-02-21 00:55:11119namespace {
120
[email protected]d4c811e72013-10-29 21:57:55121// How long we allow a workspace change notification to wait to be
122// associated with a dock activation. The animation lasts 250ms. See
123// applicationShouldHandleReopen:hasVisibleWindows:.
124static const int kWorkspaceChangeTimeoutMs = 500;
125
[email protected]a37d4b02012-06-25 21:56:10126// True while AppController is calling chrome::NewEmptyWindow(). We need a
[email protected]fe7c4872012-05-10 20:06:03127// global flag here, analogue to StartupBrowserCreator::InProcessStartup()
128// because otherwise the SessionService will try to restore sessions when we
129// make a new window while there are no other active windows.
[email protected]d630d7d52010-02-21 00:55:11130bool g_is_opening_new_window = false;
131
132// Activates a browser window having the given profile (the last one active) if
133// possible and returns a pointer to the activate |Browser| or NULL if this was
134// not possible. If the last active browser is minimized (in particular, if
135// there are only minimized windows), it will unminimize it.
136Browser* ActivateBrowser(Profile* profile) {
[email protected]5595ce6d2014-07-24 04:01:13137 Browser* browser = chrome::FindLastActiveWithProfile(
scottmg5c03fe022016-02-03 01:27:24138 profile->IsGuestSession() ? profile->GetOffTheRecordProfile() : profile);
[email protected]d630d7d52010-02-21 00:55:11139 if (browser)
140 browser->window()->Activate();
141 return browser;
142}
143
144// Creates an empty browser window with the given profile and returns a pointer
145// to the new |Browser|.
146Browser* CreateBrowser(Profile* profile) {
147 {
[email protected]997ec9f2012-11-21 04:44:14148 base::AutoReset<bool> auto_reset_in_run(&g_is_opening_new_window, true);
scottmg851949002016-02-09 20:09:44149 chrome::NewEmptyWindow(profile);
[email protected]d630d7d52010-02-21 00:55:11150 }
151
[email protected]f2bc6e92012-10-14 02:08:38152 Browser* browser = chrome::GetLastActiveBrowser();
[email protected]d630d7d52010-02-21 00:55:11153 CHECK(browser);
154 return browser;
155}
156
157// Activates a browser window having the given profile (the last one active) if
158// possible or creates an empty one if necessary. Returns a pointer to the
159// activated/new |Browser|.
160Browser* ActivateOrCreateBrowser(Profile* profile) {
161 if (Browser* browser = ActivateBrowser(profile))
162 return browser;
163 return CreateBrowser(profile);
164}
165
[email protected]151c4a62011-04-22 04:15:13166CFStringRef BaseBundleID_CFString() {
167 NSString* base_bundle_id =
168 [NSString stringWithUTF8String:base::mac::BaseBundleID()];
169 return base::mac::NSToCFCast(base_bundle_id);
170}
171
[email protected]12dc3d42010-02-22 23:37:12172// Record the location of the application bundle (containing the main framework)
173// from which Chromium was loaded. This is used by app mode shims to find
174// Chromium.
175void RecordLastRunAppBundlePath() {
176 // Going up three levels from |chrome::GetVersionedDirectory()| gives the
177 // real, user-visible app bundle directory. (The alternatives give either the
178 // framework's path or the initial app's path, which may be an app mode shim
179 // or a unit test.)
Etienne Bergeron436d42212019-02-26 17:15:12180 base::ScopedBlockingCall scoped_blocking_call(FROM_HERE,
181 base::BlockingType::MAY_BLOCK);
[email protected]bf0d26b2013-08-29 03:09:14182
Robert Sesek499376682019-05-16 23:25:48183 // Go up five levels from the versioned sub-directory of the framework, which
184 // is at C.app/Contents/Frameworks/C.framework/Versions/V.
185 base::FilePath app_bundle_path = chrome::GetFrameworkBundlePath()
186 .DirName()
187 .DirName()
188 .DirName()
189 .DirName()
190 .DirName();
[email protected]1c958682013-09-17 03:38:13191 base::ScopedCFTypeRef<CFStringRef> app_bundle_path_cfstring(
192 base::SysUTF8ToCFStringRef(app_bundle_path.value()));
[email protected]b6b72222012-02-11 02:04:13193 CFPreferencesSetAppValue(
194 base::mac::NSToCFCast(app_mode::kLastRunAppBundlePathPrefsKey),
[email protected]1c958682013-09-17 03:38:13195 app_bundle_path_cfstring, BaseBundleID_CFString());
[email protected]12dc3d42010-02-22 23:37:12196}
197
[email protected]7108d912014-01-30 08:10:45198bool IsProfileSignedOut(Profile* profile) {
lwchkg9c183942016-03-13 06:29:54199 ProfileAttributesEntry* entry;
200 bool has_entry =
201 g_browser_process->profile_manager()->GetProfileAttributesStorage().
202 GetProfileAttributesWithPath(profile->GetPath(), &entry);
203 return has_entry && entry->IsSigninRequired();
[email protected]7108d912014-01-30 08:10:45204}
205
Elly Fong-Jones5236f2b2020-03-20 19:54:09206void ConfigureNSAppForKioskMode() {
207 NSApp.presentationOptions =
208 NSApplicationPresentationHideDock | NSApplicationPresentationHideMenuBar |
209 NSApplicationPresentationDisableProcessSwitching |
210 NSApplicationPresentationDisableSessionTermination |
211 NSApplicationPresentationDisableForceQuit |
212 NSApplicationPresentationFullScreen;
213}
214
erikchen600f7962014-12-12 00:17:38215} // namespace
[email protected]d630d7d52010-02-21 00:55:11216
erikchen600f7962014-12-12 00:17:38217@interface AppController () <HandoffActiveURLObserverBridgeDelegate>
[email protected]88d74942009-01-21 22:04:44218- (void)initMenuState;
[email protected]961a6932011-07-19 19:52:46219- (void)initProfileMenu;
[email protected]a99fce0e2011-03-21 20:58:48220- (void)updateConfirmToQuitPrefMenuItem:(NSMenuItem*)item;
[email protected]eef99c22010-08-17 05:55:16221- (void)registerServicesMenuTypesTo:(NSApplication*)app;
[email protected]57750f822009-04-21 21:43:09222- (void)getUrl:(NSAppleEventDescriptor*)event
223 withReply:(NSAppleEventDescriptor*)reply;
[email protected]d4c811e72013-10-29 21:57:55224- (void)activeSpaceDidChange:(NSNotification*)inNotification;
[email protected]dd6ab1282010-07-20 01:07:32225- (void)checkForAnyKeyWindows;
[email protected]73f5b362009-08-10 23:58:21226- (BOOL)userWillWaitForInProgressDownloads:(int)downloadCount;
[email protected]09729a552009-08-03 23:21:41227- (BOOL)shouldQuitWithInProgressDownloads;
[email protected]93b59fc2010-12-21 20:00:47228- (void)executeApplication:(id)sender;
[email protected]1c62b2f2013-06-28 00:15:00229- (void)profileWasRemoved:(const base::FilePath&)profilePath;
erikchen19ee3922014-10-31 19:14:22230
231// Opens a tab for each GURL in |urls|.
232- (void)openUrls:(const std::vector<GURL>&)urls;
233
234// This class cannot open urls until startup has finished. The urls that cannot
235// be opened are cached in |startupUrls_|. This method must be called exactly
236// once after startup has completed. It opens the urls in |startupUrls_|, and
237// clears |startupUrls_|.
238- (void)openStartupUrls;
239
240// Opens a tab for each GURL in |urls|. If there is exactly one tab open before
241// this method is called, and that tab is the NTP, then this method closes the
242// NTP after all the |urls| have been opened.
243- (void)openUrlsReplacingNTP:(const std::vector<GURL>&)urls;
erikchen600f7962014-12-12 00:17:38244
erikchen600f7962014-12-12 00:17:38245// This method passes |handoffURL| to |handoffManager_|.
246- (void)passURLToHandoffManager:(const GURL&)handoffURL;
247
248// Lazily creates the Handoff Manager. Updates the state of the Handoff
249// Manager. This method is idempotent. This should be called:
250// - During initialization.
251// - When the current tab navigates to a new URL.
252// - When the active browser changes.
253// - When the active browser's active tab switches.
254// |webContents| should be the new, active WebContents.
255- (void)updateHandoffManager:(content::WebContents*)webContents;
256
257// Given |webContents|, extracts a GURL to be used for Handoff. This may return
258// the empty GURL.
259- (GURL)handoffURLFromWebContents:(content::WebContents*)webContents;
Owen Min8c5d5382018-08-02 17:52:43260
261// Return false if Chrome startup is paused by dialog and AppController is
262// called without any initialized Profile.
263- (BOOL)isProfileReady;
[email protected]88d74942009-01-21 22:04:44264@end
[email protected]1bcdb532009-01-16 17:47:57265
lwchkg9c183942016-03-13 06:29:54266class AppControllerProfileObserver : public ProfileAttributesStorage::Observer {
[email protected]1c62b2f2013-06-28 00:15:00267 public:
268 AppControllerProfileObserver(
269 ProfileManager* profile_manager, AppController* app_controller)
270 : profile_manager_(profile_manager),
271 app_controller_(app_controller) {
272 DCHECK(profile_manager_);
273 DCHECK(app_controller_);
lwchkg9c183942016-03-13 06:29:54274 profile_manager_->GetProfileAttributesStorage().AddObserver(this);
[email protected]1c62b2f2013-06-28 00:15:00275 }
276
Daniel Chenga542fca2014-10-21 09:51:29277 ~AppControllerProfileObserver() override {
[email protected]1c62b2f2013-06-28 00:15:00278 DCHECK(profile_manager_);
lwchkg9c183942016-03-13 06:29:54279 profile_manager_->GetProfileAttributesStorage().RemoveObserver(this);
[email protected]1c62b2f2013-06-28 00:15:00280 }
281
282 private:
lwchkg9c183942016-03-13 06:29:54283 // ProfileAttributesStorage::Observer implementation:
[email protected]1c62b2f2013-06-28 00:15:00284
Daniel Chenga542fca2014-10-21 09:51:29285 void OnProfileAdded(const base::FilePath& profile_path) override {}
[email protected]1c62b2f2013-06-28 00:15:00286
Daniel Chenga542fca2014-10-21 09:51:29287 void OnProfileWasRemoved(const base::FilePath& profile_path,
288 const base::string16& profile_name) override {
[email protected]1c62b2f2013-06-28 00:15:00289 // When a profile is deleted we need to notify the AppController,
290 // so it can correctly update its pointer to the last used profile.
291 [app_controller_ profileWasRemoved:profile_path];
292 }
293
Daniel Chenga542fca2014-10-21 09:51:29294 void OnProfileWillBeRemoved(const base::FilePath& profile_path) override {}
[email protected]1c62b2f2013-06-28 00:15:00295
Daniel Chenga542fca2014-10-21 09:51:29296 void OnProfileNameChanged(const base::FilePath& profile_path,
297 const base::string16& old_profile_name) override {}
[email protected]1c62b2f2013-06-28 00:15:00298
Daniel Chenga542fca2014-10-21 09:51:29299 void OnProfileAvatarChanged(const base::FilePath& profile_path) override {}
[email protected]1c62b2f2013-06-28 00:15:00300
301 ProfileManager* profile_manager_;
302
303 AppController* app_controller_; // Weak; owns us.
304
305 DISALLOW_COPY_AND_ASSIGN(AppControllerProfileObserver);
306};
307
Robert Sesek1eb85222017-08-24 23:17:09308#if BUILDFLAG(USE_ALLOCATOR_SHIM)
Robert Sesek99845182017-08-24 20:53:33309// On macOS 10.12, the IME system attempts to allocate a 2^64 size buffer,
310// which would typically cause an OOM crash. To avoid this, the problematic
311// method is swizzled out and the make-OOM-fatal bit is disabled for the
312// duration of the original call. https://crbug.com/654695
313static base::mac::ScopedObjCClassSwizzler* g_swizzle_imk_input_session;
314
315@interface OOMDisabledIMKInputSession : NSObject
316@end
317
318@implementation OOMDisabledIMKInputSession
319
320- (void)_coreAttributesFromRange:(NSRange)range
321 whichAttributes:(long long)attributes
322 completionHandler:(void (^)(void))block {
323 // The allocator flag is per-process, so other threads may temporarily
324 // not have fatal OOM occur while this method executes, but it is better
325 // than crashing when using IME.
326 base::allocator::SetCallNewHandlerOnMallocFailure(false);
Elly Fong-Jones0eaa6db2019-11-13 19:01:51327 g_swizzle_imk_input_session
328 ->InvokeOriginal<void, NSRange, long long, void (^)(void)>(
329 self, _cmd, range, attributes, block);
Robert Sesek99845182017-08-24 20:53:33330 base::allocator::SetCallNewHandlerOnMallocFailure(true);
331}
332
333@end
Robert Sesek1eb85222017-08-24 23:17:09334#endif // BUILDFLAG(USE_ALLOCATOR_SHIM)
Robert Sesek99845182017-08-24 20:53:33335
[email protected]1bcdb532009-01-16 17:47:57336@implementation AppController
337
Robert Liao44d3ef12019-12-12 21:04:33338@synthesize startupComplete = _startupComplete;
[email protected]caadfca2010-01-22 21:51:21339
andresantoso04f7bab2015-01-15 19:19:16340- (void)dealloc {
Robert Liao44d3ef12019-12-12 21:04:33341 [[_closeTabMenuItem menu] setDelegate:nil];
andresantoso04f7bab2015-01-15 19:19:16342 [super dealloc];
343}
344
[email protected]cd63ef62009-05-06 19:41:37345// This method is called very early in application startup (ie, before
346// the profile is loaded or any preferences have been registered). Defer any
347// user-data initialization until -applicationDidFinishLaunching:.
Robert Sesek124f80f92018-07-24 20:31:29348- (void)mainMenuCreated {
[email protected]7d1aaa62014-07-18 02:21:30349 MacStartupProfiler::GetInstance()->Profile(
350 MacStartupProfiler::AWAKE_FROM_NIB);
[email protected]136140c2009-05-19 13:58:25351 // We need to register the handlers early to catch events fired on launch.
[email protected]57750f822009-04-21 21:43:09352 NSAppleEventManager* em = [NSAppleEventManager sharedAppleEventManager];
353 [em setEventHandler:self
354 andSelector:@selector(getUrl:withReply:)
355 forEventClass:kInternetEventClass
356 andEventID:kAEGetURL];
357 [em setEventHandler:self
358 andSelector:@selector(getUrl:withReply:)
359 forEventClass:'WWW!' // A particularly ancient AppleEvent that dates
360 andEventID:'OURL']; // back to the Spyglass days.
[email protected]cd63ef62009-05-06 19:41:37361
[email protected]449dd2f2009-05-27 13:04:00362 NSNotificationCenter* notificationCenter =
363 [NSNotificationCenter defaultCenter];
[email protected]ce560f82009-06-03 09:39:44364 [notificationCenter
[email protected]449dd2f2009-05-27 13:04:00365 addObserver:self
andresantoso04f7bab2015-01-15 19:19:16366 selector:@selector(windowDidResignKey:)
[email protected]449dd2f2009-05-27 13:04:00367 name:NSWindowDidResignKeyNotification
368 object:nil];
369 [notificationCenter
370 addObserver:self
andresantoso04f7bab2015-01-15 19:19:16371 selector:@selector(windowDidBecomeMain:)
[email protected]449dd2f2009-05-27 13:04:00372 name:NSWindowDidBecomeMainNotification
373 object:nil];
374 [notificationCenter
375 addObserver:self
andresantoso04f7bab2015-01-15 19:19:16376 selector:@selector(windowDidResignMain:)
[email protected]449dd2f2009-05-27 13:04:00377 name:NSWindowDidResignMainNotification
378 object:nil];
379
[email protected]d4c811e72013-10-29 21:57:55380 // Register for space change notifications.
381 [[[NSWorkspace sharedWorkspace] notificationCenter]
382 addObserver:self
383 selector:@selector(activeSpaceDidChange:)
384 name:NSWorkspaceActiveSpaceDidChangeNotification
385 object:nil];
386
jochenbf9420152015-01-13 14:12:57387 [[[NSWorkspace sharedWorkspace] notificationCenter]
388 addObserver:self
389 selector:@selector(willPowerOff:)
390 name:NSWorkspaceWillPowerOffNotification
391 object:nil];
392
Robert Sesek705c3802018-10-24 20:58:42393 NSMenu* fileMenu = [[[NSApp mainMenu] itemWithTag:IDC_FILE_MENU] submenu];
Robert Liao44d3ef12019-12-12 21:04:33394 _closeTabMenuItem = [fileMenu itemWithTag:IDC_CLOSE_TAB];
395 DCHECK(_closeTabMenuItem);
396 _closeWindowMenuItem = [fileMenu itemWithTag:IDC_CLOSE_WINDOW];
397 DCHECK(_closeWindowMenuItem);
Robert Sesek705c3802018-10-24 20:58:42398
[email protected]136140c2009-05-19 13:58:25399 // Set up the command updater for when there are no windows open
400 [self initMenuState];
[email protected]961a6932011-07-19 19:52:46401
402 // Initialize the Profile menu.
403 [self initProfileMenu];
[email protected]136140c2009-05-19 13:58:25404}
405
[email protected]71c0eb92012-01-03 17:57:30406- (void)unregisterEventHandlers {
407 NSAppleEventManager* em = [NSAppleEventManager sharedAppleEventManager];
408 [em removeEventHandlerForEventClass:kInternetEventClass
409 andEventID:kAEGetURL];
[email protected]71c0eb92012-01-03 17:57:30410 [em removeEventHandlerForEventClass:'WWW!'
411 andEventID:'OURL'];
412 [[NSNotificationCenter defaultCenter] removeObserver:self];
[email protected]d4c811e72013-10-29 21:57:55413 [[[NSWorkspace sharedWorkspace] notificationCenter] removeObserver:self];
[email protected]71c0eb92012-01-03 17:57:30414}
415
[email protected]7dc8c6b2010-04-09 17:02:50416// (NSApplicationDelegate protocol) This is the Apple-approved place to override
417// the default handlers.
418- (void)applicationWillFinishLaunching:(NSNotification*)notification {
[email protected]7d1aaa62014-07-18 02:21:30419 MacStartupProfiler::GetInstance()->Profile(
420 MacStartupProfiler::WILL_FINISH_LAUNCHING);
sdy57883382016-09-12 16:00:37421
Erik Chen2d245df72017-07-15 03:16:06422 if (@available(macOS 10.12, *)) {
sdy57883382016-09-12 16:00:37423 NSWindow.allowsAutomaticWindowTabbing = NO;
424 }
spqchan210e2ed2017-07-27 23:17:40425
426 // If the OSX version supports this method, the system will automatically
427 // hide the item if there's no touch bar. However, for unsupported versions,
spqchan30556c32018-09-05 23:14:08428 // we'll have to manually remove the item from the menu.
spqchan210e2ed2017-07-27 23:17:40429 if (![NSApp
spqchan30556c32018-09-05 23:14:08430 respondsToSelector:@selector(toggleTouchBarCustomizationPalette:)]) {
spqchan210e2ed2017-07-27 23:17:40431 NSMenu* mainMenu = [NSApp mainMenu];
432 NSMenu* viewMenu = [[mainMenu itemWithTag:IDC_VIEW_MENU] submenu];
433 NSMenuItem* customizeItem = [viewMenu itemWithTag:IDC_CUSTOMIZE_TOUCH_BAR];
434 if (customizeItem)
435 [viewMenu removeItem:customizeItem];
436 }
Leonard Grey2bdd08f2017-10-03 18:40:52437
Leonard Grey34b5e622018-07-30 19:05:55438 [self initShareMenu];
[email protected]7dc8c6b2010-04-09 17:02:50439}
440
441- (BOOL)tryToTerminateApplication:(NSApplication*)app {
jochenbf9420152015-01-13 14:12:57442 // Reset this now that we've received the call to terminate.
Robert Liao44d3ef12019-12-12 21:04:33443 BOOL isPoweringOff = _isPoweringOff;
444 _isPoweringOff = NO;
jochenbf9420152015-01-13 14:12:57445
[email protected]3076fad2010-04-28 18:11:45446 // Check for in-process downloads, and prompt the user if they really want
447 // to quit (and thus cancel downloads). Only check if we're not already
448 // shutting down, else the user might be prompted multiple times if the
449 // download isn't stopped before terminate is called again.
450 if (!browser_shutdown::IsTryingToQuit() &&
451 ![self shouldQuitWithInProgressDownloads])
452 return NO;
453
[email protected]7dc8c6b2010-04-09 17:02:50454 // TODO(viettrungluu): Remove Apple Event handlers here? (It's safe to leave
455 // them in, but I'm not sure about UX; we'd also want to disable other things
456 // though.) http://crbug.com/40861
457
[email protected]70593e52014-05-15 08:30:23458 // Check for active apps. If quitting is prevented, only close browsers and
459 // sessions.
jochenbf9420152015-01-13 14:12:57460 if (!browser_shutdown::IsTryingToQuit() && !isPoweringOff &&
Robert Liao44d3ef12019-12-12 21:04:33461 _quitWithAppsController.get() && !_quitWithAppsController->ShouldQuit()) {
avi3ef9ec9e2014-12-22 22:50:17462 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
mitchelljones3bc6ced2014-12-19 05:01:37463 switches::kHostedAppQuitNotification)) {
464 return NO;
465 }
466
[email protected]70593e52014-05-15 08:30:23467 content::NotificationService::current()->Notify(
468 chrome::NOTIFICATION_CLOSE_ALL_BROWSERS_REQUEST,
469 content::NotificationService::AllSources(),
470 content::NotificationService::NoDetails());
471 // This will close all browser sessions.
472 chrome::CloseAllBrowsers();
jackhouae8e6e52015-05-29 06:36:46473
474 // At this point, the user has already chosen to cancel downloads. If we
475 // were to shut down as usual, the downloads would be cancelled in
peterccb33e82017-05-02 19:00:44476 // DownloadCoreService::Shutdown().
477 DownloadCoreService::CancelAllDownloads();
jackhouae8e6e52015-05-29 06:36:46478
[email protected]70593e52014-05-15 08:30:23479 return NO;
480 }
481
[email protected]0665ebe2013-02-13 09:53:19482 size_t num_browsers = chrome::GetTotalBrowserCount();
[email protected]7dc8c6b2010-04-09 17:02:50483
[email protected]0c95faf42013-10-28 06:27:20484 // Initiate a shutdown (via chrome::CloseAllBrowsersAndQuit()) if we aren't
[email protected]c984d9f2010-07-20 20:52:20485 // already shutting down.
[email protected]ef61b8c2012-01-20 10:54:56486 if (!browser_shutdown::IsTryingToQuit()) {
487 content::NotificationService::current()->Notify(
[email protected]d53a08c2012-07-18 20:35:30488 chrome::NOTIFICATION_CLOSE_ALL_BROWSERS_REQUEST,
[email protected]ef61b8c2012-01-20 10:54:56489 content::NotificationService::AllSources(),
490 content::NotificationService::NoDetails());
[email protected]0c95faf42013-10-28 06:27:20491 chrome::CloseAllBrowsersAndQuit();
[email protected]ef61b8c2012-01-20 10:54:56492 }
[email protected]7dc8c6b2010-04-09 17:02:50493
[email protected]c984d9f2010-07-20 20:52:20494 return num_browsers == 0 ? YES : NO;
[email protected]7dc8c6b2010-04-09 17:02:50495}
496
497- (void)stopTryingToTerminateApplication:(NSApplication*)app {
498 if (browser_shutdown::IsTryingToQuit()) {
499 // Reset the "trying to quit" state, so that closing all browser windows
500 // will no longer lead to termination.
501 browser_shutdown::SetTryingToQuit(false);
502
503 // TODO(viettrungluu): Were we to remove Apple Event handlers above, we
504 // would have to reinstall them here. http://crbug.com/40861
505 }
[email protected]a9e8afc2009-08-11 22:03:17506}
507
Avi Drissmanf19bb472018-11-29 19:51:28508- (BOOL)runConfirmQuitPanel {
[email protected]90a4b9d2013-07-31 12:14:40509 // If there are no windows, quit immediately.
scottmg8abbff832016-01-28 22:57:37510 if (BrowserList::GetInstance()->empty() &&
jackhou261f7f32015-01-07 00:59:03511 !AppWindowRegistryUtil::IsAppWindowVisibleInAnyProfile(0)) {
Avi Drissmanf19bb472018-11-29 19:51:28512 return YES;
[email protected]90a4b9d2013-07-31 12:14:40513 }
514
[email protected]a99fce0e2011-03-21 20:58:48515 // Check if the preference is turned on.
[email protected]b6366ffa2012-02-29 17:12:26516 const PrefService* prefs = g_browser_process->local_state();
[email protected]1eaddf32011-04-05 15:09:23517 if (!prefs->GetBoolean(prefs::kConfirmToQuitEnabled)) {
518 confirm_quit::RecordHistogram(confirm_quit::kNoConfirm);
Avi Drissmanf19bb472018-11-29 19:51:28519 return YES;
[email protected]1eaddf32011-04-05 15:09:23520 }
[email protected]177aceb2010-11-03 16:17:41521
Avi Drissmanf19bb472018-11-29 19:51:28522 // Run only for keyboard-initiated quits.
523 if ([[NSApp currentEvent] type] != NSKeyDown)
[email protected]2020fd0b2010-12-10 01:16:50524 return NSTerminateNow;
[email protected]177aceb2010-11-03 16:17:41525
[email protected]5df54fb2011-03-07 18:15:26526 return [[ConfirmQuitPanelController sharedController]
Avi Drissmanf19bb472018-11-29 19:51:28527 runModalLoopForApplication:NSApp];
[email protected]177aceb2010-11-03 16:17:41528}
529
[email protected]449dd2f2009-05-27 13:04:00530// Called when the app is shutting down. Clean-up as appropriate.
[email protected]7dc8c6b2010-04-09 17:02:50531- (void)applicationWillTerminate:(NSNotification*)aNotification {
[email protected]7dc8c6b2010-04-09 17:02:50532 // There better be no browser windows left at this point.
[email protected]0665ebe2013-02-13 09:53:19533 CHECK_EQ(0u, chrome::GetTotalBrowserCount());
[email protected]3b6aa8b62009-09-15 21:36:11534
[email protected]c984d9f2010-07-20 20:52:20535 // Tell BrowserList not to keep the browser process alive. Once all the
536 // browsers get dealloc'd, it will stop the RunLoop and fall back into main().
Robert Liao44d3ef12019-12-12 21:04:33537 _keep_alive.reset();
[email protected]fbc947b2009-06-19 13:28:24538
[email protected]02a31dd2013-02-05 08:40:52539 // Reset all pref watching, as this object outlives the prefs system.
Robert Liao44d3ef12019-12-12 21:04:33540 _profilePrefRegistrar.reset();
541 _localPrefRegistrar.RemoveAll();
[email protected]02a31dd2013-02-05 08:40:52542
[email protected]71c0eb92012-01-03 17:57:30543 [self unregisterEventHandlers];
[email protected]edb2d032013-08-30 09:03:17544
Robert Liao44d3ef12019-12-12 21:04:33545 _appShimMenuController.reset();
lgarron9e6dee22014-11-18 01:03:39546
Robert Liao44d3ef12019-12-12 21:04:33547 _profileBookmarkMenuBridgeMap.clear();
[email protected]449dd2f2009-05-27 13:04:00548}
549
[email protected]3b6aa8b62009-09-15 21:36:11550- (void)didEndMainMessageLoop {
scottmg34c5dd882016-02-03 05:21:54551 DCHECK_EQ(0u, chrome::GetBrowserCount([self lastProfile]));
552 if (!chrome::GetBrowserCount([self lastProfile])) {
[email protected]3b6aa8b62009-09-15 21:36:11553 // As we're shutting down, we need to nuke the TabRestoreService, which
554 // will start the shutdown of the NavigationControllers and allow for
555 // proper shutdown. If we don't do this, Chrome won't shut down cleanly,
556 // and may end up crashing when some thread tries to use the IO thread (or
557 // another thread) that is no longer valid.
[email protected]a585a8db2011-06-20 18:58:35558 TabRestoreServiceFactory::ResetForProfile([self lastProfile]);
[email protected]3b6aa8b62009-09-15 21:36:11559 }
560}
561
[email protected]8cfbdbd2011-06-16 04:17:17562// If the window has a tab controller, make "close window" be cmd-shift-w,
563// otherwise leave it as the normal cmd-w. Capitalization of the key equivalent
[email protected]eddcf7502012-02-09 22:43:48564// affects whether the shift modifier is used.
[email protected]ebbe94b2012-01-18 20:11:56565- (void)adjustCloseWindowMenuItemKeyEquivalent:(BOOL)enableCloseTabShortcut {
Robert Liao44d3ef12019-12-12 21:04:33566 [_closeWindowMenuItem setKeyEquivalent:(enableCloseTabShortcut ? @"W" :
[email protected]ebbe94b2012-01-18 20:11:56567 @"w")];
Robert Liao44d3ef12019-12-12 21:04:33568 [_closeWindowMenuItem setKeyEquivalentModifierMask:NSCommandKeyMask];
[email protected]8cfbdbd2011-06-16 04:17:17569}
570
571// If the window has a tab controller, make "close tab" take over cmd-w,
572// otherwise it shouldn't have any key-equivalent because it should be disabled.
[email protected]ebbe94b2012-01-18 20:11:56573- (void)adjustCloseTabMenuItemKeyEquivalent:(BOOL)enableCloseTabShortcut {
574 if (enableCloseTabShortcut) {
Robert Liao44d3ef12019-12-12 21:04:33575 [_closeTabMenuItem setKeyEquivalent:@"w"];
576 [_closeTabMenuItem setKeyEquivalentModifierMask:NSCommandKeyMask];
[email protected]8cfbdbd2011-06-16 04:17:17577 } else {
Robert Liao44d3ef12019-12-12 21:04:33578 [_closeTabMenuItem setKeyEquivalent:@""];
579 [_closeTabMenuItem setKeyEquivalentModifierMask:0];
[email protected]8cfbdbd2011-06-16 04:17:17580 }
581}
582
[email protected]9f5f79b62011-10-28 00:43:45583// See if the focused window window has tabs, and adjust the key equivalents for
[email protected]8cfbdbd2011-06-16 04:17:17584// Close Tab/Close Window accordingly.
andresantoso04f7bab2015-01-15 19:19:16585- (void)menuNeedsUpdate:(NSMenu*)menu {
Robert Liao44d3ef12019-12-12 21:04:33586 DCHECK(menu == [_closeTabMenuItem menu]);
erikchen22be2092018-06-12 15:54:00587 [self updateMenuItemKeyEquivalents];
[email protected]8cfbdbd2011-06-16 04:17:17588}
589
andresantoso04f7bab2015-01-15 19:19:16590- (void)windowDidResignKey:(NSNotification*)notify {
591 // If a window is closed, this notification is fired but |[NSApp keyWindow]|
592 // returns nil regardless of whether any suitable candidates for the key
593 // window remain. It seems that the new key window for the app is not set
594 // until after this notification is fired, so a check is performed after the
595 // run loop is allowed to spin.
596 [self performSelector:@selector(checkForAnyKeyWindows)
597 withObject:nil
598 afterDelay:0.0];
[email protected]8cfbdbd2011-06-16 04:17:17599}
600
andresantoso04f7bab2015-01-15 19:19:16601- (void)windowDidBecomeMain:(NSNotification*)notify {
andresantoso55fd2512015-04-01 21:07:04602 Browser* browser = chrome::FindBrowserWithWindow([notify object]);
Elly Fong-Jones0d121842019-08-12 22:03:40603 if (!browser)
604 return;
605
Joel Hockey2687ab52019-08-14 23:59:46606 if (browser->is_type_normal()) {
Robert Liao44d3ef12019-12-12 21:04:33607 _tabMenuBridge = std::make_unique<TabMenuBridge>(
Elly Fong-Jones0d121842019-08-12 22:03:40608 browser->tab_strip_model(),
609 [[NSApp mainMenu] itemWithTag:IDC_TAB_MENU]);
Robert Liao44d3ef12019-12-12 21:04:33610 _tabMenuBridge->BuildMenu();
Elly Fong-Jones0d121842019-08-12 22:03:40611 } else {
Robert Liao44d3ef12019-12-12 21:04:33612 _tabMenuBridge.reset();
Elly Fong-Jones0d121842019-08-12 22:03:40613 }
614
615 [self windowChangedToProfile:browser->profile()->GetOriginalProfile()];
andresantoso04f7bab2015-01-15 19:19:16616}
617
618- (void)windowDidResignMain:(NSNotification*)notify {
Owen Min8c5d5382018-08-02 17:52:43619 if (chrome::GetTotalBrowserCount() == 0 && [self isProfileReady]) {
[email protected]cae68b3a2011-11-21 23:29:49620 [self windowChangedToProfile:
621 g_browser_process->profile_manager()->GetLastUsedProfile()];
[email protected]5c593702011-06-17 18:35:24622 }
623}
624
[email protected]d4c811e72013-10-29 21:57:55625- (void)activeSpaceDidChange:(NSNotification*)notify {
Robert Liao44d3ef12019-12-12 21:04:33626 if (_reopenTime.is_null() ||
[email protected]d4c811e72013-10-29 21:57:55627 ![NSApp isActive] ||
Robert Liao44d3ef12019-12-12 21:04:33628 (base::TimeTicks::Now() - _reopenTime).InMilliseconds() >
[email protected]d4c811e72013-10-29 21:57:55629 kWorkspaceChangeTimeoutMs) {
630 return;
631 }
632
633 // The last applicationShouldHandleReopen:hasVisibleWindows: call
634 // happened during a space change. Now that the change has
635 // completed, raise browser windows.
Robert Liao44d3ef12019-12-12 21:04:33636 _reopenTime = base::TimeTicks();
Christopher Cameron359c61e22018-10-18 08:06:20637 std::set<gfx::NativeWindow> browserWindows;
scottmg8abbff832016-01-28 22:57:37638 for (auto* browser : *BrowserList::GetInstance())
[email protected]d4c811e72013-10-29 21:57:55639 browserWindows.insert(browser->window()->GetNativeWindow());
Christopher Cameron359c61e22018-10-18 08:06:20640 if (!browserWindows.empty())
[email protected]6c4b79602014-05-28 16:47:21641 ui::FocusWindowSetOnCurrentSpace(browserWindows);
[email protected]d4c811e72013-10-29 21:57:55642}
643
jochenbf9420152015-01-13 14:12:57644// Called when shutting down or logging out.
645- (void)willPowerOff:(NSNotification*)notify {
646 // Don't attempt any shutdown here. Cocoa will shortly call
647 // -[BrowserCrApplication terminate:].
Robert Liao44d3ef12019-12-12 21:04:33648 _isPoweringOff = YES;
jochenbf9420152015-01-13 14:12:57649}
650
[email protected]dd6ab1282010-07-20 01:07:32651- (void)checkForAnyKeyWindows {
652 if ([NSApp keyWindow])
653 return;
654
[email protected]ad50def52011-10-19 23:17:07655 content::NotificationService::current()->Notify(
[email protected]d53a08c2012-07-18 20:35:30656 chrome::NOTIFICATION_NO_KEY_WINDOW,
[email protected]ad50def52011-10-19 23:17:07657 content::NotificationService::AllSources(),
658 content::NotificationService::NoDetails());
[email protected]449dd2f2009-05-27 13:04:00659}
660
[email protected]414fde592009-05-21 16:14:43661// If the auto-update interval is not set, make it 5 hours.
[email protected]414fde592009-05-21 16:14:43662// Placed here for 2 reasons:
663// 1) Same spot as other Pref stuff
664// 2) Try and be friendly by keeping this after app launch
[email protected]414fde592009-05-21 16:14:43665- (void)setUpdateCheckInterval {
Nico Webereaa08412019-08-14 01:24:37666#if BUILDFLAG(GOOGLE_CHROME_BRANDING)
[email protected]8be07172013-04-27 05:25:49667 CFStringRef app = CFSTR("com.google.Keystone.Agent");
668 CFStringRef checkInterval = CFSTR("checkInterval");
[email protected]414fde592009-05-21 16:14:43669 CFPropertyListRef plist = CFPreferencesCopyAppValue(checkInterval, app);
670 if (!plist) {
671 const float fiveHoursInSeconds = 5.0 * 60.0 * 60.0;
[email protected]7dc8c6b2010-04-09 17:02:50672 NSNumber* value = [NSNumber numberWithFloat:fiveHoursInSeconds];
[email protected]414fde592009-05-21 16:14:43673 CFPreferencesSetAppValue(checkInterval, value, app);
674 CFPreferencesAppSynchronize(app);
675 }
676#endif
677}
678
[email protected]66791aff2014-04-29 09:45:38679- (void)openStartupUrls {
Robert Liao44d3ef12019-12-12 21:04:33680 DCHECK(_startupComplete);
681 [self openUrlsReplacingNTP:_startupUrls];
682 _startupUrls.clear();
erikchen19ee3922014-10-31 19:14:22683}
684
685- (void)openUrlsReplacingNTP:(const std::vector<GURL>&)urls {
686 if (urls.empty())
687 return;
688
[email protected]66791aff2014-04-29 09:45:38689 // On Mac, the URLs are passed in via Cocoa, not command line. The Chrome
690 // NSApplication is created in MainMessageLoop, and then the shortcut urls
691 // are passed in via Apple events. At this point, the first browser is
692 // already loaded in PreMainMessageLoop. If we initialize NSApplication
693 // before PreMainMessageLoop to capture shortcut URL events, it may cause
694 // more problems because it relies on things created in PreMainMessageLoop
695 // and may break existing message loop design.
erikchen19ee3922014-10-31 19:14:22696
697 // If the browser hasn't started yet, just queue up the URLs.
Robert Liao44d3ef12019-12-12 21:04:33698 if (!_startupComplete) {
699 _startupUrls.insert(_startupUrls.end(), urls.begin(), urls.end());
[email protected]66791aff2014-04-29 09:45:38700 return;
erikchen19ee3922014-10-31 19:14:22701 }
[email protected]66791aff2014-04-29 09:45:38702
703 // If there's only 1 tab and the tab is NTP, close this NTP tab and open all
704 // startup urls in new tabs, because the omnibox will stay focused if we
705 // load url in NTP tab.
Christian Dullweber820ee5f2018-04-26 13:24:31706 Profile* profile =
707 g_browser_process->profile_manager()->GetLastUsedProfileAllowedByPolicy();
708 Browser* browser = chrome::FindLastActiveWithProfile(profile);
709
[email protected]66791aff2014-04-29 09:45:38710 int startupIndex = TabStripModel::kNoTab;
711 content::WebContents* startupContent = NULL;
712
713 if (browser && browser->tab_strip_model()->count() == 1) {
714 startupIndex = browser->tab_strip_model()->active_index();
715 startupContent = browser->tab_strip_model()->GetActiveWebContents();
716 }
717
erikchen19ee3922014-10-31 19:14:22718 [self openUrls:urls];
[email protected]66791aff2014-04-29 09:45:38719
kristiparka34b247a2018-03-09 19:05:34720 // In test environments where there is no network connection, the visible NTP
721 // URL may be chrome-search://local-ntp/local-ntp.html instead of
722 // chrome://newtab/. See local_ntp_test_utils::GetFinalNtpUrl for more
723 // details.
724 // This NTP check should be replaced once https://crbug.com/624410 is fixed.
[email protected]66791aff2014-04-29 09:45:38725 if (startupIndex != TabStripModel::kNoTab &&
kristiparka34b247a2018-03-09 19:05:34726 (startupContent->GetVisibleURL() == chrome::kChromeUINewTabURL ||
727 startupContent->GetVisibleURL() == chrome::kChromeSearchLocalNtpUrl)) {
[email protected]66791aff2014-04-29 09:45:38728 browser->tab_strip_model()->CloseWebContentsAt(startupIndex,
729 TabStripModel::CLOSE_NONE);
730 }
731}
732
[email protected]136140c2009-05-19 13:58:25733// This is called after profiles have been loaded and preferences registered.
734// It is safe to access the default profile here.
Owen Min3bcf8d152018-07-16 16:50:39735- (void)applicationDidFinishLaunching:(NSNotification*)notify {
736 if (g_browser_process->browser_policy_connector()
Owen Mina12ff6d2019-10-17 16:55:59737 ->chrome_browser_cloud_management_controller()
Owen Min3bcf8d152018-07-16 16:50:39738 ->IsEnterpriseStartupDialogShowing()) {
739 // As Chrome is not ready when the Enterprise startup dialog is being shown.
740 // Store the notification as it will be reposted when the dialog is closed.
741 return;
742 }
743
[email protected]7d1aaa62014-07-18 02:21:30744 MacStartupProfiler::GetInstance()->Profile(
745 MacStartupProfiler::DID_FINISH_LAUNCHING);
746 MacStartupProfiler::GetInstance()->RecordMetrics();
747
[email protected]c984d9f2010-07-20 20:52:20748 // Notify BrowserList to keep the application running so it doesn't go away
749 // when all the browser windows get closed.
Robert Liao44d3ef12019-12-12 21:04:33750 _keep_alive.reset(new ScopedKeepAlive(KeepAliveOrigin::APP_CONTROLLER,
dgn02377782016-03-12 00:58:38751 KeepAliveRestartOption::DISABLED));
[email protected]136140c2009-05-19 13:58:25752
[email protected]414fde592009-05-21 16:14:43753 [self setUpdateCheckInterval];
[email protected]2bcec612009-05-14 17:50:53754
[email protected]edb2d032013-08-30 09:03:17755 // Start managing the menu for app windows. This needs to be done here because
756 // main menu item titles are not yet initialized in awakeFromNib.
[email protected]e7dc3992013-11-06 02:16:42757 [self initAppShimMenuController];
[email protected]edb2d032013-08-30 09:03:17758
[email protected]425f7a02014-04-29 01:51:44759 // If enabled, keep Chrome alive when apps are open instead of quitting all
760 // apps.
Robert Liao44d3ef12019-12-12 21:04:33761 _quitWithAppsController = new QuitWithAppsController();
[email protected]425f7a02014-04-29 01:51:44762
andresantoso04f7bab2015-01-15 19:19:16763 // Dynamically update shortcuts for "Close Window" and "Close Tab" menu items.
Robert Liao44d3ef12019-12-12 21:04:33764 [[_closeTabMenuItem menu] setDelegate:self];
andresantoso04f7bab2015-01-15 19:19:16765
lwchkg9c183942016-03-13 06:29:54766 // Instantiate the ProfileAttributesStorage observer so that we can get
[email protected]1c62b2f2013-06-28 00:15:00767 // notified when a profile is deleted.
Robert Liao44d3ef12019-12-12 21:04:33768 _profileAttributesStorageObserver.reset(new AppControllerProfileObserver(
[email protected]1c62b2f2013-06-28 00:15:00769 g_browser_process->profile_manager(), self));
770
[email protected]12dc3d42010-02-22 23:37:12771 // Record the path to the (browser) app bundle; this is used by the app mode
Gabriel Charette66d79ed22017-06-21 17:58:22772 // shim.
Robert Sesek7f66190e2019-06-13 12:28:41773 if (base::mac::AmIBundled()) {
Gabriel Charette055039132020-02-26 23:02:06774 base::ThreadPool::PostTask(
Sami Kyostila5e1306d2019-08-14 11:01:29775 FROM_HERE,
Gabriel Charette055039132020-02-26 23:02:06776 {base::MayBlock(), base::TaskPriority::BEST_EFFORT,
Sami Kyostila5e1306d2019-08-14 11:01:29777 base::TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN},
778 base::BindOnce(&RecordLastRunAppBundlePath));
Robert Sesek7f66190e2019-06-13 12:28:41779 }
[email protected]12dc3d42010-02-22 23:37:12780
[email protected]eef99c22010-08-17 05:55:16781 // Makes "Services" menu items available.
782 [self registerServicesMenuTypesTo:[notify object]];
783
Robert Liao44d3ef12019-12-12 21:04:33784 _startupComplete = YES;
[email protected]caadfca2010-01-22 21:51:21785
Leonard Grey354221d2020-03-27 16:41:55786 if (base::CommandLine::ForCurrentProcess()->HasSwitch(switches::kKioskMode))
Elly Fong-Jones5236f2b2020-03-20 19:54:09787 ConfigureNSAppForKioskMode();
788
scottmg5c03fe022016-02-03 01:27:24789 Browser* browser = chrome::FindLastActive();
erikchen600f7962014-12-12 00:17:38790 content::WebContents* activeWebContents = nullptr;
791 if (browser)
792 activeWebContents = browser->tab_strip_model()->GetActiveWebContents();
793 [self updateHandoffManager:activeWebContents];
[email protected]66791aff2014-04-29 09:45:38794 [self openStartupUrls];
[email protected]aac169d2011-03-18 19:53:03795
[email protected]05454532013-01-22 21:09:08796 PrefService* localState = g_browser_process->local_state();
797 if (localState) {
Robert Liao44d3ef12019-12-12 21:04:33798 _localPrefRegistrar.Init(localState);
799 _localPrefRegistrar.Add(
[email protected]05454532013-01-22 21:09:08800 prefs::kAllowFileSelectionDialogs,
Alexander Cooperbc87af32020-07-15 15:59:45801 base::BindRepeating(
802 &chrome::BrowserCommandController::UpdateOpenFileState,
803 _menuState.get()));
[email protected]05454532013-01-22 21:09:08804 }
erikchen600f7962014-12-12 00:17:38805
Robert Liao44d3ef12019-12-12 21:04:33806 _handoff_active_url_observer_bridge.reset(
erikchen600f7962014-12-12 00:17:38807 new HandoffActiveURLObserverBridge(self));
Robert Sesek99845182017-08-24 20:53:33808
Robert Sesek1eb85222017-08-24 23:17:09809#if BUILDFLAG(USE_ALLOCATOR_SHIM)
Robert Sesek99845182017-08-24 20:53:33810 // Disable fatal OOM to hack around an OS bug https://crbug.com/654695.
811 if (base::mac::IsOS10_12()) {
812 g_swizzle_imk_input_session = new base::mac::ScopedObjCClassSwizzler(
813 NSClassFromString(@"IMKInputSession"),
814 [OOMDisabledIMKInputSession class],
815 @selector(_coreAttributesFromRange:whichAttributes:completionHandler:));
816 }
Robert Sesek1eb85222017-08-24 23:17:09817#endif
[email protected]7c321082009-02-09 15:35:47818}
819
[email protected]73f5b362009-08-10 23:58:21820// Helper function for populating and displaying the in progress downloads at
821// exit alert panel.
822- (BOOL)userWillWaitForInProgressDownloads:(int)downloadCount {
[email protected]83a9f652012-04-25 21:03:48823 NSString* titleText = nil;
[email protected]e8b5f7882009-09-14 20:59:13824 NSString* explanationText = nil;
[email protected]73f5b362009-08-10 23:58:21825 NSString* waitTitle = nil;
826 NSString* exitTitle = nil;
827
828 // Set the dialog text based on whether or not there are multiple downloads.
jshinec68f032015-05-14 00:03:23829 // Dialog text: warning and explanation.
Bret Sepulveda331f10a2018-02-13 22:37:42830 titleText = l10n_util::GetPluralNSStringF(IDS_ABANDON_DOWNLOAD_DIALOG_TITLE,
831 downloadCount);
832 explanationText =
833 l10n_util::GetNSString(IDS_ABANDON_DOWNLOAD_DIALOG_BROWSER_MESSAGE);
834 // "Cancel download and exit" button text.
835 exitTitle = l10n_util::GetNSString(IDS_ABANDON_DOWNLOAD_DIALOG_EXIT_BUTTON);
[email protected]83a9f652012-04-25 21:03:48836
Bret Sepulveda331f10a2018-02-13 22:37:42837 // "Wait for download" button text.
838 waitTitle =
839 l10n_util::GetNSString(IDS_ABANDON_DOWNLOAD_DIALOG_CONTINUE_BUTTON);
[email protected]73f5b362009-08-10 23:58:21840
Elly Fong-Jonesbd6d39d2019-01-23 22:10:28841 base::scoped_nsobject<NSAlert> alert([[NSAlert alloc] init]);
842 [alert setMessageText:titleText];
843 [alert setInformativeText:explanationText];
844 [alert addButtonWithTitle:waitTitle];
845 [alert addButtonWithTitle:exitTitle];
846
[email protected]73f5b362009-08-10 23:58:21847 // 'waitButton' is the default choice.
Elly Fong-Jonesbd6d39d2019-01-23 22:10:28848 int choice = [alert runModal];
849 return choice == NSAlertFirstButtonReturn ? YES : NO;
[email protected]73f5b362009-08-10 23:58:21850}
851
852// Check all profiles for in progress downloads, and if we find any, prompt the
[email protected]09729a552009-08-03 23:21:41853// user to see if we should continue to exit (and thus cancel the downloads), or
854// if we should wait.
855- (BOOL)shouldQuitWithInProgressDownloads {
[email protected]73f5b362009-08-10 23:58:21856 ProfileManager* profile_manager = g_browser_process->profile_manager();
857 if (!profile_manager)
858 return YES;
[email protected]09729a552009-08-03 23:21:41859
[email protected]844a1002011-04-19 11:37:21860 std::vector<Profile*> profiles(profile_manager->GetLoadedProfiles());
Elly Fong-Jones08757f1e2018-01-31 16:24:44861
862 std::vector<Profile*> added_profiles;
863 for (Profile* p : profiles) {
864 if (p->HasOffTheRecordProfile())
865 added_profiles.push_back(p->GetOffTheRecordProfile());
866 }
867 profiles.insert(profiles.end(), added_profiles.begin(), added_profiles.end());
868
[email protected]844a1002011-04-19 11:37:21869 for (size_t i = 0; i < profiles.size(); ++i) {
peterccb33e82017-05-02 19:00:44870 DownloadCoreService* download_core_service =
871 DownloadCoreServiceFactory::GetForBrowserContext(profiles[i]);
[email protected]9bb54ee2011-10-12 17:43:35872 DownloadManager* download_manager =
peterccb33e82017-05-02 19:00:44873 (download_core_service->HasCreatedDownloadManager()
874 ? BrowserContext::GetDownloadManager(profiles[i])
875 : NULL);
[email protected]422a7d12013-10-21 12:10:42876 if (download_manager &&
877 download_manager->NonMaliciousInProgressCount() > 0) {
878 int downloadCount = download_manager->NonMaliciousInProgressCount();
[email protected]73f5b362009-08-10 23:58:21879 if ([self userWillWaitForInProgressDownloads:downloadCount]) {
880 // Create a new browser window (if necessary) and navigate to the
881 // downloads page if the user chooses to wait.
scottmg34c5dd882016-02-03 05:21:54882 Browser* browser = chrome::FindBrowserWithProfile(profiles[i]);
[email protected]73f5b362009-08-10 23:58:21883 if (!browser) {
erg7b01d692017-02-22 21:57:35884 browser = new Browser(Browser::CreateParams(profiles[i], true));
[email protected]73f5b362009-08-10 23:58:21885 browser->window()->Show();
886 }
887 DCHECK(browser);
[email protected]5d9cace72012-06-21 16:07:12888 chrome::ShowDownloads(browser);
[email protected]73f5b362009-08-10 23:58:21889 return NO;
890 }
[email protected]09729a552009-08-03 23:21:41891
[email protected]73f5b362009-08-10 23:58:21892 // User wants to exit.
893 return YES;
[email protected]09729a552009-08-03 23:21:41894 }
[email protected]09729a552009-08-03 23:21:41895 }
896
[email protected]73f5b362009-08-10 23:58:21897 // No profiles or active downloads found, okay to exit.
[email protected]09729a552009-08-03 23:21:41898 return YES;
899}
900
[email protected]fbc947b2009-06-19 13:28:24901// Called to determine if we should enable the "restore tab" menu item.
902// Checks with the TabRestoreService to see if there's anything there to
903// restore and returns YES if so.
904- (BOOL)canRestoreTab {
blundell74001adc2015-09-18 11:04:25905 sessions::TabRestoreService* service =
[email protected]a585a8db2011-06-20 18:58:35906 TabRestoreServiceFactory::GetForProfile([self lastProfile]);
[email protected]fbc947b2009-06-19 13:28:24907 return service && !service->entries().empty();
908}
909
[email protected]1c62b2f2013-06-28 00:15:00910// Called from the AppControllerProfileObserver every time a profile is deleted.
911- (void)profileWasRemoved:(const base::FilePath&)profilePath {
[email protected]1c62b2f2013-06-28 00:15:00912 // If the lastProfile has been deleted, the profile manager has
913 // already loaded a new one, so the pointer needs to be updated;
914 // otherwise we will try to start up a browser window with a pointer
915 // to the old profile.
bauerb447f6432015-03-02 22:34:51916 // In a browser test, the application is not brought to the front, so
917 // |lastProfile_| might be null.
Robert Liao44d3ef12019-12-12 21:04:33918 if (!_lastProfile || profilePath == _lastProfile->GetPath()) {
mlerman8ae56aa2015-04-24 13:56:27919 // Force windowChangedToProfile: to set the lastProfile_ and also update the
920 // relevant menuBridge objects.
Robert Liao44d3ef12019-12-12 21:04:33921 _lastProfile = nullptr;
mlerman8ae56aa2015-04-24 13:56:27922 [self windowChangedToProfile:g_browser_process->profile_manager()->
923 GetLastUsedProfile()];
924 }
lgarron9e6dee22014-11-18 01:03:39925
Robert Liao44d3ef12019-12-12 21:04:33926 _profileBookmarkMenuBridgeMap.erase(profilePath);
[email protected]1c62b2f2013-06-28 00:15:00927}
928
[email protected]05454532013-01-22 21:09:08929// Returns true if there is a modal window (either window- or application-
[email protected]f6c15fa2010-06-04 20:08:21930// modal) blocking the active browser. Note that tab modal dialogs (HTTP auth
931// sheets) will not count as blocking the browser. But things like open/save
932// dialogs that are window modal will block the browser.
[email protected]05454532013-01-22 21:09:08933- (BOOL)keyWindowIsModal {
934 if ([NSApp modalWindow])
935 return YES;
936
[email protected]f2bc6e92012-10-14 02:08:38937 Browser* browser = chrome::GetLastActiveBrowser();
Christopher Cameron359c61e22018-10-18 08:06:20938 return browser && [[browser->window()->GetNativeWindow().GetNativeNSWindow()
939 attachedSheet] isKindOfClass:[NSWindow class]];
[email protected]6a281332009-10-13 17:41:06940}
941
[email protected]88d74942009-01-21 22:04:44942// Called to validate menu items when there are no key windows. All the
943// items we care about have been set with the |commandDispatch:| action and
944// a target of FirstResponder in IB. If it's not one of those, let it
945// continue up the responder chain to be handled elsewhere. We pull out the
946// tag as the cross-platform constant to differentiate and dispatch the
947// various commands.
948- (BOOL)validateUserInterfaceItem:(id<NSValidatedUserInterfaceItem>)item {
949 SEL action = [item action];
950 BOOL enable = NO;
[email protected]05454532013-01-22 21:09:08951 if (action == @selector(commandDispatch:) ||
952 action == @selector(commandFromDock:)) {
[email protected]88d74942009-01-21 22:04:44953 NSInteger tag = [item tag];
Robert Liao44d3ef12019-12-12 21:04:33954 if (_menuState && // NULL in tests.
955 _menuState->SupportsCommand(tag)) {
[email protected]fbc947b2009-06-19 13:28:24956 switch (tag) {
[email protected]419eb2a2009-11-11 16:26:26957 // The File Menu commands are not automatically disabled by Cocoa when a
958 // dialog sheet obscures the browser window, so we disable several of
959 // them here. We don't need to include IDC_CLOSE_WINDOW, because
960 // app_controller is only activated when there are no key windows (see
961 // function comment).
[email protected]fbc947b2009-06-19 13:28:24962 case IDC_RESTORE_TAB:
[email protected]05454532013-01-22 21:09:08963 enable = ![self keyWindowIsModal] && [self canRestoreTab];
[email protected]6a281332009-10-13 17:41:06964 break;
[email protected]f6c15fa2010-06-04 20:08:21965 // Browser-level items that open in new tabs should not open if there's
966 // a window- or app-modal dialog.
[email protected]6a281332009-10-13 17:41:06967 case IDC_OPEN_FILE:
[email protected]6a281332009-10-13 17:41:06968 case IDC_NEW_TAB:
[email protected]f6c15fa2010-06-04 20:08:21969 case IDC_SHOW_HISTORY:
970 case IDC_SHOW_BOOKMARK_MANAGER:
[email protected]05454532013-01-22 21:09:08971 enable = ![self keyWindowIsModal];
[email protected]f6c15fa2010-06-04 20:08:21972 break;
973 // Browser-level items that open in new windows.
[email protected]f6c15fa2010-06-04 20:08:21974 case IDC_TASK_MANAGER:
975 // Allow the user to open a new window if there's a window-modal
976 // dialog.
[email protected]05454532013-01-22 21:09:08977 enable = ![self keyWindowIsModal];
[email protected]fbc947b2009-06-19 13:28:24978 break;
979 default:
Robert Liao44d3ef12019-12-12 21:04:33980 enable = _menuState->IsCommandEnabled(tag) ?
[email protected]05454532013-01-22 21:09:08981 ![self keyWindowIsModal] : NO;
[email protected]fbc947b2009-06-19 13:28:24982 }
983 }
spqchan9d60fce2016-02-10 18:30:47984
985 // "Show as tab" should only appear when the current window is a popup.
986 // Since |validateUserInterfaceItem:| is called only when there are no
987 // key windows, we should just hide this.
988 // This is handled outside of the switch statement because we want to hide
989 // this regardless if the command is supported or not.
990 if (tag == IDC_SHOW_AS_TAB) {
991 NSMenuItem* menuItem = base::mac::ObjCCast<NSMenuItem>(item);
992 [menuItem setHidden:YES];
993 }
[email protected]3b6aa8b62009-09-15 21:36:11994 } else if (action == @selector(terminate:)) {
[email protected]88d74942009-01-21 22:04:44995 enable = YES;
[email protected]3111f08b2009-04-30 16:01:52996 } else if (action == @selector(showPreferences:)) {
997 enable = YES;
[email protected]bde3dda2009-05-20 22:13:07998 } else if (action == @selector(orderFrontStandardAboutPanel:)) {
999 enable = YES;
[email protected]93b59fc2010-12-21 20:00:471000 } else if (action == @selector(commandFromDock:)) {
[email protected]3d4bd23f2009-10-06 03:58:381001 enable = YES;
[email protected]a99fce0e2011-03-21 20:58:481002 } else if (action == @selector(toggleConfirmToQuit:)) {
1003 [self updateConfirmToQuitPrefMenuItem:static_cast<NSMenuItem*>(item)];
1004 enable = YES;
[email protected]3a90b452013-02-15 04:33:481005 } else if (action == @selector(executeApplication:)) {
1006 enable = YES;
[email protected]88d74942009-01-21 22:04:441007 }
1008 return enable;
1009}
1010
[email protected]88d74942009-01-21 22:04:441011- (void)commandDispatch:(id)sender {
[email protected]2a39d1a2014-04-10 05:46:241012 Profile* lastProfile = [self safeLastProfileForNewWindows];
[email protected]f0a51fb52009-03-05 12:46:381013
[email protected]947fc0d2010-01-14 22:33:051014 // Handle the case where we're dispatching a command from a sender that's in a
1015 // browser window. This means that the command came from a background window
1016 // and is getting here because the foreground window is not a browser window.
1017 if ([sender respondsToSelector:@selector(window)]) {
1018 id delegate = [[sender window] windowController];
andresantoso55fd2512015-04-01 21:07:041019 if ([delegate respondsToSelector:@selector(commandDispatch:)]) {
[email protected]947fc0d2010-01-14 22:33:051020 [delegate commandDispatch:sender];
1021 return;
1022 }
1023 }
1024
[email protected]8d4bbdc2012-09-20 21:36:491025 // Ignore commands during session restore's browser creation. It uses a
gab2998ee72017-05-05 16:23:501026 // nested run loop and commands dispatched during this operation cause
[email protected]8d4bbdc2012-09-20 21:36:491027 // havoc.
Owen Min8c5d5382018-08-02 17:52:431028 if (lastProfile && SessionRestore::IsRestoring(lastProfile) &&
gab7af9dc02017-05-05 13:38:541029 base::RunLoop::IsNestedOnCurrentThread()) {
[email protected]8d4bbdc2012-09-20 21:36:491030 return;
gab7af9dc02017-05-05 13:38:541031 }
[email protected]8d4bbdc2012-09-20 21:36:491032
Trent Apted45d9ae22017-11-09 23:12:321033 // If not between -applicationDidFinishLaunching: and
1034 // -applicationWillTerminate:, ignore. This can happen when events are sitting
1035 // in the event queue while the browser is shutting down.
Robert Liao44d3ef12019-12-12 21:04:331036 if (!_keep_alive)
Trent Apted45d9ae22017-11-09 23:12:321037 return;
1038
[email protected]88d74942009-01-21 22:04:441039 NSInteger tag = [sender tag];
[email protected]7108d912014-01-30 08:10:451040
1041 // If there are no browser windows, and we are trying to open a browser
Owen Min027059d2018-03-13 22:17:361042 // for a locked profile or the system profile or the guest profile but
1043 // guest mode is disabled, we have to show the User Manager instead as the
1044 // locked profile needs authentication and the system profile cannot have a
1045 // browser.
1046 const PrefService* prefService = g_browser_process->local_state();
1047 if (IsProfileSignedOut(lastProfile) || lastProfile->IsSystemProfile() ||
1048 (lastProfile->IsGuestSession() && prefService &&
1049 !prefService->GetBoolean(prefs::kBrowserGuestModeEnabled))) {
mlermane6e040a2014-10-31 00:53:211050 UserManager::Show(base::FilePath(),
mlermane29d0032014-09-24 19:31:261051 profiles::USER_MANAGER_SELECT_PROFILE_NO_ACTION);
[email protected]7108d912014-01-30 08:10:451052 return;
1053 }
1054
[email protected]88d74942009-01-21 22:04:441055 switch (tag) {
[email protected]fbc947b2009-06-19 13:28:241056 case IDC_NEW_TAB:
[email protected]d630d7d52010-02-21 00:55:111057 // Create a new tab in an existing browser window (which we activate) if
1058 // possible.
[email protected]a585a8db2011-06-20 18:58:351059 if (Browser* browser = ActivateBrowser(lastProfile)) {
[email protected]5d98294912012-06-27 22:57:401060 chrome::ExecuteCommand(browser, IDC_NEW_TAB);
[email protected]d630d7d52010-02-21 00:55:111061 break;
1062 }
Nico Weber331a98a82018-02-06 23:26:011063 FALLTHROUGH; // To create new window.
[email protected]88d74942009-01-21 22:04:441064 case IDC_NEW_WINDOW:
[email protected]a585a8db2011-06-20 18:58:351065 CreateBrowser(lastProfile);
[email protected]d630d7d52010-02-21 00:55:111066 break;
[email protected]91a5b3e2009-10-30 19:32:221067 case IDC_FOCUS_LOCATION:
[email protected]5d98294912012-06-27 22:57:401068 chrome::ExecuteCommand(ActivateOrCreateBrowser(lastProfile),
1069 IDC_FOCUS_LOCATION);
[email protected]88d74942009-01-21 22:04:441070 break;
[email protected]cddc5242009-12-07 18:54:331071 case IDC_FOCUS_SEARCH:
[email protected]5d98294912012-06-27 22:57:401072 chrome::ExecuteCommand(ActivateOrCreateBrowser(lastProfile),
1073 IDC_FOCUS_SEARCH);
[email protected]cddc5242009-12-07 18:54:331074 break;
[email protected]863ff662009-01-26 20:18:181075 case IDC_NEW_INCOGNITO_WINDOW:
[email protected]eddcf7502012-02-09 22:43:481076 CreateBrowser(lastProfile->GetOffTheRecordProfile());
[email protected]863ff662009-01-26 20:18:181077 break;
[email protected]fbc947b2009-06-19 13:28:241078 case IDC_RESTORE_TAB:
scottmgd161e6c2016-02-17 02:08:011079 chrome::OpenWindowWithRestoredTabs(lastProfile);
[email protected]fbc947b2009-06-19 13:28:241080 break;
[email protected]e19516d2009-04-28 17:15:191081 case IDC_OPEN_FILE:
[email protected]5d98294912012-06-27 22:57:401082 chrome::ExecuteCommand(CreateBrowser(lastProfile), IDC_OPEN_FILE);
[email protected]e19516d2009-04-28 17:15:191083 break;
[email protected]1fdff702009-10-22 00:36:181084 case IDC_CLEAR_BROWSING_DATA: {
[email protected]93e181762009-05-29 14:30:381085 // There may not be a browser open, so use the default profile.
[email protected]a585a8db2011-06-20 18:58:351086 if (Browser* browser = ActivateBrowser(lastProfile)) {
[email protected]5d9cace72012-06-21 16:07:121087 chrome::ShowClearBrowsingDataDialog(browser);
[email protected]c8de64a2011-01-25 17:10:231088 } else {
[email protected]1f0b50b2012-06-22 20:37:161089 chrome::OpenClearBrowsingDataDialogWindow(lastProfile);
[email protected]c8de64a2011-01-25 17:10:231090 }
[email protected]93e181762009-05-29 14:30:381091 break;
[email protected]1fdff702009-10-22 00:36:181092 }
[email protected]4aee5652009-11-19 18:59:211093 case IDC_IMPORT_SETTINGS: {
[email protected]a585a8db2011-06-20 18:58:351094 if (Browser* browser = ActivateBrowser(lastProfile)) {
[email protected]5d9cace72012-06-21 16:07:121095 chrome::ShowImportDialog(browser);
[email protected]c8de64a2011-01-25 17:10:231096 } else {
[email protected]1f0b50b2012-06-22 20:37:161097 chrome::OpenImportSettingsDialogWindow(lastProfile);
[email protected]c8de64a2011-01-25 17:10:231098 }
[email protected]4aee5652009-11-19 18:59:211099 break;
1100 }
[email protected]af7c25e2010-01-23 05:46:451101 case IDC_SHOW_BOOKMARK_MANAGER:
[email protected]a585a8db2011-06-20 18:58:351102 if (Browser* browser = ActivateBrowser(lastProfile)) {
[email protected]5d9cace72012-06-21 16:07:121103 chrome::ShowBookmarkManager(browser);
[email protected]90a8bf252010-03-12 00:09:471104 } else {
[email protected]47e532b2010-04-23 17:46:401105 // No browser window, so create one for the bookmark manager tab.
[email protected]1f0b50b2012-06-22 20:37:161106 chrome::OpenBookmarkManagerWindow(lastProfile);
[email protected]90a8bf252010-03-12 00:09:471107 }
[email protected]af7c25e2010-01-23 05:46:451108 break;
[email protected]91a5b3e2009-10-30 19:32:221109 case IDC_SHOW_HISTORY:
[email protected]a585a8db2011-06-20 18:58:351110 if (Browser* browser = ActivateBrowser(lastProfile))
[email protected]5d9cace72012-06-21 16:07:121111 chrome::ShowHistory(browser);
[email protected]d630d7d52010-02-21 00:55:111112 else
[email protected]1f0b50b2012-06-22 20:37:161113 chrome::OpenHistoryWindow(lastProfile);
[email protected]91a5b3e2009-10-30 19:32:221114 break;
1115 case IDC_SHOW_DOWNLOADS:
[email protected]a585a8db2011-06-20 18:58:351116 if (Browser* browser = ActivateBrowser(lastProfile))
[email protected]5d9cace72012-06-21 16:07:121117 chrome::ShowDownloads(browser);
[email protected]d630d7d52010-02-21 00:55:111118 else
[email protected]1f0b50b2012-06-22 20:37:161119 chrome::OpenDownloadsWindow(lastProfile);
[email protected]91a5b3e2009-10-30 19:32:221120 break;
[email protected]bb37e6ec2009-11-18 17:59:361121 case IDC_MANAGE_EXTENSIONS:
[email protected]a585a8db2011-06-20 18:58:351122 if (Browser* browser = ActivateBrowser(lastProfile))
[email protected]bc9833c32013-02-28 04:05:081123 chrome::ShowExtensions(browser, std::string());
[email protected]d630d7d52010-02-21 00:55:111124 else
[email protected]1f0b50b2012-06-22 20:37:161125 chrome::OpenExtensionsWindow(lastProfile);
[email protected]bb37e6ec2009-11-18 17:59:361126 break;
[email protected]2ca35612012-06-12 03:49:481127 case IDC_HELP_PAGE_VIA_MENU:
[email protected]a585a8db2011-06-20 18:58:351128 if (Browser* browser = ActivateBrowser(lastProfile))
[email protected]5d9cace72012-06-21 16:07:121129 chrome::ShowHelp(browser, chrome::HELP_SOURCE_MENU);
[email protected]d630d7d52010-02-21 00:55:111130 else
[email protected]1f0b50b2012-06-22 20:37:161131 chrome::OpenHelpWindow(lastProfile, chrome::HELP_SOURCE_MENU);
[email protected]1fdff702009-10-22 00:36:181132 break;
[email protected]ceaaf1812010-01-20 22:13:091133 case IDC_TASK_MANAGER:
mlermane6e040a2014-10-31 00:53:211134 chrome::OpenTaskManager(NULL);
[email protected]ceaaf1812010-01-20 22:13:091135 break;
[email protected]4a42d272010-06-18 01:29:421136 case IDC_OPTIONS:
1137 [self showPreferences:sender];
1138 break;
[email protected]d630d7d52010-02-21 00:55:111139 }
[email protected]88d74942009-01-21 22:04:441140}
1141
[email protected]93b59fc2010-12-21 20:00:471142// Run a (background) application in a new tab.
1143- (void)executeApplication:(id)sender {
1144 NSInteger tag = [sender tag];
[email protected]a585a8db2011-06-20 18:58:351145 Profile* profile = [self lastProfile];
[email protected]93b59fc2010-12-21 20:00:471146 DCHECK(profile);
1147 BackgroundApplicationListModel applications(profile);
1148 DCHECK(tag >= 0 &&
1149 tag < static_cast<int>(applications.size()));
[email protected]1c321ee2012-05-21 03:02:341150 const extensions::Extension* extension = applications.GetExtension(tag);
[email protected]78d78a62011-09-29 18:24:331151 BackgroundModeManager::LaunchBackgroundApplication(profile, extension);
[email protected]93b59fc2010-12-21 20:00:471152}
1153
[email protected]947fc0d2010-01-14 22:33:051154// Same as |-commandDispatch:|, but executes commands using a disposition
1155// determined by the key flags. This will get called in the case where the
1156// frontmost window is not a browser window, and the user has command-clicked
1157// a button in a background browser window whose action is
1158// |-commandDispatchUsingKeyModifiers:|
1159- (void)commandDispatchUsingKeyModifiers:(id)sender {
1160 DCHECK(sender);
1161 if ([sender respondsToSelector:@selector(window)]) {
1162 id delegate = [[sender window] windowController];
andresantoso55fd2512015-04-01 21:07:041163 if ([delegate respondsToSelector:
1164 @selector(commandDispatchUsingKeyModifiers:)]) {
[email protected]947fc0d2010-01-14 22:33:051165 [delegate commandDispatchUsingKeyModifiers:sender];
1166 }
1167 }
1168}
1169
[email protected]2e29e2232013-07-26 10:40:591170// NSApplication delegate method called when someone clicks on the dock icon.
1171// To match standard mac behavior, we should open a new window if there are no
1172// browser windows.
[email protected]ff81e0a2009-04-13 14:58:541173- (BOOL)applicationShouldHandleReopen:(NSApplication*)theApplication
[email protected]bf193bc52013-06-08 04:39:581174 hasVisibleWindows:(BOOL)hasVisibleWindows {
[email protected]70631402010-04-26 20:16:541175 // If the browser is currently trying to quit, don't do anything and return NO
1176 // to prevent AppKit from doing anything.
1177 // TODO(rohitrao): Remove this code when http://crbug.com/40861 is resolved.
1178 if (browser_shutdown::IsTryingToQuit())
1179 return NO;
1180
[email protected]2e29e2232013-07-26 10:40:591181 // Bring all browser windows to the front. Specifically, this brings them in
1182 // front of any app windows. FocusWindowSet will also unminimize the most
1183 // recently minimized window if no windows in the set are visible.
[email protected]a25920ee2013-09-05 19:38:491184 // If there are any, return here. Otherwise, the windows are panels or
1185 // notifications so we still need to open a new window.
[email protected]bf193bc52013-06-08 04:39:581186 if (hasVisibleWindows) {
Christopher Cameron359c61e22018-10-18 08:06:201187 std::set<gfx::NativeWindow> browserWindows;
scottmg8abbff832016-01-28 22:57:371188 for (auto* browser : *BrowserList::GetInstance()) {
mitchelljonese59a1612014-12-16 00:34:591189 // When focusing Chrome, don't focus any browser windows associated with
1190 // a currently running app shim, so ignore them.
Joel Hockey2687ab52019-08-14 23:59:461191 if (browser && browser->deprecated_is_app()) {
mitchelljonesf00422082015-01-05 23:38:031192 extensions::ExtensionRegistry* registry =
1193 extensions::ExtensionRegistry::Get(browser->profile());
1194 const extensions::Extension* extension = registry->GetExtensionById(
Alexey Baskakov2ce2ffc842018-08-06 02:57:151195 web_app::GetAppIdFromApplicationName(browser->app_name()),
mitchelljonesf00422082015-01-05 23:38:031196 extensions::ExtensionRegistry::ENABLED);
mitchelljonesb118a0c2015-01-07 02:51:341197 if (extension && extension->is_hosted_app())
mitchelljonese59a1612014-12-16 00:34:591198 continue;
mitchelljonese59a1612014-12-16 00:34:591199 }
[email protected]2e29e2232013-07-26 10:40:591200 browserWindows.insert(browser->window()->GetNativeWindow());
[email protected]0236be22012-01-11 01:05:351201 }
[email protected]a25920ee2013-09-05 19:38:491202 if (!browserWindows.empty()) {
[email protected]d4c811e72013-10-29 21:57:551203 NSWindow* keyWindow = [NSApp keyWindow];
1204 if (keyWindow && ![keyWindow isOnActiveSpace]) {
1205 // The key window is not on the active space. We must be mid-animation
1206 // for a space transition triggered by the dock. Delay the call to
1207 // |ui::FocusWindowSet| until the transition completes. Otherwise, the
1208 // wrong space's windows get raised, resulting in an off-screen key
1209 // window. It does not work to |ui::FocusWindowSet| twice, once here
1210 // and once in |activeSpaceDidChange:|, as that appears to break when
1211 // the omnibox is focused.
1212 //
1213 // This check relies on OS X setting the key window to a window on the
1214 // target space before calling this method.
1215 //
1216 // See http://crbug.com/309656.
Robert Liao44d3ef12019-12-12 21:04:331217 _reopenTime = base::TimeTicks::Now();
[email protected]d4c811e72013-10-29 21:57:551218 } else {
[email protected]6c4b79602014-05-28 16:47:211219 ui::FocusWindowSetOnCurrentSpace(browserWindows);
[email protected]d4c811e72013-10-29 21:57:551220 }
1221 // Return NO; we've done (or soon will do) the deminiaturize, so
1222 // AppKit shouldn't do anything.
[email protected]a25920ee2013-09-05 19:38:491223 return NO;
1224 }
[email protected]0236be22012-01-11 01:05:351225 }
[email protected]ff81e0a2009-04-13 14:58:541226
[email protected]0d1210d62010-09-07 17:31:381227 // If launched as a hidden login item (due to installation of a persistent app
[email protected]eddcf7502012-02-09 22:43:481228 // or by the user, for example in System Preferences->Accounts->Login Items),
[email protected]0d1210d62010-09-07 17:31:381229 // allow session to be restored first time the user clicks on a Dock icon.
1230 // Normally, it'd just open a new empty page.
1231 {
[email protected]eddcf7502012-02-09 22:43:481232 static BOOL doneOnce = NO;
Christopher Cameron4d503112019-08-23 19:10:281233 BOOL attemptRestore =
1234 apps::AppShimTerminationManager::Get()->ShouldRestoreSession() ||
[email protected]70593e52014-05-15 08:30:231235 (!doneOnce && base::mac::WasLaunchedAsHiddenLoginItem());
1236 doneOnce = YES;
1237 if (attemptRestore) {
1238 SessionService* sessionService =
1239 SessionServiceFactory::GetForProfileForSessionRestore(
1240 [self lastProfile]);
1241 if (sessionService &&
Findit046795722018-10-03 11:44:481242 sessionService->RestoreIfNecessary(std::vector<GURL>()))
[email protected]70593e52014-05-15 08:30:231243 return NO;
[email protected]eddcf7502012-02-09 22:43:481244 }
[email protected]0d1210d62010-09-07 17:31:381245 }
[email protected]fc44f242012-02-14 16:54:391246
[email protected]ff81e0a2009-04-13 14:58:541247 // Otherwise open a new window.
[email protected]7108d912014-01-30 08:10:451248 // If the last profile was locked, we have to open the User Manager, as the
mlerman7831f57d2015-05-25 11:40:151249 // profile requires authentication. Similarly, because guest mode and system
1250 // profile are implemented as forced incognito, we can't open a new guest
1251 // browser either, so we have to show the User Manager as well.
[email protected]7108d912014-01-30 08:10:451252 Profile* lastProfile = [self lastProfile];
mlerman7831f57d2015-05-25 11:40:151253 if (lastProfile->IsGuestSession() || IsProfileSignedOut(lastProfile) ||
1254 lastProfile->IsSystemProfile()) {
mlermane6e040a2014-10-31 00:53:211255 UserManager::Show(base::FilePath(),
mlermane29d0032014-09-24 19:31:261256 profiles::USER_MANAGER_SELECT_PROFILE_NO_ACTION);
1257 } else {
[email protected]7108d912014-01-30 08:10:451258 CreateBrowser(lastProfile);
mlermane29d0032014-09-24 19:31:261259 }
[email protected]ff81e0a2009-04-13 14:58:541260
1261 // We've handled the reopen event, so return NO to tell AppKit not
1262 // to do anything.
1263 return NO;
1264}
1265
[email protected]88d74942009-01-21 22:04:441266- (void)initMenuState {
Robert Liao44d3ef12019-12-12 21:04:331267 _menuState = std::make_unique<CommandUpdaterImpl>(nullptr);
1268 _menuState->UpdateCommandEnabled(IDC_NEW_TAB, true);
1269 _menuState->UpdateCommandEnabled(IDC_NEW_WINDOW, true);
1270 _menuState->UpdateCommandEnabled(IDC_NEW_INCOGNITO_WINDOW, true);
1271 _menuState->UpdateCommandEnabled(IDC_OPEN_FILE, true);
1272 _menuState->UpdateCommandEnabled(IDC_CLEAR_BROWSING_DATA, true);
1273 _menuState->UpdateCommandEnabled(IDC_RESTORE_TAB, false);
1274 _menuState->UpdateCommandEnabled(IDC_FOCUS_LOCATION, true);
1275 _menuState->UpdateCommandEnabled(IDC_FOCUS_SEARCH, true);
1276 _menuState->UpdateCommandEnabled(IDC_SHOW_BOOKMARK_MANAGER, true);
1277 _menuState->UpdateCommandEnabled(IDC_SHOW_HISTORY, true);
1278 _menuState->UpdateCommandEnabled(IDC_SHOW_DOWNLOADS, true);
1279 _menuState->UpdateCommandEnabled(IDC_MANAGE_EXTENSIONS, true);
1280 _menuState->UpdateCommandEnabled(IDC_HELP_PAGE_VIA_MENU, true);
1281 _menuState->UpdateCommandEnabled(IDC_IMPORT_SETTINGS, true);
Nico Webereaa08412019-08-14 01:24:371282#if BUILDFLAG(GOOGLE_CHROME_BRANDING)
Robert Liao44d3ef12019-12-12 21:04:331283 _menuState->UpdateCommandEnabled(IDC_FEEDBACK, true);
[email protected]236ad3022013-09-04 03:27:431284#endif
Robert Liao44d3ef12019-12-12 21:04:331285 _menuState->UpdateCommandEnabled(IDC_TASK_MANAGER, true);
[email protected]88d74942009-01-21 22:04:441286}
[email protected]1bcdb532009-01-16 17:47:571287
[email protected]961a6932011-07-19 19:52:461288// Conditionally adds the Profile menu to the main menu bar.
1289- (void)initProfileMenu {
[email protected]961a6932011-07-19 19:52:461290 NSMenu* mainMenu = [NSApp mainMenu];
1291 NSMenuItem* profileMenu = [mainMenu itemWithTag:IDC_PROFILE_MAIN_MENU];
[email protected]e354ca42011-08-03 20:31:401292
[email protected]de71ae992013-07-18 03:30:381293 if (!profiles::IsMultipleProfilesEnabled()) {
[email protected]e354ca42011-08-03 20:31:401294 [mainMenu removeItem:profileMenu];
1295 return;
1296 }
1297
[email protected]6b2d30bc2011-10-11 18:16:391298 // The controller will unhide the menu if necessary.
1299 [profileMenu setHidden:YES];
[email protected]961a6932011-07-19 19:52:461300
Robert Liao44d3ef12019-12-12 21:04:331301 _profileMenuController.reset(
[email protected]6b2d30bc2011-10-11 18:16:391302 [[ProfileMenuController alloc] initWithMainMenuItem:profileMenu]);
[email protected]961a6932011-07-19 19:52:461303}
1304
Leonard Grey2bdd08f2017-10-03 18:40:521305- (void)initShareMenu {
Robert Liao44d3ef12019-12-12 21:04:331306 _shareMenuController.reset([[ShareMenuController alloc] init]);
Leonard Grey2bdd08f2017-10-03 18:40:521307 NSMenu* mainMenu = [NSApp mainMenu];
1308 NSMenu* fileMenu = [[mainMenu itemWithTag:IDC_FILE_MENU] submenu];
1309 NSString* shareMenuTitle = l10n_util::GetNSString(IDS_SHARE_MAC);
Leonard Grey34b5e622018-07-30 19:05:551310 NSMenuItem* shareMenuItem = [fileMenu itemWithTitle:shareMenuTitle];
Leonard Grey2bdd08f2017-10-03 18:40:521311 base::scoped_nsobject<NSMenu> shareSubmenu(
1312 [[NSMenu alloc] initWithTitle:shareMenuTitle]);
Robert Liao44d3ef12019-12-12 21:04:331313 [shareSubmenu setDelegate:_shareMenuController];
Leonard Grey2bdd08f2017-10-03 18:40:521314 [shareMenuItem setSubmenu:shareSubmenu];
Leonard Grey2bdd08f2017-10-03 18:40:521315}
1316
[email protected]a99fce0e2011-03-21 20:58:481317// The Confirm to Quit preference is atypical in that the preference lives in
1318// the app menu right above the Quit menu item. This method will refresh the
1319// display of that item depending on the preference state.
1320- (void)updateConfirmToQuitPrefMenuItem:(NSMenuItem*)item {
1321 // Format the string so that the correct key equivalent is displayed.
1322 NSString* acceleratorString = [ConfirmQuitPanelController keyCommandString];
1323 NSString* title = l10n_util::GetNSStringF(IDS_CONFIRM_TO_QUIT_OPTION,
1324 base::SysNSStringToUTF16(acceleratorString));
1325 [item setTitle:title];
1326
[email protected]b6366ffa2012-02-29 17:12:261327 const PrefService* prefService = g_browser_process->local_state();
[email protected]a99fce0e2011-03-21 20:58:481328 bool enabled = prefService->GetBoolean(prefs::kConfirmToQuitEnabled);
1329 [item setState:enabled ? NSOnState : NSOffState];
1330}
1331
[email protected]eef99c22010-08-17 05:55:161332- (void)registerServicesMenuTypesTo:(NSApplication*)app {
[email protected]09ea24c2010-08-23 19:05:411333 // Note that RenderWidgetHostViewCocoa implements NSServicesRequests which
1334 // handles requests from services.
Avi Drissmana0820222018-09-01 18:02:281335 NSArray* types = @[ base::mac::CFToNSCast(kUTTypeUTF8PlainText) ];
[email protected]09ea24c2010-08-23 19:05:411336 [app registerServicesMenuSendTypes:types returnTypes:types];
[email protected]eef99c22010-08-17 05:55:161337}
1338
Owen Min8c5d5382018-08-02 17:52:431339// Return null if Chrome is not ready or there is no ProfileManager.
[email protected]a585a8db2011-06-20 18:58:351340- (Profile*)lastProfile {
andresantoso55fd2512015-04-01 21:07:041341 // Return the profile of the last-used Browser, if available.
Robert Liao44d3ef12019-12-12 21:04:331342 if (_lastProfile)
1343 return _lastProfile;
[email protected]7fd5a462011-06-19 16:57:231344
Owen Min8c5d5382018-08-02 17:52:431345 if (![self isProfileReady])
1346 return nullptr;
1347
[email protected]fbb04552014-01-16 15:47:381348 // On first launch, use the logic that ChromeBrowserMain uses to determine
1349 // the initial profile.
1350 ProfileManager* profile_manager = g_browser_process->profile_manager();
1351 if (!profile_manager)
Owen Min8c5d5382018-08-02 17:52:431352 return nullptr;
[email protected]3f34599d2009-03-25 22:11:431353
avi3ef9ec9e2014-12-22 22:50:171354 return profile_manager->GetProfile(
1355 GetStartupProfilePath(profile_manager->user_data_dir(),
1356 *base::CommandLine::ForCurrentProcess()));
[email protected]3f34599d2009-03-25 22:11:431357}
1358
[email protected]2a39d1a2014-04-10 05:46:241359- (Profile*)safeLastProfileForNewWindows {
1360 Profile* profile = [self lastProfile];
1361
Owen Min8c5d5382018-08-02 17:52:431362 if (!profile)
1363 return nullptr;
1364
[email protected]2a39d1a2014-04-10 05:46:241365 // Guest sessions must always be OffTheRecord. Use that when opening windows.
1366 if (profile->IsGuestSession())
1367 return profile->GetOffTheRecordProfile();
1368
1369 return profile;
1370}
1371
mlermane6e040a2014-10-31 00:53:211372// Returns true if a browser window may be opened for the last active profile.
1373- (bool)canOpenNewBrowser {
1374 Profile* profile = [self safeLastProfileForNewWindows];
1375
1376 const PrefService* prefs = g_browser_process->local_state();
1377 return !profile->IsGuestSession() ||
1378 prefs->GetBoolean(prefs::kBrowserGuestModeEnabled);
1379}
1380
[email protected]57750f822009-04-21 21:43:091381// Various methods to open URLs that we get in a native fashion. We use
[email protected]fe7c4872012-05-10 20:06:031382// StartupBrowserCreator here because on the other platforms, URLs to open come
1383// through the ProcessSingleton, and it calls StartupBrowserCreator. It's best
1384// to bottleneck the openings through that for uniform handling.
[email protected]d630d7d52010-02-21 00:55:111385- (void)openUrls:(const std::vector<GURL>&)urls {
Robert Liao44d3ef12019-12-12 21:04:331386 if (!_startupComplete) {
1387 _startupUrls.insert(_startupUrls.end(), urls.begin(), urls.end());
[email protected]136140c2009-05-19 13:58:251388 return;
1389 }
Christian Dullweber820ee5f2018-04-26 13:24:311390 // Pick the last used browser from a regular profile to open the urls.
1391 Profile* profile =
1392 g_browser_process->profile_manager()->GetLastUsedProfileAllowedByPolicy();
1393 Browser* browser = chrome::FindLastActiveWithProfile(profile);
Scott Violet1f106b582017-07-12 15:49:581394 // if no browser window exists then create one with no tabs to be filled in
1395 if (!browser) {
1396 browser = new Browser(
1397 Browser::CreateParams([self safeLastProfileForNewWindows], true));
1398 browser->window()->Show();
eugenebng7afbc36e2017-06-15 13:38:471399 }
Scott Violet1f106b582017-07-12 15:49:581400
1401 base::CommandLine dummy(base::CommandLine::NO_PROGRAM);
1402 chrome::startup::IsFirstRun first_run =
1403 first_run::IsChromeFirstRun() ? chrome::startup::IS_FIRST_RUN
1404 : chrome::startup::IS_NOT_FIRST_RUN;
1405 StartupBrowserCreatorImpl launch(base::FilePath(), dummy, first_run);
1406 launch.OpenURLsInBrowser(browser, false, urls);
[email protected]57750f822009-04-21 21:43:091407}
1408
[email protected]57750f822009-04-21 21:43:091409- (void)getUrl:(NSAppleEventDescriptor*)event
1410 withReply:(NSAppleEventDescriptor*)reply {
1411 NSString* urlStr = [[event paramDescriptorForKeyword:keyDirectObject]
1412 stringValue];
1413
1414 GURL gurl(base::SysNSStringToUTF8(urlStr));
1415 std::vector<GURL> gurlVector;
1416 gurlVector.push_back(gurl);
1417
erikchen19ee3922014-10-31 19:14:221418 [self openUrlsReplacingNTP:gurlVector];
[email protected]57750f822009-04-21 21:43:091419}
1420
[email protected]caadfca2010-01-22 21:51:211421- (void)application:(NSApplication*)sender
1422 openFiles:(NSArray*)filenames {
[email protected]57750f822009-04-21 21:43:091423 std::vector<GURL> gurlVector;
[email protected]caadfca2010-01-22 21:51:211424 for (NSString* file in filenames) {
[email protected]650b2d52013-02-10 03:41:451425 GURL gurl =
[email protected]77d83542013-12-13 08:54:291426 net::FilePathToFileURL(base::FilePath([file fileSystemRepresentation]));
[email protected]57750f822009-04-21 21:43:091427 gurlVector.push_back(gurl);
1428 }
Avi Drissmand4887fb22020-04-14 19:16:251429
1430 if (!gurlVector.empty())
1431 [self openUrlsReplacingNTP:gurlVector];
1432 else
1433 NOTREACHED() << "Nothing to open!";
1434
1435 [sender replyToOpenOrPrint:NSApplicationDelegateReplySuccess];
1436}
1437
1438// TODO(avi): When Chromium requires 10.13 as a minimum, remove the
1439// -[NSApplication application:openFiles:] override and the
1440// kInternetEventClass/kAEGetURL Apple Event registration in -mainMenuCreated.
1441- (void)application:(NSApplication*)sender openURLs:(NSArray<NSURL*>*)urls {
1442 std::vector<GURL> gurlVector;
1443 for (NSURL* url in urls)
1444 gurlVector.push_back(net::GURLWithNSURL(url));
1445
[email protected]caadfca2010-01-22 21:51:211446 if (!gurlVector.empty())
erikchen19ee3922014-10-31 19:14:221447 [self openUrlsReplacingNTP:gurlVector];
[email protected]caadfca2010-01-22 21:51:211448 else
1449 NOTREACHED() << "Nothing to open!";
[email protected]57750f822009-04-21 21:43:091450
[email protected]caadfca2010-01-22 21:51:211451 [sender replyToOpenOrPrint:NSApplicationDelegateReplySuccess];
[email protected]57750f822009-04-21 21:43:091452}
[email protected]3f34599d2009-03-25 22:11:431453
[email protected]3111f08b2009-04-30 16:01:521454// Show the preferences window, or bring it to the front if it's already
1455// visible.
1456- (IBAction)showPreferences:(id)sender {
[email protected]a585a8db2011-06-20 18:58:351457 if (Browser* browser = ActivateBrowser([self lastProfile])) {
[email protected]445177652011-03-09 02:04:051458 // Show options tab in the active browser window.
[email protected]5d9cace72012-06-21 16:07:121459 chrome::ShowSettings(browser);
mlermane6e040a2014-10-31 00:53:211460 } else if ([self canOpenNewBrowser]) {
[email protected]445177652011-03-09 02:04:051461 // No browser window, so create one for the options tab.
[email protected]2a39d1a2014-04-10 05:46:241462 chrome::OpenOptionsWindow([self safeLastProfileForNewWindows]);
mlermane6e040a2014-10-31 00:53:211463 } else {
1464 // No way to create a browser, default to the User Manager.
1465 UserManager::Show(base::FilePath(),
mlermane6e040a2014-10-31 00:53:211466 profiles::USER_MANAGER_SELECT_PROFILE_CHROME_SETTINGS);
[email protected]4a42d272010-06-18 01:29:421467 }
[email protected]d989f07c2009-11-14 00:35:461468}
1469
[email protected]bde3dda2009-05-20 22:13:071470- (IBAction)orderFrontStandardAboutPanel:(id)sender {
[email protected]a048ad22012-03-23 04:26:561471 if (Browser* browser = ActivateBrowser([self lastProfile])) {
[email protected]5d9cace72012-06-21 16:07:121472 chrome::ShowAboutChrome(browser);
mlermane6e040a2014-10-31 00:53:211473 } else if ([self canOpenNewBrowser]) {
1474 // No browser window, so create one for the options tab.
[email protected]2a39d1a2014-04-10 05:46:241475 chrome::OpenAboutWindow([self safeLastProfileForNewWindows]);
mlermane6e040a2014-10-31 00:53:211476 } else {
1477 // No way to create a browser, default to the User Manager.
1478 UserManager::Show(base::FilePath(),
mlermane6e040a2014-10-31 00:53:211479 profiles::USER_MANAGER_SELECT_PROFILE_ABOUT_CHROME);
[email protected]bde3dda2009-05-20 22:13:071480 }
[email protected]bde3dda2009-05-20 22:13:071481}
1482
[email protected]a99fce0e2011-03-21 20:58:481483- (IBAction)toggleConfirmToQuit:(id)sender {
[email protected]b6366ffa2012-02-29 17:12:261484 PrefService* prefService = g_browser_process->local_state();
[email protected]a99fce0e2011-03-21 20:58:481485 bool enabled = prefService->GetBoolean(prefs::kConfirmToQuitEnabled);
1486 prefService->SetBoolean(prefs::kConfirmToQuitEnabled, !enabled);
1487}
1488
[email protected]3d4bd23f2009-10-06 03:58:381489// Explicitly bring to the foreground when creating new windows from the dock.
[email protected]93b59fc2010-12-21 20:00:471490- (void)commandFromDock:(id)sender {
[email protected]3d4bd23f2009-10-06 03:58:381491 [NSApp activateIgnoringOtherApps:YES];
1492 [self commandDispatch:sender];
1493}
1494
[email protected]227fcee2010-06-11 19:19:371495- (NSMenu*)applicationDockMenu:(NSApplication*)sender {
[email protected]018a3962009-09-17 22:23:441496 NSMenu* dockMenu = [[[NSMenu alloc] initWithTitle: @""] autorelease];
[email protected]a585a8db2011-06-20 18:58:351497 Profile* profile = [self lastProfile];
[email protected]93b59fc2010-12-21 20:00:471498
Robert Liao44d3ef12019-12-12 21:04:331499 BOOL profilesAdded = [_profileMenuController insertItemsIntoMenu:dockMenu
[email protected]646fadbf2012-09-08 01:49:011500 atOffset:0
1501 fromDock:YES];
1502 if (profilesAdded)
1503 [dockMenu addItem:[NSMenuItem separatorItem]];
[email protected]3d272f52012-08-23 19:00:331504
[email protected]646fadbf2012-09-08 01:49:011505 NSString* titleStr = l10n_util::GetNSStringWithFixup(IDS_NEW_WINDOW_MAC);
[email protected]a8522032013-06-24 22:51:461506 base::scoped_nsobject<NSMenuItem> item(
[email protected]8c6b029d2011-01-21 22:39:511507 [[NSMenuItem alloc] initWithTitle:titleStr
1508 action:@selector(commandFromDock:)
1509 keyEquivalent:@""]);
[email protected]288bfcd32009-09-14 18:14:461510 [item setTarget:self];
[email protected]646fadbf2012-09-08 01:49:011511 [item setTag:IDC_NEW_WINDOW];
[email protected]05454532013-01-22 21:09:081512 [item setEnabled:[self validateUserInterfaceItem:item]];
[email protected]646fadbf2012-09-08 01:49:011513 [dockMenu addItem:item];
1514
[email protected]8d10a412013-08-09 19:18:001515 // |profile| can be NULL during unit tests.
treib6f9598012014-11-11 12:21:241516 if (!profile ||
1517 IncognitoModePrefs::GetAvailability(profile->GetPrefs()) !=
1518 IncognitoModePrefs::DISABLED) {
[email protected]8d10a412013-08-09 19:18:001519 titleStr = l10n_util::GetNSStringWithFixup(IDS_NEW_INCOGNITO_WINDOW_MAC);
1520 item.reset(
1521 [[NSMenuItem alloc] initWithTitle:titleStr
1522 action:@selector(commandFromDock:)
1523 keyEquivalent:@""]);
1524 [item setTarget:self];
1525 [item setTag:IDC_NEW_INCOGNITO_WINDOW];
1526 [item setEnabled:[self validateUserInterfaceItem:item]];
1527 [dockMenu addItem:item];
1528 }
[email protected]288bfcd32009-09-14 18:14:461529
[email protected]8c6b029d2011-01-21 22:39:511530 // TODO(rickcam): Mock out BackgroundApplicationListModel, then add unit
1531 // tests which use the mock in place of the profile-initialized model.
1532
1533 // Avoid breaking unit tests which have no profile.
1534 if (profile) {
1535 BackgroundApplicationListModel applications(profile);
1536 if (applications.size()) {
1537 int position = 0;
1538 NSString* menuStr =
1539 l10n_util::GetNSStringWithFixup(IDS_BACKGROUND_APPS_MAC);
[email protected]a8522032013-06-24 22:51:461540 base::scoped_nsobject<NSMenu> appMenu(
1541 [[NSMenu alloc] initWithTitle:menuStr]);
[email protected]1c321ee2012-05-21 03:02:341542 for (extensions::ExtensionList::const_iterator cursor =
[email protected]3a90b452013-02-15 04:33:481543 applications.begin();
[email protected]8c6b029d2011-01-21 22:39:511544 cursor != applications.end();
1545 ++cursor, ++position) {
[email protected]f15d9ebc2013-07-02 00:14:501546 DCHECK_EQ(applications.GetPosition(cursor->get()), position);
[email protected]8c6b029d2011-01-21 22:39:511547 NSString* itemStr =
[email protected]670d3232013-12-24 17:58:581548 base::SysUTF16ToNSString(base::UTF8ToUTF16((*cursor)->name()));
[email protected]a8522032013-06-24 22:51:461549 base::scoped_nsobject<NSMenuItem> appItem(
1550 [[NSMenuItem alloc] initWithTitle:itemStr
1551 action:@selector(executeApplication:)
1552 keyEquivalent:@""]);
[email protected]8c6b029d2011-01-21 22:39:511553 [appItem setTarget:self];
1554 [appItem setTag:position];
1555 [appMenu addItem:appItem];
1556 }
[email protected]8c6b029d2011-01-21 22:39:511557 }
1558 }
1559
[email protected]018a3962009-09-17 22:23:441560 return dockMenu;
[email protected]288bfcd32009-09-14 18:14:461561}
1562
[email protected]d630d7d52010-02-21 00:55:111563- (const std::vector<GURL>&)startupUrls {
Robert Liao44d3ef12019-12-12 21:04:331564 return _startupUrls;
[email protected]caadfca2010-01-22 21:51:211565}
1566
[email protected]7e4c69d02011-05-11 22:16:471567- (BookmarkMenuBridge*)bookmarkMenuBridge {
Robert Liao44d3ef12019-12-12 21:04:331568 return _bookmarkMenuBridge;
[email protected]7e4c69d02011-05-11 22:16:471569}
1570
mlerman8ae56aa2015-04-24 13:56:271571- (HistoryMenuBridge*)historyMenuBridge {
Robert Liao44d3ef12019-12-12 21:04:331572 return _historyMenuBridge.get();
mlerman8ae56aa2015-04-24 13:56:271573}
1574
Elly Fong-Jones0d121842019-08-12 22:03:401575- (TabMenuBridge*)tabMenuBridge {
Robert Liao44d3ef12019-12-12 21:04:331576 return _tabMenuBridge.get();
Elly Fong-Jones0d121842019-08-12 22:03:401577}
1578
[email protected]e7dc3992013-11-06 02:16:421579- (void)initAppShimMenuController {
Robert Liao44d3ef12019-12-12 21:04:331580 if (!_appShimMenuController)
1581 _appShimMenuController.reset([[AppShimMenuController alloc] init]);
[email protected]e7dc3992013-11-06 02:16:421582}
1583
nomsdd5b6a612014-09-23 15:20:161584- (void)windowChangedToProfile:(Profile*)profile {
Robert Liao44d3ef12019-12-12 21:04:331585 if (_lastProfile == profile)
nomsdd5b6a612014-09-23 15:20:161586 return;
1587
lgarron9e6dee22014-11-18 01:03:391588 // Before tearing down the menu controller bridges, return the history menu to
1589 // its initial state.
Robert Liao44d3ef12019-12-12 21:04:331590 if (_historyMenuBridge)
1591 _historyMenuBridge->ResetMenu();
nomsdd5b6a612014-09-23 15:20:161592
dominicknb80838f2015-09-08 23:00:131593 // Rebuild the menus with the new profile. The bookmarks submenu is cached to
1594 // avoid slowdowns when switching between profiles with large numbers of
1595 // bookmarks. Before caching, store whether it is hidden, make the menu item
1596 // visible, and restore its original hidden state after resetting the submenu.
1597 // This works around an apparent AppKit bug where setting a *different* NSMenu
1598 // submenu on a *hidden* menu item forces the item to become visible.
1599 // See https://crbug.com/497813 for more details.
1600 NSMenuItem* bookmarkItem = [[NSApp mainMenu] itemWithTag:IDC_BOOKMARKS_MENU];
1601 BOOL hidden = [bookmarkItem isHidden];
1602 [bookmarkItem setHidden:NO];
Robert Liao44d3ef12019-12-12 21:04:331603 _lastProfile = profile;
nomsdd5b6a612014-09-23 15:20:161604
Robert Liao44d3ef12019-12-12 21:04:331605 auto& entry = _profileBookmarkMenuBridgeMap[profile->GetPath()];
Trent Aptedcf95689a2017-11-22 00:01:101606 if (!entry) {
1607 // This creates a deep copy, but only the first 3 items in the root menu
1608 // are really wanted. This can probably be optimized, but lazy-loading of
1609 // the menu should reduce the impact in most flows.
dominicknb80838f2015-09-08 23:00:131610 base::scoped_nsobject<NSMenu> submenu([[bookmarkItem submenu] copy]);
Trent Aptedcf95689a2017-11-22 00:01:101611 [submenu setDelegate:nil]; // The delegate is also copied. Remove it.
1612
1613 entry = std::make_unique<BookmarkMenuBridge>(profile, submenu);
1614
1615 // Clear bookmarks from the old profile.
1616 entry->ClearBookmarkMenu();
lgarron9e6dee22014-11-18 01:03:391617 }
Robert Liao44d3ef12019-12-12 21:04:331618 _bookmarkMenuBridge = entry.get();
lgarron9e6dee22014-11-18 01:03:391619
nomsdd5b6a612014-09-23 15:20:161620 // No need to |BuildMenu| here. It is done lazily upon menu access.
Robert Liao44d3ef12019-12-12 21:04:331621 [bookmarkItem setSubmenu:_bookmarkMenuBridge->BookmarkMenu()];
dominicknb80838f2015-09-08 23:00:131622 [bookmarkItem setHidden:hidden];
nomsdd5b6a612014-09-23 15:20:161623
Robert Liao44d3ef12019-12-12 21:04:331624 _historyMenuBridge.reset(new HistoryMenuBridge(_lastProfile));
1625 _historyMenuBridge->BuildMenu();
nomsdd5b6a612014-09-23 15:20:161626
1627 chrome::BrowserCommandController::
1628 UpdateSharedCommandsForIncognitoAvailability(
Robert Liao44d3ef12019-12-12 21:04:331629 _menuState.get(), _lastProfile);
1630 _profilePrefRegistrar.reset(new PrefChangeRegistrar());
1631 _profilePrefRegistrar->Init(_lastProfile->GetPrefs());
1632 _profilePrefRegistrar->Add(
nomsdd5b6a612014-09-23 15:20:161633 prefs::kIncognitoModeAvailability,
Alexander Cooperbc87af32020-07-15 15:59:451634 base::BindRepeating(&chrome::BrowserCommandController::
1635 UpdateSharedCommandsForIncognitoAvailability,
1636 _menuState.get(), _lastProfile));
nomsdd5b6a612014-09-23 15:20:161637}
1638
erikchen22be2092018-06-12 15:54:001639- (void)updateMenuItemKeyEquivalents {
1640 BOOL enableCloseTabShortcut = NO;
1641 id target = [NSApp targetForAction:@selector(performClose:)];
1642
1643 // |target| is an instance of NSPopover or NSWindow.
1644 // If a popover (likely the dictionary lookup popover), we want Cmd-W to
1645 // close the popover so map it to "Close Window".
1646 // Otherwise, map Cmd-W to "Close Tab" if it's a browser window.
1647 if ([target isKindOfClass:[NSWindow class]]) {
1648 NSWindow* window = target;
1649 NSWindow* mainWindow = [NSApp mainWindow];
1650 if (!window || ([window parentWindow] == mainWindow)) {
1651 // If the target window is a child of the main window (e.g. a bubble), the
1652 // main window should be the one that handles the close menu item action.
1653 window = mainWindow;
1654 }
1655 Browser* browser = chrome::FindBrowserWithWindow(window);
Joel Hockey2687ab52019-08-14 23:59:461656 enableCloseTabShortcut = browser && browser->is_type_normal();
erikchen22be2092018-06-12 15:54:001657 }
1658
1659 [self adjustCloseWindowMenuItemKeyEquivalent:enableCloseTabShortcut];
1660 [self adjustCloseTabMenuItemKeyEquivalent:enableCloseTabShortcut];
1661}
1662
[email protected]92747fe42014-08-18 23:25:261663- (BOOL)application:(NSApplication*)application
Elly Fong-Jones397009ea2019-01-25 17:41:581664 willContinueUserActivityWithType:(NSString*)userActivityType {
[email protected]92747fe42014-08-18 23:25:261665 return [userActivityType isEqualToString:NSUserActivityTypeBrowsingWeb];
1666}
1667
1668- (BOOL)application:(NSApplication*)application
1669 continueUserActivity:(NSUserActivity*)userActivity
Leonard Greybc00f8862018-09-26 17:24:221670 restorationHandler:
1671 (void (^)(NSArray<id<NSUserActivityRestoring>>*))restorationHandler
Elly Fong-Jones397009ea2019-01-25 17:41:581672{
[email protected]92747fe42014-08-18 23:25:261673 if (![userActivity.activityType
1674 isEqualToString:NSUserActivityTypeBrowsingWeb]) {
1675 return NO;
1676 }
1677
erikchen96e537d52014-10-28 23:43:171678 NSString* originString = base::mac::ObjCCast<NSString>(
1679 [userActivity.userInfo objectForKey:handoff::kOriginKey]);
1680 handoff::Origin origin = handoff::OriginFromString(originString);
1681 UMA_HISTOGRAM_ENUMERATION(
1682 "OSX.Handoff.Origin", origin, handoff::ORIGIN_COUNT);
1683
jiangjc0c568c2014-12-04 19:02:121684 NSURL* url = userActivity.webpageURL;
[email protected]92747fe42014-08-18 23:25:261685 if (!url)
1686 return NO;
1687
1688 GURL gurl(base::SysNSStringToUTF8([url absoluteString]));
1689 std::vector<GURL> gurlVector;
1690 gurlVector.push_back(gurl);
1691
erikchen19ee3922014-10-31 19:14:221692 [self openUrlsReplacingNTP:gurlVector];
[email protected]92747fe42014-08-18 23:25:261693 return YES;
1694}
1695
1696- (void)application:(NSApplication*)application
1697 didFailToContinueUserActivityWithType:(NSString*)userActivityType
1698 error:(NSError*)error {
1699}
1700
erikchen600f7962014-12-12 00:17:381701#pragma mark - Handoff Manager
1702
erikchen600f7962014-12-12 00:17:381703- (void)passURLToHandoffManager:(const GURL&)handoffURL {
Robert Liao44d3ef12019-12-12 21:04:331704 [_handoffManager updateActiveURL:handoffURL];
erikchen600f7962014-12-12 00:17:381705}
1706
1707- (void)updateHandoffManager:(content::WebContents*)webContents {
Robert Liao44d3ef12019-12-12 21:04:331708 if (!_handoffManager)
1709 _handoffManager.reset([[HandoffManager alloc] init]);
erikchen600f7962014-12-12 00:17:381710
1711 GURL handoffURL = [self handoffURLFromWebContents:webContents];
1712 [self passURLToHandoffManager:handoffURL];
1713}
1714
1715- (GURL)handoffURLFromWebContents:(content::WebContents*)webContents {
1716 if (!webContents)
1717 return GURL();
1718
1719 Profile* profile =
1720 Profile::FromBrowserContext(webContents->GetBrowserContext());
1721 if (!profile)
1722 return GURL();
1723
1724 // Handoff is not allowed from an incognito profile. To err on the safe side,
1725 // also disallow Handoff from a guest profile.
Ramin Halavati7784def0d2019-05-20 09:20:461726 if (!profile->IsRegularProfile())
erikchen600f7962014-12-12 00:17:381727 return GURL();
1728
1729 if (!webContents)
1730 return GURL();
1731
1732 return webContents->GetVisibleURL();
1733}
1734
Owen Min8c5d5382018-08-02 17:52:431735- (BOOL)isProfileReady {
1736 return !g_browser_process->browser_policy_connector()
Owen Mina12ff6d2019-10-17 16:55:591737 ->chrome_browser_cloud_management_controller()
Owen Min8c5d5382018-08-02 17:52:431738 ->IsEnterpriseStartupDialogShowing();
1739}
1740
erikchen600f7962014-12-12 00:17:381741#pragma mark - HandoffActiveURLObserverBridgeDelegate
1742
1743- (void)handoffActiveURLChanged:(content::WebContents*)webContents {
1744 [self updateHandoffManager:webContents];
1745}
1746
[email protected]d630d7d52010-02-21 00:55:111747@end // @implementation AppController
[email protected]60ad3e22009-09-18 21:07:191748
1749//---------------------------------------------------------------------------
1750
Elly Fong-Jones68b85b82018-04-21 00:36:541751namespace {
1752
1753void UpdateProfileInUse(Profile* profile, Profile::CreateStatus status) {
1754 if (status == Profile::CREATE_STATUS_INITIALIZED) {
1755 AppController* controller =
1756 base::mac::ObjCCastStrict<AppController>([NSApp delegate]);
1757 [controller windowChangedToProfile:profile];
1758 }
1759}
1760
1761} // namespace
1762
[email protected]216e9042009-10-29 17:05:231763namespace app_controller_mac {
1764
1765bool IsOpeningNewWindow() {
1766 return g_is_opening_new_window;
1767}
1768
Elly Fong-Jones68b85b82018-04-21 00:36:541769void CreateGuestProfileIfNeeded() {
1770 g_browser_process->profile_manager()->CreateProfileAsync(
1771 ProfileManager::GetGuestProfilePath(),
Marc Treib92127162018-08-27 14:20:031772 base::BindRepeating(&UpdateProfileInUse), base::string16(),
Elly Fong-Jones68b85b82018-04-21 00:36:541773 std::string());
1774}
1775
Owen Min3bcf8d152018-07-16 16:50:391776void EnterpriseStartupDialogClosed() {
1777 AppController* controller =
1778 base::mac::ObjCCastStrict<AppController>([NSApp delegate]);
1779 if (controller != nil) {
1780 NSNotification* notify = [NSNotification
1781 notificationWithName:NSApplicationDidFinishLaunchingNotification
1782 object:NSApp];
1783 [controller applicationDidFinishLaunching:notify];
1784 }
1785}
1786
[email protected]216e9042009-10-29 17:05:231787} // namespace app_controller_mac