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