Skip to content

Commit d2543e9

Browse files
committed
fix(inspector): classify cache commands alike on both threads, and report a failed clear
- The JS-thread check matched the raw text, so an escaped method name (Network.setCacheDisabled) took effect on the socket thread but went to V8 for its reply. Messages that contain a \u escape are now parsed too. - The socket thread took any number as the id (5.5 as 5) and the JS thread only an integer (not 5.0). Both now use GetInt32: an integral int32, 5.0 included. - A Network.clearBrowserCache whose clear threw was answered with success. The socket thread now answers it with a -32000 error, for every session, and the command goes no further.
1 parent ff5d57f commit d2543e9

2 files changed

Lines changed: 47 additions & 27 deletions

File tree

‎test-app/runtime/src/main/cpp/JsV8InspectorClient.cpp‎

Lines changed: 45 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,20 @@ std::string FinishResponse(json& response, const std::string& sessionId) {
6565
return JsonDump(response);
6666
}
6767

68+
// CDP integers are int32: Chromium's protocol bindings reject anything else,
69+
// though an integral double such as 5.0 still counts.
70+
bool GetInt32(const json& value, long long& out) {
71+
if (!value.is_number()) {
72+
return false;
73+
}
74+
double number = value.get<double>();
75+
if (number != std::trunc(number) || number < INT32_MIN || number > INT32_MAX) {
76+
return false;
77+
}
78+
out = static_cast<long long>(number);
79+
return true;
80+
}
81+
6882
bool IsNetworkCacheMethod(const std::string& method) {
6983
return method == "Network.setCacheDisabled" || method == "Network.clearBrowserCache";
7084
}
@@ -92,20 +106,26 @@ json NetworkCacheReply(long long id, const std::string& method, const json& mess
92106
}
93107

94108
// `message` parsed, when it is a Network.setCacheDisabled or
95-
// Network.clearBrowserCache command with an id for the main session.
109+
// Network.clearBrowserCache command with an id for the main session, its id
110+
// as an integer. Classified the way handleMessageOnSocketThread classifies
111+
// it: the text check only skips the parse for messages that can't name either
112+
// method, and a \u escape could spell the name.
96113
std::optional<json> ParseMainSessionNetworkCacheCommand(const std::string& message) {
97114
if (message.find("Network.setCacheDisabled") == std::string::npos &&
98-
message.find("Network.clearBrowserCache") == std::string::npos) {
115+
message.find("Network.clearBrowserCache") == std::string::npos &&
116+
message.find("\\u") == std::string::npos) {
99117
return std::nullopt;
100118
}
101119
auto parsed = json::parse(message, nullptr, false);
120+
long long id = -1;
102121
if (parsed.is_discarded() || !parsed.is_object() || !parsed.contains("method") ||
103122
!parsed["method"].is_string() || !IsNetworkCacheMethod(parsed["method"].get<std::string>()) ||
104-
!parsed.contains("id") || !parsed["id"].is_number_integer() ||
123+
!parsed.contains("id") || !GetInt32(parsed["id"], id) ||
105124
(parsed.contains("sessionId") && parsed["sessionId"].is_string() &&
106125
!parsed["sessionId"].get<std::string>().empty())) {
107126
return std::nullopt;
108127
}
128+
parsed["id"] = id;
109129
return parsed;
110130
}
111131

@@ -238,20 +258,6 @@ size_t TruncateUTF8ToByteSize(const uint8_t* data, size_t length) {
238258
return 0;
239259
}
240260

