From 75412200db05ddc5ee2b9aea367b580d8b0c438e Mon Sep 17 00:00:00 2001 From: Tamas Zakor Date: Mon, 20 Apr 2020 11:41:29 +0200 Subject: Avoid the network context reset during http cache clear Reinstate BrowsingDataRemoverObserverQt() to check if http cache clearing is in-progress or done. ProfileIODataQt::resetNetworkContext() should not be called during http cache clearing because it causes an assert. Call it after the clearing is done. Change-Id: I6750341ff23f704ba547c913f40b0cec92b1cc43 Reviewed-by: Allan Sandfeld Jensen --- src/core/profile_adapter.cpp | 28 +++++++++++++++------------- src/core/profile_io_data_qt.cpp | 38 ++++++++++++++++++++++++++++++++++++++ src/core/profile_io_data_qt.h | 21 ++++++++++++++++++++- 3 files changed, 73 insertions(+), 14 deletions(-) diff --git a/src/core/profile_adapter.cpp b/src/core/profile_adapter.cpp index d2c4ab1d8..b87591c97 100644 --- a/src/core/profile_adapter.cpp +++ b/src/core/profile_adapter.cpp @@ -142,7 +142,8 @@ void ProfileAdapter::setStorageName(const QString &storageName) m_name = storageName; if (!m_offTheRecord) { m_profile->setupPrefService(); - m_profile->m_profileIOData->resetNetworkContext(); + if (!m_profile->m_profileIOData->isClearHttpCacheInProgress()) + m_profile->m_profileIOData->resetNetworkContext(); if (m_visitedLinksManager) resetVisitedLinksManager(); } @@ -154,7 +155,8 @@ void ProfileAdapter::setOffTheRecord(bool offTheRecord) return; m_offTheRecord = offTheRecord; m_profile->setupPrefService(); - m_profile->m_profileIOData->resetNetworkContext(); + if (!m_profile->m_profileIOData->isClearHttpCacheInProgress()) + m_profile->m_profileIOData->resetNetworkContext(); if (m_visitedLinksManager) resetVisitedLinksManager(); } @@ -259,7 +261,8 @@ void ProfileAdapter::setDataPath(const QString &path) m_dataPath = path; if (!m_offTheRecord) { m_profile->setupPrefService(); - m_profile->m_profileIOData->resetNetworkContext(); + if (!m_profile->m_profileIOData->isClearHttpCacheInProgress()) + m_profile->m_profileIOData->resetNetworkContext(); if (m_visitedLinksManager) resetVisitedLinksManager(); } @@ -286,7 +289,7 @@ void ProfileAdapter::setCachePath(const QString &path) if (m_cachePath == path) return; m_cachePath = path; - if (!m_offTheRecord) + if (!m_offTheRecord && !m_profile->m_profileIOData->isClearHttpCacheInProgress()) m_profile->m_profileIOData->resetNetworkContext(); } @@ -341,7 +344,7 @@ void ProfileAdapter::setHttpCacheType(ProfileAdapter::HttpCacheType newhttpCache m_httpCacheType = newhttpCacheType; if (oldCacheType == httpCacheType()) return; - if (!m_offTheRecord) { + if (!m_offTheRecord && !m_profile->m_profileIOData->isClearHttpCacheInProgress()) { m_profile->m_profileIOData->resetNetworkContext(); if (m_httpCacheType == NoCache) clearHttpCache(); @@ -361,7 +364,7 @@ void ProfileAdapter::setPersistentCookiesPolicy(ProfileAdapter::PersistentCookie m_persistentCookiesPolicy = newPersistentCookiesPolicy; if (oldPolicy == persistentCookiesPolicy()) return; - if (!m_offTheRecord) + if (!m_offTheRecord && !m_profile->m_profileIOData->isClearHttpCacheInProgress()) m_profile->m_profileIOData->resetNetworkContext(); } @@ -416,7 +419,7 @@ void ProfileAdapter::setHttpCacheMaxSize(int maxSize) if (m_httpCacheMaxSize == maxSize) return; m_httpCacheMaxSize = maxSize; - if (!m_offTheRecord) + if (!m_offTheRecord && !m_profile->m_profileIOData->isClearHttpCacheInProgress()) m_profile->m_profileIOData->resetNetworkContext(); } @@ -592,10 +595,7 @@ void ProfileAdapter::setHttpAcceptLanguage(const QString &httpAcceptLanguage) void ProfileAdapter::clearHttpCache() { - content::BrowsingDataRemover *remover = content::BrowserContext::GetBrowsingDataRemover(m_profile.data()); - remover->Remove(base::Time(), base::Time::Max(), - content::BrowsingDataRemover::DATA_TYPE_CACHE, - content::BrowsingDataRemover::ORIGIN_TYPE_UNPROTECTED_WEB | content::BrowsingDataRemover::ORIGIN_TYPE_PROTECTED_WEB); + m_profile->m_profileIOData->clearHttpCache(); } void ProfileAdapter::setSpellCheckLanguages(const QStringList &languages) @@ -656,7 +656,8 @@ void ProfileAdapter::setUseForGlobalCertificateVerification(bool enable) if (enable) { if (profileForglobalCertificateVerification) { profileForglobalCertificateVerification->m_usedForGlobalCertificateVerification = false; - profileForglobalCertificateVerification->m_profile->m_profileIOData->resetNetworkContext(); + if (!m_profile->m_profileIOData->isClearHttpCacheInProgress()) + profileForglobalCertificateVerification->m_profile->m_profileIOData->resetNetworkContext(); for (auto *client : qAsConst(profileForglobalCertificateVerification->m_clients)) client->useForGlobalCertificateVerificationChanged(); } @@ -667,7 +668,8 @@ void ProfileAdapter::setUseForGlobalCertificateVerification(bool enable) profileForglobalCertificateVerification = nullptr; } - m_profile->m_profileIOData->resetNetworkContext(); + if (!m_profile->m_profileIOData->isClearHttpCacheInProgress()) + m_profile->m_profileIOData->resetNetworkContext(); } bool ProfileAdapter::isUsedForGlobalCertificateVerification() const diff --git a/src/core/profile_io_data_qt.cpp b/src/core/profile_io_data_qt.cpp index 64ad096a6..ecebbdaa7 100644 --- a/src/core/profile_io_data_qt.cpp +++ b/src/core/profile_io_data_qt.cpp @@ -43,6 +43,7 @@ #include "content/browser/storage_partition_impl.h" #include "content/public/browser/browser_task_traits.h" #include "content/public/browser/browser_thread.h" +#include "content/public/browser/browsing_data_remover.h" #include "content/public/browser/shared_cors_origin_access_list.h" #include "content/public/common/content_features.h" #include "net/ssl/ssl_config_service_defaults.h" @@ -66,6 +67,7 @@ ProfileIODataQt::ProfileIODataQt(ProfileQt *profile) #if QT_CONFIG(ssl) m_clientCertificateStoreData(new ClientCertificateStoreData), #endif + m_removerObserver(this), m_weakPtrFactory(this) { if (content::BrowserThread::IsThreadInitialized(content::BrowserThread::UI)) @@ -130,6 +132,42 @@ void ProfileIODataQt::initializeOnUIThread() m_proxyConfigMonitor.reset(new ProxyConfigMonitor(m_profile->GetPrefs())); } +void ProfileIODataQt::clearHttpCache() +{ + Q_ASSERT(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); + if (!m_clearHttpCacheInProgress) { + m_clearHttpCacheInProgress = true; + content::BrowsingDataRemover *remover = + content::BrowserContext::GetBrowsingDataRemover(m_profileAdapter->profile()); + remover->AddObserver(&m_removerObserver); + remover->RemoveAndReply(base::Time(), base::Time::Max(), + content::BrowsingDataRemover::DATA_TYPE_CACHE, + content::BrowsingDataRemover::ORIGIN_TYPE_UNPROTECTED_WEB | + content::BrowsingDataRemover::ORIGIN_TYPE_PROTECTED_WEB, + &m_removerObserver); + } +} + +void ProfileIODataQt::removeBrowsingDataRemoverObserver() +{ + content::BrowsingDataRemover *remover = + content::BrowserContext::GetBrowsingDataRemover(m_profileAdapter->profile()); + remover->RemoveObserver(&m_removerObserver); +} + +BrowsingDataRemoverObserverQt::BrowsingDataRemoverObserverQt(ProfileIODataQt *profileIOData) + : m_profileIOData(profileIOData) +{ +} + +void BrowsingDataRemoverObserverQt::OnBrowsingDataRemoverDone() +{ + Q_ASSERT(m_profileIOData->m_clearHttpCacheInProgress); + m_profileIOData->removeBrowsingDataRemoverObserver(); + m_profileIOData->m_clearHttpCacheInProgress = false; + m_profileIOData->resetNetworkContext(); +} + void ProfileIODataQt::setFullConfiguration() { Q_ASSERT(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); diff --git a/src/core/profile_io_data_qt.h b/src/core/profile_io_data_qt.h index 26ae1bcb2..b0567dead 100644 --- a/src/core/profile_io_data_qt.h +++ b/src/core/profile_io_data_qt.h @@ -40,6 +40,7 @@ #ifndef PROFILE_IO_DATA_QT_H #define PROFILE_IO_DATA_QT_H +#include "content/public/browser/browsing_data_remover.h" #include "chrome/browser/profiles/profile.h" #include "extensions/buildflags/buildflags.h" @@ -65,6 +66,16 @@ struct ClientCertificateStoreData; class ProfileIODataQt; class ProfileQt; +class BrowsingDataRemoverObserverQt : public content::BrowsingDataRemover::Observer { +public: + BrowsingDataRemoverObserverQt(ProfileIODataQt *profileIOData); + + void OnBrowsingDataRemoverDone() override; + +private: + ProfileIODataQt *m_profileIOData; +}; + // ProfileIOData contains data that lives on the IOthread // we still use shared memebers and use mutex which breaks // idea for this object, but this is wip. @@ -89,6 +100,8 @@ public: // Used in NetworkDelegateQt::OnBeforeURLRequest. void setFullConfiguration(); // runs on ui thread void resetNetworkContext(); // runs on ui thread + void clearHttpCache(); // runs on ui thread + bool isClearHttpCacheInProgress() { return m_clearHttpCacheInProgress; } network::mojom::NetworkContextParamsPtr CreateNetworkContextParams(); @@ -104,6 +117,8 @@ public: CookieMonsterDelegateQt *cookieDelegate() const { return m_cookieDelegate.get(); } private: + void removeBrowsingDataRemoverObserver(); + ProfileQt *m_profile; std::unique_ptr m_resourceContext; scoped_refptr m_cookieDelegate; @@ -126,9 +141,13 @@ private: #endif int m_httpCacheMaxSize = 0; bool m_useForGlobalCertificateVerification = false; - base::WeakPtrFactory m_weakPtrFactory; // this should be always the last member + BrowsingDataRemoverObserverQt m_removerObserver; QString m_dataPath; + bool m_clearHttpCacheInProgress = false; + base::WeakPtrFactory m_weakPtrFactory; // this should be always the last member DISALLOW_COPY_AND_ASSIGN(ProfileIODataQt); + + friend class BrowsingDataRemoverObserverQt; }; } // namespace QtWebEngineCore -- cgit v1.2.3