Showing the changes to libraries/script-engine/src/AssetScriptingInterface.cpp (1 of 260 files). Show all
Unified · Split
| @@ -13,18 +13,20 @@ · 12 unmodified lines | |||
| 13 | 13 | ||
| 14 | #include <QMimeDatabase> | 14 | #include <QMimeDatabase> |
| 15 | #include <QThread> | 15 | #include <QThread> |
| 16 | #include <QtScript/QScriptEngine> | ||
| 17 | 16 | ||
| 18 | #include <AssetRequest.h> | 17 | #include <AssetRequest.h> |
| 19 | #include <AssetUpload.h> | 18 | #include <AssetUpload.h> |
| 20 | #include <AssetUtils.h> | 19 | #include <AssetUtils.h> |
| 21 | #include <BaseScriptEngine.h> | ||
| 22 | #include <MappingRequest.h> | 20 | #include <MappingRequest.h> |
| 23 | #include <NodeList.h> | 21 | #include <NodeList.h> |
| 24 | 22 | ||
| 25 | #include <RegisteredMetaTypes.h> | 23 | #include <RegisteredMetaTypes.h> |
| 26 | #include "ScriptEngine.h" | 24 | #include "ScriptEngine.h" |
| 25 | #include "ScriptEngineCast.h" | ||
| 27 | #include "ScriptEngineLogging.h" | 26 | #include "ScriptEngineLogging.h" |
| 27 | #include "ScriptManager.h" | ||
| 28 | #include "ScriptValue.h" | ||
| 29 | #include "ScriptValueUtils.h" | ||
| 28 | 30 | ||
| 29 | #include <shared/QtHelpers.h> | 31 | #include <shared/QtHelpers.h> |
| 30 | #include <Gzip.h> | 32 | #include <Gzip.h> |
| @@ -35,7 +37,9 @@ using Promise = MiniPromise::Promise; · 4 unmodified lines | |||
| 35 | 37 | ||
| 36 | AssetScriptingInterface::AssetScriptingInterface(QObject* parent) : BaseAssetScriptingInterface(parent) { | 38 | AssetScriptingInterface::AssetScriptingInterface(QObject* parent) : BaseAssetScriptingInterface(parent) { |
| 37 | qCDebug(scriptengine) << "AssetScriptingInterface::AssetScriptingInterface" << parent; | 39 | qCDebug(scriptengine) << "AssetScriptingInterface::AssetScriptingInterface" << parent; |
| 38 | MiniPromise::registerMetaTypes(parent); | 40 | |
| 41 | auto scriptManager = qobject_cast<ScriptManager*>(parent); | ||
| 42 | scriptRegisterMetaType(scriptManager->engine().get(), promiseToScriptValue, promiseFromScriptValue); | ||
| 39 | } | 43 | } |
| 40 | 44 | ||
| 41 | #define JS_VERIFY(cond, error) { if (!this->jsVerify(cond, error)) { return; } } | 45 | #define JS_VERIFY(cond, error) { if (!this->jsVerify(cond, error)) { return; } } |
| @@ -59,16 +63,17 @@ bool AssetScriptingInterface::initializeCache() { · 17 unmodified lines | |||
| 59 | } | 63 | } |
| 60 | } | 64 | } |
| 61 | 65 | ||
| 62 | void AssetScriptingInterface::uploadData(QString data, QScriptValue callback) { | 66 | void AssetScriptingInterface::uploadData(QString data, ScriptValuePointer callback) { |
| 63 | auto handler = jsBindCallback(thisObject(), callback); | 67 | auto handler = jsBindCallback(thisObject(), callback); |
| 64 | QByteArray dataByteArray = data.toUtf8(); | 68 | QByteArray dataByteArray = data.toUtf8(); |
| 65 | auto upload = DependencyManager::get<AssetClient>()->createUpload(dataByteArray); | 69 | auto upload = DependencyManager::get<AssetClient>()->createUpload(dataByteArray); |
| 66 | 70 | ||
| 67 | Promise deferred = makePromise(__FUNCTION__); | 71 | Promise deferred = makePromise(__FUNCTION__); |
| 72 | auto scriptEngine = engine(); | ||
| 68 | deferred->ready([=](QString error, QVariantMap result) { | 73 | deferred->ready([=](QString error, QVariantMap result) { |
| 69 | auto url = result.value("url").toString(); | 74 | auto url = result.value("url").toString(); |
| 70 | auto hash = result.value("hash").toString(); | 75 | auto hash = result.value("hash").toString(); |
| 71 | jsCallback(handler, url, hash); | 76 | jsCallback(handler, scriptEngine->newValue(url), scriptEngine->newValue(hash)); |
| 72 | }); | 77 | }); |
| 73 | 78 | ||
| 74 | connect(upload, &AssetUpload::finished, upload, [deferred](AssetUpload* upload, const QString& hash) { | 79 | connect(upload, &AssetUpload::finished, upload, [deferred](AssetUpload* upload, const QString& hash) { |
| @@ -83,12 +88,13 @@ void AssetScriptingInterface::uploadData(QString data, QScriptValue callback) { · 8 unmodified lines | |||
| 83 | upload->start(); | 88 | upload->start(); |
| 84 | } | 89 | } |
| 85 | 90 | ||
| 86 | void AssetScriptingInterface::setMapping(QString path, QString hash, QScriptValue callback) { | 91 | void AssetScriptingInterface::setMapping(QString path, QString hash, ScriptValuePointer callback) { |
| 87 | auto handler = jsBindCallback(thisObject(), callback); | 92 | auto handler = jsBindCallback(thisObject(), callback); |
| 88 | auto setMappingRequest = assetClient()->createSetMappingRequest(path, hash); | 93 | auto setMappingRequest = assetClient()->createSetMappingRequest(path, hash); |
| 89 | Promise deferred = makePromise(__FUNCTION__); | 94 | Promise deferred = makePromise(__FUNCTION__); |
| 95 | auto scriptEngine = engine(); | ||
| 90 | deferred->ready([=](QString error, QVariantMap result) { | 96 | deferred->ready([=](QString error, QVariantMap result) { |
| 91 | jsCallback(handler, error, result); | 97 | jsCallback(handler, scriptEngine->newValue(error), result); |
| 92 | }); | 98 | }); |
| 93 | 99 | ||
| 94 | connect(setMappingRequest, &SetMappingRequest::finished, setMappingRequest, [deferred](SetMappingRequest* request) { | 100 | connect(setMappingRequest, &SetMappingRequest::finished, setMappingRequest, [deferred](SetMappingRequest* request) { |
| @@ -107,7 +113,7 @@ void AssetScriptingInterface::setMapping(QString path, QString hash, QScriptValu · 12 unmodified lines | |||
| 107 | * @typedef {object} Assets.DownloadDataError | 113 | * @typedef {object} Assets.DownloadDataError |
| 108 | * @property {string} errorMessage - <code>""</code> if the download was successful, otherwise a description of the error. | 114 | * @property {string} errorMessage - <code>""</code> if the download was successful, otherwise a description of the error. |
| 109 | */ | 115 | */ |
| 110 | void AssetScriptingInterface::downloadData(QString urlString, QScriptValue callback) { | 116 | void AssetScriptingInterface::downloadData(QString urlString, ScriptValuePointer callback) { |
| 111 | // FIXME: historically this API method failed silently when given a non-atp prefixed | 117 | // FIXME: historically this API method failed silently when given a non-atp prefixed |
| 112 | // urlString (or if the AssetRequest failed). | 118 | // urlString (or if the AssetRequest failed). |
| 113 | // .. is that by design or could we update without breaking things to provide better feedback to scripts? | 119 | // .. is that by design or could we update without breaking things to provide better feedback to scripts? |
| @@ -123,9 +129,10 @@ void AssetScriptingInterface::downloadData(QString urlString, QScriptValue callb · 9 unmodified lines | |||
| 123 | auto assetRequest = assetClient->createRequest(hash); | 129 | auto assetRequest = assetClient->createRequest(hash); |
| 124 | 130 | ||
| 125 | Promise deferred = makePromise(__FUNCTION__); | 131 | Promise deferred = makePromise(__FUNCTION__); |
| 132 | auto scriptEngine = engine(); | ||
| 126 | deferred->ready([=](QString error, QVariantMap result) { | 133 | deferred->ready([=](QString error, QVariantMap result) { |
| 127 | // FIXME: to remain backwards-compatible the signature here is "callback(data, n/a)" | 134 | // FIXME: to remain backwards-compatible the signature here is "callback(data, n/a)" |
| 128 | jsCallback(handler, result.value("data").toString(), { { "errorMessage", error } }); | 135 | jsCallback(handler, scriptEngine->newValue(result.value("data").toString()), { { "errorMessage", error } }); |
| 129 | }); | 136 | }); |
| 130 | 137 | ||
| 131 | connect(assetRequest, &AssetRequest::finished, assetRequest, [deferred](AssetRequest* request) { | 138 | connect(assetRequest, &AssetRequest::finished, assetRequest, [deferred](AssetRequest* request) { |
| @@ -149,7 +156,7 @@ void AssetScriptingInterface::downloadData(QString urlString, QScriptValue callb · 17 unmodified lines | |||
| 149 | assetRequest->start(); | 156 | assetRequest->start(); |
| 150 | } | 157 | } |
| 151 | 158 | ||
| 152 | void AssetScriptingInterface::setBakingEnabled(QString path, bool enabled, QScriptValue callback) { | 159 | void AssetScriptingInterface::setBakingEnabled(QString path, bool enabled, ScriptValuePointer callback) { |
| 153 | auto setBakingEnabledRequest = DependencyManager::get<AssetClient>()->createSetBakingEnabledRequest({ path }, enabled); | 160 | auto setBakingEnabledRequest = DependencyManager::get<AssetClient>()->createSetBakingEnabledRequest({ path }, enabled); |
| 154 | 161 | ||
| 155 | Promise deferred = jsPromiseReady(makePromise(__FUNCTION__), thisObject(), callback); | 162 | Promise deferred = jsPromiseReady(makePromise(__FUNCTION__), thisObject(), callback); |
| @@ -179,14 +186,15 @@ void AssetScriptingInterface::sendFakedHandshake() { · 23 unmodified lines | |||
| 179 | 186 | ||
| 180 | #endif | 187 | #endif |
| 181 | 188 | ||
| 182 | void AssetScriptingInterface::getMapping(QString asset, QScriptValue callback) { | 189 | void AssetScriptingInterface::getMapping(QString asset, ScriptValuePointer callback) { |
| 183 | auto path = AssetUtils::getATPUrl(asset).path(); | 190 | auto path = AssetUtils::getATPUrl(asset).path(); |
| 184 | auto handler = jsBindCallback(thisObject(), callback); | 191 | auto handler = jsBindCallback(thisObject(), callback); |
| 185 | JS_VERIFY(AssetUtils::isValidFilePath(path), "invalid ATP file path: " + asset + "(path:"+path+")"); | 192 | JS_VERIFY(AssetUtils::isValidFilePath(path), "invalid ATP file path: " + asset + "(path:"+path+")"); |
| 186 | JS_VERIFY(callback.isFunction(), "expected second parameter to be a callback function"); | 193 | JS_VERIFY(callback->isFunction(), "expected second parameter to be a callback function"); |
| 187 | Promise promise = getAssetInfo(path); | 194 | Promise promise = getAssetInfo(path); |
| 195 | auto scriptEngine = engine(); | ||
| 188 | promise->ready([=](QString error, QVariantMap result) { | 196 | promise->ready([=](QString error, QVariantMap result) { |
| 189 | jsCallback(handler, error, result.value("hash").toString()); | 197 | jsCallback(handler, scriptEngine->newValue(error), scriptEngine->newValue(result.value("hash").toString())); |
| 190 | }); | 198 | }); |
| 191 | } | 199 | } |
| 192 | 200 | ||
| @@ -202,45 +210,46 @@ bool AssetScriptingInterface::jsVerify(bool condition, const QString& error) { · 9 unmodified lines | |||
| 202 | return false; | 210 | return false; |
| 203 | } | 211 | } |
| 204 | 212 | ||
| 205 | QScriptValue AssetScriptingInterface::jsBindCallback(QScriptValue scope, QScriptValue callback) { | 213 | ScriptValuePointer AssetScriptingInterface::jsBindCallback(ScriptValuePointer scope, ScriptValuePointer callback) { |
| 206 | QScriptValue handler = ::makeScopedHandlerObject(scope, callback); | 214 | ScriptValuePointer handler = ::makeScopedHandlerObject(scope, callback); |
| 207 | QScriptValue value = handler.property("callback"); | 215 | ScriptValuePointer value = handler->property("callback"); |
| 208 | if (!jsVerify(handler.isObject() && value.isFunction(), | 216 | if (!jsVerify(handler->isObject() && value->isFunction(), |
| 209 | QString("jsBindCallback -- .callback is not a function (%1)").arg(value.toVariant().typeName()))) { | 217 | QString("jsBindCallback -- .callback is not a function (%1)").arg(value->toVariant().typeName()))) { |
| 210 | return QScriptValue(); | 218 | return ScriptValuePointer(); |
| 211 | } | 219 | } |
| 212 | return handler; | 220 | return handler; |
| 213 | } | 221 | } |
| 214 | 222 | ||
| 215 | Promise AssetScriptingInterface::jsPromiseReady(Promise promise, QScriptValue scope, QScriptValue callback) { | 223 | Promise AssetScriptingInterface::jsPromiseReady(Promise promise, ScriptValuePointer scope, ScriptValuePointer callback) { |
| 216 | auto handler = jsBindCallback(scope, callback); | 224 | auto handler = jsBindCallback(scope, callback); |
| 217 | if (!jsVerify(handler.isValid(), "jsPromiseReady -- invalid callback handler")) { | 225 | if (!jsVerify(handler->isValid(), "jsPromiseReady -- invalid callback handler")) { |
| 218 | return nullptr; | 226 | return nullptr; |
| 219 | } | 227 | } |
| 220 | return promise->ready([this, handler](QString error, QVariantMap result) { | 228 | auto scriptEngine = engine(); |
| 221 | jsCallback(handler, error, result); | 229 | return promise->ready([this, handler, scriptEngine](QString error, QVariantMap result) { |
| 230 | jsCallback(handler, scriptEngine->newValue(error), result); | ||
| 222 | }); | 231 | }); |
| 223 | } | 232 | } |
| 224 | 233 | ||
| 225 | void AssetScriptingInterface::jsCallback(const QScriptValue& handler, | 234 | void AssetScriptingInterface::jsCallback(const ScriptValuePointer& handler, |
| 226 | const QScriptValue& error, const QScriptValue& result) { | 235 | const ScriptValuePointer& error, const ScriptValuePointer& result) { |
| 227 | Q_ASSERT(thread() == QThread::currentThread()); | 236 | Q_ASSERT(thread() == QThread::currentThread()); |
| 228 | auto errorValue = !error.toBool() ? QScriptValue::NullValue : error; | 237 | auto errorValue = !error->toBool() ? engine()->nullValue() : error; |
| 229 | JS_VERIFY(handler.isObject() && handler.property("callback").isFunction(), | 238 | JS_VERIFY(handler->isObject() && handler->property("callback")->isFunction(), |
| 230 | QString("jsCallback -- .callback is not a function (%1)") | 239 | QString("jsCallback -- .callback is not a function (%1)") |
| 231 | .arg(handler.property("callback").toVariant().typeName())); | 240 | .arg(handler->property("callback")->toVariant().typeName())); |
| 232 | ::callScopedHandlerObject(handler, errorValue, result); | 241 | ::callScopedHandlerObject(handler, errorValue, result); |
| 233 | } | 242 | } |
| 234 | 243 | ||
| 235 | void AssetScriptingInterface::jsCallback(const QScriptValue& handler, | 244 | void AssetScriptingInterface::jsCallback(const ScriptValuePointer& handler, |
| 236 | const QScriptValue& error, const QVariantMap& result) { | 245 | const ScriptValuePointer& error, const QVariantMap& result) { |
| 237 | Q_ASSERT(thread() == QThread::currentThread()); | 246 | Q_ASSERT(thread() == QThread::currentThread()); |
| 238 | Q_ASSERT(handler.engine()); | 247 | Q_ASSERT(handler->engine()); |
| 239 | auto engine = handler.engine(); | 248 | auto engine = handler->engine(); |
| 240 | jsCallback(handler, error, engine->toScriptValue(result)); | 249 | jsCallback(handler, error, engine->toScriptValue(result)); |
| 241 | } | 250 | } |
| 242 | 251 | ||
| 243 | void AssetScriptingInterface::deleteAsset(QScriptValue options, QScriptValue scope, QScriptValue callback) { | 252 | void AssetScriptingInterface::deleteAsset(ScriptValuePointer options, ScriptValuePointer scope, ScriptValuePointer callback) { |
| 244 | jsVerify(false, "TODO: deleteAsset API"); | 253 | jsVerify(false, "TODO: deleteAsset API"); |
| 245 | } | 254 | } |
| 246 | 255 | ||
| @@ -270,14 +279,14 @@ void AssetScriptingInterface::deleteAsset(QScriptValue options, QScriptValue sco · 23 unmodified lines | |||
| 270 | * @property {boolean} [wasRedirected] - <code>true</code> if the downloaded data is the baked version of the asset, | 279 | * @property {boolean} [wasRedirected] - <code>true</code> if the downloaded data is the baked version of the asset, |
| 271 | * <code>false</code> if it isn't baked. | 280 | * <code>false</code> if it isn't baked. |
| 272 | */ | 281 | */ |
| 273 | void AssetScriptingInterface::getAsset(QScriptValue options, QScriptValue scope, QScriptValue callback) { | 282 | void AssetScriptingInterface::getAsset(ScriptValuePointer options, ScriptValuePointer scope, ScriptValuePointer callback) { |
| 274 | JS_VERIFY(options.isObject() || options.isString(), "expected request options Object or URL as first parameter"); | 283 | JS_VERIFY(options->isObject() || options->isString(), "expected request options Object or URL as first parameter"); |
| 275 | 284 | ||
| 276 | auto decompress = options.property("decompress").toBool() || options.property("compressed").toBool(); | 285 | auto decompress = options->property("decompress")->toBool() || options->property("compressed")->toBool(); |
| 277 | auto responseType = options.property("responseType").toString().toLower(); | 286 | auto responseType = options->property("responseType")->toString().toLower(); |
| 278 | auto url = options.property("url").toString(); | 287 | auto url = options->property("url")->toString(); |
| 279 | if (options.isString()) { | 288 | if (options->isString()) { |
| 280 | url = options.toString(); | 289 | url = options->toString(); |
| 281 | } | 290 | } |
| 282 | if (responseType.isEmpty()) { | 291 | if (responseType.isEmpty()) { |
| 283 | responseType = "text"; | 292 | responseType = "text"; |
| @@ -336,10 +345,10 @@ void AssetScriptingInterface::getAsset(QScriptValue options, QScriptValue scope, · 52 unmodified lines | |||
| 336 | * @property {boolean} [wasRedirected] - <code>true</code> if the resolved data is for the baked version of the asset, | 345 | * @property {boolean} [wasRedirected] - <code>true</code> if the resolved data is for the baked version of the asset, |
| 337 | * <code>false</code> if it isn't. | 346 | * <code>false</code> if it isn't. |
| 338 | */ | 347 | */ |
| 339 | void AssetScriptingInterface::resolveAsset(QScriptValue options, QScriptValue scope, QScriptValue callback) { | 348 | void AssetScriptingInterface::resolveAsset(ScriptValuePointer options, ScriptValuePointer scope, ScriptValuePointer callback) { |
| 340 | const QString& URL{ "url" }; | 349 | const QString& URL{ "url" }; |
| 341 | 350 | ||
| 342 | auto url = (options.isString() ? options : options.property(URL)).toString(); | 351 | auto url = (options->isString() ? options : options->property(URL))->toString(); |
| 343 | auto asset = AssetUtils::getATPUrl(url).path(); | 352 | auto asset = AssetUtils::getATPUrl(url).path(); |
| 344 | 353 | ||
| 345 | JS_VERIFY(AssetUtils::isValidFilePath(asset) || AssetUtils::isValidHash(asset), | 354 | JS_VERIFY(AssetUtils::isValidFilePath(asset) || AssetUtils::isValidHash(asset), |
| @@ -363,10 +372,10 @@ void AssetScriptingInterface::resolveAsset(QScriptValue options, QScriptValue sc · 17 unmodified lines | |||
| 363 | * @property {string|object|ArrayBuffer} [response] - The decompressed data. | 372 | * @property {string|object|ArrayBuffer} [response] - The decompressed data. |
| 364 | * @property {Assets.ResponseType} [responseType] - The type of the decompressed data in <code>response</code>. | 373 | * @property {Assets.ResponseType} [responseType] - The type of the decompressed data in <code>response</code>. |
| 365 | */ | 374 | */ |
| 366 | void AssetScriptingInterface::decompressData(QScriptValue options, QScriptValue scope, QScriptValue callback) { | 375 | void AssetScriptingInterface::decompressData(ScriptValuePointer options, ScriptValuePointer scope, ScriptValuePointer callback) { |
| 367 | auto data = options.property("data"); | 376 | auto data = options->property("data"); |
| 368 | QByteArray dataByteArray = qscriptvalue_cast<QByteArray>(data); | 377 | QByteArray dataByteArray = scriptvalue_cast<QByteArray>(data); |
| 369 | auto responseType = options.property("responseType").toString().toLower(); | 378 | auto responseType = options->property("responseType")->toString().toLower(); |
| 370 | if (responseType.isEmpty()) { | 379 | if (responseType.isEmpty()) { |
| 371 | responseType = "text"; | 380 | responseType = "text"; |
| 372 | } | 381 | } |
| @@ -404,10 +413,10 @@ namespace { · 31 unmodified lines | |||
| 404 | * @property {string} [contentType] - The MIME type of the compressed data, i.e., <code>"application/gzip"</code>. | 413 | * @property {string} [contentType] - The MIME type of the compressed data, i.e., <code>"application/gzip"</code>. |
| 405 | * @property {ArrayBuffer} [data] - The compressed data. | 414 | * @property {ArrayBuffer} [data] - The compressed data. |
| 406 | */ | 415 | */ |
| 407 | void AssetScriptingInterface::compressData(QScriptValue options, QScriptValue scope, QScriptValue callback) { | 416 | void AssetScriptingInterface::compressData(ScriptValuePointer options, ScriptValuePointer scope, ScriptValuePointer callback) { |
| 408 | auto data = options.property("data").isValid() ? options.property("data") : options; | 417 | auto data = options->property("data")->isValid() ? options->property("data") : options; |
| 409 | QByteArray dataByteArray = data.isString() ? data.toString().toUtf8() : qscriptvalue_cast<QByteArray>(data); | 418 | QByteArray dataByteArray = data->isString() ? data->toString().toUtf8() : scriptvalue_cast<QByteArray>(data); |
| 410 | int level = options.property("level").isNumber() ? options.property("level").toInt32() : DEFAULT_GZIP_COMPRESSION_LEVEL; | 419 | int level = options->property("level")->isNumber() ? options->property("level")->toInt32() : DEFAULT_GZIP_COMPRESSION_LEVEL; |
| 411 | JS_VERIFY(level >= DEFAULT_GZIP_COMPRESSION_LEVEL || level <= MAX_GZIP_COMPRESSION_LEVEL, QString("invalid .level %1").arg(level)); | 420 | JS_VERIFY(level >= DEFAULT_GZIP_COMPRESSION_LEVEL || level <= MAX_GZIP_COMPRESSION_LEVEL, QString("invalid .level %1").arg(level)); |
| 412 | jsPromiseReady(compressBytes(dataByteArray, level), scope, callback); | 421 | jsPromiseReady(compressBytes(dataByteArray, level), scope, callback); |
| 413 | } | 422 | } |
| @@ -433,18 +442,18 @@ void AssetScriptingInterface::compressData(QScriptValue options, QScriptValue sc · 19 unmodified lines | |||
| 433 | * @property {string} [url] - The <code>atp:</code> URL of the content: using the path if specified, otherwise the hash. | 442 | * @property {string} [url] - The <code>atp:</code> URL of the content: using the path if specified, otherwise the hash. |
| 434 | * @property {string} [path] - The uploaded content's mapped path, if specified. | 443 | * @property {string} [path] - The uploaded content's mapped path, if specified. |
| 435 | */ | 444 | */ |
| 436 | void AssetScriptingInterface::putAsset(QScriptValue options, QScriptValue scope, QScriptValue callback) { | 445 | void AssetScriptingInterface::putAsset(ScriptValuePointer options, ScriptValuePointer scope, ScriptValuePointer callback) { |
| 437 | auto compress = options.property("compress").toBool() || options.property("compressed").toBool(); | 446 | auto compress = options->property("compress")->toBool() || options->property("compressed")->toBool(); |
| 438 | auto data = options.isObject() ? options.property("data") : options; | 447 | auto data = options->isObject() ? options->property("data") : options; |
| 439 | auto rawPath = options.property("path").toString(); | 448 | auto rawPath = options->property("path")->toString(); |
| 440 | auto path = AssetUtils::getATPUrl(rawPath).path(); | 449 | auto path = AssetUtils::getATPUrl(rawPath).path(); |
| 441 | 450 | ||
| 442 | QByteArray dataByteArray = data.isString() ? data.toString().toUtf8() : qscriptvalue_cast<QByteArray>(data); | 451 | QByteArray dataByteArray = data->isString() ? data->toString().toUtf8() : scriptvalue_cast<QByteArray>(data); |
| 443 | 452 | ||
| 444 | JS_VERIFY(path.isEmpty() || AssetUtils::isValidFilePath(path), | 453 | JS_VERIFY(path.isEmpty() || AssetUtils::isValidFilePath(path), |
| 445 | QString("expected valid ATP file path '%1' ('%2')").arg(rawPath).arg(path)); | 454 | QString("expected valid ATP file path '%1' ('%2')").arg(rawPath).arg(path)); |
| 446 | JS_VERIFY(dataByteArray.size() > 0, | 455 | JS_VERIFY(dataByteArray.size() > 0, |
| 447 | QString("expected non-zero .data (got %1 / #%2 bytes)").arg(data.toVariant().typeName()).arg(dataByteArray.size())); | 456 | QString("expected non-zero .data (got %1 / #%2 bytes)").arg(data->toVariant().typeName()).arg(dataByteArray.size())); |
| 448 | 457 | ||
| 449 | // [compressed] => uploaded to server => [mapped to path] | 458 | // [compressed] => uploaded to server => [mapped to path] |
| 450 | Promise prepared = makePromise("putAsset::prepared"); | 459 | Promise prepared = makePromise("putAsset::prepared"); |
| @@ -489,8 +498,8 @@ void AssetScriptingInterface::putAsset(QScriptValue options, QScriptValue scope, · 38 unmodified lines | |||
| 489 | * @property {string} url - The URL of the cached asset to get information on. Must start with <code>"atp:"</code> or | 498 | * @property {string} url - The URL of the cached asset to get information on. Must start with <code>"atp:"</code> or |
| 490 | * <code>"cache:"</code>. | 499 | * <code>"cache:"</code>. |
| 491 | */ | 500 | */ |
| 492 | void AssetScriptingInterface::queryCacheMeta(QScriptValue options, QScriptValue scope, QScriptValue callback) { | 501 | void AssetScriptingInterface::queryCacheMeta(ScriptValuePointer options, ScriptValuePointer scope, ScriptValuePointer callback) { |
| 493 | QString url = options.isString() ? options.toString() : options.property("url").toString(); | 502 | QString url = options->isString() ? options->toString() : options->property("url")->toString(); |
| 494 | JS_VERIFY(QUrl(url).isValid(), QString("Invalid URL '%1'").arg(url)); | 503 | JS_VERIFY(QUrl(url).isValid(), QString("Invalid URL '%1'").arg(url)); |
| 495 | jsPromiseReady(Parent::queryCacheMeta(url), scope, callback); | 504 | jsPromiseReady(Parent::queryCacheMeta(url), scope, callback); |
| 496 | } | 505 | } |
| @@ -504,16 +513,16 @@ void AssetScriptingInterface::queryCacheMeta(QScriptValue options, QScriptValue · 7 unmodified lines | |||
| 504 | * @property {string} url - The URL of the asset to load from cache. Must start with <code>"atp:"</code> or | 513 | * @property {string} url - The URL of the asset to load from cache. Must start with <code>"atp:"</code> or |
| 505 | * <code>"cache:"</code>. | 514 | * <code>"cache:"</code>. |
| 506 | */ | 515 | */ |
| 507 | void AssetScriptingInterface::loadFromCache(QScriptValue options, QScriptValue scope, QScriptValue callback) { | 516 | void AssetScriptingInterface::loadFromCache(ScriptValuePointer options, ScriptValuePointer scope, ScriptValuePointer callback) { |
| 508 | QString url, responseType; | 517 | QString url, responseType; |
| 509 | bool decompress = false; | 518 | bool decompress = false; |
| 510 | if (options.isString()) { | 519 | if (options->isString()) { |
| 511 | url = options.toString(); | 520 | url = options->toString(); |
| 512 | responseType = "text"; | 521 | responseType = "text"; |
| 513 | } else { | 522 | } else { |
| 514 | url = options.property("url").toString(); | 523 | url = options->property("url")->toString(); |
| 515 | responseType = options.property("responseType").isValid() ? options.property("responseType").toString() : "text"; | 524 | responseType = options->property("responseType")->isValid() ? options->property("responseType")->toString() : "text"; |
| 516 | decompress = options.property("decompress").toBool() || options.property("compressed").toBool(); | 525 | decompress = options->property("decompress")->toBool() || options->property("compressed")->toBool(); |
| 517 | } | 526 | } |
| 518 | JS_VERIFY(QUrl(url).isValid(), QString("Invalid URL '%1'").arg(url)); | 527 | JS_VERIFY(QUrl(url).isValid(), QString("Invalid URL '%1'").arg(url)); |
| 519 | JS_VERIFY(RESPONSE_TYPES.contains(responseType), | 528 | JS_VERIFY(RESPONSE_TYPES.contains(responseType), |
| @@ -523,14 +532,14 @@ void AssetScriptingInterface::loadFromCache(QScriptValue options, QScriptValue s · 3 unmodified lines | |||
| 523 | } | 532 | } |
| 524 | 533 | ||
| 525 | bool AssetScriptingInterface::canWriteCacheValue(const QUrl& url) { | 534 | bool AssetScriptingInterface::canWriteCacheValue(const QUrl& url) { |
| 526 | auto scriptEngine = qobject_cast<ScriptEngine*>(engine()); | 535 | auto scriptManager = engine()->manager(); |
| 527 | if (!scriptEngine) { | 536 | if (!scriptManager) { |
| 528 | return false; | 537 | return false; |
| 529 | } | 538 | } |
| 530 | // allow cache writes only from Client, EntityServer and Agent scripts | 539 | // allow cache writes only from Client, EntityServer and Agent scripts |
| 531 | bool isAllowedContext = ( | 540 | bool isAllowedContext = ( |
| 532 | scriptEngine->isClientScript() || | 541 | scriptManager->isClientScript() || |
| 533 | scriptEngine->isAgentScript() | 542 | scriptManager->isAgentScript() |
| 534 | ); | 543 | ); |
| 535 | if (!isAllowedContext) { | 544 | if (!isAllowedContext) { |
| 536 | return false; | 545 | return false; |
| @@ -546,17 +555,17 @@ bool AssetScriptingInterface::canWriteCacheValue(const QUrl& url) { · 9 unmodified lines | |||
| 546 | * @property {string} [url] - The URL to associate with the cache item. Must start with <code>"atp:"</code> or | 555 | * @property {string} [url] - The URL to associate with the cache item. Must start with <code>"atp:"</code> or |
| 547 | * <code>"cache:"</code>. If not specified, the URL is <code>"atp:"</code> followed by the SHA256 hash of the content. | 556 | * <code>"cache:"</code>. If not specified, the URL is <code>"atp:"</code> followed by the SHA256 hash of the content. |
| 548 | */ | 557 | */ |
| 549 | void AssetScriptingInterface::saveToCache(QScriptValue options, QScriptValue scope, QScriptValue callback) { | 558 | void AssetScriptingInterface::saveToCache(ScriptValuePointer options, ScriptValuePointer scope, ScriptValuePointer callback) { |
| 550 | JS_VERIFY(options.isObject(), QString("expected options object as first parameter not: %1").arg(options.toVariant().typeName())); | 559 | JS_VERIFY(options->isObject(), QString("expected options object as first parameter not: %1").arg(options->toVariant().typeName())); |
| 551 | 560 | ||
| 552 | QString url = options.property("url").toString(); | 561 | QString url = options->property("url")->toString(); |
| 553 | QByteArray data = qscriptvalue_cast<QByteArray>(options.property("data")); | 562 | QByteArray data = scriptvalue_cast<QByteArray>(options->property("data")); |
| 554 | QVariantMap headers = qscriptvalue_cast<QVariantMap>(options.property("headers")); | 563 | QVariantMap headers = scriptvalue_cast<QVariantMap>(options->property("headers")); |
| 555 | 564 | ||
| 556 | saveToCache(url, data, headers, scope, callback); | 565 | saveToCache(url, data, headers, scope, callback); |
| 557 | } | 566 | } |
| 558 | 567 | ||
| 559 | void AssetScriptingInterface::saveToCache(const QUrl& rawURL, const QByteArray& data, const QVariantMap& metadata, QScriptValue scope, QScriptValue callback) { | 568 | void AssetScriptingInterface::saveToCache(const QUrl& rawURL, const QByteArray& data, const QVariantMap& metadata, ScriptValuePointer scope, ScriptValuePointer callback) { |
| 560 | QUrl url = rawURL; | 569 | QUrl url = rawURL; |
| 561 | if (url.path().isEmpty() && !data.isEmpty()) { | 570 | if (url.path().isEmpty() && !data.isEmpty()) { |
| 562 | // generate a valid ATP URL from the data -- appending any existing fragment or querystring values | 571 | // generate a valid ATP URL from the data -- appending any existing fragment or querystring values |