[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] | 4d170e8 | 2012-12-04 00:34:13 | [diff] [blame] | 8 | #include <sys/sysctl.h> |
[email protected] | 1152b7e | 2009-09-14 03:26:03 | [diff] [blame] | 9 | |
[email protected] | 5c9587c | 2008-12-09 21:20:16 | [diff] [blame] | 10 | #include "base/command_line.h" |
[email protected] | 5799981 | 2013-02-24 05:40:52 | [diff] [blame] | 11 | #include "base/files/file_path.h" |
[email protected] | 2f1804c | 2012-01-19 14:59:07 | [diff] [blame] | 12 | #include "base/mac/bundle_locations.h" |
[email protected] | 0378bf4 | 2011-01-01 18:20:14 | [diff] [blame] | 13 | #include "base/mac/mac_util.h" |
[email protected] | a852203 | 2013-06-24 22:51:46 | [diff] [blame] | 14 | #include "base/mac/scoped_nsobject.h" |
[email protected] | 4d170e8 | 2012-12-04 00:34:13 | [diff] [blame] | 15 | #include "base/metrics/histogram.h" |
[email protected] | e1cb0e9 | 2010-06-15 07:23:59 | [diff] [blame] | 16 | #include "base/path_service.h" |
[email protected] | 1152b7e | 2009-09-14 03:26:03 | [diff] [blame] | 17 | #include "chrome/app/breakpad_mac.h" |
[email protected] | 3b6aa8b6 | 2009-09-15 21:36:11 | [diff] [blame] | 18 | #import "chrome/browser/app_controller_mac.h" |
[email protected] | e8b6ca0 | 2013-07-10 18:00:51 | [diff] [blame] | 19 | #include "chrome/browser/browser_process.h" |
[email protected] | aaa47ee | 2009-11-05 21:53:01 | [diff] [blame] | 20 | #import "chrome/browser/chrome_browser_application_mac.h" |
[email protected] | c4d501e | 2012-03-27 20:08:02 | [diff] [blame] | 21 | #include "chrome/browser/mac/install_from_dmg.h" |
[email protected] | f0c58de | 2012-05-15 22:11:28 | [diff] [blame] | 22 | #include "chrome/browser/mac/keychain_reauthorize.h" |
[email protected] | 5950f571 | 2011-06-20 22:15:52 | [diff] [blame] | 23 | #import "chrome/browser/mac/keystone_glue.h" |
[email protected] | 1152b7e | 2009-09-14 03:26:03 | [diff] [blame] | 24 | #include "chrome/browser/metrics/metrics_service.h" |
[email protected] | e1cb0e9 | 2010-06-15 07:23:59 | [diff] [blame] | 25 | #include "chrome/common/chrome_paths.h" |
[email protected] | d7dbe28c | 2010-07-29 04:33:47 | [diff] [blame] | 26 | #include "chrome/common/chrome_switches.h" |
[email protected] | 4573fbd | 2011-10-31 20:25:18 | [diff] [blame] | 27 | #include "content/public/common/main_function_params.h" |
[email protected] | b39ef1cb | 2011-10-25 04:46:55 | [diff] [blame] | 28 | #include "content/public/common/result_codes.h" |
[email protected] | c051a1b | 2011-01-21 23:30:17 | [diff] [blame] | 29 | #include "ui/base/l10n/l10n_util_mac.h" |
[email protected] | 42ce29d | 2011-01-20 23:19:46 | [diff] [blame] | 30 | #include "ui/base/resource/resource_bundle.h" |
[email protected] | 63942f2 | 2012-05-01 06:11:52 | [diff] [blame] | 31 | #include "ui/base/resource/resource_handle.h" |
[email protected] | 5c9587c | 2008-12-09 21:20:16 | [diff] [blame] | 32 | |
[email protected] | e1972bd | 2012-05-16 20:43:35 | [diff] [blame] | 33 | namespace { |
| 34 | |
[email protected] | e1972bd | 2012-05-16 20:43:35 | [diff] [blame] | 35 | // Some users rarely restart Chrome, so they might never get a chance to run |
| 36 | // the at-launch KeychainReauthorize. To account for them, there's also an |
| 37 | // at-update KeychainReauthorize option, which runs from .keystone_install for |
| 38 | // users on a user Keystone ticket. This operation may make sense for a period |
| 39 | // of time after the application switches to being signed by the new |
| 40 | // certificate, as long as the at-update stub executable is still signed by |
| 41 | // the old one. |
| 42 | NSString* const kKeychainReauthorizeAtUpdatePref = |
| 43 | @"KeychainReauthorizeAtUpdateMay2012"; |
| 44 | const int kKeychainReauthorizeAtUpdateMaxTries = 3; |
| 45 | |
[email protected] | 4d170e8 | 2012-12-04 00:34:13 | [diff] [blame] | 46 | // This is one enum instead of two so that the values can be correlated in a |
| 47 | // histogram. |
| 48 | enum CatSixtyFour { |
| 49 | // Older than any expected cat. |
| 50 | SABER_TOOTHED_CAT_32 = 0, |
| 51 | SABER_TOOTHED_CAT_64, |
| 52 | |
| 53 | // Known cats. |
| 54 | SNOW_LEOPARD_32, |
| 55 | SNOW_LEOPARD_64, |
| 56 | LION_32, // Unexpected, Lion requires a 64-bit CPU. |
| 57 | LION_64, |
| 58 | MOUNTAIN_LION_32, // Unexpected, Mountain Lion requires a 64-bit CPU. |
| 59 | MOUNTAIN_LION_64, |
[email protected] | aaf93de4 | 2013-10-02 07:41:57 | [diff] [blame^] | 60 | MAVERICKS_32, // Unexpected, Mavericks requires a 64-bit CPU. |
| 61 | MAVERICKS_64, |
[email protected] | 4d170e8 | 2012-12-04 00:34:13 | [diff] [blame] | 62 | |
| 63 | // DON'T add new constants here. It's important to keep the constant values, |
| 64 | // um, constant. Add new constants at the bottom. |
| 65 | |
[email protected] | 4d170e8 | 2012-12-04 00:34:13 | [diff] [blame] | 66 | // What if the bitsiness of the CPU can't be determined? |
| 67 | SABER_TOOTHED_CAT_DUNNO, |
| 68 | SNOW_LEOPARD_DUNNO, |
| 69 | LION_DUNNO, |
| 70 | MOUNTAIN_LION_DUNNO, |
[email protected] | aaf93de4 | 2013-10-02 07:41:57 | [diff] [blame^] | 71 | MAVERICKS_DUNNO, |
| 72 | |
| 73 | // Newer than any known cat. |
| 74 | FUTURE_CAT_32, // Unexpected, it's unlikely Apple will un-obsolete old CPUs. |
| 75 | FUTURE_CAT_64, |
[email protected] | 4d170e8 | 2012-12-04 00:34:13 | [diff] [blame] | 76 | FUTURE_CAT_DUNNO, |
| 77 | |
[email protected] | aaf93de4 | 2013-10-02 07:41:57 | [diff] [blame^] | 78 | // As new versions of Mac OS X are released with sillier and sillier names, |
| 79 | // rename the FUTURE_CAT enum values to match those names, and re-create |
| 80 | // FUTURE_CAT_[32|64|DUNNO] here. |
[email protected] | 4d170e8 | 2012-12-04 00:34:13 | [diff] [blame] | 81 | |
| 82 | CAT_SIXTY_FOUR_MAX |
| 83 | }; |
| 84 | |
| 85 | CatSixtyFour CatSixtyFourValue() { |
| 86 | #if defined(ARCH_CPU_64_BITS) |
| 87 | // If 64-bit code is running, then it's established that this CPU can run |
| 88 | // 64-bit code, and no further inquiry is necessary. |
| 89 | int cpu64 = 1; |
| 90 | bool cpu64_known = true; |
| 91 | #else |
| 92 | // Check a sysctl conveniently provided by the kernel that identifies |
| 93 | // whether the CPU supports 64-bit operation. Note that this tests the |
| 94 | // actual hardware capabilities, not the bitsiness of the running process, |
| 95 | // and not the bitsiness of the running kernel. The value thus determines |
| 96 | // whether the CPU is capable of running 64-bit programs (in the presence of |
| 97 | // proper OS runtime support) without regard to whether the current program |
| 98 | // is 64-bit (it may not be) or whether the current kernel is (the kernel |
| 99 | // can launch cross-bitted user-space tasks). |
| 100 | |
| 101 | int cpu64; |
| 102 | size_t len = sizeof(cpu64); |
| 103 | const char kSysctlName[] = "hw.cpu64bit_capable"; |
| 104 | bool cpu64_known = sysctlbyname(kSysctlName, &cpu64, &len, NULL, 0) == 0; |
| 105 | if (!cpu64_known) { |
| 106 | PLOG(WARNING) << "sysctlbyname(\"" << kSysctlName << "\")"; |
| 107 | } |
| 108 | #endif |
| 109 | |
| 110 | if (base::mac::IsOSSnowLeopard()) { |
| 111 | return cpu64_known ? (cpu64 ? SNOW_LEOPARD_64 : SNOW_LEOPARD_32) : |
| 112 | SNOW_LEOPARD_DUNNO; |
| 113 | } |
| 114 | if (base::mac::IsOSLion()) { |
| 115 | return cpu64_known ? (cpu64 ? LION_64 : LION_32) : |
| 116 | LION_DUNNO; |
| 117 | } |
| 118 | if (base::mac::IsOSMountainLion()) { |
| 119 | return cpu64_known ? (cpu64 ? MOUNTAIN_LION_64 : MOUNTAIN_LION_32) : |
| 120 | MOUNTAIN_LION_DUNNO; |
| 121 | } |
[email protected] | aaf93de4 | 2013-10-02 07:41:57 | [diff] [blame^] | 122 | if (base::mac::IsOSMavericks()) { |
| 123 | return cpu64_known ? (cpu64 ? MAVERICKS_64 : MAVERICKS_32) : |
| 124 | MAVERICKS_DUNNO; |
| 125 | } |
| 126 | if (base::mac::IsOSLaterThanMavericks_DontCallThis()) { |
[email protected] | 4d170e8 | 2012-12-04 00:34:13 | [diff] [blame] | 127 | return cpu64_known ? (cpu64 ? FUTURE_CAT_64 : FUTURE_CAT_32) : |
| 128 | FUTURE_CAT_DUNNO; |
| 129 | } |
| 130 | |
| 131 | // If it's not any of the expected OS versions or later than them, it must |
| 132 | // be prehistoric. |
| 133 | return cpu64_known ? (cpu64 ? SABER_TOOTHED_CAT_64 : SABER_TOOTHED_CAT_32) : |
| 134 | SABER_TOOTHED_CAT_DUNNO; |
| 135 | } |
| 136 | |
| 137 | void RecordCatSixtyFour() { |
| 138 | CatSixtyFour cat_sixty_four = CatSixtyFourValue(); |
| 139 | |
| 140 | // Set this higher than the highest value in the CatSixtyFour enum to |
| 141 | // provide some headroom and then leave it alone. See HISTOGRAM_ENUMERATION |
| 142 | // in base/metrics/histogram.h. |
| 143 | const int kMaxCatsAndSixtyFours = 32; |
| 144 | COMPILE_ASSERT(kMaxCatsAndSixtyFours >= CAT_SIXTY_FOUR_MAX, |
| 145 | CatSixtyFour_enum_grew_too_large); |
| 146 | |
| 147 | UMA_HISTOGRAM_ENUMERATION("OSX.CatSixtyFour", |
| 148 | cat_sixty_four, |
| 149 | kMaxCatsAndSixtyFours); |
| 150 | } |
| 151 | |
[email protected] | e1972bd | 2012-05-16 20:43:35 | [diff] [blame] | 152 | } // namespace |
| 153 | |
[email protected] | e6b5bc2 | 2011-09-08 22:01:56 | [diff] [blame] | 154 | // ChromeBrowserMainPartsMac --------------------------------------------------- |
[email protected] | 1fec6435 | 2010-07-27 13:55:21 | [diff] [blame] | 155 | |
[email protected] | e6b5bc2 | 2011-09-08 22:01:56 | [diff] [blame] | 156 | ChromeBrowserMainPartsMac::ChromeBrowserMainPartsMac( |
[email protected] | 4573fbd | 2011-10-31 20:25:18 | [diff] [blame] | 157 | const content::MainFunctionParams& parameters) |
[email protected] | e6b5bc2 | 2011-09-08 22:01:56 | [diff] [blame] | 158 | : ChromeBrowserMainPartsPosix(parameters) { |
[email protected] | f967b72 | 2011-09-07 00:58:04 | [diff] [blame] | 159 | } |
[email protected] | 1fec6435 | 2010-07-27 13:55:21 | [diff] [blame] | 160 | |
[email protected] | 39fc5d32 | 2012-09-15 10:54:55 | [diff] [blame] | 161 | ChromeBrowserMainPartsMac::~ChromeBrowserMainPartsMac() { |
| 162 | } |
| 163 | |
[email protected] | e6b5bc2 | 2011-09-08 22:01:56 | [diff] [blame] | 164 | void ChromeBrowserMainPartsMac::PreEarlyInitialization() { |
[email protected] | e1972bd | 2012-05-16 20:43:35 | [diff] [blame] | 165 | if (parsed_command_line().HasSwitch(switches::kKeychainReauthorize)) { |
| 166 | if (base::mac::AmIBundled()) { |
| 167 | LOG(FATAL) << "Inappropriate process type for Keychain reauthorization"; |
| 168 | } |
| 169 | |
| 170 | // Do Keychain reauthorization at the time of update installation. This |
| 171 | // gets three chances to run. If the first or second try doesn't complete |
| 172 | // successfully (crashes or is interrupted for any reason), there will be |
| 173 | // another chance. Once this step completes successfully, it should never |
| 174 | // have to run again. |
| 175 | // |
| 176 | // This is kicked off by a special stub executable during an automatic |
[email protected] | c89215835 | 2012-06-06 20:12:46 | [diff] [blame] | 177 | // update. See chrome/installer/mac/keychain_reauthorize_main.cc. |
[email protected] | 70f3204 | 2013-03-10 18:51:49 | [diff] [blame] | 178 | chrome::KeychainReauthorizeIfNeeded(kKeychainReauthorizeAtUpdatePref, |
| 179 | kKeychainReauthorizeAtUpdateMaxTries); |
[email protected] | e1972bd | 2012-05-16 20:43:35 | [diff] [blame] | 180 | |
| 181 | exit(0); |
| 182 | } |
| 183 | |
[email protected] | 37c722b | 2011-09-08 22:18:46 | [diff] [blame] | 184 | ChromeBrowserMainPartsPosix::PreEarlyInitialization(); |
[email protected] | 91484af | 2010-08-27 17:55:54 | [diff] [blame] | 185 | |
[email protected] | f967b72 | 2011-09-07 00:58:04 | [diff] [blame] | 186 | if (base::mac::WasLaunchedAsHiddenLoginItem()) { |
| 187 | CommandLine* singleton_command_line = CommandLine::ForCurrentProcess(); |
| 188 | singleton_command_line->AppendSwitch(switches::kNoStartupWindow); |
| 189 | } |
[email protected] | 4d170e8 | 2012-12-04 00:34:13 | [diff] [blame] | 190 | |
| 191 | RecordCatSixtyFour(); |
[email protected] | f967b72 | 2011-09-07 00:58:04 | [diff] [blame] | 192 | } |
| 193 | |
[email protected] | e6b5bc2 | 2011-09-08 22:01:56 | [diff] [blame] | 194 | void ChromeBrowserMainPartsMac::PreMainMessageLoopStart() { |
[email protected] | 37c722b | 2011-09-08 22:18:46 | [diff] [blame] | 195 | ChromeBrowserMainPartsPosix::PreMainMessageLoopStart(); |
[email protected] | f967b72 | 2011-09-07 00:58:04 | [diff] [blame] | 196 | |
[email protected] | 4764efa | 2013-01-25 17:18:48 | [diff] [blame] | 197 | // Tell Cocoa to finish its initialization, which we want to do manually |
[email protected] | f967b72 | 2011-09-07 00:58:04 | [diff] [blame] | 198 | // instead of calling NSApplicationMain(). The primary reason is that NSAM() |
| 199 | // never returns, which would leave all the objects currently on the stack |
| 200 | // in scoped_ptrs hanging and never cleaned up. We then load the main nib |
| 201 | // directly. The main event loop is run from common code using the |
| 202 | // MessageLoop API, which works out ok for us because it's a wrapper around |
| 203 | // CFRunLoop. |
| 204 | |
| 205 | // Initialize NSApplication using the custom subclass. |
[email protected] | d7de5787 | 2011-12-06 23:32:43 | [diff] [blame] | 206 | chrome_browser_application_mac::RegisterBrowserCrApp(); |
[email protected] | f967b72 | 2011-09-07 00:58:04 | [diff] [blame] | 207 | |
| 208 | // If ui_task is not NULL, the app is actually a browser_test, so startup is |
| 209 | // handled outside of BrowserMain (which is what called this). |
[email protected] | 716476c | 2011-12-29 00:07:03 | [diff] [blame] | 210 | if (!parameters().ui_task) { |
[email protected] | f967b72 | 2011-09-07 00:58:04 | [diff] [blame] | 211 | // The browser process only wants to support the language Cocoa will use, |
| 212 | // so force the app locale to be overriden with that value. |
| 213 | l10n_util::OverrideLocaleWithCocoaLocale(); |
| 214 | |
| 215 | // Before we load the nib, we need to start up the resource bundle so we |
| 216 | // have the strings avaiable for localization. |
| 217 | // TODO(markusheintz): Read preference pref::kApplicationLocale in order |
| 218 | // to enforce the application locale. |
| 219 | const std::string loaded_locale = |
[email protected] | fb317a5 | 2012-05-09 15:57:47 | [diff] [blame] | 220 | ResourceBundle::InitSharedInstanceWithLocale(std::string(), NULL); |
[email protected] | f967b72 | 2011-09-07 00:58:04 | [diff] [blame] | 221 | CHECK(!loaded_locale.empty()) << "Default locale could not be found"; |
| 222 | |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 223 | base::FilePath resources_pack_path; |
[email protected] | f967b72 | 2011-09-07 00:58:04 | [diff] [blame] | 224 | PathService::Get(chrome::FILE_RESOURCES_PACK, &resources_pack_path); |
[email protected] | 6c26d9c | 2012-07-10 19:18:04 | [diff] [blame] | 225 | ResourceBundle::GetSharedInstance().AddDataPackFromPath( |
[email protected] | bf44c13 | 2012-11-15 19:41:42 | [diff] [blame] | 226 | resources_pack_path, ui::SCALE_FACTOR_NONE); |
[email protected] | 91484af | 2010-08-27 17:55:54 | [diff] [blame] | 227 | } |
| 228 | |
[email protected] | c4d501e | 2012-03-27 20:08:02 | [diff] [blame] | 229 | // This is a no-op if the KeystoneRegistration framework is not present. |
| 230 | // The framework is only distributed with branded Google Chrome builds. |
| 231 | [[KeystoneGlue defaultKeystoneGlue] registerWithKeystone]; |
| 232 | |
| 233 | // 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] | 234 | // no first run switches. |
[email protected] | c4d501e | 2012-03-27 20:08:02 | [diff] [blame] | 235 | // |
| 236 | // This needs to be done after the resource bundle is initialized (for |
| 237 | // access to localizations in the UI) and after Keystone is initialized |
| 238 | // (because the installation may need to promote Keystone) but before the |
| 239 | // app controller is set up (and thus before MainMenu.nib is loaded, because |
| 240 | // the app controller assumes that a browser has been set up and will crash |
| 241 | // upon receipt of certain notifications if no browser exists), before |
| 242 | // anyone tries doing anything silly like firing off an import job, and |
| 243 | // before anything creating preferences like Local State in order for the |
| 244 | // relaunched installed application to still consider itself as first-run. |
[email protected] | 0a160a1b | 2013-08-08 22:20:00 | [diff] [blame] | 245 | if (!first_run::IsFirstRunSuppressed(parsed_command_line())) { |
[email protected] | c4d501e | 2012-03-27 20:08:02 | [diff] [blame] | 246 | if (MaybeInstallFromDiskImage()) { |
| 247 | // The application was installed and the installed copy has been |
| 248 | // launched. This process is now obsolete. Exit. |
| 249 | exit(0); |
| 250 | } |
| 251 | } |
| 252 | |
[email protected] | f967b72 | 2011-09-07 00:58:04 | [diff] [blame] | 253 | // Now load the nib (from the right bundle). |
[email protected] | a852203 | 2013-06-24 22:51:46 | [diff] [blame] | 254 | base::scoped_nsobject<NSNib> nib( |
| 255 | [[NSNib alloc] initWithNibNamed:@"MainMenu" |
| 256 | bundle:base::mac::FrameworkBundle()]); |
[email protected] | f967b72 | 2011-09-07 00:58:04 | [diff] [blame] | 257 | // TODO(viettrungluu): crbug.com/20504 - This currently leaks, so if you |
| 258 | // change this, you'll probably need to change the Valgrind suppression. |
| 259 | [nib instantiateNibWithOwner:NSApp topLevelObjects:nil]; |
| 260 | // Make sure the app controller has been created. |
| 261 | DCHECK([NSApp delegate]); |
[email protected] | 1fec6435 | 2010-07-27 13:55:21 | [diff] [blame] | 262 | |
[email protected] | f967b72 | 2011-09-07 00:58:04 | [diff] [blame] | 263 | // Prevent Cocoa from turning command-line arguments into |
| 264 | // |-application:openFiles:|, since we already handle them directly. |
| 265 | [[NSUserDefaults standardUserDefaults] |
| 266 | setObject:@"NO" forKey:@"NSTreatUnknownArgumentsAsOpen"]; |
| 267 | } |
[email protected] | 03d8d3e9 | 2011-09-20 06:07:11 | [diff] [blame] | 268 | |
[email protected] | e8b6ca0 | 2013-07-10 18:00:51 | [diff] [blame] | 269 | void ChromeBrowserMainPartsMac::PostProfileInit() { |
| 270 | ChromeBrowserMainPartsPosix::PostProfileInit(); |
| 271 | g_browser_process->metrics_service()->RecordBreakpadRegistration( |
| 272 | IsCrashReporterEnabled()); |
| 273 | } |
| 274 | |
[email protected] | 03d8d3e9 | 2011-09-20 06:07:11 | [diff] [blame] | 275 | void ChromeBrowserMainPartsMac::DidEndMainMessageLoop() { |
| 276 | AppController* appController = [NSApp delegate]; |
| 277 | [appController didEndMainMessageLoop]; |
| 278 | } |