241-
// CDP integers are int32: Chromium's protocol bindings reject anything else,
242-
// though an integral double such as 5.0 still counts.
243-
bool GetInt32(const json& value, long long& out) {
244-
if (!value.is_number()) {
245-
return false;
246-
}
247-
double number = value.get<double>();
248-
if (number != std::trunc(number) || number < INT32_MIN || number > INT32_MAX) {
249-
return false;
250-
}
251-
out = static_cast<long long>(number);
252-
return true;
253-
}
254-
255261
// Opt-out via nativescript.config.ts (serialized into the bundled
256262
// package.json): `android: { disableSourceMapURLRewrite: true }`, or the same
257263
// key at the top level.
@@ -602,9 +608,10 @@ bool JsV8InspectorClient::handleMessageOnSocketThread(const std::string& message
602608
std::string method = parsed.contains("method") && parsed["method"].is_string()
603609
? parsed["method"].get<std::string>()
604610
: "";
605-
long long msgId = parsed.contains("id") && parsed["id"].is_number()
606-
? parsed["id"].get<long long>()
607-
: -1;
611+
long long msgId = -1;
612+
if (parsed.contains("id")) {
613+
GetInt32(parsed["id"], msgId);
614+
}
608615
std::string sessionId = parsed.contains("sessionId") && parsed["sessionId"].is_string()
609616
? parsed["sessionId"].get<std::string>()
610617
: "";
@@ -780,7 +787,15 @@ bool JsV8InspectorClient::handleMessageOnSocketThread(const std::string& message
780787
}
781788
}
782789
if (method == "Network.clearBrowserCache") {
783-
ClearModuleHttpCacheFromInspector();
790+
// A failed clear is answered here, for every session, so the JS
791+
// handler can't report success for it.
792+
if (std::optional<std::string> failure = ClearModuleHttpCacheFromInspector()) {
793+
if (msgId >= 0) {
794+
json error = {{"id", msgId}, {"error", {{"code", -32000}, {"message", *failure}}}};
795+
response = FinishResponse(error, sessionId);
796+
}
797+
return true;
798+
}
784799
} else if (std::optional<bool> cacheDisabled = SetCacheDisabledParam(parsed)) {
785800
SetCacheDisabledBy(socket, *cacheDisabled);
786801
}
@@ -820,15 +835,20 @@ bool JsV8InspectorClient::handleMessageOnSocketThread(const std::string& message
820835
return false;
821836
}
822837

823-
void JsV8InspectorClient::ClearModuleHttpCacheFromInspector() {
838+
std::optional<std::string> JsV8InspectorClient::ClearModuleHttpCacheFromInspector() {
839+
std::string failure = "Could not clear the HTTP module cache";
824840
try {
825841
ClearModuleHttpCache();
842+
return std::nullopt;
826843
} catch (NativeScriptException& e) {
827-
DEBUG_WRITE_FORCE("Network.clearBrowserCache: could not clear the HTTP module cache: %s",
828-
e.GetErrorMessage().c_str());
844+
const std::string detail = e.GetErrorMessage();
845+
if (!detail.empty()) {
846+
failure += ": " + detail;
847+
}
829848
} catch (...) {
830-
DEBUG_WRITE_FORCE("Network.clearBrowserCache: could not clear the HTTP module cache");
831849
}
850+
DEBUG_WRITE_FORCE("Network.clearBrowserCache: %s", failure.c_str());
851+
return failure;
832852
}
833853

834854
void JsV8InspectorClient::SetCacheDisabledBy(jobject socket, bool cacheDisabled) {

‎test-app/runtime/src/main/cpp/JsV8InspectorClient.h‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,8 @@ class JsV8InspectorClient : V8InspectorClient, v8_inspector::V8Inspector::Channe
132132

133133
// The runtime's effect of Network.clearBrowserCache and
134134
// Network.setCacheDisabled on the HTTP module cache (HttpLoader.h).
135-
// Socket thread.
136-
void ClearModuleHttpCacheFromInspector();
135+
// Socket thread. The clear returns the error message when it failed.
136+
std::optional<std::string> ClearModuleHttpCacheFromInspector();
137137
void SetCacheDisabledBy(jobject socket, bool cacheDisabled);
138138
void AnnounceWorkerTargets();
139139

0 commit comments

Comments
 (0)