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.
5 #include "chrome/browser/chrome_browser_main_mac.h"
7 #import <Cocoa/Cocoa.h>
8 #include <sys/sysctl.h>
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 #import "chrome/browser/app_controller_mac.h"
18 #include "chrome/browser/browser_process.h"
19 #import "chrome/browser/chrome_browser_application_mac.h"
20 #include "chrome/browser/mac/install_from_dmg.h"
21 #import "chrome/browser/mac/keystone_glue.h"
22 #include "chrome/browser/metrics/metrics_service.h"
23 #include "chrome/common/chrome_paths.h"
24 #include "chrome/common/chrome_switches.h"
25 #include "components/breakpad/app/breakpad_mac.h"
26 #include "content/public/common/main_function_params.h"
27 #include "content/public/common/result_codes.h"
28 #include "ui/base/l10n/l10n_util_mac.h"
29 #include "ui/base/resource/resource_bundle.h"
30 #include "ui/base/resource/resource_handle.h"
34 // This is one enum instead of two so that the values can be correlated in a
37 // Older than any expected cat.
38 SABER_TOOTHED_CAT_32 = 0,
44 LION_32, // Unexpected, Lion requires a 64-bit CPU.
46 MOUNTAIN_LION_32, // Unexpected, Mountain Lion requires a 64-bit CPU.
48 MAVERICKS_32, // Unexpected, Mavericks requires a 64-bit CPU.
51 // DON'T add new constants here. It's important to keep the constant values,
52 // um, constant. Add new constants at the bottom.
54 // What if the bitsiness of the CPU can't be determined?
55 SABER_TOOTHED_CAT_DUNNO,
61 // Newer than any known cat.
62 FUTURE_CAT_32, // Unexpected, it's unlikely Apple will un-obsolete old CPUs.
66 // As new versions of Mac OS X are released with sillier and sillier names,
67 // rename the FUTURE_CAT enum values to match those names, and re-create
68 // FUTURE_CAT_[32|64|DUNNO] here.
73 CatSixtyFour CatSixtyFourValue() {
74 #if defined(ARCH_CPU_64_BITS)
75 // If 64-bit code is running, then it's established that this CPU can run
76 // 64-bit code, and no further inquiry is necessary.
78 bool cpu64_known = true;
80 // Check a sysctl conveniently provided by the kernel that identifies
81 // whether the CPU supports 64-bit operation. Note that this tests the
82 // actual hardware capabilities, not the bitsiness of the running process,
83 // and not the bitsiness of the running kernel. The value thus determines
84 // whether the CPU is capable of running 64-bit programs (in the presence of
85 // proper OS runtime support) without regard to whether the current program
86 // is 64-bit (it may not be) or whether the current kernel is (the kernel
87 // can launch cross-bitted user-space tasks).
90 size_t len = sizeof(cpu64);
91 const char kSysctlName[] = "hw.cpu64bit_capable";
92 bool cpu64_known = sysctlbyname(kSysctlName, &cpu64, &len, NULL, 0) == 0;
94 PLOG(WARNING) << "sysctlbyname(\"" << kSysctlName << "\")";
98 if (base::mac::IsOSSnowLeopard()) {
99 return cpu64_known ? (cpu64 ? SNOW_LEOPARD_64 : SNOW_LEOPARD_32) :
102 if (base::mac::IsOSLion()) {
103 return cpu64_known ? (cpu64 ? LION_64 : LION_32) :
106 if (base::mac::IsOSMountainLion()) {
107 return cpu64_known ? (cpu64 ? MOUNTAIN_LION_64 : MOUNTAIN_LION_32) :
110 if (base::mac::IsOSMavericks()) {
111 return cpu64_known ? (cpu64 ? MAVERICKS_64 : MAVERICKS_32) :
114 if (base::mac::IsOSLaterThanMavericks_DontCallThis()) {
115 return cpu64_known ? (cpu64 ? FUTURE_CAT_64 : FUTURE_CAT_32) :
119 // If it's not any of the expected OS versions or later than them, it must
121 return cpu64_known ? (cpu64 ? SABER_TOOTHED_CAT_64 : SABER_TOOTHED_CAT_32) :
122 SABER_TOOTHED_CAT_DUNNO;
125 void RecordCatSixtyFour() {
126 CatSixtyFour cat_sixty_four = CatSixtyFourValue();
128 // Set this higher than the highest value in the CatSixtyFour enum to
129 // provide some headroom and then leave it alone. See HISTOGRAM_ENUMERATION
130 // in base/metrics/histogram.h.
131 const int kMaxCatsAndSixtyFours = 32;
132 COMPILE_ASSERT(kMaxCatsAndSixtyFours >= CAT_SIXTY_FOUR_MAX,
133 CatSixtyFour_enum_grew_too_large);
135 UMA_HISTOGRAM_ENUMERATION("OSX.CatSixtyFour",
137 kMaxCatsAndSixtyFours);
142 // ChromeBrowserMainPartsMac ---------------------------------------------------
144 ChromeBrowserMainPartsMac::ChromeBrowserMainPartsMac(
145 const content::MainFunctionParams& parameters)
146 : ChromeBrowserMainPartsPosix(parameters) {
149 ChromeBrowserMainPartsMac::~ChromeBrowserMainPartsMac() {
152 void ChromeBrowserMainPartsMac::PreEarlyInitialization() {
153 ChromeBrowserMainPartsPosix::PreEarlyInitialization();
155 if (base::mac::WasLaunchedAsHiddenLoginItem()) {
156 CommandLine* singleton_command_line = CommandLine::ForCurrentProcess();
157 singleton_command_line->AppendSwitch(switches::kNoStartupWindow);
160 RecordCatSixtyFour();
163 void ChromeBrowserMainPartsMac::PreMainMessageLoopStart() {
164 ChromeBrowserMainPartsPosix::PreMainMessageLoopStart();
166 // Tell Cocoa to finish its initialization, which we want to do manually
167 // instead of calling NSApplicationMain(). The primary reason is that NSAM()
168 // never returns, which would leave all the objects currently on the stack
169 // in scoped_ptrs hanging and never cleaned up. We then load the main nib
170 // directly. The main event loop is run from common code using the
171 // MessageLoop API, which works out ok for us because it's a wrapper around
174 // Initialize NSApplication using the custom subclass.
175 chrome_browser_application_mac::RegisterBrowserCrApp();
177 // If ui_task is not NULL, the app is actually a browser_test.
178 if (!parameters().ui_task) {
179 // The browser process only wants to support the language Cocoa will use,
180 // so force the app locale to be overriden with that value.
181 l10n_util::OverrideLocaleWithCocoaLocale();
184 // Before we load the nib, we need to start up the resource bundle so we
185 // have the strings avaiable for localization.
186 // TODO(markusheintz): Read preference pref::kApplicationLocale in order
187 // to enforce the application locale.
188 const std::string loaded_locale =
189 ResourceBundle::InitSharedInstanceWithLocale(std::string(), NULL);
190 CHECK(!loaded_locale.empty()) << "Default locale could not be found";
192 base::FilePath resources_pack_path;
193 PathService::Get(chrome::FILE_RESOURCES_PACK, &resources_pack_path);
194 ResourceBundle::GetSharedInstance().AddDataPackFromPath(
195 resources_pack_path, ui::SCALE_FACTOR_NONE);
197 // This is a no-op if the KeystoneRegistration framework is not present.
198 // The framework is only distributed with branded Google Chrome builds.
199 [[KeystoneGlue defaultKeystoneGlue] registerWithKeystone];
201 // Disk image installation is sort of a first-run task, so it shares the
202 // no first run switches.
204 // This needs to be done after the resource bundle is initialized (for
205 // access to localizations in the UI) and after Keystone is initialized
206 // (because the installation may need to promote Keystone) but before the
207 // app controller is set up (and thus before MainMenu.nib is loaded, because
208 // the app controller assumes that a browser has been set up and will crash
209 // upon receipt of certain notifications if no browser exists), before
210 // anyone tries doing anything silly like firing off an import job, and
211 // before anything creating preferences like Local State in order for the
212 // relaunched installed application to still consider itself as first-run.
213 if (!first_run::IsFirstRunSuppressed(parsed_command_line())) {
214 if (MaybeInstallFromDiskImage()) {
215 // The application was installed and the installed copy has been
216 // launched. This process is now obsolete. Exit.
221 // Now load the nib (from the right bundle).
222 base::scoped_nsobject<NSNib> nib(
223 [[NSNib alloc] initWithNibNamed:@"MainMenu"
224 bundle:base::mac::FrameworkBundle()]);
225 // TODO(viettrungluu): crbug.com/20504 - This currently leaks, so if you
226 // change this, you'll probably need to change the Valgrind suppression.
227 [nib instantiateNibWithOwner:NSApp topLevelObjects:nil];
228 // Make sure the app controller has been created.
229 DCHECK([NSApp delegate]);
231 [[NSUserDefaults standardUserDefaults] registerDefaults:@{
232 // Prevent Cocoa from turning command-line arguments into
233 // |-application:openFiles:|, since we already handle them directly.
234 // @"NO" looks like a mistake, but the value really is supposed to be a
236 @"NSTreatUnknownArgumentsAsOpen": @"NO",
237 // CoreAnimation has poor performance and CoreAnimation and
238 // non-CoreAnimation exhibit window flickering when layers are not hosted
239 // in the window server, which is the default when not not using the
241 // TODO: Remove this when we build with the 10.9 SDK.
242 @"NSWindowHostsLayersInWindowServer": @(base::mac::IsOSMavericksOrLater())
246 void ChromeBrowserMainPartsMac::PostProfileInit() {
247 ChromeBrowserMainPartsPosix::PostProfileInit();
248 g_browser_process->metrics_service()->RecordBreakpadRegistration(
249 breakpad::IsCrashReporterEnabled());
252 void ChromeBrowserMainPartsMac::DidEndMainMessageLoop() {
253 AppController* appController = [NSApp delegate];
254 [appController didEndMainMessageLoop];