diff --git a/BUILD.gn b/BUILD.gn index 27d1677d9..05671d1d2 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -851,6 +851,7 @@ source_set("libcef_static") { "libcef/common/task_runner_impl.h", "libcef/common/task_runner_manager.cc", "libcef/common/task_runner_manager.h", + "libcef/common/task_util.h", "libcef/common/thread_impl.cc", "libcef/common/thread_impl.h", "libcef/common/time_impl.cc", diff --git a/libcef/common/file_util_impl.cc b/libcef/common/file_util_impl.cc index a4269ddc4..6c69993f2 100644 --- a/libcef/common/file_util_impl.cc +++ b/libcef/common/file_util_impl.cc @@ -6,13 +6,14 @@ #include "base/files/file_util.h" #include "base/logging.h" #include "cef/include/cef_file_util.h" -#include "cef/include/cef_task.h" +#include "cef/libcef/common/task_util.h" #include "third_party/zlib/google/zip.h" namespace { bool AllowFileIO() { - if (CefCurrentlyOn(TID_UI) || CefCurrentlyOn(TID_IO)) { + // Use internal variant that doesn't log before CefInitialize. + if (cef::CurrentlyOnThread(TID_UI) || cef::CurrentlyOnThread(TID_IO)) { DCHECK(false) << "file IO is not allowed on the current thread"; return false; } diff --git a/libcef/common/task_impl.cc b/libcef/common/task_impl.cc index ea7353ef6..9cc5f38eb 100644 --- a/libcef/common/task_impl.cc +++ b/libcef/common/task_impl.cc @@ -8,6 +8,7 @@ #include "base/time/time.h" #include "cef/include/cef_task.h" #include "cef/libcef/common/task_runner_impl.h" +#include "cef/libcef/common/task_util.h" bool CefCurrentlyOn(CefThreadId threadId) { scoped_refptr task_runner = @@ -46,3 +47,19 @@ bool CefPostDelayedTask(CefThreadId threadId, LOG(WARNING) << "No task runner for threadId " << threadId; return false; } + +namespace cef { + +bool CurrentlyOnThread(CefThreadId thread_id) { + scoped_refptr task_runner = + CefTaskRunnerImpl::GetTaskRunner(thread_id); + if (task_runner.get()) { + return task_runner->RunsTasksInCurrentSequence(); + } + + // Return false silently when task runners are not initialized (before + // CefInitialize). This avoids logging that cannot be configured. + return false; +} + +} // namespace cef diff --git a/libcef/common/task_util.h b/libcef/common/task_util.h new file mode 100644 index 000000000..a2f247d70 --- /dev/null +++ b/libcef/common/task_util.h @@ -0,0 +1,21 @@ +// Copyright 2025 The Chromium Embedded Framework Authors. All rights +// reserved. Use of this source code is governed by a BSD-style license that can +// be found in the LICENSE file. + +#ifndef CEF_LIBCEF_COMMON_TASK_UTIL_H_ +#define CEF_LIBCEF_COMMON_TASK_UTIL_H_ +#pragma once + +#include "cef/include/cef_task.h" + +namespace cef { + +// Internal variant of CefCurrentlyOn that does not log when called before +// CefInitialize (when task runners are not yet initialized). Should only be +// used by libcef internal code that may be called before initialization. +// Returns false when task runners are not initialized. +bool CurrentlyOnThread(CefThreadId thread_id); + +} // namespace cef + +#endif // CEF_LIBCEF_COMMON_TASK_UTIL_H_ diff --git a/libcef/common/waitable_event_impl.cc b/libcef/common/waitable_event_impl.cc index ffcf8f1c6..18dc612dd 100644 --- a/libcef/common/waitable_event_impl.cc +++ b/libcef/common/waitable_event_impl.cc @@ -6,12 +6,13 @@ #include "base/notreached.h" #include "base/time/time.h" -#include "cef/include/cef_task.h" +#include "cef/libcef/common/task_util.h" namespace { bool AllowWait() { - if (CefCurrentlyOn(TID_UI) || CefCurrentlyOn(TID_IO)) { + // Use internal variant that doesn't log before CefInitialize. + if (cef::CurrentlyOnThread(TID_UI) || cef::CurrentlyOnThread(TID_IO)) { DCHECK(false) << "waiting is not allowed on the current thread"; return false; }