Skip to content

Commit 5ae8e21

Browse files
DragonDragonDDDjianliang00
authored andcommitted
[Feature][DevTool] Add global memory usage CDP plumbing
Add the first part of the global memory query pipeline for DevTool. The commit registers Memory.getAllMemoryUsage in the Memory domain and routes both global and instance dispatch through LynxGlobalDevToolMediator. The mediator validates the optional timeoutMs parameter, treats missing or zero timeoutMs values as the default wait policy, rejects negative values, rejects values outside the int64 and five-minute bounds, forwards accepted requests to GlobalDevToolPlatformFacade, and keeps CDP envelope construction in one place. Introduce a move-only MemoryUsageCallback for the facade and embedder hook because the platform completion is request-scoped and consumed once. GlobalDevToolPlatformFacade now provides a default unsupported response for source compatibility with out-of-tree facade implementations, while Android, Darwin, and generic embedder fallbacks return an empty JSON result object together with an explicit unsupported-bridge error. Add focused InspectorMemoryAgent coverage for successful platform results, invalid params, invalid timeoutMs values, default timeout normalization, negative timeout rejection, oversized timeout rejection, uint64 overflow rejection, platform error forwarding, malformed platform JSON, and unknown Memory methods. Move the test facade GetInstance implementation into the mock .cc file so the non-inline singleton override is defined once across DevTool test translation units. The Memory-agent tests use a test-local condition-variable backed MessageSender and explicitly initialize the UIThread runner before constructing the global mediator, so task-runner message delivery cannot race with assertions, time out on delivered messages with empty payloads, or block waiting for UIThread initialization without changing shared DevTool test mocks. AutoSubmit: true
1 parent 9b67fcf commit 5ae8e21

15 files changed

Lines changed: 539 additions & 7 deletions

devtool/embedder/core/global_devtool_platform_embedder.cc

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
#include "devtool/embedder/core/global_devtool_platform_embedder.h"
66

7+
#include <utility>
8+
79
#if ENABLE_TRACE_PERFETTO || ENABLE_TRACE_SYSTRACE
810
#include "base/trace/native/trace_controller.h"
911
#endif
@@ -23,6 +25,13 @@ class GlobalDevtoolPlatformCommon
2325
GlobalDevtoolPlatformEmbedder::StopMemoryTracing();
2426
}
2527

