PR feedback

This commit is contained in:
Brad Davis 2019-08-29 08:46:46 -07:00
parent 5a6e2e5daf
commit af22ab55bb
2 changed files with 21 additions and 14 deletions

View file

@ -10,19 +10,20 @@
// //
#include "ContextAwareProfile.h" #include "ContextAwareProfile.h"
#if !defined(Q_OS_ANDROID)
#include <cassert>
#include <QtCore/QThread> #include <QtCore/QThread>
#include <QtQml/QQmlContext> #include <QtQml/QQmlContext>
#include <shared/QtHelpers.h> #include <shared/QtHelpers.h>
#include <SharedUtil.h> #include <SharedUtil.h>
static const QString RESTRICTED_FLAG_PROPERTY = "RestrictFileAccess"; static const QString RESTRICTED_FLAG_PROPERTY = "RestrictFileAccess";
ContextAwareProfile::ContextAwareProfile(QQmlContext* context) : ContextAwareProfile::ContextAwareProfile(QQmlContext* context) :
QQuickWebEngineProfile(context), _context(context) { } QQuickWebEngineProfile(context), _context(context) {
assert(context);
}
void ContextAwareProfile::restrictContext(QQmlContext* context, bool restrict) { void ContextAwareProfile::restrictContext(QQmlContext* context, bool restrict) {
@ -40,12 +41,9 @@ bool ContextAwareProfile::isRestrictedInternal() {
bool ContextAwareProfile::isRestricted() { bool ContextAwareProfile::isRestricted() {
auto now = usecTimestampNow(); auto now = usecTimestampNow();
auto cacheAge = now - _lastCacheCheck; if (now > _cacheExpiry) {
if (cacheAge > MAX_CACHE_AGE) {
_cachedValue = isRestrictedInternal(); _cachedValue = isRestrictedInternal();
_lastCacheCheck = now; _cacheExpiry = now + MAX_CACHE_AGE;
} }
return _cachedValue; return _cachedValue;
} }
#endif

View file

@ -16,11 +16,21 @@
#if !defined(Q_OS_ANDROID) #if !defined(Q_OS_ANDROID)
#include <QtWebEngine/QQuickWebEngineProfile> #include <QtWebEngine/QQuickWebEngineProfile>
#include <QtWebEngineCore/QWebEngineUrlRequestInterceptor> #include <QtWebEngineCore/QWebEngineUrlRequestInterceptor>
using ContextAwareProfileParent = QQuickWebEngineProfile;
using RequestInterceptorParent = QWebEngineUrlRequestInterceptor;
#else
#include <QtCore/QObject>
using ContextAwareProfileParent = QObject;
using RequestInterceptorParent = QObject;
#endif
#include <NumericalConstants.h> #include <NumericalConstants.h>
class QQmlContext; class QQmlContext;
class ContextAwareProfile : public QQuickWebEngineProfile { class ContextAwareProfile : public ContextAwareProfileParent {
Q_OBJECT Q_OBJECT
public: public:
static void restrictContext(QQmlContext* context, bool restrict = true); static void restrictContext(QQmlContext* context, bool restrict = true);
@ -28,9 +38,9 @@ public:
Q_INVOKABLE bool isRestrictedInternal(); Q_INVOKABLE bool isRestrictedInternal();
protected: protected:
class RequestInterceptor : public QWebEngineUrlRequestInterceptor { class RequestInterceptor : public RequestInterceptorParent {
public: public:
RequestInterceptor(ContextAwareProfile* parent) : QWebEngineUrlRequestInterceptor(parent), _profile(parent) {} RequestInterceptor(ContextAwareProfile* parent) : RequestInterceptorParent(parent), _profile(parent) { }
bool isRestricted() { return _profile->isRestricted(); } bool isRestricted() { return _profile->isRestricted(); }
protected: protected:
ContextAwareProfile* _profile; ContextAwareProfile* _profile;
@ -39,9 +49,8 @@ protected:
ContextAwareProfile(QQmlContext* parent); ContextAwareProfile(QQmlContext* parent);
QQmlContext* _context{ nullptr }; QQmlContext* _context{ nullptr };
bool _cachedValue{ false }; bool _cachedValue{ false };
quint64 _lastCacheCheck{ 0 }; quint64 _cacheExpiry{ 0 };
static const quint64 MAX_CACHE_AGE = MSECS_PER_SECOND; constexpr static quint64 MAX_CACHE_AGE = MSECS_PER_SECOND;
}; };
#endif
#endif // hifi_FileTypeProfile_h #endif // hifi_FileTypeProfile_h