Overte C++ Documentation
ContextAwareProfile.h
1 //
2 // Created by Bradley Austin Davis on 2018/07/27
3 // Copyright 2013-2018 High Fidelity, Inc.
4 //
5 // Distributed under the Apache License, Version 2.0.
6 // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
7 //
8 
9 #pragma once
10 
11 #ifndef hifi_ContextAwareProfile_h
12 #define hifi_ContextAwareProfile_h
13 
14 #include <atomic>
15 #include <QtCore/QHash>
16 #include <QtCore/QReadWriteLock>
17 #include <QtCore/QSet>
18 
19 #if !defined(Q_OS_ANDROID)
20 #include <QtWebEngine/QQuickWebEngineProfile>
21 #include <QtWebEngineCore/QWebEngineUrlRequestInterceptor>
22 
23 using ContextAwareProfileParent = QQuickWebEngineProfile;
24 using RequestInterceptorParent = QWebEngineUrlRequestInterceptor;
25 #else
26 #include <QtCore/QObject>
27 
28 using ContextAwareProfileParent = QObject;
29 using RequestInterceptorParent = QObject;
30 #endif
31 
32 class QQmlContext;
33 
34 class ContextAwareProfile : public ContextAwareProfileParent {
35  Q_OBJECT
36 public:
37  static void restrictContext(QQmlContext* context, bool restrict = true);
38  bool isRestricted();
39  Q_INVOKABLE bool isRestrictedGetProperty();
40 protected:
41 
42  class RequestInterceptor : public RequestInterceptorParent {
43  public:
44  RequestInterceptor(ContextAwareProfile* parent) : RequestInterceptorParent(parent), _profile(parent) { }
45  bool isRestricted() { return _profile->isRestricted(); }
46  protected:
47  ContextAwareProfile* _profile;
48  };
49 
50  ContextAwareProfile(QQmlContext* parent);
51  ~ContextAwareProfile();
52 
53 private:
54  typedef QSet<ContextAwareProfile*> ContextAwareProfileSet;
55  typedef QHash<QQmlContext*, ContextAwareProfileSet> ContextMap;
56 
57  QQmlContext* _context{ nullptr };
58  std::atomic<bool> _isRestricted{ false };
59 
60  static QReadWriteLock _contextMapProtect;
61  static ContextMap _contextMap;
62 };
63 
64 #endif // hifi_FileTypeProfile_h