diff --git a/browser/tor/tor_profile_manager.cc b/browser/tor/tor_profile_manager.cc index 8794f927d18..f32ad86a9f4 100644 --- a/browser/tor/tor_profile_manager.cc +++ b/browser/tor/tor_profile_manager.cc @@ -5,6 +5,8 @@ #include "brave/browser/tor/tor_profile_manager.h" +#include + #include "brave/browser/tor/tor_profile_service_factory.h" #include "brave/browser/translate/buildflags/buildflags.h" #include "brave/common/pref_names.h" @@ -23,6 +25,15 @@ #include "components/translate/core/browser/translate_pref_names.h" #endif +namespace { +size_t GetTorBrowserCount() { + BrowserList* list = BrowserList::GetInstance(); + return std::count_if(list->begin(), list->end(), [](Browser* browser) { + return browser->profile()->IsTor(); + }); +} +} // namespace + // static TorProfileManager& TorProfileManager::GetInstance() { static base::NoDestructor instance; @@ -78,8 +89,14 @@ Profile* TorProfileManager::GetTorProfile(Profile* original_profile) { } void TorProfileManager::OnBrowserRemoved(Browser* browser) { - // TODO(darkdh): make KillTor logic here and travere windows through - // BrowserList + if (!browser || !browser->profile()->IsTor()) + return; + + if (!GetTorBrowserCount()) { + tor::TorProfileService* service = + TorProfileServiceFactory::GetForContext(browser->profile()); + service->KillTor(); + } } void TorProfileManager::OnProfileWillBeDestroyed(Profile* profile) { diff --git a/browser/tor/tor_profile_service_factory.cc b/browser/tor/tor_profile_service_factory.cc index 1c7b79e9ee7..65d5004a5a4 100644 --- a/browser/tor/tor_profile_service_factory.cc +++ b/browser/tor/tor_profile_service_factory.cc @@ -6,7 +6,6 @@ #include "brave/browser/tor/tor_profile_service_factory.h" #include -#include #include "base/path_service.h" #include "brave/browser/brave_browser_process_impl.h" @@ -18,10 +17,6 @@ #include "components/prefs/pref_service.h" #include "content/public/browser/browser_context.h" -namespace { -std::set g_context_set; -} - // static tor::TorProfileService* TorProfileServiceFactory::GetForContext( content::BrowserContext* context) { @@ -59,9 +54,7 @@ bool TorProfileServiceFactory::IsTorDisabled() { TorProfileServiceFactory::TorProfileServiceFactory() : BrowserContextKeyedServiceFactory( "TorProfileService", - BrowserContextDependencyManager::GetInstance()) { - g_context_set.clear(); -} + BrowserContextDependencyManager::GetInstance()) {} TorProfileServiceFactory::~TorProfileServiceFactory() {} @@ -80,11 +73,6 @@ KeyedService* TorProfileServiceFactory::BuildServiceInstanceFor( : nullptr, user_data_dir)); - // We only care about Tor incognito profiles for deciding whether to KillTor. - if (context->IsOffTheRecord()) { - g_context_set.emplace(context); - } - return tor_profile_service.release(); } @@ -94,24 +82,3 @@ content::BrowserContext* TorProfileServiceFactory::GetBrowserContextToUse( // LaunchTor when a new Tor window is created. return context; } - -void TorProfileServiceFactory::BrowserContextShutdown( - content::BrowserContext* context) { - // KillTor when the last Tor incognito profile is shutting down. - if (g_context_set.size() == 1) { - auto* service = static_cast( - TorProfileServiceFactory::GetForContext(context, false)); - if (service) { - service->KillTor(); - } else { - DCHECK(!brave::IsTorProfile(context)); - } - } - BrowserContextKeyedServiceFactory::BrowserContextShutdown(context); -} - -void TorProfileServiceFactory::BrowserContextDestroyed( - content::BrowserContext* context) { - g_context_set.erase(context); - BrowserContextKeyedServiceFactory::BrowserContextDestroyed(context); -} diff --git a/browser/tor/tor_profile_service_factory.h b/browser/tor/tor_profile_service_factory.h index 0e996c1a253..15b28eb609e 100644 --- a/browser/tor/tor_profile_service_factory.h +++ b/browser/tor/tor_profile_service_factory.h @@ -38,8 +38,6 @@ class TorProfileServiceFactory : public BrowserContextKeyedServiceFactory { content::BrowserContext* context) const override; content::BrowserContext* GetBrowserContextToUse( content::BrowserContext* context) const override; - void BrowserContextShutdown(content::BrowserContext* context) override; - void BrowserContextDestroyed(content::BrowserContext* context) override; DISALLOW_COPY_AND_ASSIGN(TorProfileServiceFactory); }; diff --git a/components/tor/tor_profile_service.h b/components/tor/tor_profile_service.h index f2e43367be4..54b574c67b2 100644 --- a/components/tor/tor_profile_service.h +++ b/components/tor/tor_profile_service.h @@ -49,6 +49,7 @@ class TorProfileService : public KeyedService { virtual std::unique_ptr CreateProxyConfigService() = 0; virtual bool IsTorConnected() = 0; + virtual void KillTor() = 0; virtual void SetTorLaunchedForTest() {} void AddObserver(TorLauncherServiceObserver* observer); void RemoveObserver(TorLauncherServiceObserver* observer); diff --git a/components/tor/tor_profile_service_impl.h b/components/tor/tor_profile_service_impl.h index f6e9446985d..74a3e2c468b 100644 --- a/components/tor/tor_profile_service_impl.h +++ b/components/tor/tor_profile_service_impl.h @@ -44,9 +44,9 @@ class TorProfileServiceImpl : public TorProfileService, void SetNewTorCircuit(content::WebContents* web_contents) override; std::unique_ptr CreateProxyConfigService() override; bool IsTorConnected() override; + void KillTor() override; void SetTorLaunchedForTest() override; - void KillTor(); // For internal observer void NotifyTorLauncherCrashed();