feature:webengineprofile use non off record profile

This commit is contained in:
Xiao YiFang 2023-03-25 20:10:28 +08:00 committed by xiaoyifang
parent da406e1e02
commit e84b5aa861
5 changed files with 33 additions and 29 deletions

View file

@ -1,8 +1,9 @@
#include "articlewebpage.h" #include "articlewebpage.h"
#include "utils.hh" #include "utils.hh"
#include "globalbroadcaster.h"
ArticleWebPage::ArticleWebPage(QObject *parent) ArticleWebPage::ArticleWebPage( QObject * parent ):
: QWebEnginePage{parent} QWebEnginePage{ GlobalBroadcaster::instance()->profile, parent }
{ {
} }
bool ArticleWebPage::acceptNavigationRequest( const QUrl & resUrl, NavigationType type, bool isMainFrame ) bool ArticleWebPage::acceptNavigationRequest( const QUrl & resUrl, NavigationType type, bool isMainFrame )

View file

@ -3,6 +3,7 @@
#include <QObject> #include <QObject>
#include <vector> #include <vector>
#include <QWebEngineProfile>
#include "config.hh" #include "config.hh"
struct ActiveDictIds struct ActiveDictIds
@ -27,6 +28,7 @@ public:
static GlobalBroadcaster * instance(); static GlobalBroadcaster * instance();
unsigned currentGroupId; unsigned currentGroupId;
QString translateLineText{}; QString translateLineText{};
QWebEngineProfile * profile;
signals: signals:
void dictionaryChanges( ActiveDictIds ad ); void dictionaryChanges( ActiveDictIds ad );

View file

@ -97,12 +97,11 @@ void MainWindow::changeWebEngineViewFont()
{ {
if( cfg.preferences.webFontFamily.isEmpty() ) if( cfg.preferences.webFontFamily.isEmpty() )
{ {
QWebEngineProfile::defaultProfile()->settings()->resetFontFamily( QWebEngineSettings::StandardFont ); webEngineProfile->settings()->resetFontFamily( QWebEngineSettings::StandardFont );
} }
else else
{ {
QWebEngineProfile::defaultProfile()->settings()->setFontFamily( QWebEngineSettings::StandardFont, webEngineProfile->settings()->setFontFamily( QWebEngineSettings::StandardFont, cfg.preferences.webFontFamily );
cfg.preferences.webFontFamily );
} }
} }
@ -154,28 +153,28 @@ MainWindow::MainWindow( Config::Class & cfg_ ):
QThreadPool::globalInstance()->start( new InitSSLRunnable ); QThreadPool::globalInstance()->start( new InitSSLRunnable );
#endif #endif
GlobalBroadcaster::instance()->setPreference(&cfg.preferences); GlobalBroadcaster::instance()->setPreference( &cfg.preferences );
localSchemeHandler = new LocalSchemeHandler( articleNetMgr, this); webEngineProfile.reset( new QWebEngineProfile( "GoldenDictProfile" ) );
QWebEngineProfile::defaultProfile()->installUrlSchemeHandler( "gdlookup", localSchemeHandler ); GlobalBroadcaster::instance()->profile = webEngineProfile.get();
QWebEngineProfile::defaultProfile()->installUrlSchemeHandler( "bword", localSchemeHandler ); localSchemeHandler = new LocalSchemeHandler( articleNetMgr, this );
QWebEngineProfile::defaultProfile()->installUrlSchemeHandler( "entry", localSchemeHandler ); webEngineProfile->installUrlSchemeHandler( "gdlookup", localSchemeHandler );
webEngineProfile->installUrlSchemeHandler( "bword", localSchemeHandler );
webEngineProfile->installUrlSchemeHandler( "entry", localSchemeHandler );
iframeSchemeHandler = new IframeSchemeHandler( this ); iframeSchemeHandler = new IframeSchemeHandler( this );
QWebEngineProfile::defaultProfile()->installUrlSchemeHandler( "ifr", iframeSchemeHandler ); webEngineProfile->installUrlSchemeHandler( "ifr", iframeSchemeHandler );
QStringList localSchemes = { "gdau", "gico", "qrcx", "bres", "gdprg", "gdvideo", "gdpicture", "gdtts" }; QStringList localSchemes = { "gdau", "gico", "qrcx", "bres", "gdprg", "gdvideo", "gdpicture", "gdtts" };
resourceSchemeHandler = new ResourceSchemeHandler( articleNetMgr, this); resourceSchemeHandler = new ResourceSchemeHandler( articleNetMgr, this );
for( int i = 0; i < localSchemes.size(); i++ ) for( const auto & localScheme : localSchemes ) {
{ webEngineProfile->installUrlSchemeHandler( localScheme.toLatin1(), resourceSchemeHandler );
QWebEngineProfile::defaultProfile()->installUrlSchemeHandler( localSchemes.at( i ).toLatin1(),
resourceSchemeHandler );
} }
QWebEngineProfile::defaultProfile()->setUrlRequestInterceptor( new WebUrlRequestInterceptor(this) ); webEngineProfile->setUrlRequestInterceptor( new WebUrlRequestInterceptor( this ) );
if(!cfg.preferences.hideGoldenDictHeader){ if( !cfg.preferences.hideGoldenDictHeader ) {
QWebEngineProfile::defaultProfile()->setHttpUserAgent(QWebEngineProfile::defaultProfile()->httpUserAgent()+" GoldenDict/WebEngine"); webEngineProfile->setHttpUserAgent( webEngineProfile->httpUserAgent() + " GoldenDict/WebEngine" );
} }
qRegisterMetaType< Config::InputPhrase >(); qRegisterMetaType< Config::InputPhrase >();
@ -1416,8 +1415,7 @@ void MainWindow::setupNetworkCache( int maxSize )
return; // There is currently no cache and it is not needed. return; // There is currently no cache and it is not needed.
QString const cacheDirectory = Config::getNetworkCacheDir(); QString const cacheDirectory = Config::getNetworkCacheDir();
if( !QDir().mkpath( cacheDirectory ) ) if( !QDir().mkpath( cacheDirectory ) ) {
{
gdWarning( "Cannot create a cache directory %s. Disabling network cache.", cacheDirectory.toUtf8().constData() ); gdWarning( "Cannot create a cache directory %s. Disabling network cache.", cacheDirectory.toUtf8().constData() );
return; return;
} }
@ -1425,6 +1423,9 @@ void MainWindow::setupNetworkCache( int maxSize )
diskCache->setMaximumCacheSize( maxCacheSizeInBytes ); diskCache->setMaximumCacheSize( maxCacheSizeInBytes );
diskCache->setCacheDirectory( cacheDirectory ); diskCache->setCacheDirectory( cacheDirectory );
articleNetMgr.setCache( diskCache ); articleNetMgr.setCache( diskCache );
webEngineProfile->setCachePath( cacheDirectory );
webEngineProfile->setPersistentStoragePath( cacheDirectory );
} }
void MainWindow::makeDictionaries() void MainWindow::makeDictionaries()

