Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Toss cookies and other browsing data for New Tor Identity. #2425

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 70 additions & 2 deletions browser/ui/browser_commands.cc
Original file line number Diff line number Diff line change
@@ -1,29 +1,97 @@
/* 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 <utility>

#include "brave/browser/tor/tor_profile_service.h"
#include "brave/browser/tor/tor_profile_service_factory.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/profiles/profile_manager.h"
#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 NewTorIdentityCallback(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);
}

// Flush everything else in the storage partition.
void NewTorIdentityCallbackClearData(WebContents* current_tab) {
auto* context = current_tab->GetBrowserContext();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure any of this is necessary because I believe we're already using a new partition/network context when cycling, but this not only has race conditions, it will also cause problems for other tabs because it deletes data across the entire browser context, not just the individual tab

auto* site = current_tab->GetSiteInstance();
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(
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(&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<bool(const GURL&)>(),
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<bool(const GURL&)>(),
std::move(callback));
}
}
} // namespace

namespace brave {
Expand Down