2014-10-31 18:17:05 +00:00
|
|
|
// Copyright (c) 2013 GitHub, Inc.
|
2014-04-25 09:49:37 +00:00
|
|
|
// Use of this source code is governed by the MIT license that can be
|
2013-04-12 01:46:58 +00:00
|
|
|
// found in the LICENSE file.
|
|
|
|
|
2019-06-19 20:46:59 +00:00
|
|
|
#include "shell/app/atom_main_delegate.h"
|
2013-04-12 01:46:58 +00:00
|
|
|
|
2015-10-10 03:39:02 +00:00
|
|
|
#include <iostream>
|
2018-09-13 00:25:56 +00:00
|
|
|
#include <memory>
|
2016-08-26 22:30:02 +00:00
|
|
|
#include <string>
|
2014-03-16 01:13:06 +00:00
|
|
|
|
2019-01-11 01:41:49 +00:00
|
|
|
#if defined(OS_LINUX)
|
|
|
|
#include <glib.h> // for g_setenv()
|
|
|
|
#endif
|
|
|
|
|
2013-04-20 03:19:25 +00:00
|
|
|
#include "base/command_line.h"
|
2014-02-19 11:07:52 +00:00
|
|
|
#include "base/debug/stack_trace.h"
|
2014-10-08 10:27:39 +00:00
|
|
|
#include "base/environment.h"
|
2013-07-08 03:09:53 +00:00
|
|
|
#include "base/logging.h"
|
2018-10-23 12:16:13 +00:00
|
|
|
#include "base/mac/bundle_locations.h"
|
|
|
|
#include "base/path_service.h"
|
2015-11-13 05:05:16 +00:00
|
|
|
#include "chrome/common/chrome_paths.h"
|
2013-04-20 03:19:25 +00:00
|
|
|
#include "content/public/common/content_switches.h"
|
2018-10-23 12:16:13 +00:00
|
|
|
#include "electron/buildflags/buildflags.h"
|
2018-07-21 12:30:16 +00:00
|
|
|
#include "ipc/ipc_buildflags.h"
|
2018-10-23 12:16:13 +00:00
|
|
|
#include "services/service_manager/embedder/switches.h"
|
2018-04-10 18:54:22 +00:00
|
|
|
#include "services/service_manager/sandbox/switches.h"
|
2019-06-19 20:46:59 +00:00
|
|
|
#include "shell/app/atom_content_client.h"
|
|
|
|
#include "shell/browser/atom_browser_client.h"
|
|
|
|
#include "shell/browser/atom_gpu_client.h"
|
|
|
|
#include "shell/browser/feature_list.h"
|
|
|
|
#include "shell/browser/relauncher.h"
|
|
|
|
#include "shell/common/options_switches.h"
|
|
|
|
#include "shell/renderer/atom_renderer_client.h"
|
|
|
|
#include "shell/renderer/atom_sandboxed_renderer_client.h"
|
|
|
|
#include "shell/utility/atom_content_utility_client.h"
|
2016-01-08 04:06:06 +00:00
|
|
|
#include "ui/base/l10n/l10n_util.h"
|
2014-07-16 07:05:02 +00:00
|
|
|
#include "ui/base/resource/resource_bundle.h"
|
2018-10-23 12:16:13 +00:00
|
|
|
#include "ui/base/ui_base_switches.h"
|
2013-09-12 07:42:36 +00:00
|
|
|
|
2018-10-03 20:49:18 +00:00
|
|
|
#if defined(OS_MACOSX)
|
2019-06-19 20:46:59 +00:00
|
|
|
#include "shell/app/atom_main_delegate_mac.h"
|
2018-10-03 20:49:18 +00:00
|
|
|
#endif
|
|
|
|
|
2019-05-14 22:42:39 +00:00
|
|
|
#if defined(OS_WIN)
|
|
|
|
#include "base/win/win_util.h"
|
2019-06-13 06:42:21 +00:00
|
|
|
#if defined(_WIN64)
|
2019-06-19 20:46:59 +00:00
|
|
|
#include "shell/common/crash_reporter/crash_reporter_win.h"
|
2019-06-13 06:42:21 +00:00
|
|
|
#endif
|
2019-05-14 22:42:39 +00:00
|
|
|
#endif
|
|
|
|
|
2019-06-19 21:23:04 +00:00
|
|
|
namespace electron {
|
2013-04-12 01:46:58 +00:00
|
|
|
|
2015-10-15 04:44:55 +00:00
|
|
|
namespace {
|
|
|
|
|
2016-06-01 09:22:14 +00:00
|
|
|
const char* kRelauncherProcess = "relauncher";
|
|
|
|
|
2015-10-15 04:44:55 +00:00
|
|
|
bool IsBrowserProcess(base::CommandLine* cmd) {
|
2016-08-15 10:59:08 +00:00
|
|
|
std::string process_type = cmd->GetSwitchValueASCII(::switches::kProcessType);
|
2015-10-15 04:44:55 +00:00
|
|
|
return process_type.empty();
|
|
|
|
}
|
|
|
|
|
2019-06-20 10:10:56 +00:00
|
|
|
bool IsSandboxEnabled(base::CommandLine* command_line) {
|
|
|
|
return command_line->HasSwitch(switches::kEnableSandbox) ||
|
|
|
|
!command_line->HasSwitch(service_manager::switches::kNoSandbox);
|
|
|
|
}
|
|
|
|
|
2018-10-23 12:16:13 +00:00
|
|
|
// Returns true if this subprocess type needs the ResourceBundle initialized
|
|
|
|
// and resources loaded.
|
|
|
|
bool SubprocessNeedsResourceBundle(const std::string& process_type) {
|
|
|
|
return
|
|
|
|
#if defined(OS_POSIX) && !defined(OS_MACOSX)
|
|
|
|
// The zygote process opens the resources for the renderers.
|
|
|
|
process_type == service_manager::switches::kZygoteProcess ||
|
|
|
|
#endif
|
|
|
|
#if defined(OS_MACOSX)
|
|
|
|
// Mac needs them too for scrollbar related images and for sandbox
|
|
|
|
// profiles.
|
|
|
|
process_type == ::switches::kPpapiPluginProcess ||
|
|
|
|
process_type == ::switches::kPpapiBrokerProcess ||
|
|
|
|
process_type == ::switches::kGpuProcess ||
|
|
|
|
#endif
|
|
|
|
process_type == ::switches::kRendererProcess ||
|
|
|
|
process_type == ::switches::kUtilityProcess;
|
|
|
|
}
|
|
|
|
|
2016-05-25 04:45:25 +00:00
|
|
|
#if defined(OS_WIN)
|
2018-04-18 01:55:30 +00:00
|
|
|
void InvalidParameterHandler(const wchar_t*,
|
|
|
|
const wchar_t*,
|
|
|
|
const wchar_t*,
|
|
|
|
unsigned int,
|
|
|
|
uintptr_t) {
|
2016-05-25 04:45:25 +00:00
|
|
|
// noop.
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2015-10-15 04:44:55 +00:00
|
|
|
} // namespace
|
|
|
|
|
2018-10-23 12:16:13 +00:00
|
|
|
void LoadResourceBundle(const std::string& locale) {
|
|
|
|
const bool initialized = ui::ResourceBundle::HasSharedInstance();
|
|
|
|
if (initialized)
|
|
|
|
ui::ResourceBundle::CleanupSharedInstance();
|
|
|
|
|
|
|
|
// Load other resource files.
|
|
|
|
base::FilePath pak_dir;
|
|
|
|
#if defined(OS_MACOSX)
|
|
|
|
pak_dir =
|
|
|
|
base::mac::FrameworkBundlePath().Append(FILE_PATH_LITERAL("Resources"));
|
|
|
|
#else
|
|
|
|
base::PathService::Get(base::DIR_MODULE, &pak_dir);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
ui::ResourceBundle::InitSharedInstanceWithLocale(
|
|
|
|
locale, nullptr, ui::ResourceBundle::LOAD_COMMON_RESOURCES);
|
|
|
|
ui::ResourceBundle& bundle = ui::ResourceBundle::GetSharedInstance();
|
|
|
|
bundle.ReloadLocaleResources(locale);
|
|
|
|
bundle.AddDataPackFromPath(pak_dir.Append(FILE_PATH_LITERAL("resources.pak")),
|
|
|
|
ui::SCALE_FACTOR_NONE);
|
|
|
|
#if BUILDFLAG(ENABLE_PDF_VIEWER)
|
|
|
|
NOTIMPLEMENTED()
|
|
|
|
<< "Hi, whoever's fixing PDF support! Thanks! The pdf "
|
|
|
|
"viewer resources haven't been ported over to the GN build yet, so "
|
|
|
|
"you'll probably need to change this bit of code.";
|
|
|
|
bundle.AddDataPackFromPath(
|
|
|
|
pak_dir.Append(FILE_PATH_LITERAL("pdf_viewer_resources.pak")),
|
|
|
|
ui::GetSupportedScaleFactors()[0]);
|
|
|
|
#endif // BUILDFLAG(ENABLE_PDF_VIEWER)
|
|
|
|
}
|
|
|
|
|
2018-04-18 01:55:30 +00:00
|
|
|
AtomMainDelegate::AtomMainDelegate() {}
|
2013-04-12 01:46:58 +00:00
|
|
|
|
2018-04-18 01:55:30 +00:00
|
|
|
AtomMainDelegate::~AtomMainDelegate() {}
|
2013-04-12 01:46:58 +00:00
|
|
|
|
2013-07-08 03:09:53 +00:00
|
|
|
bool AtomMainDelegate::BasicStartupComplete(int* exit_code) {
|
2018-04-17 22:41:47 +00:00
|
|
|
auto* command_line = base::CommandLine::ForCurrentProcess();
|
2015-10-15 04:44:55 +00:00
|
|
|
|
2013-12-17 06:01:40 +00:00
|
|
|
logging::LoggingSettings settings;
|
2015-03-25 12:21:56 +00:00
|
|
|
#if defined(OS_WIN)
|
2019-06-13 06:42:21 +00:00
|
|
|
#if defined(_WIN64)
|
|
|
|
crash_reporter::CrashReporterWin::SetUnhandledExceptionFilter();
|
|
|
|
#endif
|
|
|
|
|
2015-10-10 03:39:02 +00:00
|
|
|
// On Windows the terminal returns immediately, so we add a new line to
|
|
|
|
// prevent output in the same line as the prompt.
|
2015-10-15 04:44:55 +00:00
|
|
|
if (IsBrowserProcess(command_line))
|
|
|
|
std::wcout << std::endl;
|
2013-07-23 08:37:00 +00:00
|
|
|
#if defined(DEBUG)
|
2015-10-03 07:33:55 +00:00
|
|
|
// Print logging to debug.log on Windows
|
2013-12-17 06:01:40 +00:00
|
|
|
settings.logging_dest = logging::LOG_TO_ALL;
|
2019-05-20 17:41:30 +00:00
|
|
|
base::FilePath log_filename;
|
|
|
|
base::PathService::Get(base::DIR_EXE, &log_filename);
|
|
|
|
log_filename = log_filename.AppendASCII("debug.log");
|
|
|
|
settings.log_file = log_filename.value().c_str();
|
2013-12-17 06:01:40 +00:00
|
|
|
settings.lock_log = logging::LOCK_LOG_FILE;
|
|
|
|
settings.delete_old = logging::DELETE_OLD_LOG_FILE;
|
2013-07-23 08:37:00 +00:00
|
|
|
#else
|
2019-06-01 16:21:13 +00:00
|
|
|
settings.logging_dest =
|
|
|
|
logging::LOG_TO_SYSTEM_DEBUG_LOG | logging::LOG_TO_STDERR;
|
2015-03-26 03:27:06 +00:00
|
|
|
#endif // defined(DEBUG)
|
2018-04-18 01:55:30 +00:00
|
|
|
#else // defined(OS_WIN)
|
2019-06-01 16:21:13 +00:00
|
|
|
settings.logging_dest =
|
|
|
|
logging::LOG_TO_SYSTEM_DEBUG_LOG | logging::LOG_TO_STDERR;
|
2015-09-07 13:44:03 +00:00
|
|
|
#endif // !defined(OS_WIN)
|
2015-10-03 07:33:55 +00:00
|
|
|
|
|
|
|
// Only enable logging when --enable-logging is specified.
|
2018-06-18 07:32:55 +00:00
|
|
|
auto env = base::Environment::Create();
|
2016-08-15 10:59:08 +00:00
|
|
|
if (!command_line->HasSwitch(::switches::kEnableLogging) &&
|
2015-10-20 05:00:28 +00:00
|
|
|
!env->HasVar("ELECTRON_ENABLE_LOGGING")) {
|
2015-10-03 07:33:55 +00:00
|
|
|
settings.logging_dest = logging::LOG_NONE;
|
2015-10-10 17:44:22 +00:00
|
|
|
logging::SetMinLogLevel(logging::LOG_NUM_SEVERITIES);
|
|
|
|
}
|
2015-10-03 07:33:55 +00:00
|
|
|
|
2015-03-25 12:21:56 +00:00
|
|
|
logging::InitLogging(settings);
|
|
|
|
|
2013-12-23 14:04:49 +00:00
|
|
|
// Logging with pid and timestamp.
|
|
|
|
logging::SetLogItems(true, false, true, false);
|
|
|
|
|
2018-11-29 06:23:41 +00:00
|
|
|
// Enable convient stack printing. This is enabled by default in non-official
|
|
|
|
// builds.
|
|
|
|
if (env->HasVar("ELECTRON_ENABLE_STACK_DUMPING"))
|
2015-10-20 05:02:48 +00:00
|
|
|
base::debug::EnableInProcessStackDumping();
|
2014-02-19 11:07:52 +00:00
|
|
|
|
2019-01-29 06:30:17 +00:00
|
|
|
if (env->HasVar("ELECTRON_DISABLE_SANDBOX"))
|
|
|
|
command_line->AppendSwitch(service_manager::switches::kNoSandbox);
|
|
|
|
|
2015-11-13 05:05:16 +00:00
|
|
|
chrome::RegisterPathProvider();
|
|
|
|
|
2016-05-18 07:42:26 +00:00
|
|
|
#if defined(OS_MACOSX)
|
2018-10-23 12:16:13 +00:00
|
|
|
OverrideChildProcessPath();
|
|
|
|
OverrideFrameworkBundlePath();
|
2016-05-18 07:42:26 +00:00
|
|
|
SetUpBundleOverrides();
|
|
|
|
#endif
|
|
|
|
|
2016-05-25 04:45:25 +00:00
|
|
|
#if defined(OS_WIN)
|
|
|
|
// Ignore invalid parameter errors.
|
|
|
|
_set_invalid_parameter_handler(InvalidParameterHandler);
|
2017-02-14 08:23:20 +00:00
|
|
|
// Disable the ActiveVerifier, which is used by Chrome to track possible
|
|
|
|
// bugs, but no use in Electron.
|
|
|
|
base::win::DisableHandleVerifier();
|
2019-05-14 22:42:39 +00:00
|
|
|
|
|
|
|
if (IsBrowserProcess(command_line))
|
|
|
|
base::win::PinUser32();
|
2016-05-25 04:45:25 +00:00
|
|
|
#endif
|
|
|
|
|
2019-07-15 14:38:41 +00:00
|
|
|
#if defined(OS_LINUX)
|
|
|
|
// Check for --no-sandbox parameter when running as root.
|
|
|
|
if (getuid() == 0 && IsSandboxEnabled(command_line))
|
|
|
|
LOG(FATAL) << "Running as root without --"
|
|
|
|
<< service_manager::switches::kNoSandbox
|
|
|
|
<< " is not supported. See https://crbug.com/638180.";
|
|
|
|
#endif
|
|
|
|
|
2018-10-22 17:04:23 +00:00
|
|
|
content_client_ = std::make_unique<AtomContentClient>();
|
|
|
|
SetContentClient(content_client_.get());
|
|
|
|
|
2018-10-23 12:16:13 +00:00
|
|
|
return false;
|
2013-07-08 03:09:53 +00:00
|
|
|
}
|
|
|
|
|
2019-01-10 16:19:25 +00:00
|
|
|
void AtomMainDelegate::PostEarlyInitialization(bool is_running_tests) {
|
|
|
|
std::string custom_locale;
|
|
|
|
ui::ResourceBundle::InitSharedInstanceWithLocale(
|
|
|
|
custom_locale, nullptr, ui::ResourceBundle::LOAD_COMMON_RESOURCES);
|
|
|
|
auto* cmd_line = base::CommandLine::ForCurrentProcess();
|
|
|
|
if (cmd_line->HasSwitch(::switches::kLang)) {
|
|
|
|
const std::string locale = cmd_line->GetSwitchValueASCII(::switches::kLang);
|
|
|
|
const base::FilePath locale_file_path =
|
|
|
|
ui::ResourceBundle::GetSharedInstance().GetLocaleFilePath(locale, true);
|
|
|
|
if (!locale_file_path.empty()) {
|
|
|
|
custom_locale = locale;
|
|
|
|
#if defined(OS_LINUX)
|
|
|
|
/* When built with USE_GLIB, libcc's GetApplicationLocaleInternal() uses
|
|
|
|
* glib's g_get_language_names(), which keys off of getenv("LC_ALL") */
|
|
|
|
g_setenv("LC_ALL", custom_locale.c_str(), TRUE);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#if defined(OS_MACOSX)
|
|
|
|
if (custom_locale.empty())
|
|
|
|
l10n_util::OverrideLocaleWithCocoaLocale();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
LoadResourceBundle(custom_locale);
|
|
|
|
|
|
|
|
AtomBrowserClient::SetApplicationLocale(
|
|
|
|
l10n_util::GetApplicationLocale(custom_locale));
|
|
|
|
}
|
|
|
|
|
2013-04-20 03:19:25 +00:00
|
|
|
void AtomMainDelegate::PreSandboxStartup() {
|
2018-04-17 22:41:47 +00:00
|
|
|
auto* command_line = base::CommandLine::ForCurrentProcess();
|
2014-01-30 13:06:56 +00:00
|
|
|
|
2018-10-23 12:16:13 +00:00
|
|
|
std::string process_type =
|
|
|
|
command_line->GetSwitchValueASCII(::switches::kProcessType);
|
|
|
|
|
|
|
|
// Initialize ResourceBundle which handles files loaded from external
|
|
|
|
// sources. The language should have been passed in to us from the
|
|
|
|
// browser process as a command line flag.
|
|
|
|
if (SubprocessNeedsResourceBundle(process_type)) {
|
|
|
|
std::string locale = command_line->GetSwitchValueASCII(::switches::kLang);
|
|
|
|
LoadResourceBundle(locale);
|
|
|
|
}
|
|
|
|
|
2014-02-20 17:04:27 +00:00
|
|
|
// Only append arguments for browser process.
|
2015-10-15 04:44:55 +00:00
|
|
|
if (!IsBrowserProcess(command_line))
|
2014-01-30 13:06:56 +00:00
|
|
|
return;
|
2017-06-09 06:39:53 +00:00
|
|
|
|
2015-05-12 12:51:48 +00:00
|
|
|
// Allow file:// URIs to read other file:// URIs by default.
|
2016-08-15 10:59:08 +00:00
|
|
|
command_line->AppendSwitch(::switches::kAllowFileAccessFromFiles);
|
2015-05-12 12:51:48 +00:00
|
|
|
|
2014-10-08 10:17:27 +00:00
|
|
|
#if defined(OS_MACOSX)
|
|
|
|
// Enable AVFoundation.
|
|
|
|
command_line->AppendSwitch("enable-avfoundation");
|
|
|
|
#endif
|
2013-04-20 03:19:25 +00:00
|
|
|
}
|
|
|
|
|
2018-10-25 06:02:58 +00:00
|
|
|
void AtomMainDelegate::PreCreateMainMessageLoop() {
|
2019-04-23 22:18:12 +00:00
|
|
|
// This is initialized early because the service manager reads some feature
|
|
|
|
// flags and we need to make sure the feature list is initialized before the
|
|
|
|
// service manager reads the features.
|
|
|
|
InitializeFeatureList();
|
2018-10-03 20:49:18 +00:00
|
|
|
#if defined(OS_MACOSX)
|
|
|
|
RegisterAtomCrApp();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2013-04-12 01:46:58 +00:00
|
|
|
content::ContentBrowserClient* AtomMainDelegate::CreateContentBrowserClient() {
|
|
|
|
browser_client_.reset(new AtomBrowserClient);
|
|
|
|
return browser_client_.get();
|
|
|
|
}
|
|
|
|
|
2019-04-30 16:45:48 +00:00
|
|
|
content::ContentGpuClient* AtomMainDelegate::CreateContentGpuClient() {
|
|
|
|
gpu_client_.reset(new AtomGpuClient);
|
|
|
|
return gpu_client_.get();
|
|
|
|
}
|
|
|
|
|
2013-04-12 01:46:58 +00:00
|
|
|
content::ContentRendererClient*
|
2018-04-18 01:55:30 +00:00
|
|
|
AtomMainDelegate::CreateContentRendererClient() {
|
2019-06-20 10:10:56 +00:00
|
|
|
auto* command_line = base::CommandLine::ForCurrentProcess();
|
|
|
|
|
|
|
|
if (IsSandboxEnabled(command_line)) {
|
2016-08-19 11:47:32 +00:00
|
|
|
renderer_client_.reset(new AtomSandboxedRendererClient);
|
2016-08-15 10:59:08 +00:00
|
|
|
} else {
|
|
|
|
renderer_client_.reset(new AtomRendererClient);
|
|
|
|
}
|
|
|
|
|
2013-04-12 01:46:58 +00:00
|
|
|
return renderer_client_.get();
|
|
|
|
}
|
|
|
|
|
2015-04-30 03:31:17 +00:00
|
|
|
content::ContentUtilityClient* AtomMainDelegate::CreateContentUtilityClient() {
|
|
|
|
utility_client_.reset(new AtomContentUtilityClient);
|
|
|
|
return utility_client_.get();
|
|
|
|
}
|
|
|
|
|
2016-06-01 09:22:14 +00:00
|
|
|
int AtomMainDelegate::RunProcess(
|
|
|
|
const std::string& process_type,
|
|
|
|
const content::MainFunctionParams& main_function_params) {
|
|
|
|
if (process_type == kRelauncherProcess)
|
2016-06-02 07:23:46 +00:00
|
|
|
return relauncher::RelauncherMain(main_function_params);
|
2016-06-01 09:22:14 +00:00
|
|
|
else
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2016-06-02 07:54:21 +00:00
|
|
|
#if defined(OS_MACOSX)
|
2016-06-01 09:22:14 +00:00
|
|
|
bool AtomMainDelegate::ShouldSendMachPort(const std::string& process_type) {
|
|
|
|
return process_type != kRelauncherProcess;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool AtomMainDelegate::DelaySandboxInitialization(
|
|
|
|
const std::string& process_type) {
|
|
|
|
return process_type == kRelauncherProcess;
|
|
|
|
}
|
2016-06-02 07:54:21 +00:00
|
|
|
#endif
|
2016-06-01 09:22:14 +00:00
|
|
|
|
2018-11-22 17:02:52 +00:00
|
|
|
bool AtomMainDelegate::ShouldLockSchemeRegistry() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-01-12 01:00:43 +00:00
|
|
|
bool AtomMainDelegate::ShouldCreateFeatureList() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-06-19 21:23:04 +00:00
|
|
|
} // namespace electron
|