Overte C++ Documentation
ScriptCache.h
1 //
2 // ScriptCache.h
3 // libraries/script-engine/src
4 //
5 // Created by Brad Hefta-Gaub on 2015-03-30
6 // Copyright 2015 High Fidelity, Inc.
7 // Copyright 2023 Overte e.V.
8 //
9 // Distributed under the Apache License, Version 2.0.
10 // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
11 // SPDX-License-Identifier: Apache-2.0
12 //
13 
16 
17 #ifndef hifi_ScriptCache_h
18 #define hifi_ScriptCache_h
19 
20 #include <mutex>
21 #include <DependencyManager.h>
22 
23 using contentAvailableCallback = std::function<void(const QString& scriptOrURL, const QString& contents, bool isURL, bool contentAvailable, const QString& status)>;
24 
25 class ScriptUser {
26 public:
27  virtual void scriptContentsAvailable(const QUrl& url, const QString& scriptContents) = 0;
28  virtual void errorInLoadingScript(const QUrl& url) = 0;
29 };
30 
31 class ScriptRequest {
32 public:
33  static const int MAX_RETRIES { 5 };
34  static const int START_DELAY_BETWEEN_RETRIES { 200 };
35  std::vector<contentAvailableCallback> scriptUsers { };
36  int numRetries { 0 };
37  int maxRetries { MAX_RETRIES };
38 };
39 
41 class ScriptCache : public QObject, public Dependency {
42  Q_OBJECT
43  SINGLETON_DEPENDENCY
44 
45  using Mutex = std::mutex;
46  using Lock = std::unique_lock<Mutex>;
47 
48 public:
49  static const QString STATUS_INLINE;
50  static const QString STATUS_CACHED;
51  static bool isSuccessStatus(const QString& status) {
52  return status == "Success" || status == STATUS_INLINE || status == STATUS_CACHED;
53  }
54 
55  void clearCache();
56  Q_INVOKABLE void clearATPScriptsFromCache();
57  void getScriptContents(const QString& scriptOrURL, contentAvailableCallback contentAvailable, bool forceDownload = false, int maxRetries = ScriptRequest::MAX_RETRIES);
58 
59  void deleteScript(const QUrl& unnormalizedURL);
60 
61 private:
62  void scriptContentAvailable(int maxRetries); // new version
63  ScriptCache(QObject* parent = NULL);
64 
65  Mutex _containerLock;
66  QMap<QUrl, ScriptRequest> _activeScriptRequests;
67 
68  QHash<QUrl, QVariantMap> _scriptCache;
69  QMultiMap<QUrl, ScriptUser*> _scriptUsers;
70 };
71 
72 #endif // hifi_ScriptCache_h
73 
Dependency for for loading and caching scripts.
Definition: ScriptCache.h:41