[email protected] | 71c0eb9 | 2012-01-03 17:57:30 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 5c9587c | 2008-12-09 21:20:16 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | e6b5bc2 | 2011-09-08 22:01:56 | [diff] [blame] | 5 | #include "chrome/browser/chrome_browser_main_mac.h" |
[email protected] | 1152b7e | 2009-09-14 03:26:03 | [diff] [blame] | 6 | |
[email protected] | ce8c16ad | 2009-08-12 19:00:42 | [diff] [blame] | 7 | #import <Cocoa/Cocoa.h> |
[email protected] | 1152b7e | 2009-09-14 03:26:03 | [diff] [blame] | 8 | |
[email protected] | 5c9587c | 2008-12-09 21:20:16 | [diff] [blame] | 9 | #include "base/command_line.h" |
[email protected] | 5858035 | 2010-10-26 04:07:50 | [diff] [blame] | 10 | #include "base/debug/debugger.h" |
[email protected] | e1cb0e9 | 2010-06-15 07:23:59 | [diff] [blame] | 11 | #include "base/file_path.h" |
[email protected] | 2f1804c | 2012-01-19 14:59:07 | [diff] [blame] | 12 | #include "base/mac/bundle_locations.h" |
[email protected] | 0378bf4 | 2011-01-01 18:20:14 | [diff] [blame] | 13 | #include "base/mac/mac_util.h" |
[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 14 | #include "base/memory/scoped_nsobject.h" |
[email protected] | e1cb0e9 | 2010-06-15 07:23:59 | [diff] [blame] | 15 | #include "base/path_service.h" |
[email protected] | 1152b7e | 2009-09-14 03:26:03 | [diff] [blame] | 16 | #include "chrome/app/breakpad_mac.h" |
[email protected] | 3b6aa8b6 | 2009-09-15 21:36:11 | [diff] [blame] | 17 | #import "chrome/browser/app_controller_mac.h" |
[email protected] | aaa47ee | 2009-11-05 21:53:01 | [diff] [blame] | 18 | #import "chrome/browser/chrome_browser_application_mac.h" |
[email protected] | c4d501e | 2012-03-27 20:08:02 | [diff] [blame^] | 19 | #include "chrome/browser/mac/install_from_dmg.h" |
[email protected] | 5950f571 | 2011-06-20 22:15:52 | [diff] [blame] | 20 | #import "chrome/browser/mac/keystone_glue.h" |
[email protected] | 1152b7e | 2009-09-14 03:26:03 | [diff] [blame] | 21 | #include "chrome/browser/metrics/metrics_service.h" |
[email protected] | e1cb0e9 | 2010-06-15 07:23:59 | [diff] [blame] | 22 | #include "chrome/common/chrome_paths.h" |
[email protected] | d7dbe28c | 2010-07-29 04:33:47 | [diff] [blame] | 23 | #include "chrome/common/chrome_switches.h" |
[email protected] | 4573fbd | 2011-10-31 20:25:18 | [diff] [blame] | 24 | #include "content/public/common/main_function_params.h" |
[email protected] | b39ef1cb | 2011-10-25 04:46:55 | [diff] [blame] | 25 | #include "content/public/common/result_codes.h" |
[email protected] | c051a1b | 2011-01-21 23:30:17 | [diff] [blame] | 26 | #include "ui/base/l10n/l10n_util_mac.h" |
[email protected] | 42ce29d | 2011-01-20 23:19:46 | [diff] [blame] | 27 | #include "ui/base/resource/resource_bundle.h" |
[email protected] | 5c9587c | 2008-12-09 21:20:16 | [diff] [blame] | 28 | |
[email protected] | 1152b7e | 2009-09-14 03:26:03 | [diff] [blame] | 29 | void RecordBreakpadStatusUMA(MetricsService* metrics) { |
| 30 | metrics->RecordBreakpadRegistration(IsCrashReporterEnabled()); |
[email protected] | 5858035 | 2010-10-26 04:07:50 | [diff] [blame] | 31 | metrics->RecordBreakpadHasDebugger(base::debug::BeingDebugged()); |
[email protected] | 1152b7e | 2009-09-14 03:26:03 | [diff] [blame] | 32 | } |
| 33 | |
[email protected] | 45d7276 | 2011-04-15 18:58:20 | [diff] [blame] | 34 | void RecordBrowserStartupTime() { |
| 35 | // Not implemented on Mac for now. |
| 36 | } |
| 37 | |
[email protected] | 34f73fb | 2010-03-24 20:50:34 | [diff] [blame] | 38 | void WarnAboutMinimumSystemRequirements() { |
| 39 | // Nothing to check for on Mac right now. |
| 40 | } |
[email protected] | 1bcdb53 | 2009-01-16 17:47:57 | [diff] [blame] | 41 | |
| 42 | // From browser_main_win.h, stubs until we figure out the right thing... |
| 43 | |
[email protected] | 53c38d23 | 2009-02-13 20:52:18 | [diff] [blame] | 44 | int DoUninstallTasks(bool chrome_still_running) { |
[email protected] | 1fcfb20 | 2011-07-19 19:53:14 | [diff] [blame] | 45 | return content::RESULT_CODE_NORMAL_EXIT; |
[email protected] | 1bcdb53 | 2009-01-16 17:47:57 | [diff] [blame] | 46 | } |
| 47 | |
[email protected] | e6b5bc2 | 2011-09-08 22:01:56 | [diff] [blame] | 48 | // ChromeBrowserMainPartsMac --------------------------------------------------- |
[email protected] | 1fec6435 | 2010-07-27 13:55:21 | [diff] [blame] | 49 | |
[email protected] | e6b5bc2 | 2011-09-08 22:01:56 | [diff] [blame] | 50 | ChromeBrowserMainPartsMac::ChromeBrowserMainPartsMac( |
[email protected] | 4573fbd | 2011-10-31 20:25:18 | [diff] [blame] | 51 | const content::MainFunctionParams& parameters) |
[email protected] | e6b5bc2 | 2011-09-08 22:01:56 | [diff] [blame] | 52 | : ChromeBrowserMainPartsPosix(parameters) { |
[email protected] | f967b72 | 2011-09-07 00:58:04 | [diff] [blame] | 53 | } |
[email protected] | 1fec6435 | 2010-07-27 13:55:21 | [diff] [blame] | 54 | |
[email protected] | e6b5bc2 | 2011-09-08 22:01:56 | [diff] [blame] | 55 | void ChromeBrowserMainPartsMac::PreEarlyInitialization() { |
[email protected] | 37c722b | 2011-09-08 22:18:46 | [diff] [blame] | 56 | ChromeBrowserMainPartsPosix::PreEarlyInitialization(); |
[email protected] | 91484af | 2010-08-27 17:55:54 | [diff] [blame] | 57 | |
[email protected] | f967b72 | 2011-09-07 00:58:04 | [diff] [blame] | 58 | if (base::mac::WasLaunchedAsHiddenLoginItem()) { |
| 59 | CommandLine* singleton_command_line = CommandLine::ForCurrentProcess(); |
| 60 | singleton_command_line->AppendSwitch(switches::kNoStartupWindow); |
| 61 | } |
| 62 | } |
| 63 | |
[email protected] | e6b5bc2 | 2011-09-08 22:01:56 | [diff] [blame] | 64 | void ChromeBrowserMainPartsMac::PreMainMessageLoopStart() { |
[email protected] | 37c722b | 2011-09-08 22:18:46 | [diff] [blame] | 65 | ChromeBrowserMainPartsPosix::PreMainMessageLoopStart(); |
[email protected] | f967b72 | 2011-09-07 00:58:04 | [diff] [blame] | 66 | |
| 67 | // Tell Cooca to finish its initialization, which we want to do manually |
| 68 | // instead of calling NSApplicationMain(). The primary reason is that NSAM() |
| 69 | // never returns, which would leave all the objects currently on the stack |
| 70 | // in scoped_ptrs hanging and never cleaned up. We then load the main nib |
| 71 | // directly. The main event loop is run from common code using the |
| 72 | // MessageLoop API, which works out ok for us because it's a wrapper around |
| 73 | // CFRunLoop. |
| 74 | |
| 75 | // Initialize NSApplication using the custom subclass. |
[email protected] | d7de5787 | 2011-12-06 23:32:43 | [diff] [blame] | 76 | chrome_browser_application_mac::RegisterBrowserCrApp(); |
[email protected] | f967b72 | 2011-09-07 00:58:04 | [diff] [blame] | 77 | |
| 78 | // If ui_task is not NULL, the app is actually a browser_test, so startup is |
| 79 | // handled outside of BrowserMain (which is what called this). |
[email protected] | 716476c | 2011-12-29 00:07:03 | [diff] [blame] | 80 | if (!parameters().ui_task) { |
[email protected] | f967b72 | 2011-09-07 00:58:04 | [diff] [blame] | 81 | // The browser process only wants to support the language Cocoa will use, |
| 82 | // so force the app locale to be overriden with that value. |
| 83 | l10n_util::OverrideLocaleWithCocoaLocale(); |
| 84 | |
| 85 | // Before we load the nib, we need to start up the resource bundle so we |
| 86 | // have the strings avaiable for localization. |
| 87 | // TODO(markusheintz): Read preference pref::kApplicationLocale in order |
| 88 | // to enforce the application locale. |
| 89 | const std::string loaded_locale = |
[email protected] | 70f9df1 | 2012-01-28 02:30:04 | [diff] [blame] | 90 | ResourceBundle::InitSharedInstanceWithLocale(std::string()); |
[email protected] | f967b72 | 2011-09-07 00:58:04 | [diff] [blame] | 91 | CHECK(!loaded_locale.empty()) << "Default locale could not be found"; |
| 92 | |
| 93 | FilePath resources_pack_path; |
| 94 | PathService::Get(chrome::FILE_RESOURCES_PACK, &resources_pack_path); |
| 95 | ResourceBundle::AddDataPackToSharedInstance(resources_pack_path); |
[email protected] | 91484af | 2010-08-27 17:55:54 | [diff] [blame] | 96 | } |
| 97 | |
[email protected] | c4d501e | 2012-03-27 20:08:02 | [diff] [blame^] | 98 | // This is a no-op if the KeystoneRegistration framework is not present. |
| 99 | // The framework is only distributed with branded Google Chrome builds. |
| 100 | [[KeystoneGlue defaultKeystoneGlue] registerWithKeystone]; |
| 101 | |
| 102 | // Disk image installation is sort of a first-run task, so it shares the |
| 103 | // kNoFirstRun switch. |
| 104 | // |
| 105 | // This needs to be done after the resource bundle is initialized (for |
| 106 | // access to localizations in the UI) and after Keystone is initialized |
| 107 | // (because the installation may need to promote Keystone) but before the |
| 108 | // app controller is set up (and thus before MainMenu.nib is loaded, because |
| 109 | // the app controller assumes that a browser has been set up and will crash |
| 110 | // upon receipt of certain notifications if no browser exists), before |
| 111 | // anyone tries doing anything silly like firing off an import job, and |
| 112 | // before anything creating preferences like Local State in order for the |
| 113 | // relaunched installed application to still consider itself as first-run. |
| 114 | if (!parsed_command_line().HasSwitch(switches::kNoFirstRun)) { |
| 115 | if (MaybeInstallFromDiskImage()) { |
| 116 | // The application was installed and the installed copy has been |
| 117 | // launched. This process is now obsolete. Exit. |
| 118 | exit(0); |
| 119 | } |
| 120 | } |
| 121 | |
[email protected] | f967b72 | 2011-09-07 00:58:04 | [diff] [blame] | 122 | // Now load the nib (from the right bundle). |
| 123 | scoped_nsobject<NSNib> |
| 124 | nib([[NSNib alloc] initWithNibNamed:@"MainMenu" |
[email protected] | 2f1804c | 2012-01-19 14:59:07 | [diff] [blame] | 125 | bundle:base::mac::FrameworkBundle()]); |
[email protected] | f967b72 | 2011-09-07 00:58:04 | [diff] [blame] | 126 | // TODO(viettrungluu): crbug.com/20504 - This currently leaks, so if you |
| 127 | // change this, you'll probably need to change the Valgrind suppression. |
| 128 | [nib instantiateNibWithOwner:NSApp topLevelObjects:nil]; |
| 129 | // Make sure the app controller has been created. |
| 130 | DCHECK([NSApp delegate]); |
[email protected] | 1fec6435 | 2010-07-27 13:55:21 | [diff] [blame] | 131 | |
[email protected] | f967b72 | 2011-09-07 00:58:04 | [diff] [blame] | 132 | // Prevent Cocoa from turning command-line arguments into |
| 133 | // |-application:openFiles:|, since we already handle them directly. |
| 134 | [[NSUserDefaults standardUserDefaults] |
| 135 | setObject:@"NO" forKey:@"NSTreatUnknownArgumentsAsOpen"]; |
| 136 | } |
[email protected] | 03d8d3e9 | 2011-09-20 06:07:11 | [diff] [blame] | 137 | |
[email protected] | 03d8d3e9 | 2011-09-20 06:07:11 | [diff] [blame] | 138 | void ChromeBrowserMainPartsMac::DidEndMainMessageLoop() { |
| 139 | AppController* appController = [NSApp delegate]; |
| 140 | [appController didEndMainMessageLoop]; |
| 141 | } |