electron/atom/app/atom_main_delegate.cc

219 lines
6.6 KiB
C++
Raw Normal View History

// 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.
2014-03-16 00:30:26 +00:00
#include "atom/app/atom_main_delegate.h"
2013-04-12 01:46:58 +00:00
#include <iostream>
#include <memory>
2016-08-26 22:30:02 +00:00
#include <string>
#include "atom/app/atom_content_client.h"
#include "atom/browser/atom_browser_client.h"
#include "atom/browser/relauncher.h"
2014-10-08 10:27:39 +00:00
#include "atom/common/google_api_key.h"
#include "atom/common/options_switches.h"
#include "atom/renderer/atom_renderer_client.h"
#include "atom/renderer/atom_sandboxed_renderer_client.h"
2015-05-04 12:58:48 +00:00
#include "atom/utility/atom_content_utility_client.h"
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"
#include "base/logging.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"
#include "ipc/ipc_buildflags.h"
#include "services/service_manager/sandbox/switches.h"
#include "ui/base/l10n/l10n_util.h"
2014-07-16 07:05:02 +00:00
#include "ui/base/resource/resource_bundle.h"
2018-02-10 02:01:14 +00:00
#if BUILDFLAG(IPC_MESSAGE_LOG_ENABLED)
#define IPC_MESSAGE_MACROS_LOG_ENABLED
#include "content/public/common/content_ipc_logging.h"
#define IPC_LOG_TABLE_ADD_ENTRY(msg_id, logger) \
content::RegisterIPCLogger(msg_id, logger)
2018-02-11 18:25:11 +00:00
#include "atom/common/common_message_generator.h"
2018-02-10 02:01:14 +00:00
#endif
#if defined(OS_MACOSX)
#include "atom/app/atom_main_delegate_mac.h"
#endif
2013-04-12 01:46:58 +00:00
namespace atom {
namespace {
2016-06-01 09:22:14 +00:00
const char* kRelauncherProcess = "relauncher";
bool IsBrowserProcess(base::CommandLine* cmd) {
std::string process_type = cmd->GetSwitchValueASCII(::switches::kProcessType);
return process_type.empty();
}
#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) {
// noop.
}
#endif
} // namespace
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
bool AtomMainDelegate::BasicStartupComplete(int* exit_code) {
auto* command_line = base::CommandLine::ForCurrentProcess();
logging::LoggingSettings settings;
2015-03-25 12:21:56 +00:00
#if defined(OS_WIN)
// On Windows the terminal returns immediately, so we add a new line to
// prevent output in the same line as the prompt.
if (IsBrowserProcess(command_line))
std::wcout << std::endl;
#if defined(DEBUG)
// Print logging to debug.log on Windows
settings.logging_dest = logging::LOG_TO_ALL;
settings.log_file = L"debug.log";
settings.lock_log = logging::LOCK_LOG_FILE;
settings.delete_old = logging::DELETE_OLD_LOG_FILE;
#else
settings.logging_dest = logging::LOG_TO_SYSTEM_DEBUG_LOG;
2015-03-26 03:27:06 +00:00
#endif // defined(DEBUG)
2018-04-18 01:55:30 +00:00
#else // defined(OS_WIN)
settings.logging_dest = logging::LOG_TO_SYSTEM_DEBUG_LOG;
#endif // !defined(OS_WIN)
// Only enable logging when --enable-logging is specified.
auto env = base::Environment::Create();
if (!command_line->HasSwitch(::switches::kEnableLogging) &&
!env->HasVar("ELECTRON_ENABLE_LOGGING")) {
settings.logging_dest = logging::LOG_NONE;
logging::SetMinLogLevel(logging::LOG_NUM_SEVERITIES);
}
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);
2015-03-26 03:27:06 +00:00
// Enable convient stack printing.
#if defined(DEBUG) && defined(OS_LINUX)
bool enable_stack_dumping = true;
#else
bool enable_stack_dumping = env->HasVar("ELECTRON_ENABLE_STACK_DUMPING");
2018-02-06 07:50:58 +00:00
#endif
#if defined(ARCH_CPU_ARM_FAMILY) && defined(ARCH_CPU_32_BITS)
// For 32bit ARM enabling stack printing would end up crashing.
// https://github.com/electron/electron/pull/11230#issuecomment-363232482
enable_stack_dumping = false;
2014-02-19 11:07:52 +00:00
#endif
if (enable_stack_dumping)
base::debug::EnableInProcessStackDumping();
2014-02-19 11:07:52 +00:00
2015-11-13 05:05:16 +00:00
chrome::RegisterPathProvider();
2016-05-18 07:42:26 +00:00
#if defined(OS_MACOSX)
SetUpBundleOverrides();
#endif
#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();
#endif
content_client_ = std::make_unique<AtomContentClient>();
SetContentClient(content_client_.get());
2014-07-09 09:15:28 +00:00
return brightray::MainDelegate::BasicStartupComplete(exit_code);
}
2013-04-20 03:19:25 +00:00
void AtomMainDelegate::PreSandboxStartup() {
2014-07-09 09:15:28 +00:00
brightray::MainDelegate::PreSandboxStartup();
auto* command_line = base::CommandLine::ForCurrentProcess();
// Only append arguments for browser process.
if (!IsBrowserProcess(command_line))
return;
2017-06-09 06:39:53 +00:00
2017-06-09 06:29:35 +00:00
if (!command_line->HasSwitch(switches::kEnableMixedSandbox)) {
if (command_line->HasSwitch(switches::kEnableSandbox)) {
2017-06-09 06:46:37 +00:00
// Disable setuid sandbox since it is not longer required on
2017-06-09 06:39:53 +00:00
// linux(namespace sandbox is available on most distros).
command_line->AppendSwitch(
service_manager::switches::kDisableSetuidSandbox);
2017-06-09 06:29:35 +00:00
} else {
// Disable renderer sandbox for most of node's functions.
2018-09-15 00:07:49 +00:00
command_line->AppendSwitch(service_manager::switches::kNoSandbox);
2017-06-09 06:29:35 +00:00
}
}
// Allow file:// URIs to read other file:// URIs by default.
command_line->AppendSwitch(::switches::kAllowFileAccessFromFiles);
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
}
void AtomMainDelegate::PreContentInitialization() {
#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();
}
content::ContentRendererClient*
2018-04-18 01:55:30 +00:00
AtomMainDelegate::CreateContentRendererClient() {
if (base::CommandLine::ForCurrentProcess()->HasSwitch(
2018-04-18 01:55:30 +00:00
switches::kEnableSandbox) ||
!base::CommandLine::ForCurrentProcess()->HasSwitch(
2018-09-15 00:07:49 +00:00
service_manager::switches::kNoSandbox)) {
renderer_client_.reset(new AtomSandboxedRendererClient);
} 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)
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
2013-04-12 01:46:58 +00:00
} // namespace atom