Dekko
Loading...
Searching...
No Matches
DekkoWebContext.cpp
1#include "DekkoWebContext.h"
2#include "DekkoWebEngineUrlSchemeHandler.h"
3#include "DekkoWebEngineUrlRequestInterceptor.h"
4#include <QQmlEngine>
5#include <QtQml>
6#include <QDebug>
7#include <QDir>
8#include <QStorageInfo>
9#include <QCoreApplication>
10#include <QStandardPaths>
11
12DekkoWebContext::DekkoWebContext(QQuickWebEngineProfile *parent) : QQuickWebEngineProfile(parent)
13{
14 this->setRequestInterceptor(&this->urlRequestInterceptor);
15
16 this->installUrlSchemeHandler("dekko-msg", &this->urlSchemeHandler);
17 this->installUrlSchemeHandler("dekko-part", &this->urlSchemeHandler);
18 this->installUrlSchemeHandler("cid", &this->urlSchemeHandler);
19 connect(&this->urlRequestInterceptor, SIGNAL(interceptedRemoteRequest(bool)), this, SLOT(onInterceptedRemoteRequest(bool)));
20}
21
22void DekkoWebContext::onInterceptedRemoteRequest(bool wasBlocked)
23{
24 if (wasBlocked)
25 {
26 emit remoteContentBlocked();
27 }
28}
29
30void DekkoWebContext::setMessageUid(QString messageUid)
31{
32 this->urlSchemeHandler.setMessageUid(messageUid);
33}
34
35void DekkoWebContext::setRemoteContentAllowed(bool allowed)
36{
37 this->urlRequestInterceptor.setBlockRemoteResources(!allowed);
38}
39
40QString DekkoWebContext::getMessageUid() const
41{
42 return this->urlSchemeHandler.getMessageUid();
43}
44
45bool DekkoWebContext::isRemoteContentAllowed() const
46{
47 return !this->urlRequestInterceptor.areRemoteResourcesBlocked();
48}
49
50
51QString DekkoWebContext::cacheLocation() const
52{
53 QDir location(QStandardPaths::writableLocation(QStandardPaths::CacheLocation));
54 if (!location.exists()) {
55 QDir::root().mkpath(location.absolutePath());
56 }
57 return location.absolutePath();
58}
59
60QString DekkoWebContext::dataLocation() const
61{
62 QDir location(QStandardPaths::writableLocation(QStandardPaths::DataLocation));
63 if (!location.exists()) {
64 QDir::root().mkpath(location.absolutePath());
65 } else {
66 // Prior to fixing https://launchpad.net/bugs/1424726, chromium’s cache
67 // data was written to the data location. Purge the old cache data.
68 QDir(location.absoluteFilePath("Cache")).removeRecursively();
69 }
70 return location.absolutePath();
71}
72
73int DekkoWebContext::cacheSizeHint() const
74{
75 if (QCoreApplication::applicationName() == "webbrowser-app") {
76 // Let chromium decide the optimum cache size based on available disk space
77 return 0;
78 } else {
79 // For webapps and other embedders, determine the cache size hint
80 // using heuristics based on the disk space (total, and available).
81 QStorageInfo storageInfo(QStandardPaths::writableLocation(QStandardPaths::CacheLocation));
82 const int MB = 1024 * 1024;
83 // The total cache size for all apps should not exceed 10% of the total disk space
84 int maxSharedCache = storageInfo.bytesTotal() / MB * 0.1;
85 // One given app is allowed to use up to 5% o the total cache size
86 int maxAppCacheAllowance = maxSharedCache * 0.05;
87 // Ensure it never exceeds 200 MB though
88 int maxAppCacheAbsolute = qMin(maxAppCacheAllowance, 200);
89 // Never use more than 20% of the available disk space
90 int maxAppCacheRelative = storageInfo.bytesAvailable() / MB * 0.2;
91 // Never set a size hint below 5 MB, as that would result in a very inefficient cache
92 return qMax(5, qMin(maxAppCacheAbsolute, maxAppCacheRelative));
93 }
94}
95
96QObject * DekkoWebContext::qmlEngineInjector() const
97{
98 return nullptr;
99}
100
101void DekkoWebContext::setQmlEngineInjector(QObject * dummy)
102{
103 QQmlEngine* engine = qmlEngine(dummy);
104 if (!engine) {
105 qDebug() << "Failed to get qml engine from injected qml object.";
106 return;
107 }
108
109 this->urlSchemeHandler.setNetworkAccessManager(engine->networkAccessManager());
110}