diff --git a/libraries/ui/src/ui/types/ContextAwareProfile.cpp b/libraries/ui/src/ui/types/ContextAwareProfile.cpp index 8e363e6546..ee47435d4d 100644 --- a/libraries/ui/src/ui/types/ContextAwareProfile.cpp +++ b/libraries/ui/src/ui/types/ContextAwareProfile.cpp @@ -10,13 +10,15 @@ // #include "ContextAwareProfile.h" -#include -#include - #if !defined(Q_OS_ANDROID) +#include #include +#include +#include + + static const QString RESTRICTED_FLAG_PROPERTY = "RestrictFileAccess"; ContextAwareProfile::ContextAwareProfile(QQmlContext* context) : @@ -27,12 +29,23 @@ void ContextAwareProfile::restrictContext(QQmlContext* context, bool restrict) { context->setContextProperty(RESTRICTED_FLAG_PROPERTY, restrict); } -bool ContextAwareProfile::isRestricted() { +bool ContextAwareProfile::isRestrictedInternal() { if (QThread::currentThread() != thread()) { bool restrictedResult = false; - BLOCKING_INVOKE_METHOD(this, "isRestricted", Q_RETURN_ARG(bool, restrictedResult)); + BLOCKING_INVOKE_METHOD(this, "isRestrictedInternal", Q_RETURN_ARG(bool, restrictedResult)); + return restrictedResult; } return _context->contextProperty(RESTRICTED_FLAG_PROPERTY).toBool(); } +bool ContextAwareProfile::isRestricted() { + auto now = usecTimestampNow(); + auto cacheAge = now - _lastCacheCheck; + if (cacheAge > MAX_CACHE_AGE) { + _cachedValue = isRestrictedInternal(); + _lastCacheCheck = now; + } + return _cachedValue; +} + #endif \ No newline at end of file diff --git a/libraries/ui/src/ui/types/ContextAwareProfile.h b/libraries/ui/src/ui/types/ContextAwareProfile.h index ad42c2677d..b55f95c180 100644 --- a/libraries/ui/src/ui/types/ContextAwareProfile.h +++ b/libraries/ui/src/ui/types/ContextAwareProfile.h @@ -16,6 +16,7 @@ #if !defined(Q_OS_ANDROID) #include #include +#include class QQmlContext; @@ -23,7 +24,8 @@ class ContextAwareProfile : public QQuickWebEngineProfile { Q_OBJECT public: static void restrictContext(QQmlContext* context, bool restrict = true); - Q_INVOKABLE bool isRestricted(); + bool isRestricted(); + Q_INVOKABLE bool isRestrictedInternal(); protected: class RequestInterceptor : public QWebEngineUrlRequestInterceptor { @@ -35,7 +37,10 @@ protected: }; ContextAwareProfile(QQmlContext* parent); - QQmlContext* _context; + QQmlContext* _context{ nullptr }; + bool _cachedValue{ false }; + quint64 _lastCacheCheck{ 0 }; + static const quint64 MAX_CACHE_AGE = MSECS_PER_SECOND; }; #endif