Files
electron/shell/common/logging.cc
trop[bot] e3cabb6119 chore: bump chromium to 144.0.7547.0 (40-x-y) (#49146)
* chore: bump chromium in DEPS to 144.0.7543.0

Co-authored-by: electron-roller[bot] <84116207+electron-roller[bot]@users.noreply.github.com>

* Pass PipScreenCaptureCoordinatorProxy to ScreenCaptureKitDeviceMac

https://chromium-review.googlesource.com/c/chromium/src/+/7157590

Co-authored-by: John Kleinschmidt <jkleinsc@electronjs.org>

* chore: update patches

Co-authored-by: John Kleinschmidt <jkleinsc@electronjs.org>

* chore: update filenames.libcxx.gni

Co-authored-by: John Kleinschmidt <jkleinsc@electronjs.org>

* 7142359: Spanification of process_singleton_posix.cc

https: //chromium-review.googlesource.com/c/chromium/src/+/7142359
Co-Authored-By: Charles Kerr <70381+ckerr@users.noreply.github.com>

Co-authored-by: John Kleinschmidt <jkleinsc@electronjs.org>

* Move logging::LoggingSettings to base/logging/logging_settings.h

https://chromium-review.googlesource.com/c/chromium/src/+/7173024

Co-authored-by: John Kleinschmidt <jkleinsc@electronjs.org>

* chore: bump chromium in DEPS to 144.0.7545.0

Co-authored-by: electron-roller[bot] <84116207+electron-roller[bot]@users.noreply.github.com>

* 7159368: update PluginService API for sync GetPlugins

Upstream removed async PluginService APIs:
- GetPluginsAsync() removed, use synchronous GetPlugins()
- RegisterInternalPlugin() now takes single argument (remove add_at_beginning)
- RefreshPlugins() removed entirely

Updated ElectronPluginInfoHostImpl to use synchronous plugin loading and
simplified ElectronBrowserMainParts internal plugin registration.

Ref: https://chromium-review.googlesource.com/c/chromium/src/+/7159368
Ref: https://chromium-review.googlesource.com/c/chromium/src/+/7159328
Ref: https://chromium-review.googlesource.com/c/chromium/src/+/7159056
(cherry picked from commit 88cdf50b0a)

Co-authored-by: Samuel Attard <sattard@anthropic.com>

* 7159184: add missing base/files/file_path.h include

Add explicit include for base/files/file_path.h in electron_browser_context.h.
After removal of superfluous Mojo includes from content headers, base::FilePath
is no longer transitively included via content/public/browser/browser_context.h.

Ref: https://chromium-review.googlesource.com/c/chromium/src/+/7159184
(cherry picked from commit 6ca8ea03ec)

Co-authored-by: Samuel Attard <sattard@anthropic.com>

* 7126479: add ShouldForceRefreshTextCheckService parameter to SpellCheckClient

Upstream added a force-refresh parameter to WebTextCheckClient::RequestCheckingOfText
to bypass spell check cache. Add the new ShouldForceRefreshTextCheckService
parameter to SpellCheckClient's override (currently unused in Electron).

Ref: https://chromium-review.googlesource.com/c/chromium/src/+/7126479
(cherry picked from commit 879c0401c4)

Co-authored-by: Samuel Attard <sattard@anthropic.com>

* 7083663: remove fingerprinting_protection_ruleset_service override

Upstream deleted external references to Fingerprinting Protection Filter (FPF)
component. Remove the fingerprinting_protection_ruleset_service() override
from BrowserProcessImpl as the method no longer exists in the base class.

Ref: https://chromium-review.googlesource.com/c/chromium/src/+/7083663
(cherry picked from commit 8350d152f9)

Co-authored-by: Samuel Attard <sattard@anthropic.com>

* 7155287: implement WebContentsView::GetSize and Resize

Upstream delegated WebContents::GetSize() and Resize() to WebContentsView,
making them pure virtual. Add const qualifier to GetSize() and implement
the Resize() override in OffScreenWebContentsView (no-op for offscreen).

Ref: https://chromium-review.googlesource.com/c/chromium/src/+/7155287
(cherry picked from commit 084eaa568e)

Co-authored-by: Samuel Attard <sattard@anthropic.com>

* 7184238: add OnUnconfirmedTapConvertedToTap override

Upstream added OnUnconfirmedTapConvertedToTap as a pure virtual method
to RenderWidgetHostViewBase to inform root view when child frame converts
an unconfirmed tap. Add empty override for offscreen rendering.

Ref: https://chromium-review.googlesource.com/c/chromium/src/+/7184238
(cherry picked from commit ef03400d9a)

Co-authored-by: Samuel Attard <sattard@anthropic.com>

* 7143586: add widget parameter to OnCommandsChanged

GlobalAcceleratorListener::OnCommandsChanged gained a gfx::AcceleratedWidget
parameter for window association in the GlobalShortcutListenerLinux
implementation. Pass gfx::kNullAcceleratedWidget for Electron's usage.

Ref: : Pass parent handle to GlobalAcceleratorListenerLinux::BindShortcuts | https://chromium-review.googlesource.com/c/chromium/src/+/7143586
(cherry picked from commit 38306e2bfc)
Co-Authored-By: Samuel Attard <MarshallOfSound@users.noreply.github.com>

Co-authored-by: Samuel Attard <sattard@anthropic.com>

* chore: update patches

Co-Authored-By: Samuel Attard <MarshallOfSound@users.noreply.github.com>

Co-authored-by: John Kleinschmidt <jkleinsc@electronjs.org>

* chore: bump chromium in DEPS to 144.0.7547.0

Co-authored-by: electron-roller[bot] <84116207+electron-roller[bot]@users.noreply.github.com>

* chore: update patches

Co-authored-by: John Kleinschmidt <jkleinsc@electronjs.org>

* 7189232: Add support for UnownedUserData in GlobalFeatures

https: //chromium-review.googlesource.com/c/chromium/src/+/7189232

Co-authored-by: John Kleinschmidt <jkleinsc@electronjs.org>

* Refactor: Use std::u16string for extension load error messages

https://chromium-review.googlesource.com/c/chromium/src/+/7185844

Co-authored-by: John Kleinschmidt <jkleinsc@electronjs.org>

* fixup Add support for UnownedUserData in GlobalFeatures

Co-authored-by: John Kleinschmidt <jkleinsc@electronjs.org>

* 7165650: Remove ResourceContext

https://chromium-review.googlesource.com/c/chromium/src/+/7165650

Co-authored-by: John Kleinschmidt <jkleinsc@electronjs.org>

* fixup BUILD.gn for lint

Co-authored-by: John Kleinschmidt <jkleinsc@electronjs.org>

* 7202164: Reland "Reland "Remove GenericScopedHandle:IsValid""

https://chromium-review.googlesource.com/c/chromium/src/+/7202164

Co-authored-by: John Kleinschmidt <jkleinsc@electronjs.org>

* advance deprecation of v8::ReturnValue<void>::Set(Local<S>).

7168624: [runtime][api] Relax requirements for setter/definer/deleter callbacks | https://chromium-review.googlesource.com/c/v8/v8/+/7168624

Co-authored-by: John Kleinschmidt <jkleinsc@electronjs.org>

* fixup advance deprecation of v8::ReturnValue<void>::Set(Local<S>)

Co-authored-by: John Kleinschmidt <jkleinsc@electronjs.org>

* chore: skip setting LPAC ACLs

Co-authored-by: John Kleinschmidt <jkleinsc@electronjs.org>

* Revert "chore: skip setting LPAC ACLs"

This reverts commit e187aec488.

Co-authored-by: John Kleinschmidt <kleinschmidtorama@gmail.com>

* chore: revert Convert to UNSAFE_TODO in sandbox

revert https://chromium-review.googlesource.com/c/chromium/src/+/7131661 to see if it fixes the Windows sandbox issue.

Co-authored-by: John Kleinschmidt <kleinschmidtorama@gmail.com>

* Revert "chore: revert Convert to UNSAFE_TODO in sandbox"

This reverts commit 57afbfefe5.

Co-authored-by: John Kleinschmidt <kleinschmidtorama@gmail.com>

* chore: Revert "Enable network sandbox by default on Windows"

see if this fixes the Windows sandbox issue

Co-authored-by: John Kleinschmidt <kleinschmidtorama@gmail.com>

* Enable network sandbox by default on Windows

https://chromium-review.googlesource.com/c/chromium/src/+/7204292

Co-authored-by: John Kleinschmidt <kleinschmidtorama@gmail.com>

* Revert "chore: Revert "Enable network sandbox by default on Windows""

This reverts commit 530ab6af82.

Co-authored-by: John Kleinschmidt <kleinschmidtorama@gmail.com>

* fixup! Enable network sandbox by default on Windows | https://chromium-review.googlesource.com/c/chromium/src/+/7204292

Co-authored-by: Keeley Hammond <khammond@slack-corp.com>

* fixup!: Correct flag name, add kLocalNetworkAccessChecks to all platforms

Co-authored-by: Keeley Hammond <khammond@slack-corp.com>

* chore: remove patch that hasn't been backported

See https://github.com/electron/electron/pull/48675

---------

Co-authored-by: trop[bot] <37223003+trop[bot]@users.noreply.github.com>
Co-authored-by: electron-roller[bot] <84116207+electron-roller[bot]@users.noreply.github.com>
Co-authored-by: John Kleinschmidt <jkleinsc@electronjs.org>
Co-authored-by: Samuel Attard <sattard@anthropic.com>
Co-authored-by: John Kleinschmidt <kleinschmidtorama@gmail.com>
Co-authored-by: Keeley Hammond <khammond@slack-corp.com>
Co-authored-by: Niklas Wenzel <dev@nikwen.de>
2025-12-05 12:18:13 -05:00

213 lines
7.7 KiB
C++

// Copyright (c) 2021 Slack Technologies, Inc.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
#include "shell/common/logging.h"
#include <string>
#include "base/base_switches.h"
#include "base/command_line.h"
#include "base/environment.h"
#include "base/files/file_path.h"
#include "base/logging.h"
#include "base/logging/logging_settings.h"
#include "base/path_service.h"
#include "base/strings/cstring_view.h"
#include "base/strings/string_number_conversions.h"
#include "chrome/common/chrome_paths.h"
#include "content/public/common/content_switches.h"
#if BUILDFLAG(IS_WIN)
#include <windows.h>
#include "base/win/scoped_handle.h"
#include "base/win/windows_handle_util.h"
#include "sandbox/policy/switches.h"
#endif
namespace logging {
constexpr base::cstring_view kLogFileName{"ELECTRON_LOG_FILE"};
constexpr base::cstring_view kElectronEnableLogging{"ELECTRON_ENABLE_LOGGING"};
#if BUILDFLAG(IS_WIN)
base::win::ScopedHandle GetLogInheritedHandle(
const base::CommandLine& command_line) {
auto handle_str = command_line.GetSwitchValueNative(::switches::kLogFile);
uint32_t handle_value = 0;
if (!base::StringToUint(handle_str, &handle_value)) {
return {};
}
// Duplicate the handle from the command line so that different things can
// init logging. This means the handle from the parent is never closed, but
// there will only be one of these in the process.
HANDLE log_handle = nullptr;
if (!::DuplicateHandle(::GetCurrentProcess(),
base::win::Uint32ToHandle(handle_value),
::GetCurrentProcess(), &log_handle, 0,
/*bInheritHandle=*/FALSE, DUPLICATE_SAME_ACCESS)) {
return {};
}
// Transfer ownership to the caller.
return base::win::ScopedHandle(log_handle);
}
#endif
base::FilePath GetLogFileName(const base::CommandLine& command_line) {
std::string filename = command_line.GetSwitchValueASCII(switches::kLogFile);
if (filename.empty())
filename = base::Environment::Create()->GetVar(kLogFileName).value_or("");
if (!filename.empty())
return base::FilePath::FromUTF8Unsafe(filename);
auto log_filename = base::FilePath{FILE_PATH_LITERAL("electron_debug.log")};
if (base::FilePath path; base::PathService::Get(chrome::DIR_LOGS, &path))
return path.Append(log_filename);
// error with path service, just use some default file somewhere
return log_filename;
}
namespace {
bool HasExplicitLogFile(const base::CommandLine& command_line) {
std::string filename = command_line.GetSwitchValueASCII(switches::kLogFile);
if (filename.empty())
filename = base::Environment::Create()->GetVar(kLogFileName).value_or("");
return !filename.empty();
}
std::pair<LoggingDestination, bool /* filename_is_handle */>
DetermineLoggingDestination(const base::CommandLine& command_line,
bool is_preinit) {
bool enable_logging = false;
std::string logging_destination;
if (command_line.HasSwitch(::switches::kEnableLogging)) {
enable_logging = true;
logging_destination =
command_line.GetSwitchValueASCII(switches::kEnableLogging);
} else {
auto env = base::Environment::Create();
if (env->HasVar(kElectronEnableLogging)) {
enable_logging = true;
logging_destination = env->GetVar(kElectronEnableLogging).value();
}
}
if (!enable_logging)
return {LOG_NONE, false};
bool also_log_to_stderr = false;
#if !defined(NDEBUG)
if (std::optional<std::string> also_log_to_stderr_str =
base::Environment::Create()->GetVar("ELECTRON_ALSO_LOG_TO_STDERR"))
also_log_to_stderr = !also_log_to_stderr_str->empty();
#endif
#if BUILDFLAG(IS_WIN)
if (logging_destination == "handle" &&
command_line.HasSwitch(::switches::kProcessType) &&
command_line.HasSwitch(::switches::kLogFile)) {
// Child processes can log to a handle duplicated from the parent, and
// provided in the log-file switch value.
return {LOG_TO_FILE, true};
}
#endif // BUILDFLAG(IS_WIN)
// --enable-logging logs to stderr, --enable-logging=file logs to a file.
// NB. this differs from Chromium, in which --enable-logging logs to a file
// and --enable-logging=stderr logs to stderr, because that's how Electron
// used to work, so in order to not break anyone who was depending on
// --enable-logging logging to stderr, we preserve the old behavior by
// default.
// If --log-file or ELECTRON_LOG_FILE is specified along with
// --enable-logging, return LOG_TO_FILE.
// If we're in the pre-init phase, before JS has run, we want to avoid
// logging to the default log file, which is inside the user data directory,
// because we aren't able to accurately determine the user data directory
// before JS runs. Instead, log to stderr unless there's an explicit filename
// given.
if (HasExplicitLogFile(command_line) ||
(logging_destination == "file" && !is_preinit))
return {LOG_TO_FILE | (also_log_to_stderr ? LOG_TO_STDERR : 0), false};
return {LOG_TO_SYSTEM_DEBUG_LOG | LOG_TO_STDERR, false};
}
} // namespace
void InitElectronLogging(const base::CommandLine& command_line,
bool is_preinit) {
const std::string process_type =
command_line.GetSwitchValueASCII(::switches::kProcessType);
auto [logging_dest, filename_is_handle] =
DetermineLoggingDestination(command_line, is_preinit);
LogLockingState log_locking_state = LOCK_LOG_FILE;
base::FilePath log_path;
#if BUILDFLAG(IS_WIN)
base::win::ScopedHandle log_handle;
#endif
if (command_line.HasSwitch(::switches::kLoggingLevel) &&
GetMinLogLevel() >= 0) {
std::string log_level =
command_line.GetSwitchValueASCII(::switches::kLoggingLevel);
int level = 0;
if (base::StringToInt(log_level, &level) && level >= 0 &&
level < LOGGING_NUM_SEVERITIES) {
SetMinLogLevel(level);
} else {
DLOG(WARNING) << "Bad log level: " << log_level;
}
}
// Don't resolve the log path unless we need to. Otherwise we leave an open
// ALPC handle after sandbox lockdown on Windows.
if ((logging_dest & LOG_TO_FILE) != 0) {
if (filename_is_handle) {
#if BUILDFLAG(IS_WIN)
// Child processes on Windows are provided a file handle if logging is
// enabled as sandboxed processes cannot open files.
log_handle = GetLogInheritedHandle(command_line);
if (!log_handle.is_valid()) {
LOG(ERROR) << "Unable to initialize logging from handle.";
return;
}
#endif
} else {
log_path = GetLogFileName(command_line);
}
} else {
log_locking_state = DONT_LOCK_LOG_FILE;
}
// On Windows, having non canonical forward slashes in log file name causes
// problems with sandbox filters, see https://crbug.com/859676
log_path = log_path.NormalizePathSeparators();
LoggingSettings settings;
settings.logging_dest = logging_dest;
settings.log_file_path = log_path.value().c_str();
#if BUILDFLAG(IS_WIN)
// Avoid initializing with INVALID_HANDLE_VALUE.
// This handle is owned by the logging framework and is closed when the
// process exits.
// TODO(crbug.com/328285906) Use a ScopedHandle in logging settings.
settings.log_file = log_handle.is_valid() ? log_handle.release() : nullptr;
#endif
settings.lock_log = log_locking_state;
// If we're logging to an explicit file passed with --log-file, we don't want
// to delete the log file on our second initialization.
settings.delete_old =
process_type.empty() && (is_preinit || !HasExplicitLogFile(command_line))
? DELETE_OLD_LOG_FILE
: APPEND_TO_OLD_LOG_FILE;
bool success = InitLogging(settings);
if (!success) {
PLOG(ERROR) << "Failed to init logging";
}
SetLogItems(true /* pid */, false, true /* timestamp */, false);
}
} // namespace logging