Showing the changes to libraries/script-engine/src/ScriptCache.cpp (1 of 2 files). Show all
Unified · Split
| @@ -28,21 +28,26 @@ ScriptCache::ScriptCache(QObject* parent) { · 27 unmodified lines | ||
| 28 | 28 | } |
| 29 | 29 | |
| 30 | 30 | void ScriptCache::clearCache() { |
| 31 | + Lock lock(_containerLock); | |
| 31 | 32 | _scriptCache.clear(); |
| 32 | 33 | } |
| 33 | 34 | |
| 34 | 35 | QString ScriptCache::getScript(const QUrl& unnormalizedURL, ScriptUser* scriptUser, bool& isPending, bool reload) { |
| 35 | 36 | QUrl url = ResourceManager::normalizeURL(unnormalizedURL); |
| 36 | 37 | QString scriptContents; |
| 38 | + | |
| 39 | + Lock lock(_containerLock); | |
| 37 | 40 | if (_scriptCache.contains(url) && !reload) { |
| 38 | 41 | qCDebug(scriptengine) << "Found script in cache:" << url.toString(); |
| 39 | 42 | scriptContents = _scriptCache[url]; |
| 43 | + lock.unlock(); | |
| 40 | 44 | scriptUser->scriptContentsAvailable(url, scriptContents); |
| 41 | 45 | isPending = false; |
| 42 | 46 | } else { |
| 43 | 47 | isPending = true; |
| 44 | 48 | bool alreadyWaiting = _scriptUsers.contains(url); |
| 45 | 49 | _scriptUsers.insert(url, scriptUser); |
| 50 | + lock.unlock(); | |
| 46 | 51 | |
| 47 | 52 | if (alreadyWaiting) { |
| 48 | 53 | qCDebug(scriptengine) << "Already downloading script at:" << url.toString(); |
| @@ -58,6 +63,7 @@ QString ScriptCache::getScript(const QUrl& unnormalizedURL, ScriptUser* scriptUs · 9 unmodified lines | ||
| 58 | 63 | |
| 59 | 64 | void ScriptCache::deleteScript(const QUrl& unnormalizedURL) { |
| 60 | 65 | QUrl url = ResourceManager::normalizeURL(unnormalizedURL); |
| 66 | + Lock lock(_containerLock); | |
| 61 | 67 | if (_scriptCache.contains(url)) { |
| 62 | 68 | qCDebug(scriptengine) << "Delete script from cache:" << url.toString(); |
| 63 | 69 | _scriptCache.remove(url); |
| @@ -67,17 +73,22 @@ void ScriptCache::deleteScript(const QUrl& unnormalizedURL) { · 3 unmodified lines | ||
| 67 | 73 | void ScriptCache::scriptDownloaded() { |
| 68 | 74 | ResourceRequest* req = qobject_cast<ResourceRequest*>(sender()); |
| 69 | 75 | QUrl url = req->getUrl(); |
| 76 | + | |
| 77 | + Lock lock(_containerLock); | |
| 70 | 78 | QList<ScriptUser*> scriptUsers = _scriptUsers.values(url); |
| 71 | 79 | _scriptUsers.remove(url); |
| 72 | 80 | |
| 73 | 81 | if (req->getResult() == ResourceRequest::Success) { |
| 74 | − _scriptCache[url] = req->getData(); | |
| 82 | + auto scriptContents = req->getData(); | |
| 83 | + _scriptCache[url] = scriptContents; | |
| 84 | + lock.unlock(); | |
| 75 | 85 | qCDebug(scriptengine) << "Done downloading script at:" << url.toString(); |
| 76 | 86 | |
| 77 | 87 | foreach(ScriptUser* user, scriptUsers) { |
| 78 | − user->scriptContentsAvailable(url, _scriptCache[url]); | |
| 88 | + user->scriptContentsAvailable(url, scriptContents); | |
| 79 | 89 | } |
| 80 | 90 | } else { |
| 91 | + lock.unlock(); | |
| 81 | 92 | qCWarning(scriptengine) << "Error loading script from URL " << url; |
| 82 | 93 | foreach(ScriptUser* user, scriptUsers) { |
| 83 | 94 | user->errorInLoadingScript(url); |
| @@ -99,15 +110,19 @@ void ScriptCache::getScriptContents(const QString& scriptOrURL, contentAvailable · 15 unmodified lines | ||
| 99 | 110 | return; |
| 100 | 111 | } |
| 101 | 112 | |
| 113 | + Lock lock(_containerLock); | |
| 102 | 114 | if (_scriptCache.contains(url) && !forceDownload) { |
| 115 | + auto scriptContent = _scriptCache[url]; | |
| 116 | + lock.unlock(); | |
| 103 | 117 | qCDebug(scriptengine) << "Found script in cache:" << url.toString(); |
| 104 | 118 | #if 1 // def THREAD_DEBUGGING |
| 105 | 119 | qCDebug(scriptengine) << "ScriptCache::getScriptContents() about to call contentAvailable() on thread [" << QThread::currentThread() << "] expected thread [" << thread() << "]"; |
| 106 | 120 | #endif |
| 107 | − contentAvailable(url.toString(), _scriptCache[url], true, true); | |
| 121 | + contentAvailable(url.toString(), scriptContent, true, true); | |
| 108 | 122 | } else { |
| 109 | 123 | bool alreadyWaiting = _contentCallbacks.contains(url); |
| 110 | 124 | _contentCallbacks.insert(url, contentAvailable); |
| 125 | + lock.unlock(); | |
| 111 | 126 | |
| 112 | 127 | if (alreadyWaiting) { |
| 113 | 128 | qCDebug(scriptengine) << "Already downloading script at:" << url.toString(); |
| @@ -129,19 +144,29 @@ void ScriptCache::scriptContentAvailable() { · 15 unmodified lines | ||
| 129 | 144 | #endif |
| 130 | 145 | ResourceRequest* req = qobject_cast<ResourceRequest*>(sender()); |
| 131 | 146 | QUrl url = req->getUrl(); |
| 132 | − QList<contentAvailableCallback> allCallbacks = _contentCallbacks.values(url); | |
| 133 | − _contentCallbacks.remove(url); | |
| 134 | 147 | |
| 135 | − bool success = req->getResult() == ResourceRequest::Success; | |
| 148 | + QString scriptContent; | |
| 149 | + QList<contentAvailableCallback> allCallbacks; | |
| 150 | + bool success { false }; | |
| 136 | 151 | |
| 137 | − if (success) { | |
| 138 | − _scriptCache[url] = req->getData(); | |
| 139 | − qCDebug(scriptengine) << "Done downloading script at:" << url.toString(); | |
| 140 | − } else { | |
| 141 | − qCWarning(scriptengine) << "Error loading script from URL " << url; | |
| 152 | + { | |
| 153 | + Lock lock(_containerLock); | |
| 154 | + allCallbacks = _contentCallbacks.values(url); | |
| 155 | + _contentCallbacks.remove(url); | |
| 156 | + success = req->getResult() == ResourceRequest::Success; | |
| 157 | + | |
| 158 | + if (success) { | |
| 159 | + _scriptCache[url] = scriptContent = req->getData(); | |
| 160 | + qCDebug(scriptengine) << "Done downloading script at:" << url.toString(); | |
| 161 | + } else { | |
| 162 | + // Dubious, but retained here because it matches the behavior before fixing the threading | |
| 163 | + scriptContent = _scriptCache[url]; | |
| 164 | + qCWarning(scriptengine) << "Error loading script from URL " << url; | |
| 165 | + } | |
| 142 | 166 | } |
| 167 | + | |
| 143 | 168 | foreach(contentAvailableCallback thisCallback, allCallbacks) { |
| 144 | − thisCallback(url.toString(), _scriptCache[url], true, success); | |
| 169 | + thisCallback(url.toString(), scriptContent, true, success); | |
| 145 | 170 | } |
| 146 | 171 | req->deleteLater(); |
| 147 | 172 | } |