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>
9 #include "base/command_line.h"
10 #include "base/files/file_path.h"
11 #include "base/mac/bundle_locations.h"
12 #include "base/mac/mac_util.h"
13 #include "base/mac/scoped_nsobject.h"
14 #include "base/path_service.h"
15 #import "chrome/browser/app_controller_mac.h"
16 #include "chrome/browser/apps/app_shim/app_shim_host_manager_mac.h"
17 #include "chrome/browser/browser_process.h"
18 #import "chrome/browser/chrome_browser_application_mac.h"
19 #include "chrome/browser/mac/install_from_dmg.h"
20 #import "chrome/browser/mac/keystone_glue.h"
21 #include "chrome/browser/mac/mac_startup_profiler.h"
22 #include "chrome/browser/ui/app_list/app_list_service.h"
23 #include "chrome/common/chrome_paths.h"
24 #include "chrome/common/chrome_switches.h"
25 #include "components/crash/app/crashpad_mac.h"
26 #include "components/metrics/metrics_service.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"
33 // ChromeBrowserMainPartsMac ---------------------------------------------------
35 ChromeBrowserMainPartsMac::ChromeBrowserMainPartsMac(
36 const content::MainFunctionParams& parameters)
37 : ChromeBrowserMainPartsPosix(parameters) {
40 ChromeBrowserMainPartsMac::~ChromeBrowserMainPartsMac() {
43 void ChromeBrowserMainPartsMac::PreEarlyInitialization() {
44 ChromeBrowserMainPartsPosix::PreEarlyInitialization();
46 if (base::mac::WasLaunchedAsLoginItemRestoreState()) {
47 base::CommandLine* singleton_command_line =
48 base::CommandLine::ForCurrentProcess();
49 singleton_command_line->AppendSwitch(switches::kRestoreLastSession);
50 } else if (base::mac::WasLaunchedAsHiddenLoginItem()) {
51 base::CommandLine* singleton_command_line =
52 base::CommandLine::ForCurrentProcess();
53 singleton_command_line->AppendSwitch(switches::kNoStartupWindow);
57 void ChromeBrowserMainPartsMac::PreMainMessageLoopStart() {
58 MacStartupProfiler::GetInstance()->Profile(
59 MacStartupProfiler::PRE_MAIN_MESSAGE_LOOP_START);
60 ChromeBrowserMainPartsPosix::PreMainMessageLoopStart();
62 // Tell Cocoa to finish its initialization, which we want to do manually
63 // instead of calling NSApplicationMain(). The primary reason is that NSAM()
64 // never returns, which would leave all the objects currently on the stack
65 // in scoped_ptrs hanging and never cleaned up. We then load the main nib
66 // directly. The main event loop is run from common code using the
67 // MessageLoop API, which works out ok for us because it's a wrapper around
70 // Initialize NSApplication using the custom subclass.
71 chrome_browser_application_mac::RegisterBrowserCrApp();
73 // If ui_task is not NULL, the app is actually a browser_test.
74 if (!parameters().ui_task) {
75 // The browser process only wants to support the language Cocoa will use,
76 // so force the app locale to be overriden with that value.
77 l10n_util::OverrideLocaleWithCocoaLocale();
80 // Before we load the nib, we need to start up the resource bundle so we
81 // have the strings avaiable for localization.
82 // TODO(markusheintz): Read preference pref::kApplicationLocale in order
83 // to enforce the application locale.
84 const std::string loaded_locale =
85 ui::ResourceBundle::InitSharedInstanceWithLocale(
86 std::string(), NULL, ui::ResourceBundle::LOAD_COMMON_RESOURCES);
87 CHECK(!loaded_locale.empty()) << "Default locale could not be found";
89 base::FilePath resources_pack_path;
90 PathService::Get(chrome::FILE_RESOURCES_PACK, &resources_pack_path);
91 ResourceBundle::GetSharedInstance().AddDataPackFromPath(
92 resources_pack_path, ui::SCALE_FACTOR_NONE);
94 // This is a no-op if the KeystoneRegistration framework is not present.
95 // The framework is only distributed with branded Google Chrome builds.
96 [[KeystoneGlue defaultKeystoneGlue] registerWithKeystone];
98 // Disk image installation is sort of a first-run task, so it shares the
99 // no first run switches.
101 // This needs to be done after the resource bundle is initialized (for
102 // access to localizations in the UI) and after Keystone is initialized
103 // (because the installation may need to promote Keystone) but before the
104 // app controller is set up (and thus before MainMenu.nib is loaded, because
105 // the app controller assumes that a browser has been set up and will crash
106 // upon receipt of certain notifications if no browser exists), before
107 // anyone tries doing anything silly like firing off an import job, and
108 // before anything creating preferences like Local State in order for the
109 // relaunched installed application to still consider itself as first-run.
110 if (!first_run::IsFirstRunSuppressed(parsed_command_line())) {
111 if (MaybeInstallFromDiskImage()) {
112 // The application was installed and the installed copy has been
113 // launched. This process is now obsolete. Exit.
118 // Now load the nib (from the right bundle).
119 base::scoped_nsobject<NSNib> nib(
120 [[NSNib alloc] initWithNibNamed:@"MainMenu"
121 bundle:base::mac::FrameworkBundle()]);
122 // TODO(viettrungluu): crbug.com/20504 - This currently leaks, so if you
123 // change this, you'll probably need to change the Valgrind suppression.
124 [nib instantiateNibWithOwner:NSApp topLevelObjects:nil];
125 // Make sure the app controller has been created.
126 DCHECK([NSApp delegate]);
128 [[NSUserDefaults standardUserDefaults] registerDefaults:@{
129 // Prevent Cocoa from turning command-line arguments into
130 // |-application:openFiles:|, since we already handle them directly.
131 // @"NO" looks like a mistake, but the value really is supposed to be a
133 @"NSTreatUnknownArgumentsAsOpen": @"NO",
134 // CoreAnimation has poor performance and CoreAnimation and
135 // non-CoreAnimation exhibit window flickering when layers are not hosted
136 // in the window server, which is the default when not not using the
138 // TODO: Remove this when we build with the 10.9 SDK.
139 @"NSWindowHostsLayersInWindowServer":
140 @(base::mac::IsOSMavericksOrLater()),
141 // This setting prevents views from ditching their layers when the view
142 // gets removed from the view hierarchy. It defaults to YES for
143 // applications linked against an OSX 10.8+ SDK. In Yosemite, failing to
144 // set this to YES causes an AppKit crash. http://crbug.com/428977
145 // TODO(erikchen): Remove this when we build with an OSX 10.8+ SDK.
146 @"NSViewKeepLayersAround": @(YES)
150 void ChromeBrowserMainPartsMac::PostMainMessageLoopStart() {
151 MacStartupProfiler::GetInstance()->Profile(
152 MacStartupProfiler::POST_MAIN_MESSAGE_LOOP_START);
153 ChromeBrowserMainPartsPosix::PostMainMessageLoopStart();
156 void ChromeBrowserMainPartsMac::PreProfileInit() {
157 MacStartupProfiler::GetInstance()->Profile(
158 MacStartupProfiler::PRE_PROFILE_INIT);
159 ChromeBrowserMainPartsPosix::PreProfileInit();
161 // This is called here so that the app shim socket is only created after
162 // taking the singleton lock.
163 g_browser_process->platform_part()->app_shim_host_manager()->Init();
164 AppListService::InitAll(NULL,
165 GetStartupProfilePath(user_data_dir(), parsed_command_line()));
168 void ChromeBrowserMainPartsMac::PostProfileInit() {
169 MacStartupProfiler::GetInstance()->Profile(
170 MacStartupProfiler::POST_PROFILE_INIT);
171 ChromeBrowserMainPartsPosix::PostProfileInit();
173 g_browser_process->metrics_service()->RecordBreakpadRegistration(
174 crash_reporter::GetUploadsEnabled());
177 void ChromeBrowserMainPartsMac::DidEndMainMessageLoop() {
178 AppController* appController = [NSApp delegate];
179 [appController didEndMainMessageLoop];