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