-
Notifications
You must be signed in to change notification settings - Fork 589
/
Copy pathWebProfileMgr.cpp
141 lines (123 loc) · 6.81 KB
/
WebProfileMgr.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/************************************************************************
**
** Copyright (C) 2024 Kevin B. Hendricks, Stratford Ontario Canada
**
** This file is part of Sigil.
**
** Sigil is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation, either version 3 of the License, or
** (at your option) any later version.
**
** Sigil is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with Sigil. If not, see <http://www.gnu.org/licenses/>.
**
*************************************************************************/
#include <QDir>
#include <QString>
#include <QStringList>
#include <QWebEngineProfile>
#include <QWebEngineSettings>
#include "Misc/Utility.h"
#include "Misc/SettingsStore.h"
#include "Misc/URLSchemeHandler.h"
#include "Misc/URLInterceptor.h"
#include "Misc/WebProfileMgr.h"
WebProfileMgr *WebProfileMgr::m_instance = 0;
WebProfileMgr *WebProfileMgr::instance()
{
if (m_instance == 0) {
m_instance = new WebProfileMgr();
}
return m_instance;
}
QWebEngineProfile* WebProfileMgr::GetPreviewProfile()
{
return m_preview_profile;
}
QWebEngineProfile* WebProfileMgr::GetOneTimeProfile()
{
return m_onetime_profile;
}
void WebProfileMgr::InitializeDefaultSettings(QWebEngineSettings* web_settings)
{
SettingsStore ss;
SettingsStore::PreviewAppearance PVAppearance = ss.previewAppearance();
web_settings->setFontSize(QWebEngineSettings::DefaultFontSize, PVAppearance.font_size);
web_settings->setFontFamily(QWebEngineSettings::StandardFont, PVAppearance.font_family_standard);
web_settings->setFontFamily(QWebEngineSettings::SerifFont, PVAppearance.font_family_serif);
web_settings->setFontFamily(QWebEngineSettings::SansSerifFont, PVAppearance.font_family_sans_serif);
web_settings->setAttribute(QWebEngineSettings::AutoLoadImages, true);
web_settings->setAttribute(QWebEngineSettings::JavascriptEnabled, false);
web_settings->setAttribute(QWebEngineSettings::JavascriptCanOpenWindows, false);
web_settings->setAttribute(QWebEngineSettings::JavascriptCanAccessClipboard, false);
web_settings->setAttribute(QWebEngineSettings::LocalContentCanAccessRemoteUrls, false);
web_settings->setAttribute(QWebEngineSettings::LocalContentCanAccessFileUrls, false);
web_settings->setAttribute(QWebEngineSettings::PluginsEnabled, false);
web_settings->setAttribute(QWebEngineSettings::AutoLoadIconsForPage, false);
web_settings->setAttribute(QWebEngineSettings::FocusOnNavigationEnabled, false);
web_settings->setAttribute(QWebEngineSettings::AllowRunningInsecureContent, false);
web_settings->setAttribute(QWebEngineSettings::XSSAuditingEnabled, true);
web_settings->setAttribute(QWebEngineSettings::AllowGeolocationOnInsecureOrigins, false);
web_settings->setAttribute(QWebEngineSettings::ScreenCaptureEnabled, false);
web_settings->setAttribute(QWebEngineSettings::LocalStorageEnabled, false);
web_settings->setAttribute(QWebEngineSettings::AllowWindowActivationFromJavaScript, false);
web_settings->setUnknownUrlSchemePolicy(QWebEngineSettings::DisallowUnknownUrlSchemes);
web_settings->setAttribute(QWebEngineSettings::PlaybackRequiresUserGesture, true);
web_settings->setAttribute(QWebEngineSettings::JavascriptCanPaste, false);
web_settings->setAttribute(QWebEngineSettings::DnsPrefetchEnabled, false);
web_settings->setAttribute(QWebEngineSettings::PdfViewerEnabled, false);
}
WebProfileMgr::WebProfileMgr()
{
// Create URLSchemeHandler and URLInterceptor
m_URLhandler = new URLSchemeHandler();
m_URLint = new URLInterceptor();
// initialize the defaultProfile to be restrictive for security
QWebEngineSettings *web_settings = QWebEngineProfile::defaultProfile()->settings();
InitializeDefaultSettings(web_settings);
// Use URLInterceptor for protection
QWebEngineProfile::defaultProfile()->setUrlRequestInterceptor(m_URLint);
// create the profile for Preview
SettingsStore ss;
m_preview_profile = new QWebEngineProfile("Preview", nullptr);
InitializeDefaultSettings(m_preview_profile->settings());
m_preview_profile->settings()->setAttribute(QWebEngineSettings::LocalContentCanAccessFileUrls, true);
m_preview_profile->settings()->setAttribute(QWebEngineSettings::ErrorPageEnabled, false);
m_preview_profile->settings()->setAttribute(QWebEngineSettings::PluginsEnabled, true);
m_preview_profile->settings()->setAttribute(QWebEngineSettings::PdfViewerEnabled, true);
m_preview_profile->settings()->setDefaultTextEncoding("UTF-8");
m_preview_profile->settings()->setAttribute(QWebEngineSettings::JavascriptEnabled, (ss.javascriptOn() == 1));
m_preview_profile->settings()->setAttribute(QWebEngineSettings::JavascriptCanOpenWindows, (ss.javascriptOn() == 1));
m_preview_profile->settings()->setAttribute(QWebEngineSettings::LocalContentCanAccessRemoteUrls, (ss.remoteOn() == 1));
m_preview_profile->settings()->setAttribute(QWebEngineSettings::FocusOnNavigationEnabled, false);
m_preview_profile->settings()->setAttribute(QWebEngineSettings::LocalStorageEnabled, true);
// Enable local-storage for epub3
QString localStorePath = Utility::DefinePrefsDir() + "/local-storage/";
QDir storageDir(localStorePath);
if (!storageDir.exists()) {
storageDir.mkpath(localStorePath);
}
m_preview_profile->setPersistentStoragePath(localStorePath);
// Use both our URLInterceptor and our URLSchemeHandler
m_preview_profile->installUrlSchemeHandler("sigil", m_URLhandler);
m_preview_profile->setUrlRequestInterceptor(m_URLint);
// create the profile for OneTime
m_onetime_profile = new QWebEngineProfile();
InitializeDefaultSettings(m_onetime_profile->settings());
m_onetime_profile->settings()->setDefaultTextEncoding("UTF-8");
m_onetime_profile->settings()->setAttribute(QWebEngineSettings::LocalContentCanAccessFileUrls, true);
m_onetime_profile->settings()->setAttribute(QWebEngineSettings::PluginsEnabled, true);
m_onetime_profile->settings()->setAttribute(QWebEngineSettings::PdfViewerEnabled, true);
// Unfortunately the PdfView used for PdfTab now requires both java and LocalStorage work
m_onetime_profile->settings()->setAttribute(QWebEngineSettings::JavascriptEnabled, (ss.javascriptOn() == 1));
m_onetime_profile->settings()->setAttribute(QWebEngineSettings::LocalStorageEnabled, true);
m_onetime_profile->setPersistentStoragePath(localStorePath);
// Use URLInterceptor for protection
m_onetime_profile->setUrlRequestInterceptor(m_URLint);
}