[email protected] | 71c0eb9 | 2012-01-03 17:57:30 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 5c9587c | 2008-12-09 21:20:16 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | e6b5bc2 | 2011-09-08 22:01:56 | [diff] [blame] | 5 | #include "chrome/browser/chrome_browser_main_mac.h" |
[email protected] | 1152b7e | 2009-09-14 03:26:03 | [diff] [blame] | 6 | |
[email protected] | ce8c16ad | 2009-08-12 19:00:42 | [diff] [blame] | 7 | #import <Cocoa/Cocoa.h> |
[email protected] | 1152b7e | 2009-09-14 03:26:03 | [diff] [blame] | 8 | |
[email protected] | 5c9587c | 2008-12-09 21:20:16 | [diff] [blame] | 9 | #include "base/command_line.h" |
[email protected] | 5799981 | 2013-02-24 05:40:52 | [diff] [blame] | 10 | #include "base/files/file_path.h" |
calamity | c95d782 | 2015-05-04 02:10:16 | [diff] [blame] | 11 | #include "base/files/file_util.h" |
[email protected] | 2f1804c | 2012-01-19 14:59:07 | [diff] [blame] | 12 | #include "base/mac/bundle_locations.h" |
tapted | 676995d | 2016-04-18 11:32:29 | [diff] [blame] | 13 | #import "base/mac/foundation_util.h" |
[email protected] | 0378bf4 | 2011-01-01 18:20:14 | [diff] [blame] | 14 | #include "base/mac/mac_util.h" |
[email protected] | a852203 | 2013-06-24 22:51:46 | [diff] [blame] | 15 | #include "base/mac/scoped_nsobject.h" |
thakis | 17c246d1 | 2016-07-01 22:28:09 | [diff] [blame] | 16 | #include "base/mac/sdk_forward_declarations.h" |
[email protected] | e1cb0e9 | 2010-06-15 07:23:59 | [diff] [blame] | 17 | #include "base/path_service.h" |
Christopher Lam | 32c68452 | 2017-07-28 03:05:02 | [diff] [blame] | 18 | #include "base/task_scheduler/post_task.h" |
| 19 | #include "base/task_scheduler/task_traits.h" |
gab | b15e1907 | 2016-05-11 20:45:41 | [diff] [blame] | 20 | #include "base/threading/thread_task_runner_handle.h" |
[email protected] | 3b6aa8b6 | 2009-09-15 21:36:11 | [diff] [blame] | 21 | #import "chrome/browser/app_controller_mac.h" |
tapted | 63829f7 | 2014-09-24 23:50:50 | [diff] [blame] | 22 | #include "chrome/browser/apps/app_shim/app_shim_host_manager_mac.h" |
[email protected] | e8b6ca0 | 2013-07-10 18:00:51 | [diff] [blame] | 23 | #include "chrome/browser/browser_process.h" |
[email protected] | aaa47ee | 2009-11-05 21:53:01 | [diff] [blame] | 24 | #import "chrome/browser/chrome_browser_application_mac.h" |
Christopher Lam | 32c68452 | 2017-07-28 03:05:02 | [diff] [blame] | 25 | #include "chrome/browser/first_run/first_run.h" |
[email protected] | c4d501e | 2012-03-27 20:08:02 | [diff] [blame] | 26 | #include "chrome/browser/mac/install_from_dmg.h" |
kerrnel | 26eeef0 | 2017-03-10 19:03:43 | [diff] [blame] | 27 | #include "chrome/browser/mac/keychain_reauthorize.h" |
[email protected] | 5950f571 | 2011-06-20 22:15:52 | [diff] [blame] | 28 | #import "chrome/browser/mac/keystone_glue.h" |
[email protected] | 7d1aaa6 | 2014-07-18 02:21:30 | [diff] [blame] | 29 | #include "chrome/browser/mac/mac_startup_profiler.h" |
[email protected] | bdad6e6 | 2014-05-06 08:47:37 | [diff] [blame] | 30 | #include "chrome/browser/ui/app_list/app_list_service.h" |
[email protected] | e1cb0e9 | 2010-06-15 07:23:59 | [diff] [blame] | 31 | #include "chrome/common/chrome_paths.h" |
[email protected] | d7dbe28c | 2010-07-29 04:33:47 | [diff] [blame] | 32 | #include "chrome/common/chrome_switches.h" |
scottmg | cfa5b01b | 2015-11-30 23:13:46 | [diff] [blame] | 33 | #include "components/crash/content/app/crashpad.h" |
[email protected] | d6147bd | 2014-06-11 01:58:19 | [diff] [blame] | 34 | #include "components/metrics/metrics_service.h" |
[email protected] | 4573fbd | 2011-10-31 20:25:18 | [diff] [blame] | 35 | #include "content/public/common/main_function_params.h" |
[email protected] | b39ef1cb | 2011-10-25 04:46:55 | [diff] [blame] | 36 | #include "content/public/common/result_codes.h" |
[email protected] | c051a1b | 2011-01-21 23:30:17 | [diff] [blame] | 37 | #include "ui/base/l10n/l10n_util_mac.h" |
[email protected] | 42ce29d | 2011-01-20 23:19:46 | [diff] [blame] | 38 | #include "ui/base/resource/resource_bundle.h" |
[email protected] | 63942f2 | 2012-05-01 06:11:52 | [diff] [blame] | 39 | #include "ui/base/resource/resource_handle.h" |
[email protected] | 5c9587c | 2008-12-09 21:20:16 | [diff] [blame] | 40 | |
calamity | c95d782 | 2015-05-04 02:10:16 | [diff] [blame] | 41 | namespace { |
| 42 | |
| 43 | // Writes an undocumented sentinel file that prevents Spotlight from indexing |
| 44 | // below a particular path in order to reap some power savings. |
| 45 | void EnsureMetadataNeverIndexFileOnFileThread( |
| 46 | const base::FilePath& user_data_dir) { |
| 47 | const char kMetadataNeverIndexFilename[] = ".metadata_never_index"; |
| 48 | base::FilePath metadata_file_path = |
| 49 | user_data_dir.Append(kMetadataNeverIndexFilename); |
| 50 | if (base::PathExists(metadata_file_path)) |
| 51 | return; |
| 52 | |
| 53 | if (base::WriteFile(metadata_file_path, nullptr, 0) == -1) |
| 54 | DLOG(FATAL) << "Could not write .metadata_never_index file."; |
| 55 | } |
| 56 | |
| 57 | void EnsureMetadataNeverIndexFile(const base::FilePath& user_data_dir) { |
Christopher Lam | 32c68452 | 2017-07-28 03:05:02 | [diff] [blame] | 58 | base::PostTaskWithTraits( |
| 59 | FROM_HERE, |
| 60 | {base::MayBlock(), base::TaskPriority::BACKGROUND, |
| 61 | base::TaskShutdownBehavior::BLOCK_SHUTDOWN}, |
calamity | c95d782 | 2015-05-04 02:10:16 | [diff] [blame] | 62 | base::Bind(&EnsureMetadataNeverIndexFileOnFileThread, user_data_dir)); |
| 63 | } |
| 64 | |
| 65 | } // namespace |
| 66 | |
[email protected] | e6b5bc2 | 2011-09-08 22:01:56 | [diff] [blame] | 67 | // ChromeBrowserMainPartsMac --------------------------------------------------- |
[email protected] | 1fec6435 | 2010-07-27 13:55:21 | [diff] [blame] | 68 | |
[email protected] | e6b5bc2 | 2011-09-08 22:01:56 | [diff] [blame] | 69 | ChromeBrowserMainPartsMac::ChromeBrowserMainPartsMac( |
[email protected] | 4573fbd | 2011-10-31 20:25:18 | [diff] [blame] | 70 | const content::MainFunctionParams& parameters) |
[email protected] | e6b5bc2 | 2011-09-08 22:01:56 | [diff] [blame] | 71 | : ChromeBrowserMainPartsPosix(parameters) { |
[email protected] | f967b72 | 2011-09-07 00:58:04 | [diff] [blame] | 72 | } |
[email protected] | 1fec6435 | 2010-07-27 13:55:21 | [diff] [blame] | 73 | |
[email protected] | 39fc5d32 | 2012-09-15 10:54:55 | [diff] [blame] | 74 | ChromeBrowserMainPartsMac::~ChromeBrowserMainPartsMac() { |
| 75 | } |
| 76 | |
Scott Violet | 9068b4df | 2018-01-12 16:44:21 | [diff] [blame] | 77 | int ChromeBrowserMainPartsMac::PreEarlyInitialization() { |
[email protected] | 769ddfe | 2014-06-13 23:13:20 | [diff] [blame] | 78 | if (base::mac::WasLaunchedAsLoginItemRestoreState()) { |
avi | 3ef9ec9e | 2014-12-22 22:50:17 | [diff] [blame] | 79 | base::CommandLine* singleton_command_line = |
| 80 | base::CommandLine::ForCurrentProcess(); |
[email protected] | 769ddfe | 2014-06-13 23:13:20 | [diff] [blame] | 81 | singleton_command_line->AppendSwitch(switches::kRestoreLastSession); |
| 82 | } else if (base::mac::WasLaunchedAsHiddenLoginItem()) { |
avi | 3ef9ec9e | 2014-12-22 22:50:17 | [diff] [blame] | 83 | base::CommandLine* singleton_command_line = |
| 84 | base::CommandLine::ForCurrentProcess(); |
[email protected] | f967b72 | 2011-09-07 00:58:04 | [diff] [blame] | 85 | singleton_command_line->AppendSwitch(switches::kNoStartupWindow); |
| 86 | } |
Scott Violet | 9068b4df | 2018-01-12 16:44:21 | [diff] [blame] | 87 | |
[email protected] | 4764efa | 2013-01-25 17:18:48 | [diff] [blame] | 88 | // Tell Cocoa to finish its initialization, which we want to do manually |
[email protected] | f967b72 | 2011-09-07 00:58:04 | [diff] [blame] | 89 | // instead of calling NSApplicationMain(). The primary reason is that NSAM() |
| 90 | // never returns, which would leave all the objects currently on the stack |
| 91 | // in scoped_ptrs hanging and never cleaned up. We then load the main nib |
| 92 | // directly. The main event loop is run from common code using the |
| 93 | // MessageLoop API, which works out ok for us because it's a wrapper around |
| 94 | // CFRunLoop. |
| 95 | |
| 96 | // Initialize NSApplication using the custom subclass. |
[email protected] | d7de5787 | 2011-12-06 23:32:43 | [diff] [blame] | 97 | chrome_browser_application_mac::RegisterBrowserCrApp(); |
[email protected] | f967b72 | 2011-09-07 00:58:04 | [diff] [blame] | 98 | |
[email protected] | a88f636 | 2014-03-18 04:25:35 | [diff] [blame] | 99 | // If ui_task is not NULL, the app is actually a browser_test. |
[email protected] | 716476c | 2011-12-29 00:07:03 | [diff] [blame] | 100 | if (!parameters().ui_task) { |
[email protected] | f967b72 | 2011-09-07 00:58:04 | [diff] [blame] | 101 | // The browser process only wants to support the language Cocoa will use, |
Scott Violet | 740c263 | 2018-03-09 04:11:42 | [diff] [blame^] | 102 | // so force the app locale to be overriden with that value. This must |
| 103 | // happen before the ResourceBundle is loaded, which happens in |
| 104 | // ChromeBrowserMainParts::PreEarlyInitialization(). |
[email protected] | f967b72 | 2011-09-07 00:58:04 | [diff] [blame] | 105 | l10n_util::OverrideLocaleWithCocoaLocale(); |
[email protected] | 91484af | 2010-08-27 17:55:54 | [diff] [blame] | 106 | } |
| 107 | |
Scott Violet | 740c263 | 2018-03-09 04:11:42 | [diff] [blame^] | 108 | return ChromeBrowserMainPartsPosix::PreEarlyInitialization(); |
Scott Violet | 875789e | 2018-02-02 07:46:48 | [diff] [blame] | 109 | } |
[email protected] | a88f636 | 2014-03-18 04:25:35 | [diff] [blame] | 110 | |
Scott Violet | 875789e | 2018-02-02 07:46:48 | [diff] [blame] | 111 | void ChromeBrowserMainPartsMac::PreMainMessageLoopStart() { |
| 112 | MacStartupProfiler::GetInstance()->Profile( |
| 113 | MacStartupProfiler::PRE_MAIN_MESSAGE_LOOP_START); |
| 114 | ChromeBrowserMainPartsPosix::PreMainMessageLoopStart(); |
| 115 | |
| 116 | // ChromeBrowserMainParts should have loaded the resource bundle by this |
| 117 | // point (needed to load the nib). |
| 118 | CHECK(ui::ResourceBundle::HasSharedInstance()); |
[email protected] | a88f636 | 2014-03-18 04:25:35 | [diff] [blame] | 119 | |
[email protected] | c4d501e | 2012-03-27 20:08:02 | [diff] [blame] | 120 | // This is a no-op if the KeystoneRegistration framework is not present. |
| 121 | // The framework is only distributed with branded Google Chrome builds. |
| 122 | [[KeystoneGlue defaultKeystoneGlue] registerWithKeystone]; |
| 123 | |
| 124 | // Disk image installation is sort of a first-run task, so it shares the |
[email protected] | 0a160a1b | 2013-08-08 22:20:00 | [diff] [blame] | 125 | // no first run switches. |
[email protected] | c4d501e | 2012-03-27 20:08:02 | [diff] [blame] | 126 | // |
| 127 | // This needs to be done after the resource bundle is initialized (for |
| 128 | // access to localizations in the UI) and after Keystone is initialized |
| 129 | // (because the installation may need to promote Keystone) but before the |
| 130 | // app controller is set up (and thus before MainMenu.nib is loaded, because |
| 131 | // the app controller assumes that a browser has been set up and will crash |
| 132 | // upon receipt of certain notifications if no browser exists), before |
| 133 | // anyone tries doing anything silly like firing off an import job, and |
| 134 | // before anything creating preferences like Local State in order for the |
| 135 | // relaunched installed application to still consider itself as first-run. |
[email protected] | 0a160a1b | 2013-08-08 22:20:00 | [diff] [blame] | 136 | if (!first_run::IsFirstRunSuppressed(parsed_command_line())) { |
[email protected] | c4d501e | 2012-03-27 20:08:02 | [diff] [blame] | 137 | if (MaybeInstallFromDiskImage()) { |
| 138 | // The application was installed and the installed copy has been |
| 139 | // launched. This process is now obsolete. Exit. |
| 140 | exit(0); |
| 141 | } |
| 142 | } |
| 143 | |
[email protected] | f967b72 | 2011-09-07 00:58:04 | [diff] [blame] | 144 | // Now load the nib (from the right bundle). |
[email protected] | a852203 | 2013-06-24 22:51:46 | [diff] [blame] | 145 | base::scoped_nsobject<NSNib> nib( |
| 146 | [[NSNib alloc] initWithNibNamed:@"MainMenu" |
| 147 | bundle:base::mac::FrameworkBundle()]); |
[email protected] | f967b72 | 2011-09-07 00:58:04 | [diff] [blame] | 148 | // TODO(viettrungluu): crbug.com/20504 - This currently leaks, so if you |
| 149 | // change this, you'll probably need to change the Valgrind suppression. |
thakis | 17c246d1 | 2016-07-01 22:28:09 | [diff] [blame] | 150 | NSArray* top_level_objects = nil; |
| 151 | [nib instantiateWithOwner:NSApp topLevelObjects:&top_level_objects]; |
| 152 | for (NSObject* object : top_level_objects) |
| 153 | [object retain]; |
[email protected] | f967b72 | 2011-09-07 00:58:04 | [diff] [blame] | 154 | // Make sure the app controller has been created. |
| 155 | DCHECK([NSApp delegate]); |
kerrnel | 26eeef0 | 2017-03-10 19:03:43 | [diff] [blame] | 156 | |
| 157 | // Do Keychain reauthorization. This gets two chances to run. If the first |
| 158 | // try doesn't complete successfully (crashes or is interrupted for any |
| 159 | // reason), there will be a second chance. Once this step completes |
| 160 | // successfully, it should never have to run again. |
| 161 | NSString* const keychain_reauthorize_pref = |
| 162 | @"KeychainReauthorizeInAppSpring2017"; |
| 163 | const int kKeychainReauthorizeMaxTries = 2; |
| 164 | |
| 165 | chrome::KeychainReauthorizeIfNeeded(keychain_reauthorize_pref, |
| 166 | kKeychainReauthorizeMaxTries); |
[email protected] | f967b72 | 2011-09-07 00:58:04 | [diff] [blame] | 167 | } |
[email protected] | 03d8d3e9 | 2011-09-20 06:07:11 | [diff] [blame] | 168 | |
[email protected] | 7d1aaa6 | 2014-07-18 02:21:30 | [diff] [blame] | 169 | void ChromeBrowserMainPartsMac::PostMainMessageLoopStart() { |
| 170 | MacStartupProfiler::GetInstance()->Profile( |
| 171 | MacStartupProfiler::POST_MAIN_MESSAGE_LOOP_START); |
[email protected] | 6ddc1097 | 2014-07-29 07:53:33 | [diff] [blame] | 172 | ChromeBrowserMainPartsPosix::PostMainMessageLoopStart(); |
[email protected] | 7d1aaa6 | 2014-07-18 02:21:30 | [diff] [blame] | 173 | } |
| 174 | |
[email protected] | bdad6e6 | 2014-05-06 08:47:37 | [diff] [blame] | 175 | void ChromeBrowserMainPartsMac::PreProfileInit() { |
[email protected] | 7d1aaa6 | 2014-07-18 02:21:30 | [diff] [blame] | 176 | MacStartupProfiler::GetInstance()->Profile( |
| 177 | MacStartupProfiler::PRE_PROFILE_INIT); |
[email protected] | bdad6e6 | 2014-05-06 08:47:37 | [diff] [blame] | 178 | ChromeBrowserMainPartsPosix::PreProfileInit(); |
mlerman | a312cec | 2015-01-28 21:13:33 | [diff] [blame] | 179 | |
[email protected] | bdad6e6 | 2014-05-06 08:47:37 | [diff] [blame] | 180 | // This is called here so that the app shim socket is only created after |
| 181 | // taking the singleton lock. |
| 182 | g_browser_process->platform_part()->app_shim_host_manager()->Init(); |
mlerman | a312cec | 2015-01-28 21:13:33 | [diff] [blame] | 183 | AppListService::InitAll(NULL, |
| 184 | GetStartupProfilePath(user_data_dir(), parsed_command_line())); |
[email protected] | bdad6e6 | 2014-05-06 08:47:37 | [diff] [blame] | 185 | } |
| 186 | |
[email protected] | e8b6ca0 | 2013-07-10 18:00:51 | [diff] [blame] | 187 | void ChromeBrowserMainPartsMac::PostProfileInit() { |
[email protected] | 7d1aaa6 | 2014-07-18 02:21:30 | [diff] [blame] | 188 | MacStartupProfiler::GetInstance()->Profile( |
| 189 | MacStartupProfiler::POST_PROFILE_INIT); |
[email protected] | e8b6ca0 | 2013-07-10 18:00:51 | [diff] [blame] | 190 | ChromeBrowserMainPartsPosix::PostProfileInit(); |
mark | d413b2d | 2015-03-13 07:45:40 | [diff] [blame] | 191 | |
[email protected] | e8b6ca0 | 2013-07-10 18:00:51 | [diff] [blame] | 192 | g_browser_process->metrics_service()->RecordBreakpadRegistration( |
mark | d413b2d | 2015-03-13 07:45:40 | [diff] [blame] | 193 | crash_reporter::GetUploadsEnabled()); |
calamity | c95d782 | 2015-05-04 02:10:16 | [diff] [blame] | 194 | |
Christopher Lam | 32c68452 | 2017-07-28 03:05:02 | [diff] [blame] | 195 | if (first_run::IsChromeFirstRun()) |
| 196 | EnsureMetadataNeverIndexFile(user_data_dir()); |
bcwhite | e930129 | 2015-06-22 15:31:58 | [diff] [blame] | 197 | |
borisv | 957d52d | 2016-04-13 18:35:43 | [diff] [blame] | 198 | // Activation of Keystone is not automatic but done in response to the |
| 199 | // counting and reporting of profiles. |
| 200 | KeystoneGlue* glue = [KeystoneGlue defaultKeystoneGlue]; |
| 201 | if (glue && ![glue isRegisteredAndActive]) { |
| 202 | // If profile loading has failed, we still need to handle other tasks |
| 203 | // like marking of the product as active. |
| 204 | [glue updateProfileCountsWithNumProfiles:0 |
| 205 | numSignedInProfiles:0]; |
| 206 | } |
[email protected] | e8b6ca0 | 2013-07-10 18:00:51 | [diff] [blame] | 207 | } |
| 208 | |
[email protected] | 03d8d3e9 | 2011-09-20 06:07:11 | [diff] [blame] | 209 | void ChromeBrowserMainPartsMac::DidEndMainMessageLoop() { |
tapted | 676995d | 2016-04-18 11:32:29 | [diff] [blame] | 210 | AppController* appController = |
| 211 | base::mac::ObjCCastStrict<AppController>([NSApp delegate]); |
[email protected] | 03d8d3e9 | 2011-09-20 06:07:11 | [diff] [blame] | 212 | [appController didEndMainMessageLoop]; |
| 213 | } |