Add caching to the restricted value

This commit is contained in:
Brad Davis 2019-08-28 10:13:03 -07:00
parent 954773124a
commit 5a6e2e5daf
2 changed files with 25 additions and 7 deletions

View file

@ -10,13 +10,15 @@
//
#include "ContextAwareProfile.h"
#include <QtCore/QThread>
#include <shared/QtHelpers.h>
#if !defined(Q_OS_ANDROID)
#include <QtCore/QThread>
#include <QtQml/QQmlContext>
#include <shared/QtHelpers.h>
#include <SharedUtil.h>
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

View file

@ -16,6 +16,7 @@
#if !defined(Q_OS_ANDROID)
#include <QtWebEngine/QQuickWebEngineProfile>
#include <QtWebEngineCore/QWebEngineUrlRequestInterceptor>
#include <NumericalConstants.h>
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