Skip to content

Commit

Permalink
opt: GlobalNetworkAccessManager
Browse files Browse the repository at this point in the history
  • Loading branch information
shenlebantongying committed Jun 21, 2024
1 parent 21aeb27 commit 9fa2e4b
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 33 deletions.
7 changes: 4 additions & 3 deletions src/ankiconnector.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <QJsonObject>
#include <QJsonValue>
#include "utils.hh"
#include "global_network_access_manager.hh"

QString markTargetWord( QString const & sentence, QString const & word )
{
Expand All @@ -12,10 +13,10 @@ QString markTargetWord( QString const & sentence, QString const & word )
}

AnkiConnector::AnkiConnector( QObject * parent, Config::Class const & _cfg ):
QObject{ parent },
QObject{parent},
cfg( _cfg )
{
mgr = new QNetworkAccessManager( this );
mgr = GlobalNetworkAccessManager;
connect( mgr, &QNetworkAccessManager::finished, this, &AnkiConnector::finishedSlot );
}

Expand Down Expand Up @@ -119,4 +120,4 @@ void AnkiConnector::finishedSlot( QNetworkReply * reply )
}

reply->deleteLater();
}
}
21 changes: 21 additions & 0 deletions src/global_network_access_manager.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#pragma once

#include <QNetworkAccessManager>

/*
* Notes:
*
* This MUST be used only in the main/UI thread
*
* Retaining a pointer in objects to this globalNetworkAccessManager is OK.
* Having a pointer to it reduce minor overhead of global object accessing.
*
*/

#if ( QT_VERSION < QT_VERSION_CHECK( 6, 4, 0 ) )
Q_GLOBAL_STATIC( QNetworkAccessManager, GlobalNetAccessMgr );
#else
#include <qapplicationstatic.h>
Q_APPLICATION_STATIC( QNetworkAccessManager, GlobalNetworkAccessManager );

#endif
6 changes: 5 additions & 1 deletion src/iframeschemehandler.cc
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
#include "iframeschemehandler.hh"

#include <iostream>
#include <QTextCodec>

#include "global_network_access_manager.hh"

IframeSchemeHandler::IframeSchemeHandler( QObject * parent ):
QWebEngineUrlSchemeHandler( parent )
{
Expand All @@ -17,7 +20,8 @@ void IframeSchemeHandler::requestStarted( QWebEngineUrlRequestJob * requestJob )
request.setAttribute( QNetworkRequest::RedirectPolicyAttribute,
QNetworkRequest::RedirectPolicy::NoLessSafeRedirectPolicy );

QNetworkReply * reply = mgr.get( request );
QNetworkReply * reply = GlobalNetworkAccessManager->get( request );
std::cout<< "thread ID" << std::this_thread::get_id() << std::endl;

auto finishAction = [ = ]() {
QByteArray contentType = "text/html";
Expand Down
5 changes: 0 additions & 5 deletions src/iframeschemehandler.hh
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,6 @@ class IframeSchemeHandler: public QWebEngineUrlSchemeHandler
public:
IframeSchemeHandler( QObject * parent = nullptr );
void requestStarted( QWebEngineUrlRequestJob * requestJob );

protected:

private:
QNetworkAccessManager mgr;
};

#endif // IFRAMESCHEMEHANDLER_H
36 changes: 15 additions & 21 deletions src/ui/mainwindow.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#ifndef NO_EPWING_SUPPORT
#include "dict/epwing_book.hh"
#endif

#include <iostream>
#include "mainwindow.hh"
#include <QWebEngineProfile>
#include "editdictionaries.hh"
Expand All @@ -16,25 +16,16 @@
#include "mruqmenu.hh"
#include "gestures.hh"
#include "dictheadwords.hh"
#include <QTextStream>
#include <QDir>
#include <QUrl>

#include <QMessageBox>
#include <QIcon>
#include <QList>
#include <QToolBar>
#include <QCloseEvent>
#include <QDesktopServices>
#include <QProcess>
#include <QCryptographicHash>
#include <QFileDialog>
#include <QPrinter>
#include <QPageSetupDialog>
#include <QPrintPreviewDialog>
#include <QPrintDialog>
#include <QRunnable>
#include <QThreadPool>
#include <QSslConfiguration>
#include <QStyleFactory>
#include "weburlrequestinterceptor.hh"
#include "folding.hh"
Expand All @@ -49,8 +40,7 @@
#include "help.hh"
#include "ui_authentication.h"
#include "resourceschemehandler.hh"
#include <QListWidgetItem>

#include "global_network_access_manager.hh"
#include "globalregex.hh"

#ifdef Q_OS_MAC
Expand Down Expand Up @@ -161,7 +151,6 @@ MainWindow::MainWindow( Config::Class & cfg_ ):
articleMaker,
cfg.preferences.disallowContentFromOtherSites,
cfg.preferences.hideGoldenDictHeader ),
dictNetMgr( this ),
audioPlayerFactory( cfg.preferences ),
wordFinder( this ),
wordListSelChanged( false ),
Expand All @@ -180,8 +169,13 @@ MainWindow::MainWindow( Config::Class & cfg_ ):

