diff --git a/app/brave_generated_resources.grd b/app/brave_generated_resources.grd index 766333ca049c..b96a2426c9b7 100644 --- a/app/brave_generated_resources.grd +++ b/app/brave_generated_resources.grd @@ -359,9 +359,6 @@ By installing this extension, you are agreeing to the Google Widevine Terms of U Tor - - Tor Launcher - Bat Ads Service diff --git a/browser/BUILD.gn b/browser/BUILD.gn index d14040d410b6..aa3bb5904849 100644 --- a/browser/BUILD.gn +++ b/browser/BUILD.gn @@ -1,4 +1,3 @@ -import("//brave/browser/tor/buildflags/buildflags.gni") import("//brave/build/config.gni") import("//brave/components/binance/browser/buildflags/buildflags.gni") import("//brave/components/brave_ads/browser/buildflags/buildflags.gni") @@ -16,6 +15,7 @@ import("//brave/components/greaselion/browser/buildflags/buildflags.gni") import("//brave/components/ipfs/buildflags/buildflags.gni") import("//brave/components/moonpay/browser/buildflags/buildflags.gni") import("//brave/components/speedreader/buildflags.gni") +import("//brave/components/tor/buildflags/buildflags.gni") import("//build/buildflag_header.gni") import("//build/config/features.gni") import("//chrome/common/features.gni") @@ -130,7 +130,6 @@ source_set("browser_process") { "//base", "//brave/browser/net", "//brave/browser/notifications", - "//brave/browser/tor:buildflags", "//brave/browser/translate/buildflags", "//brave/chromium_src:browser", "//brave/chromium_src:browser", @@ -160,6 +159,7 @@ source_set("browser_process") { "//brave/components/p3a", "//brave/components/resources", "//brave/components/speedreader:buildflags", + "//brave/components/tor/buildflags", "//brave/components/weekly_storage", "//brave/services/network/public/cpp", "//chrome/common", @@ -197,15 +197,6 @@ source_set("browser_process") { "//ui/base", ] - if (enable_tor) { - sources += [ - # TODO(bridiver) - change the directory for these - "extensions/brave_tor_client_updater.cc", - "extensions/brave_tor_client_updater.h", - ] - deps += [ "//third_party/re2" ] - } - if (brave_wallet_enabled) { deps += [ "//brave/components/brave_wallet", @@ -330,7 +321,7 @@ source_set("browser_process") { if (enable_tor) { deps += [ "//brave/browser/tor", - "//brave/common/tor:pref_names", + "//brave/components/tor", ] } diff --git a/browser/brave_browser_main_parts.cc b/browser/brave_browser_main_parts.cc index dd2304d81dd0..bc854312333a 100644 --- a/browser/brave_browser_main_parts.cc +++ b/browser/brave_browser_main_parts.cc @@ -7,10 +7,10 @@ #include "base/command_line.h" #include "brave/browser/browsing_data/brave_clear_browsing_data.h" -#include "brave/browser/tor/buildflags.h" #include "brave/common/pref_names.h" #include "brave/components/brave_sync/buildflags/buildflags.h" #include "brave/components/brave_sync/features.h" +#include "brave/components/tor/buildflags/buildflags.h" #include "chrome/common/chrome_features.h" #include "components/prefs/pref_service.h" #include "components/sync/driver/sync_driver_switches.h" @@ -20,7 +20,7 @@ #if BUILDFLAG(ENABLE_TOR) #include #include "base/files/file_util.h" -#include "brave/common/tor/tor_constants.h" +#include "brave/components/tor/tor_constants.h" #include "chrome/browser/browser_process_impl.h" #include "chrome/browser/profiles/profile_attributes_storage.h" #include "chrome/browser/profiles/profile_manager.h" diff --git a/browser/brave_browser_process_impl.cc b/browser/brave_browser_process_impl.cc index 7d7536409815..0dc99dc198d4 100644 --- a/browser/brave_browser_process_impl.cc +++ b/browser/brave_browser_process_impl.cc @@ -17,7 +17,6 @@ #include "brave/browser/net/brave_system_request_handler.h" #include "brave/browser/profiles/brave_profile_manager.h" #include "brave/browser/themes/brave_dark_mode_utils.h" -#include "brave/browser/tor/buildflags.h" #include "brave/browser/ui/brave_browser_command_controller.h" #include "brave/common/pref_names.h" #include "brave/components/brave_component_updater/browser/brave_on_demand_updater.h" @@ -70,8 +69,8 @@ #endif #if BUILDFLAG(ENABLE_TOR) -#include "brave/browser/extensions/brave_tor_client_updater.h" -#include "brave/common/tor/pref_names.h" +#include "brave/components/tor/brave_tor_client_updater.h" +#include "brave/components/tor/pref_names.h" #endif #if BUILDFLAG(IPFS_ENABLED) @@ -309,13 +308,16 @@ void BraveBrowserProcessImpl::OnBraveDarkModeChanged() { } #if BUILDFLAG(ENABLE_TOR) -extensions::BraveTorClientUpdater* +tor::BraveTorClientUpdater* BraveBrowserProcessImpl::tor_client_updater() { if (tor_client_updater_) return tor_client_updater_.get(); - tor_client_updater_ = extensions::BraveTorClientUpdaterFactory( - brave_component_updater_delegate()); + base::FilePath user_data_dir; + base::PathService::Get(chrome::DIR_USER_DATA, &user_data_dir); + + tor_client_updater_.reset(new tor::BraveTorClientUpdater( + brave_component_updater_delegate(), local_state(), user_data_dir)); return tor_client_updater_.get(); } diff --git a/browser/brave_browser_process_impl.h b/browser/brave_browser_process_impl.h index c4ffe87e09f9..2b139303bbf5 100644 --- a/browser/brave_browser_process_impl.h +++ b/browser/brave_browser_process_impl.h @@ -9,13 +9,13 @@ #include #include "base/memory/ref_counted.h" -#include "brave/browser/tor/buildflags.h" #include "brave/components/brave_ads/browser/buildflags/buildflags.h" #include "brave/components/brave_component_updater/browser/brave_component.h" #include "brave/components/brave_referrals/buildflags/buildflags.h" #include "brave/components/greaselion/browser/buildflags/buildflags.h" #include "brave/components/ipfs/buildflags/buildflags.h" #include "brave/components/speedreader/buildflags.h" +#include "brave/components/tor/buildflags/buildflags.h" #include "chrome/browser/browser_process_impl.h" #include "extensions/buildflags/buildflags.h" #include "third_party/widevine/cdm/buildflags.h" @@ -58,7 +58,7 @@ namespace ntp_background_images { class NTPBackgroundImagesService; } // namespace ntp_background_images -namespace extensions { +namespace tor { class BraveTorClientUpdater; } @@ -100,7 +100,7 @@ class BraveBrowserProcessImpl : public BrowserProcessImpl { brave_shields::HTTPSEverywhereService* https_everywhere_service(); brave_component_updater::LocalDataFilesService* local_data_files_service(); #if BUILDFLAG(ENABLE_TOR) - extensions::BraveTorClientUpdater* tor_client_updater(); + tor::BraveTorClientUpdater* tor_client_updater(); #endif #if BUILDFLAG(IPFS_ENABLED) ipfs::BraveIpfsClientUpdater* ipfs_client_updater(); @@ -160,7 +160,7 @@ class BraveBrowserProcessImpl : public BrowserProcessImpl { std::unique_ptr brave_referrals_service_; #endif #if BUILDFLAG(ENABLE_TOR) - std::unique_ptr tor_client_updater_; + std::unique_ptr tor_client_updater_; #endif #if BUILDFLAG(IPFS_ENABLED) std::unique_ptr ipfs_client_updater_; diff --git a/browser/brave_content_browser_client.cc b/browser/brave_content_browser_client.cc index 591ba586c758..8e49b8c1c771 100644 --- a/browser/brave_content_browser_client.cc +++ b/browser/brave_content_browser_client.cc @@ -16,11 +16,9 @@ #include "base/task/post_task.h" #include "brave/browser/brave_browser_main_extra_parts.h" #include "brave/browser/brave_browser_process_impl.h" -#include "brave/browser/extensions/brave_tor_client_updater.h" #include "brave/browser/net/brave_proxying_url_loader_factory.h" #include "brave/browser/net/brave_proxying_web_socket.h" #include "brave/browser/profiles/profile_util.h" -#include "brave/browser/tor/buildflags.h" #include "brave/common/pref_names.h" #include "brave/common/webui_url_constants.h" #include "brave/components/binance/browser/buildflags/buildflags.h" @@ -35,6 +33,7 @@ #include "brave/components/ipfs/buildflags/buildflags.h" #include "brave/components/ipfs/features.h" #include "brave/components/speedreader/buildflags.h" +#include "brave/components/tor/buildflags/buildflags.h" #include "brave/grit/brave_generated_resources.h" #include "chrome/browser/content_settings/host_content_settings_map_factory.h" #include "chrome/browser/profiles/profile.h" @@ -85,8 +84,10 @@ using extensions::ChromeContentBrowserClientExtensionsPart; #endif #if BUILDFLAG(ENABLE_TOR) -#include "brave/browser/tor/onion_location_navigation_throttle.h" -#include "brave/browser/tor/tor_navigation_throttle.h" +#include "brave/browser/tor/onion_location_navigation_throttle_delegate.h" +#include "brave/browser/tor/tor_profile_service_factory.h" +#include "brave/components/tor/onion_location_navigation_throttle.h" +#include "brave/components/tor/tor_navigation_throttle.h" #endif #if BUILDFLAG(ENABLE_SPEEDREADER) @@ -459,21 +460,32 @@ BraveContentBrowserClient::CreateThrottlesForNavigation( std::make_unique(handle)); #endif +#if BUILDFLAG(ENABLE_TOR) ||BUILDFLAG(IPFS_ENABLED) + content::BrowserContext* context = + handle->GetWebContents()->GetBrowserContext(); +#endif + #if BUILDFLAG(ENABLE_TOR) std::unique_ptr tor_navigation_throttle = - tor::TorNavigationThrottle::MaybeCreateThrottleFor(handle); + tor::TorNavigationThrottle::MaybeCreateThrottleFor(handle, + TorProfileServiceFactory::GetForContext(context), + brave::IsTorProfile(context)); if (tor_navigation_throttle) throttles.push_back(std::move(tor_navigation_throttle)); + std::unique_ptr + onion_location_navigation_throttle_delegate = + std::make_unique(); std::unique_ptr onion_location_navigation_throttle = - tor::OnionLocationNavigationThrottle::MaybeCreateThrottleFor(handle); + tor::OnionLocationNavigationThrottle::MaybeCreateThrottleFor( + handle, TorProfileServiceFactory::IsTorDisabled(), + std::move(onion_location_navigation_throttle_delegate), + brave::IsTorProfile(context)); if (onion_location_navigation_throttle) throttles.push_back(std::move(onion_location_navigation_throttle)); #endif #if BUILDFLAG(IPFS_ENABLED) - content::BrowserContext* context = - handle->GetWebContents()->GetBrowserContext(); std::unique_ptr ipfs_navigation_throttle = ipfs::IpfsNavigationThrottle::MaybeCreateThrottleFor(handle, ipfs::IpfsServiceFactory::GetForContext(context), diff --git a/browser/brave_local_state_prefs.cc b/browser/brave_local_state_prefs.cc index 2da4c7e5f97b..fa2179051867 100644 --- a/browser/brave_local_state_prefs.cc +++ b/browser/brave_local_state_prefs.cc @@ -9,7 +9,6 @@ #include "brave/browser/brave_stats/brave_stats_updater.h" #include "brave/browser/metrics/metrics_reporting_util.h" #include "brave/browser/themes/brave_dark_mode_utils.h" -#include "brave/browser/tor/buildflags.h" #include "brave/common/pref_names.h" #include "brave/components/brave_referrals/buildflags/buildflags.h" #include "brave/components/brave_shields/browser/ad_block_service.h" @@ -17,6 +16,7 @@ #include "brave/components/ntp_background_images/browser/ntp_background_images_service.h" #include "brave/components/p3a/brave_p3a_service.h" #include "brave/components/p3a/buildflags.h" +#include "brave/components/tor/buildflags/buildflags.h" #include "chrome/browser/first_run/first_run.h" #include "chrome/common/pref_names.h" #include "components/metrics/metrics_pref_names.h" @@ -28,7 +28,7 @@ #endif #if BUILDFLAG(ENABLE_TOR) -#include "brave/browser/tor/tor_profile_service.h" +#include "brave/components/tor/tor_profile_service.h" #endif #if !defined(OS_ANDROID) diff --git a/browser/brave_profile_prefs.cc b/browser/brave_profile_prefs.cc index dfdb7d986018..908c05e32770 100644 --- a/browser/brave_profile_prefs.cc +++ b/browser/brave_profile_prefs.cc @@ -7,7 +7,6 @@ #include "brave/browser/search/ntp_utils.h" #include "brave/browser/themes/brave_dark_mode_utils.h" -#include "brave/browser/tor/buildflags.h" #include "brave/browser/ui/omnibox/brave_omnibox_client_impl.h" #include "brave/common/pref_names.h" #include "brave/components/binance/browser/buildflags/buildflags.h" @@ -24,6 +23,7 @@ #include "brave/components/crypto_dot_com/browser/buildflags/buildflags.h" #include "brave/components/ipfs/buildflags/buildflags.h" #include "brave/components/speedreader/buildflags.h" +#include "brave/components/tor/buildflags/buildflags.h" #include "chrome/browser/net/prediction_options.h" #include "chrome/browser/prefs/session_startup_pref.h" #include "chrome/common/pref_names.h" @@ -89,7 +89,7 @@ #endif #if BUILDFLAG(ENABLE_TOR) -#include "brave/browser/tor/tor_profile_service.h" +#include "brave/components/tor/tor_profile_service.h" #endif #if defined(OS_ANDROID) diff --git a/browser/brave_tab_helpers.cc b/browser/brave_tab_helpers.cc index c3bf429ff674..7711d296bc33 100644 --- a/browser/brave_tab_helpers.cc +++ b/browser/brave_tab_helpers.cc @@ -9,7 +9,7 @@ #include "base/feature_list.h" #include "brave/browser/brave_stats/brave_stats_tab_helper.h" #include "brave/browser/farbling/farbling_tab_helper.h" -#include "brave/browser/tor/buildflags.h" +#include "brave/browser/profiles/profile_util.h" #include "brave/browser/ui/bookmark/brave_bookmark_tab_helper.h" #include "brave/components/brave_ads/browser/ads_tab_helper.h" #include "brave/components/brave_perf_predictor/browser/buildflags.h" @@ -20,6 +20,7 @@ #include "brave/components/greaselion/browser/buildflags/buildflags.h" #include "brave/components/ipfs/buildflags/buildflags.h" #include "brave/components/speedreader/buildflags.h" +#include "brave/components/tor/buildflags/buildflags.h" #include "content/public/browser/web_contents.h" #include "third_party/widevine/cdm/buildflags.h" @@ -59,8 +60,8 @@ #endif #if BUILDFLAG(ENABLE_TOR) -#include "brave/browser/tor/tor_tab_helper.h" -#include "brave/browser/tor/onion_location_tab_helper.h" +#include "brave/components/tor/onion_location_tab_helper.h" +#include "brave/components/tor/tor_tab_helper.h" #endif #if BUILDFLAG(IPFS_ENABLED) @@ -118,7 +119,8 @@ void AttachTabHelpers(content::WebContents* web_contents) { #endif #if BUILDFLAG(ENABLE_TOR) - tor::TorTabHelper::MaybeCreateForWebContents(web_contents); + tor::TorTabHelper::MaybeCreateForWebContents( + web_contents, brave::IsTorProfile(web_contents->GetBrowserContext())); tor::OnionLocationTabHelper::CreateForWebContents(web_contents); #endif diff --git a/browser/browser_context_keyed_service_factories.cc b/browser/browser_context_keyed_service_factories.cc index f29e7dcaad76..b512894636cd 100644 --- a/browser/browser_context_keyed_service_factories.cc +++ b/browser/browser_context_keyed_service_factories.cc @@ -5,17 +5,17 @@ #include "brave/browser/browser_context_keyed_service_factories.h" +#include "brave/browser/brave_rewards/rewards_service_factory.h" #include "brave/browser/brave_shields/ad_block_pref_service_factory.h" #include "brave/browser/brave_shields/cookie_pref_service_factory.h" +#include "brave/browser/ntp_background_images/view_counter_service_factory.h" #include "brave/browser/search_engines/search_engine_provider_service_factory.h" #include "brave/browser/search_engines/search_engine_tracker.h" -#include "brave/browser/tor/tor_profile_service_factory.h" #include "brave/components/brave_ads/browser/ads_service_factory.h" -#include "brave/components/ipfs/buildflags/buildflags.h" -#include "brave/browser/brave_rewards/rewards_service_factory.h" -#include "brave/components/greaselion/browser/buildflags/buildflags.h" -#include "brave/browser/ntp_background_images/view_counter_service_factory.h" #include "brave/components/brave_wallet/buildflags/buildflags.h" +#include "brave/components/greaselion/browser/buildflags/buildflags.h" +#include "brave/components/ipfs/buildflags/buildflags.h" +#include "brave/components/tor/buildflags/buildflags.h" #if BUILDFLAG(ENABLE_GREASELION) #include "brave/browser/greaselion/greaselion_service_factory.h" @@ -35,6 +35,10 @@ #include "brave/browser/ipfs/ipfs_service_factory.h" #endif +#if BUILDFLAG(ENABLE_TOR) +#include "brave/browser/tor/tor_profile_service_factory.h" +#endif + namespace brave { void EnsureBrowserContextKeyedServiceFactoriesBuilt() { @@ -45,7 +49,9 @@ void EnsureBrowserContextKeyedServiceFactoriesBuilt() { #if BUILDFLAG(ENABLE_GREASELION) greaselion::GreaselionServiceFactory::GetInstance(); #endif +#if BUILDFLAG(ENABLE_TOR) TorProfileServiceFactory::GetInstance(); +#endif SearchEngineProviderServiceFactory::GetInstance(); SearchEngineTrackerFactory::GetInstance(); ntp_background_images::ViewCounterServiceFactory::GetInstance(); diff --git a/browser/extensions/BUILD.gn b/browser/extensions/BUILD.gn index be7f11d22f48..46bc8dca1b05 100644 --- a/browser/extensions/BUILD.gn +++ b/browser/extensions/BUILD.gn @@ -1,4 +1,3 @@ -import("//brave/browser/tor/buildflags/buildflags.gni") import("//brave/components/binance/browser/buildflags/buildflags.gni") import("//brave/components/gemini/browser/buildflags/buildflags.gni") import("//brave/components/crypto_dot_com/browser/buildflags/buildflags.gni") @@ -9,6 +8,7 @@ import("//brave/components/brave_webtorrent/browser/buildflags/buildflags.gni") import("//brave/components/brave_together/buildflags/buildflags.gni") import("//brave/components/ipfs/buildflags/buildflags.gni") import("//brave/components/moonpay/browser/buildflags/buildflags.gni") +import("//brave/components/tor/buildflags/buildflags.gni") import("//build/config/features.gni") import("//components/gcm_driver/config.gni") @@ -55,7 +55,6 @@ source_set("extensions") { "//base", "//brave/app:brave_generated_resources_grit", "//brave/browser/profiles", - "//brave/browser/tor:buildflags", "//brave/browser/webcompat_reporter", "//brave/common", "//brave/common:pref_names", @@ -66,6 +65,7 @@ source_set("extensions") { "//brave/components/brave_shields/common", "//brave/components/brave_wayback_machine:buildflags", "//brave/components/ipfs/buildflags", + "//brave/components/tor/buildflags", "//chrome/browser/extensions", "//chrome/common", "//components/gcm_driver:gcm_driver", diff --git a/browser/extensions/api/settings_private/brave_prefs_util.cc b/browser/extensions/api/settings_private/brave_prefs_util.cc index 2685ad84d239..10f4dc8db73b 100644 --- a/browser/extensions/api/settings_private/brave_prefs_util.cc +++ b/browser/extensions/api/settings_private/brave_prefs_util.cc @@ -5,7 +5,6 @@ #include "brave/browser/extensions/api/settings_private/brave_prefs_util.h" -#include "brave/browser/tor/buildflags.h" #include "brave/common/pref_names.h" #include "brave/components/brave_rewards/common/pref_names.h" #include "brave/components/brave_wallet/buildflags/buildflags.h" @@ -14,6 +13,7 @@ #include "brave/components/ipfs/buildflags/buildflags.h" #include "brave/components/moonpay/browser/buildflags/buildflags.h" #include "brave/components/ntp_background_images/common/pref_names.h" +#include "brave/components/tor/buildflags/buildflags.h" #include "chrome/browser/extensions/api/settings_private/prefs_util.h" #include "chrome/common/extensions/api/settings_private.h" #include "chrome/common/pref_names.h" @@ -42,7 +42,7 @@ #endif #if BUILDFLAG(ENABLE_TOR) -#include "brave/common/tor/pref_names.h" +#include "brave/components/tor/pref_names.h" #endif namespace extensions { diff --git a/browser/extensions/brave_extension_management.cc b/browser/extensions/brave_extension_management.cc index 29f95ed66dae..3d5c2d4d2054 100644 --- a/browser/extensions/brave_extension_management.cc +++ b/browser/extensions/brave_extension_management.cc @@ -8,10 +8,11 @@ #include #include "brave/browser/brave_browser_process_impl.h" -#include "brave/browser/tor/buildflags.h" -#include "brave/common/pref_names.h" -#include "brave/common/tor/pref_names.h" #include "brave/browser/extensions/brave_extension_provider.h" +#include "brave/common/pref_names.h" +#include "brave/browser/tor/tor_profile_service_factory.h" +#include "brave/components/tor/buildflags/buildflags.h" +#include "brave/components/tor/pref_names.h" #include "chrome/browser/extensions/external_policy_loader.h" #include "chrome/browser/profiles/profile.h" #include "components/prefs/pref_service.h" @@ -21,8 +22,7 @@ #include "extensions/common/extension_urls.h" #if BUILDFLAG(ENABLE_TOR) -#include "brave/browser/extensions/brave_tor_client_updater.h" -#include "brave/browser/tor/tor_profile_service.h" +#include "brave/components/tor/brave_tor_client_updater.h" #endif namespace extensions { @@ -65,7 +65,7 @@ void BraveExtensionManagement::OnExtensionUnloaded( void BraveExtensionManagement::OnTorDisabledChanged() { #if BUILDFLAG(ENABLE_TOR) - if (tor::TorProfileService::IsTorDisabled()) + if (TorProfileServiceFactory::IsTorDisabled()) g_brave_browser_process->tor_client_updater()->Cleanup(); #endif } diff --git a/browser/ipfs/content_browser_client_helper_unittest.cc b/browser/ipfs/content_browser_client_helper_unittest.cc index 46ad8dc1dce9..cc7cb745a78d 100644 --- a/browser/ipfs/content_browser_client_helper_unittest.cc +++ b/browser/ipfs/content_browser_client_helper_unittest.cc @@ -13,10 +13,10 @@ #include "brave/browser/profiles/brave_profile_manager.h" #include "brave/browser/profiles/brave_unittest_profile_manager.h" #include "brave/browser/profiles/profile_util.h" -#include "brave/browser/tor/buildflags.h" #include "brave/components/ipfs/features.h" #include "brave/components/ipfs/ipfs_constants.h" #include "brave/components/ipfs/pref_names.h" +#include "brave/components/tor/buildflags/buildflags.h" #include "chrome/browser/prefs/browser_prefs.h" #include "chrome/test/base/chrome_render_view_host_test_harness.h" #include "chrome/test/base/scoped_testing_local_state.h" diff --git a/browser/policy/brave_policy_browsertest.cc b/browser/policy/brave_policy_browsertest.cc index 21eadc865ba6..035f07a21648 100644 --- a/browser/policy/brave_policy_browsertest.cc +++ b/browser/policy/brave_policy_browsertest.cc @@ -3,8 +3,9 @@ * 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/tor/buildflags.h" +#include "brave/browser/tor/tor_profile_service_factory.h" #include "brave/common/pref_names.h" +#include "brave/components/tor/buildflags/buildflags.h" #include "chrome/browser/browser_process.h" #include "chrome/browser/profiles/profile.h" #include "chrome/browser/ui/browser.h" @@ -15,10 +16,6 @@ #include "components/prefs/pref_service.h" #include "content/public/test/browser_test.h" -#if BUILDFLAG(ENABLE_TOR) -#include "brave/browser/tor/tor_profile_service.h" -#endif - using testing::_; using testing::Return; using NoTorPolicyBrowserTest = InProcessBrowserTest; @@ -60,8 +57,8 @@ class TorDisabledPolicyBrowserTest : public BravePolicyTest { IN_PROC_BROWSER_TEST_F(TorDisabledPolicyBrowserTest, TorDisabledPrefValueTest) { // When policy is set, explicit setting doesn't change its pref value. - tor::TorProfileService::SetTorDisabled(false); - EXPECT_TRUE(tor::TorProfileService::IsTorDisabled()); + TorProfileServiceFactory::SetTorDisabled(false); + EXPECT_TRUE(TorProfileServiceFactory::IsTorDisabled()); } class TorEnabledPolicyBrowserTest : public BravePolicyTest { @@ -82,9 +79,9 @@ class TorEnabledPolicyBrowserTest : public BravePolicyTest { IN_PROC_BROWSER_TEST_F(TorEnabledPolicyBrowserTest, TorDisabledPrefValueTest) { // When policy is set, explicit setting doesn't change its pref value. - tor::TorProfileService::SetTorDisabled(true); - EXPECT_FALSE(tor::TorProfileService::IsTorDisabled()); + TorProfileServiceFactory::SetTorDisabled(true); + EXPECT_FALSE(TorProfileServiceFactory::IsTorDisabled()); } -#endif // ENABLE_TOR +#endif } // namespace policy diff --git a/browser/profiles/BUILD.gn b/browser/profiles/BUILD.gn index 7bd266878c3b..a4b654e18262 100644 --- a/browser/profiles/BUILD.gn +++ b/browser/profiles/BUILD.gn @@ -29,10 +29,10 @@ source_set("profiles") { "//brave/browser/tor", "//brave/browser/translate/buildflags", "//brave/common:pref_names", - "//brave/common/tor", "//brave/components/brave_ads/browser", "//brave/components/brave_rewards/browser", "//brave/components/brave_sync:prefs", + "//brave/components/tor", "//brave/content:browser", "//chrome/common", "//components/gcm_driver:gcm_buildflags", diff --git a/browser/profiles/brave_profile_manager.cc b/browser/profiles/brave_profile_manager.cc index 3fc26e9b2b81..182e69dae9b1 100644 --- a/browser/profiles/brave_profile_manager.cc +++ b/browser/profiles/brave_profile_manager.cc @@ -12,13 +12,8 @@ #include "base/metrics/histogram_macros.h" #include "brave/browser/brave_rewards/rewards_service_factory.h" #include "brave/browser/profiles/profile_util.h" -#include "brave/browser/tor/buildflags.h" -#include "brave/browser/tor/tor_profile_service.h" -#include "brave/browser/tor/tor_profile_service_factory.h" #include "brave/browser/translate/buildflags/buildflags.h" #include "brave/common/pref_names.h" -#include "brave/common/tor/pref_names.h" -#include "brave/common/tor/tor_constants.h" #include "brave/components/brave_ads/browser/ads_service_factory.h" #include "brave/components/brave_shields/browser/ad_block_regional_service.h" #include "brave/components/brave_shields/browser/ad_block_service.h" @@ -26,6 +21,10 @@ #include "brave/components/brave_wallet/buildflags/buildflags.h" #include "brave/components/brave_webtorrent/browser/buildflags/buildflags.h" #include "brave/components/ipfs/buildflags/buildflags.h" +#include "brave/components/tor/buildflags/buildflags.h" +#include "brave/components/tor/pref_names.h" +#include "brave/components/tor/tor_constants.h" +#include "brave/components/tor/tor_profile_service.h" #include "brave/content/browser/webui/brave_shared_resources_data_source.h" #include "chrome/browser/browser_process.h" #include "chrome/browser/chrome_notification_types.h" @@ -66,6 +65,10 @@ #include "brave/browser/ipfs/ipfs_service_factory.h" #endif +#if BUILDFLAG(ENABLE_TOR) +#include "brave/browser/tor/tor_profile_service_factory.h" +#endif + using content::BrowserThread; BraveProfileManager::BraveProfileManager(const base::FilePath& user_data_dir) @@ -203,7 +206,7 @@ void BraveProfileManager::OnProfileCreated(Profile* profile, // We need to wait until OnProfileCreated to // ensure that the request context is available. - TorProfileServiceFactory::GetForProfile(profile); + TorProfileServiceFactory::GetForContext(profile); } #endif } diff --git a/browser/profiles/brave_profile_manager_browsertest.cc b/browser/profiles/brave_profile_manager_browsertest.cc index 4103c716bb6f..972035ce9f66 100644 --- a/browser/profiles/brave_profile_manager_browsertest.cc +++ b/browser/profiles/brave_profile_manager_browsertest.cc @@ -9,12 +9,11 @@ #include "base/strings/utf_string_conversions.h" #include "brave/browser/brave_browser_process_impl.h" #include "brave/browser/brave_rewards/rewards_service_factory.h" -#include "brave/browser/extensions/brave_tor_client_updater.h" #include "brave/browser/profiles/profile_util.h" -#include "brave/browser/tor/buildflags.h" #include "brave/common/brave_paths.h" #include "brave/components/brave_ads/browser/ads_service_factory.h" #include "brave/components/ipfs/buildflags/buildflags.h" +#include "brave/components/tor/buildflags/buildflags.h" #include "chrome/browser/bookmarks/bookmark_model_factory.h" #include "chrome/browser/browser_process.h" #include "chrome/browser/chrome_notification_types.h" @@ -38,7 +37,8 @@ #include "content/public/test/test_utils.h" #if BUILDFLAG(ENABLE_TOR) -#include "brave/browser/tor/tor_launcher_factory.h" +#include "brave/components/tor/brave_tor_client_updater.h" +#include "brave/components/tor/tor_launcher_factory.h" #endif #if BUILDFLAG(ENABLE_EXTENSIONS) diff --git a/browser/profiles/brave_profile_manager_unittest.cc b/browser/profiles/brave_profile_manager_unittest.cc index 0300bfa29a24..c4df935e3f8c 100644 --- a/browser/profiles/brave_profile_manager_unittest.cc +++ b/browser/profiles/brave_profile_manager_unittest.cc @@ -14,10 +14,10 @@ #include "brave/browser/profiles/brave_unittest_profile_manager.h" #include "brave/browser/profiles/profile_util.h" #include "brave/browser/tor/tor_profile_service_factory.h" -#include "brave/browser/tor/tor_profile_service_impl.h" #include "brave/browser/translate/buildflags/buildflags.h" -#include "brave/common/tor/tor_constants.h" #include "brave/components/brave_webtorrent/browser/webtorrent_util.h" +#include "brave/components/tor/tor_constants.h" +#include "brave/components/tor/tor_profile_service_impl.h" #include "chrome/browser/net/proxy_config_monitor.h" #include "chrome/browser/profiles/profile.h" #include "chrome/browser/profiles/profile_avatar_icon_util.h" @@ -257,7 +257,7 @@ TEST_F(BraveProfileManagerTest, ProxyConfigMonitorInTorProfile) { EXPECT_TRUE(config.value().proxy_rules().empty()); // Emulate get socks info from tor control - auto* tor_profile_service = TorProfileServiceFactory::GetForProfile(profile); + auto* tor_profile_service = TorProfileServiceFactory::GetForContext(profile); static_cast(tor_profile_service) ->NotifyTorNewProxyURI("socks5://127.0.0.1:5566"); diff --git a/browser/profiles/profile_util.cc b/browser/profiles/profile_util.cc index 4df4987d3ea1..df39adf0ecc5 100644 --- a/browser/profiles/profile_util.cc +++ b/browser/profiles/profile_util.cc @@ -12,15 +12,15 @@ #include "base/files/file_path.h" #include "base/memory/ptr_util.h" #include "base/no_destructor.h" -#include "brave/browser/tor/buildflags.h" -#include "brave/common/tor/tor_constants.h" +#include "brave/components/tor/buildflags/buildflags.h" +#include "brave/components/tor/tor_constants.h" #include "chrome/browser/browser_process.h" #include "chrome/browser/profiles/profile.h" #include "chrome/browser/profiles/profile_key.h" #include "chrome/browser/profiles/profile_manager.h" #if BUILDFLAG(ENABLE_TOR) -#include "brave/browser/tor/tor_profile_service.h" +#include "brave/browser/tor/tor_profile_service_factory.h" #endif namespace brave { @@ -201,7 +201,7 @@ bool IsGuestProfile(content::BrowserContext* context) { bool IsTorDisabledForProfile(Profile* profile) { #if BUILDFLAG(ENABLE_TOR) - return tor::TorProfileService::IsTorDisabled() || + return TorProfileServiceFactory::IsTorDisabled() || profile->IsGuestSession(); #else return true; diff --git a/browser/resources/settings/BUILD.gn b/browser/resources/settings/BUILD.gn index e04409dc0dd2..407b4f9c0f67 100644 --- a/browser/resources/settings/BUILD.gn +++ b/browser/resources/settings/BUILD.gn @@ -3,9 +3,9 @@ # 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/. -import("//brave/browser/tor/buildflags/buildflags.gni") import("//brave/build/config.gni") import("//brave/components/brave_wayback_machine/buildflags/buildflags.gni") +import("//brave/components/tor/buildflags/buildflags.gni") import("//third_party/closure_compiler/compile_js.gni") import("//chrome/browser/resources/optimize_webui.gni") import("//chrome/common/features.gni") diff --git a/browser/search_engines/search_engine_provider_service_browsertest.cc b/browser/search_engines/search_engine_provider_service_browsertest.cc index 45ff2ea6280e..d0d923414df4 100644 --- a/browser/search_engines/search_engine_provider_service_browsertest.cc +++ b/browser/search_engines/search_engine_provider_service_browsertest.cc @@ -10,9 +10,9 @@ #include "brave/browser/search_engines/guest_window_search_engine_provider_service.h" #include "brave/browser/search_engines/search_engine_provider_service_factory.h" #include "brave/browser/search_engines/search_engine_provider_util.h" -#include "brave/browser/tor/buildflags.h" #include "brave/browser/ui/browser_commands.h" #include "brave/components/search_engines/brave_prepopulated_engines.h" +#include "brave/components/tor/buildflags/buildflags.h" #include "chrome/browser/profiles/profile.h" #include "chrome/browser/profiles/profile_window.h" #include "chrome/browser/search_engines/template_url_service_factory.h" @@ -27,8 +27,8 @@ #include "content/public/test/test_utils.h" #if BUILDFLAG(ENABLE_TOR) -#include "brave/browser/extensions/brave_tor_client_updater.h" -#include "brave/browser/tor/tor_launcher_factory.h" +#include "brave/components/tor/brave_tor_client_updater.h" +#include "brave/components/tor/tor_launcher_factory.h" #endif class SearchEngineProviderServiceTest : public InProcessBrowserTest { diff --git a/browser/search_engines/search_engine_tracker_browsertest.cc b/browser/search_engines/search_engine_tracker_browsertest.cc index e302b7d3952c..b115ccd684c2 100644 --- a/browser/search_engines/search_engine_tracker_browsertest.cc +++ b/browser/search_engines/search_engine_tracker_browsertest.cc @@ -8,14 +8,18 @@ #include "base/test/metrics/histogram_tester.h" #include "brave/browser/profiles/profile_util.h" #include "brave/browser/ui/browser_commands.h" -#include "brave/browser/tor/tor_launcher_factory.h" #include "brave/components/search_engines/brave_prepopulated_engines.h" +#include "brave/components/tor/buildflags/buildflags.h" #include "chrome/browser/search_engines/template_url_service_factory.h" #include "chrome/browser/ui/browser.h" #include "chrome/test/base/in_process_browser_test.h" #include "components/search_engines/template_url_prepopulate_data.h" #include "content/public/test/browser_test.h" +#if BUILDFLAG(ENABLE_TOR) +#include "brave/components/tor/tor_launcher_factory.h" +#endif + class SearchEngineProviderP3ATest : public InProcessBrowserTest { public: SearchEngineProviderP3ATest() { @@ -47,8 +51,10 @@ IN_PROC_BROWSER_TEST_F(SearchEngineProviderP3ATest, // Check that incognito or TOR profiles do not emit the metric. CreateIncognitoBrowser(); +#if BUILDFLAG(ENABLE_TOR) ScopedTorLaunchPreventerForTest prevent_tor_process; brave::NewOffTheRecordWindowTor(browser()); +#endif histogram_tester_->ExpectTotalCount(kDefaultSearchEngineMetric, 2); } diff --git a/browser/tor/BUILD.gn b/browser/tor/BUILD.gn index e25a21ade813..c37d5eded1a3 100644 --- a/browser/tor/BUILD.gn +++ b/browser/tor/BUILD.gn @@ -1,60 +1,31 @@ -import("//build/buildflag_header.gni") -import("buildflags/buildflags.gni") - -buildflag_header("buildflags") { - header = "buildflags.h" - flags = [ "ENABLE_TOR=$enable_tor" ] -} +import("//brave/components/tor/buildflags/buildflags.gni") source_set("tor") { # Remove when https://github.com/brave/brave-browser/issues/10655 is resolved check_includes = false - sources = [ - "tor_launcher_service_observer.h", - "tor_profile_service_factory.cc", - "tor_profile_service_factory.h", - ] - - deps = [ - "//base", - "//brave/common/tor", - "//components/keyed_service/content", - "//components/keyed_service/core", - "//content/public/browser", - "//mojo/public/cpp/bindings", + visibility = [ + "//brave/browser:browser_process", + "//brave/browser/profiles", + "//brave/browser/ui", + "//brave/test:*", + ":*", ] - public_deps = [ ":buildflags" ] - if (enable_tor) { - sources += [ - "onion_location_navigation_throttle.cc", - "onion_location_navigation_throttle.h", - "onion_location_tab_helper.cc", - "onion_location_tab_helper.h", - "tor_control.cc", - "tor_control.h", - "tor_launcher_factory.cc", - "tor_launcher_factory.h", - "tor_navigation_throttle.cc", - "tor_navigation_throttle.h", - "tor_profile_service.cc", - "tor_profile_service.h", - "tor_profile_service_impl.cc", - "tor_profile_service_impl.h", - "tor_tab_helper.cc", - "tor_tab_helper.h", + sources = [ + "onion_location_navigation_throttle_delegate.cc", + "onion_location_navigation_throttle_delegate.h", + "tor_profile_service_factory.cc", + "tor_profile_service_factory.h", ] - deps += [ - "//brave/app:brave_generated_resources_grit", - "//brave/components/services/tor/public/interfaces", + deps = [ + "//base", + "//brave/components/tor", + "//brave/components/tor:pref_names", + "//chrome/common", + "//components/keyed_service/content", "//content/public/browser", - "//content/public/common", - "//extensions/common:common_constants", - "//net", - "//services/service_manager", - "//url", ] # Below dep list are not directly used tor target. @@ -69,3 +40,54 @@ source_set("tor") { ] } } + +source_set("unit_tests") { + testonly = true + if (enable_tor) { + sources = [ "tor_navigation_throttle_unittest.cc" ] + + deps = [ + "//base", + "//base/test:test_support", + "//brave/browser/profiles", + "//brave/browser/tor", + "//brave/components/tor", + "//brave/test:test_support", + "//chrome/test:test_support", + "//content/public/browser", + "//content/test:test_support", + "//testing/gtest", + "//url", + ] + } +} + +source_set("browser_tests") { + testonly = true + if (enable_tor) { + sources = [ + "brave_local_state_browsertest.cc", + "brave_tor_client_updater_browsertest.cc", + "onion_location_navigation_throttle_browsertest.cc", + ] + + deps = [ + "//base", + "//brave/app:brave_generated_resources_grit", + "//brave/browser:browser_process", + "//brave/browser/profiles", + "//brave/browser/tor", + "//brave/browser/ui", + "//brave/common", + "//brave/components/tor", + "//brave/components/tor:pref_names", + "//chrome/browser", + "//chrome/test:test_support", + "//chrome/test:test_support_ui", + "//content/public/browser", + "//content/test:test_support", + "//net:test_support", + ] + defines = [ "HAS_OUT_OF_PROC_TEST_RUNNER" ] + } +} diff --git a/browser/brave_local_state_browsertest.cc b/browser/tor/brave_local_state_browsertest.cc similarity index 72% rename from browser/brave_local_state_browsertest.cc rename to browser/tor/brave_local_state_browsertest.cc index 8eabdf9226e1..75c31a7cc2fb 100644 --- a/browser/brave_local_state_browsertest.cc +++ b/browser/tor/brave_local_state_browsertest.cc @@ -3,20 +3,15 @@ * 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/tor/buildflags.h" +#include "brave/browser/tor/tor_profile_service_factory.h" #include "chrome/browser/browser_process.h" #include "chrome/test/base/in_process_browser_test.h" #include "content/public/test/browser_test.h" -#if BUILDFLAG(ENABLE_TOR) -#include "brave/browser/tor/tor_profile_service.h" -#endif using BraveLocalStateBrowserTest = InProcessBrowserTest; IN_PROC_BROWSER_TEST_F(BraveLocalStateBrowserTest, BasicTest) { -#if BUILDFLAG(ENABLE_TOR) // Tor is enabled by default. - EXPECT_FALSE(tor::TorProfileService::IsTorDisabled()); -#endif + EXPECT_FALSE(TorProfileServiceFactory::IsTorDisabled()); } diff --git a/browser/extensions/brave_tor_client_updater_browsertest.cc b/browser/tor/brave_tor_client_updater_browsertest.cc similarity index 97% rename from browser/extensions/brave_tor_client_updater_browsertest.cc rename to browser/tor/brave_tor_client_updater_browsertest.cc index a3b2d4f1c111..37db9a7c022d 100644 --- a/browser/extensions/brave_tor_client_updater_browsertest.cc +++ b/browser/tor/brave_tor_client_updater_browsertest.cc @@ -10,7 +10,7 @@ #include "base/process/launch.h" #include "base/test/thread_test_helper.h" #include "brave/browser/brave_browser_process_impl.h" -#include "brave/browser/extensions/brave_tor_client_updater.h" +#include "brave/components/tor/brave_tor_client_updater.h" #include "brave/common/brave_paths.h" #include "chrome/browser/extensions/extension_browsertest.h" #include "content/public/test/browser_test.h" @@ -64,7 +64,7 @@ class BraveTorClientUpdaterTest : public ExtensionBrowserTest { void SetComponentIdAndBase64PublicKeyForTest( const std::string& component_id, const std::string& component_base64_public_key) { - extensions::BraveTorClientUpdater::SetComponentIdAndBase64PublicKeyForTest( + tor::BraveTorClientUpdater::SetComponentIdAndBase64PublicKeyForTest( component_id, component_base64_public_key); } diff --git a/browser/tor/onion_location_navigation_throttle_browsertest.cc b/browser/tor/onion_location_navigation_throttle_browsertest.cc index 5b75795546b5..56883a549210 100644 --- a/browser/tor/onion_location_navigation_throttle_browsertest.cc +++ b/browser/tor/onion_location_navigation_throttle_browsertest.cc @@ -5,12 +5,12 @@ #include "base/strings/utf_string_conversions.h" #include "brave/browser/profiles/profile_util.h" -#include "brave/browser/tor/onion_location_tab_helper.h" -#include "brave/browser/tor/tor_profile_service.h" +#include "brave/browser/tor/tor_profile_service_factory.h" #include "brave/browser/ui/browser_commands.h" #include "brave/browser/ui/views/location_bar/brave_location_bar_view.h" #include "brave/browser/ui/views/location_bar/onion_location_view.h" -#include "brave/common/tor/pref_names.h" +#include "brave/components/tor/onion_location_tab_helper.h" +#include "brave/components/tor/pref_names.h" #include "brave/grit/brave_generated_resources.h" #include "chrome/browser/chrome_notification_types.h" #include "chrome/browser/ui/browser.h" @@ -182,7 +182,7 @@ IN_PROC_BROWSER_TEST_F(OnionLocationNavigationThrottleBrowserTest, IN_PROC_BROWSER_TEST_F(OnionLocationNavigationThrottleBrowserTest, TorDisabled) { // Disable tor - tor::TorProfileService::SetTorDisabled(true); + TorProfileServiceFactory::SetTorDisabled(true); // OnionLocationHeader_ GURL url = test_server()->GetURL("/onion"); diff --git a/browser/tor/onion_location_navigation_throttle_delegate.cc b/browser/tor/onion_location_navigation_throttle_delegate.cc new file mode 100644 index 000000000000..0d2a7e52f380 --- /dev/null +++ b/browser/tor/onion_location_navigation_throttle_delegate.cc @@ -0,0 +1,56 @@ +/* Copyright (c) 2020 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/tor/onion_location_navigation_throttle_delegate.h" + +#include + +#include "chrome/browser/profiles/profile.h" +#include "chrome/browser/profiles/profile_window.h" +#include "chrome/browser/ui/browser.h" +#include "chrome/browser/ui/browser_finder.h" +#include "content/public/browser/web_contents.h" + +namespace tor { + +namespace { + +void OnTorProfileCreated(GURL onion_location, + Profile* profile, + Profile::CreateStatus status) { + if (status != Profile::CreateStatus::CREATE_STATUS_INITIALIZED) + return; + Browser* browser = chrome::FindTabbedBrowser(profile, true); + if (!browser) + return; + content::OpenURLParams open_tor(onion_location, content::Referrer(), + WindowOpenDisposition::OFF_THE_RECORD, + ui::PAGE_TRANSITION_TYPED, false); + browser->OpenURL(open_tor); +} + +} // namespace + +OnionLocationNavigationThrottleDelegate:: + OnionLocationNavigationThrottleDelegate() = default; + +OnionLocationNavigationThrottleDelegate:: + ~OnionLocationNavigationThrottleDelegate() = default; + +void OnionLocationNavigationThrottleDelegate::OpenInTorWindow( + content::WebContents* web_contents, + GURL onion_location) { + profiles::SwitchToTorProfile( + base::BindRepeating(&OnTorProfileCreated, std::move(onion_location))); + + // We do not close last tab of the window + Profile* profile = + Profile::FromBrowserContext(web_contents->GetBrowserContext()); + Browser* browser = chrome::FindBrowserWithProfile(profile); + if (browser && browser->tab_strip_model()->count() > 1) + web_contents->ClosePage(); +} + +} // namespace tor diff --git a/browser/tor/onion_location_navigation_throttle_delegate.h b/browser/tor/onion_location_navigation_throttle_delegate.h new file mode 100644 index 000000000000..c6f4c5b5a08d --- /dev/null +++ b/browser/tor/onion_location_navigation_throttle_delegate.h @@ -0,0 +1,37 @@ +/* Copyright (c) 2020 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_BROWSER_TOR_ONION_LOCATION_NAVIGATION_THROTTLE_DELEGATE_H_ +#define BRAVE_BROWSER_TOR_ONION_LOCATION_NAVIGATION_THROTTLE_DELEGATE_H_ + +#include "brave/components/tor/onion_location_navigation_throttle.h" + +class GURL; + +namespace content { +class WebContents; +} // namespace content + +namespace tor { + +class OnionLocationNavigationThrottleDelegate + : public OnionLocationNavigationThrottle::Delegate { + public: + OnionLocationNavigationThrottleDelegate(); + ~OnionLocationNavigationThrottleDelegate() override; + + void OpenInTorWindow(content::WebContents* web_contents, + GURL onion_location) override; + + private: + OnionLocationNavigationThrottleDelegate( + const OnionLocationNavigationThrottleDelegate&) = delete; + OnionLocationNavigationThrottleDelegate& operator=( + const OnionLocationNavigationThrottleDelegate&) = delete; +}; + +} // namespace tor + +#endif // BRAVE_BROWSER_TOR_ONION_LOCATION_NAVIGATION_THROTTLE_DELEGATE_H_ diff --git a/browser/tor/tor_navigation_throttle_unittest.cc b/browser/tor/tor_navigation_throttle_unittest.cc index 240ce67eb2cd..5cbd50dc25cd 100644 --- a/browser/tor/tor_navigation_throttle_unittest.cc +++ b/browser/tor/tor_navigation_throttle_unittest.cc @@ -1,10 +1,8 @@ -/* Copyright (c) 2019 The Brave Authors. All rights reserved. +/* Copyright (c) 2020 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/tor/tor_navigation_throttle.h" - #include #include "base/files/scoped_temp_dir.h" @@ -12,14 +10,15 @@ #include "brave/browser/profiles/brave_profile_manager.h" #include "brave/browser/profiles/brave_unittest_profile_manager.h" #include "brave/browser/profiles/profile_util.h" -#include "brave/browser/tor/tor_profile_service.h" #include "brave/browser/tor/tor_profile_service_factory.h" +#include "brave/components/tor/tor_navigation_throttle.h" +#include "brave/components/tor/tor_profile_service.h" #include "chrome/test/base/scoped_testing_local_state.h" #include "chrome/test/base/testing_browser_process.h" #include "chrome/test/base/testing_profile.h" #include "content/public/browser/navigation_handle.h" -#include "content/public/test/mock_navigation_handle.h" #include "content/public/test/browser_task_environment.h" +#include "content/public/test/mock_navigation_handle.h" #include "content/public/test/test_renderer_host.h" #include "content/public/test/web_contents_tester.h" #include "testing/gtest/include/gtest/gtest.h" @@ -45,14 +44,15 @@ class TorNavigationThrottleUnitTest : public testing::Test { ASSERT_TRUE(profile_manager); Profile* profile = profile_manager->GetProfile( temp_dir_.GetPath().AppendASCII(TestingProfile::kTestUserProfileDir)); - Profile* tor_profile = profile_manager->GetProfile( - BraveProfileManager::GetTorProfilePath()); + Profile* tor_profile = + profile_manager->GetProfile(BraveProfileManager::GetTorProfilePath()); ASSERT_EQ(brave::GetParentProfile(tor_profile), profile); web_contents_ = content::WebContentsTester::CreateTestWebContents(profile, nullptr); tor_web_contents_ = content::WebContentsTester::CreateTestWebContents(tor_profile, nullptr); - tor_profile_service_ = TorProfileServiceFactory::GetForProfile(tor_profile); + tor_profile_service_ = TorProfileServiceFactory::GetForContext(tor_profile); + ASSERT_EQ(TorProfileServiceFactory::GetForContext(profile), nullptr); } void TearDown() override { @@ -61,17 +61,11 @@ class TorNavigationThrottleUnitTest : public testing::Test { TestingBrowserProcess::GetGlobal()->SetProfileManager(nullptr); } - content::WebContents* web_contents() { - return web_contents_.get(); - } + content::WebContents* web_contents() { return web_contents_.get(); } - content::WebContents* tor_web_contents() { - return tor_web_contents_.get(); - } + content::WebContents* tor_web_contents() { return tor_web_contents_.get(); } - TorProfileService* tor_profile_service() { - return tor_profile_service_; - } + TorProfileService* tor_profile_service() { return tor_profile_service_; } private: content::BrowserTaskEnvironment task_environment_; @@ -89,12 +83,16 @@ class TorNavigationThrottleUnitTest : public testing::Test { TEST_F(TorNavigationThrottleUnitTest, Instantiation) { content::MockNavigationHandle test_handle(tor_web_contents()); std::unique_ptr throttle = - TorNavigationThrottle::MaybeCreateThrottleFor(&test_handle); + TorNavigationThrottle::MaybeCreateThrottleFor( + &test_handle, tor_profile_service(), + brave::IsTorProfile(tor_web_contents()->GetBrowserContext())); EXPECT_TRUE(throttle != nullptr); content::MockNavigationHandle test_handle2(web_contents()); std::unique_ptr throttle2 = - TorNavigationThrottle::MaybeCreateThrottleFor(&test_handle2); + TorNavigationThrottle::MaybeCreateThrottleFor( + &test_handle2, nullptr, + brave::IsTorProfile(web_contents()->GetBrowserContext())); EXPECT_TRUE(throttle2 == nullptr); } @@ -102,7 +100,9 @@ TEST_F(TorNavigationThrottleUnitTest, WhitelistedScheme) { tor_profile_service()->SetTorLaunchedForTest(); content::MockNavigationHandle test_handle(tor_web_contents()); std::unique_ptr throttle = - TorNavigationThrottle::MaybeCreateThrottleFor(&test_handle); + TorNavigationThrottle::MaybeCreateThrottleFor( + &test_handle, tor_profile_service(), + brave::IsTorProfile(tor_web_contents()->GetBrowserContext())); GURL url("http://www.example.com"); test_handle.set_url(url); EXPECT_EQ(NavigationThrottle::PROCEED, throttle->WillStartRequest().action()) @@ -136,27 +136,34 @@ TEST_F(TorNavigationThrottleUnitTest, BlockedScheme) { tor_profile_service()->SetTorLaunchedForTest(); content::MockNavigationHandle test_handle(tor_web_contents()); std::unique_ptr throttle = - TorNavigationThrottle::MaybeCreateThrottleFor(&test_handle); + TorNavigationThrottle::MaybeCreateThrottleFor( + &test_handle, tor_profile_service(), + brave::IsTorProfile(tor_web_contents()->GetBrowserContext())); GURL url("ftp://ftp.example.com"); test_handle.set_url(url); EXPECT_EQ(NavigationThrottle::BLOCK_REQUEST, - throttle->WillStartRequest().action()) << url; + throttle->WillStartRequest().action()) + << url; GURL url2("mailto:example@www.example.com"); test_handle.set_url(url2); EXPECT_EQ(NavigationThrottle::BLOCK_REQUEST, - throttle->WillStartRequest().action()) << url2; + throttle->WillStartRequest().action()) + << url2; GURL url3("magnet:?xt=urn:btih:***.torrent"); test_handle.set_url(url3); EXPECT_EQ(NavigationThrottle::BLOCK_REQUEST, - throttle->WillStartRequest().action()) << url3; + throttle->WillStartRequest().action()) + << url3; } TEST_F(TorNavigationThrottleUnitTest, DeferUntilTorProcessLaunched) { content::MockNavigationHandle test_handle(tor_web_contents()); std::unique_ptr throttle = - TorNavigationThrottle::MaybeCreateThrottleFor(&test_handle); + TorNavigationThrottle::MaybeCreateThrottleFor( + &test_handle, tor_profile_service(), + brave::IsTorProfile(tor_web_contents()->GetBrowserContext())); bool was_navigation_resumed = false; throttle->set_resume_callback_for_testing( base::BindLambdaForTesting([&]() { was_navigation_resumed = true; })); diff --git a/browser/tor/tor_profile_service.cc b/browser/tor/tor_profile_service.cc deleted file mode 100644 index 4cb42490a02f..000000000000 --- a/browser/tor/tor_profile_service.cc +++ /dev/null @@ -1,76 +0,0 @@ -/* 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/tor/tor_profile_service.h" - -#include - -#include "brave/browser/brave_browser_process_impl.h" -// TODO(bridiver) - move this out of extensions -#include "brave/browser/extensions/brave_tor_client_updater.h" -#include "brave/browser/tor/tor_launcher_service_observer.h" -#include "brave/common/tor/pref_names.h" -#include "components/pref_registry/pref_registry_syncable.h" -#include "components/prefs/pref_registry_simple.h" -#include "components/prefs/pref_service.h" - -namespace tor { - -TorProfileService::TorProfileService() { -} - -TorProfileService::~TorProfileService() { -} - -// static -void TorProfileService::RegisterLocalStatePrefs(PrefRegistrySimple* registry) { - registry->RegisterBooleanPref(prefs::kTorDisabled, false); -} - -// static -void TorProfileService::RegisterPrefs(PrefRegistrySimple* registry) { - registry->RegisterBooleanPref(prefs::kAutoOnionLocation, false); -} - -// static -bool TorProfileService::IsTorDisabled() { - if (!g_brave_browser_process) - return false; - return g_browser_process->local_state()->GetBoolean(prefs::kTorDisabled); -} - -// static -void TorProfileService::SetTorDisabled(bool disabled) { - if (g_brave_browser_process) - g_browser_process->local_state()->SetBoolean(prefs::kTorDisabled, disabled); -} - -// static -void TorProfileService::RegisterTorClientUpdater() { - if (g_brave_browser_process) { - g_brave_browser_process->tor_client_updater()->Register(); - } -} - -// static -void TorProfileService::UnregisterTorClientUpdater() { - if (g_brave_browser_process) { - g_brave_browser_process->tor_client_updater()->Unregister(); - } -} - -base::FilePath TorProfileService::GetTorExecutablePath() { - return g_brave_browser_process->tor_client_updater()->GetExecutablePath(); -} - -void TorProfileService::AddObserver(TorLauncherServiceObserver* observer) { - observers_.AddObserver(observer); -} - -void TorProfileService::RemoveObserver(TorLauncherServiceObserver* observer) { - observers_.RemoveObserver(observer); -} - -} // namespace tor diff --git a/browser/tor/tor_profile_service_factory.cc b/browser/tor/tor_profile_service_factory.cc index 700a66f40c29..1c7b79e9ee7e 100644 --- a/browser/tor/tor_profile_service_factory.cc +++ b/browser/tor/tor_profile_service_factory.cc @@ -8,34 +8,32 @@ #include #include -#include "brave/browser/tor/tor_profile_service_impl.h" -#include "brave/browser/tor/buildflags.h" -#include "brave/common/tor/pref_names.h" -#include "chrome/browser/profiles/incognito_helpers.h" -#include "chrome/browser/profiles/profile.h" +#include "base/path_service.h" +#include "brave/browser/brave_browser_process_impl.h" +#include "brave/browser/profiles/profile_util.h" +#include "brave/components/tor/pref_names.h" +#include "brave/components/tor/tor_profile_service_impl.h" +#include "chrome/common/chrome_paths.h" #include "components/keyed_service/content/browser_context_dependency_manager.h" #include "components/prefs/pref_service.h" - -#if BUILDFLAG(ENABLE_TOR) -#include "brave/browser/profiles/profile_util.h" -#endif +#include "content/public/browser/browser_context.h" namespace { -std::set g_profile_set; +std::set g_context_set; } // static -tor::TorProfileService* TorProfileServiceFactory::GetForProfile( - Profile* profile) { - return GetForProfile(profile, true); +tor::TorProfileService* TorProfileServiceFactory::GetForContext( + content::BrowserContext* context) { + return GetForContext(context, true); } // static -tor::TorProfileService* TorProfileServiceFactory::GetForProfile( - Profile* profile, - bool create) { +tor::TorProfileService* TorProfileServiceFactory::GetForContext( + content::BrowserContext* context, + bool create) { return static_cast( - GetInstance()->GetServiceForBrowserContext(profile, create)); + GetInstance()->GetServiceForBrowserContext(context, create)); } // static @@ -43,61 +41,77 @@ TorProfileServiceFactory* TorProfileServiceFactory::GetInstance() { return base::Singleton::get(); } +// static +void TorProfileServiceFactory::SetTorDisabled(bool disabled) { + if (g_brave_browser_process) + g_brave_browser_process->local_state()->SetBoolean(tor::prefs::kTorDisabled, + disabled); +} + +// static +bool TorProfileServiceFactory::IsTorDisabled() { + if (g_brave_browser_process) + return g_brave_browser_process->local_state()->GetBoolean( + tor::prefs::kTorDisabled); + return false; +} + TorProfileServiceFactory::TorProfileServiceFactory() : BrowserContextKeyedServiceFactory( "TorProfileService", BrowserContextDependencyManager::GetInstance()) { - g_profile_set.clear(); + g_context_set.clear(); } TorProfileServiceFactory::~TorProfileServiceFactory() {} KeyedService* TorProfileServiceFactory::BuildServiceInstanceFor( content::BrowserContext* context) const { -#if BUILDFLAG(ENABLE_TOR) - Profile* profile = Profile::FromBrowserContext(context); + if (!brave::IsTorProfile(context)) + return nullptr; + base::FilePath user_data_dir; + base::PathService::Get(chrome::DIR_USER_DATA, &user_data_dir); + DCHECK(!user_data_dir.empty()); std::unique_ptr tor_profile_service( - new tor::TorProfileServiceImpl(profile)); + new tor::TorProfileServiceImpl( + context, + g_brave_browser_process + ? g_brave_browser_process->tor_client_updater() + : nullptr, + user_data_dir)); // We only care about Tor incognito profiles for deciding whether to KillTor. if (context->IsOffTheRecord()) { - g_profile_set.emplace(profile); + g_context_set.emplace(context); } return tor_profile_service.release(); -#else - return nullptr; -#endif } content::BrowserContext* TorProfileServiceFactory::GetBrowserContextToUse( content::BrowserContext* context) const { // Not shared with our dummy regular Tor profile because we want to trigger // LaunchTor when a new Tor window is created. - return chrome::GetBrowserContextOwnInstanceInIncognito(context); + return context; } void TorProfileServiceFactory::BrowserContextShutdown( content::BrowserContext* context) { -#if BUILDFLAG(ENABLE_TOR) // KillTor when the last Tor incognito profile is shutting down. - if (g_profile_set.size() == 1) { + if (g_context_set.size() == 1) { auto* service = static_cast( - TorProfileServiceFactory::GetForProfile( - Profile::FromBrowserContext(context), false)); + TorProfileServiceFactory::GetForContext(context, false)); if (service) { service->KillTor(); } else { DCHECK(!brave::IsTorProfile(context)); } } -#endif BrowserContextKeyedServiceFactory::BrowserContextShutdown(context); } void TorProfileServiceFactory::BrowserContextDestroyed( content::BrowserContext* context) { - Profile* profile = Profile::FromBrowserContext(context); - g_profile_set.erase(profile); + 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 845948de9a1b..0e996c1a2538 100644 --- a/browser/tor/tor_profile_service_factory.h +++ b/browser/tor/tor_profile_service_factory.h @@ -9,8 +9,6 @@ #include "base/memory/singleton.h" #include "components/keyed_service/content/browser_context_keyed_service_factory.h" -class Profile; - namespace tor { class TorProfileService; } // namespace tor @@ -19,13 +17,18 @@ class TorProfileService; // Profiles. class TorProfileServiceFactory : public BrowserContextKeyedServiceFactory { public: - static tor::TorProfileService* GetForProfile(Profile* profile); + static tor::TorProfileService* GetForContext( + content::BrowserContext* context); static TorProfileServiceFactory* GetInstance(); + static void SetTorDisabled(bool disabled); + static bool IsTorDisabled(); + private: friend struct base::DefaultSingletonTraits; - static tor::TorProfileService* GetForProfile(Profile* profile, bool create); + static tor::TorProfileService* GetForContext(content::BrowserContext* context, + bool create); TorProfileServiceFactory(); ~TorProfileServiceFactory() override; diff --git a/browser/ui/BUILD.gn b/browser/ui/BUILD.gn index 444577337002..646db01cf836 100644 --- a/browser/ui/BUILD.gn +++ b/browser/ui/BUILD.gn @@ -5,6 +5,7 @@ import("//brave/components/brave_wallet/buildflags/buildflags.gni") import("//brave/components/brave_wayback_machine/buildflags/buildflags.gni") import("//brave/components/ipfs/buildflags/buildflags.gni") import("//brave/components/speedreader/buildflags.gni") +import("//brave/components/tor/buildflags/buildflags.gni") import("//build/config/features.gni") import("//components/gcm_driver/config.gni") import("//chrome/common/features.gni") @@ -198,7 +199,6 @@ source_set("ui") { "//brave/chromium_src/chrome/browser/ui", "//brave/common", "//brave/common:pref_names", - "//brave/common/tor:pref_names", "//brave/components/brave_adblock_ui:generated_resources", "//brave/components/webcompat_reporter/ui:generated_resources", "//brave/components/brave_ads/browser", @@ -215,6 +215,7 @@ source_set("ui") { "//brave/components/ntp_background_images/common", "//brave/components/ntp_widget_utils/browser", "//brave/components/p3a:buildflags", + "//brave/components/tor:pref_names", "//brave/components/vector_icons", "//brave/components/webcompat_reporter/browser", "//brave/components/weekly_storage", @@ -293,8 +294,6 @@ source_set("ui") { "views/location_bar/brave_location_bar_view.h", "views/location_bar/brave_star_view.cc", "views/location_bar/brave_star_view.h", - "views/location_bar/onion_location_view.cc", - "views/location_bar/onion_location_view.h", "views/profiles/brave_avatar_toolbar_button.cc", "views/profiles/brave_avatar_toolbar_button.h", "views/tabs/brave_alert_indicator.cc", @@ -319,6 +318,13 @@ source_set("ui") { ] } + if (enable_tor) { + sources += [ + "views/location_bar/onion_location_view.cc", + "views/location_bar/onion_location_view.h", + ] + } + deps += [ "//brave/browser/resources/extensions:resources", "//brave/components/brave_extension:generated_resources", diff --git a/browser/ui/brave_browser_command_controller.cc b/browser/ui/brave_browser_command_controller.cc index a210cd5bcd3e..8d5d820e0946 100644 --- a/browser/ui/brave_browser_command_controller.cc +++ b/browser/ui/brave_browser_command_controller.cc @@ -9,7 +9,6 @@ #include "brave/app/brave_command_ids.h" #include "brave/browser/profiles/profile_util.h" -#include "brave/browser/tor/buildflags.h" #include "brave/browser/ui/brave_pages.h" #include "brave/browser/ui/browser_commands.h" #include "brave/common/pref_names.h" diff --git a/browser/ui/brave_browser_command_controller.h b/browser/ui/brave_browser_command_controller.h index 5b20cc8f189b..03f5ba092393 100644 --- a/browser/ui/brave_browser_command_controller.h +++ b/browser/ui/brave_browser_command_controller.h @@ -6,7 +6,7 @@ #ifndef BRAVE_BROWSER_UI_BRAVE_BROWSER_COMMAND_CONTROLLER_H_ #define BRAVE_BROWSER_UI_BRAVE_BROWSER_COMMAND_CONTROLLER_H_ -#include "brave/browser/tor/buildflags.h" +#include "brave/components/tor/buildflags/buildflags.h" #include "chrome/browser/ui/browser_command_controller.h" // This namespace is needed for a chromium_src override diff --git a/browser/ui/brave_browser_command_controller_browsertest.cc b/browser/ui/brave_browser_command_controller_browsertest.cc index e574ff36e4b0..8324d258d154 100644 --- a/browser/ui/brave_browser_command_controller_browsertest.cc +++ b/browser/ui/brave_browser_command_controller_browsertest.cc @@ -6,12 +6,12 @@ #include #include "brave/browser/profiles/profile_util.h" -#include "brave/browser/tor/buildflags.h" #include "brave/browser/ui/brave_browser_command_controller.h" #include "brave/browser/ui/browser_commands.h" #include "brave/components/brave_rewards/browser/buildflags/buildflags.h" #include "brave/components/brave_sync/buildflags/buildflags.h" #include "brave/components/brave_wallet/buildflags/buildflags.h" +#include "brave/components/tor/buildflags/buildflags.h" #include "chrome/app/chrome_command_ids.h" #include "chrome/browser/chrome_notification_types.h" #include "chrome/browser/profiles/profile.h" @@ -23,14 +23,14 @@ #include "content/public/test/browser_test.h" #include "content/public/test/test_utils.h" -#if BUILDFLAG(ENABLE_TOR) -#include "brave/browser/tor/tor_profile_service.h" -#endif - #if BUILDFLAG(ENABLE_BRAVE_SYNC) #include "components/sync/driver/sync_driver_switches.h" #endif +#if BUILDFLAG(ENABLE_TOR) +#include "brave/browser/tor/tor_profile_service_factory.h" +#endif + using BraveBrowserCommandControllerTest = InProcessBrowserTest; IN_PROC_BROWSER_TEST_F(BraveBrowserCommandControllerTest, @@ -155,6 +155,7 @@ IN_PROC_BROWSER_TEST_F(BraveBrowserCommandControllerTest, EXPECT_TRUE(command_controller->IsCommandEnabled( IDC_SHOW_BRAVE_WEBCOMPAT_REPORTER)); +#if BUILDFLAG(ENABLE_TOR) // Launch tor window and check its command status. content::WindowedNotificationObserver tor_browser_creation_observer( chrome::NOTIFICATION_BROWSER_OPENED, @@ -176,12 +177,10 @@ IN_PROC_BROWSER_TEST_F(BraveBrowserCommandControllerTest, EXPECT_TRUE(command_controller->IsCommandEnabled(IDC_SHOW_BRAVE_ADBLOCK)); -#if BUILDFLAG(ENABLE_TOR) EXPECT_TRUE( command_controller->IsCommandEnabled(IDC_NEW_TOR_CONNECTION_FOR_SITE)); EXPECT_TRUE( command_controller->IsCommandEnabled(IDC_NEW_OFFTHERECORD_WINDOW_TOR)); -#endif #if BUILDFLAG(ENABLE_BRAVE_SYNC) if (switches::IsSyncAllowedByFlag()) @@ -200,8 +199,7 @@ IN_PROC_BROWSER_TEST_F(BraveBrowserCommandControllerTest, IDC_SHOW_BRAVE_WEBCOMPAT_REPORTER)); // Check tor commands when tor is disabled. -#if BUILDFLAG(ENABLE_TOR) - tor::TorProfileService::SetTorDisabled(true); + TorProfileServiceFactory::SetTorDisabled(true); command_controller = browser()->command_controller(); EXPECT_FALSE( command_controller->IsCommandEnabled(IDC_NEW_TOR_CONNECTION_FOR_SITE)); diff --git a/browser/ui/browser_commands.cc b/browser/ui/browser_commands.cc index a8d247aab4e9..d1a48c7ca5eb 100644 --- a/browser/ui/browser_commands.cc +++ b/browser/ui/browser_commands.cc @@ -6,9 +6,8 @@ #include "brave/browser/ui/browser_commands.h" #include "brave/browser/profiles/profile_util.h" -#include "brave/browser/tor/tor_profile_service.h" -#include "brave/browser/tor/tor_profile_service_factory.h" #include "brave/components/speedreader/buildflags.h" +#include "brave/components/tor/buildflags/buildflags.h" #include "chrome/browser/browser_process.h" #include "chrome/browser/profiles/profile.h" #include "chrome/browser/profiles/profile_manager.h" @@ -26,6 +25,11 @@ #include "brave/components/speedreader/speedreader_service.h" #endif +#if BUILDFLAG(ENABLE_TOR) +#include "brave/browser/tor/tor_profile_service_factory.h" +#include "brave/components/tor/tor_profile_service.h" +#endif + using content::WebContents; namespace { @@ -43,16 +47,18 @@ void NewOffTheRecordWindowTor(Browser* browser) { } void NewTorConnectionForSite(Browser* browser) { +#if BUILDFLAG(ENABLE_TOR) Profile* profile = browser->profile(); DCHECK(profile); tor::TorProfileService* service = - TorProfileServiceFactory::GetForProfile(profile); + TorProfileServiceFactory::GetForContext(profile); DCHECK(service); WebContents* current_tab = browser->tab_strip_model()->GetActiveWebContents(); if (!current_tab) return; service->SetNewTorCircuit(current_tab); +#endif } void AddNewProfile() { diff --git a/browser/ui/toolbar/brave_app_menu_model_browsertest.cc b/browser/ui/toolbar/brave_app_menu_model_browsertest.cc index 172f74eafd18..45fd3dbc1100 100644 --- a/browser/ui/toolbar/brave_app_menu_model_browsertest.cc +++ b/browser/ui/toolbar/brave_app_menu_model_browsertest.cc @@ -9,12 +9,12 @@ #include #include "brave/browser/profiles/profile_util.h" -#include "brave/browser/tor/buildflags.h" #include "brave/browser/ui/brave_browser_command_controller.h" #include "brave/browser/ui/browser_commands.h" #include "brave/components/brave_rewards/browser/buildflags/buildflags.h" #include "brave/components/brave_sync/buildflags/buildflags.h" #include "brave/components/brave_wallet/buildflags/buildflags.h" +#include "brave/components/tor/buildflags/buildflags.h" #include "chrome/app/chrome_command_ids.h" #include "chrome/browser/chrome_notification_types.h" #include "chrome/browser/profiles/profile.h" @@ -197,6 +197,7 @@ IN_PROC_BROWSER_TEST_F(BraveAppMenuBrowserTest, MenuOrderTest) { CheckCommandsAreDisabledInMenuModel(guest_browser, commands_disabled_for_guest_profile); +#if BUILDFLAG(ENABLE_TOR) content::WindowedNotificationObserver tor_browser_creation_observer( chrome::NOTIFICATION_BROWSER_OPENED, content::NotificationService::AllSources()); @@ -215,9 +216,7 @@ IN_PROC_BROWSER_TEST_F(BraveAppMenuBrowserTest, MenuOrderTest) { IDC_NEW_TOR_CONNECTION_FOR_SITE, IDC_NEW_WINDOW, IDC_NEW_INCOGNITO_WINDOW, -#if BUILDFLAG(ENABLE_TOR) IDC_NEW_OFFTHERECORD_WINDOW_TOR, -#endif #if BUILDFLAG(BRAVE_REWARDS_ENABLED) IDC_SHOW_BRAVE_REWARDS, #endif @@ -251,4 +250,5 @@ IN_PROC_BROWSER_TEST_F(BraveAppMenuBrowserTest, MenuOrderTest) { commands_in_order_for_tor_profile); CheckCommandsAreDisabledInMenuModel(tor_browser, commands_disabled_for_tor_profile); +#endif } diff --git a/browser/ui/views/location_bar/brave_location_bar_view.cc b/browser/ui/views/location_bar/brave_location_bar_view.cc index e1f6a3e636b3..fc1df2315ff5 100644 --- a/browser/ui/views/location_bar/brave_location_bar_view.cc +++ b/browser/ui/views/location_bar/brave_location_bar_view.cc @@ -10,7 +10,6 @@ #include "brave/browser/profiles/profile_util.h" #include "brave/browser/themes/brave_theme_service.h" #include "brave/browser/ui/views/brave_actions/brave_actions_container.h" -#include "brave/browser/ui/views/location_bar/onion_location_view.h" #include "brave/browser/ui/views/toolbar/brave_toolbar_view.h" #include "chrome/browser/profiles/profile.h" #include "chrome/browser/themes/theme_service_factory.h" @@ -21,6 +20,10 @@ #include "components/version_info/channel.h" #include "ui/views/controls/highlight_path_generator.h" +#if BUILDFLAG(ENABLE_TOR) +#include "brave/browser/ui/views/location_bar/onion_location_view.h" +#endif + namespace { class BraveLocationBarViewFocusRingHighlightPathGenerator @@ -49,11 +52,13 @@ void BraveLocationBarView::Init() { std::make_unique< BraveLocationBarViewFocusRingHighlightPathGenerator>()); } - // brave action buttons +#if BUILDFLAG(ENABLE_TOR) onion_location_view_ = new OnionLocationView(browser_->profile()); + AddChildView(onion_location_view_); +#endif + // brave action buttons brave_actions_ = new BraveActionsContainer(browser_, profile()); brave_actions_->Init(); - AddChildView(onion_location_view_); AddChildView(brave_actions_); // Call Update again to cause a Layout Update(nullptr); @@ -69,8 +74,10 @@ void BraveLocationBarView::Update(content::WebContents* contents) { if (brave_actions_) { brave_actions_->Update(); } +#if BUILDFLAG(ENABLE_TOR) if (onion_location_view_) onion_location_view_->Update(contents); +#endif LocationBarView::Update(contents); } @@ -81,9 +88,11 @@ void BraveLocationBarView::OnChanged() { ShouldHidePageActionIcons() && !omnibox_view_->GetText().empty(); brave_actions_->SetShouldHide(should_hide); } +#if BUILDFLAG(ENABLE_TOR) if (onion_location_view_) onion_location_view_->Update( browser_->tab_strip_model()->GetActiveWebContents()); +#endif // OnChanged calls Layout LocationBarView::OnChanged(); @@ -91,8 +100,10 @@ void BraveLocationBarView::OnChanged() { std::vector BraveLocationBarView::GetTrailingViews() { std::vector views; +#if BUILDFLAG(ENABLE_TOR) if (onion_location_view_) views.push_back(onion_location_view_); +#endif if (brave_actions_) views.push_back(brave_actions_); @@ -107,11 +118,13 @@ gfx::Size BraveLocationBarView::CalculatePreferredSize() const { GetLayoutConstant(LOCATION_BAR_ELEMENT_PADDING); min_size.Enlarge(extra_width, 0); } +#if BUILDFLAG(ENABLE_TOR) if (onion_location_view_ && onion_location_view_->GetVisible()) { const int extra_width = GetLayoutConstant(LOCATION_BAR_ELEMENT_PADDING) + onion_location_view_->GetMinimumSize().width(); min_size.Enlarge(extra_width, 0); } +#endif return min_size; } diff --git a/browser/ui/views/location_bar/brave_location_bar_view.h b/browser/ui/views/location_bar/brave_location_bar_view.h index 539b2752b6d4..42241a275aaf 100644 --- a/browser/ui/views/location_bar/brave_location_bar_view.h +++ b/browser/ui/views/location_bar/brave_location_bar_view.h @@ -8,14 +8,18 @@ #include +#include "brave/components/tor/buildflags/buildflags.h" #include "chrome/browser/ui/views/location_bar/location_bar_view.h" class BraveActionsContainer; class BraveActionsContainerTest; -class OnionLocationView; class RewardsBrowserTest; class SkPath; +#if BUILDFLAG(ENABLE_TOR) +class OnionLocationView; +#endif + // The purposes of this subclass are to: // - Add the BraveActionsContainer to the location bar class BraveLocationBarView : public LocationBarView { @@ -25,7 +29,9 @@ class BraveLocationBarView : public LocationBarView { void Update(content::WebContents* contents) override; void OnChanged() override; BraveActionsContainer* GetBraveActionsContainer() { return brave_actions_; } +#if BUILDFLAG(ENABLE_TOR) OnionLocationView* GetOnionLocationView() { return onion_location_view_; } +#endif // LocationBarView: std::vector GetTrailingViews() override; @@ -44,7 +50,9 @@ class BraveLocationBarView : public LocationBarView { friend class ::BraveActionsContainerTest; friend class ::RewardsBrowserTest; BraveActionsContainer* brave_actions_ = nullptr; +#if BUILDFLAG(ENABLE_TOR) OnionLocationView* onion_location_view_ = nullptr; +#endif DISALLOW_COPY_AND_ASSIGN(BraveLocationBarView); }; diff --git a/browser/ui/views/location_bar/onion_location_view.cc b/browser/ui/views/location_bar/onion_location_view.cc index 5f7cbac451de..c1199a092ac0 100644 --- a/browser/ui/views/location_bar/onion_location_view.cc +++ b/browser/ui/views/location_bar/onion_location_view.cc @@ -11,7 +11,7 @@ #include "base/strings/utf_string_conversions.h" #include "brave/app/vector_icons/vector_icons.h" #include "brave/browser/profiles/profile_util.h" -#include "brave/browser/tor/onion_location_tab_helper.h" +#include "brave/components/tor/onion_location_tab_helper.h" #include "brave/grit/brave_generated_resources.h" #include "chrome/browser/profiles/profile_window.h" #include "chrome/browser/ui/browser.h" diff --git a/browser/ui/views/profiles/brave_incognito_menu_view.cc b/browser/ui/views/profiles/brave_incognito_menu_view.cc index 787bbbe951b3..18f31d4bfad8 100644 --- a/browser/ui/views/profiles/brave_incognito_menu_view.cc +++ b/browser/ui/views/profiles/brave_incognito_menu_view.cc @@ -10,7 +10,7 @@ #include "brave/browser/brave_browser_process_impl.h" #include "brave/browser/profiles/profile_util.h" -#include "brave/browser/tor/buildflags.h" +#include "brave/components/tor/buildflags/buildflags.h" #include "brave/grit/brave_generated_resources.h" #include "chrome/app/vector_icons/vector_icons.h" #include "chrome/browser/profiles/profile.h" @@ -25,8 +25,8 @@ #include "ui/base/models/image_model.h" #if BUILDFLAG(ENABLE_TOR) -#include "brave/browser/extensions/brave_tor_client_updater.h" -#include "brave/browser/tor/tor_profile_service.h" +#include "brave/browser/tor/tor_profile_service_factory.h" +#include "brave/components/tor/brave_tor_client_updater.h" #endif namespace { @@ -34,7 +34,7 @@ namespace { bool ShouldShowTorProfileButton(Profile* profile) { DCHECK(profile); #if BUILDFLAG(ENABLE_TOR) - return !tor::TorProfileService::IsTorDisabled() && + return !TorProfileServiceFactory::IsTorDisabled() && !brave::IsTorProfile(profile) && !g_brave_browser_process->tor_client_updater() ->GetExecutablePath() diff --git a/browser/ui/webui/brave_new_tab_message_handler.cc b/browser/ui/webui/brave_new_tab_message_handler.cc index f290f86d9e53..232bd04c058b 100644 --- a/browser/ui/webui/brave_new_tab_message_handler.cc +++ b/browser/ui/webui/brave_new_tab_message_handler.cc @@ -13,8 +13,6 @@ #include "base/values.h" #include "brave/browser/profiles/profile_util.h" #include "brave/browser/search_engines/search_engine_provider_util.h" -#include "brave/browser/tor/tor_profile_service.h" -#include "brave/browser/tor/tor_profile_service_factory.h" #include "brave/browser/ui/webui/brave_new_tab_ui.h" #include "brave/browser/ntp_background_images/view_counter_service_factory.h" #include "brave/common/pref_names.h" @@ -52,6 +50,11 @@ using ntp_background_images::ViewCounterServiceFactory; #include "brave/components/crypto_dot_com/common/pref_names.h" #endif +#if BUILDFLAG(ENABLE_TOR) +#include "brave/browser/tor/tor_profile_service_factory.h" +#include "brave/components/tor/tor_profile_service.h" +#endif + namespace { bool IsPrivateNewTab(Profile* profile) { @@ -179,14 +182,18 @@ BraveNewTabMessageHandler* BraveNewTabMessageHandler::Create( BraveNewTabMessageHandler::BraveNewTabMessageHandler(Profile* profile) : profile_(profile) { +#if BUILDFLAG(ENABLE_TOR) if (brave::IsTorProfile(profile)) { - tor_profile_service_ = TorProfileServiceFactory::GetForProfile(profile); + tor_profile_service_ = TorProfileServiceFactory::GetForContext(profile); } +#endif } BraveNewTabMessageHandler::~BraveNewTabMessageHandler() { +#if BUILDFLAG(ENABLE_TOR) if (tor_profile_service_) tor_profile_service_->RemoveObserver(this); +#endif } void BraveNewTabMessageHandler::RegisterMessages() { @@ -318,14 +325,18 @@ void BraveNewTabMessageHandler::OnJavascriptAllowed() { base::Unretained(this))); #endif +#if BUILDFLAG(ENABLE_TOR) if (tor_profile_service_) tor_profile_service_->AddObserver(this); +#endif } void BraveNewTabMessageHandler::OnJavascriptDisallowed() { pref_change_registrar_.RemoveAll(); +#if BUILDFLAG(ENABLE_TOR) if (tor_profile_service_) tor_profile_service_->RemoveObserver(this); +#endif } void BraveNewTabMessageHandler::HandleGetPreferences( @@ -354,9 +365,13 @@ void BraveNewTabMessageHandler::HandleGetPrivateProperties( void BraveNewTabMessageHandler::HandleGetTorProperties( const base::ListValue* args) { AllowJavascript(); +#if BUILDFLAG(ENABLE_TOR) auto data = GetTorPropertiesDictionary( tor_profile_service_ ? tor_profile_service_->IsTorConnected() : false, ""); +#else + auto data = GetTorPropertiesDictionary(false, ""); +#endif ResolveJavascriptCallback(args->GetList()[0], data); } diff --git a/browser/ui/webui/brave_new_tab_message_handler.h b/browser/ui/webui/brave_new_tab_message_handler.h index fc8857c4a752..80d8066dcb1a 100644 --- a/browser/ui/webui/brave_new_tab_message_handler.h +++ b/browser/ui/webui/brave_new_tab_message_handler.h @@ -8,7 +8,8 @@ #include -#include "brave/browser/tor/tor_launcher_service_observer.h" +#include "brave/components/tor/buildflags/buildflags.h" +#include "brave/components/tor/tor_launcher_service_observer.h" #include "components/prefs/pref_change_registrar.h" #include "content/public/browser/web_ui_message_handler.h" @@ -16,9 +17,12 @@ class Profile; namespace content { class WebUIDataSource; } + +#if BUILDFLAG(ENABLE_TOR) namespace tor { class TorProfileService; } // namespace tor +#endif // Handles messages to and from the New Tab Page javascript class BraveNewTabMessageHandler : public content::WebUIMessageHandler, @@ -58,7 +62,9 @@ class BraveNewTabMessageHandler : public content::WebUIMessageHandler, PrefChangeRegistrar pref_change_registrar_; // Weak pointer. Profile* profile_; +#if BUILDFLAG(ENABLE_TOR) tor::TorProfileService* tor_profile_service_ = nullptr; +#endif DISALLOW_COPY_AND_ASSIGN(BraveNewTabMessageHandler); }; diff --git a/browser/ui/webui/settings/brave_default_extensions_handler.cc b/browser/ui/webui/settings/brave_default_extensions_handler.cc index 10f0dd76c9b3..e3c6115cc2c5 100644 --- a/browser/ui/webui/settings/brave_default_extensions_handler.cc +++ b/browser/ui/webui/settings/brave_default_extensions_handler.cc @@ -11,7 +11,6 @@ #include "base/values.h" #include "brave/browser/brave_browser_process_impl.h" #include "brave/browser/extensions/brave_component_loader.h" -#include "brave/browser/tor/buildflags.h" #include "brave/common/pref_names.h" #include "brave/components/brave_webtorrent/grit/brave_webtorrent_resources.h" #include "chrome/browser/about_flags.h" @@ -36,8 +35,8 @@ #include "extensions/common/feature_switch.h" #if BUILDFLAG(ENABLE_TOR) -#include "brave/browser/tor/tor_profile_service.h" -#include "brave/common/tor/pref_names.h" +#include "brave/browser/tor/tor_profile_service_factory.h" +#include "brave/components/tor/pref_names.h" #endif #if BUILDFLAG(BRAVE_WALLET_ENABLED) @@ -84,7 +83,6 @@ void BraveDefaultExtensionsHandler::RegisterMessages() { "getRestartNeeded", base::BindRepeating(&BraveDefaultExtensionsHandler::GetRestartNeeded, base::Unretained(this))); -#if BUILDFLAG(ENABLE_TOR) web_ui()->RegisterMessageCallback( "setTorEnabled", base::BindRepeating( @@ -98,7 +96,6 @@ void BraveDefaultExtensionsHandler::RegisterMessages() { "isTorManaged", base::BindRepeating(&BraveDefaultExtensionsHandler::IsTorManaged, base::Unretained(this))); -#endif // Can't call this in ctor because it needs to access web_ui(). InitializePrefCallbacks(); @@ -234,13 +231,14 @@ void BraveDefaultExtensionsHandler::SetMediaRouterEnabled( about_flags::SetFeatureEntryEnabled(&flags_storage, feature_name, true); } -#if BUILDFLAG(ENABLE_TOR) void BraveDefaultExtensionsHandler::SetTorEnabled(const base::ListValue* args) { +#if BUILDFLAG(ENABLE_TOR) CHECK_EQ(args->GetSize(), 1U); bool enabled; args->GetBoolean(0, &enabled); AllowJavascript(); - tor::TorProfileService::SetTorDisabled(!enabled); + TorProfileServiceFactory::SetTorDisabled(!enabled); +#endif } void BraveDefaultExtensionsHandler::IsTorEnabled( @@ -249,14 +247,22 @@ void BraveDefaultExtensionsHandler::IsTorEnabled( AllowJavascript(); ResolveJavascriptCallback( args->GetList()[0], - base::Value(!tor::TorProfileService::IsTorDisabled())); +#if BUILDFLAG(ENABLE_TOR) + base::Value(!TorProfileServiceFactory::IsTorDisabled())); +#else + base::Value(false)); +#endif } void BraveDefaultExtensionsHandler::OnTorEnabledChanged() { if (IsJavascriptAllowed()) { FireWebUIListener( "tor-enabled-changed", - base::Value(!tor::TorProfileService::IsTorDisabled())); +#if BUILDFLAG(ENABLE_TOR) + base::Value(!TorProfileServiceFactory::IsTorDisabled())); +#else + base::Value(false)); +#endif } } @@ -264,13 +270,16 @@ void BraveDefaultExtensionsHandler::IsTorManaged( const base::ListValue* args) { CHECK_EQ(args->GetSize(), 1U); +#if BUILDFLAG(ENABLE_TOR) const bool is_managed = g_brave_browser_process->local_state()-> FindPreference(tor::prefs::kTorDisabled)->IsManaged(); +#else + const bool is_managed = false; +#endif AllowJavascript(); ResolveJavascriptCallback(args->GetList()[0], base::Value(is_managed)); } -#endif void BraveDefaultExtensionsHandler::SetIPFSCompanionEnabled( const base::ListValue* args) { diff --git a/browser/ui/webui/settings/brave_default_extensions_handler.h b/browser/ui/webui/settings/brave_default_extensions_handler.h index 1cdd340edc51..dd9e5aa2490b 100644 --- a/browser/ui/webui/settings/brave_default_extensions_handler.h +++ b/browser/ui/webui/settings/brave_default_extensions_handler.h @@ -9,8 +9,8 @@ #include #include "base/memory/weak_ptr.h" -#include "brave/browser/tor/buildflags.h" #include "brave/components/brave_wallet/buildflags/buildflags.h" +#include "brave/components/tor/buildflags/buildflags.h" #include "chrome/browser/ui/webui/settings/settings_page_ui_handler.h" #include "chrome/common/extensions/webstore_install_result.h" #include "components/prefs/pref_change_registrar.h" @@ -36,12 +36,10 @@ class BraveDefaultExtensionsHandler : public settings::SettingsPageUIHandler { #if BUILDFLAG(BRAVE_WALLET_ENABLED) void SetBraveWalletEnabled(const base::ListValue* args); #endif -#if BUILDFLAG(ENABLE_TOR) void SetTorEnabled(const base::ListValue* args); void IsTorEnabled(const base::ListValue* args); void OnTorEnabledChanged(); void IsTorManaged(const base::ListValue* args); -#endif void InitializePrefCallbacks(); diff --git a/chromium_src/chrome/browser/net/proxy_config_monitor.cc b/chromium_src/chrome/browser/net/proxy_config_monitor.cc index 513cc914aa43..cc5a20d0ac82 100644 --- a/chromium_src/chrome/browser/net/proxy_config_monitor.cc +++ b/chromium_src/chrome/browser/net/proxy_config_monitor.cc @@ -6,12 +6,12 @@ #include #include "brave/browser/profiles/profile_util.h" -#include "brave/browser/tor/buildflags.h" +#include "brave/components/tor/buildflags/buildflags.h" #if BUILDFLAG(ENABLE_TOR) #include "net/proxy_resolution/proxy_config_service.h" -#include "brave/browser/tor/tor_profile_service.h" #include "brave/browser/tor/tor_profile_service_factory.h" +#include "brave/components/tor/tor_profile_service.h" #endif namespace { @@ -19,7 +19,7 @@ namespace { #if BUILDFLAG(ENABLE_TOR) std::unique_ptr CreateProxyConfigServiceTor( Profile* profile) { - auto* tor_service = TorProfileServiceFactory::GetForProfile(profile); + auto* tor_service = TorProfileServiceFactory::GetForContext(profile); DCHECK(tor_service); return tor_service->CreateProxyConfigService(); } diff --git a/chromium_src/chrome/browser/policy/configuration_policy_handler_list_factory.cc b/chromium_src/chrome/browser/policy/configuration_policy_handler_list_factory.cc index d5745c1ce06c..feb9bf467347 100644 --- a/chromium_src/chrome/browser/policy/configuration_policy_handler_list_factory.cc +++ b/chromium_src/chrome/browser/policy/configuration_policy_handler_list_factory.cc @@ -4,9 +4,9 @@ * You can obtain one at http://mozilla.org/MPL/2.0/. */ #include "base/stl_util.h" -#include "brave/browser/tor/buildflags.h" #include "brave/common/pref_names.h" -#include "brave/common/tor/pref_names.h" +#include "brave/components/tor/buildflags/buildflags.h" +#include "brave/components/tor/pref_names.h" #include "chrome/browser/policy/configuration_policy_handler_list_factory.h" #include "components/policy/core/browser/configuration_policy_handler.h" #include "components/policy/policy_constants.h" diff --git a/chromium_src/chrome/browser/profiles/pref_service_builder_utils.cc b/chromium_src/chrome/browser/profiles/pref_service_builder_utils.cc index d980ba0e41bd..4d0a856ab731 100644 --- a/chromium_src/chrome/browser/profiles/pref_service_builder_utils.cc +++ b/chromium_src/chrome/browser/profiles/pref_service_builder_utils.cc @@ -8,10 +8,10 @@ #include "brave/browser/brave_profile_prefs.h" #include "brave/browser/profiles/brave_profile_impl.h" #include "brave/browser/profiles/profile_util.h" -#include "brave/browser/tor/buildflags.h" #include "brave/common/pref_names.h" #include "brave/components/brave_ads/browser/ads_service.h" #include "brave/components/brave_rewards/browser/rewards_service.h" +#include "brave/components/tor/buildflags/buildflags.h" #include "chrome/browser/browser_process.h" #include "chrome/browser/prefs/pref_service_syncable_util.h" #include "chrome/browser/profiles/profile.h" diff --git a/chromium_src/chrome/browser/profiles/profile_window.cc b/chromium_src/chrome/browser/profiles/profile_window.cc index 20185df6e923..5cb9bc45745e 100644 --- a/chromium_src/chrome/browser/profiles/profile_window.cc +++ b/chromium_src/chrome/browser/profiles/profile_window.cc @@ -12,13 +12,17 @@ #include "base/bind.h" #include "base/threading/sequenced_task_runner_handle.h" #include "brave/browser/profiles/brave_profile_manager.h" -#include "brave/browser/tor/tor_profile_service.h" -#include "brave/browser/tor/tor_profile_service_factory.h" +#include "brave/components/tor/buildflags/buildflags.h" +#include "brave/components/tor/tor_profile_service.h" #include "chrome/browser/profiles/profile_attributes_storage.h" #include "chrome/browser/profiles/profile_avatar_icon_util.h" #include "chrome/browser/profiles/profile_manager.h" #include "chrome/browser/profiles/profile_metrics.h" +#if BUILDFLAG(ENABLE_TOR) +#include "brave/browser/tor/tor_profile_service_factory.h" +#endif + namespace profiles { void CreateAndSwitchToNewProfile(ProfileManager::CreateCallback callback, @@ -44,10 +48,12 @@ void OpenBrowserWindowForTorProfile(ProfileManager::CreateCallback callback, profiles::OpenBrowserWindowForProfile( callback, always_create, is_new_profile, unblock_extensions, profile->GetOffTheRecordProfile(), status); +#if BUILDFLAG(ENABLE_TOR) tor::TorProfileService* service = - TorProfileServiceFactory::GetForProfile(profile); + TorProfileServiceFactory::GetForContext(profile); DCHECK(service); service->RegisterTorClientUpdater(); +#endif } void OnTorRegularProfileCreated(ProfileManager::CreateCallback callback, diff --git a/chromium_src/chrome/browser/renderer_context_menu/render_view_context_menu.cc b/chromium_src/chrome/browser/renderer_context_menu/render_view_context_menu.cc index 0b292c5775e5..be16c40b4dd9 100644 --- a/chromium_src/chrome/browser/renderer_context_menu/render_view_context_menu.cc +++ b/chromium_src/chrome/browser/renderer_context_menu/render_view_context_menu.cc @@ -6,15 +6,15 @@ #include "brave/browser/autocomplete/brave_autocomplete_scheme_classifier.h" #include "brave/browser/profiles/profile_util.h" -#include "brave/browser/tor/buildflags.h" -#include "brave/browser/translate/buildflags/buildflags.h" #include "brave/browser/renderer_context_menu/brave_spelling_options_submenu_observer.h" +#include "brave/browser/translate/buildflags/buildflags.h" +#include "brave/components/tor/buildflags/buildflags.h" #include "chrome/browser/autocomplete/chrome_autocomplete_provider_client.h" #include "components/omnibox/browser/autocomplete_classifier.h" #include "components/omnibox/browser/autocomplete_controller.h" #if BUILDFLAG(ENABLE_TOR) -#include "brave/browser/tor/tor_profile_service.h" +#include "brave/browser/tor/tor_profile_service_factory.h" #endif // Our .h file creates a masquerade for RenderViewContextMenu. Switch @@ -115,10 +115,12 @@ void BraveRenderViewContextMenu::AddSpellCheckServiceItem( void BraveRenderViewContextMenu::InitMenu() { RenderViewContextMenu_Chromium::InitMenu(); +#if BUILDFLAG(ENABLE_TOR) || !BUILDFLAG(ENABLE_BRAVE_TRANSLATE_GO) + int index = -1; +#endif #if BUILDFLAG(ENABLE_TOR) // Add Open Link with Tor - int index = -1; - if (!tor::TorProfileService::IsTorDisabled() && + if (!TorProfileServiceFactory::IsTorDisabled() && !params_.link_url.is_empty()) { const Browser* browser = GetBrowser(); const bool is_app = browser && browser->is_type_app(); diff --git a/chromium_src/chrome/browser/service_sandbox_type.h b/chromium_src/chrome/browser/service_sandbox_type.h index c0ee8248fc54..730c315877b1 100644 --- a/chromium_src/chrome/browser/service_sandbox_type.h +++ b/chromium_src/chrome/browser/service_sandbox_type.h @@ -27,18 +27,6 @@ content::GetServiceSandboxType() { #include "brave/components/ipfs/service_sandbox_type.h" #endif -namespace tor { -namespace mojom { -class TorLauncher; -} // namespace mojom -} // namespace tor - -template <> -inline sandbox::policy::SandboxType -content::GetServiceSandboxType() { - return sandbox::policy::SandboxType::kNoSandbox; -} - #if !defined(OS_ANDROID) // Android will use default, which is kUtility. namespace bat_ledger { namespace mojom { diff --git a/chromium_src/chrome/utility/services.cc b/chromium_src/chrome/utility/services.cc index 2de171c751c1..fc6f27371d54 100644 --- a/chromium_src/chrome/utility/services.cc +++ b/chromium_src/chrome/utility/services.cc @@ -4,10 +4,10 @@ * You can obtain one at http://mozilla.org/MPL/2.0/. */ #include "build/build_config.h" -#include "brave/browser/tor/buildflags.h" #include "brave/components/brave_ads/browser/buildflags/buildflags.h" #include "brave/components/brave_rewards/browser/buildflags/buildflags.h" #include "brave/components/ipfs/buildflags/buildflags.h" +#include "brave/components/tor/buildflags/buildflags.h" #if !defined(OS_ANDROID) #include "brave/utility/importer/brave_profile_import_impl.h" diff --git a/common/brave_switches.cc b/common/brave_switches.cc index 5bd92b056ba9..cc04f6c67432 100644 --- a/common/brave_switches.cc +++ b/common/brave_switches.cc @@ -20,10 +20,6 @@ const char kDisableBraveRewardsExtension[] = "disable-brave-rewards-extension"; // This switch disables update module(Sparkle). const char kDisableBraveUpdate[] = "disable-brave-update"; -// Allows disabling the Tor client updater extension. -const char kDisableTorClientUpdaterExtension[] = - "disable-tor-client-updater-extension"; - // Allows disabling the WebTorrent extension. const char kDisableWebTorrentExtension[] = "disable-webtorrent-extension"; diff --git a/common/brave_switches.h b/common/brave_switches.h index 2af4d160c5e7..a19715b6e5d9 100644 --- a/common/brave_switches.h +++ b/common/brave_switches.h @@ -16,8 +16,6 @@ extern const char kDisableBraveRewardsExtension[]; extern const char kDisableBraveUpdate[]; -extern const char kDisableTorClientUpdaterExtension[]; - extern const char kDisableWebTorrentExtension[]; extern const char kDisableBraveWaybackMachineExtension[]; diff --git a/common/tor/BUILD.gn b/common/tor/BUILD.gn deleted file mode 100644 index f73fb125d6c3..000000000000 --- a/common/tor/BUILD.gn +++ /dev/null @@ -1,28 +0,0 @@ -import("//mojo/public/tools/bindings/mojom.gni") - -source_set("tor") { - sources = [ - "tor_common.cc", - "tor_common.h", - "tor_constants.cc", - "tor_constants.h", - "tor_control_event.cc", - "tor_control_event.h", - "tor_control_event_list.h", - ] - - deps = [ - ":pref_names", - "//base", - "//brave/components/services/tor/public/interfaces", - "//chrome/common:constants", - "//url:url", - ] -} - -source_set("pref_names") { - sources = [ - "pref_names.cc", - "pref_names.h", - ] -} diff --git a/common/tor/tor_common.cc b/common/tor/tor_common.cc deleted file mode 100644 index 70169482de3a..000000000000 --- a/common/tor/tor_common.cc +++ /dev/null @@ -1,36 +0,0 @@ -/* 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/common/tor/tor_common.h" - -#include "base/path_service.h" -#include "chrome/common/chrome_paths.h" - -namespace tor { - -TorConfig::TorConfig() {} - -TorConfig::TorConfig(const base::FilePath& binary_path) - : binary_path_(binary_path) { - base::FilePath user_data_dir; - base::PathService::Get(chrome::DIR_USER_DATA, &user_data_dir); - DCHECK(!user_data_dir.empty()); - tor_data_path_ = user_data_dir.Append(FILE_PATH_LITERAL("tor")) - .Append(FILE_PATH_LITERAL("data")); - tor_watch_path_ = user_data_dir.Append(FILE_PATH_LITERAL("tor")) - .Append(FILE_PATH_LITERAL("watch")); -} - -TorConfig::TorConfig(const TorConfig& that) = default; - -TorConfig::~TorConfig() {} - -bool TorConfig::empty() const { - if (binary_path_.empty()) - return true; - return false; -} - -} // namespace tor diff --git a/common/tor/tor_common.h b/common/tor/tor_common.h deleted file mode 100644 index c3e202af2bda..000000000000 --- a/common/tor/tor_common.h +++ /dev/null @@ -1,33 +0,0 @@ -/* 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/. */ - -#ifndef BRAVE_COMMON_TOR_TOR_COMMON_H_ -#define BRAVE_COMMON_TOR_TOR_COMMON_H_ - -#include "base/files/file_path.h" - -namespace tor { - -class TorConfig { - public: - TorConfig(); - explicit TorConfig(const base::FilePath& binary_path); - TorConfig(const TorConfig& that); - ~TorConfig(); - - bool empty() const; - const base::FilePath& binary_path() const { return binary_path_; } - const base::FilePath& tor_data_path() const { return tor_data_path_; } - const base::FilePath& tor_watch_path() const { return tor_watch_path_; } - - private: - base::FilePath binary_path_; - base::FilePath tor_data_path_; - base::FilePath tor_watch_path_; -}; - -} // namespace tor - -#endif // BRAVE_COMMON_TOR_TOR_COMMON_H_ diff --git a/common/tor/tor_config.typemap b/common/tor/tor_config.typemap deleted file mode 100644 index de66ceded2ea..000000000000 --- a/common/tor/tor_config.typemap +++ /dev/null @@ -1,15 +0,0 @@ -# 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/. - -mojom = "//brave/components/services/tor/public/interfaces/tor.mojom" -public_headers = [ "//brave/common/tor/tor_common.h" ] -traits_headers = [ "//brave/common/tor/tor_config_struct_traits.h" ] -sources = [ - "//brave/common/tor/tor_config_struct_traits.cc", -] -type_mappings = [ "tor.mojom.TorConfig=tor::TorConfig" ] -public_deps = [ - "//mojo/public/cpp/base", - "//mojo/public/cpp/base:shared_typemap_traits", -] diff --git a/common/tor/tor_config_struct_traits.cc b/common/tor/tor_config_struct_traits.cc deleted file mode 100644 index 96a83eb3cf5b..000000000000 --- a/common/tor/tor_config_struct_traits.cc +++ /dev/null @@ -1,27 +0,0 @@ -/* Copyright (c) 2020 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/common/tor/tor_config_struct_traits.h" - -#include "mojo/public/cpp/base/file_path_mojom_traits.h" - -namespace mojo { - -// static -bool StructTraits:: - Read(tor::mojom::TorConfigDataView in, - tor::TorConfig* out) { - base::FilePath binary_path; - if (!in.ReadBinaryPath(&binary_path)) - return false; - - *out = tor::TorConfig(binary_path); - if (out->empty()) - return false; - return true; -} - -} // namespace mojo diff --git a/common/tor/tor_config_struct_traits.h b/common/tor/tor_config_struct_traits.h deleted file mode 100644 index 9c59905af5a9..000000000000 --- a/common/tor/tor_config_struct_traits.h +++ /dev/null @@ -1,38 +0,0 @@ -/* 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/. */ - -#ifndef BRAVE_COMMON_TOR_TOR_CONFIG_STRUCT_TRAITS_H_ -#define BRAVE_COMMON_TOR_TOR_CONFIG_STRUCT_TRAITS_H_ - -#include - -#include "brave/common/tor/tor_common.h" -#include "brave/components/services/tor/public/interfaces/tor.mojom.h" -#include "ipc/ipc_message_utils.h" - -namespace mojo { - -template <> -struct StructTraits { - static const base::FilePath& binary_path(const tor::TorConfig& config) { - return config.binary_path(); - } - - static const base::FilePath& tor_data_path(const tor::TorConfig& config) { - return config.tor_data_path(); - } - - static const base::FilePath& tor_watch_path(const tor::TorConfig& config) { - return config.tor_watch_path(); - } - - static bool Read(tor::mojom::TorConfigDataView in, - tor::TorConfig* out); -}; - -} // namespace mojo - -#endif // BRAVE_COMMON_TOR_TOR_CONFIG_STRUCT_TRAITS_H_ diff --git a/common/tor/tor_test_constants.cc b/common/tor/tor_test_constants.cc deleted file mode 100644 index 5daa7ed2dfc0..000000000000 --- a/common/tor/tor_test_constants.cc +++ /dev/null @@ -1,17 +0,0 @@ -/* 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/common/tor/tor_test_constants.h" - -#define FPL FILE_PATH_LITERAL - -namespace tor { - -const char kTestTorProxy[] = "socks5://127.0.0.1:9999"; -const char kTestTorPacString[] = "SOCKS5 127.0.0.1:9999"; -const base::FilePath::CharType kTestTorPath[] = FPL("."); -const base::FilePath::CharType kTestBrokenTorPath[] = FPL(""); - -} // namespace tor diff --git a/common/tor/tor_test_constants.h b/common/tor/tor_test_constants.h deleted file mode 100644 index 5c54f8652a2b..000000000000 --- a/common/tor/tor_test_constants.h +++ /dev/null @@ -1,20 +0,0 @@ -/* 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/. */ - -#ifndef BRAVE_COMMON_TOR_TOR_TEST_CONSTANTS_H_ -#define BRAVE_COMMON_TOR_TOR_TEST_CONSTANTS_H_ - -#include "base/files/file_path.h" - -namespace tor { - -extern const char kTestTorProxy[]; -extern const char kTestTorPacString[]; -extern const base::FilePath::CharType kTestTorPath[]; -extern const base::FilePath::CharType kTestBrokenTorPath[]; - -} // namespace tor - -#endif // BRAVE_COMMON_TOR_TOR_TEST_CONSTANTS_H_ diff --git a/common/tor/typemaps.gni b/common/tor/typemaps.gni deleted file mode 100644 index 7912208a2da0..000000000000 --- a/common/tor/typemaps.gni +++ /dev/null @@ -1,7 +0,0 @@ -# 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/. - -typemaps = [ - "//brave/common/tor/tor_config.typemap", -] diff --git a/components/ipfs/ipfs_navigation_throttle_unittest.cc b/components/ipfs/ipfs_navigation_throttle_unittest.cc index b1de8a8024c0..fe7137a31dba 100644 --- a/components/ipfs/ipfs_navigation_throttle_unittest.cc +++ b/components/ipfs/ipfs_navigation_throttle_unittest.cc @@ -15,11 +15,11 @@ #include "brave/browser/profiles/brave_profile_manager.h" #include "brave/browser/profiles/brave_unittest_profile_manager.h" #include "brave/browser/profiles/profile_util.h" -#include "brave/browser/tor/buildflags.h" #include "brave/components/ipfs/features.h" #include "brave/components/ipfs/ipfs_constants.h" #include "brave/components/ipfs/ipfs_service.h" #include "brave/components/ipfs/pref_names.h" +#include "brave/components/tor/buildflags/buildflags.h" #include "chrome/browser/prefs/browser_prefs.h" #include "chrome/test/base/chrome_render_view_host_test_harness.h" #include "chrome/test/base/scoped_testing_local_state.h" diff --git a/components/ipfs/test/BUILD.gn b/components/ipfs/test/BUILD.gn index a8310b9d2911..69ad44306e40 100644 --- a/components/ipfs/test/BUILD.gn +++ b/components/ipfs/test/BUILD.gn @@ -21,8 +21,8 @@ source_set("brave_ipfs_unit_tests") { "//base/test:test_support", "//brave/browser:browser_process", "//brave/browser/profiles", - "//brave/browser/tor:buildflags", "//brave/components/ipfs", + "//brave/components/tor/buildflags", "//brave/test:test_support", "//chrome:browser_dependencies", "//chrome/test:test_support", diff --git a/components/resources/BUILD.gn b/components/resources/BUILD.gn index d28eec69750f..c948cc77aad4 100644 --- a/components/resources/BUILD.gn +++ b/components/resources/BUILD.gn @@ -5,6 +5,7 @@ import("//brave/components/crypto_dot_com/browser/buildflags/buildflags.gni") import("//brave/components/ipfs/buildflags/buildflags.gni") import("//brave/components/moonpay/browser/buildflags/buildflags.gni") import("//brave/components/speedreader/buildflags.gni") +import("//brave/components/tor/buildflags/buildflags.gni") import("//build/config/locales.gni") import("//extensions/buildflags/buildflags.gni") import("//tools/grit/grit_rule.gni") @@ -106,6 +107,7 @@ grit("strings") { "ipfs_enabled=$ipfs_enabled", "moonpay_enabled=$moonpay_enabled", "crypto_dot_com_enabled=$crypto_dot_com_enabled", + "enable_tor=$enable_tor", ] foreach(locale, locales_with_fake_bidi) { diff --git a/components/resources/brave_components_strings.grd b/components/resources/brave_components_strings.grd index 8eaef1d75fe4..47fd1f8fd702 100644 --- a/components/resources/brave_components_strings.grd +++ b/components/resources/brave_components_strings.grd @@ -950,6 +950,7 @@ + diff --git a/components/resources/tor_strings.grdp b/components/resources/tor_strings.grdp new file mode 100644 index 000000000000..088d111ce304 --- /dev/null +++ b/components/resources/tor_strings.grdp @@ -0,0 +1,8 @@ + + + + + Tor Launcher + + + diff --git a/components/services/tor/BUILD.gn b/components/services/tor/BUILD.gn index f4d8db055a85..3237dae9bc44 100644 --- a/components/services/tor/BUILD.gn +++ b/components/services/tor/BUILD.gn @@ -11,7 +11,6 @@ source_set("tor") { deps = [ "//base", - "//brave/common/tor", "//mojo/public/cpp/bindings", "public/interfaces", ] diff --git a/components/services/tor/tor_launcher_impl.cc b/components/services/tor/tor_launcher_impl.cc index 3c812d7cc9c5..f2e824919ec4 100644 --- a/components/services/tor/tor_launcher_impl.cc +++ b/components/services/tor/tor_launcher_impl.cc @@ -114,9 +114,9 @@ void TorLauncherImpl::Shutdown() { Cleanup(); } -void TorLauncherImpl::Launch(const TorConfig& config, +void TorLauncherImpl::Launch(mojom::TorConfigPtr config, LaunchCallback callback) { - base::CommandLine args(config.binary_path()); + base::CommandLine args(config->binary_path); args.AppendArg("--ignore-missing-torrc"); args.AppendArg("-f"); args.AppendArg("/nonexistent"); @@ -126,7 +126,7 @@ void TorLauncherImpl::Launch(const TorConfig& config, args.AppendArg("auto"); args.AppendArg("--TruncateLogFile"); args.AppendArg("1"); - base::FilePath tor_data_path = config.tor_data_path(); + base::FilePath tor_data_path = config->tor_data_path; if (!tor_data_path.empty()) { if (!base::DirectoryExists(tor_data_path)) base::CreateDirectory(tor_data_path); @@ -138,7 +138,7 @@ void TorLauncherImpl::Launch(const TorConfig& config, args.AppendArgNative(log_file + tor_data_path.AppendASCII("tor.log").value()); } - base::FilePath tor_watch_path = config.tor_watch_path(); + base::FilePath tor_watch_path = config->tor_watch_path; if (!tor_watch_path.empty()) { if (!base::DirectoryExists(tor_watch_path)) base::CreateDirectory(tor_watch_path); diff --git a/components/services/tor/tor_launcher_impl.h b/components/services/tor/tor_launcher_impl.h index 355ec3016827..d8f20bc7a6f2 100644 --- a/components/services/tor/tor_launcher_impl.h +++ b/components/services/tor/tor_launcher_impl.h @@ -26,7 +26,7 @@ class TorLauncherImpl : public tor::mojom::TorLauncher { // tor::mojom::TorLauncher void Shutdown() override; - void Launch(const TorConfig& config, + void Launch(mojom::TorConfigPtr config, LaunchCallback callback) override; void SetCrashHandler(SetCrashHandlerCallback callback) override; private: diff --git a/components/tor/BUILD.gn b/components/tor/BUILD.gn new file mode 100644 index 000000000000..3317ccaf999c --- /dev/null +++ b/components/tor/BUILD.gn @@ -0,0 +1,86 @@ +import("//brave/components/tor/buildflags/buildflags.gni") + +source_set("tor") { + public_deps = [ + "//brave/components/tor/buildflags", + ] + + sources = [ + "tor_constants.cc", + "tor_constants.h", + "tor_launcher_service_observer.h", + "tor_switches.h", + ] + + deps = [ + ":pref_names", + "//base", + ] + + if (enable_tor) { + sources += [ + "brave_tor_client_updater.cc", + "brave_tor_client_updater.h", + "service_sandbox_type.h", + "tor_control.cc", + "tor_control.h", + "tor_control_event.cc", + "tor_control_event.h", + "tor_control_event_list.h", + "tor_launcher_factory.cc", + "tor_launcher_factory.h", + "tor_navigation_throttle.cc", + "tor_navigation_throttle.h", + "tor_profile_service.cc", + "tor_profile_service.h", + "tor_profile_service_impl.cc", + "tor_profile_service_impl.h", + "tor_tab_helper.cc", + "tor_tab_helper.h", + "onion_location_navigation_throttle.cc", + "onion_location_navigation_throttle.h", + "onion_location_tab_helper.cc", + "onion_location_tab_helper.h", + ] + } + + deps += [ + "//brave/components/brave_component_updater/browser", + "//brave/components/resources:strings", + "//brave/components/services/tor/public/interfaces", + "//components/pref_registry", + "//components/prefs", + "//components/user_prefs", + "//content/public/browser", + "//extensions/common:common_constants", + "//net", + "//services/service_manager", + "//third_party/re2", + "//url", + ] +} + +source_set("pref_names") { + sources = [ + "pref_names.cc", + "pref_names.h", + ] +} + +source_set("tor_unit_tests") { + testonly = true + if (enable_tor) { + sources = [ + "tor_control_unittest.cc", + ] + + deps = [ + "//base/test:test_support", + "//brave/components/tor", + "//brave/test:test_support", + "//content/public/browser", + "//content/test:test_support", + "//testing/gtest", + ] + } +} diff --git a/browser/extensions/brave_tor_client_updater.cc b/components/tor/brave_tor_client_updater.cc similarity index 81% rename from browser/extensions/brave_tor_client_updater.cc rename to components/tor/brave_tor_client_updater.cc index 10b5dc4e67a4..e5ddd4abef6d 100644 --- a/browser/extensions/brave_tor_client_updater.cc +++ b/components/tor/brave_tor_client_updater.cc @@ -1,12 +1,13 @@ -/* Copyright (c) 2019 The Brave Authors. All rights reserved. +/* Copyright (c) 2020 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/extensions/brave_tor_client_updater.h" +#include "brave/components/tor/brave_tor_client_updater.h" #include #include +#include #include "base/command_line.h" #include "base/files/file_enumerator.h" @@ -16,21 +17,14 @@ #include "base/task/post_task.h" #include "base/task_runner.h" #include "base/task_runner_util.h" -#include "brave/browser/tor/tor_profile_service.h" -#include "brave/common/brave_switches.h" -#include "chrome/browser/browser_process.h" -#include "chrome/browser/profiles/profile_manager.h" +#include "brave/components/tor/pref_names.h" +#include "brave/components/tor/tor_switches.h" +#include "components/prefs/pref_service.h" #include "third_party/re2/src/re2/re2.h" using brave_component_updater::BraveComponent; -namespace { -void DeleteDir(const base::FilePath& path) { - base::DeletePathRecursively(path); -} -} // namespace - -namespace extensions { +namespace tor { namespace { @@ -69,6 +63,10 @@ base::FilePath InitExecutablePath(const base::FilePath& install_dir) { return executable_path; } +void DeleteDir(const base::FilePath& path) { + base::DeletePathRecursively(path); +} + } // namespace #if defined(OS_WIN) @@ -111,21 +109,25 @@ std::string BraveTorClientUpdater::g_tor_client_component_id_( std::string BraveTorClientUpdater::g_tor_client_component_base64_public_key_( kTorClientComponentBase64PublicKey); -BraveTorClientUpdater::BraveTorClientUpdater(BraveComponent::Delegate* delegate) - : BraveComponent(delegate), +BraveTorClientUpdater::BraveTorClientUpdater( + BraveComponent::Delegate* component_delegate, + PrefService* local_state, + const base::FilePath& user_data_dir) + : BraveComponent(component_delegate), task_runner_(base::CreateSequencedTaskRunner( {base::ThreadPool(), base::MayBlock()})), registered_(false), + local_state_(local_state), + user_data_dir_(user_data_dir), weak_ptr_factory_(this) {} -BraveTorClientUpdater::~BraveTorClientUpdater() { -} +BraveTorClientUpdater::~BraveTorClientUpdater() {} void BraveTorClientUpdater::Register() { const base::CommandLine& command_line = *base::CommandLine::ForCurrentProcess(); - if (tor::TorProfileService::IsTorDisabled() || - command_line.HasSwitch(switches::kDisableTorClientUpdaterExtension) || + if (IsTorDisabled() || + command_line.HasSwitch(tor::kDisableTorClientUpdaterExtension) || registered_) { return; } @@ -143,14 +145,11 @@ void BraveTorClientUpdater::Unregister() { } void BraveTorClientUpdater::Cleanup() { - // Delete tor binaries if tor is disabled. - if (tor::TorProfileService::IsTorDisabled()) { - ProfileManager* profile_manager = g_browser_process->profile_manager(); - base::FilePath tor_component_dir = - profile_manager->user_data_dir().AppendASCII(kTorClientComponentId); - GetTaskRunner()->PostTask(FROM_HERE, - base::BindOnce(&DeleteDir, tor_component_dir)); - } + DCHECK(!user_data_dir_.empty()); + base::FilePath tor_component_dir = + user_data_dir_.AppendASCII(kTorClientComponentId); + task_runner_->PostTask(FROM_HERE, + base::BindOnce(&DeleteDir, tor_component_dir)); } void BraveTorClientUpdater::SetExecutablePath(const base::FilePath& path) { @@ -174,6 +173,12 @@ void BraveTorClientUpdater::OnComponentReady( weak_ptr_factory_.GetWeakPtr())); } +bool BraveTorClientUpdater::IsTorDisabled() { + if (local_state_) + return local_state_->GetBoolean(tor::prefs::kTorDisabled); + return false; +} + void BraveTorClientUpdater::AddObserver(Observer* observer) { observers_.AddObserver(observer); } @@ -190,12 +195,4 @@ void BraveTorClientUpdater::SetComponentIdAndBase64PublicKeyForTest( g_tor_client_component_base64_public_key_ = component_base64_public_key; } -/////////////////////////////////////////////////////////////////////////////// - -// The Brave Tor client extension factory. -std::unique_ptr -BraveTorClientUpdaterFactory(BraveComponent::Delegate* delegate) { - return std::make_unique(delegate); -} - -} // namespace extensions +} // namespace tor diff --git a/browser/extensions/brave_tor_client_updater.h b/components/tor/brave_tor_client_updater.h similarity index 82% rename from browser/extensions/brave_tor_client_updater.h rename to components/tor/brave_tor_client_updater.h index 2bbee79ad3c4..0365ecbf57a0 100644 --- a/browser/extensions/brave_tor_client_updater.h +++ b/components/tor/brave_tor_client_updater.h @@ -1,10 +1,10 @@ -/* Copyright (c) 2019 The Brave Authors. All rights reserved. +/* Copyright (c) 2020 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_BROWSER_EXTENSIONS_BRAVE_TOR_CLIENT_UPDATER_H_ -#define BRAVE_BROWSER_EXTENSIONS_BRAVE_TOR_CLIENT_UPDATER_H_ +#ifndef BRAVE_COMPONENTS_TOR_BRAVE_TOR_CLIENT_UPDATER_H_ +#define BRAVE_COMPONENTS_TOR_BRAVE_TOR_CLIENT_UPDATER_H_ #include #include @@ -18,12 +18,12 @@ class BraveProfileManagerTest; class BraveProfileManagerExtensionTest; class BraveTorClientUpdaterTest; +class PrefService; class SearchEngineProviderServiceTest; using brave_component_updater::BraveComponent; -// TODO(bridiver) - this doesn't belong under extensions -namespace extensions { +namespace tor { #if defined(OS_WIN) extern const char kTorClientComponentName[]; @@ -48,7 +48,9 @@ class BraveTorClientUpdater : public BraveComponent { ~Observer() override = default; }; - explicit BraveTorClientUpdater(BraveComponent::Delegate* delegate); + BraveTorClientUpdater(BraveComponent::Delegate* component_delegate, + PrefService* local_state, + const base::FilePath& user_data_dir); ~BraveTorClientUpdater() override; void Register(); @@ -66,6 +68,7 @@ class BraveTorClientUpdater : public BraveComponent { void OnComponentReady(const std::string& component_id, const base::FilePath& install_dir, const std::string& manifest) override; + bool IsTorDisabled(); private: friend class ::BraveProfileManagerTest; @@ -85,16 +88,14 @@ class BraveTorClientUpdater : public BraveComponent { bool registered_; base::FilePath executable_path_; base::ObserverList observers_; + PrefService* local_state_; + base::FilePath user_data_dir_; base::WeakPtrFactory weak_ptr_factory_; DISALLOW_COPY_AND_ASSIGN(BraveTorClientUpdater); }; -// Creates the BraveTorClientUpdater -std::unique_ptr -BraveTorClientUpdaterFactory(BraveComponent::Delegate* delegate); +} // namespace tor -} // namespace extensions - -#endif // BRAVE_BROWSER_EXTENSIONS_BRAVE_TOR_CLIENT_UPDATER_H_ +#endif // BRAVE_COMPONENTS_TOR_BRAVE_TOR_CLIENT_UPDATER_H_ diff --git a/components/tor/buildflags/BUILD.gn b/components/tor/buildflags/BUILD.gn new file mode 100644 index 000000000000..5d3fc842bff9 --- /dev/null +++ b/components/tor/buildflags/BUILD.gn @@ -0,0 +1,7 @@ +import("//brave/components/tor/buildflags/buildflags.gni") +import("//build/buildflag_header.gni") + +buildflag_header("buildflags") { + header = "buildflags.h" + flags = [ "ENABLE_TOR=$enable_tor" ] +} diff --git a/browser/tor/buildflags/buildflags.gni b/components/tor/buildflags/buildflags.gni similarity index 100% rename from browser/tor/buildflags/buildflags.gni rename to components/tor/buildflags/buildflags.gni diff --git a/browser/tor/onion_location_navigation_throttle.cc b/components/tor/onion_location_navigation_throttle.cc similarity index 54% rename from browser/tor/onion_location_navigation_throttle.cc rename to components/tor/onion_location_navigation_throttle.cc index 698dcfba4cb1..ae3e8204e719 100644 --- a/browser/tor/onion_location_navigation_throttle.cc +++ b/components/tor/onion_location_navigation_throttle.cc @@ -3,20 +3,17 @@ * 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/tor/onion_location_navigation_throttle.h" +#include "brave/components/tor/onion_location_navigation_throttle.h" #include #include #include "base/bind.h" -#include "brave/browser/profiles/profile_util.h" -#include "brave/browser/tor/onion_location_tab_helper.h" -#include "brave/browser/tor/tor_profile_service.h" -#include "brave/common/tor/pref_names.h" -#include "chrome/browser/profiles/profile.h" -#include "chrome/browser/profiles/profile_window.h" -#include "chrome/browser/ui/browser.h" -#include "chrome/browser/ui/browser_finder.h" +#include "brave/components/tor/onion_location_tab_helper.h" +#include "brave/components/tor/pref_names.h" +#include "components/prefs/pref_service.h" +#include "components/user_prefs/user_prefs.h" +#include "content/public/browser/browser_context.h" #include "content/public/browser/navigation_handle.h" #include "content/public/browser/web_contents.h" @@ -36,37 +33,31 @@ bool GetOnionLocation(const net::HttpResponseHeaders* headers, return true; } -void OnTorProfileCreated(GURL onion_location, - Profile* profile, - Profile::CreateStatus status) { - if (status != Profile::CreateStatus::CREATE_STATUS_INITIALIZED) - return; - Browser* browser = chrome::FindTabbedBrowser(profile, true); - if (!browser) - return; - content::OpenURLParams open_tor(onion_location, content::Referrer(), - WindowOpenDisposition::OFF_THE_RECORD, - ui::PAGE_TRANSITION_TYPED, false); - browser->OpenURL(open_tor); -} - } // namespace // static std::unique_ptr OnionLocationNavigationThrottle::MaybeCreateThrottleFor( - content::NavigationHandle* navigation_handle) { - if (tor::TorProfileService::IsTorDisabled() || - !navigation_handle->IsInMainFrame()) + content::NavigationHandle* navigation_handle, + bool is_tor_disabled, + std::unique_ptr delegate, + bool is_tor_profile) { + if (is_tor_disabled || !navigation_handle->IsInMainFrame()) return nullptr; - return std::make_unique(navigation_handle); + return std::make_unique( + navigation_handle, std::move(delegate), is_tor_profile); } OnionLocationNavigationThrottle::OnionLocationNavigationThrottle( - content::NavigationHandle* navigation_handle) - : content::NavigationThrottle(navigation_handle) { - profile_ = Profile::FromBrowserContext( - navigation_handle->GetWebContents()->GetBrowserContext()); + content::NavigationHandle* navigation_handle, + std::unique_ptr delegate, + bool is_tor_profile) + : content::NavigationThrottle(navigation_handle), + is_tor_profile_(is_tor_profile), + delegate_(std::move(delegate)) { + content::BrowserContext* context = + navigation_handle->GetWebContents()->GetBrowserContext(); + pref_service_ = user_prefs::UserPrefs::Get(context); } OnionLocationNavigationThrottle::~OnionLocationNavigationThrottle() {} @@ -80,13 +71,9 @@ OnionLocationNavigationThrottle::WillProcessResponse() { if (headers && GetOnionLocation(headers, &onion_location) && !navigation_handle()->GetURL().DomainIs("onion")) { // If user prefers opening it automatically - if (profile_->GetPrefs()->GetBoolean(prefs::kAutoOnionLocation)) { - profiles::SwitchToTorProfile( - base::BindRepeating(&OnTorProfileCreated, GURL(onion_location))); - // We do not close last tab of the window - Browser* browser = chrome::FindBrowserWithProfile(profile_); - if (browser && browser->tab_strip_model()->count() > 1) - navigation_handle()->GetWebContents()->ClosePage(); + if (pref_service_->GetBoolean(prefs::kAutoOnionLocation)) { + delegate_->OpenInTorWindow(navigation_handle()->GetWebContents(), + GURL(onion_location)); } else { OnionLocationTabHelper::SetOnionLocation( navigation_handle()->GetWebContents(), GURL(onion_location)); @@ -101,11 +88,11 @@ OnionLocationNavigationThrottle::WillProcessResponse() { content::NavigationThrottle::ThrottleCheckResult OnionLocationNavigationThrottle::WillStartRequest() { // Open .onion site in Tor window - if (!brave::IsTorProfile(profile_)) { + if (!is_tor_profile_) { GURL url = navigation_handle()->GetURL(); if (url.SchemeIsHTTPOrHTTPS() && url.DomainIs("onion")) { - profiles::SwitchToTorProfile( - base::BindRepeating(&OnTorProfileCreated, std::move(url))); + delegate_->OpenInTorWindow(navigation_handle()->GetWebContents(), + std::move(url)); return content::NavigationThrottle::CANCEL_AND_IGNORE; } } diff --git a/browser/tor/onion_location_navigation_throttle.h b/components/tor/onion_location_navigation_throttle.h similarity index 58% rename from browser/tor/onion_location_navigation_throttle.h rename to components/tor/onion_location_navigation_throttle.h index 77827cd17d8e..6d3d5d09fcec 100644 --- a/browser/tor/onion_location_navigation_throttle.h +++ b/components/tor/onion_location_navigation_throttle.h @@ -3,28 +3,40 @@ * 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_BROWSER_TOR_ONION_LOCATION_NAVIGATION_THROTTLE_H_ -#define BRAVE_BROWSER_TOR_ONION_LOCATION_NAVIGATION_THROTTLE_H_ +#ifndef BRAVE_COMPONENTS_TOR_ONION_LOCATION_NAVIGATION_THROTTLE_H_ +#define BRAVE_COMPONENTS_TOR_ONION_LOCATION_NAVIGATION_THROTTLE_H_ #include -#include "chrome/browser/profiles/profile.h" #include "content/public/browser/navigation_throttle.h" -class Profile; +class GURL; +class PrefService; namespace content { class NavigationHandle; +class WebContents; } // namespace content namespace tor { class OnionLocationNavigationThrottle : public content::NavigationThrottle { public: + class Delegate { + public: + virtual ~Delegate() = default; + virtual void OpenInTorWindow(content::WebContents* context, + GURL onion_location) = 0; + }; static std::unique_ptr - MaybeCreateThrottleFor(content::NavigationHandle* navigation_handle); + MaybeCreateThrottleFor(content::NavigationHandle* navigation_handle, + bool is_tor_disabled, + std::unique_ptr delegate, + bool is_tor_profile); explicit OnionLocationNavigationThrottle( - content::NavigationHandle* navigation_handle); + content::NavigationHandle* navigation_handle, + std::unique_ptr delegate, + bool is_tor_profile); ~OnionLocationNavigationThrottle() override; // content::NavigationThrottle implementation: @@ -33,7 +45,11 @@ class OnionLocationNavigationThrottle : public content::NavigationThrottle { const char* GetNameForLogging() override; private: - Profile* profile_; + bool is_tor_profile_ = false; + + PrefService* pref_service_ = nullptr; + + std::unique_ptr delegate_; OnionLocationNavigationThrottle(const OnionLocationNavigationThrottle&) = delete; @@ -43,4 +59,4 @@ class OnionLocationNavigationThrottle : public content::NavigationThrottle { } // namespace tor -#endif // BRAVE_BROWSER_TOR_ONION_LOCATION_NAVIGATION_THROTTLE_H_ +#endif // BRAVE_COMPONENTS_TOR_ONION_LOCATION_NAVIGATION_THROTTLE_H_ diff --git a/browser/tor/onion_location_tab_helper.cc b/components/tor/onion_location_tab_helper.cc similarity index 93% rename from browser/tor/onion_location_tab_helper.cc rename to components/tor/onion_location_tab_helper.cc index 05fb3144a54f..17ac6a4e8d28 100644 --- a/browser/tor/onion_location_tab_helper.cc +++ b/components/tor/onion_location_tab_helper.cc @@ -3,7 +3,7 @@ * 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/tor/onion_location_tab_helper.h" +#include "brave/components/tor/onion_location_tab_helper.h" namespace tor { diff --git a/browser/tor/onion_location_tab_helper.h b/components/tor/onion_location_tab_helper.h similarity index 86% rename from browser/tor/onion_location_tab_helper.h rename to components/tor/onion_location_tab_helper.h index c5ab89e91fa8..742bc446719e 100644 --- a/browser/tor/onion_location_tab_helper.h +++ b/components/tor/onion_location_tab_helper.h @@ -3,8 +3,8 @@ * 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_BROWSER_TOR_ONION_LOCATION_TAB_HELPER_H_ -#define BRAVE_BROWSER_TOR_ONION_LOCATION_TAB_HELPER_H_ +#ifndef BRAVE_COMPONENTS_TOR_ONION_LOCATION_TAB_HELPER_H_ +#define BRAVE_COMPONENTS_TOR_ONION_LOCATION_TAB_HELPER_H_ #include "content/public/browser/web_contents_user_data.h" @@ -36,4 +36,4 @@ class OnionLocationTabHelper } // namespace tor -#endif // BRAVE_BROWSER_TOR_ONION_LOCATION_TAB_HELPER_H_ +#endif // BRAVE_COMPONENTS_TOR_ONION_LOCATION_TAB_HELPER_H_ diff --git a/common/tor/pref_names.cc b/components/tor/pref_names.cc similarity index 91% rename from common/tor/pref_names.cc rename to components/tor/pref_names.cc index 25e609540c55..591c040082df 100644 --- a/common/tor/pref_names.cc +++ b/components/tor/pref_names.cc @@ -3,7 +3,7 @@ * 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/common/tor/pref_names.h" +#include "brave/components/tor/pref_names.h" namespace tor { namespace prefs { diff --git a/common/tor/pref_names.h b/components/tor/pref_names.h similarity index 68% rename from common/tor/pref_names.h rename to components/tor/pref_names.h index e8c7a87a0480..630cd0444cfa 100644 --- a/common/tor/pref_names.h +++ b/components/tor/pref_names.h @@ -1,10 +1,10 @@ -/* Copyright (c) 2019 The Brave Authors. All rights reserved. +/* Copyright (c) 2020 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_COMMON_TOR_PREF_NAMES_H_ -#define BRAVE_COMMON_TOR_PREF_NAMES_H_ +#ifndef BRAVE_COMPONENTS_TOR_PREF_NAMES_H_ +#define BRAVE_COMPONENTS_TOR_PREF_NAMES_H_ namespace tor { namespace prefs { @@ -17,4 +17,4 @@ extern const char kAutoOnionLocation[]; } // namespace prefs } // namespace tor -#endif // BRAVE_COMMON_TOR_PREF_NAMES_H_ +#endif // BRAVE_COMPONENTS_TOR_PREF_NAMES_H_ diff --git a/components/tor/service_sandbox_type.h b/components/tor/service_sandbox_type.h new file mode 100644 index 000000000000..b82a4931f321 --- /dev/null +++ b/components/tor/service_sandbox_type.h @@ -0,0 +1,23 @@ +/* Copyright 2020 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_COMPONENTS_TOR_SERVICE_SANDBOX_TYPE_H_ +#define BRAVE_COMPONENTS_TOR_SERVICE_SANDBOX_TYPE_H_ + +#include "content/public/browser/service_process_host.h" + +namespace tor { +namespace mojom { +class TorLauncher; +} // namespace mojom +} // namespace tor + +template <> +inline sandbox::policy::SandboxType +content::GetServiceSandboxType() { + return sandbox::policy::SandboxType::kNoSandbox; +} + +#endif // BRAVE_COMPONENTS_TOR_SERVICE_SANDBOX_TYPE_H_ diff --git a/common/tor/tor_constants.cc b/components/tor/tor_constants.cc similarity index 61% rename from common/tor/tor_constants.cc rename to components/tor/tor_constants.cc index e2800ba6f4d8..650307a1ccb7 100644 --- a/common/tor/tor_constants.cc +++ b/components/tor/tor_constants.cc @@ -1,8 +1,9 @@ -/* This Source Code Form is subject to the terms of the Mozilla Public +/* Copyright (c) 2020 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/common/tor/tor_constants.h" +#include "brave/components/tor/tor_constants.h" #define FPL FILE_PATH_LITERAL diff --git a/common/tor/tor_constants.h b/components/tor/tor_constants.h similarity index 52% rename from common/tor/tor_constants.h rename to components/tor/tor_constants.h index df65ac1e4621..d6454c558d34 100644 --- a/common/tor/tor_constants.h +++ b/components/tor/tor_constants.h @@ -1,9 +1,10 @@ -/* This Source Code Form is subject to the terms of the Mozilla Public +/* Copyright (c) 2020 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_COMMON_TOR_TOR_CONSTANTS_H_ -#define BRAVE_COMMON_TOR_TOR_CONSTANTS_H_ +#ifndef BRAVE_COMPONENTS_TOR_TOR_CONSTANTS_H_ +#define BRAVE_COMPONENTS_TOR_TOR_CONSTANTS_H_ #include @@ -15,4 +16,4 @@ extern const base::FilePath::CharType kTorProfileDir[]; } // namespace tor -#endif // BRAVE_COMMON_TOR_TOR_CONSTANTS_H_ +#endif // BRAVE_COMPONENTS_TOR_TOR_CONSTANTS_H_ diff --git a/browser/tor/tor_control.cc b/components/tor/tor_control.cc similarity index 99% rename from browser/tor/tor_control.cc rename to components/tor/tor_control.cc index 9e9a5a9631cc..a66416fb1e13 100644 --- a/browser/tor/tor_control.cc +++ b/components/tor/tor_control.cc @@ -3,7 +3,7 @@ * 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/tor/tor_control.h" +#include "brave/components/tor/tor_control.h" #include "base/bind_helpers.h" #include "base/files/file.h" diff --git a/browser/tor/tor_control.h b/components/tor/tor_control.h similarity index 97% rename from browser/tor/tor_control.h rename to components/tor/tor_control.h index 4f0b5e5b145d..3130765670e8 100644 --- a/browser/tor/tor_control.h +++ b/components/tor/tor_control.h @@ -3,8 +3,8 @@ * 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_BROWSER_TOR_TOR_CONTROL_H_ -#define BRAVE_BROWSER_TOR_TOR_CONTROL_H_ +#ifndef BRAVE_COMPONENTS_TOR_TOR_CONTROL_H_ +#define BRAVE_COMPONENTS_TOR_TOR_CONTROL_H_ #include #include @@ -13,7 +13,7 @@ #include #include -#include "brave/common/tor/tor_control_event.h" +#include "brave/components/tor/tor_control_event.h" #include "base/callback.h" #include "base/files/file_path.h" @@ -241,4 +241,4 @@ class TorControl { } // namespace tor -#endif // BRAVE_BROWSER_TOR_TOR_CONTROL_H_ +#endif // BRAVE_COMPONENTS_TOR_TOR_CONTROL_H_ diff --git a/common/tor/tor_control_event.cc b/components/tor/tor_control_event.cc similarity index 93% rename from common/tor/tor_control_event.cc rename to components/tor/tor_control_event.cc index 33452ee3d593..0620c9dae60a 100644 --- a/common/tor/tor_control_event.cc +++ b/components/tor/tor_control_event.cc @@ -3,7 +3,7 @@ * 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/common/tor/tor_control_event.h" +#include "brave/components/tor/tor_control_event.h" namespace tor { diff --git a/common/tor/tor_control_event.h b/components/tor/tor_control_event.h similarity index 81% rename from common/tor/tor_control_event.h rename to components/tor/tor_control_event.h index 8e5741cb569b..b5cb45c4821f 100644 --- a/common/tor/tor_control_event.h +++ b/components/tor/tor_control_event.h @@ -3,8 +3,8 @@ * 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_COMMON_TOR_TOR_CONTROL_EVENT_H_ -#define BRAVE_COMMON_TOR_TOR_CONTROL_EVENT_H_ +#ifndef BRAVE_COMPONENTS_TOR_TOR_CONTROL_EVENT_H_ +#define BRAVE_COMPONENTS_TOR_TOR_CONTROL_EVENT_H_ #include #include @@ -23,4 +23,4 @@ extern const std::map kTorControlEventByEnum; } // namespace tor -#endif // BRAVE_COMMON_TOR_TOR_CONTROL_EVENT_H_ +#endif // BRAVE_COMPONENTS_TOR_TOR_CONTROL_EVENT_H_ diff --git a/common/tor/tor_control_event_list.h b/components/tor/tor_control_event_list.h similarity index 100% rename from common/tor/tor_control_event_list.h rename to components/tor/tor_control_event_list.h diff --git a/browser/tor/tor_control_unittest.cc b/components/tor/tor_control_unittest.cc similarity index 99% rename from browser/tor/tor_control_unittest.cc rename to components/tor/tor_control_unittest.cc index 6c7aaf3e35bb..03ee4bbff369 100644 --- a/browser/tor/tor_control_unittest.cc +++ b/components/tor/tor_control_unittest.cc @@ -3,7 +3,7 @@ * 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/tor/tor_control.h" +#include "brave/components/tor/tor_control.h" #include "base/run_loop.h" #include "base/bind_helpers.h" diff --git a/browser/tor/tor_launcher_factory.cc b/components/tor/tor_launcher_factory.cc similarity index 94% rename from browser/tor/tor_launcher_factory.cc rename to components/tor/tor_launcher_factory.cc index a1e741e061cd..0c0a53544c99 100644 --- a/browser/tor/tor_launcher_factory.cc +++ b/components/tor/tor_launcher_factory.cc @@ -3,16 +3,16 @@ * 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/tor/tor_launcher_factory.h" +#include "brave/components/tor/tor_launcher_factory.h" #include #include "base/bind.h" #include "base/process/kill.h" #include "base/task/post_task.h" -#include "brave/browser/tor/tor_profile_service_impl.h" -#include "brave/grit/brave_generated_resources.h" -#include "chrome/browser/service_sandbox_type.h" +#include "brave/components/tor/service_sandbox_type.h" +#include "brave/components/tor/tor_profile_service_impl.h" +#include "components/grit/brave_components_strings.h" #include "content/public/browser/browser_task_traits.h" #include "content/public/browser/browser_thread.h" #include "content/public/browser/child_process_launcher_utils.h" @@ -68,14 +68,7 @@ void TorLauncherFactory::Init() { TorLauncherFactory::~TorLauncherFactory() {} -bool TorLauncherFactory::SetConfig(const tor::TorConfig& config) { - if (config.empty()) - return false; - config_ = config; - return true; -} - -void TorLauncherFactory::LaunchTorProcess(const tor::TorConfig& config) { +void TorLauncherFactory::LaunchTorProcess(const tor::mojom::TorConfig& config) { DCHECK_CURRENTLY_ON(BrowserThread::UI); if (g_prevent_tor_launch_for_tests) { VLOG(1) << "Skipping the tor process launch in tests."; @@ -93,10 +86,11 @@ void TorLauncherFactory::LaunchTorProcess(const tor::TorConfig& config) { LOG(WARNING) << "tor process(" << tor_pid_ << ") is running"; return; } - if (!SetConfig(config)) { - LOG(WARNING) << "config is empty"; - return; - } + + DCHECK(!config.binary_path.empty()); + DCHECK(!config.tor_data_path.empty()); + DCHECK(!config.tor_watch_path.empty()); + config_ = config; // Tor launcher could be null if we created Tor process and killed it // through KillTorProcess function before. So we need to initialize @@ -107,7 +101,7 @@ void TorLauncherFactory::LaunchTorProcess(const tor::TorConfig& config) { // Launch tor after cleanup is done control_->PreStartCheck( - config_.tor_watch_path(), + config_.tor_watch_path, base::BindOnce(&TorLauncherFactory::OnTorControlCheckComplete, weak_ptr_factory_.GetWeakPtr())); } @@ -115,7 +109,8 @@ void TorLauncherFactory::LaunchTorProcess(const tor::TorConfig& config) { void TorLauncherFactory::OnTorControlCheckComplete() { DCHECK_CURRENTLY_ON(BrowserThread::UI); if (tor_launcher_.is_bound()) { - tor_launcher_->Launch(config_, + auto config = tor::mojom::TorConfig::New(config_); + tor_launcher_->Launch(std::move(config), base::BindOnce(&TorLauncherFactory::OnTorLaunched, weak_ptr_factory_.GetWeakPtr())); } else { @@ -251,7 +246,7 @@ void TorLauncherFactory::KillOldTorProcess(base::ProcessId id) { void TorLauncherFactory::RelaunchTor() { Init(); control_->PreStartCheck( - config_.tor_watch_path(), + config_.tor_watch_path, base::BindOnce(&TorLauncherFactory::OnTorControlCheckComplete, weak_ptr_factory_.GetWeakPtr())); } diff --git a/browser/tor/tor_launcher_factory.h b/components/tor/tor_launcher_factory.h similarity index 84% rename from browser/tor/tor_launcher_factory.h rename to components/tor/tor_launcher_factory.h index 1b8f9e6e1eba..a77295237e44 100644 --- a/browser/tor/tor_launcher_factory.h +++ b/components/tor/tor_launcher_factory.h @@ -1,10 +1,10 @@ -/* Copyright (c) 2019 The Brave Authors. All rights reserved. +/* Copyright (c) 2020 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_BROWSER_TOR_TOR_LAUNCHER_FACTORY_H_ -#define BRAVE_BROWSER_TOR_TOR_LAUNCHER_FACTORY_H_ +#ifndef BRAVE_COMPONENTS_TOR_TOR_LAUNCHER_FACTORY_H_ +#define BRAVE_COMPONENTS_TOR_TOR_LAUNCHER_FACTORY_H_ #include #include @@ -14,9 +14,8 @@ #include "base/memory/singleton.h" #include "base/memory/weak_ptr.h" #include "base/observer_list.h" -#include "brave/browser/tor/tor_control.h" -#include "brave/common/tor/tor_common.h" #include "brave/components/services/tor/public/interfaces/tor.mojom.h" +#include "brave/components/tor/tor_control.h" #include "mojo/public/cpp/bindings/remote.h" namespace tor { @@ -28,9 +27,8 @@ class TorLauncherFactory : public tor::TorControl::Delegate { static TorLauncherFactory* GetInstance(); void Init(); - void LaunchTorProcess(const tor::TorConfig& config); + void LaunchTorProcess(const tor::mojom::TorConfig& config); void KillTorProcess(); - const tor::TorConfig& GetTorConfig() const { return config_; } int64_t GetTorPid() const { return tor_pid_; } bool IsTorConnected() const { return is_connected_; } @@ -58,8 +56,6 @@ class TorLauncherFactory : public tor::TorControl::Delegate { void OnTorControlCheckComplete(); - bool SetConfig(const tor::TorConfig& config); - void OnTorLauncherCrashed(); void OnTorCrashed(int64_t pid); void OnTorLaunched(bool result, int64_t pid); @@ -78,7 +74,7 @@ class TorLauncherFactory : public tor::TorControl::Delegate { int64_t tor_pid_; - tor::TorConfig config_; + tor::mojom::TorConfig config_; base::ObserverList observers_; @@ -96,4 +92,4 @@ class ScopedTorLaunchPreventerForTest { ~ScopedTorLaunchPreventerForTest(); }; -#endif // BRAVE_BROWSER_TOR_TOR_LAUNCHER_FACTORY_H_ +#endif // BRAVE_COMPONENTS_TOR_TOR_LAUNCHER_FACTORY_H_ diff --git a/browser/tor/tor_launcher_service_observer.h b/components/tor/tor_launcher_service_observer.h similarity index 73% rename from browser/tor/tor_launcher_service_observer.h rename to components/tor/tor_launcher_service_observer.h index 31a60844e5a9..72b36950696f 100644 --- a/browser/tor/tor_launcher_service_observer.h +++ b/components/tor/tor_launcher_service_observer.h @@ -1,10 +1,10 @@ -/* Copyright 2019 The Brave Authors. All rights reserved. +/* Copyright 2020 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_BROWSER_TOR_TOR_LAUNCHER_SERVICE_OBSERVER_H_ -#define BRAVE_BROWSER_TOR_TOR_LAUNCHER_SERVICE_OBSERVER_H_ +#ifndef BRAVE_COMPONENTS_TOR_TOR_LAUNCHER_SERVICE_OBSERVER_H_ +#define BRAVE_COMPONENTS_TOR_TOR_LAUNCHER_SERVICE_OBSERVER_H_ #include @@ -25,4 +25,4 @@ class TorLauncherServiceObserver : public base::CheckedObserver { } // namespace tor -#endif // BRAVE_BROWSER_TOR_TOR_LAUNCHER_SERVICE_OBSERVER_H_ +#endif // BRAVE_COMPONENTS_TOR_TOR_LAUNCHER_SERVICE_OBSERVER_H_ diff --git a/browser/tor/tor_navigation_throttle.cc b/components/tor/tor_navigation_throttle.cc similarity index 63% rename from browser/tor/tor_navigation_throttle.cc rename to components/tor/tor_navigation_throttle.cc index b91891dc4a5e..3ae85fe54a3d 100644 --- a/browser/tor/tor_navigation_throttle.cc +++ b/components/tor/tor_navigation_throttle.cc @@ -1,16 +1,13 @@ -/* Copyright (c) 2019 The Brave Authors. All rights reserved. +/* Copyright (c) 2020 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/tor/tor_navigation_throttle.h" +#include "brave/components/tor/tor_navigation_throttle.h" #include -#include "brave/browser/profiles/profile_util.h" -#include "brave/browser/tor/tor_profile_service.h" -#include "brave/browser/tor/tor_profile_service_factory.h" -#include "chrome/browser/profiles/profile.h" +#include "brave/components/tor/tor_profile_service.h" #include "content/public/browser/navigation_handle.h" #include "content/public/browser/web_contents.h" #include "content/public/common/url_constants.h" @@ -21,21 +18,19 @@ namespace tor { // static std::unique_ptr TorNavigationThrottle::MaybeCreateThrottleFor( - content::NavigationHandle* navigation_handle) { - Profile* profile = Profile::FromBrowserContext( - navigation_handle->GetWebContents()->GetBrowserContext()); - if (!brave::IsTorProfile(profile)) + content::NavigationHandle* navigation_handle, + TorProfileService* service, + bool is_tor_profile) { + if (!is_tor_profile || !service) return nullptr; - return std::make_unique(navigation_handle); + return std::make_unique(navigation_handle, service); } TorNavigationThrottle::TorNavigationThrottle( - content::NavigationHandle* navigation_handle) - : content::NavigationThrottle(navigation_handle) { - Profile* profile = Profile::FromBrowserContext( - navigation_handle->GetWebContents()->GetBrowserContext()); - DCHECK(brave::IsTorProfile(profile)); - tor_profile_service_ = TorProfileServiceFactory::GetForProfile(profile); + content::NavigationHandle* navigation_handle, + TorProfileService* service) + : content::NavigationThrottle(navigation_handle), + tor_profile_service_(service) { DCHECK(tor_profile_service_); tor_profile_service_->AddObserver(this); } @@ -47,8 +42,7 @@ TorNavigationThrottle::~TorNavigationThrottle() { content::NavigationThrottle::ThrottleCheckResult TorNavigationThrottle::WillStartRequest() { GURL url = navigation_handle()->GetURL(); - if (url.SchemeIsHTTPOrHTTPS() || - url.SchemeIs(content::kChromeUIScheme) || + if (url.SchemeIsHTTPOrHTTPS() || url.SchemeIs(content::kChromeUIScheme) || url.SchemeIs(extensions::kExtensionScheme) || url.SchemeIs(content::kChromeDevToolsScheme)) { if (!tor_profile_service_->IsTorConnected() && diff --git a/browser/tor/tor_navigation_throttle.h b/components/tor/tor_navigation_throttle.h similarity index 65% rename from browser/tor/tor_navigation_throttle.h rename to components/tor/tor_navigation_throttle.h index 7ec0e58dcade..2b649bf07d40 100644 --- a/browser/tor/tor_navigation_throttle.h +++ b/components/tor/tor_navigation_throttle.h @@ -1,15 +1,15 @@ -/* Copyright (c) 2019 The Brave Authors. All rights reserved. +/* Copyright (c) 2020 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_BROWSER_TOR_TOR_NAVIGATION_THROTTLE_H_ -#define BRAVE_BROWSER_TOR_TOR_NAVIGATION_THROTTLE_H_ +#ifndef BRAVE_COMPONENTS_TOR_TOR_NAVIGATION_THROTTLE_H_ +#define BRAVE_COMPONENTS_TOR_TOR_NAVIGATION_THROTTLE_H_ #include #include "base/gtest_prod_util.h" -#include "brave/browser/tor/tor_launcher_service_observer.h" +#include "brave/components/tor/tor_launcher_service_observer.h" #include "content/public/browser/navigation_throttle.h" namespace content { @@ -23,9 +23,12 @@ class TorProfileService; class TorNavigationThrottle : public content::NavigationThrottle, public TorLauncherServiceObserver { public: - static std::unique_ptr - MaybeCreateThrottleFor(content::NavigationHandle* navigation_handle); - explicit TorNavigationThrottle(content::NavigationHandle* navigation_handle); + static std::unique_ptr MaybeCreateThrottleFor( + content::NavigationHandle* navigation_handle, + TorProfileService* service, + bool is_tor_profile); + TorNavigationThrottle(content::NavigationHandle* navigation_handle, + TorProfileService* service); ~TorNavigationThrottle() override; // content::NavigationThrottle implementation: @@ -47,4 +50,4 @@ class TorNavigationThrottle : public content::NavigationThrottle, } // namespace tor -#endif // BRAVE_BROWSER_TOR_TOR_NAVIGATION_THROTTLE_H_ +#endif // BRAVE_COMPONENTS_TOR_TOR_NAVIGATION_THROTTLE_H_ diff --git a/components/tor/tor_profile_service.cc b/components/tor/tor_profile_service.cc new file mode 100644 index 000000000000..7c4d33ac1090 --- /dev/null +++ b/components/tor/tor_profile_service.cc @@ -0,0 +1,40 @@ +/* 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/components/tor/tor_profile_service.h" + +#include + +#include "brave/components/tor/pref_names.h" +#include "brave/components/tor/tor_launcher_service_observer.h" +#include "components/pref_registry/pref_registry_syncable.h" +#include "components/prefs/pref_registry_simple.h" +#include "components/prefs/pref_service.h" + +namespace tor { + +TorProfileService::TorProfileService() = default; + +TorProfileService::~TorProfileService() = default; + +// static +void TorProfileService::RegisterLocalStatePrefs(PrefRegistrySimple* registry) { + registry->RegisterBooleanPref(prefs::kTorDisabled, false); +} + +// static +void TorProfileService::RegisterPrefs(PrefRegistrySimple* registry) { + registry->RegisterBooleanPref(prefs::kAutoOnionLocation, false); +} + +void TorProfileService::AddObserver(TorLauncherServiceObserver* observer) { + observers_.AddObserver(observer); +} + +void TorProfileService::RemoveObserver(TorLauncherServiceObserver* observer) { + observers_.RemoveObserver(observer); +} + +} // namespace tor diff --git a/browser/tor/tor_profile_service.h b/components/tor/tor_profile_service.h similarity index 71% rename from browser/tor/tor_profile_service.h rename to components/tor/tor_profile_service.h index 79b04d43c808..f2e43367be41 100644 --- a/browser/tor/tor_profile_service.h +++ b/components/tor/tor_profile_service.h @@ -1,19 +1,21 @@ -/* Copyright (c) 2019 The Brave Authors. All rights reserved. +/* Copyright (c) 2020 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_BROWSER_TOR_TOR_PROFILE_SERVICE_H_ -#define BRAVE_BROWSER_TOR_TOR_PROFILE_SERVICE_H_ +#ifndef BRAVE_COMPONENTS_TOR_TOR_PROFILE_SERVICE_H_ +#define BRAVE_COMPONENTS_TOR_TOR_PROFILE_SERVICE_H_ #include #include #include "base/macros.h" #include "base/observer_list.h" -#include "brave/common/tor/tor_common.h" #include "components/keyed_service/core/keyed_service.h" -#include "url/gurl.h" + +namespace base { +class FilePath; +} namespace content { class WebContents; @@ -28,7 +30,6 @@ class PrefRegistrySyncable; } class PrefRegistrySimple; -class BraveAppMenuBrowserTestWithTorDisabled; namespace tor { @@ -41,11 +42,9 @@ class TorProfileService : public KeyedService { static void RegisterLocalStatePrefs(PrefRegistrySimple* registry); static void RegisterPrefs(PrefRegistrySimple* registry); - static void SetTorDisabled(bool disabled); - static bool IsTorDisabled(); - static void RegisterTorClientUpdater(); - static void UnregisterTorClientUpdater(); + virtual void RegisterTorClientUpdater() = 0; + virtual void UnregisterTorClientUpdater() = 0; virtual void SetNewTorCircuit(content::WebContents* web_contents) = 0; virtual std::unique_ptr CreateProxyConfigService() = 0; @@ -55,7 +54,6 @@ class TorProfileService : public KeyedService { void RemoveObserver(TorLauncherServiceObserver* observer); protected: - base::FilePath GetTorExecutablePath(); base::ObserverList observers_; private: @@ -64,4 +62,4 @@ class TorProfileService : public KeyedService { } // namespace tor -#endif // BRAVE_BROWSER_TOR_TOR_PROFILE_SERVICE_H_ +#endif // BRAVE_COMPONENTS_TOR_TOR_PROFILE_SERVICE_H_ diff --git a/browser/tor/tor_profile_service_impl.cc b/components/tor/tor_profile_service_impl.cc similarity index 75% rename from browser/tor/tor_profile_service_impl.cc rename to components/tor/tor_profile_service_impl.cc index 3d7b48ab6f7c..62a52312fcc3 100644 --- a/browser/tor/tor_profile_service_impl.cc +++ b/components/tor/tor_profile_service_impl.cc @@ -3,7 +3,7 @@ * 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/tor/tor_profile_service_impl.h" +#include "brave/components/tor/tor_profile_service_impl.h" #include #include @@ -11,12 +11,11 @@ #include "base/bind.h" #include "base/task/post_task.h" -#include "brave/browser/brave_browser_process_impl.h" -#include "brave/browser/tor/tor_launcher_service_observer.h" -#include "brave/common/tor/pref_names.h" -#include "brave/common/tor/tor_constants.h" +#include "brave/components/tor/pref_names.h" +#include "brave/components/tor/tor_constants.h" +#include "brave/components/tor/tor_launcher_service_observer.h" #include "brave/net/proxy_resolution/proxy_config_service_tor.h" -#include "chrome/browser/profiles/profile.h" +#include "content/public/browser/browser_context.h" #include "content/public/browser/browser_task_traits.h" #include "content/public/browser/browser_thread.h" #include "content/public/browser/navigation_controller.h" @@ -78,9 +77,7 @@ class TorProxyLookupClient : public network::mojom::ProxyLookupClient { explicit TorProxyLookupClient(NewTorCircuitCallback callback) : callback_(std::move(callback)) {} - ~TorProxyLookupClient() override { - receiver_.reset(); - } + ~TorProxyLookupClient() override { receiver_.reset(); } mojo::PendingRemote GetProxyLookupClient() { @@ -96,8 +93,8 @@ class TorProxyLookupClient : public network::mojom::ProxyLookupClient { // network::mojom::ProxyLookupClient: void OnProxyLookupComplete( - int32_t net_error, - const base::Optional& proxy_info) override { + int32_t net_error, + const base::Optional& proxy_info) override { std::move(callback_).Run(proxy_info); delete this; } @@ -109,27 +106,26 @@ class TorProxyLookupClient : public network::mojom::ProxyLookupClient { }; void OnNewTorCircuit(std::unique_ptr tracker, - const base::Optional& proxy_info) { - tracker->NewIdentityLoaded( - proxy_info.has_value() && !proxy_info->is_direct()); + const base::Optional& proxy_info) { + tracker->NewIdentityLoaded(proxy_info.has_value() && + !proxy_info->is_direct()); } } // namespace -TorProfileServiceImpl::TorProfileServiceImpl(Profile* profile) - : profile_(profile), +TorProfileServiceImpl::TorProfileServiceImpl( + content::BrowserContext* context, + BraveTorClientUpdater* tor_client_updater, + const base::FilePath& user_data_dir) + : context_(context), + tor_client_updater_(tor_client_updater), + user_data_dir_(user_data_dir), tor_launcher_factory_(nullptr), weak_ptr_factory_(this) { - // Return early since g_brave_browser_process and tor_client_updater are not - // available in unit tests. - if (profile_->AsTestingProfile()) { - return; - } - - tor_launcher_factory_ = TorLauncherFactory::GetInstance(); - tor_launcher_factory_->AddObserver(this); - if (g_brave_browser_process) { - g_brave_browser_process->tor_client_updater()->AddObserver(this); + if (tor_client_updater_) { + tor_client_updater_->AddObserver(this); + tor_launcher_factory_ = TorLauncherFactory::GetInstance(); + tor_launcher_factory_->AddObserver(this); } } @@ -137,8 +133,8 @@ TorProfileServiceImpl::~TorProfileServiceImpl() { if (tor_launcher_factory_) tor_launcher_factory_->RemoveObserver(this); - if (g_brave_browser_process) { - g_brave_browser_process->tor_client_updater()->RemoveObserver(this); + if (tor_client_updater_) { + tor_client_updater_->RemoveObserver(this); } } @@ -152,10 +148,40 @@ void TorProfileServiceImpl::OnExecutableReady(const base::FilePath& path) { } void TorProfileServiceImpl::LaunchTor() { - tor::TorConfig config(GetTorExecutablePath()); + tor::mojom::TorConfig config(GetTorExecutablePath(), GetTorDataPath(), + GetTorWatchPath()); tor_launcher_factory_->LaunchTorProcess(config); } +base::FilePath TorProfileServiceImpl::GetTorExecutablePath() { + return tor_client_updater_ ? tor_client_updater_->GetExecutablePath() + : base::FilePath(); +} + +base::FilePath TorProfileServiceImpl::GetTorDataPath() { + DCHECK(!user_data_dir_.empty()); + return user_data_dir_.Append(FILE_PATH_LITERAL("tor")) + .Append(FILE_PATH_LITERAL("data")); +} + +base::FilePath TorProfileServiceImpl::GetTorWatchPath() { + DCHECK(!user_data_dir_.empty()); + return user_data_dir_.Append(FILE_PATH_LITERAL("tor")) + .Append(FILE_PATH_LITERAL("watch")); +} + +void TorProfileServiceImpl::RegisterTorClientUpdater() { + if (tor_client_updater_) { + tor_client_updater_->Register(); + } +} + +void TorProfileServiceImpl::UnregisterTorClientUpdater() { + if (tor_client_updater_) { + tor_client_updater_->Unregister(); + } +} + void TorProfileServiceImpl::SetNewTorCircuit(WebContents* tab) { DCHECK_CURRENTLY_ON(content::BrowserThread::UI); // track the webcontents lifetime so we don't reload if it has already @@ -170,10 +196,10 @@ void TorProfileServiceImpl::SetNewTorCircuit(WebContents* tab) { // Force lookup to erase the old circuit and also get a callback // so we know when it is safe to reload the tab auto* storage_partition = - BrowserContext::GetStoragePartitionForSite(profile_, url, false); + BrowserContext::GetStoragePartitionForSite(context_, url, false); if (!storage_partition) { storage_partition = - content::BrowserContext::GetDefaultStoragePartition(profile_); + content::BrowserContext::GetDefaultStoragePartition(context_); } auto proxy_lookup_client = TorProxyLookupClient::CreateTorProxyLookupClient(std::move(callback)); diff --git a/browser/tor/tor_profile_service_impl.h b/components/tor/tor_profile_service_impl.h similarity index 63% rename from browser/tor/tor_profile_service_impl.h rename to components/tor/tor_profile_service_impl.h index 6321b78c9b4f..f6e9446985db 100644 --- a/browser/tor/tor_profile_service_impl.h +++ b/components/tor/tor_profile_service_impl.h @@ -1,30 +1,30 @@ -/* Copyright (c) 2019 The Brave Authors. All rights reserved. +/* Copyright (c) 2020 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_BROWSER_TOR_TOR_PROFILE_SERVICE_IMPL_H_ -#define BRAVE_BROWSER_TOR_TOR_PROFILE_SERVICE_IMPL_H_ +#ifndef BRAVE_COMPONENTS_TOR_TOR_PROFILE_SERVICE_IMPL_H_ +#define BRAVE_COMPONENTS_TOR_TOR_PROFILE_SERVICE_IMPL_H_ #include #include #include "base/memory/weak_ptr.h" #include "base/optional.h" -#include "brave/browser/extensions/brave_tor_client_updater.h" -#include "brave/browser/tor/tor_launcher_factory.h" -#include "brave/browser/tor/tor_profile_service.h" +#include "brave/components/tor/brave_tor_client_updater.h" +#include "brave/components/tor/tor_launcher_factory.h" +#include "brave/components/tor/tor_profile_service.h" #include "net/proxy_resolution/proxy_info.h" -class Profile; +namespace content { +class BrowserContext; +} // namespace content namespace net { class ProxyConfigService; class ProxyConfigServiceTor; } -using extensions::BraveTorClientUpdater; - namespace tor { using NewTorCircuitCallback = base::OnceCallback CreateProxyConfigService() override; bool IsTorConnected() override; @@ -55,11 +59,17 @@ class TorProfileServiceImpl : public TorProfileService, private: void LaunchTor(); + base::FilePath GetTorExecutablePath(); + base::FilePath GetTorDataPath(); + base::FilePath GetTorWatchPath(); + // BraveTorClientUpdater::Observer void OnExecutableReady(const base::FilePath& path) override; bool is_tor_launched_for_test_ = false; - Profile* profile_; // NOT OWNED + content::BrowserContext* context_ = nullptr; + BraveTorClientUpdater* tor_client_updater_ = nullptr; + base::FilePath user_data_dir_; TorLauncherFactory* tor_launcher_factory_; // Singleton net::ProxyConfigServiceTor* proxy_config_service_; // NOT OWNED base::WeakPtrFactory weak_ptr_factory_; @@ -69,4 +79,4 @@ class TorProfileServiceImpl : public TorProfileService, } // namespace tor -#endif // BRAVE_BROWSER_TOR_TOR_PROFILE_SERVICE_IMPL_H_ +#endif // BRAVE_COMPONENTS_TOR_TOR_PROFILE_SERVICE_IMPL_H_ diff --git a/components/tor/tor_switches.h b/components/tor/tor_switches.h new file mode 100644 index 000000000000..b182bba63559 --- /dev/null +++ b/components/tor/tor_switches.h @@ -0,0 +1,16 @@ +/* Copyright 2020 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_COMPONENTS_TOR_TOR_SWITCHES_H_ +#define BRAVE_COMPONENTS_TOR_TOR_SWITCHES_H_ + +namespace tor { + +constexpr char kDisableTorClientUpdaterExtension[] = + "disable-tor-client-updater-extension"; + +} // namespace tor + +#endif // BRAVE_COMPONENTS_TOR_TOR_SWITCHES_H_ diff --git a/browser/tor/tor_tab_helper.cc b/components/tor/tor_tab_helper.cc similarity index 80% rename from browser/tor/tor_tab_helper.cc rename to components/tor/tor_tab_helper.cc index 9dff4939de5e..f340f5908e5d 100644 --- a/browser/tor/tor_tab_helper.cc +++ b/components/tor/tor_tab_helper.cc @@ -3,11 +3,9 @@ * 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/tor/tor_tab_helper.h" +#include "brave/components/tor/tor_tab_helper.h" #include "base/threading/sequenced_task_runner_handle.h" -#include "brave/browser/profiles/profile_util.h" -#include "chrome/browser/profiles/profile.h" #include "content/public/browser/navigation_handle.h" namespace tor { @@ -18,11 +16,9 @@ TorTabHelper::TorTabHelper(content::WebContents* web_contents) TorTabHelper::~TorTabHelper() = default; // static -void TorTabHelper::MaybeCreateForWebContents( - content::WebContents* web_contents) { - Profile* profile = - Profile::FromBrowserContext(web_contents->GetBrowserContext()); - if (!brave::IsTorProfile(profile)) +void TorTabHelper::MaybeCreateForWebContents(content::WebContents* web_contents, + bool is_tor_profile) { + if (!is_tor_profile) return; TorTabHelper::CreateForWebContents(web_contents); } diff --git a/browser/tor/tor_tab_helper.h b/components/tor/tor_tab_helper.h similarity index 85% rename from browser/tor/tor_tab_helper.h rename to components/tor/tor_tab_helper.h index 256fb6ad0bb1..14d4d0d8ffa6 100644 --- a/browser/tor/tor_tab_helper.h +++ b/components/tor/tor_tab_helper.h @@ -3,8 +3,8 @@ * 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_BROWSER_TOR_TOR_TAB_HELPER_H_ -#define BRAVE_BROWSER_TOR_TOR_TAB_HELPER_H_ +#ifndef BRAVE_COMPONENTS_TOR_TOR_TAB_HELPER_H_ +#define BRAVE_COMPONENTS_TOR_TOR_TAB_HELPER_H_ #include "base/memory/weak_ptr.h" #include "content/public/browser/web_contents_observer.h" @@ -23,7 +23,8 @@ class TorTabHelper : public content::WebContentsObserver, public: ~TorTabHelper() override; - static void MaybeCreateForWebContents(content::WebContents* web_contents); + static void MaybeCreateForWebContents(content::WebContents* web_contents, + bool is_tor_profile); private: friend class content::WebContentsUserData; @@ -42,4 +43,4 @@ class TorTabHelper : public content::WebContentsObserver, } // namespace tor -#endif // BRAVE_BROWSER_TOR_TOR_TAB_HELPER_H_ +#endif // BRAVE_COMPONENTS_TOR_TOR_TAB_HELPER_H_ diff --git a/net/proxy_resolution/BUILD.gn b/net/proxy_resolution/BUILD.gn new file mode 100644 index 000000000000..b583e42b5156 --- /dev/null +++ b/net/proxy_resolution/BUILD.gn @@ -0,0 +1,23 @@ +import("//brave/components/tor/buildflags/buildflags.gni") + +source_set("unit_tests") { + testonly = true + if (enable_tor) { + sources = [ + "configured_proxy_resolution_service_unittest.cc", + "proxy_config_service_tor_unittest.cc", + ] + + deps = [ + "//base", + "//base/test:test_support", + "//brave/test:test_support", + "//content/public/browser", + "//content/test:test_support", + "//net", + "//net:test_support", + "//testing/gtest", + "//url", + ] + } +} diff --git a/patches/mojo-public-tools-bindings-chromium_bindings_configuration.gni.patch b/patches/mojo-public-tools-bindings-chromium_bindings_configuration.gni.patch deleted file mode 100644 index b07f86bda560..000000000000 --- a/patches/mojo-public-tools-bindings-chromium_bindings_configuration.gni.patch +++ /dev/null @@ -1,12 +0,0 @@ -diff --git a/mojo/public/tools/bindings/chromium_bindings_configuration.gni b/mojo/public/tools/bindings/chromium_bindings_configuration.gni -index 9296a358091a7d8b9f4a858cba7254ddc917de71..706bd2e9579bae4d0a7dfad4caac106135a196a2 100644 ---- a/mojo/public/tools/bindings/chromium_bindings_configuration.gni -+++ b/mojo/public/tools/bindings/chromium_bindings_configuration.gni -@@ -4,6 +4,7 @@ - - _typemap_imports = [ - "//chrome/chrome_cleaner/mojom/typemaps/typemaps.gni", -+ "//brave/common/tor/typemaps.gni", - "//chrome/common/importer/typemaps.gni", - "//chrome/common/media_router/mojom/typemaps.gni", - "//chrome/typemaps.gni", diff --git a/test/BUILD.gn b/test/BUILD.gn index 1b13f4ddc9f1..0ba0acc327ed 100644 --- a/test/BUILD.gn +++ b/test/BUILD.gn @@ -1,4 +1,3 @@ -import("//brave/browser/tor/buildflags/buildflags.gni") import("//brave/browser/translate/buildflags/buildflags.gni") import("//brave/build/config.gni") import("//brave/build/rust/config.gni") @@ -19,6 +18,7 @@ import("//brave/components/greaselion/browser/buildflags/buildflags.gni") import("//brave/components/ipfs/buildflags/buildflags.gni") import("//brave/components/moonpay/browser/buildflags/buildflags.gni") import("//brave/components/speedreader/buildflags.gni") +import("//brave/components/tor/buildflags/buildflags.gni") import("//brave/test/testing.gni") import("//components/gcm_driver/config.gni") import("//testing/test.gni") @@ -145,6 +145,7 @@ test("brave_unit_tests") { ":test_support", "//brave/base:base_unittests", "//brave/browser/safebrowsing", + "//brave/browser/tor:unit_tests", "//brave/components/brave_ads/test:brave_ads_unit_tests", "//brave/components/brave_private_cdn", "//brave/components/brave_referrals/common", @@ -152,6 +153,8 @@ test("brave_unit_tests") { "//brave/components/ipfs/test:brave_ipfs_unit_tests", "//brave/components/ntp_background_images/browser", "//brave/components/ntp_widget_utils/browser", + "//brave/components/tor:tor_unit_tests", + "//brave/net/proxy_resolution:unit_tests", "//brave/vendor/brave_base", "//chrome:browser_dependencies", "//chrome:child_dependencies", @@ -356,12 +359,6 @@ test("brave_unit_tests") { sources += [ # TODO(darkdh): move these out and use buildflag guard once it contains non tor specifics "//brave/browser/profiles/brave_profile_manager_unittest.cc", - "//brave/browser/tor/tor_control_unittest.cc", - "//brave/browser/tor/tor_navigation_throttle_unittest.cc", - "//brave/common/tor/tor_test_constants.cc", - "//brave/common/tor/tor_test_constants.h", - "//brave/net/proxy_resolution/configured_proxy_resolution_service_unittest.cc", - "//brave/net/proxy_resolution/proxy_config_service_tor_unittest.cc", ] deps += [ "//third_party/blink/public/common" ] @@ -497,7 +494,6 @@ if (!is_android) { "//brave/app/brave_main_delegate_runtime_flags_browsertest.cc", "//brave/browser/autoplay/autoplay_permission_context_browsertest.cc", "//brave/browser/brave_content_browser_client_browsertest.cc", - "//brave/browser/brave_local_state_browsertest.cc", "//brave/browser/brave_profile_prefs_browsertest.cc", "//brave/browser/brave_resources_browsertest.cc", "//brave/browser/brave_shields/ad_block_service_browsertest.cc", @@ -535,7 +531,6 @@ if (!is_android) { "//brave/browser/search_engines/search_engine_tracker_browsertest.cc", "//brave/browser/themes/brave_dark_mode_prefs_migration_browsertest.cc", "//brave/browser/themes/brave_theme_service_browsertest.cc", - "//brave/browser/tor/onion_location_navigation_throttle_browsertest.cc", "//brave/browser/ui/bookmark/bookmark_tab_helper_browsertest.cc", "//brave/browser/ui/brave_browser_command_controller_browsertest.cc", "//brave/browser/ui/content_settings/brave_autoplay_blocked_image_model_browsertest.cc", @@ -599,6 +594,7 @@ if (!is_android) { } deps = [ + "//brave/browser/tor:browser_tests", "//brave/components/ipfs/test:brave_ipfs_browser_tests", "//brave/browser/ui/tabs/test:browser_tests", ] @@ -697,12 +693,6 @@ if (!is_android) { configs += [ "//brave/vendor/bat-native-ledger:internal_config" ] } - if (enable_tor) { - sources += [ - "//brave/browser/extensions/brave_tor_client_updater_browsertest.cc", - ] - } - if (enable_brave_wayback_machine) { sources += [ "//brave/components/brave_wayback_machine/brave_wayback_machine_browsertest.cc" ] } @@ -743,9 +733,7 @@ if (!is_android) { deps += [ ":brave_browser_tests_deps", "//brave/app:command_ids", - "//brave/browser/tor:buildflags", "//brave/common:pref_names", - "//brave/common/tor:pref_names", "//brave/components/brave_referrals/common", "//brave/components/brave_rewards/browser/buildflags:buildflags", "//brave/components/brave_sync/buildflags:buildflags", diff --git a/utility/BUILD.gn b/utility/BUILD.gn index 1b23a8de2be1..31855780e4ec 100644 --- a/utility/BUILD.gn +++ b/utility/BUILD.gn @@ -1,7 +1,7 @@ -import("//brave/browser/tor/buildflags/buildflags.gni") import("//brave/components/brave_ads/browser/buildflags/buildflags.gni") import("//brave/components/brave_rewards/browser/buildflags/buildflags.gni") import("//brave/components/ipfs/buildflags/buildflags.gni") +import("//brave/components/tor/buildflags/buildflags.gni") import("//build/config/features.gni") import("//build/config/ui.gni")