[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" |
[email protected] | e1cb0e9 | 2010-06-15 07:23:59 | [diff] [blame] | 16 | #include "base/path_service.h" |
gab | b15e1907 | 2016-05-11 20:45:41 | [diff] [blame^] | 17 | #include "base/threading/thread_task_runner_handle.h" |
[email protected] | 3b6aa8b6 | 2009-09-15 21:36:11 | [diff] [blame] | 18 | #import "chrome/browser/app_controller_mac.h" |
tapted | 63829f7 | 2014-09-24 23:50:50 | [diff] [blame] | 19 | #include "chrome/browser/apps/app_shim/app_shim_host_manager_mac.h" |
[email protected] | e8b6ca0 | 2013-07-10 18:00:51 | [diff] [blame] | 20 | #include "chrome/browser/browser_process.h" |
[email protected] | aaa47ee | 2009-11-05 21:53:01 | [diff] [blame] | 21 | #import "chrome/browser/chrome_browser_application_mac.h" |
[email protected] | c4d501e | 2012-03-27 20:08:02 | [diff] [blame] | 22 | #include "chrome/browser/mac/install_from_dmg.h" |
[email protected] | 5950f571 | 2011-06-20 22:15:52 | [diff] [blame] | 23 | #import "chrome/browser/mac/keystone_glue.h" |
[email protected] | 7d1aaa6 | 2014-07-18 02:21:30 | [diff] [blame] | 24 | #include "chrome/browser/mac/mac_startup_profiler.h" |
[email protected] | bdad6e6 | 2014-05-06 08:47:37 | [diff] [blame] | 25 | #include "chrome/browser/ui/app_list/app_list_service.h" |
[email protected] | e1cb0e9 | 2010-06-15 07:23:59 | [diff] [blame] | 26 | #include "chrome/common/chrome_paths.h" |
[email protected] | d7dbe28c | 2010-07-29 04:33:47 | [diff] [blame] | 27 | #include "chrome/common/chrome_switches.h" |
scottmg | cfa5b01b | 2015-11-30 23:13:46 | [diff] [blame] | 28 | #include "components/crash/content/app/crashpad.h" |
[email protected] | d6147bd | 2014-06-11 01:58:19 | [diff] [blame] | 29 | #include "components/metrics/metrics_service.h" |
[email protected] | 4573fbd | 2011-10-31 20:25:18 | [diff] [blame] | 30 | #include "content/public/common/main_function_params.h" |
[email protected] | b39ef1cb | 2011-10-25 04:46:55 | [diff] [blame] | 31 | #include "content/public/common/result_codes.h" |
[email protected] | c051a1b | 2011-01-21 23:30:17 | [diff] [blame] | 32 | #include "ui/base/l10n/l10n_util_mac.h" |
[email protected] | 42ce29d | 2011-01-20 23:19:46 | [diff] [blame] | 33 | #include "ui/base/resource/resource_bundle.h" |
[email protected] | 63942f2 | 2012-05-01 06:11:52 | [diff] [blame] | 34 | #include "ui/base/resource/resource_handle.h" |
[email protected] | 5c9587c | 2008-12-09 21:20:16 | [diff] [blame] | 35 | |
calamity | c95d782 | 2015-05-04 02:10:16 | [diff] [blame] | 36 | namespace { |
| 37 | |
| 38 | // Writes an undocumented sentinel file that prevents Spotlight from indexing |
| 39 | // below a particular path in order to reap some power savings. |
| 40 | void EnsureMetadataNeverIndexFileOnFileThread( |
| 41 | const base::FilePath& user_data_dir) { |
| 42 | const char kMetadataNeverIndexFilename[] = ".metadata_never_index"; |
| 43 | base::FilePath metadata_file_path = |
| 44 | user_data_dir.Append(kMetadataNeverIndexFilename); |
| 45 | if (base::PathExists(metadata_file_path)) |
| 46 | return; |
| 47 | |
| 48 | if (base::WriteFile(metadata_file_path, nullptr, 0) == -1) |
| 49 | DLOG(FATAL) << "Could not write .metadata_never_index file."; |
| 50 | } |
| 51 | |
| 52 | void EnsureMetadataNeverIndexFile(const base::FilePath& user_data_dir) { |
| 53 | content::BrowserThread::PostTask( |
| 54 | content::BrowserThread::FILE, FROM_HERE, |
| 55 | base::Bind(&EnsureMetadataNeverIndexFileOnFileThread, user_data_dir)); |
| 56 | } |
| 57 | |
| 58 | } // namespace |
| 59 | |
[email protected] | e6b5bc2 | 2011-09-08 22:01:56 | [diff] [blame] | 60 | // ChromeBrowserMainPartsMac --------------------------------------------------- |
[email protected] | 1fec6435 | 2010-07-27 13:55:21 | [diff] [blame] | 61 | |
[email protected] | e6b5bc2 | 2011-09-08 22:01:56 | [diff] [blame] | 62 | ChromeBrowserMainPartsMac::ChromeBrowserMainPartsMac( |
[email protected] | 4573fbd | 2011-10-31 20:25:18 | [diff] [blame] | 63 | const content::MainFunctionParams& parameters) |
[email protected] | e6b5bc2 | 2011-09-08 22:01:56 | [diff] [blame] | 64 | : ChromeBrowserMainPartsPosix(parameters) { |
[email protected] | f967b72 | 2011-09-07 00:58:04 | [diff] [blame] | 65 | } |
[email protected] | 1fec6435 | 2010-07-27 13:55:21 | [diff] [blame] | 66 | |
[email protected] | 39fc5d32 | 2012-09-15 10:54:55 | [diff] [blame] | 67 | ChromeBrowserMainPartsMac::~ChromeBrowserMainPartsMac() { |
| 68 | } |
| 69 | |
[email protected] | e6b5bc2 | 2011-09-08 22:01:56 | [diff] [blame] | 70 | void ChromeBrowserMainPartsMac::PreEarlyInitialization() { |
[email protected] | 37c722b | 2011-09-08 22:18:46 | [diff] [blame] | 71 | ChromeBrowserMainPartsPosix::PreEarlyInitialization(); |
[email protected] | 91484af | 2010-08-27 17:55:54 | [diff] [blame] | 72 | |
[email protected] | 769ddfe | 2014-06-13 23:13:20 | [diff] [blame] | 73 | if (base::mac::WasLaunchedAsLoginItemRestoreState()) { |
avi | 3ef9ec9e | 2014-12-22 22:50:17 | [diff] [blame] | 74 | base::CommandLine* singleton_command_line = |
| 75 | base::CommandLine::ForCurrentProcess(); |
[email protected] | 769ddfe | 2014-06-13 23:13:20 | [diff] [blame] | 76 | singleton_command_line->AppendSwitch(switches::kRestoreLastSession); |
| 77 | } else if (base::mac::WasLaunchedAsHiddenLoginItem()) { |
avi | 3ef9ec9e | 2014-12-22 22:50:17 | [diff] [blame] | 78 | base::CommandLine* singleton_command_line = |
| 79 | base::CommandLine::ForCurrentProcess(); |
[email protected] | f967b72 | 2011-09-07 00:58:04 | [diff] [blame] | 80 | singleton_command_line->AppendSwitch(switches::kNoStartupWindow); |
| 81 | } |
| 82 | } |
| 83 | |
[email protected] | e6b5bc2 | 2011-09-08 22:01:56 | [diff] [blame] | 84 | void ChromeBrowserMainPartsMac::PreMainMessageLoopStart() { |
[email protected] | 7d1aaa6 | 2014-07-18 02:21:30 | [diff] [blame] | 85 | MacStartupProfiler::GetInstance()->Profile( |
| 86 | MacStartupProfiler::PRE_MAIN_MESSAGE_LOOP_START); |
[email protected] | 37c722b | 2011-09-08 22:18:46 | [diff] [blame] | 87 | ChromeBrowserMainPartsPosix::PreMainMessageLoopStart(); |
[email protected] | f967b72 | 2011-09-07 00:58:04 | [diff] [blame] | 88 | |
[email protected] | 4764efa | 2013-01-25 17:18:48 | [diff] [blame] | 89 | // Tell Cocoa to finish its initialization, which we want to do manually |
[email protected] | f967b72 | 2011-09-07 00:58:04 | [diff] [blame] | 90 | // instead of calling NSApplicationMain(). The primary reason is that NSAM() |
| 91 | // never returns, which would leave all the objects currently on the stack |
| 92 | // in scoped_ptrs hanging and never cleaned up. We then load the main nib |
| 93 | // directly. The main event loop is run from common code using the |
| 94 | // MessageLoop API, which works out ok for us because it's a wrapper around |
| 95 | // CFRunLoop. |
| 96 | |
| 97 | // Initialize NSApplication using the custom subclass. |
[email protected] | d7de5787 | 2011-12-06 23:32:43 | [diff] [blame] | 98 | chrome_browser_application_mac::RegisterBrowserCrApp(); |
[email protected] | f967b72 | 2011-09-07 00:58:04 | [diff] [blame] | 99 | |
[email protected] | a88f636 | 2014-03-18 04:25:35 | [diff] [blame] | 100 | // If ui_task is not NULL, the app is actually a browser_test. |
[email protected] | 716476c | 2011-12-29 00:07:03 | [diff] [blame] | 101 | if (!parameters().ui_task) { |
[email protected] | f967b72 | 2011-09-07 00:58:04 | [diff] [blame] | 102 | // The browser process only wants to support the language Cocoa will use, |
| 103 | // so force the app locale to be overriden with that value. |
| 104 | l10n_util::OverrideLocaleWithCocoaLocale(); |
[email protected] | 91484af | 2010-08-27 17:55:54 | [diff] [blame] | 105 | } |
| 106 | |
[email protected] | a88f636 | 2014-03-18 04:25:35 | [diff] [blame] | 107 | // Before we load the nib, we need to start up the resource bundle so we |
| 108 | // have the strings avaiable for localization. |
| 109 | // TODO(markusheintz): Read preference pref::kApplicationLocale in order |
| 110 | // to enforce the application locale. |
| 111 | const std::string loaded_locale = |
[email protected] | 4ce2aa6 | 2014-08-14 01:05:49 | [diff] [blame] | 112 | ui::ResourceBundle::InitSharedInstanceWithLocale( |
karandeepb | e60fc3c | 2016-01-21 23:22:42 | [diff] [blame] | 113 | std::string(), &resource_delegate_, |
| 114 | ui::ResourceBundle::LOAD_COMMON_RESOURCES); |
[email protected] | a88f636 | 2014-03-18 04:25:35 | [diff] [blame] | 115 | CHECK(!loaded_locale.empty()) << "Default locale could not be found"; |
| 116 | |
| 117 | base::FilePath resources_pack_path; |
| 118 | PathService::Get(chrome::FILE_RESOURCES_PACK, &resources_pack_path); |
| 119 | ResourceBundle::GetSharedInstance().AddDataPackFromPath( |
| 120 | resources_pack_path, ui::SCALE_FACTOR_NONE); |
| 121 | |
[email protected] | c4d501e | 2012-03-27 20:08:02 | [diff] [blame] | 122 | // This is a no-op if the KeystoneRegistration framework is not present. |
| 123 | // The framework is only distributed with branded Google Chrome builds. |
| 124 | [[KeystoneGlue defaultKeystoneGlue] registerWithKeystone]; |
| 125 | |
| 126 | // 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] | 127 | // no first run switches. |
[email protected] | c4d501e | 2012-03-27 20:08:02 | [diff] [blame] | 128 | // |
| 129 | // This needs to be done after the resource bundle is initialized (for |
| 130 | // access to localizations in the UI) and after Keystone is initialized |
| 131 | // (because the installation may need to promote Keystone) but before the |
| 132 | // app controller is set up (and thus before MainMenu.nib is loaded, because |
| 133 | // the app controller assumes that a browser has been set up and will crash |
| 134 | // upon receipt of certain notifications if no browser exists), before |
| 135 | // anyone tries doing anything silly like firing off an import job, and |
| 136 | // before anything creating preferences like Local State in order for the |
| 137 | // relaunched installed application to still consider itself as first-run. |
[email protected] | 0a160a1b | 2013-08-08 22:20:00 | [diff] [blame] | 138 | if (!first_run::IsFirstRunSuppressed(parsed_command_line())) { |
[email protected] | c4d501e | 2012-03-27 20:08:02 | [diff] [blame] | 139 | if (MaybeInstallFromDiskImage()) { |
| 140 | // The application was installed and the installed copy has been |
| 141 | // launched. This process is now obsolete. Exit. |
| 142 | exit(0); |
| 143 | } |
| 144 | } |
| 145 | |
[email protected] | f967b72 | 2011-09-07 00:58:04 | [diff] [blame] | 146 | // Now load the nib (from the right bundle). |
[email protected] | a852203 | 2013-06-24 22:51:46 | [diff] [blame] | 147 | base::scoped_nsobject<NSNib> nib( |
| 148 | [[NSNib alloc] initWithNibNamed:@"MainMenu" |
| 149 | bundle:base::mac::FrameworkBundle()]); |
[email protected] | f967b72 | 2011-09-07 00:58:04 | [diff] [blame] | 150 | // TODO(viettrungluu): crbug.com/20504 - This currently leaks, so if you |
| 151 | // change this, you'll probably need to change the Valgrind suppression. |
| 152 | [nib instantiateNibWithOwner:NSApp topLevelObjects:nil]; |
| 153 | // Make sure the app controller has been created. |
| 154 | DCHECK([NSApp delegate]); |
[email protected] | 1fec6435 | 2010-07-27 13:55:21 | [diff] [blame] | 155 | |
[email protected] | 47077b5 | 2014-03-11 15:25:59 | [diff] [blame] | 156 | [[NSUserDefaults standardUserDefaults] registerDefaults:@{ |
| 157 | // Prevent Cocoa from turning command-line arguments into |
| 158 | // |-application:openFiles:|, since we already handle them directly. |
| 159 | // @"NO" looks like a mistake, but the value really is supposed to be a |
| 160 | // string. |
ccameron | c685e64c | 2015-05-12 07:05:17 | [diff] [blame] | 161 | @"NSTreatUnknownArgumentsAsOpen": @"NO" |
[email protected] | 47077b5 | 2014-03-11 15:25:59 | [diff] [blame] | 162 | }]; |
[email protected] | f967b72 | 2011-09-07 00:58:04 | [diff] [blame] | 163 | } |
[email protected] | 03d8d3e9 | 2011-09-20 06:07:11 | [diff] [blame] | 164 | |
[email protected] | 7d1aaa6 | 2014-07-18 02:21:30 | [diff] [blame] | 165 | void ChromeBrowserMainPartsMac::PostMainMessageLoopStart() { |
| 166 | MacStartupProfiler::GetInstance()->Profile( |
| 167 | MacStartupProfiler::POST_MAIN_MESSAGE_LOOP_START); |
[email protected] | 6ddc1097 | 2014-07-29 07:53:33 | [diff] [blame] | 168 | ChromeBrowserMainPartsPosix::PostMainMessageLoopStart(); |
[email protected] | 7d1aaa6 | 2014-07-18 02:21:30 | [diff] [blame] | 169 | } |
| 170 | |
[email protected] | bdad6e6 | 2014-05-06 08:47:37 | [diff] [blame] | 171 | void ChromeBrowserMainPartsMac::PreProfileInit() { |
[email protected] | 7d1aaa6 | 2014-07-18 02:21:30 | [diff] [blame] | 172 | MacStartupProfiler::GetInstance()->Profile( |
| 173 | MacStartupProfiler::PRE_PROFILE_INIT); |
[email protected] | bdad6e6 | 2014-05-06 08:47:37 | [diff] [blame] | 174 | ChromeBrowserMainPartsPosix::PreProfileInit(); |
mlerman | a312cec | 2015-01-28 21:13:33 | [diff] [blame] | 175 | |
[email protected] | bdad6e6 | 2014-05-06 08:47:37 | [diff] [blame] | 176 | // This is called here so that the app shim socket is only created after |
| 177 | // taking the singleton lock. |
| 178 | g_browser_process->platform_part()->app_shim_host_manager()->Init(); |
mlerman | a312cec | 2015-01-28 21:13:33 | [diff] [blame] | 179 | AppListService::InitAll(NULL, |
| 180 | GetStartupProfilePath(user_data_dir(), parsed_command_line())); |
[email protected] | bdad6e6 | 2014-05-06 08:47:37 | [diff] [blame] | 181 | } |
| 182 | |
[email protected] | e8b6ca0 | 2013-07-10 18:00:51 | [diff] [blame] | 183 | void ChromeBrowserMainPartsMac::PostProfileInit() { |
[email protected] | 7d1aaa6 | 2014-07-18 02:21:30 | [diff] [blame] | 184 | MacStartupProfiler::GetInstance()->Profile( |
| 185 | MacStartupProfiler::POST_PROFILE_INIT); |
[email protected] | e8b6ca0 | 2013-07-10 18:00:51 | [diff] [blame] | 186 | ChromeBrowserMainPartsPosix::PostProfileInit(); |
mark | d413b2d | 2015-03-13 07:45:40 | [diff] [blame] | 187 | |
[email protected] | e8b6ca0 | 2013-07-10 18:00:51 | [diff] [blame] | 188 | g_browser_process->metrics_service()->RecordBreakpadRegistration( |
mark | d413b2d | 2015-03-13 07:45:40 | [diff] [blame] | 189 | crash_reporter::GetUploadsEnabled()); |
calamity | c95d782 | 2015-05-04 02:10:16 | [diff] [blame] | 190 | |
| 191 | // TODO(calamity): Make this gated on first_run::IsChromeFirstRun() in M45. |
| 192 | content::BrowserThread::PostAfterStartupTask( |
| 193 | FROM_HERE, base::ThreadTaskRunnerHandle::Get(), |
| 194 | base::Bind(&EnsureMetadataNeverIndexFile, user_data_dir())); |
bcwhite | e930129 | 2015-06-22 15:31:58 | [diff] [blame] | 195 | |
borisv | 957d52d | 2016-04-13 18:35:43 | [diff] [blame] | 196 | // Activation of Keystone is not automatic but done in response to the |
| 197 | // counting and reporting of profiles. |
| 198 | KeystoneGlue* glue = [KeystoneGlue defaultKeystoneGlue]; |
| 199 | if (glue && ![glue isRegisteredAndActive]) { |
| 200 | // If profile loading has failed, we still need to handle other tasks |
| 201 | // like marking of the product as active. |
| 202 | [glue updateProfileCountsWithNumProfiles:0 |
| 203 | numSignedInProfiles:0]; |
| 204 | } |
[email protected] | e8b6ca0 | 2013-07-10 18:00:51 | [diff] [blame] | 205 | } |
| 206 | |
[email protected] | 03d8d3e9 | 2011-09-20 06:07:11 | [diff] [blame] | 207 | void ChromeBrowserMainPartsMac::DidEndMainMessageLoop() { |
tapted | 676995d | 2016-04-18 11:32:29 | [diff] [blame] | 208 | AppController* appController = |
| 209 | base::mac::ObjCCastStrict<AppController>([NSApp delegate]); |
[email protected] | 03d8d3e9 | 2011-09-20 06:07:11 | [diff] [blame] | 210 | [appController didEndMainMessageLoop]; |
| 211 | } |