Skip to content

Commit

Permalink
Add CefTaskManager API
Browse files Browse the repository at this point in the history
See https://tests/task_manager example in cefclient.
  • Loading branch information
nik-sp authored and magreenblatt committed Jul 18, 2024
1 parent cbbca05 commit d470cf8
Show file tree
Hide file tree
Showing 24 changed files with 1,242 additions and 19 deletions.
2 changes: 2 additions & 0 deletions BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -678,6 +678,8 @@ source_set("libcef_static") {
"libcef/browser/ssl_status_impl.h",
"libcef/browser/stream_impl.cc",
"libcef/browser/stream_impl.h",
"libcef/browser/task_manager_impl.cc",
"libcef/browser/task_manager_impl.h",
"libcef/browser/trace_impl.cc",
"libcef/browser/trace_subscriber.cc",
"libcef/browser/trace_subscriber.h",
Expand Down
8 changes: 7 additions & 1 deletion cef_paths.gypi
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
# by hand. See the translator.README.txt file in the tools directory for
# more information.
#
# $hash=29c508f2fb23e0ed2899163039384fb0a230be70$
# $hash=e092ed49124ef5969b2052fd6ae5f6e6d89390ba$
#

{
Expand Down Expand Up @@ -81,6 +81,7 @@
'include/cef_stream.h',
'include/cef_string_visitor.h',
'include/cef_task.h',
'include/cef_task_manager.h',
'include/cef_thread.h',
'include/cef_trace.h',
'include/cef_unresponsive_process_callback.h',
Expand Down Expand Up @@ -184,6 +185,7 @@
'include/capi/cef_stream_capi.h',
'include/capi/cef_string_visitor_capi.h',
'include/capi/cef_task_capi.h',
'include/capi/cef_task_manager_capi.h',
'include/capi/cef_thread_capi.h',
'include/capi/cef_trace_capi.h',
'include/capi/cef_unresponsive_process_callback_capi.h',
Expand Down Expand Up @@ -460,6 +462,8 @@
'libcef_dll/ctocpp/string_visitor_ctocpp.h',
'libcef_dll/ctocpp/task_ctocpp.cc',
'libcef_dll/ctocpp/task_ctocpp.h',
'libcef_dll/cpptoc/task_manager_cpptoc.cc',
'libcef_dll/cpptoc/task_manager_cpptoc.h',
'libcef_dll/cpptoc/task_runner_cpptoc.cc',
'libcef_dll/cpptoc/task_runner_cpptoc.h',
'libcef_dll/cpptoc/test/test_server_cpptoc.cc',
Expand Down Expand Up @@ -784,6 +788,8 @@
'libcef_dll/cpptoc/string_visitor_cpptoc.h',
'libcef_dll/cpptoc/task_cpptoc.cc',
'libcef_dll/cpptoc/task_cpptoc.h',
'libcef_dll/ctocpp/task_manager_ctocpp.cc',
'libcef_dll/ctocpp/task_manager_ctocpp.h',
'libcef_dll/ctocpp/task_runner_ctocpp.cc',
'libcef_dll/ctocpp/task_runner_ctocpp.h',
'libcef_dll/ctocpp/test/test_server_ctocpp.cc',
Expand Down
3 changes: 3 additions & 0 deletions cef_paths2.gypi
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,8 @@
'tests/cefclient/browser/scheme_test.h',
'tests/cefclient/browser/server_test.cc',
'tests/cefclient/browser/server_test.h',
'tests/cefclient/browser/task_manager_test.cc',
'tests/cefclient/browser/task_manager_test.h',
'tests/cefclient/browser/temp_window.h',
'tests/cefclient/browser/test_runner.cc',
'tests/cefclient/browser/test_runner.h',
Expand Down Expand Up @@ -332,6 +334,7 @@
'tests/cefclient/resources/preferences.html',
'tests/cefclient/resources/response_filter.html',
'tests/cefclient/resources/server.html',
'tests/cefclient/resources/task_manager.html',
'tests/cefclient/resources/transparency.html',
'tests/cefclient/resources/urlrequest.html',
'tests/cefclient/resources/websocket.html',
Expand Down
107 changes: 107 additions & 0 deletions include/capi/cef_task_manager_capi.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
// Copyright (c) 2024 Marshall A. Greenblatt. All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
// * Neither the name of Google Inc. nor the name Chromium Embedded
// Framework nor the names of its contributors may be used to endorse
// or promote products derived from this software without specific prior
// written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// ---------------------------------------------------------------------------
//
// This file was generated by the CEF translator tool and should not edited
// by hand. See the translator.README.txt file in the tools directory for
// more information.
//
// $hash=944e04f8c3213981c3955d6b5ede036b887d4d9e$
//

#ifndef CEF_INCLUDE_CAPI_CEF_TASK_MANAGER_CAPI_H_
#define CEF_INCLUDE_CAPI_CEF_TASK_MANAGER_CAPI_H_
#pragma once

#include "include/capi/cef_base_capi.h"

#ifdef __cplusplus
extern "C" {
#endif

///
/// Structure that facilitates managing the browser-related tasks. The functions
/// of this structure may only be called on the UI thread.
///
typedef struct _cef_task_manager_t {
///
/// Base structure.
///
cef_base_ref_counted_t base;

///
/// Returns the number of tasks currently tracked by the task manager. Returns
/// 0 if the function was called from the incorrect thread.
///
size_t(CEF_CALLBACK* get_tasks_count)(struct _cef_task_manager_t* self);

///
/// Gets the list of task IDs currently tracked by the task manager. Tasks
/// that share the same process id will always be consecutive. The list will
/// be sorted in a way that reflects the process tree: the browser process
/// will be first, followed by the gpu process if it exists. Related processes
/// (e.g., a subframe process and its parent) will be kept together if
/// possible. Callers can expect this ordering to be stable when a process is
/// added or removed. The task IDs are unique within the application lifespan.
/// Returns false (0) if the function was called from the incorrect thread.
///
int(CEF_CALLBACK* get_task_ids_list)(struct _cef_task_manager_t* self,
size_t* task_idsCount,
int64_t* task_ids);

///
/// Gets information about the task with |task_id|. Returns true (1) if the
/// information about the task was successfully retrieved and false (0) if the
/// |task_id| is invalid or the function was called from the incorrect thread.
///
int(CEF_CALLBACK* get_task_info)(struct _cef_task_manager_t* self,
int64_t task_id,
struct _cef_task_info_t* info);

///
/// Attempts to terminate a task with |task_id|. Returns false (0) if the
/// |task_id| is invalid, the call is made from an incorrect thread, or if the
/// task cannot be terminated.
///
int(CEF_CALLBACK* kill_task)(struct _cef_task_manager_t* self,
int64_t task_id);
} cef_task_manager_t;

///
/// Returns the global task manager object. Returns nullptr if the function was
/// called from the incorrect thread.
///
CEF_EXPORT cef_task_manager_t* cef_task_manager_get(void);

#ifdef __cplusplus
}
#endif

#endif // CEF_INCLUDE_CAPI_CEF_TASK_MANAGER_CAPI_H_
8 changes: 4 additions & 4 deletions include/cef_api_hash.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,13 @@
// way that may cause binary incompatibility with other builds. The universal
// hash value will change if any platform is affected whereas the platform hash
// values will change only if that particular platform is affected.
#define CEF_API_HASH_UNIVERSAL "ea36c0aa57b37523b5677f107729dbf4d0b23933"
#define CEF_API_HASH_UNIVERSAL "af31a52a9b7a67e9e43aecc3dca57be096bd3c52"
#if defined(OS_WIN)
#define CEF_API_HASH_PLATFORM "42cdaa02d9babfd5409c5d7cb4abf77ea9f0b8d4"
#define CEF_API_HASH_PLATFORM "b24a9d79201dabc26957dc207746d8b6248e0e58"
#elif defined(OS_MAC)
#define CEF_API_HASH_PLATFORM "832e89ccb2ee279fb7b6fe9a30c83863ffa42fa1"
#define CEF_API_HASH_PLATFORM "1b1e2db70d324c123a5be09fd9c528acbf49b9a4"
#elif defined(OS_LINUX)
#define CEF_API_HASH_PLATFORM "7b768d2e27822636321fb437be8d17cb0c3b7809"
#define CEF_API_HASH_PLATFORM "9d549b9917bc2f0be789a75179e1aeea134798ef"
#endif

#ifdef __cplusplus
Expand Down
100 changes: 100 additions & 0 deletions include/cef_task_manager.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
// Copyright (c) 2024 Marshall A. Greenblatt. All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
// * Neither the name of Google Inc. nor the name Chromium Embedded
// Framework nor the names of its contributors may be used to endorse
// or promote products derived from this software without specific prior
// written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// ---------------------------------------------------------------------------
//
// The contents of this file must follow a specific format in order to
// support the CEF translator tool. See the translator.README.txt file in the
// tools directory for more information.
//

#ifndef CEF_INCLUDE_CEF_TASK_MANAGER_H_
#define CEF_INCLUDE_CEF_TASK_MANAGER_H_
#pragma once

#include <vector>

#include "include/cef_base.h"

///
/// Class that facilitates managing the browser-related tasks.
/// The methods of this class may only be called on the UI thread.
///
/*--cef(source=library)--*/
class CefTaskManager : public virtual CefBaseRefCounted {
public:
typedef std::vector<int64_t> TaskIdList;
typedef cef_task_type_t TaskType;

///
/// Returns the global task manager object.
/// Returns nullptr if the method was called from the incorrect thread.
///
/*--cef()--*/
static CefRefPtr<CefTaskManager> GetTaskManager();

///
/// Returns the number of tasks currently tracked by the task manager.
/// Returns 0 if the method was called from the incorrect thread.
///
/*--cef()--*/
virtual size_t GetTasksCount() = 0;

///
/// Gets the list of task IDs currently tracked by the task manager. Tasks
/// that share the same process id will always be consecutive. The list will
/// be sorted in a way that reflects the process tree: the browser process
/// will be first, followed by the gpu process if it exists. Related processes
/// (e.g., a subframe process and its parent) will be kept together if
/// possible. Callers can expect this ordering to be stable when a process is
/// added or removed. The task IDs are unique within the application lifespan.
/// Returns false if the method was called from the incorrect thread.
///
/*--cef(count_func=task_ids:GetTasksCount)--*/
virtual bool GetTaskIdsList(TaskIdList& task_ids) = 0;

///
/// Gets information about the task with |task_id|.
/// Returns true if the information about the task was successfully
/// retrieved and false if the |task_id| is invalid or the method was called
/// from the incorrect thread.
///
/*--cef()--*/
virtual bool GetTaskInfo(int64_t task_id, CefTaskInfo& info) = 0;

///
/// Attempts to terminate a task with |task_id|.
/// Returns false if the |task_id| is invalid, the call is made from an
/// incorrect thread, or if the task cannot be terminated.
///
/*--cef()--*/
virtual bool KillTask(int64_t task_id) = 0;
};

#endif // CEF_INCLUDE_CEF_TASK_MANAGER_H_
61 changes: 61 additions & 0 deletions include/internal/cef_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -3812,6 +3812,67 @@ typedef enum {
CEF_COLOR_VARIANT_EXPRESSIVE,
} cef_color_variant_t;

///
/// Specifies the task type variants supported by CefTaskManager.
/// Should be kept in sync with Chromium's task_manager::Task::Type type.
///
typedef enum {
CEF_TASK_TYPE_UNKNOWN = 0,
/// The main browser process.
CEF_TASK_TYPE_BROWSER,
/// A graphics process.
CEF_TASK_TYPE_GPU,
/// A Linux zygote process.
CEF_TASK_TYPE_ZYGOTE,
/// A browser utility process.
CEF_TASK_TYPE_UTILITY,
/// A normal WebContents renderer process.
CEF_TASK_TYPE_RENDERER,
/// An extension or app process.
CEF_TASK_TYPE_EXTENSION,
/// A browser plugin guest process.
CEF_TASK_TYPE_GUEST,
/// A plugin process.
CEF_TASK_TYPE_PLUGIN,
/// A sandbox helper process
CEF_TASK_TYPE_SANDBOX_HELPER,
/// A dedicated worker running on the renderer process.
CEF_TASK_TYPE_DEDICATED_WORKER,
/// A shared worker running on the renderer process.
CEF_TASK_TYPE_SHARED_WORKER,
/// A service worker running on the renderer process.
CEF_TASK_TYPE_SERVICE_WORKER,
} cef_task_type_t;

///
/// Structure representing task information provided by CefTaskManager.
///
typedef struct _cef_task_info_t {
/// The task ID.
int64_t id;
/// The task type.
cef_task_type_t type;
/// Set to true (1) if the task is killable.
int is_killable;
/// The task title.
cef_string_t title;
/// The CPU usage of the process on which the task is running. The value is
/// in the range zero to number_of_processors * 100%.
double cpu_usage;
/// The number of processors available on the system.
int number_of_processors;
/// The memory footprint of the task in bytes. A value of -1 means no valid
/// value is currently available.
int64_t memory;
/// The GPU memory usage of the task in bytes. A value of -1 means no valid
/// value is currently available.
int64_t gpu_memory;
/// Set to true (1) if this task process' GPU resource count is inflated
/// because it is counting other processes' resources (e.g, the GPU process
/// has this value set to true because it is the aggregate of all processes).
int is_gpu_memory_inflated;
} cef_task_info_t;

#ifdef __cplusplus
}
#endif
Expand Down
27 changes: 27 additions & 0 deletions include/internal/cef_types_wrappers.h
Original file line number Diff line number Diff line change
Expand Up @@ -756,4 +756,31 @@ class CefAcceleratedPaintInfo : public cef_accelerated_paint_info_t {
: cef_accelerated_paint_info_t(r) {}
};

struct CefTaskInfoTraits {
using struct_type = cef_task_info_t;

static inline void init(struct_type* s) {}

static inline void clear(struct_type* s) { cef_string_clear(&s->title); }

static inline void set(const struct_type* src,
struct_type* target,
bool copy) {
target->id = src->id;
target->type = src->type;
target->is_killable = src->is_killable;
cef_string_set(src->title.str, src->title.length, &target->title, copy);
target->cpu_usage = src->cpu_usage;
target->number_of_processors = src->number_of_processors;
target->memory = src->memory;
target->gpu_memory = src->gpu_memory;
target->is_gpu_memory_inflated = src->is_gpu_memory_inflated;
}
};

///
/// Class representing task information.
///
using CefTaskInfo = CefStructBase<CefTaskInfoTraits>;

#endif // CEF_INCLUDE_INTERNAL_CEF_TYPES_WRAPPERS_H_
Loading

0 comments on commit d470cf8

Please sign in to comment.