History

isolate calls to the QtScript libraries to an interface we control

Heather Anderson committed 5 years ago

Commit 1e018db1 parent 713b29e260 files · +6904 −5064Browse files

Showing the changes to libraries/script-engine/src/ScriptEngines.cpp (1 of 260 files). Show all

Unified · Split

libraries/script-engine/src/ScriptEngines.cpp+75−74
@@ -12,6 +12,7 @@ · 11 unmodified lines
12#include <QtCore/QSharedPointer>12#include <QtCore/QSharedPointer>
13 13
14#include <QtWidgets/QApplication>14#include <QtWidgets/QApplication>
15#include <QtCore/QThread>
15 16
16#include <shared/QtHelpers.h>17#include <shared/QtHelpers.h>
17#include <SettingHandle.h>18#include <SettingHandle.h>
@@ -19,6 +20,7 @@ · 1 unmodified line
19#include <PathUtils.h>20#include <PathUtils.h>
20#include <shared/FileUtils.h>21#include <shared/FileUtils.h>
21 22
23#include "ScriptCache.h"
22#include "ScriptEngine.h"24#include "ScriptEngine.h"
23#include "ScriptEngineLogging.h"25#include "ScriptEngineLogging.h"
24 26
@@ -65,7 +67,7 @@ void ScriptEngines::onErrorLoadingScript(const QString& url) { · 40 unmodified lines
65 emit errorLoadingScript(url);67 emit errorLoadingScript(url);
66}68}
67 69
68ScriptEngines::ScriptEngines(ScriptEngine::Context context, const QUrl& defaultScriptsOverride)70ScriptEngines::ScriptEngines(ScriptManager::Context context, const QUrl& defaultScriptsOverride)
69 : _context(context), _defaultScriptsOverride(defaultScriptsOverride)71 : _context(context), _defaultScriptsOverride(defaultScriptsOverride)
70{72{
71 scriptGatekeeper.initialize();73 scriptGatekeeper.initialize();
@@ -138,53 +140,53 @@ QUrl expandScriptUrl(const QUrl& rawScriptURL) { · 66 unmodified lines
138 140
139QObject* scriptsModel();141QObject* scriptsModel();
140 142
141void ScriptEngines::addScriptEngine(ScriptEnginePointer engine) {143void ScriptEngines::addScriptEngine(ScriptManagerPointer manager) {
142 if (!_isStopped) {144 if (!_isStopped) {
143 QMutexLocker locker(&_allScriptsMutex);145 QMutexLocker locker(&_allScriptsMutex);
144 _allKnownScriptEngines.insert(engine);146 _allKnownScriptManagers.insert(manager);
145 }147 }
146}148}
147 149
148void ScriptEngines::removeScriptEngine(ScriptEnginePointer engine) {150void ScriptEngines::removeScriptEngine(ScriptManagerPointer manager) {
149 // If we're not already in the middle of stopping all scripts, then we should remove ourselves151 // If we're not already in the middle of stopping all scripts, then we should remove ourselves
150 // from the list of running scripts. We don't do this if we're in the process of stopping all scripts152 // from the list of running scripts. We don't do this if we're in the process of stopping all scripts
151 // because that method removes scripts from its list as it iterates them153 // because that method removes scripts from its list as it iterates them
152 if (!_isStopped) {154 if (!_isStopped) {
153 QMutexLocker locker(&_allScriptsMutex);155 QMutexLocker locker(&_allScriptsMutex);
154 _allKnownScriptEngines.remove(engine);156 _allKnownScriptManagers.remove(manager);
155 }157 }
156}158}
157 159
158void ScriptEngines::shutdownScripting() {160void ScriptEngines::shutdownScripting() {
159 _isStopped = true;161 _isStopped = true;
160 QMutexLocker locker(&_allScriptsMutex);162 QMutexLocker locker(&_allScriptsMutex);
161 qCDebug(scriptengine) << "Stopping all scripts.... currently known scripts:" << _allKnownScriptEngines.size();163 qCDebug(scriptengine) << "Stopping all scripts.... currently known scripts:" << _allKnownScriptManagers.size();
162 164
163 QMutableSetIterator<ScriptEnginePointer> i(_allKnownScriptEngines);165 QMutableSetIterator<ScriptManagerPointer> i(_allKnownScriptManagers);
164 while (i.hasNext()) {166 while (i.hasNext()) {
165 ScriptEnginePointer scriptEngine = i.next();167 ScriptManagerPointer scriptManager = i.next();
166 QString scriptName = scriptEngine->getFilename();168 QString scriptName = scriptManager->getFilename();
167 169
168 // NOTE: typically all script engines are running. But there's at least one known exception to this, the170 // NOTE: typically all script engines are running. But there's at least one known exception to this, the
169 // "entities sandbox" which is only used to evaluate entities scripts to test their validity before using171 // "entities sandbox" which is only used to evaluate entities scripts to test their validity before using
170 // them. We don't need to stop scripts that aren't running.172 // them. We don't need to stop scripts that aren't running.
171 // TODO: Scripts could be shut down faster if we spread them across a threadpool.173 // TODO: Scripts could be shut down faster if we spread them across a threadpool.
172 if (scriptEngine->isRunning()) {174 if (scriptManager->isRunning()) {
173 qCDebug(scriptengine) << "about to shutdown script:" << scriptName;175 qCDebug(scriptengine) << "about to shutdown script:" << scriptName;
174 176
175 // We disconnect any script engine signals from the application because we don't want to do any177 // We disconnect any script engine signals from the application because we don't want to do any
176 // extra stopScript/loadScript processing that the Application normally does when scripts start178 // extra stopScript/loadScript processing that the Application normally does when scripts start
177 // and stop. We can safely short circuit this because we know we're in the "quitting" process179 // and stop. We can safely short circuit this because we know we're in the "quitting" process
178 scriptEngine->disconnect(this);180 scriptManager->disconnect(this);
179 181
180 // Gracefully stop the engine's scripting thread182 // Gracefully stop the engine's scripting thread
181 scriptEngine->stop();183 scriptManager->stop();
182 184
183 // We need to wait for the engine to be done running before we proceed, because we don't185 // We need to wait for the engine to be done running before we proceed, because we don't
184 // want any of the scripts final "scriptEnding()" or pending "update()" methods from accessing186 // want any of the scripts final "scriptEnding()" or pending "update()" methods from accessing
185 // any application state after we leave this stopAllScripts() method187 // any application state after we leave this stopAllScripts() method
186 qCDebug(scriptengine) << "waiting on script:" << scriptName;188 qCDebug(scriptengine) << "waiting on script:" << scriptName;
187 scriptEngine->waitTillDoneRunning(true);189 scriptManager->waitTillDoneRunning(true);
188 qCDebug(scriptengine) << "done waiting on script:" << scriptName;190 qCDebug(scriptengine) << "done waiting on script:" << scriptName;
189 }191 }
190 // Once the script is stopped, we can remove it from our set192 // Once the script is stopped, we can remove it from our set
@@ -372,8 +374,8 @@ void ScriptEngines::saveScripts() { · 181 unmodified lines
372 QVariantList list;374 QVariantList list;
373 375
374 {376 {
375 QReadLocker lock(&_scriptEnginesHashLock);377 QReadLocker lock(&_scriptManagersHashLock);
376 for (auto it = _scriptEnginesHash.begin(); it != _scriptEnginesHash.end(); ++it) {378 for (auto it = _scriptManagersHash.begin(); it != _scriptManagersHash.end(); ++it) {
377 // Save user-loaded scripts, only if they are set to quit when finished379 // Save user-loaded scripts, only if they are set to quit when finished
378 if (it.value() && it.value()->isUserLoaded() && !it.value()->isQuitWhenFinished()) {380 if (it.value() && it.value()->isUserLoaded() && !it.value()->isQuitWhenFinished()) {
379 auto normalizedUrl = normalizeScriptURL(it.key());381 auto normalizedUrl = normalizeScriptURL(it.key());
@@ -390,8 +392,8 @@ void ScriptEngines::saveScripts() { · 10 unmodified lines
390}392}
391 393
392QStringList ScriptEngines::getRunningScripts() {394QStringList ScriptEngines::getRunningScripts() {
393 QReadLocker lock(&_scriptEnginesHashLock);395 QReadLocker lock(&_scriptManagersHashLock);
394 QList<QUrl> urls = _scriptEnginesHash.keys();396 QList<QUrl> urls = _scriptManagersHash.keys();
395 QStringList result;397 QStringList result;
396 for (auto url : urls) {398 for (auto url : urls) {
397 result.append(url.toString());399 result.append(url.toString());
@@ -400,33 +402,33 @@ QStringList ScriptEngines::getRunningScripts() { · 2 unmodified lines
400}402}
401 403
402void ScriptEngines::stopAllScripts(bool restart) {404void ScriptEngines::stopAllScripts(bool restart) {
403 QReadLocker lock(&_scriptEnginesHashLock);405 QReadLocker lock(&_scriptManagersHashLock);
404 406
405 if (_isReloading) {407 if (_isReloading) {
406 return;408 return;
407 }409 }
408 410
409 for (QHash<QUrl, ScriptEnginePointer>::const_iterator it = _scriptEnginesHash.constBegin();411 for (QHash<QUrl, ScriptManagerPointer>::const_iterator it = _scriptManagersHash.constBegin();
410 it != _scriptEnginesHash.constEnd(); it++) {412 it != _scriptManagersHash.constEnd(); it++) {
411 ScriptEnginePointer scriptEngine = it.value();413 ScriptManagerPointer scriptManager = it.value();
412 // skip already stopped scripts414 // skip already stopped scripts
413 if (scriptEngine->isFinished() || scriptEngine->isStopping()) {415 if (scriptManager->isFinished() || scriptManager->isStopping()) {
414 continue;416 continue;
415 }417 }
416 418
417 bool isOverrideScript = it.key().toString().compare(this->_defaultScriptsOverride.toString()) == 0;419 bool isOverrideScript = it.key().toString().compare(this->_defaultScriptsOverride.toString()) == 0;
418 // queue user scripts if restarting420 // queue user scripts if restarting
419 if (restart && (scriptEngine->isUserLoaded() || isOverrideScript)) {421 if (restart && (scriptManager->isUserLoaded() || isOverrideScript)) {
420 _isReloading = true;422 _isReloading = true;
421 ScriptEngine::Type type = scriptEngine->getType();423 ScriptManager::Type type = scriptManager->getType();
422 424
423 connect(scriptEngine.data(), &ScriptEngine::finished, this, [this, type, isOverrideScript] (QString scriptName) {425 connect(scriptManager.data(), &ScriptManager::finished, this, [this, type, isOverrideScript](QString scriptName) {
424 reloadScript(scriptName, !isOverrideScript)->setType(type);426 reloadScript(scriptName, !isOverrideScript)->setType(type);
425 });427 });
426 }428 }
427 429
428 // stop all scripts430 // stop all scripts
429 scriptEngine->stop();431 scriptManager->stop();
430 }432 }
431 433
432 if (restart) {434 if (restart) {
@@ -446,23 +448,23 @@ bool ScriptEngines::stopScript(const QString& rawScriptURL, bool restart) { · 13 unmodified lines
446 scriptURL = normalizeScriptURL(QUrl::fromLocalFile(rawScriptURL));448 scriptURL = normalizeScriptURL(QUrl::fromLocalFile(rawScriptURL));
447 }449 }
448 450
449 QReadLocker lock(&_scriptEnginesHashLock);451 QReadLocker lock(&_scriptManagersHashLock);
450 if (_scriptEnginesHash.contains(scriptURL)) {452 if (_scriptManagersHash.contains(scriptURL)) {
451 ScriptEnginePointer scriptEngine = _scriptEnginesHash.value(scriptURL);453 ScriptManagerPointer scriptManager = _scriptManagersHash.value(scriptURL);
452 if (restart) {454 if (restart) {
453 bool isUserLoaded = scriptEngine->isUserLoaded();455 bool isUserLoaded = scriptManager->isUserLoaded();
454 ScriptEngine::Type type = scriptEngine->getType();456 ScriptManager::Type type = scriptManager->getType();
455 auto scriptCache = DependencyManager::get<ScriptCache>();457 auto scriptCache = DependencyManager::get<ScriptCache>();
456 scriptCache->deleteScript(scriptURL);458 scriptCache->deleteScript(scriptURL);
457 459
458 if (!scriptEngine->isStopping()) {460 if (!scriptManager->isStopping()) {
459 connect(scriptEngine.data(), &ScriptEngine::finished,461 connect(scriptManager.data(), &ScriptManager::finished,
460 this, [this, isUserLoaded, type](QString scriptName, ScriptEnginePointer engine) {462 this, [this, isUserLoaded, type](QString scriptName, ScriptManagerPointer manager) {
461 reloadScript(scriptName, isUserLoaded)->setType(type);463 reloadScript(scriptName, isUserLoaded)->setType(type);
462 });464 });
463 }465 }
464 }466 }
465 scriptEngine->stop();467 scriptManager->stop();
466 stoppedScript = true;468 stoppedScript = true;
467 }469 }
468 }470 }
@@ -480,11 +482,11 @@ void ScriptEngines::reloadAllScripts() { · 11 unmodified lines
480 stopAllScripts(true);482 stopAllScripts(true);
481}483}
482 484
483ScriptEnginePointer ScriptEngines::loadScript(const QUrl& scriptFilename, bool isUserLoaded, bool loadScriptFromEditor,485ScriptManagerPointer ScriptEngines::loadScript(const QUrl& scriptFilename, bool isUserLoaded, bool loadScriptFromEditor,
484 bool activateMainWindow, bool reload, bool quitWhenFinished) {486 bool activateMainWindow, bool reload, bool quitWhenFinished) {
485 if (thread() != QThread::currentThread()) {487 if (thread() != QThread::currentThread()) {
486 ScriptEnginePointer result { nullptr };488 ScriptManagerPointer result { nullptr };
487 BLOCKING_INVOKE_METHOD(this, "loadScript", Q_RETURN_ARG(ScriptEnginePointer, result),489 BLOCKING_INVOKE_METHOD(this, "loadScript", Q_RETURN_ARG(ScriptManagerPointer, result),
488 Q_ARG(QUrl, scriptFilename),490 Q_ARG(QUrl, scriptFilename),
489 Q_ARG(bool, isUserLoaded),491 Q_ARG(bool, isUserLoaded),
490 Q_ARG(bool, loadScriptFromEditor),492 Q_ARG(bool, loadScriptFromEditor),
@@ -508,41 +510,41 @@ ScriptEnginePointer ScriptEngines::loadScript(const QUrl& scriptFilename, bool i · 17 unmodified lines
508 510
509 scriptUrl = QUrl(FileUtils::selectFile(scriptUrl.toString()));511 scriptUrl = QUrl(FileUtils::selectFile(scriptUrl.toString()));
510 512
511 auto scriptEngine = getScriptEngine(scriptUrl);513 auto scriptManager = getScriptEngine(scriptUrl);
512 if (scriptEngine && !scriptEngine->isStopping()) {514 if (scriptManager && !scriptManager->isStopping()) {
513 return scriptEngine;515 return scriptManager;
514 }516 }
515 517
516 scriptEngine = scriptEngineFactory(_context, NO_SCRIPT, "about:" + scriptFilename.fileName());518 scriptManager = scriptManagerFactory(_context, NO_SCRIPT, "about:" + scriptFilename.fileName());
517 scriptEngine->setUserLoaded(isUserLoaded);519 scriptManager->setUserLoaded(isUserLoaded);
518 scriptEngine->setQuitWhenFinished(quitWhenFinished);520 scriptManager->setQuitWhenFinished(quitWhenFinished);
519 521
520 if (scriptFilename.isEmpty() || !scriptUrl.isValid()) {522 if (scriptFilename.isEmpty() || !scriptUrl.isValid()) {
521 launchScriptEngine(scriptEngine);523 launchScriptEngine(scriptManager);
522 } else {524 } else {
523 // connect to the appropriate signals of this script engine525 // connect to the appropriate signals of this script engine
524 connect(scriptEngine.data(), &ScriptEngine::scriptLoaded, this, &ScriptEngines::onScriptEngineLoaded);526 connect(scriptManager.data(), &ScriptManager::scriptLoaded, this, &ScriptEngines::onScriptEngineLoaded);
525 connect(scriptEngine.data(), &ScriptEngine::errorLoadingScript, this, &ScriptEngines::onScriptEngineError);527 connect(scriptManager.data(), &ScriptManager::errorLoadingScript, this, &ScriptEngines::onScriptEngineError);
526 528
527 // Shutdown Interface when script finishes, if requested529 // Shutdown Interface when script finishes, if requested
528 if (quitWhenFinished) {530 if (quitWhenFinished) {
529 connect(scriptEngine.data(), &ScriptEngine::finished, this, &ScriptEngines::quitWhenFinished);531 connect(scriptManager.data(), &ScriptManager::finished, this, &ScriptEngines::quitWhenFinished);
530 }532 }
531 533
532 // get the script engine object to load the script at the designated script URL534 // get the script engine object to load the script at the designated script URL
533 scriptEngine->loadURL(scriptUrl, reload);535 scriptManager->loadURL(scriptUrl, reload);
534 }536 }
535 537
536 return scriptEngine;538 return scriptManager;
537}539}
538 540
539ScriptEnginePointer ScriptEngines::getScriptEngine(const QUrl& rawScriptURL) {541ScriptManagerPointer ScriptEngines::getScriptEngine(const QUrl& rawScriptURL) {
540 ScriptEnginePointer result;542 ScriptManagerPointer result;
541 {543 {
542 QReadLocker lock(&_scriptEnginesHashLock);544 QReadLocker lock(&_scriptManagersHashLock);
543 const QUrl scriptURL = normalizeScriptURL(rawScriptURL);545 const QUrl scriptURL = normalizeScriptURL(rawScriptURL);
544 auto it = _scriptEnginesHash.find(scriptURL);546 auto it = _scriptManagersHash.find(scriptURL);
545 if (it != _scriptEnginesHash.end()) {547 if (it != _scriptManagersHash.end()) {
546 result = it.value();548 result = it.value();
547 }549 }
548 }550 }
@@ -552,16 +554,15 @@ ScriptEnginePointer ScriptEngines::getScriptEngine(const QUrl& rawScriptURL) { · 3 unmodified lines
552// FIXME - change to new version of ScriptCache loading notification554// FIXME - change to new version of ScriptCache loading notification
553void ScriptEngines::onScriptEngineLoaded(const QString& rawScriptURL) {555void ScriptEngines::onScriptEngineLoaded(const QString& rawScriptURL) {
554 UserActivityLogger::getInstance().loadedScript(rawScriptURL);556 UserActivityLogger::getInstance().loadedScript(rawScriptURL);
555 QSharedPointer<BaseScriptEngine> baseScriptEngine = qobject_cast<ScriptEngine*>(sender())->sharedFromThis();557 ScriptManagerPointer scriptEngine = qobject_cast<ScriptManager*>(sender())->sharedFromThis();
556 ScriptEnginePointer scriptEngine = qSharedPointerCast<ScriptEngine>(baseScriptEngine);
557 558
558 launchScriptEngine(scriptEngine);559 launchScriptEngine(scriptEngine);
559 560
560 {561 {
561 QWriteLocker lock(&_scriptEnginesHashLock);562 QWriteLocker lock(&_scriptManagersHashLock);
562 QUrl url = QUrl(rawScriptURL);563 QUrl url = QUrl(rawScriptURL);
563 QUrl normalized = normalizeScriptURL(url);564 QUrl normalized = normalizeScriptURL(url);
564 _scriptEnginesHash.insert(normalized, scriptEngine);565 _scriptManagersHash.insert(normalized, scriptEngine);
565 }566 }
566 567
567 // Update settings with new script568 // Update settings with new script
@@ -573,40 +574,40 @@ void ScriptEngines::quitWhenFinished() { · 5 unmodified lines
573 qApp->quit();574 qApp->quit();
574}575}
575 576
576int ScriptEngines::runScriptInitializers(ScriptEnginePointer scriptEngine) {577int ScriptEngines::runScriptInitializers(ScriptManagerPointer scriptManager) {
577 auto nativeCount = DependencyManager::get<ScriptInitializers>()->runScriptInitializers(scriptEngine.data());578 auto nativeCount = DependencyManager::get<ScriptInitializers>()->runScriptInitializers(scriptManager->engine().data());
578 return nativeCount + ScriptInitializerMixin<ScriptEnginePointer>::runScriptInitializers(scriptEngine);579 return nativeCount + ScriptInitializerMixin<ScriptManagerPointer>::runScriptInitializers(scriptManager);
579}580}
580 581
581void ScriptEngines::launchScriptEngine(ScriptEnginePointer scriptEngine) {582void ScriptEngines::launchScriptEngine(ScriptManagerPointer scriptManager) {
582 connect(scriptEngine.data(), &ScriptEngine::finished, this, &ScriptEngines::onScriptFinished, Qt::DirectConnection);583 connect(scriptManager.data(), &ScriptManager::finished, this, &ScriptEngines::onScriptFinished, Qt::DirectConnection);
583 connect(scriptEngine.data(), &ScriptEngine::loadScript, [this](const QString& scriptName, bool userLoaded) {584 connect(scriptManager.data(), &ScriptManager::loadScript, [this](const QString& scriptName, bool userLoaded) {
584 loadScript(scriptName, userLoaded);585 loadScript(scriptName, userLoaded);
585 });586 });
586 connect(scriptEngine.data(), &ScriptEngine::reloadScript, [this](const QString& scriptName, bool userLoaded) {587 connect(scriptManager.data(), &ScriptManager::reloadScript, [this](const QString& scriptName, bool userLoaded) {
587 loadScript(scriptName, userLoaded, false, false, true);588 loadScript(scriptName, userLoaded, false, false, true);
588 });589 });
589 590
590 // register our application services and set it off on its own thread591 // register our application services and set it off on its own thread
591 runScriptInitializers(scriptEngine);592 runScriptInitializers(scriptManager);
592 scriptEngine->runInThread();593 scriptManager->runInThread();
593}594}
594 595
595void ScriptEngines::onScriptFinished(const QString& rawScriptURL, ScriptEnginePointer engine) {596void ScriptEngines::onScriptFinished(const QString& rawScriptURL, ScriptManagerPointer manager) {
596 bool removed = false;597 bool removed = false;
597 {598 {
598 QWriteLocker lock(&_scriptEnginesHashLock);599 QWriteLocker lock(&_scriptManagersHashLock);
599 const QUrl scriptURL = normalizeScriptURL(QUrl(rawScriptURL));600 const QUrl scriptURL = normalizeScriptURL(QUrl(rawScriptURL));
600 for (auto it = _scriptEnginesHash.find(scriptURL); it != _scriptEnginesHash.end(); ++it) {601 for (auto it = _scriptManagersHash.find(scriptURL); it != _scriptManagersHash.end(); ++it) {
601 if (it.value() == engine) {602 if (it.value() == manager) {
602 _scriptEnginesHash.erase(it);603 _scriptManagersHash.erase(it);
603 removed = true;604 removed = true;
604 break;605 break;
605 }606 }
606 }607 }
607 }608 }
608 609
609 removeScriptEngine(engine);610 removeScriptEngine(manager);
610 611
611 if (removed && !_isReloading) {612 if (removed && !_isReloading) {
612 // Update settings with removed script613 // Update settings with removed script

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t