1
mirror of https://invent.kde.org/network/falkon.git synced 2024-12-20 10:46:35 +01:00

[AdBlockSubscription] Fix infinite loading of subscription when cannot write to file

This commit is contained in:
David Rosca 2014-04-16 17:11:25 +02:00
parent cc479f29e1
commit 9df585a8f8
2 changed files with 25 additions and 17 deletions

View File

@ -157,34 +157,40 @@ void AdBlockSubscription::subscriptionDownloaded()
return; return;
} }
QByteArray response = QString::fromUtf8(m_reply->readAll()).toUtf8(); bool error = false;
const QByteArray response = QString::fromUtf8(m_reply->readAll()).toUtf8();
if (m_reply->error() == QNetworkReply::NoError && response.startsWith("[Adblock")) { if (m_reply->error() != QNetworkReply::NoError ||
// Prepend subscription info !response.startsWith(QByteArray("[Adblock")) ||
response.prepend(QString("Title: %1\nUrl: %2\n").arg(title(), url().toString()).toUtf8()); !saveDownloadedData(response)
) {
saveDownloadedData(response); error = true;
loadSubscription(AdBlockManager::instance()->disabledRules());
emit subscriptionUpdated();
}
else {
emit subscriptionError(tr("Cannot load subscription!"));
} }
m_reply->deleteLater(); m_reply->deleteLater();
m_reply = 0; m_reply = 0;
if (error) {
emit subscriptionError(tr("Cannot load subscription!"));
return;
}
loadSubscription(AdBlockManager::instance()->disabledRules());
emit subscriptionUpdated();
} }
void AdBlockSubscription::saveDownloadedData(const QByteArray &data) bool AdBlockSubscription::saveDownloadedData(const QByteArray &data)
{ {
QFile file(m_filePath); QFile file(m_filePath);
if (!file.open(QFile::ReadWrite | QFile::Truncate)) { if (!file.open(QFile::ReadWrite | QFile::Truncate)) {
qWarning() << "AdBlockSubscription::" << __FUNCTION__ << "Unable to open adblock file for writing:" << m_filePath; qWarning() << "AdBlockSubscription::" << __FUNCTION__ << "Unable to open adblock file for writing:" << m_filePath;
return; return false;
} }
// Write subscription header
file.write(QString("Title: %1\nUrl: %2\n").arg(title(), url().toString()).toUtf8());
if (AdBlockManager::instance()->useLimitedEasyList() && m_url == QUrl(ADBLOCK_EASYLIST_URL)) { if (AdBlockManager::instance()->useLimitedEasyList() && m_url == QUrl(ADBLOCK_EASYLIST_URL)) {
// Third-party advertisers rules are with start domain (||) placeholder which needs regexps // Third-party advertisers rules are with start domain (||) placeholder which needs regexps
// So we are ignoring it for keeping good performance // So we are ignoring it for keeping good performance
@ -193,13 +199,15 @@ void AdBlockSubscription::saveDownloadedData(const QByteArray &data)
QByteArray part1 = data.left(data.indexOf(QLatin1String("!-----------------------------Third-party adverts-----------------------------!"))); QByteArray part1 = data.left(data.indexOf(QLatin1String("!-----------------------------Third-party adverts-----------------------------!")));
QByteArray part2 = data.mid(data.indexOf(QLatin1String("!---------------------------------Whitelists----------------------------------!"))); QByteArray part2 = data.mid(data.indexOf(QLatin1String("!---------------------------------Whitelists----------------------------------!")));
file.write(part1 + part2); file.write(part1);
file.write(part2);
file.close(); file.close();
return; return true;
} }
file.write(data); file.write(data);
file.close(); file.close();
return true;
} }
const AdBlockRule* AdBlockSubscription::match(const QNetworkRequest &request, const QString &urlDomain, const QString &urlString) const const AdBlockRule* AdBlockSubscription::match(const QNetworkRequest &request, const QString &urlDomain, const QString &urlString) const

View File

@ -109,7 +109,7 @@ protected slots:
void subscriptionDownloaded(); void subscriptionDownloaded();
protected: protected:
virtual void saveDownloadedData(const QByteArray &data); virtual bool saveDownloadedData(const QByteArray &data);
void populateCache(); void populateCache();