Move KillTor() to TorProfileManager

This commit is contained in:
Anthony Tseng
2020-11-18 15:13:23 -08:00
parent b03d2c44db
commit dfd40f7ca1
5 changed files with 22 additions and 39 deletions
+19 -2
View File
@@ -5,6 +5,8 @@
#include "brave/browser/tor/tor_profile_manager.h"
#include <algorithm>
#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<TorProfileManager> 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) {
+1 -34
View File
@@ -6,7 +6,6 @@
#include "brave/browser/tor/tor_profile_service_factory.h"
#include <memory>
#include <set>
#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<content::BrowserContext*> 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<tor::TorProfileServiceImpl*>(
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);
}
@@ -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);
};
+1
View File
@@ -49,6 +49,7 @@ class TorProfileService : public KeyedService {
virtual std::unique_ptr<net::ProxyConfigService>
CreateProxyConfigService() = 0;
virtual bool IsTorConnected() = 0;
virtual void KillTor() = 0;
virtual void SetTorLaunchedForTest() {}
void AddObserver(TorLauncherServiceObserver* observer);
void RemoveObserver(TorLauncherServiceObserver* observer);
+1 -1
View File
@@ -44,9 +44,9 @@ class TorProfileServiceImpl : public TorProfileService,
void SetNewTorCircuit(content::WebContents* web_contents) override;
std::unique_ptr<net::ProxyConfigService> CreateProxyConfigService() override;
bool IsTorConnected() override;
void KillTor() override;
void SetTorLaunchedForTest() override;
void KillTor();
// For internal observer
void NotifyTorLauncherCrashed();