Skip to content

Commit e491b51

Browse files
committed
fix(http-cache): match iOS on throwing cache handlers, unknown sessions and numeric config
- A JS Network domain handler that throws on Network.setCacheDisabled or Network.clearBrowserCache is answered with a -32000 error carrying the thrown message, as iOS does, instead of the runtime's own {}. - A cache command for a worker sessionId with no target gets the same "Session not found" error as other commands and changes nothing. - "moduleHttpCache": 0 turns the cache off and any other number leaves it on, as iOS's NSNumber boolValue reads it; other non-boolean values still warn and leave it on. - The main-thread check for these commands matches their two method names, so other Network messages are not parsed a second time.
1 parent fce224c commit e491b51

3 files changed

Lines changed: 76 additions & 13 deletions

File tree

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

Lines changed: 71 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -91,10 +91,11 @@ json NetworkCacheReply(long long id, const std::string& method, const json& mess
9191
return {{"id", id}, {"result", json::object()}};
9292
}
9393

94-
// The runtime's reply to `message` when it is a Network.setCacheDisabled or
95-
// Network.clearBrowserCache command for the main session.
96-
std::optional<std::string> MainSessionNetworkCacheReply(const std::string& message) {
97-
if (message.find("Network.") == std::string::npos) {
94+
// `message` parsed, when it is a Network.setCacheDisabled or
95+
// Network.clearBrowserCache command with an id for the main session.
96+
std::optional<json> ParseMainSessionNetworkCacheCommand(const std::string& message) {
97+
if (message.find("Network.setCacheDisabled") == std::string::npos &&
98+
message.find("Network.clearBrowserCache") == std::string::npos) {
9899
return std::nullopt;
99100
}
100101
auto parsed = json::parse(message, nullptr, false);
@@ -105,8 +106,24 @@ std::optional<std::string> MainSessionNetworkCacheReply(const std::string& messa
105106
!parsed["sessionId"].get<std::string>().empty())) {
106107
return std::nullopt;
107108
}
108-
return JsonDump(NetworkCacheReply(parsed["id"].get<long long>(),
109-
parsed["method"].get<std::string>(), parsed));
109+
return parsed;
110+
}
111+
112+
// The runtime's reply to `message` when it is a main-session
113+
// Network.setCacheDisabled or Network.clearBrowserCache command.
114+
std::optional<std::string> MainSessionNetworkCacheReply(const std::string& message) {
115+
std::optional<json> parsed = ParseMainSessionNetworkCacheCommand(message);
116+
if (!parsed) {
117+
return std::nullopt;
118+
}
119+
return JsonDump(NetworkCacheReply((*parsed)["id"].get<long long>(),
120+
(*parsed)["method"].get<std::string>(), *parsed));
121+
}
122+
123+
json SessionNotFoundReply(long long id, const std::string& sessionId) {
124+
return {{"id", id},
125+
{"sessionId", sessionId},
126+
{"error", {{"code", -32001}, {"message", "Session not found"}}}};
110127
}
111128

112129
// Target.attachedToTarget event announcing a worker as a child target.
@@ -474,7 +491,9 @@ void JsV8InspectorClient::dispatchMessage(const std::string& message) {
474491
Local<v8::Function> domainMethodFunc = GetDebuggerFunctionFromObject(context, argObject, domainDebugger);
475492

476493
Local<Value> result;
477-
success = this->CallDomainHandlerFunction(context, domainMethodFunc, argObject, domainDebugger, result);
494+
std::optional<std::string> thrownMessage;
495+
success = this->CallDomainHandlerFunction(context, domainMethodFunc, argObject,
496+
domainDebugger, result, thrownMessage);
478497

479498
if(success) {
480499
auto requestId = arg.As<Object>()->Get(context, tns::ArgConverter::ConvertToV8String(isolate_, "id")).ToLocalChecked();
@@ -490,6 +509,17 @@ void JsV8InspectorClient::dispatchMessage(const std::string& message) {
490509
}
491510
return;
492511
}
512+
513+
// The handler owns these commands, so the runtime's own reply in
514+
// doDispatchMessage must not answer them as well.
515+
if (thrownMessage) {
516+
if (std::optional<json> command = ParseMainSessionNetworkCacheCommand(message)) {
517+
json error = {{"id", (*command)["id"]},
518+
{"error", {{"code", -32000}, {"message", *thrownMessage}}}};
519+
this->SendRawToFrontend(JsonDump(error));
520+
return;
521+
}
522+
}
493523
}
494524

495525

@@ -735,6 +765,20 @@ bool JsV8InspectorClient::handleMessageOnSocketThread(const std::string& message
735765
// Network domain handler, if one is registered, for the response. Workers
736766
// have no JS domain handlers, so a worker session is answered here.
737767
if (IsNetworkCacheMethod(method)) {
768+
if (!sessionId.empty()) {
769+
bool knownSession;
770+
{
771+
std::lock_guard<std::mutex> lock(workerTargetsMutex_);
772+
knownSession = workerTargets_.count(sessionId) != 0;
773+
}
774+
if (!knownSession) {
775+
if (msgId >= 0) {
776+
json error = SessionNotFoundReply(msgId, sessionId);
777+
response = JsonDump(error);
778+
}
779+
return true;
780+
}
781+
}
738782
if (method == "Network.clearBrowserCache") {
739783
ClearModuleHttpCacheFromInspector();
740784
} else if (std::optional<bool> cacheDisabled = SetCacheDisabledParam(parsed)) {
@@ -825,10 +869,7 @@ void JsV8InspectorClient::RouteToWorker(const std::string& sessionId, const std:
825869
// The worker died (or never existed): answer commands so the
826870
// frontend does not wait forever.
827871
if (msgId >= 0) {
828-
json error = {{"id", msgId},
829-
{"sessionId", sessionId},
830-
{"error", {{"code", -32001}, {"message", "Session not found"}}}};
831-
errorPayload = JsonDump(error);
872+
errorPayload = JsonDump(SessionNotFoundReply(msgId, sessionId));
832873
}
833874
} else {
834875
// The lock keeps the client alive against a concurrent
@@ -1372,7 +1413,7 @@ void JsV8InspectorClient::InspectorIsConnectedGetterCallback(v8::Local<v8::Strin
13721413
std::atomic<JsV8InspectorClient*> JsV8InspectorClient::instance{nullptr};
13731414

13741415

1375-
bool JsV8InspectorClient::CallDomainHandlerFunction(Local<Context> context, Local<Function> domainMethodFunc, const Local<Object>& arg, Local<Object>& domainDebugger, Local<Value>& result) {
1416+
bool JsV8InspectorClient::CallDomainHandlerFunction(Local<Context> context, Local<Function> domainMethodFunc, const Local<Object>& arg, Local<Object>& domainDebugger, Local<Value>& result, std::optional<std::string>& thrownMessage) {
13761417
if(domainMethodFunc.IsEmpty() || !domainMethodFunc->IsFunction()) {
13771418
return false;
13781419
}
@@ -1405,6 +1446,24 @@ bool JsV8InspectorClient::CallDomainHandlerFunction(Local<Context> context, Loca
14051446
// log any other errors - they are caught, but still make them visible to the user.
14061447
// tns::LogError(isolate, tc);
14071448

1449+
// The error's message, as Chromium reports a failed command; the whole
1450+
// text for anything else that was thrown.
1451+
if (!tc.HasTerminated()) {
1452+
Local<Value> exception = tc.Exception();
1453+
TryCatch messageTc(isolate);
1454+
Local<Value> message;
1455+
if (exception->IsNativeError() &&
1456+
exception.As<Object>()->Get(context, tns::ArgConverter::ConvertToV8String(isolate, "message")).ToLocal(&message) &&
1457+
message->IsString()) {
1458+
thrownMessage = tns::ArgConverter::ToString(isolate, message);
1459+
} else if (exception->ToString(context).ToLocal(&message)) {
1460+
thrownMessage = tns::ArgConverter::ToString(isolate, message);
1461+
}
1462+
if (!thrownMessage) {
1463+
thrownMessage = std::string();
1464+
}
1465+
}
1466+
14081467
return false;
14091468
}
14101469

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,8 @@ class JsV8InspectorClient : V8InspectorClient, v8_inspector::V8Inspector::Channe
187187
v8::Local<v8::Function> domainMethodFunc,
188188
const v8::Local<v8::Object>& arg,
189189
v8::Local<v8::Object>& domainDebugger,
190-
v8::Local<v8::Value>& result);
190+
v8::Local<v8::Value>& result,
191+
std::optional<std::string>& thrownMessage);
191192
std::string GetReturnMessageFromDomainHandlerResult(const v8::Local<v8::Value>& result, const v8::Local<v8::Value>& requestId);
192193
};
193194
}

‎test-app/runtime/src/main/java/com/tns/AppConfig.java‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,9 @@ public AppConfig(File appDir) {
8585
Object value = rootObject.opt(ModuleHttpCacheKey);
8686
if (value instanceof Boolean) {
8787
moduleHttpCache = (Boolean) value;
88+
} else if (value instanceof Number) {
89+
// As iOS reads it: 0 is false, any other number true.
90+
moduleHttpCache = ((Number) value).doubleValue() != 0;
8891
} else {
8992
Log.w("JS", String.format("moduleHttpCache must be a boolean, not %s; the HTTP module cache stays enabled.", JSONObject.quote(String.valueOf(value))));
9093
}

0 commit comments

Comments
 (0)