GlobalBroadcaster::instance()->setPreference( &cfg.preferences );

localSchemeHandler = new LocalSchemeHandler( articleNetMgr, this );
QStringList htmlScheme = { "gdlookup", "bword", "entry" };

// Ensure globalNetworkAccessManager initalized on the UI theread by doing something with side effect.
qDebug() << "Initalized: " << GlobalNetworkAccessManager->metaObject()->className();
std::cout<< "Main thread ID" << std::this_thread::get_id() << std::endl;

localSchemeHandler = new LocalSchemeHandler( articleNetMgr, this );
QStringList htmlScheme = {"gdlookup", "bword", "entry"};
for ( const auto & localScheme : htmlScheme ) {
QWebEngineProfile::defaultProfile()->installUrlSchemeHandler( localScheme.toLatin1(), localSchemeHandler );
}
Expand Down Expand Up @@ -717,7 +711,7 @@ MainWindow::MainWindow( Config::Class & cfg_ ):
//set webengineview font
changeWebEngineViewFont();

connect( &dictNetMgr, &QNetworkAccessManager::proxyAuthenticationRequired, this, &MainWindow::proxyAuthentication );
connect( GlobalNetworkAccessManager, &QNetworkAccessManager::proxyAuthenticationRequired, this, &MainWindow::proxyAuthentication );

connect( &articleNetMgr,
&QNetworkAccessManager::proxyAuthenticationRequired,
Expand Down Expand Up @@ -1518,7 +1512,7 @@ void MainWindow::makeDictionaries()
ftsIndexing.stopIndexing();
ftsIndexing.clearDictionaries();

loadDictionaries( this, isVisible(), cfg, dictionaries, dictNetMgr, false );
loadDictionaries( this, isVisible(), cfg, dictionaries, *GlobalNetworkAccessManager, false );

//create map
dictMap = Dictionary::dictToMap( dictionaries );
Expand Down Expand Up @@ -2090,7 +2084,7 @@ void MainWindow::editDictionaries( unsigned editDictionaryGroup )
{ // Limit existence of newCfg

Config::Class newCfg = cfg;
EditDictionaries dicts( this, newCfg, dictionaries, groupInstances, dictNetMgr );
EditDictionaries dicts( this, newCfg, dictionaries, groupInstances, *GlobalNetworkAccessManager );

connect( &dicts, &EditDictionaries::showDictionaryInfo, this, &MainWindow::showDictionaryInfo );

Expand Down Expand Up @@ -2850,7 +2844,7 @@ void MainWindow::checkNewRelease()
// github_release_api.setRawHeader( QByteArray( "Authorization" ), QByteArray( "" ) );
github_release_api.setRawHeader( QByteArray( "X-GitHub-Api-Version" ), QByteArray( "2022-11-28" ) );

auto * github_reply = dictNetMgr.get( github_release_api ); // will be marked as deleteLater when reply finished.
auto * github_reply = GlobalNetworkAccessManager->get( github_release_api ); // will be marked as deleteLater when reply finished.

QObject::connect( github_reply, &QNetworkReply::finished, [ github_reply, this ]() {
if ( github_reply->error() != QNetworkReply::NoError ) {
Expand Down Expand Up @@ -3277,7 +3271,7 @@ void MainWindow::on_rescanFiles_triggered()
dictionariesUnmuted.clear();
dictionaryBar.setDictionaries( dictionaries );

loadDictionaries( this, true, cfg, dictionaries, dictNetMgr );
loadDictionaries( this, true, cfg, dictionaries, *GlobalNetworkAccessManager );
dictMap = Dictionary::dictToMap( dictionaries );

for ( unsigned x = 0; x < dictionaries.size(); x++ ) {
Expand Down
4 changes: 1 addition & 3 deletions src/ui/mainwindow.hh
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,7 @@ private:
Instances::Groups groupInstances;
ArticleMaker articleMaker;
ArticleNetworkAccessManager articleNetMgr;
QNetworkAccessManager dictNetMgr; // We give dictionaries a separate manager,
// since their requests can be destroyed
// in a separate thread

AudioPlayerFactory audioPlayerFactory;

//current active translateLine;
Expand Down

0 comments on commit 9fa2e4b

Please sign in to comment.