wasm: add network object checking

This should help when things are moving fast, and downloads and
network object are destroyed before the callbacks finishes.

Change-Id: I1f65965bd61efc2e641d03eb071f23e684dd5c44
Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
This commit is contained in:
Lorn Potter 2019-07-29 10:07:31 +10:00 committed by Edward Welbourne
parent 4fc3fa4c8b
commit 536cab1a93

View File

@ -63,6 +63,8 @@ static void q_requestErrorCallback(val event)
return;
val xhr = event["target"];
if (xhr.isNull() || xhr.isUndefined())
return;
quintptr func = xhr["data-handler"].as<quintptr>();
QNetworkReplyWasmImplPrivate *reply = reinterpret_cast<QNetworkReplyWasmImplPrivate*>(func);
@ -84,6 +86,8 @@ static void q_progressCallback(val event)
return;
val xhr = event["target"];
if (xhr.isNull() || xhr.isUndefined())
return;
QNetworkReplyWasmImplPrivate *reply =
reinterpret_cast<QNetworkReplyWasmImplPrivate*>(xhr["data-handler"].as<quintptr>());
@ -99,6 +103,8 @@ static void q_loadCallback(val event)
return;
val xhr = event["target"];
if (xhr.isNull() || xhr.isUndefined())
return;
QNetworkReplyWasmImplPrivate *reply =
reinterpret_cast<QNetworkReplyWasmImplPrivate*>(xhr["data-handler"].as<quintptr>());
@ -123,8 +129,13 @@ static void q_loadCallback(val event)
} else if (responseType == "arraybuffer" || responseType == "blob") {
// handle this data in the FileReader, triggered by the call to readAsArrayBuffer
val blob = xhr["response"];
if (blob.isNull() || blob.isUndefined())
return;
val reader = val::global("FileReader").new_();
if (reader.isNull() || reader.isUndefined())
return;
reader.set("onload", val::module_property("qt_QNetworkReplyWasmImplPrivate_readBinary"));
reader.set("data-handler", xhr["data-handler"]);
@ -151,6 +162,8 @@ static void q_responseHeadersCallback(val event)
return;
val xhr = event["target"];
if (xhr.isNull() || xhr.isUndefined())
return;
if (xhr["readyState"].as<int>() == 2) { // HEADERS_RECEIVED
std::string responseHeaders = xhr.call<std::string>("getAllResponseHeaders");
@ -170,6 +183,8 @@ static void q_readBinary(val event)
return;
val fileReader = event["target"];
if (fileReader.isNull() || fileReader.isUndefined())
return;
QNetworkReplyWasmImplPrivate *reply =
reinterpret_cast<QNetworkReplyWasmImplPrivate*>(fileReader["data-handler"].as<quintptr>());
@ -180,6 +195,9 @@ static void q_readBinary(val event)
// Set up source typed array
val result = fileReader["result"]; // ArrayBuffer
if (result.isNull() || result.isUndefined())
return;
val Uint8Array = val::global("Uint8Array");
val sourceTypedArray = Uint8Array.new_(result);