28+
void GetAllMemoryUsage(
29+
int64_t timeout_ms,
30+
GlobalDevToolPlatformFacade::MemoryUsageCallback callback) override {
31+
GlobalDevtoolPlatformEmbedder::GetAllMemoryUsage(timeout_ms,
32+
std::move(callback));
33+
}
34+
2635
lynx::trace::TraceController* GetTraceController() override {
2736
#if ENABLE_TRACE_PERFETTO || ENABLE_TRACE_SYSTRACE
2837
return lynx::trace::GetTraceControllerInstance();
@@ -50,6 +59,23 @@ void GlobalDevtoolPlatformEmbedder::StartMemoryTracing() {}
5059

5160
void GlobalDevtoolPlatformEmbedder::StopMemoryTracing() {}
5261

62+
void GlobalDevtoolPlatformEmbedder::GetAllMemoryUsage(
63+
int64_t timeout_ms,
64+
base::MoveOnlyClosure<void, const std::string&, const std::string&>
65+
callback) {
66+
(void)timeout_ms;
67+
// The generic embedder has no host-specific memory bridge. Keep the CDP
68+
// plumbing honest by returning an explicit error until embedders provide
69+
// their own memory counters through this static hook.
70+
constexpr char kBridgeUnavailable[] =
71+
"Memory.getAllMemoryUsage is not available in this Lynx runtime. "
72+
"Upgrade the Lynx runtime and DevTool platform integration to a version "
73+
"with the global memory bridge.";
74+
if (callback) {
75+
std::move(callback)("{}", kBridgeUnavailable);
76+
}
77+
}
78+
5379
GlobalDevToolPlatformFacade& GlobalDevToolPlatformFacade::GetInstance() {
5480
static GlobalDevtoolPlatformCommon instance;
5581
return instance;

devtool/embedder/core/global_devtool_platform_embedder.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,12 @@
55
#ifndef DEVTOOL_EMBEDDER_CORE_GLOBAL_DEVTOOL_PLATFORM_EMBEDDER_H_
66
#define DEVTOOL_EMBEDDER_CORE_GLOBAL_DEVTOOL_PLATFORM_EMBEDDER_H_
77

8+
#include <cstdint>
89
#include <memory>
910
#include <string>
1011

12+
#include "base/include/closure.h"
13+
1114
namespace lynx {
1215
namespace devtool {
1316

@@ -19,6 +22,10 @@ class GlobalDevtoolPlatformEmbedder
1922

2023
static void StartMemoryTracing();
2124
static void StopMemoryTracing();
25+
static void GetAllMemoryUsage(
26+
int64_t timeout_ms,
27+
base::MoveOnlyClosure<void, const std::string&, const std::string&>
28+
callback);
2229
};
2330

2431
} // namespace devtool

devtool/lynx_devtool/agent/android/global_devtool_platform_android.cc

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
#include <sys/system_properties.h>
88

9+
#include <utility>
10+
911
#include "core/base/android/jni_helper.h"
1012
#include "devtool/lynx_devtool/agent/lynx_global_devtool_mediator.h"
1113
#include "platform/android/lynx_devtool/src/main/jni/gen/GlobalDevToolPlatformAndroidDelegate_jni.h"
@@ -26,6 +28,11 @@ bool RegisterJNIForGlobalDevToolPlatformAndroidDelegate(JNIEnv* env) {
2628
namespace lynx {
2729
namespace devtool {
2830

31+
constexpr char kAndroidMemoryUsageBridgeUnavailable[] =
32+
"Memory.getAllMemoryUsage is not available in this Android Lynx runtime. "
33+
"Upgrade the Lynx runtime and DevTool platform integration to a version "
34+
"with the global memory bridge.";
35+
2936
GlobalDevToolPlatformFacade& GlobalDevToolPlatformFacade::GetInstance() {
3037
static base::NoDestructor<GlobalDevToolPlatformAndroid> instance;
3138
return *(instance.get());
@@ -41,6 +48,18 @@ void GlobalDevToolPlatformAndroid::StopMemoryTracing() {
4148
Java_GlobalDevToolPlatformAndroidDelegate_stopMemoryTracing(env);
4249
}
4350

51+
void GlobalDevToolPlatformAndroid::GetAllMemoryUsage(
52+
int64_t timeout_ms, MemoryUsageCallback callback) {
53+
(void)timeout_ms;
54+
// This CDP method needs the Android-side memory bridge that lands in the
55+
// platform integration. Reaching this fallback means the current runtime only
56+
// contains the global CDP plumbing, so clients should upgrade the Lynx
57+
// runtime and DevTool platform integration to a version with that bridge.
58+
if (callback) {
59+
std::move(callback)("{}", kAndroidMemoryUsageBridgeUnavailable);
60+
}
61+
}
62+
4463
#if ENABLE_TRACE_PERFETTO || ENABLE_TRACE_SYSTRACE
4564
lynx::trace::TraceController*
4665
GlobalDevToolPlatformAndroid::GetTraceController() {

devtool/lynx_devtool/agent/android/global_devtool_platform_android.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
#include <jni.h>
99

10+
#include <cstdint>
11+
1012
#include "base/include/no_destructor.h"
1113
#include "base/include/platform/android/scoped_java_ref.h"
1214
#include "devtool/lynx_devtool/agent/global_devtool_platform_facade.h"
@@ -17,6 +19,8 @@ class GlobalDevToolPlatformAndroid : public GlobalDevToolPlatformFacade {
1719
public:
1820
void StartMemoryTracing() override;
1921
void StopMemoryTracing() override;
22+
void GetAllMemoryUsage(int64_t timeout_ms,
23+
MemoryUsageCallback callback) override;
2024

2125
#if ENABLE_TRACE_PERFETTO || ENABLE_TRACE_SYSTRACE
2226
// The following functions are used for tracing agent.

devtool/lynx_devtool/agent/domain_agent/inspector_memory_agent.cc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ namespace devtool {
1212
InspectorMemoryAgent::InspectorMemoryAgent() {
1313
functions_map_["Memory.startTracing"] = &InspectorMemoryAgent::StartTracing;
1414
functions_map_["Memory.stopTracing"] = &InspectorMemoryAgent::StopTracing;
15+
functions_map_["Memory.getAllMemoryUsage"] =
16+
&InspectorMemoryAgent::GetAllMemoryUsage;
1517
}
1618

1719
InspectorMemoryAgent::~InspectorMemoryAgent() = default;
@@ -26,6 +28,12 @@ void InspectorMemoryAgent::StopTracing(
2628
LynxGlobalDevToolMediator::GetInstance().MemoryStopTracing(sender, message);
2729
}
2830

31+
void InspectorMemoryAgent::GetAllMemoryUsage(
32+
const std::shared_ptr<MessageSender>& sender, const Json::Value& message) {
33+
LynxGlobalDevToolMediator::GetInstance().MemoryGetAllMemoryUsage(sender,
34+
message);
35+
}
36+
2937
void InspectorMemoryAgent::CallMethod(
3038
const std::shared_ptr<MessageSender>& sender, const Json::Value& content) {
3139
std::string method = content["method"].asString();

devtool/lynx_devtool/agent/domain_agent/inspector_memory_agent.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ class InspectorMemoryAgent : public CDPDomainAgentBase {
2525
const Json::Value& message);
2626
void StopTracing(const std::shared_ptr<MessageSender>& sender,
2727
const Json::Value& message);
28+
void GetAllMemoryUsage(const std::shared_ptr<MessageSender>& sender,
29+
const Json::Value& message);
2830

2931
std::map<std::string, MemoryAgentMethod> functions_map_;
3032
};

0 commit comments

Comments
 (0)