From 0d78434a6af06b611ad7ab8552c5cb38afe70ec8 Mon Sep 17 00:00:00 2001 From: Jocelyn Liu Date: Tue, 12 Jan 2021 16:16:20 -0800 Subject: [PATCH] Clear IPFS cache when clearing browsing data --- browser/browsing_data/BUILD.gn | 6 ++ .../brave_browsing_data_remover_delegate.cc | 84 +++++++++++++++++++ .../brave_browsing_data_remover_delegate.h | 9 ++ .../base/threading/thread_restrictions.h | 17 ++++ .../chrome_browsing_data_remover_delegate.h | 17 ++++ components/ipfs/ipfs_service.h | 2 +- ...base-threading-thread_restrictions.h.patch | 12 +++ ...ome_browsing_data_remover_delegate.h.patch | 12 +++ 8 files changed, 158 insertions(+), 1 deletion(-) create mode 100644 chromium_src/base/threading/thread_restrictions.h create mode 100644 chromium_src/chrome/browser/browsing_data/chrome_browsing_data_remover_delegate.h create mode 100644 patches/base-threading-thread_restrictions.h.patch create mode 100644 patches/chrome-browser-browsing_data-chrome_browsing_data_remover_delegate.h.patch diff --git a/browser/browsing_data/BUILD.gn b/browser/browsing_data/BUILD.gn index f9ca6eeaa4c4..14b2d2b90c62 100644 --- a/browser/browsing_data/BUILD.gn +++ b/browser/browsing_data/BUILD.gn @@ -1,3 +1,4 @@ +import("//brave/components/ipfs/buildflags/buildflags.gni") import("//extensions/buildflags/buildflags.gni") source_set("browsing_data") { @@ -12,6 +13,7 @@ source_set("browsing_data") { deps = [ "//base", + "//brave/components/ipfs/buildflags", "//chrome/common", "//components/browsing_data/core", "//components/content_settings/core/browser", @@ -26,4 +28,8 @@ source_set("browsing_data") { "//extensions/browser", ] } + + if (ipfs_enabled) { + deps += [ "//brave/components/ipfs" ] + } } diff --git a/browser/browsing_data/brave_browsing_data_remover_delegate.cc b/browser/browsing_data/brave_browsing_data_remover_delegate.cc index 6967b996b230..aa3be6f7f750 100644 --- a/browser/browsing_data/brave_browsing_data_remover_delegate.cc +++ b/browser/browsing_data/brave_browsing_data_remover_delegate.cc @@ -20,6 +20,19 @@ #include "extensions/browser/event_router.h" #endif +#if BUILDFLAG(IPFS_ENABLED) +#include "base/command_line.h" +#include "base/files/file_path.h" +#include "base/process/launch.h" +#include "base/process/process.h" +#include "base/task/task_traits.h" +#include "base/task/thread_pool.h" +#include "base/threading/thread_restrictions.h" +#include "base/time/time.h" +#include "brave/browser/ipfs/ipfs_service_factory.h" +#include "brave/components/ipfs/ipfs_service.h" +#endif + BraveBrowsingDataRemoverDelegate::BraveBrowsingDataRemoverDelegate( content::BrowserContext* browser_context) : ChromeBrowsingDataRemoverDelegate(browser_context), @@ -46,6 +59,12 @@ void BraveBrowsingDataRemoverDelegate::RemoveEmbedderData( // shields settings with non-empty resource ids. if (remove_mask & DATA_TYPE_CONTENT_SETTINGS) ClearShieldsSettings(delete_begin, delete_end); + +#if BUILDFLAG(IPFS_ENABLED) + if (remove_mask & content::BrowsingDataRemover::DATA_TYPE_CACHE) + ClearIPFSCache(); +#endif + #if BUILDFLAG(ENABLE_EXTENSIONS) if (remove_mask & DATA_TYPE_HISTORY) { auto* event_router = extensions::EventRouter::Get(profile_); @@ -91,3 +110,68 @@ void BraveBrowsingDataRemoverDelegate::ClearShieldsSettings( } } } + +#if BUILDFLAG(IPFS_ENABLED) +void BraveBrowsingDataRemoverDelegate::WaitForIPFSRepoGC( + base::Process process) { + bool exited = false; + + { + base::ScopedAllowBaseSyncPrimitives scoped_allow_base_sync_primitives; + + // Because we set maximum IPFS storage size as 1GB in Brave, ipfs repo gc + // command should be finished in just a few seconds and we do not expect + // this child process would hang forever. To be safe, we will wait for 30 + // seconds max here. + exited = process.WaitForExitWithTimeout(base::TimeDelta::FromSeconds(30), + nullptr); + } + + if (!exited) + process.Terminate(0, false /* wait */); +} + +// Run ipfs repo gc command to clear IPFS cache when IPFS executable path is +// available. Because the command does not support time ranged cleanup, we will +// always clear the whole cache expect for pinned files when clearing browsing +// data. +void BraveBrowsingDataRemoverDelegate::ClearIPFSCache() { + auto* service = + ipfs::IpfsServiceFactory::GetInstance()->GetForContext(profile_); + if (!service) + return; + + base::FilePath path = service->GetIpfsExecutablePath(); + if (path.empty()) + return; + + base::CommandLine cmdline(path); + cmdline.AppendArg("repo"); + cmdline.AppendArg("gc"); + + base::FilePath data_path = service->GetDataPath(); + base::LaunchOptions options; +#if defined(OS_WIN) + options.environment[L"IPFS_PATH"] = data_path.value(); +#else + options.environment["IPFS_PATH"] = data_path.value(); +#endif +#if defined(OS_LINUX) + options.kill_on_parent_death = true; +#endif +#if defined(OS_WIN) + options.start_hidden = true; +#endif + + base::Process process = base::LaunchProcess(cmdline, options); + if (!process.IsValid()) { + return; + } + + base::ThreadPool::PostTaskAndReply( + FROM_HERE, {base::TaskPriority::USER_VISIBLE, base::MayBlock()}, + base::BindOnce(&BraveBrowsingDataRemoverDelegate::WaitForIPFSRepoGC, + base::Unretained(this), base::Passed(&process)), + CreateTaskCompletionClosure(TracingDataType::kHostCache)); +} +#endif // BUILDFLAG(IPFS_ENABLED) diff --git a/browser/browsing_data/brave_browsing_data_remover_delegate.h b/browser/browsing_data/brave_browsing_data_remover_delegate.h index 6975eecfd8e3..ad822e13690a 100644 --- a/browser/browsing_data/brave_browsing_data_remover_delegate.h +++ b/browser/browsing_data/brave_browsing_data_remover_delegate.h @@ -7,8 +7,13 @@ #define BRAVE_BROWSER_BROWSING_DATA_BRAVE_BROWSING_DATA_REMOVER_DELEGATE_H_ #include "base/time/time.h" +#include "brave/components/ipfs/buildflags/buildflags.h" #include "chrome/browser/browsing_data/chrome_browsing_data_remover_delegate.h" +namespace base { +class Process; +} // namespace base + namespace content_settings { class BravePrefProvider; } // namespace content_settings @@ -41,6 +46,10 @@ class BraveBrowsingDataRemoverDelegate override; void ClearShieldsSettings(base::Time begin_time, base::Time end_time); +#if BUILDFLAG(IPFS_ENABLED) + void ClearIPFSCache(); + void WaitForIPFSRepoGC(base::Process process); +#endif Profile* profile_; }; diff --git a/chromium_src/base/threading/thread_restrictions.h b/chromium_src/base/threading/thread_restrictions.h new file mode 100644 index 000000000000..0dc8ae73e0e8 --- /dev/null +++ b/chromium_src/base/threading/thread_restrictions.h @@ -0,0 +1,17 @@ +/* Copyright (c) 2021 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/. */ + +#ifndef BRAVE_CHROMIUM_SRC_BASE_THREADING_THREAD_RESTRICTIONS_H_ +#define BRAVE_CHROMIUM_SRC_BASE_THREADING_THREAD_RESTRICTIONS_H_ + +class BraveBrowsingDataRemoverDelegate; + +#define BRAVE_SCOPED_ALLOW_BASE_SYNC_PRIMITIVES_H \ + friend class ::BraveBrowsingDataRemoverDelegate; + +#include "../../../../base/threading/thread_restrictions.h" +#undef BRAVE_SCOPED_ALLOW_BASE_SYNC_PRIMITIVES_H + +#endif // BRAVE_CHROMIUM_SRC_BASE_THREADING_THREAD_RESTRICTIONS_H_ diff --git a/chromium_src/chrome/browser/browsing_data/chrome_browsing_data_remover_delegate.h b/chromium_src/chrome/browser/browsing_data/chrome_browsing_data_remover_delegate.h new file mode 100644 index 000000000000..55479a99f0bb --- /dev/null +++ b/chromium_src/chrome/browser/browsing_data/chrome_browsing_data_remover_delegate.h @@ -0,0 +1,17 @@ +/* Copyright (c) 2021 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/. */ + +#ifndef BRAVE_CHROMIUM_SRC_CHROME_BROWSER_BROWSING_DATA_CHROME_BROWSING_DATA_REMOVER_DELEGATE_H_ +#define BRAVE_CHROMIUM_SRC_CHROME_BROWSER_BROWSING_DATA_CHROME_BROWSING_DATA_REMOVER_DELEGATE_H_ + +class BraveBrowsingDataRemoverDelegate; + +#define BRAVE_CHROME_BROWSING_DATA_REMOVER_DELEGATE_H \ + friend class BraveBrowsingDataRemoverDelegate; + +#include "../../../../../chrome/browser/browsing_data/chrome_browsing_data_remover_delegate.h" +#undef BRAVE_CHROME_BROWSING_DATA_REMOVER_DELEGATE_H + +#endif // BRAVE_CHROMIUM_SRC_CHROME_BROWSER_BROWSING_DATA_CHROME_BROWSING_DATA_REMOVER_DELEGATE_H_ diff --git a/components/ipfs/ipfs_service.h b/components/ipfs/ipfs_service.h index 9090a76d753d..901af25ea7e2 100644 --- a/components/ipfs/ipfs_service.h +++ b/components/ipfs/ipfs_service.h @@ -70,6 +70,7 @@ class IpfsService : public KeyedService, bool IsIPFSExecutableAvailable() const; void RegisterIpfsClientUpdater(); IPFSResolveMethodTypes GetIPFSResolveMethodType() const; + base::FilePath GetIpfsExecutablePath(); base::FilePath GetDataPath() const; base::FilePath GetConfigFilePath() const; @@ -88,7 +89,6 @@ class IpfsService : public KeyedService, void RunLaunchDaemonCallbackForTest(bool result); protected: - base::FilePath GetIpfsExecutablePath(); void OnConfigLoaded(GetConfigCallback, const std::pair&); private: diff --git a/patches/base-threading-thread_restrictions.h.patch b/patches/base-threading-thread_restrictions.h.patch new file mode 100644 index 000000000000..b51367b3f620 --- /dev/null +++ b/patches/base-threading-thread_restrictions.h.patch @@ -0,0 +1,12 @@ +diff --git a/base/threading/thread_restrictions.h b/base/threading/thread_restrictions.h +index ac19eaecc4cc36c4376052c33314bc289bab7bcf..6e79d17b49ba84fa4ccbbfefd88c3aa1888a855b 100644 +--- a/base/threading/thread_restrictions.h ++++ b/base/threading/thread_restrictions.h +@@ -433,6 +433,7 @@ INLINE_IF_DCHECK_IS_OFF void DisallowBaseSyncPrimitives() + EMPTY_BODY_IF_DCHECK_IS_OFF; + + class BASE_EXPORT ScopedAllowBaseSyncPrimitives { ++ BRAVE_SCOPED_ALLOW_BASE_SYNC_PRIMITIVES_H + private: + // This can only be instantiated by friends. Use + // ScopedAllowBaseSyncPrimitivesForTesting in unit tests to avoid the friend diff --git a/patches/chrome-browser-browsing_data-chrome_browsing_data_remover_delegate.h.patch b/patches/chrome-browser-browsing_data-chrome_browsing_data_remover_delegate.h.patch new file mode 100644 index 000000000000..542591f21077 --- /dev/null +++ b/patches/chrome-browser-browsing_data-chrome_browsing_data_remover_delegate.h.patch @@ -0,0 +1,12 @@ +diff --git a/chrome/browser/browsing_data/chrome_browsing_data_remover_delegate.h b/chrome/browser/browsing_data/chrome_browsing_data_remover_delegate.h +index ae0d55606244bca6ac1a1fa0037e0dc286c36174..74bc2123ced2db3fcdfc8854aac5a61f6067bd62 100644 +--- a/chrome/browser/browsing_data/chrome_browsing_data_remover_delegate.h ++++ b/chrome/browser/browsing_data/chrome_browsing_data_remover_delegate.h +@@ -195,6 +195,7 @@ class ChromeBrowsingDataRemoverDelegate + void OverrideDomainReliabilityClearerForTesting( + DomainReliabilityClearer clearer); + ++ BRAVE_CHROME_BROWSING_DATA_REMOVER_DELEGATE_H + private: + using WebRtcEventLogManager = webrtc_event_logging::WebRtcEventLogManager; +