fixed deleting QTimer on wrong thread

This commit is contained in:
fourtf 2019-08-20 23:30:39 +02:00
parent 14222f84f2
commit 7643c0d20d
16 changed files with 64 additions and 59 deletions

View file

@ -16,12 +16,17 @@
namespace chatterino {
NetworkData::NetworkData()
: timer_(new QTimer())
{
timer_->setSingleShot(true);
DebugCount::increase("NetworkData");
}
NetworkData::~NetworkData()
{
this->timer_->deleteLater();
DebugCount::decrease("NetworkData");
}
@ -75,8 +80,8 @@ void loadUncached(const std::shared_ptr<NetworkData> &data)
if (data->hasTimeout_)
{
data->timer_.setSingleShot(true);
data->timer_.start();
data->timer_->setSingleShot(true);
data->timer_->start();
}
auto onUrlRequested = [data, worker]() mutable {
@ -106,9 +111,9 @@ void loadUncached(const std::shared_ptr<NetworkData> &data)
return;
}
if (data->timer_.isActive())
if (data->timer_->isActive())
{
QObject::connect(&data->timer_, &QTimer::timeout, worker,
QObject::connect(data->timer_, &QTimer::timeout, worker,
[reply, data]() {
log("Aborted!");
reply->abort();
@ -228,7 +233,7 @@ void loadCached(const std::shared_ptr<NetworkData> &data)
});
}
}
} // namespace chatterino
}
}
void load(const std::shared_ptr<NetworkData> &data)

View file

