From f3307f3c4a2eeac89599ba1a0ec666ba34ed7019 Mon Sep 17 00:00:00 2001 From: Taylor `Riastradh' Campbell Date: Sat, 11 May 2019 19:14:10 +0000 Subject: [PATCH] Flush HTTP, media, code, and renderer caches too. --- browser/ui/browser_commands.cc | 60 +++++++++++++++++++++++++++++----- 1 file changed, 52 insertions(+), 8 deletions(-) diff --git a/browser/ui/browser_commands.cc b/browser/ui/browser_commands.cc index be782ac706bd..67f8a94fbb73 100644 --- a/browser/ui/browser_commands.cc +++ b/browser/ui/browser_commands.cc @@ -14,39 +14,83 @@ #include "chrome/browser/profiles/profile_window.h" #include "chrome/browser/ui/browser.h" #include "chrome/browser/ui/tabs/tab_strip_model.h" +#include "components/web_cache/browser/web_cache_manager.h" #include "content/public/browser/browser_thread.h" #include "content/public/browser/navigation_controller.h" +#include "content/public/browser/render_process_host.h" #include "content/public/browser/storage_partition.h" #include "content/public/browser/web_contents.h" +#include "services/network/public/cpp/features.h" +using content::BrowserContext; using content::BrowserThread; using content::NavigationController; +using content::StoragePartition; using content::WebContents; namespace { -void NewTorIdentityCallbackDone(WebContents* current_tab) { +// Reload the page once we have a new circuit and cleared browsing data. +void NewTorIdentityCallbackReload(WebContents* current_tab) { NavigationController& controller = current_tab->GetController(); controller.Reload(content::ReloadType::BYPASSING_CACHE, true); } -void NewTorIdentityCallback(WebContents* current_tab) { +// Flush everything else in the storage partition. +void NewTorIdentityCallbackClearData(WebContents* current_tab) { auto* context = current_tab->GetBrowserContext(); auto* site = current_tab->GetSiteInstance(); - auto* partition = - content::BrowserContext::GetStoragePartition(context, site); + auto* partition = BrowserContext::GetStoragePartition(context, site); auto cookie_delete_filter = network::mojom::CookieDeletionFilter::New(); // Delete all cookies, irrespective of persistence status. cookie_delete_filter->session_control = network::mojom::CookieDeletionSessionControl::IGNORE_CONTROL; partition->ClearData( - content::StoragePartition::REMOVE_DATA_MASK_ALL, - content::StoragePartition::QUOTA_MANAGED_STORAGE_MASK_ALL, - content::StoragePartition::OriginMatcherFunction(), + StoragePartition::REMOVE_DATA_MASK_ALL, + StoragePartition::QUOTA_MANAGED_STORAGE_MASK_ALL, + StoragePartition::OriginMatcherFunction(), std::move(cookie_delete_filter), /*perform_cleanup*/ true, /*begin*/ base::Time(), // beginning of time /*end*/ base::Time::Max(), // end of time - base::Bind(&NewTorIdentityCallbackDone, current_tab)); + base::Bind(&NewTorIdentityCallbackReload, current_tab)); +} + +// Flush any code caches in the storage partition. +void NewTorIdentityCallbackClearCodeCaches(WebContents* current_tab) { + auto* context = current_tab->GetBrowserContext(); + auto* site = current_tab->GetSiteInstance(); + auto* partition = BrowserContext::GetStoragePartition(context, site); + partition->ClearCodeCaches( + /*begin*/ base::Time(), // beginning of time + /*end*/ base::Time::Max(), // end of time + base::RepeatingCallback(), + base::BindOnce(&NewTorIdentityCallbackClearData, current_tab)); +} + +// Flush the renderer cache and the HTTP and media caches. +void NewTorIdentityCallback(WebContents* current_tab) { + int render_process_id = current_tab->GetMainFrame()->GetProcess()->GetID(); + auto* cache_manager = web_cache::WebCacheManager::GetInstance(); + cache_manager->ClearCacheForProcess(render_process_id); + + auto* context = current_tab->GetBrowserContext(); + auto* site = current_tab->GetSiteInstance(); + auto* partition = BrowserContext::GetStoragePartition(context, site); + base::OnceClosure callback = base::BindOnce( + &NewTorIdentityCallbackClearCodeCaches, current_tab); + if (base::FeatureList::IsEnabled(network::features::kNetworkService)) { + partition->GetNetworkContext()->ClearHttpCache( + /*begin*/ base::Time(), // beginning of time + /*end*/ base::Time::Max(), // end of time + /*ClearDataFilter*/ nullptr, + std::move(callback)); + } else { + partition->ClearHttpAndMediaCaches( + /*begin*/ base::Time(), // beginning of time + /*end*/ base::Time::Max(), // end of time + base::RepeatingCallback(), + std::move(callback)); + } } } // namespace