| // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file. |
| |
| #include "chrome/browser/chrome_browser_main_mac.h" |
| |
| #import <Cocoa/Cocoa.h> |
| |
| #include "base/command_line.h" |
| #include "base/files/file_path.h" |
| #include "base/files/file_util.h" |
| #include "base/mac/bundle_locations.h" |
| #include "base/mac/mac_util.h" |
| #include "base/mac/scoped_nsobject.h" |
| #include "base/path_service.h" |
| #include "base/thread_task_runner_handle.h" |
| #import "chrome/browser/app_controller_mac.h" |
| #include "chrome/browser/apps/app_shim/app_shim_host_manager_mac.h" |
| #include "chrome/browser/browser_process.h" |
| #import "chrome/browser/chrome_browser_application_mac.h" |
| #include "chrome/browser/mac/install_from_dmg.h" |
| #import "chrome/browser/mac/keystone_glue.h" |
| #include "chrome/browser/mac/mac_startup_profiler.h" |
| #include "chrome/browser/ui/app_list/app_list_service.h" |
| #include "chrome/common/chrome_paths.h" |
| #include "chrome/common/chrome_switches.h" |
| #include "components/crash/content/app/crashpad.h" |
| #include "components/metrics/metrics_service.h" |
| #include "content/public/common/main_function_params.h" |
| #include "content/public/common/result_codes.h" |
| #include "ui/base/l10n/l10n_util_mac.h" |
| #include "ui/base/resource/resource_bundle.h" |
| #include "ui/base/resource/resource_handle.h" |
| |
| namespace { |
| |
| // Writes an undocumented sentinel file that prevents Spotlight from indexing |
| // below a particular path in order to reap some power savings. |
| void EnsureMetadataNeverIndexFileOnFileThread( |
| const base::FilePath& user_data_dir) { |
| const char kMetadataNeverIndexFilename[] = ".metadata_never_index"; |
| base::FilePath metadata_file_path = |
| user_data_dir.Append(kMetadataNeverIndexFilename); |
| if (base::PathExists(metadata_file_path)) |
| return; |
| |
| if (base::WriteFile(metadata_file_path, nullptr, 0) == -1) |
| DLOG(FATAL) << "Could not write .metadata_never_index file."; |
| } |
| |
| void EnsureMetadataNeverIndexFile(const base::FilePath& user_data_dir) { |
| content::BrowserThread::PostTask( |
| content::BrowserThread::FILE, FROM_HERE, |
| base::Bind(&EnsureMetadataNeverIndexFileOnFileThread, user_data_dir)); |
| } |
| |
| } // namespace |
| |
| // ChromeBrowserMainPartsMac --------------------------------------------------- |
| |
| ChromeBrowserMainPartsMac::ChromeBrowserMainPartsMac( |
| const content::MainFunctionParams& parameters) |
| : ChromeBrowserMainPartsPosix(parameters) { |
| } |
| |
| ChromeBrowserMainPartsMac::~ChromeBrowserMainPartsMac() { |
| } |
| |
| void ChromeBrowserMainPartsMac::PreEarlyInitialization() { |
| ChromeBrowserMainPartsPosix::PreEarlyInitialization(); |
| |
| if (base::mac::WasLaunchedAsLoginItemRestoreState()) { |
| base::CommandLine* singleton_command_line = |
| base::CommandLine::ForCurrentProcess(); |
| singleton_command_line->AppendSwitch(switches::kRestoreLastSession); |
| } else if (base::mac::WasLaunchedAsHiddenLoginItem()) { |
| base::CommandLine* singleton_command_line = |
| base::CommandLine::ForCurrentProcess(); |
| singleton_command_line->AppendSwitch(switches::kNoStartupWindow); |
| } |
| } |
| |
| void ChromeBrowserMainPartsMac::PreMainMessageLoopStart() { |
| MacStartupProfiler::GetInstance()->Profile( |
| MacStartupProfiler::PRE_MAIN_MESSAGE_LOOP_START); |
| ChromeBrowserMainPartsPosix::PreMainMessageLoopStart(); |
| |
| // Tell Cocoa to finish its initialization, which we want to do manually |
| // instead of calling NSApplicationMain(). The primary reason is that NSAM() |
| // never returns, which would leave all the objects currently on the stack |
| // in scoped_ptrs hanging and never cleaned up. We then load the main nib |
| // directly. The main event loop is run from common code using the |
| // MessageLoop API, which works out ok for us because it's a wrapper around |
| // CFRunLoop. |
| |
| // Initialize NSApplication using the custom subclass. |
| chrome_browser_application_mac::RegisterBrowserCrApp(); |
| |
| // If ui_task is not NULL, the app is actually a browser_test. |
| if (!parameters().ui_task) { |
| // The browser process only wants to support the language Cocoa will use, |
| // so force the app locale to be overriden with that value. |
| l10n_util::OverrideLocaleWithCocoaLocale(); |
| } |
| |
| // Before we load the nib, we need to start up the resource bundle so we |
| // have the strings avaiable for localization. |
| // TODO(markusheintz): Read preference pref::kApplicationLocale in order |
| // to enforce the application locale. |
| const std::string loaded_locale = |
| ui::ResourceBundle::InitSharedInstanceWithLocale( |
| std::string(), NULL, ui::ResourceBundle::LOAD_COMMON_RESOURCES); |
| CHECK(!loaded_locale.empty()) << "Default locale could not be found"; |
| |
| base::FilePath resources_pack_path; |
| PathService::Get(chrome::FILE_RESOURCES_PACK, &resources_pack_path); |
| ResourceBundle::GetSharedInstance().AddDataPackFromPath( |
| resources_pack_path, ui::SCALE_FACTOR_NONE); |
| |
| // This is a no-op if the KeystoneRegistration framework is not present. |
| // The framework is only distributed with branded Google Chrome builds. |
| [[KeystoneGlue defaultKeystoneGlue] registerWithKeystone]; |
| |
| // Disk image installation is sort of a first-run task, so it shares the |
| // no first run switches. |
| // |
| // This needs to be done after the resource bundle is initialized (for |
| // access to localizations in the UI) and after Keystone is initialized |
| // (because the installation may need to promote Keystone) but before the |
| // app controller is set up (and thus before MainMenu.nib is loaded, because |
| // the app controller assumes that a browser has been set up and will crash |
| // upon receipt of certain notifications if no browser exists), before |
| // anyone tries doing anything silly like firing off an import job, and |
| // before anything creating preferences like Local State in order for the |
| // relaunched installed application to still consider itself as first-run. |
| if (!first_run::IsFirstRunSuppressed(parsed_command_line())) { |
| if (MaybeInstallFromDiskImage()) { |
| // The application was installed and the installed copy has been |
| // launched. This process is now obsolete. Exit. |
| exit(0); |
| } |
| } |
| |
| // Now load the nib (from the right bundle). |
| base::scoped_nsobject<NSNib> nib( |
| [[NSNib alloc] initWithNibNamed:@"MainMenu" |
| bundle:base::mac::FrameworkBundle()]); |
| // TODO(viettrungluu): crbug.com/20504 - This currently leaks, so if you |
| // change this, you'll probably need to change the Valgrind suppression. |
| [nib instantiateNibWithOwner:NSApp topLevelObjects:nil]; |
| // Make sure the app controller has been created. |
| DCHECK([NSApp delegate]); |
| |
| [[NSUserDefaults standardUserDefaults] registerDefaults:@{ |
| // Prevent Cocoa from turning command-line arguments into |
| // |-application:openFiles:|, since we already handle them directly. |
| // @"NO" looks like a mistake, but the value really is supposed to be a |
| // string. |
| @"NSTreatUnknownArgumentsAsOpen": @"NO" |
| }]; |
| } |
| |
| void ChromeBrowserMainPartsMac::PostMainMessageLoopStart() { |
| MacStartupProfiler::GetInstance()->Profile( |
| MacStartupProfiler::POST_MAIN_MESSAGE_LOOP_START); |
| ChromeBrowserMainPartsPosix::PostMainMessageLoopStart(); |
| } |
| |
| void ChromeBrowserMainPartsMac::PreProfileInit() { |
| MacStartupProfiler::GetInstance()->Profile( |
| MacStartupProfiler::PRE_PROFILE_INIT); |
| ChromeBrowserMainPartsPosix::PreProfileInit(); |
| |
| // This is called here so that the app shim socket is only created after |
| // taking the singleton lock. |
| g_browser_process->platform_part()->app_shim_host_manager()->Init(); |
| AppListService::InitAll(NULL, |
| GetStartupProfilePath(user_data_dir(), parsed_command_line())); |
| } |
| |
| void ChromeBrowserMainPartsMac::PostProfileInit() { |
| MacStartupProfiler::GetInstance()->Profile( |
| MacStartupProfiler::POST_PROFILE_INIT); |
| ChromeBrowserMainPartsPosix::PostProfileInit(); |
| |
| g_browser_process->metrics_service()->RecordBreakpadRegistration( |
| crash_reporter::GetUploadsEnabled()); |
| |
| // TODO(calamity): Make this gated on first_run::IsChromeFirstRun() in M45. |
| content::BrowserThread::PostAfterStartupTask( |
| FROM_HERE, base::ThreadTaskRunnerHandle::Get(), |
| base::Bind(&EnsureMetadataNeverIndexFile, user_data_dir())); |
| |
| // Activation of KeyStone is not automatic but done in response to the |
| // counting and reporting of profiles. Make sure, assuming KeyStone |
| // is active, that it happened. |
| CHECK(![KeystoneGlue defaultKeystoneGlue] || |
| [[KeystoneGlue defaultKeystoneGlue] isRegisteredAndActive]); |
| } |
| |
| void ChromeBrowserMainPartsMac::DidEndMainMessageLoop() { |
| AppController* appController = [NSApp delegate]; |
| [appController didEndMainMessageLoop]; |
| } |