From b7889d94dd97407cc361110e0466cc43ff07db95 Mon Sep 17 00:00:00 2001 From: Taylor `Riastradh' Campbell Date: Sat, 11 May 2019 13:43:48 +0000 Subject: [PATCH 1/2] WIP: Toss cookies and other browsing data for New Tor Identity. fix brave/brave-browser#3872 --- browser/ui/browser_commands.cc | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/browser/ui/browser_commands.cc b/browser/ui/browser_commands.cc index 79b2bd4c8a97..be782ac706bd 100644 --- a/browser/ui/browser_commands.cc +++ b/browser/ui/browser_commands.cc @@ -1,9 +1,12 @@ -/* This Source Code Form is subject to the terms of the Mozilla Public +/* Copyright (c) 2019 The Brave Authors. All rights reserved. + * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this file, * You can obtain one at http://mozilla.org/MPL/2.0/. */ #include "brave/browser/ui/browser_commands.h" +#include + #include "brave/browser/tor/tor_profile_service.h" #include "brave/browser/tor/tor_profile_service_factory.h" #include "chrome/browser/profiles/profile.h" @@ -13,6 +16,7 @@ #include "chrome/browser/ui/tabs/tab_strip_model.h" #include "content/public/browser/browser_thread.h" #include "content/public/browser/navigation_controller.h" +#include "content/public/browser/storage_partition.h" #include "content/public/browser/web_contents.h" using content::BrowserThread; @@ -20,10 +24,30 @@ using content::NavigationController; using content::WebContents; namespace { -void NewTorIdentityCallback(WebContents* current_tab) { +void NewTorIdentityCallbackDone(WebContents* current_tab) { NavigationController& controller = current_tab->GetController(); controller.Reload(content::ReloadType::BYPASSING_CACHE, true); } + +void NewTorIdentityCallback(WebContents* current_tab) { + auto* context = current_tab->GetBrowserContext(); + auto* site = current_tab->GetSiteInstance(); + auto* partition = + content::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(), + 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)); +} } // namespace namespace brave { From 810612cc6b8e7b753b7bffff059df83a01b691fc Mon Sep 17 00:00:00 2001 From: Taylor `Riastradh' Campbell Date: Sat, 11 May 2019 19:14:10 +0000 Subject: [PATCH 2/2] 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