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