View file

@ -136,7 +136,7 @@ private:
History history; History history;
DictionaryBar dictionaryBar; DictionaryBar dictionaryBar;
vector< sptr< Dictionary::Class > > dictionaries; vector< sptr< Dictionary::Class > > dictionaries;
QMap<std::string, sptr< Dictionary::Class > > dictMap; QMap< std::string, sptr< Dictionary::Class > > dictMap;
/// Here we store unmuted dictionaries when the dictionary bar is active /// Here we store unmuted dictionaries when the dictionary bar is active
vector< sptr< Dictionary::Class > > dictionariesUnmuted; vector< sptr< Dictionary::Class > > dictionariesUnmuted;
Instances::Groups groupInstances; Instances::Groups groupInstances;
@ -145,6 +145,9 @@ private:
QNetworkAccessManager dictNetMgr; // We give dictionaries a separate manager, QNetworkAccessManager dictNetMgr; // We give dictionaries a separate manager,
// since their requests can be destroyed // since their requests can be destroyed
// in a separate thread // in a separate thread
QScopedPointer< QWebEngineProfile > webEngineProfile;
AudioPlayerFactory audioPlayerFactory; AudioPlayerFactory audioPlayerFactory;
WordList * wordList; WordList * wordList;

View file

@ -657,14 +657,11 @@ void Preferences::on_buttonBox_accepted()
if( prevWebFontFamily != currentFontFamily ) if( prevWebFontFamily != currentFontFamily )
{ {
//reset to default font . //reset to default font .
if( currentFontFamily.isEmpty() ) if( currentFontFamily.isEmpty() ) {
{ GlobalBroadcaster::instance()->profile->settings()->resetFontFamily( QWebEngineSettings::StandardFont );
QWebEngineProfile::defaultProfile()->settings()->resetFontFamily( QWebEngineSettings::StandardFont );
} }
else else {
{ GlobalBroadcaster::instance()->profile->settings()->setFontFamily( QWebEngineSettings::StandardFont, currentFontFamily );
QWebEngineProfile::defaultProfile()->settings()->setFontFamily( QWebEngineSettings::StandardFont,
currentFontFamily );
} }
} }
} }