@ -51,7 +51,7 @@ struct NetworkData {
// to enable the timer, the "setTimeout" function needs to be called before
// execute is called
bool hasTimeout_{};
QTimer timer_;
QTimer *timer_;
QString getHash();

View file

@ -50,11 +50,14 @@ NetworkRequest NetworkRequest::type(NetworkRequestType newRequestType) &&
NetworkRequest NetworkRequest::caller(const QObject *caller) &&
{
// Caller must be in gui thread
assert(caller->thread() == qApp->thread());
if (caller)
{
// Caller must be in gui thread
assert(caller->thread() == qApp->thread());
this->data->caller_ = const_cast<QObject *>(caller);
this->data->hasCaller_ = true;
this->data->caller_ = const_cast<QObject *>(caller);
this->data->hasCaller_ = true;
}
return std::move(*this);
}
@ -100,7 +103,7 @@ NetworkRequest NetworkRequest::header(const char *headerName,
NetworkRequest NetworkRequest::timeout(int ms) &&
{
this->data->hasTimeout_ = true;
this->data->timer_.setInterval(ms);
this->data->timer_->setInterval(ms);
return std::move(*this);
}

View file

@ -156,7 +156,6 @@ void NotificationController::getFakeTwitchChannelLiveStatus(
QString url("https://api.twitch.tv/kraken/streams/" + roomID);
NetworkRequest::twitchRequest(url)
.caller(QThread::currentThread())
.onSuccess([this, channelName](auto result) -> Outcome {
rapidjson::Document document = result.parseRapidJson();
if (!document.IsObject())

View file

@ -387,26 +387,27 @@ void MessageBuilder::addLink(const QString &origLink,
textColor)
->setLink(linkElement);
LinkResolver::getLinkInfo(matchedLink, [weakMessage = this->weakOf(),
linkMELowercase, linkMEOriginal,
matchedLink](QString tooltipText,
Link originalLink) {
auto shared = weakMessage.lock();
if (!shared)
{
return;
}
if (!tooltipText.isEmpty())
{
linkMELowercase->setTooltip(tooltipText);
linkMEOriginal->setTooltip(tooltipText);
}
if (originalLink.value != matchedLink && !originalLink.value.isEmpty())
{
linkMELowercase->setLink(originalLink)->updateLink();
linkMEOriginal->setLink(originalLink)->updateLink();
}
});
LinkResolver::getLinkInfo(
matchedLink, nullptr,
[weakMessage = this->weakOf(), linkMELowercase, linkMEOriginal,
matchedLink](QString tooltipText, Link originalLink) {
auto shared = weakMessage.lock();
if (!shared)
{
return;
}
if (!tooltipText.isEmpty())
{
linkMELowercase->setTooltip(tooltipText);
linkMEOriginal->setTooltip(tooltipText);
}
if (originalLink.value != matchedLink &&
!originalLink.value.isEmpty())
{
linkMELowercase->setLink(originalLink)->updateLink();
linkMEOriginal->setLink(originalLink)->updateLink();
}
});
}
} // namespace chatterino

View file

@ -11,7 +11,8 @@
namespace chatterino {
void LinkResolver::getLinkInfo(
const QString url, std::function<void(QString, Link)> successCallback)
const QString url, QObject *caller,
std::function<void(QString, Link)> successCallback)
{
if (!getSettings()->linkInfoTooltip)
{
@ -22,7 +23,7 @@ void LinkResolver::getLinkInfo(
// QTimer::singleShot(3000, [=]() {
NetworkRequest(Env::get().linkResolverUrl.arg(QString::fromUtf8(
QUrl::toPercentEncoding(url, "", "/:"))))
.caller(QThread::currentThread())
.caller(caller)
.timeout(30000)
.onSuccess([successCallback, url](auto result) mutable -> Outcome {
auto root = result.parseJson();

View file

@ -10,10 +10,8 @@ namespace chatterino {
class LinkResolver
{
public:
static void getLinkInfo(const QString url,
static void getLinkInfo(const QString url, QObject *caller,
std::function<void(QString, Link)> callback);
private:
};
} // namespace chatterino

View file

@ -113,7 +113,6 @@ boost::optional<EmotePtr> BttvEmotes::emote(const EmoteName &name) const
void BttvEmotes::loadEmotes()
{
NetworkRequest(QString(globalEmoteApiUrl))
.caller(QThread::currentThread())
.timeout(30000)
.onSuccess([this](auto result) -> Outcome {
auto emotes = this->global_.get();
@ -130,7 +129,6 @@ void BttvEmotes::loadChannel(const QString &channelName,
std::function<void(EmoteMap &&)> callback)
{
NetworkRequest(QString(bttvChannelEmoteApiUrl) + channelName)
.caller(QThread::currentThread())
.timeout(3000)
.onSuccess([callback = std::move(callback)](auto result) -> Outcome {
auto pair = parseChannelEmotes(result.parseJson());

View file

@ -37,7 +37,7 @@ void ChatterinoBadges::loadChatterinoBadges()
static QUrl url("https://fourtf.com/chatterino/badges.json");
NetworkRequest(url)
.caller(QThread::currentThread())
.onSuccess([this](auto result) -> Outcome {
auto jsonRoot = result.parseJson();
int index = 0;

View file

@ -138,7 +138,7 @@ void FfzEmotes::loadEmotes()
QString url("https://api.frankerfacez.com/v1/set/global");
NetworkRequest(url)
.caller(QThread::currentThread())
.timeout(30000)
.onSuccess([this](auto result) -> Outcome {
auto emotes = this->emotes();
@ -157,7 +157,7 @@ void FfzEmotes::loadChannel(const QString &channelName,
log("[FFZEmotes] Reload FFZ Channel Emotes for channel {}\n", channelName);
NetworkRequest("https://api.frankerfacez.com/v1/room/" + channelName)
.caller(QThread::currentThread())
.timeout(20000)
.onSuccess([callback = std::move(callback)](auto result) -> Outcome {
auto pair = parseChannelEmotes(result.parseJson());

View file

@ -23,7 +23,7 @@ void FfzModBadge::loadCustomModBadge()
QString url = partialUrl + channelName_ + "/1";
NetworkRequest(url)
.caller(QThread::currentThread())
.onSuccess([this, url](auto result) -> Outcome {
auto data = result.getData();

View file

@ -95,7 +95,7 @@ void TwitchAccount::loadIgnores()
"/blocks");
NetworkRequest(url)
.caller(QThread::currentThread())
.authorizeTwitchV5(this->getOAuthClient(), this->getOAuthToken())
.onSuccess([=](auto result) -> Outcome {
auto document = result.parseRapidJson();
@ -168,7 +168,7 @@ void TwitchAccount::ignoreByID(
"/blocks/" + targetUserID);
NetworkRequest(url, NetworkRequestType::Put)
.caller(QThread::currentThread())
.authorizeTwitchV5(this->getOAuthClient(), this->getOAuthToken())
.onError([=](int errorCode) {
onFinished(IgnoreResult_Failed,
@ -245,7 +245,7 @@ void TwitchAccount::unignoreByID(
"/blocks/" + targetUserID);
NetworkRequest(url, NetworkRequestType::Delete)
.caller(QThread::currentThread())
.authorizeTwitchV5(this->getOAuthClient(), this->getOAuthToken())
.onError([=](int errorCode) {
onFinished(
@ -279,7 +279,7 @@ void TwitchAccount::checkFollow(const QString targetUserID,
"/follows/channels/" + targetUserID);
NetworkRequest(url)
.caller(QThread::currentThread())
.authorizeTwitchV5(this->getOAuthClient(), this->getOAuthToken())
.onError([=](int errorCode) {
if (errorCode == 203)
@ -308,7 +308,7 @@ void TwitchAccount::followUser(const QString userID,
"/follows/channels/" + userID);
NetworkRequest(requestUrl, NetworkRequestType::Put)
.caller(QThread::currentThread())
.authorizeTwitchV5(this->getOAuthClient(), this->getOAuthToken())
.onSuccess([successCallback](auto result) -> Outcome {
// TODO: Properly check result of follow request
@ -326,7 +326,7 @@ void TwitchAccount::unfollowUser(const QString userID,
"/follows/channels/" + userID);
NetworkRequest(requestUrl, NetworkRequestType::Delete)
.caller(QThread::currentThread())
.authorizeTwitchV5(this->getOAuthClient(), this->getOAuthToken())
.onError([successCallback](int code) {
if (code >= 200 && code <= 299)
@ -368,7 +368,7 @@ void TwitchAccount::loadEmotes()
"/emotes");
NetworkRequest(url)
.caller(QThread::currentThread())
.authorizeTwitchV5(this->getOAuthClient(), this->getOAuthToken())
.onError([=](int errorCode) {
log("[TwitchAccount::loadEmotes] Error {}", errorCode);
@ -409,7 +409,7 @@ void TwitchAccount::autoModAllow(const QString msgID)
.header("Content-Type", "application/json")
.header("Content-Length", QByteArray::number(qba.size()))
.payload(qba)
.caller(QThread::currentThread())
.authorizeTwitchV5(this->getOAuthClient(), this->getOAuthToken())
.onError([=](int errorCode) {
log("[TwitchAccounts::autoModAllow] Error {}", errorCode);
@ -429,7 +429,7 @@ void TwitchAccount::autoModDeny(const QString msgID)
.header("Content-Type", "application/json")
.header("Content-Length", QByteArray::number(qba.size()))
.payload(qba)
.caller(QThread::currentThread())
.authorizeTwitchV5(this->getOAuthClient(), this->getOAuthToken())
.onError([=](int errorCode) {
log("[TwitchAccounts::autoModDeny] Error {}", errorCode);

View file

@ -16,7 +16,7 @@ void TwitchApi::findUserId(const QString user,
QString requestUrl("https://api.twitch.tv/kraken/users?login=" + user);
NetworkRequest(requestUrl)
.caller(QThread::currentThread())
.authorizeTwitchV5(getDefaultClientID())
.timeout(30000)
.onSuccess([successCallback](auto result) mutable -> Outcome {
@ -65,7 +65,7 @@ void TwitchApi::findUserName(const QString userid,
QString requestUrl("https://api.twitch.tv/kraken/users/" + userid);
NetworkRequest(requestUrl)
.caller(QThread::currentThread())
.authorizeTwitchV5(getDefaultClientID())
.timeout(30000)
.onSuccess([successCallback](auto result) mutable -> Outcome {

View file

@ -18,7 +18,7 @@ void TwitchBadges::loadTwitchBadges()
"https://badges.twitch.tv/v1/badges/global/display?language=en");
NetworkRequest(url)
.caller(QThread::currentThread())
.onSuccess([this](auto result) -> Outcome {
auto root = result.parseJson();
auto badgeSets = this->badgeSets_.access();

View file

@ -502,7 +502,7 @@ void TwitchChannel::refreshLiveStatus()
// auto request = makeGetStreamRequest(roomID, QThread::currentThread());
NetworkRequest::twitchRequest(url)
.caller(QThread::currentThread())
.onSuccess(
[this, weak = weakOf<Channel>(this)](auto result) -> Outcome {
ChannelPtr shared = weak.lock();
@ -670,7 +670,7 @@ void TwitchChannel::refreshChatters()
// get viewer list
NetworkRequest("https://tmi.twitch.tv/group/user/" + this->getName() +
"/chatters")
.caller(QThread::currentThread())
.onSuccess(
[this, weak = weakOf<Channel>(this)](auto result) -> Outcome {
// channel still exists?
@ -694,7 +694,7 @@ void TwitchChannel::refreshBadges()
auto url = Url{"https://badges.twitch.tv/v1/badges/channels/" +
this->roomId() + "/display?language=en"};
NetworkRequest(url.string)
.caller(QThread::currentThread())
.onSuccess([this,
weak = weakOf<Channel>(this)](auto result) -> Outcome {
auto shared = weak.lock();

View file

@ -214,7 +214,7 @@ void Toasts::fetchChannelAvatar(const QString channelName,
channelName);
NetworkRequest(requestUrl)
.caller(QThread::currentThread())
.authorizeTwitchV5(getDefaultClientID())
.timeout(30000)
.onSuccess([successCallback](auto result) mutable -> Outcome {