blob: 2dc68c85646d84bccf8cb91d1eeb6ba0025563fe [file] [log] [blame]
[email protected]71c0eb92012-01-03 17:57:301// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]5c9587c2008-12-09 21:20:162// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]e6b5bc22011-09-08 22:01:565#include "chrome/browser/chrome_browser_main_mac.h"
[email protected]1152b7e2009-09-14 03:26:036
[email protected]ce8c16ad2009-08-12 19:00:427#import <Cocoa/Cocoa.h>
[email protected]4d170e82012-12-04 00:34:138#include <sys/sysctl.h>
[email protected]1152b7e2009-09-14 03:26:039
[email protected]5c9587c2008-12-09 21:20:1610#include "base/command_line.h"
[email protected]57999812013-02-24 05:40:5211#include "base/files/file_path.h"
[email protected]2f1804c2012-01-19 14:59:0712#include "base/mac/bundle_locations.h"
[email protected]0378bf42011-01-01 18:20:1413#include "base/mac/mac_util.h"
[email protected]a8522032013-06-24 22:51:4614#include "base/mac/scoped_nsobject.h"
[email protected]4d170e82012-12-04 00:34:1315#include "base/metrics/histogram.h"
[email protected]e1cb0e92010-06-15 07:23:5916#include "base/path_service.h"
[email protected]3b6aa8b62009-09-15 21:36:1117#import "chrome/browser/app_controller_mac.h"
tapted63829f72014-09-24 23:50:5018#include "chrome/browser/apps/app_shim/app_shim_host_manager_mac.h"
[email protected]e8b6ca02013-07-10 18:00:5119#include "chrome/browser/browser_process.h"
[email protected]aaa47ee2009-11-05 21:53:0120#import "chrome/browser/chrome_browser_application_mac.h"
[email protected]c4d501e2012-03-27 20:08:0221#include "chrome/browser/mac/install_from_dmg.h"
[email protected]5950f5712011-06-20 22:15:5222#import "chrome/browser/mac/keystone_glue.h"
[email protected]7d1aaa62014-07-18 02:21:3023#include "chrome/browser/mac/mac_startup_profiler.h"
[email protected]bdad6e62014-05-06 08:47:3724#include "chrome/browser/ui/app_list/app_list_service.h"
[email protected]e1cb0e92010-06-15 07:23:5925#include "chrome/common/chrome_paths.h"
[email protected]d7dbe28c2010-07-29 04:33:4726#include "chrome/common/chrome_switches.h"
Robert Sesekabcd8102014-08-27 16:12:4427#include "components/crash/app/breakpad_mac.h"
[email protected]d6147bd2014-06-11 01:58:1928#include "components/metrics/metrics_service.h"
[email protected]4573fbd2011-10-31 20:25:1829#include "content/public/common/main_function_params.h"
[email protected]b39ef1cb2011-10-25 04:46:5530#include "content/public/common/result_codes.h"
[email protected]c051a1b2011-01-21 23:30:1731#include "ui/base/l10n/l10n_util_mac.h"
[email protected]42ce29d2011-01-20 23:19:4632#include "ui/base/resource/resource_bundle.h"
[email protected]63942f22012-05-01 06:11:5233#include "ui/base/resource/resource_handle.h"
[email protected]5c9587c2008-12-09 21:20:1634
[email protected]e1972bd2012-05-16 20:43:3535namespace {
36
[email protected]4d170e82012-12-04 00:34:1337// This is one enum instead of two so that the values can be correlated in a
38// histogram.
39enum CatSixtyFour {
40 // Older than any expected cat.
41 SABER_TOOTHED_CAT_32 = 0,
42 SABER_TOOTHED_CAT_64,
43
44 // Known cats.
45 SNOW_LEOPARD_32,
46 SNOW_LEOPARD_64,
47 LION_32, // Unexpected, Lion requires a 64-bit CPU.
48 LION_64,
49 MOUNTAIN_LION_32, // Unexpected, Mountain Lion requires a 64-bit CPU.
50 MOUNTAIN_LION_64,
[email protected]aaf93de42013-10-02 07:41:5751 MAVERICKS_32, // Unexpected, Mavericks requires a 64-bit CPU.
52 MAVERICKS_64,
[email protected]4d170e82012-12-04 00:34:1353
54 // DON'T add new constants here. It's important to keep the constant values,
55 // um, constant. Add new constants at the bottom.
56
[email protected]4d170e82012-12-04 00:34:1357 // What if the bitsiness of the CPU can't be determined?
58 SABER_TOOTHED_CAT_DUNNO,
59 SNOW_LEOPARD_DUNNO,
60 LION_DUNNO,
61 MOUNTAIN_LION_DUNNO,
[email protected]aaf93de42013-10-02 07:41:5762 MAVERICKS_DUNNO,
63
[email protected]81ceb2b2014-06-12 18:28:1964 // More known cats.
65 YOSEMITE_32, // Unexpected, Yosemite requires a 64-bit CPU.
66 YOSEMITE_64,
67 YOSEMITE_DUNNO,
68
[email protected]aaf93de42013-10-02 07:41:5769 // Newer than any known cat.
70 FUTURE_CAT_32, // Unexpected, it's unlikely Apple will un-obsolete old CPUs.
71 FUTURE_CAT_64,
[email protected]4d170e82012-12-04 00:34:1372 FUTURE_CAT_DUNNO,
73
[email protected]aaf93de42013-10-02 07:41:5774 // As new versions of Mac OS X are released with sillier and sillier names,
75 // rename the FUTURE_CAT enum values to match those names, and re-create
76 // FUTURE_CAT_[32|64|DUNNO] here.
[email protected]4d170e82012-12-04 00:34:1377
78 CAT_SIXTY_FOUR_MAX
79};
80
81CatSixtyFour CatSixtyFourValue() {
82#if defined(ARCH_CPU_64_BITS)
83 // If 64-bit code is running, then it's established that this CPU can run
84 // 64-bit code, and no further inquiry is necessary.
85 int cpu64 = 1;
86 bool cpu64_known = true;
87#else
88 // Check a sysctl conveniently provided by the kernel that identifies
89 // whether the CPU supports 64-bit operation. Note that this tests the
90 // actual hardware capabilities, not the bitsiness of the running process,
91 // and not the bitsiness of the running kernel. The value thus determines
92 // whether the CPU is capable of running 64-bit programs (in the presence of
93 // proper OS runtime support) without regard to whether the current program
94 // is 64-bit (it may not be) or whether the current kernel is (the kernel
95 // can launch cross-bitted user-space tasks).
96
97 int cpu64;
98 size_t len = sizeof(cpu64);
99 const char kSysctlName[] = "hw.cpu64bit_capable";
100 bool cpu64_known = sysctlbyname(kSysctlName, &cpu64, &len, NULL, 0) == 0;
101 if (!cpu64_known) {
102 PLOG(WARNING) << "sysctlbyname(\"" << kSysctlName << "\")";
103 }
104#endif
105
106 if (base::mac::IsOSSnowLeopard()) {
107 return cpu64_known ? (cpu64 ? SNOW_LEOPARD_64 : SNOW_LEOPARD_32) :
108 SNOW_LEOPARD_DUNNO;
109 }
110 if (base::mac::IsOSLion()) {
111 return cpu64_known ? (cpu64 ? LION_64 : LION_32) :
112 LION_DUNNO;
113 }
114 if (base::mac::IsOSMountainLion()) {
115 return cpu64_known ? (cpu64 ? MOUNTAIN_LION_64 : MOUNTAIN_LION_32) :
116 MOUNTAIN_LION_DUNNO;
117 }
[email protected]aaf93de42013-10-02 07:41:57118 if (base::mac::IsOSMavericks()) {
119 return cpu64_known ? (cpu64 ? MAVERICKS_64 : MAVERICKS_32) :
120 MAVERICKS_DUNNO;
121 }
[email protected]81ceb2b2014-06-12 18:28:19122 if (base::mac::IsOSYosemite()) {
123 return cpu64_known ? (cpu64 ? YOSEMITE_64 : YOSEMITE_32) :
124 YOSEMITE_DUNNO;
125 }
126 if (base::mac::IsOSLaterThanYosemite_DontCallThis()) {
[email protected]4d170e82012-12-04 00:34:13127 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
137void RecordCatSixtyFour() {
138 CatSixtyFour cat_sixty_four = CatSixtyFourValue();
139
asvitkinec0fb8022014-08-26 04:39:35140 // Set this higher than the highest value in the CatSixtyFour enum to provide
141 // some headroom and then leave it alone. See UMA_HISTOGRAM_ENUMERATION in
142 // base/metrics/histogram.h.
[email protected]4d170e82012-12-04 00:34:13143 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]e1972bd2012-05-16 20:43:35152} // namespace
153
[email protected]e6b5bc22011-09-08 22:01:56154// ChromeBrowserMainPartsMac ---------------------------------------------------
[email protected]1fec64352010-07-27 13:55:21155
[email protected]e6b5bc22011-09-08 22:01:56156ChromeBrowserMainPartsMac::ChromeBrowserMainPartsMac(
[email protected]4573fbd2011-10-31 20:25:18157 const content::MainFunctionParams& parameters)
[email protected]e6b5bc22011-09-08 22:01:56158 : ChromeBrowserMainPartsPosix(parameters) {
[email protected]f967b722011-09-07 00:58:04159}
[email protected]1fec64352010-07-27 13:55:21160
[email protected]39fc5d322012-09-15 10:54:55161ChromeBrowserMainPartsMac::~ChromeBrowserMainPartsMac() {
162}
163
[email protected]e6b5bc22011-09-08 22:01:56164void ChromeBrowserMainPartsMac::PreEarlyInitialization() {
[email protected]37c722b2011-09-08 22:18:46165 ChromeBrowserMainPartsPosix::PreEarlyInitialization();
[email protected]91484af2010-08-27 17:55:54166
[email protected]769ddfe2014-06-13 23:13:20167 if (base::mac::WasLaunchedAsLoginItemRestoreState()) {
avi3ef9ec9e2014-12-22 22:50:17168 base::CommandLine* singleton_command_line =
169 base::CommandLine::ForCurrentProcess();
[email protected]769ddfe2014-06-13 23:13:20170 singleton_command_line->AppendSwitch(switches::kRestoreLastSession);
171 } else if (base::mac::WasLaunchedAsHiddenLoginItem()) {
avi3ef9ec9e2014-12-22 22:50:17172 base::CommandLine* singleton_command_line =
173 base::CommandLine::ForCurrentProcess();
[email protected]f967b722011-09-07 00:58:04174 singleton_command_line->AppendSwitch(switches::kNoStartupWindow);
175 }
[email protected]4d170e82012-12-04 00:34:13176
177 RecordCatSixtyFour();
[email protected]f967b722011-09-07 00:58:04178}
179
[email protected]e6b5bc22011-09-08 22:01:56180void ChromeBrowserMainPartsMac::PreMainMessageLoopStart() {
[email protected]7d1aaa62014-07-18 02:21:30181 MacStartupProfiler::GetInstance()->Profile(
182 MacStartupProfiler::PRE_MAIN_MESSAGE_LOOP_START);
[email protected]37c722b2011-09-08 22:18:46183 ChromeBrowserMainPartsPosix::PreMainMessageLoopStart();
[email protected]f967b722011-09-07 00:58:04184
[email protected]4764efa2013-01-25 17:18:48185 // Tell Cocoa to finish its initialization, which we want to do manually
[email protected]f967b722011-09-07 00:58:04186 // instead of calling NSApplicationMain(). The primary reason is that NSAM()
187 // never returns, which would leave all the objects currently on the stack
188 // in scoped_ptrs hanging and never cleaned up. We then load the main nib
189 // directly. The main event loop is run from common code using the
190 // MessageLoop API, which works out ok for us because it's a wrapper around
191 // CFRunLoop.
192
193 // Initialize NSApplication using the custom subclass.
[email protected]d7de57872011-12-06 23:32:43194 chrome_browser_application_mac::RegisterBrowserCrApp();
[email protected]f967b722011-09-07 00:58:04195
[email protected]a88f6362014-03-18 04:25:35196 // If ui_task is not NULL, the app is actually a browser_test.
[email protected]716476c2011-12-29 00:07:03197 if (!parameters().ui_task) {
[email protected]f967b722011-09-07 00:58:04198 // The browser process only wants to support the language Cocoa will use,
199 // so force the app locale to be overriden with that value.
200 l10n_util::OverrideLocaleWithCocoaLocale();
[email protected]91484af2010-08-27 17:55:54201 }
202
[email protected]a88f6362014-03-18 04:25:35203 // Before we load the nib, we need to start up the resource bundle so we
204 // have the strings avaiable for localization.
205 // TODO(markusheintz): Read preference pref::kApplicationLocale in order
206 // to enforce the application locale.
207 const std::string loaded_locale =
[email protected]4ce2aa62014-08-14 01:05:49208 ui::ResourceBundle::InitSharedInstanceWithLocale(
209 std::string(), NULL, ui::ResourceBundle::LOAD_COMMON_RESOURCES);
[email protected]a88f6362014-03-18 04:25:35210 CHECK(!loaded_locale.empty()) << "Default locale could not be found";
211
212 base::FilePath resources_pack_path;
213 PathService::Get(chrome::FILE_RESOURCES_PACK, &resources_pack_path);
214 ResourceBundle::GetSharedInstance().AddDataPackFromPath(
215 resources_pack_path, ui::SCALE_FACTOR_NONE);
216
[email protected]c4d501e2012-03-27 20:08:02217 // This is a no-op if the KeystoneRegistration framework is not present.
218 // The framework is only distributed with branded Google Chrome builds.
219 [[KeystoneGlue defaultKeystoneGlue] registerWithKeystone];
220
221 // Disk image installation is sort of a first-run task, so it shares the
[email protected]0a160a1b2013-08-08 22:20:00222 // no first run switches.
[email protected]c4d501e2012-03-27 20:08:02223 //
224 // This needs to be done after the resource bundle is initialized (for
225 // access to localizations in the UI) and after Keystone is initialized
226 // (because the installation may need to promote Keystone) but before the
227 // app controller is set up (and thus before MainMenu.nib is loaded, because
228 // the app controller assumes that a browser has been set up and will crash
229 // upon receipt of certain notifications if no browser exists), before
230 // anyone tries doing anything silly like firing off an import job, and
231 // before anything creating preferences like Local State in order for the
232 // relaunched installed application to still consider itself as first-run.
[email protected]0a160a1b2013-08-08 22:20:00233 if (!first_run::IsFirstRunSuppressed(parsed_command_line())) {
[email protected]c4d501e2012-03-27 20:08:02234 if (MaybeInstallFromDiskImage()) {
235 // The application was installed and the installed copy has been
236 // launched. This process is now obsolete. Exit.
237 exit(0);
238 }
239 }
240
[email protected]f967b722011-09-07 00:58:04241 // Now load the nib (from the right bundle).
[email protected]a8522032013-06-24 22:51:46242 base::scoped_nsobject<NSNib> nib(
243 [[NSNib alloc] initWithNibNamed:@"MainMenu"
244 bundle:base::mac::FrameworkBundle()]);
[email protected]f967b722011-09-07 00:58:04245 // TODO(viettrungluu): crbug.com/20504 - This currently leaks, so if you
246 // change this, you'll probably need to change the Valgrind suppression.
247 [nib instantiateNibWithOwner:NSApp topLevelObjects:nil];
248 // Make sure the app controller has been created.
249 DCHECK([NSApp delegate]);
[email protected]1fec64352010-07-27 13:55:21250
[email protected]47077b52014-03-11 15:25:59251 [[NSUserDefaults standardUserDefaults] registerDefaults:@{
252 // Prevent Cocoa from turning command-line arguments into
253 // |-application:openFiles:|, since we already handle them directly.
254 // @"NO" looks like a mistake, but the value really is supposed to be a
255 // string.
256 @"NSTreatUnknownArgumentsAsOpen": @"NO",
257 // CoreAnimation has poor performance and CoreAnimation and
258 // non-CoreAnimation exhibit window flickering when layers are not hosted
259 // in the window server, which is the default when not not using the
260 // 10.9 SDK.
261 // TODO: Remove this when we build with the 10.9 SDK.
262 @"NSWindowHostsLayersInWindowServer": @(base::mac::IsOSMavericksOrLater())
263 }];
[email protected]f967b722011-09-07 00:58:04264}
[email protected]03d8d3e92011-09-20 06:07:11265
[email protected]7d1aaa62014-07-18 02:21:30266void ChromeBrowserMainPartsMac::PostMainMessageLoopStart() {
267 MacStartupProfiler::GetInstance()->Profile(
268 MacStartupProfiler::POST_MAIN_MESSAGE_LOOP_START);
[email protected]6ddc10972014-07-29 07:53:33269 ChromeBrowserMainPartsPosix::PostMainMessageLoopStart();
[email protected]7d1aaa62014-07-18 02:21:30270}
271
[email protected]bdad6e62014-05-06 08:47:37272void ChromeBrowserMainPartsMac::PreProfileInit() {
[email protected]7d1aaa62014-07-18 02:21:30273 MacStartupProfiler::GetInstance()->Profile(
274 MacStartupProfiler::PRE_PROFILE_INIT);
[email protected]bdad6e62014-05-06 08:47:37275 ChromeBrowserMainPartsPosix::PreProfileInit();
276 // This is called here so that the app shim socket is only created after
277 // taking the singleton lock.
278 g_browser_process->platform_part()->app_shim_host_manager()->Init();
279 AppListService::InitAll(NULL);
280}
281
[email protected]e8b6ca02013-07-10 18:00:51282void ChromeBrowserMainPartsMac::PostProfileInit() {
[email protected]7d1aaa62014-07-18 02:21:30283 MacStartupProfiler::GetInstance()->Profile(
284 MacStartupProfiler::POST_PROFILE_INIT);
[email protected]e8b6ca02013-07-10 18:00:51285 ChromeBrowserMainPartsPosix::PostProfileInit();
286 g_browser_process->metrics_service()->RecordBreakpadRegistration(
[email protected]73929052013-10-11 12:22:09287 breakpad::IsCrashReporterEnabled());
[email protected]e8b6ca02013-07-10 18:00:51288}
289
[email protected]03d8d3e92011-09-20 06:07:11290void ChromeBrowserMainPartsMac::DidEndMainMessageLoop() {
291 AppController* appController = [NSApp delegate];
292 [appController didEndMainMessageLoop];
293}