From 08e8604cd44f2b06ae04b7c0a38e7f12e44b5e27 Mon Sep 17 00:00:00 2001 From: Anthony Tseng Date: Wed, 14 Oct 2020 14:56:50 -0700 Subject: [PATCH 01/10] Move buildflag --- browser/BUILD.gn | 4 ++-- browser/brave_browser_main_parts.cc | 2 +- browser/brave_browser_process_impl.cc | 1 - browser/brave_browser_process_impl.h | 2 +- browser/brave_content_browser_client.cc | 2 +- browser/brave_local_state_browsertest.cc | 2 +- browser/brave_local_state_prefs.cc | 2 +- browser/brave_profile_prefs.cc | 2 +- browser/brave_tab_helpers.cc | 2 +- browser/extensions/BUILD.gn | 4 ++-- .../api/settings_private/brave_prefs_util.cc | 2 +- browser/extensions/brave_extension_management.cc | 4 ++-- .../ipfs/content_browser_client_helper_unittest.cc | 2 +- browser/policy/brave_policy_browsertest.cc | 2 +- browser/profiles/brave_profile_manager.cc | 2 +- .../profiles/brave_profile_manager_browsertest.cc | 2 +- browser/profiles/profile_util.cc | 2 +- browser/resources/settings/BUILD.gn | 2 +- .../search_engine_provider_service_browsertest.cc | 2 +- browser/tor/BUILD.gn | 12 ++++-------- browser/tor/tor_profile_service_factory.cc | 2 +- browser/ui/brave_browser_command_controller.cc | 1 - browser/ui/brave_browser_command_controller.h | 2 +- .../brave_browser_command_controller_browsertest.cc | 2 +- .../ui/toolbar/brave_app_menu_model_browsertest.cc | 2 +- .../ui/views/profiles/brave_incognito_menu_view.cc | 2 +- .../settings/brave_default_extensions_handler.cc | 1 - .../settings/brave_default_extensions_handler.h | 2 +- .../chrome/browser/net/proxy_config_monitor.cc | 2 +- .../configuration_policy_handler_list_factory.cc | 2 +- .../browser/profiles/pref_service_builder_utils.cc | 2 +- .../render_view_context_menu.cc | 2 +- chromium_src/chrome/utility/services.cc | 2 +- components/ipfs/ipfs_navigation_throttle_unittest.cc | 2 +- components/ipfs/test/BUILD.gn | 2 +- components/tor/buildflags/BUILD.gn | 7 +++++++ .../tor/buildflags/buildflags.gni | 0 test/BUILD.gn | 4 ++-- utility/BUILD.gn | 2 +- 39 files changed, 48 insertions(+), 48 deletions(-) create mode 100644 components/tor/buildflags/BUILD.gn rename {browser => components}/tor/buildflags/buildflags.gni (100%) diff --git a/browser/BUILD.gn b/browser/BUILD.gn index d14040d410b6..c2837ffce4fe 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", diff --git a/browser/brave_browser_main_parts.cc b/browser/brave_browser_main_parts.cc index dd2304d81dd0..29c30d0d44df 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" diff --git a/browser/brave_browser_process_impl.cc b/browser/brave_browser_process_impl.cc index 7d7536409815..6c8c0c9e60ab 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" diff --git a/browser/brave_browser_process_impl.h b/browser/brave_browser_process_impl.h index c4ffe87e09f9..6e04b1e3b008 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" diff --git a/browser/brave_content_browser_client.cc b/browser/brave_content_browser_client.cc index 591ba586c758..7c6f14e1da58 100644 --- a/browser/brave_content_browser_client.cc +++ b/browser/brave_content_browser_client.cc @@ -20,7 +20,6 @@ #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 +34,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" diff --git a/browser/brave_local_state_browsertest.cc b/browser/brave_local_state_browsertest.cc index 8eabdf9226e1..5cad5ef4efb6 100644 --- a/browser/brave_local_state_browsertest.cc +++ b/browser/brave_local_state_browsertest.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/buildflags.h" +#include "brave/components/tor/buildflags/buildflags.h" #include "chrome/browser/browser_process.h" #include "chrome/test/base/in_process_browser_test.h" #include "content/public/test/browser_test.h" diff --git a/browser/brave_local_state_prefs.cc b/browser/brave_local_state_prefs.cc index 2da4c7e5f97b..e47b5486d7cf 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" diff --git a/browser/brave_profile_prefs.cc b/browser/brave_profile_prefs.cc index dfdb7d986018..1b9a7db4cd95 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" diff --git a/browser/brave_tab_helpers.cc b/browser/brave_tab_helpers.cc index c3bf429ff674..cb157f0f5764 100644 --- a/browser/brave_tab_helpers.cc +++ b/browser/brave_tab_helpers.cc @@ -9,7 +9,6 @@ #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/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 +19,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" 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..fb3ed0012618 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" diff --git a/browser/extensions/brave_extension_management.cc b/browser/extensions/brave_extension_management.cc index 29f95ed66dae..b35e182d164f 100644 --- a/browser/extensions/brave_extension_management.cc +++ b/browser/extensions/brave_extension_management.cc @@ -8,10 +8,10 @@ #include #include "brave/browser/brave_browser_process_impl.h" -#include "brave/browser/tor/buildflags.h" +#include "brave/browser/extensions/brave_extension_provider.h" #include "brave/common/pref_names.h" #include "brave/common/tor/pref_names.h" -#include "brave/browser/extensions/brave_extension_provider.h" +#include "brave/components/tor/buildflags/buildflags.h" #include "chrome/browser/extensions/external_policy_loader.h" #include "chrome/browser/profiles/profile.h" #include "components/prefs/pref_service.h" 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..6428d082e07c 100644 --- a/browser/policy/brave_policy_browsertest.cc +++ b/browser/policy/brave_policy_browsertest.cc @@ -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/. */ -#include "brave/browser/tor/buildflags.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" diff --git a/browser/profiles/brave_profile_manager.cc b/browser/profiles/brave_profile_manager.cc index 3fc26e9b2b81..b343b6ae0f6b 100644 --- a/browser/profiles/brave_profile_manager.cc +++ b/browser/profiles/brave_profile_manager.cc @@ -12,7 +12,6 @@ #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" @@ -26,6 +25,7 @@ #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/content/browser/webui/brave_shared_resources_data_source.h" #include "chrome/browser/browser_process.h" #include "chrome/browser/chrome_notification_types.h" diff --git a/browser/profiles/brave_profile_manager_browsertest.cc b/browser/profiles/brave_profile_manager_browsertest.cc index 4103c716bb6f..9ecbe361229b 100644 --- a/browser/profiles/brave_profile_manager_browsertest.cc +++ b/browser/profiles/brave_profile_manager_browsertest.cc @@ -11,10 +11,10 @@ #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" diff --git a/browser/profiles/profile_util.cc b/browser/profiles/profile_util.cc index 4df4987d3ea1..55e3f9fd67fe 100644 --- a/browser/profiles/profile_util.cc +++ b/browser/profiles/profile_util.cc @@ -12,8 +12,8 @@ #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 "chrome/browser/browser_process.h" #include "chrome/browser/profiles/profile.h" #include "chrome/browser/profiles/profile_key.h" 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..73d2d6504d8e 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" diff --git a/browser/tor/BUILD.gn b/browser/tor/BUILD.gn index e25a21ade813..e361ad59d101 100644 --- a/browser/tor/BUILD.gn +++ b/browser/tor/BUILD.gn @@ -1,10 +1,4 @@ -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 @@ -24,7 +18,9 @@ source_set("tor") { "//mojo/public/cpp/bindings", ] - public_deps = [ ":buildflags" ] + public_deps = [ + "//brave/components/tor/buildflags", + ] if (enable_tor) { sources += [ diff --git a/browser/tor/tor_profile_service_factory.cc b/browser/tor/tor_profile_service_factory.cc index 700a66f40c29..b0e7c6ee434b 100644 --- a/browser/tor/tor_profile_service_factory.cc +++ b/browser/tor/tor_profile_service_factory.cc @@ -9,8 +9,8 @@ #include #include "brave/browser/tor/tor_profile_service_impl.h" -#include "brave/browser/tor/buildflags.h" #include "brave/common/tor/pref_names.h" +#include "brave/components/tor/buildflags/buildflags.h" #include "chrome/browser/profiles/incognito_helpers.h" #include "chrome/browser/profiles/profile.h" #include "components/keyed_service/content/browser_context_dependency_manager.h" 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..bb8769606500 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" diff --git a/browser/ui/toolbar/brave_app_menu_model_browsertest.cc b/browser/ui/toolbar/brave_app_menu_model_browsertest.cc index 172f74eafd18..1d65d880c6c8 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" diff --git a/browser/ui/views/profiles/brave_incognito_menu_view.cc b/browser/ui/views/profiles/brave_incognito_menu_view.cc index 787bbbe951b3..f9c760d28fab 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" diff --git a/browser/ui/webui/settings/brave_default_extensions_handler.cc b/browser/ui/webui/settings/brave_default_extensions_handler.cc index 10f0dd76c9b3..a9de31f33457 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" diff --git a/browser/ui/webui/settings/brave_default_extensions_handler.h b/browser/ui/webui/settings/brave_default_extensions_handler.h index 1cdd340edc51..0af0cbcee3f1 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" diff --git a/chromium_src/chrome/browser/net/proxy_config_monitor.cc b/chromium_src/chrome/browser/net/proxy_config_monitor.cc index 513cc914aa43..e2cdaffda2c7 100644 --- a/chromium_src/chrome/browser/net/proxy_config_monitor.cc +++ b/chromium_src/chrome/browser/net/proxy_config_monitor.cc @@ -6,7 +6,7 @@ #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" 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..e0887dfae5a5 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 "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/renderer_context_menu/render_view_context_menu.cc b/chromium_src/chrome/browser/renderer_context_menu/render_view_context_menu.cc index 0b292c5775e5..9cee684c622f 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,9 +6,9 @@ #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/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" 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/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/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/test/BUILD.gn b/test/BUILD.gn index 1b13f4ddc9f1..d299e3e49212 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") @@ -743,7 +743,6 @@ 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", @@ -751,6 +750,7 @@ if (!is_android) { "//brave/components/brave_sync/buildflags:buildflags", "//brave/components/brave_wallet/buildflags:buildflags", "//brave/components/ipfs/buildflags:buildflags", + "//brave/components/tor/buildflags", "//chrome/browser/ui", "//chrome/test:test_support_ui", "//components/embedder_support", 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") From 64d6268d67235dc715ad657d2ebe6c86be6676b5 Mon Sep 17 00:00:00 2001 From: Anthony Tseng Date: Thu, 15 Oct 2020 11:01:28 -0700 Subject: [PATCH 02/10] Remove TorConfig typemap --- browser/tor/BUILD.gn | 1 - browser/tor/tor_launcher_factory.cc | 25 +++++------- browser/tor/tor_launcher_factory.h | 8 +--- browser/tor/tor_profile_service.h | 5 ++- browser/tor/tor_profile_service_impl.cc | 33 +++++++++++----- browser/tor/tor_profile_service_impl.h | 3 ++ common/tor/BUILD.gn | 5 --- common/tor/tor_common.cc | 36 ------------------ common/tor/tor_common.h | 33 ---------------- common/tor/tor_config.typemap | 15 -------- common/tor/tor_config_struct_traits.cc | 27 ------------- common/tor/tor_config_struct_traits.h | 38 ------------------- common/tor/typemaps.gni | 7 ---- components/services/tor/BUILD.gn | 1 - components/services/tor/tor_launcher_impl.cc | 8 ++-- components/services/tor/tor_launcher_impl.h | 2 +- ...-chromium_bindings_configuration.gni.patch | 12 ------ 17 files changed, 48 insertions(+), 211 deletions(-) delete mode 100644 common/tor/tor_common.cc delete mode 100644 common/tor/tor_common.h delete mode 100644 common/tor/tor_config.typemap delete mode 100644 common/tor/tor_config_struct_traits.cc delete mode 100644 common/tor/tor_config_struct_traits.h delete mode 100644 common/tor/typemaps.gni delete mode 100644 patches/mojo-public-tools-bindings-chromium_bindings_configuration.gni.patch diff --git a/browser/tor/BUILD.gn b/browser/tor/BUILD.gn index e361ad59d101..aeab347c3ba7 100644 --- a/browser/tor/BUILD.gn +++ b/browser/tor/BUILD.gn @@ -11,7 +11,6 @@ source_set("tor") { deps = [ "//base", - "//brave/common/tor", "//components/keyed_service/content", "//components/keyed_service/core", "//content/public/browser", diff --git a/browser/tor/tor_launcher_factory.cc b/browser/tor/tor_launcher_factory.cc index a1e741e061cd..acd4a8f1f6ae 100644 --- a/browser/tor/tor_launcher_factory.cc +++ b/browser/tor/tor_launcher_factory.cc @@ -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/browser/tor/tor_launcher_factory.h index 1b8f9e6e1eba..5d47dfb18a39 100644 --- a/browser/tor/tor_launcher_factory.h +++ b/browser/tor/tor_launcher_factory.h @@ -15,7 +15,6 @@ #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 "mojo/public/cpp/bindings/remote.h" @@ -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_; diff --git a/browser/tor/tor_profile_service.h b/browser/tor/tor_profile_service.h index 79b04d43c808..467244a2a774 100644 --- a/browser/tor/tor_profile_service.h +++ b/browser/tor/tor_profile_service.h @@ -11,10 +11,13 @@ #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; } diff --git a/browser/tor/tor_profile_service_impl.cc b/browser/tor/tor_profile_service_impl.cc index 3d7b48ab6f7c..d37dc5e3c02a 100644 --- a/browser/tor/tor_profile_service_impl.cc +++ b/browser/tor/tor_profile_service_impl.cc @@ -10,6 +10,7 @@ #include #include "base/bind.h" +#include "base/path_service.h" #include "base/task/post_task.h" #include "brave/browser/brave_browser_process_impl.h" #include "brave/browser/tor/tor_launcher_service_observer.h" @@ -17,6 +18,7 @@ #include "brave/common/tor/tor_constants.h" #include "brave/net/proxy_resolution/proxy_config_service_tor.h" #include "chrome/browser/profiles/profile.h" +#include "chrome/common/chrome_paths.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 +80,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 +96,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,9 +109,9 @@ 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 @@ -152,9 +152,24 @@ 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::GetTorDataPath() { + base::FilePath user_data_dir; + base::PathService::Get(chrome::DIR_USER_DATA, &user_data_dir); + DCHECK(!user_data_dir.empty()); + return user_data_dir.Append(FILE_PATH_LITERAL("tor")) + .Append(FILE_PATH_LITERAL("data")); +} +base::FilePath TorProfileServiceImpl::GetTorWatchPath() { + base::FilePath user_data_dir; + base::PathService::Get(chrome::DIR_USER_DATA, &user_data_dir); + DCHECK(!user_data_dir.empty()); + return user_data_dir.Append(FILE_PATH_LITERAL("tor")) + .Append(FILE_PATH_LITERAL("watch")); +} void TorProfileServiceImpl::SetNewTorCircuit(WebContents* tab) { DCHECK_CURRENTLY_ON(content::BrowserThread::UI); diff --git a/browser/tor/tor_profile_service_impl.h b/browser/tor/tor_profile_service_impl.h index 6321b78c9b4f..fbdede80a528 100644 --- a/browser/tor/tor_profile_service_impl.h +++ b/browser/tor/tor_profile_service_impl.h @@ -55,6 +55,9 @@ class TorProfileServiceImpl : public TorProfileService, private: void LaunchTor(); + base::FilePath GetTorDataPath(); + base::FilePath GetTorWatchPath(); + // BraveTorClientUpdater::Observer void OnExecutableReady(const base::FilePath& path) override; diff --git a/common/tor/BUILD.gn b/common/tor/BUILD.gn index f73fb125d6c3..6aa6c9eb75b2 100644 --- a/common/tor/BUILD.gn +++ b/common/tor/BUILD.gn @@ -1,9 +1,5 @@ -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", @@ -14,7 +10,6 @@ source_set("tor") { deps = [ ":pref_names", "//base", - "//brave/components/services/tor/public/interfaces", "//chrome/common:constants", "//url:url", ] 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/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/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/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", From 1d74cf94c3a75c355fb6acfbcc6a4b9d46b3f938 Mon Sep 17 00:00:00 2001 From: Anthony Tseng Date: Thu, 15 Oct 2020 16:25:01 -0700 Subject: [PATCH 03/10] Move browser/tor & common/tor except navigation_throttle and tab_helper --- browser/BUILD.gn | 2 +- browser/brave_browser_main_parts.cc | 2 +- browser/brave_browser_process_impl.cc | 2 +- browser/brave_local_state_browsertest.cc | 9 +-- browser/brave_local_state_prefs.cc | 2 +- browser/brave_profile_prefs.cc | 2 +- .../api/settings_private/brave_prefs_util.cc | 2 +- .../extensions/brave_extension_management.cc | 6 +- .../extensions/brave_tor_client_updater.cc | 6 +- browser/policy/brave_policy_browsertest.cc | 16 ++-- browser/profiles/BUILD.gn | 2 +- browser/profiles/brave_profile_manager.cc | 6 +- .../brave_profile_manager_browsertest.cc | 2 +- .../brave_profile_manager_unittest.cc | 4 +- browser/profiles/profile_util.cc | 9 +-- ...rch_engine_provider_service_browsertest.cc | 2 +- .../search_engine_tracker_browsertest.cc | 2 +- browser/tor/BUILD.gn | 16 +--- .../tor/onion_location_navigation_throttle.cc | 6 +- ...ocation_navigation_throttle_browsertest.cc | 6 +- browser/tor/tor_control_unittest.cc | 2 +- browser/tor/tor_navigation_throttle.cc | 2 +- browser/tor/tor_navigation_throttle.h | 2 +- .../tor/tor_navigation_throttle_unittest.cc | 2 +- browser/tor/tor_profile_service.cc | 76 ------------------- browser/tor/tor_profile_service_factory.cc | 35 +++++++-- browser/tor/tor_profile_service_factory.h | 3 + browser/ui/BUILD.gn | 2 +- ..._browser_command_controller_browsertest.cc | 7 +- browser/ui/browser_commands.cc | 2 +- .../profiles/brave_incognito_menu_view.cc | 4 +- .../ui/webui/brave_new_tab_message_handler.cc | 2 +- .../ui/webui/brave_new_tab_message_handler.h | 2 +- .../brave_default_extensions_handler.cc | 10 +-- .../browser/net/proxy_config_monitor.cc | 2 +- ...nfiguration_policy_handler_list_factory.cc | 2 +- .../chrome/browser/profiles/profile_window.cc | 2 +- .../render_view_context_menu.cc | 16 +--- .../chrome/browser/service_sandbox_type.h | 12 --- common/tor/BUILD.gn | 23 ------ components/tor/BUILD.gn | 52 +++++++++++++ {common => components}/tor/pref_names.cc | 2 +- {common => components}/tor/pref_names.h | 8 +- components/tor/service_sandbox_type.h | 23 ++++++ {common => components}/tor/tor_constants.cc | 5 +- {common => components}/tor/tor_constants.h | 9 ++- {browser => components}/tor/tor_control.cc | 2 +- {browser => components}/tor/tor_control.h | 8 +- .../tor/tor_control_event.cc | 2 +- .../tor/tor_control_event.h | 6 +- .../tor/tor_control_event_list.h | 0 .../tor/tor_launcher_factory.cc | 6 +- .../tor/tor_launcher_factory.h | 10 +-- .../tor/tor_launcher_service_observer.h | 8 +- components/tor/tor_profile_service.cc | 42 ++++++++++ .../tor/tor_profile_service.h | 17 ++--- .../tor/tor_profile_service_impl.cc | 75 ++++++++++-------- .../tor/tor_profile_service_impl.h | 28 ++++--- test/BUILD.gn | 2 +- 59 files changed, 315 insertions(+), 302 deletions(-) delete mode 100644 browser/tor/tor_profile_service.cc delete mode 100644 common/tor/BUILD.gn create mode 100644 components/tor/BUILD.gn rename {common => components}/tor/pref_names.cc (91%) rename {common => components}/tor/pref_names.h (68%) create mode 100644 components/tor/service_sandbox_type.h rename {common => components}/tor/tor_constants.cc (61%) rename {common => components}/tor/tor_constants.h (52%) rename {browser => components}/tor/tor_control.cc (99%) rename {browser => components}/tor/tor_control.h (97%) rename {common => components}/tor/tor_control_event.cc (93%) rename {common => components}/tor/tor_control_event.h (81%) rename {common => components}/tor/tor_control_event_list.h (100%) rename {browser => components}/tor/tor_launcher_factory.cc (98%) rename {browser => components}/tor/tor_launcher_factory.h (90%) rename {browser => components}/tor/tor_launcher_service_observer.h (73%) create mode 100644 components/tor/tor_profile_service.cc rename {browser => components}/tor/tor_profile_service.h (74%) rename {browser => components}/tor/tor_profile_service_impl.cc (82%) rename {browser => components}/tor/tor_profile_service_impl.h (67%) diff --git a/browser/BUILD.gn b/browser/BUILD.gn index c2837ffce4fe..dcc5e2b9b34e 100644 --- a/browser/BUILD.gn +++ b/browser/BUILD.gn @@ -330,7 +330,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 29c30d0d44df..bc854312333a 100644 --- a/browser/brave_browser_main_parts.cc +++ b/browser/brave_browser_main_parts.cc @@ -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 6c8c0c9e60ab..b01aa980b11b 100644 --- a/browser/brave_browser_process_impl.cc +++ b/browser/brave_browser_process_impl.cc @@ -70,7 +70,7 @@ #if BUILDFLAG(ENABLE_TOR) #include "brave/browser/extensions/brave_tor_client_updater.h" -#include "brave/common/tor/pref_names.h" +#include "brave/components/tor/pref_names.h" #endif #if BUILDFLAG(IPFS_ENABLED) diff --git a/browser/brave_local_state_browsertest.cc b/browser/brave_local_state_browsertest.cc index 5cad5ef4efb6..75c31a7cc2fb 100644 --- a/browser/brave_local_state_browsertest.cc +++ b/browser/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/components/tor/buildflags/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/brave_local_state_prefs.cc b/browser/brave_local_state_prefs.cc index e47b5486d7cf..fa2179051867 100644 --- a/browser/brave_local_state_prefs.cc +++ b/browser/brave_local_state_prefs.cc @@ -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 1b9a7db4cd95..908c05e32770 100644 --- a/browser/brave_profile_prefs.cc +++ b/browser/brave_profile_prefs.cc @@ -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/extensions/api/settings_private/brave_prefs_util.cc b/browser/extensions/api/settings_private/brave_prefs_util.cc index fb3ed0012618..10f4dc8db73b 100644 --- a/browser/extensions/api/settings_private/brave_prefs_util.cc +++ b/browser/extensions/api/settings_private/brave_prefs_util.cc @@ -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 b35e182d164f..d099d4406a19 100644 --- a/browser/extensions/brave_extension_management.cc +++ b/browser/extensions/brave_extension_management.cc @@ -10,8 +10,9 @@ #include "brave/browser/brave_browser_process_impl.h" #include "brave/browser/extensions/brave_extension_provider.h" #include "brave/common/pref_names.h" -#include "brave/common/tor/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" @@ -22,7 +23,6 @@ #if BUILDFLAG(ENABLE_TOR) #include "brave/browser/extensions/brave_tor_client_updater.h" -#include "brave/browser/tor/tor_profile_service.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/extensions/brave_tor_client_updater.cc b/browser/extensions/brave_tor_client_updater.cc index 10b5dc4e67a4..a265048919db 100644 --- a/browser/extensions/brave_tor_client_updater.cc +++ b/browser/extensions/brave_tor_client_updater.cc @@ -16,7 +16,7 @@ #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/browser/tor/tor_profile_service_factory.h" #include "brave/common/brave_switches.h" #include "chrome/browser/browser_process.h" #include "chrome/browser/profiles/profile_manager.h" @@ -124,7 +124,7 @@ BraveTorClientUpdater::~BraveTorClientUpdater() { void BraveTorClientUpdater::Register() { const base::CommandLine& command_line = *base::CommandLine::ForCurrentProcess(); - if (tor::TorProfileService::IsTorDisabled() || + if (TorProfileServiceFactory::IsTorDisabled() || command_line.HasSwitch(switches::kDisableTorClientUpdaterExtension) || registered_) { return; @@ -144,7 +144,7 @@ void BraveTorClientUpdater::Unregister() { void BraveTorClientUpdater::Cleanup() { // Delete tor binaries if tor is disabled. - if (tor::TorProfileService::IsTorDisabled()) { + if (TorProfileServiceFactory::IsTorDisabled()) { ProfileManager* profile_manager = g_browser_process->profile_manager(); base::FilePath tor_component_dir = profile_manager->user_data_dir().AppendASCII(kTorClientComponentId); diff --git a/browser/policy/brave_policy_browsertest.cc b/browser/policy/brave_policy_browsertest.cc index 6428d082e07c..5e790f3010dc 100644 --- a/browser/policy/brave_policy_browsertest.cc +++ b/browser/policy/brave_policy_browsertest.cc @@ -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/. */ +#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 +15,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; @@ -39,7 +35,6 @@ class BravePolicyTest : public InProcessBrowserTest { MockConfigurationPolicyProvider provider_; }; -#if BUILDFLAG(ENABLE_TOR) // This policy only exists on Windows. // Sets the tor policy before the browser is started. class TorDisabledPolicyBrowserTest : public BravePolicyTest { @@ -60,8 +55,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 +77,8 @@ 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 } // 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 b343b6ae0f6b..8d593946a948 100644 --- a/browser/profiles/brave_profile_manager.cc +++ b/browser/profiles/brave_profile_manager.cc @@ -12,12 +12,9 @@ #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/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 +23,9 @@ #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" diff --git a/browser/profiles/brave_profile_manager_browsertest.cc b/browser/profiles/brave_profile_manager_browsertest.cc index 9ecbe361229b..ce348d591d7e 100644 --- a/browser/profiles/brave_profile_manager_browsertest.cc +++ b/browser/profiles/brave_profile_manager_browsertest.cc @@ -38,7 +38,7 @@ #include "content/public/test/test_utils.h" #if BUILDFLAG(ENABLE_TOR) -#include "brave/browser/tor/tor_launcher_factory.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..ddfd096924ab 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" diff --git a/browser/profiles/profile_util.cc b/browser/profiles/profile_util.cc index 55e3f9fd67fe..a832052a0ebe 100644 --- a/browser/profiles/profile_util.cc +++ b/browser/profiles/profile_util.cc @@ -12,17 +12,14 @@ #include "base/files/file_path.h" #include "base/memory/ptr_util.h" #include "base/no_destructor.h" -#include "brave/common/tor/tor_constants.h" +#include "brave/browser/tor/tor_profile_service_factory.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" -#endif - namespace brave { namespace { @@ -201,7 +198,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/search_engines/search_engine_provider_service_browsertest.cc b/browser/search_engines/search_engine_provider_service_browsertest.cc index 73d2d6504d8e..85eeda4c6cbf 100644 --- a/browser/search_engines/search_engine_provider_service_browsertest.cc +++ b/browser/search_engines/search_engine_provider_service_browsertest.cc @@ -28,7 +28,7 @@ #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/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..bf0681149160 100644 --- a/browser/search_engines/search_engine_tracker_browsertest.cc +++ b/browser/search_engines/search_engine_tracker_browsertest.cc @@ -8,8 +8,8 @@ #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/tor_launcher_factory.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" diff --git a/browser/tor/BUILD.gn b/browser/tor/BUILD.gn index aeab347c3ba7..77ffd510d5d1 100644 --- a/browser/tor/BUILD.gn +++ b/browser/tor/BUILD.gn @@ -4,13 +4,14 @@ 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/components/tor", + "//chrome/common", "//components/keyed_service/content", "//components/keyed_service/core", "//content/public/browser", @@ -27,29 +28,16 @@ source_set("tor") { "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", ] deps += [ - "//brave/app:brave_generated_resources_grit", - "//brave/components/services/tor/public/interfaces", "//content/public/browser", "//content/public/common", "//extensions/common:common_constants", - "//net", - "//services/service_manager", - "//url", ] # Below dep list are not directly used tor target. diff --git a/browser/tor/onion_location_navigation_throttle.cc b/browser/tor/onion_location_navigation_throttle.cc index 698dcfba4cb1..d0ee348246fb 100644 --- a/browser/tor/onion_location_navigation_throttle.cc +++ b/browser/tor/onion_location_navigation_throttle.cc @@ -11,8 +11,8 @@ #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 "brave/browser/tor/tor_profile_service_factory.h" +#include "brave/components/tor/pref_names.h" #include "chrome/browser/profiles/profile.h" #include "chrome/browser/profiles/profile_window.h" #include "chrome/browser/ui/browser.h" @@ -56,7 +56,7 @@ void OnTorProfileCreated(GURL onion_location, std::unique_ptr OnionLocationNavigationThrottle::MaybeCreateThrottleFor( content::NavigationHandle* navigation_handle) { - if (tor::TorProfileService::IsTorDisabled() || + if (TorProfileServiceFactory::IsTorDisabled() || !navigation_handle->IsInMainFrame()) return nullptr; return std::make_unique(navigation_handle); diff --git a/browser/tor/onion_location_navigation_throttle_browsertest.cc b/browser/tor/onion_location_navigation_throttle_browsertest.cc index 5b75795546b5..cbc105d98975 100644 --- a/browser/tor/onion_location_navigation_throttle_browsertest.cc +++ b/browser/tor/onion_location_navigation_throttle_browsertest.cc @@ -6,11 +6,11 @@ #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/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/tor_control_unittest.cc b/browser/tor/tor_control_unittest.cc index 6c7aaf3e35bb..03ee4bbff369 100644 --- a/browser/tor/tor_control_unittest.cc +++ b/browser/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_navigation_throttle.cc b/browser/tor/tor_navigation_throttle.cc index b91891dc4a5e..7beed84ba68d 100644 --- a/browser/tor/tor_navigation_throttle.cc +++ b/browser/tor/tor_navigation_throttle.cc @@ -8,8 +8,8 @@ #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 "brave/components/tor/tor_profile_service.h" #include "chrome/browser/profiles/profile.h" #include "content/public/browser/navigation_handle.h" #include "content/public/browser/web_contents.h" diff --git a/browser/tor/tor_navigation_throttle.h b/browser/tor/tor_navigation_throttle.h index 7ec0e58dcade..01f4ca1c0abb 100644 --- a/browser/tor/tor_navigation_throttle.h +++ b/browser/tor/tor_navigation_throttle.h @@ -9,7 +9,7 @@ #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 { diff --git a/browser/tor/tor_navigation_throttle_unittest.cc b/browser/tor/tor_navigation_throttle_unittest.cc index 240ce67eb2cd..c6927170cad8 100644 --- a/browser/tor/tor_navigation_throttle_unittest.cc +++ b/browser/tor/tor_navigation_throttle_unittest.cc @@ -12,8 +12,8 @@ #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_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" 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 b0e7c6ee434b..c60550504a9b 100644 --- a/browser/tor/tor_profile_service_factory.cc +++ b/browser/tor/tor_profile_service_factory.cc @@ -8,16 +8,19 @@ #include #include -#include "brave/browser/tor/tor_profile_service_impl.h" -#include "brave/common/tor/pref_names.h" +#include "base/path_service.h" +#include "brave/browser/brave_browser_process_impl.h" #include "brave/components/tor/buildflags/buildflags.h" +#include "brave/components/tor/pref_names.h" #include "chrome/browser/profiles/incognito_helpers.h" #include "chrome/browser/profiles/profile.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" +#include "brave/components/tor/tor_profile_service_impl.h" #endif namespace { @@ -43,6 +46,21 @@ 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", @@ -55,13 +73,20 @@ TorProfileServiceFactory::~TorProfileServiceFactory() {} KeyedService* TorProfileServiceFactory::BuildServiceInstanceFor( content::BrowserContext* context) const { #if BUILDFLAG(ENABLE_TOR) - Profile* profile = Profile::FromBrowserContext(context); + 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_profile_set.emplace(Profile::FromBrowserContext(context)); } return tor_profile_service.release(); diff --git a/browser/tor/tor_profile_service_factory.h b/browser/tor/tor_profile_service_factory.h index 845948de9a1b..2a1197a24621 100644 --- a/browser/tor/tor_profile_service_factory.h +++ b/browser/tor/tor_profile_service_factory.h @@ -22,6 +22,9 @@ class TorProfileServiceFactory : public BrowserContextKeyedServiceFactory { static tor::TorProfileService* GetForProfile(Profile* profile); static TorProfileServiceFactory* GetInstance(); + static void SetTorDisabled(bool disabled); + static bool IsTorDisabled(); + private: friend struct base::DefaultSingletonTraits; diff --git a/browser/ui/BUILD.gn b/browser/ui/BUILD.gn index 444577337002..ad3302665d6b 100644 --- a/browser/ui/BUILD.gn +++ b/browser/ui/BUILD.gn @@ -198,7 +198,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 +214,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", diff --git a/browser/ui/brave_browser_command_controller_browsertest.cc b/browser/ui/brave_browser_command_controller_browsertest.cc index bb8769606500..65740d58a1ac 100644 --- a/browser/ui/brave_browser_command_controller_browsertest.cc +++ b/browser/ui/brave_browser_command_controller_browsertest.cc @@ -6,6 +6,7 @@ #include #include "brave/browser/profiles/profile_util.h" +#include "brave/browser/tor/tor_profile_service_factory.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" @@ -23,10 +24,6 @@ #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 @@ -201,7 +198,7 @@ IN_PROC_BROWSER_TEST_F(BraveBrowserCommandControllerTest, // 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..45b950c62801 100644 --- a/browser/ui/browser_commands.cc +++ b/browser/ui/browser_commands.cc @@ -6,9 +6,9 @@ #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/tor_profile_service.h" #include "chrome/browser/browser_process.h" #include "chrome/browser/profiles/profile.h" #include "chrome/browser/profiles/profile_manager.h" diff --git a/browser/ui/views/profiles/brave_incognito_menu_view.cc b/browser/ui/views/profiles/brave_incognito_menu_view.cc index f9c760d28fab..542fc6326a45 100644 --- a/browser/ui/views/profiles/brave_incognito_menu_view.cc +++ b/browser/ui/views/profiles/brave_incognito_menu_view.cc @@ -10,6 +10,7 @@ #include "brave/browser/brave_browser_process_impl.h" #include "brave/browser/profiles/profile_util.h" +#include "brave/browser/tor/tor_profile_service_factory.h" #include "brave/components/tor/buildflags/buildflags.h" #include "brave/grit/brave_generated_resources.h" #include "chrome/app/vector_icons/vector_icons.h" @@ -26,7 +27,6 @@ #if BUILDFLAG(ENABLE_TOR) #include "brave/browser/extensions/brave_tor_client_updater.h" -#include "brave/browser/tor/tor_profile_service.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..0bb10e9b10b0 100644 --- a/browser/ui/webui/brave_new_tab_message_handler.cc +++ b/browser/ui/webui/brave_new_tab_message_handler.cc @@ -13,7 +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" @@ -27,6 +26,7 @@ #include "brave/components/ntp_background_images/browser/url_constants.h" #include "brave/components/ntp_background_images/browser/view_counter_service.h" #include "brave/components/ntp_background_images/common/pref_names.h" +#include "brave/components/tor/tor_profile_service.h" #include "chrome/browser/profiles/profile.h" #include "chrome/common/chrome_features.h" #include "content/public/browser/web_ui_data_source.h" diff --git a/browser/ui/webui/brave_new_tab_message_handler.h b/browser/ui/webui/brave_new_tab_message_handler.h index fc8857c4a752..295e09115bab 100644 --- a/browser/ui/webui/brave_new_tab_message_handler.h +++ b/browser/ui/webui/brave_new_tab_message_handler.h @@ -8,7 +8,7 @@ #include -#include "brave/browser/tor/tor_launcher_service_observer.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" diff --git a/browser/ui/webui/settings/brave_default_extensions_handler.cc b/browser/ui/webui/settings/brave_default_extensions_handler.cc index a9de31f33457..83a61e019204 100644 --- a/browser/ui/webui/settings/brave_default_extensions_handler.cc +++ b/browser/ui/webui/settings/brave_default_extensions_handler.cc @@ -11,6 +11,7 @@ #include "base/values.h" #include "brave/browser/brave_browser_process_impl.h" #include "brave/browser/extensions/brave_component_loader.h" +#include "brave/browser/tor/tor_profile_service_factory.h" #include "brave/common/pref_names.h" #include "brave/components/brave_webtorrent/grit/brave_webtorrent_resources.h" #include "chrome/browser/about_flags.h" @@ -35,8 +36,7 @@ #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/components/tor/pref_names.h" #endif #if BUILDFLAG(BRAVE_WALLET_ENABLED) @@ -239,7 +239,7 @@ void BraveDefaultExtensionsHandler::SetTorEnabled(const base::ListValue* args) { bool enabled; args->GetBoolean(0, &enabled); AllowJavascript(); - tor::TorProfileService::SetTorDisabled(!enabled); + TorProfileServiceFactory::SetTorDisabled(!enabled); } void BraveDefaultExtensionsHandler::IsTorEnabled( @@ -248,14 +248,14 @@ void BraveDefaultExtensionsHandler::IsTorEnabled( AllowJavascript(); ResolveJavascriptCallback( args->GetList()[0], - base::Value(!tor::TorProfileService::IsTorDisabled())); + base::Value(!TorProfileServiceFactory::IsTorDisabled())); } void BraveDefaultExtensionsHandler::OnTorEnabledChanged() { if (IsJavascriptAllowed()) { FireWebUIListener( "tor-enabled-changed", - base::Value(!tor::TorProfileService::IsTorDisabled())); + base::Value(!TorProfileServiceFactory::IsTorDisabled())); } } diff --git a/chromium_src/chrome/browser/net/proxy_config_monitor.cc b/chromium_src/chrome/browser/net/proxy_config_monitor.cc index e2cdaffda2c7..fda6f253eac1 100644 --- a/chromium_src/chrome/browser/net/proxy_config_monitor.cc +++ b/chromium_src/chrome/browser/net/proxy_config_monitor.cc @@ -10,8 +10,8 @@ #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 { 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 e0887dfae5a5..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 @@ -5,8 +5,8 @@ #include "base/stl_util.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/profile_window.cc b/chromium_src/chrome/browser/profiles/profile_window.cc index 20185df6e923..fa94d40fab5c 100644 --- a/chromium_src/chrome/browser/profiles/profile_window.cc +++ b/chromium_src/chrome/browser/profiles/profile_window.cc @@ -12,8 +12,8 @@ #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/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" 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 9cee684c622f..6c90319c3d80 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,17 +6,13 @@ #include "brave/browser/autocomplete/brave_autocomplete_scheme_classifier.h" #include "brave/browser/profiles/profile_util.h" -#include "brave/browser/translate/buildflags/buildflags.h" #include "brave/browser/renderer_context_menu/brave_spelling_options_submenu_observer.h" -#include "brave/components/tor/buildflags/buildflags.h" +#include "brave/browser/tor/tor_profile_service_factory.h" +#include "brave/browser/translate/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" -#endif - // Our .h file creates a masquerade for RenderViewContextMenu. Switch // back to the Chromium one for the Chromium implementation. #undef RenderViewContextMenu @@ -67,16 +63,12 @@ BraveRenderViewContextMenu::BraveRenderViewContextMenu( bool BraveRenderViewContextMenu::IsCommandIdEnabled(int id) const { switch (id) { case IDC_CONTENT_CONTEXT_OPENLINKTOR: -#if BUILDFLAG(ENABLE_TOR) if (brave::IsTorDisabledForProfile(GetProfile())) return false; return params_.link_url.is_valid() && IsURLAllowedInIncognito(params_.link_url, browser_context_) && !brave::IsTorProfile(GetProfile()); -#else - return false; -#endif default: return RenderViewContextMenu_Chromium::IsCommandIdEnabled(id); } @@ -115,10 +107,9 @@ void BraveRenderViewContextMenu::AddSpellCheckServiceItem( void BraveRenderViewContextMenu::InitMenu() { RenderViewContextMenu_Chromium::InitMenu(); -#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(); @@ -133,7 +124,6 @@ void BraveRenderViewContextMenu::InitMenu() { is_app ? IDS_CONTENT_CONTEXT_OPENLINKTOR_INAPP : IDS_CONTENT_CONTEXT_OPENLINKTOR); } -#endif // Only show the translate item when go-translate is enabled. #if !BUILDFLAG(ENABLE_BRAVE_TRANSLATE_GO) 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/common/tor/BUILD.gn b/common/tor/BUILD.gn deleted file mode 100644 index 6aa6c9eb75b2..000000000000 --- a/common/tor/BUILD.gn +++ /dev/null @@ -1,23 +0,0 @@ -source_set("tor") { - sources = [ - "tor_constants.cc", - "tor_constants.h", - "tor_control_event.cc", - "tor_control_event.h", - "tor_control_event_list.h", - ] - - deps = [ - ":pref_names", - "//base", - "//chrome/common:constants", - "//url:url", - ] -} - -source_set("pref_names") { - sources = [ - "pref_names.cc", - "pref_names.h", - ] -} diff --git a/components/tor/BUILD.gn b/components/tor/BUILD.gn new file mode 100644 index 000000000000..e44e4e6f3a67 --- /dev/null +++ b/components/tor/BUILD.gn @@ -0,0 +1,52 @@ +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", + ] + + deps = [ + ":pref_names", + "//base", + ] + + if (enable_tor) { + sources += [ + "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_profile_service.cc", + "tor_profile_service.h", + "tor_profile_service_impl.cc", + "tor_profile_service_impl.h", + ] + } + + deps += [ + "//brave/app:brave_generated_resources_grit", + "//brave/components/services/tor/public/interfaces", + "//content/public/browser", + "//extensions/common:common_constants", + "//net", + "//services/service_manager", + "//url", + ] +} + +source_set("pref_names") { + sources = [ + "pref_names.cc", + "pref_names.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_launcher_factory.cc b/components/tor/tor_launcher_factory.cc similarity index 98% rename from browser/tor/tor_launcher_factory.cc rename to components/tor/tor_launcher_factory.cc index acd4a8f1f6ae..d129202022dc 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/components/tor/service_sandbox_type.h" +#include "brave/components/tor/tor_profile_service_impl.h" #include "brave/grit/brave_generated_resources.h" -#include "chrome/browser/service_sandbox_type.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" diff --git a/browser/tor/tor_launcher_factory.h b/components/tor/tor_launcher_factory.h similarity index 90% rename from browser/tor/tor_launcher_factory.h rename to components/tor/tor_launcher_factory.h index 5d47dfb18a39..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,8 +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/components/services/tor/public/interfaces/tor.mojom.h" +#include "brave/components/tor/tor_control.h" #include "mojo/public/cpp/bindings/remote.h" namespace tor { @@ -92,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/components/tor/tor_profile_service.cc b/components/tor/tor_profile_service.cc new file mode 100644 index 000000000000..6b24be296331 --- /dev/null +++ b/components/tor/tor_profile_service.cc @@ -0,0 +1,42 @@ +/* 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() { +} + +TorProfileService::~TorProfileService() { +} + +// 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 74% rename from browser/tor/tor_profile_service.h rename to components/tor/tor_profile_service.h index 467244a2a774..f2e43367be41 100644 --- a/browser/tor/tor_profile_service.h +++ b/components/tor/tor_profile_service.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_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 @@ -12,7 +12,6 @@ #include "base/macros.h" #include "base/observer_list.h" #include "components/keyed_service/core/keyed_service.h" -#include "url/gurl.h" namespace base { class FilePath; @@ -31,7 +30,6 @@ class PrefRegistrySyncable; } class PrefRegistrySimple; -class BraveAppMenuBrowserTestWithTorDisabled; namespace tor { @@ -44,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; @@ -58,7 +54,6 @@ class TorProfileService : public KeyedService { void RemoveObserver(TorLauncherServiceObserver* observer); protected: - base::FilePath GetTorExecutablePath(); base::ObserverList observers_; private: @@ -67,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 82% rename from browser/tor/tor_profile_service_impl.cc rename to components/tor/tor_profile_service_impl.cc index d37dc5e3c02a..62a52312fcc3 100644 --- a/browser/tor/tor_profile_service_impl.cc +++ b/components/tor/tor_profile_service_impl.cc @@ -3,22 +3,19 @@ * 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 #include #include "base/bind.h" -#include "base/path_service.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 "chrome/common/chrome_paths.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" @@ -116,20 +113,19 @@ void OnNewTorCircuit(std::unique_ptr tracker, } // 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); } } @@ -156,21 +152,36 @@ void TorProfileServiceImpl::LaunchTor() { GetTorWatchPath()); tor_launcher_factory_->LaunchTorProcess(config); } + +base::FilePath TorProfileServiceImpl::GetTorExecutablePath() { + return tor_client_updater_ ? tor_client_updater_->GetExecutablePath() + : base::FilePath(); +} + base::FilePath TorProfileServiceImpl::GetTorDataPath() { - base::FilePath user_data_dir; - base::PathService::Get(chrome::DIR_USER_DATA, &user_data_dir); - DCHECK(!user_data_dir.empty()); - return user_data_dir.Append(FILE_PATH_LITERAL("tor")) + DCHECK(!user_data_dir_.empty()); + return user_data_dir_.Append(FILE_PATH_LITERAL("tor")) .Append(FILE_PATH_LITERAL("data")); } + base::FilePath TorProfileServiceImpl::GetTorWatchPath() { - base::FilePath user_data_dir; - base::PathService::Get(chrome::DIR_USER_DATA, &user_data_dir); - DCHECK(!user_data_dir.empty()); - return user_data_dir.Append(FILE_PATH_LITERAL("tor")) + 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 @@ -185,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 67% rename from browser/tor/tor_profile_service_impl.h rename to components/tor/tor_profile_service_impl.h index fbdede80a528..f591e808fdbf 100644 --- a/browser/tor/tor_profile_service_impl.h +++ b/components/tor/tor_profile_service_impl.h @@ -1,22 +1,25 @@ -/* 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" +// TODO(darkdh): move to components/tor in a following commit #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/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; @@ -33,10 +36,14 @@ using NewTorCircuitCallback = base::OnceCallback CreateProxyConfigService() override; bool IsTorConnected() override; @@ -55,6 +62,7 @@ class TorProfileServiceImpl : public TorProfileService, private: void LaunchTor(); + base::FilePath GetTorExecutablePath(); base::FilePath GetTorDataPath(); base::FilePath GetTorWatchPath(); @@ -62,7 +70,9 @@ class TorProfileServiceImpl : public TorProfileService, 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_; @@ -72,4 +82,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/test/BUILD.gn b/test/BUILD.gn index d299e3e49212..a815fbf0e65e 100644 --- a/test/BUILD.gn +++ b/test/BUILD.gn @@ -744,13 +744,13 @@ if (!is_android) { ":brave_browser_tests_deps", "//brave/app:command_ids", "//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", "//brave/components/brave_wallet/buildflags:buildflags", "//brave/components/ipfs/buildflags:buildflags", "//brave/components/tor/buildflags", + "//brave/components/tor:pref_names", "//chrome/browser/ui", "//chrome/test:test_support_ui", "//components/embedder_support", From 93b2f545d748e1e503de5a1e1f4fb7dca80e7fd6 Mon Sep 17 00:00:00 2001 From: Anthony Tseng Date: Fri, 16 Oct 2020 17:53:55 -0700 Subject: [PATCH 04/10] Move BraveTorClientUpdater --- browser/BUILD.gn | 9 --- browser/brave_browser_process_impl.cc | 14 +++-- browser/brave_browser_process_impl.h | 6 +- browser/brave_content_browser_client.cc | 1 - .../extensions/brave_extension_management.cc | 2 +- .../brave_tor_client_updater_browsertest.cc | 4 +- .../brave_profile_manager_browsertest.cc | 2 +- ...rch_engine_provider_service_browsertest.cc | 2 +- browser/tor/BUILD.gn | 2 + .../tor/brave_tor_client_updater_delegate.cc | 40 ++++++++++++++ .../tor/brave_tor_client_updater_delegate.h | 35 ++++++++++++ .../profiles/brave_incognito_menu_view.cc | 2 +- common/brave_switches.cc | 4 -- common/brave_switches.h | 2 - components/tor/BUILD.gn | 11 ++++ .../tor}/brave_tor_client_updater.cc | 55 ++++++------------- .../tor}/brave_tor_client_updater.h | 29 ++++++---- components/tor/tor_profile_service_impl.h | 5 +- components/tor/tor_switches.cc | 15 +++++ components/tor/tor_switches.h | 15 +++++ 20 files changed, 173 insertions(+), 82 deletions(-) create mode 100644 browser/tor/brave_tor_client_updater_delegate.cc create mode 100644 browser/tor/brave_tor_client_updater_delegate.h rename {browser/extensions => components/tor}/brave_tor_client_updater.cc (80%) rename {browser/extensions => components/tor}/brave_tor_client_updater.h (81%) create mode 100644 components/tor/tor_switches.cc create mode 100644 components/tor/tor_switches.h diff --git a/browser/BUILD.gn b/browser/BUILD.gn index dcc5e2b9b34e..aa3bb5904849 100644 --- a/browser/BUILD.gn +++ b/browser/BUILD.gn @@ -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", diff --git a/browser/brave_browser_process_impl.cc b/browser/brave_browser_process_impl.cc index b01aa980b11b..57f3596cff33 100644 --- a/browser/brave_browser_process_impl.cc +++ b/browser/brave_browser_process_impl.cc @@ -69,7 +69,8 @@ #endif #if BUILDFLAG(ENABLE_TOR) -#include "brave/browser/extensions/brave_tor_client_updater.h" +#include "brave/browser/tor/brave_tor_client_updater_delegate.h" +#include "brave/components/tor/brave_tor_client_updater.h" #include "brave/components/tor/pref_names.h" #endif @@ -308,13 +309,18 @@ 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); + std::unique_ptr delegate = + std::make_unique(user_data_dir); + + tor_client_updater_.reset(new tor::BraveTorClientUpdater( + brave_component_updater_delegate(), std::move(delegate))); return tor_client_updater_.get(); } diff --git a/browser/brave_browser_process_impl.h b/browser/brave_browser_process_impl.h index 6e04b1e3b008..2b139303bbf5 100644 --- a/browser/brave_browser_process_impl.h +++ b/browser/brave_browser_process_impl.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 7c6f14e1da58..95f13bb5a492 100644 --- a/browser/brave_content_browser_client.cc +++ b/browser/brave_content_browser_client.cc @@ -16,7 +16,6 @@ #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" diff --git a/browser/extensions/brave_extension_management.cc b/browser/extensions/brave_extension_management.cc index d099d4406a19..3d5c2d4d2054 100644 --- a/browser/extensions/brave_extension_management.cc +++ b/browser/extensions/brave_extension_management.cc @@ -22,7 +22,7 @@ #include "extensions/common/extension_urls.h" #if BUILDFLAG(ENABLE_TOR) -#include "brave/browser/extensions/brave_tor_client_updater.h" +#include "brave/components/tor/brave_tor_client_updater.h" #endif namespace extensions { diff --git a/browser/extensions/brave_tor_client_updater_browsertest.cc b/browser/extensions/brave_tor_client_updater_browsertest.cc index a3b2d4f1c111..37db9a7c022d 100644 --- a/browser/extensions/brave_tor_client_updater_browsertest.cc +++ b/browser/extensions/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/profiles/brave_profile_manager_browsertest.cc b/browser/profiles/brave_profile_manager_browsertest.cc index ce348d591d7e..972035ce9f66 100644 --- a/browser/profiles/brave_profile_manager_browsertest.cc +++ b/browser/profiles/brave_profile_manager_browsertest.cc @@ -9,7 +9,6 @@ #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/common/brave_paths.h" #include "brave/components/brave_ads/browser/ads_service_factory.h" @@ -38,6 +37,7 @@ #include "content/public/test/test_utils.h" #if BUILDFLAG(ENABLE_TOR) +#include "brave/components/tor/brave_tor_client_updater.h" #include "brave/components/tor/tor_launcher_factory.h" #endif diff --git a/browser/search_engines/search_engine_provider_service_browsertest.cc b/browser/search_engines/search_engine_provider_service_browsertest.cc index 85eeda4c6cbf..d0d923414df4 100644 --- a/browser/search_engines/search_engine_provider_service_browsertest.cc +++ b/browser/search_engines/search_engine_provider_service_browsertest.cc @@ -27,7 +27,7 @@ #include "content/public/test/test_utils.h" #if BUILDFLAG(ENABLE_TOR) -#include "brave/browser/extensions/brave_tor_client_updater.h" +#include "brave/components/tor/brave_tor_client_updater.h" #include "brave/components/tor/tor_launcher_factory.h" #endif diff --git a/browser/tor/BUILD.gn b/browser/tor/BUILD.gn index 77ffd510d5d1..b4a75e999c87 100644 --- a/browser/tor/BUILD.gn +++ b/browser/tor/BUILD.gn @@ -24,6 +24,8 @@ source_set("tor") { if (enable_tor) { sources += [ + "brave_tor_client_updater_delegate.cc", + "brave_tor_client_updater_delegate.h", "onion_location_navigation_throttle.cc", "onion_location_navigation_throttle.h", "onion_location_tab_helper.cc", diff --git a/browser/tor/brave_tor_client_updater_delegate.cc b/browser/tor/brave_tor_client_updater_delegate.cc new file mode 100644 index 000000000000..47f0eb4d26dd --- /dev/null +++ b/browser/tor/brave_tor_client_updater_delegate.cc @@ -0,0 +1,40 @@ +/* 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/brave_tor_client_updater_delegate.h" + +#include "base/files/file_util.h" +#include "base/task/post_task.h" +#include "base/task_runner.h" +#include "brave/browser/tor/tor_profile_service_factory.h" + +namespace tor { + +namespace { +void DeleteDir(const base::FilePath& path) { + base::DeletePathRecursively(path); +} +} // namespace + +BraveTorClientUpdaterDelegate::BraveTorClientUpdaterDelegate( + const base::FilePath& user_data_dir) + : user_data_dir_(user_data_dir), + task_runner_(base::CreateSequencedTaskRunner( + {base::ThreadPool(), base::MayBlock()})) {} + +BraveTorClientUpdaterDelegate::~BraveTorClientUpdaterDelegate() = default; + +void BraveTorClientUpdaterDelegate::Cleanup(const char* component_id) { + DCHECK(!user_data_dir_.empty()); + base::FilePath tor_component_dir = user_data_dir_.AppendASCII(component_id); + task_runner_->PostTask(FROM_HERE, + base::BindOnce(&DeleteDir, tor_component_dir)); +} + +bool BraveTorClientUpdaterDelegate::IsTorDisabled() { + return TorProfileServiceFactory::IsTorDisabled(); +} + +} // namespace tor diff --git a/browser/tor/brave_tor_client_updater_delegate.h b/browser/tor/brave_tor_client_updater_delegate.h new file mode 100644 index 000000000000..c281b3acdd55 --- /dev/null +++ b/browser/tor/brave_tor_client_updater_delegate.h @@ -0,0 +1,35 @@ +/* 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_BRAVE_TOR_CLIENT_UPDATER_DELEGATE_H_ +#define BRAVE_BROWSER_TOR_BRAVE_TOR_CLIENT_UPDATER_DELEGATE_H_ + +#include "base/files/file_path.h" +#include "brave/components/tor/brave_tor_client_updater.h" + +namespace tor { + +class BraveTorClientUpdaterDelegate : public BraveTorClientUpdater::Delegate { + public: + explicit BraveTorClientUpdaterDelegate(const base::FilePath& user_data_dir); + ~BraveTorClientUpdaterDelegate() override; + + // BraveTorClientUpdater::Delegate + void Cleanup(const char* component_id) override; + bool IsTorDisabled() override; + + private: + base::FilePath user_data_dir_; + + scoped_refptr task_runner_; + + BraveTorClientUpdaterDelegate(const BraveTorClientUpdaterDelegate&) = delete; + BraveTorClientUpdaterDelegate& operator=( + const BraveTorClientUpdaterDelegate&) = delete; +}; + +} // namespace tor + +#endif // BRAVE_BROWSER_TOR_BRAVE_TOR_CLIENT_UPDATER_DELEGATE_H_ diff --git a/browser/ui/views/profiles/brave_incognito_menu_view.cc b/browser/ui/views/profiles/brave_incognito_menu_view.cc index 542fc6326a45..d4e05a3f0914 100644 --- a/browser/ui/views/profiles/brave_incognito_menu_view.cc +++ b/browser/ui/views/profiles/brave_incognito_menu_view.cc @@ -26,7 +26,7 @@ #include "ui/base/models/image_model.h" #if BUILDFLAG(ENABLE_TOR) -#include "brave/browser/extensions/brave_tor_client_updater.h" +#include "brave/components/tor/brave_tor_client_updater.h" #endif namespace { 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/components/tor/BUILD.gn b/components/tor/BUILD.gn index e44e4e6f3a67..fd7bfb7ad346 100644 --- a/components/tor/BUILD.gn +++ b/components/tor/BUILD.gn @@ -13,11 +13,14 @@ source_set("tor") { deps = [ ":pref_names", + ":switches", "//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", @@ -40,6 +43,7 @@ source_set("tor") { "//extensions/common:common_constants", "//net", "//services/service_manager", + "//third_party/re2", "//url", ] } @@ -50,3 +54,10 @@ source_set("pref_names") { "pref_names.h", ] } + +source_set("switches") { + sources = [ + "tor_switches.cc", + "tor_switches.h", + ] +} diff --git a/browser/extensions/brave_tor_client_updater.cc b/components/tor/brave_tor_client_updater.cc similarity index 80% rename from browser/extensions/brave_tor_client_updater.cc rename to components/tor/brave_tor_client_updater.cc index a265048919db..6fb3293614f3 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,12 @@ #include "base/task/post_task.h" #include "base/task_runner.h" #include "base/task_runner_util.h" -#include "brave/browser/tor/tor_profile_service_factory.h" -#include "brave/common/brave_switches.h" -#include "chrome/browser/browser_process.h" -#include "chrome/browser/profiles/profile_manager.h" +#include "brave/components/tor/tor_switches.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 { @@ -111,21 +103,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, + std::unique_ptr delegate) + : BraveComponent(component_delegate), task_runner_(base::CreateSequencedTaskRunner( {base::ThreadPool(), base::MayBlock()})), registered_(false), - weak_ptr_factory_(this) {} - -BraveTorClientUpdater::~BraveTorClientUpdater() { + delegate_(std::move(delegate)), + weak_ptr_factory_(this) { + DCHECK(delegate_); } +BraveTorClientUpdater::~BraveTorClientUpdater() {} + void BraveTorClientUpdater::Register() { const base::CommandLine& command_line = *base::CommandLine::ForCurrentProcess(); - if (TorProfileServiceFactory::IsTorDisabled() || - command_line.HasSwitch(switches::kDisableTorClientUpdaterExtension) || + if (delegate_->IsTorDisabled() || + command_line.HasSwitch(tor::kDisableTorClientUpdaterExtension) || registered_) { return; } @@ -143,14 +139,7 @@ void BraveTorClientUpdater::Unregister() { } void BraveTorClientUpdater::Cleanup() { - // Delete tor binaries if tor is disabled. - if (TorProfileServiceFactory::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)); - } + delegate_->Cleanup(kTorClientComponentId); } void BraveTorClientUpdater::SetExecutablePath(const base::FilePath& path) { @@ -190,12 +179,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 81% rename from browser/extensions/brave_tor_client_updater.h rename to components/tor/brave_tor_client_updater.h index 2bbee79ad3c4..d022b5414550 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 @@ -22,8 +22,7 @@ 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 +47,15 @@ class BraveTorClientUpdater : public BraveComponent { ~Observer() override = default; }; - explicit BraveTorClientUpdater(BraveComponent::Delegate* delegate); + class Delegate { + public: + virtual ~Delegate() = default; + virtual void Cleanup(const char* component_id) = 0; + virtual bool IsTorDisabled() = 0; + }; + + BraveTorClientUpdater(BraveComponent::Delegate* component_delegate, + std::unique_ptr delegate); ~BraveTorClientUpdater() override; void Register(); @@ -86,15 +93,13 @@ class BraveTorClientUpdater : public BraveComponent { base::FilePath executable_path_; base::ObserverList observers_; + std::unique_ptr delegate_; + base::WeakPtrFactory weak_ptr_factory_; DISALLOW_COPY_AND_ASSIGN(BraveTorClientUpdater); }; -// Creates the BraveTorClientUpdater -std::unique_ptr -BraveTorClientUpdaterFactory(BraveComponent::Delegate* delegate); - -} // namespace extensions +} // namespace tor -#endif // BRAVE_BROWSER_EXTENSIONS_BRAVE_TOR_CLIENT_UPDATER_H_ +#endif // BRAVE_COMPONENTS_TOR_BRAVE_TOR_CLIENT_UPDATER_H_ diff --git a/components/tor/tor_profile_service_impl.h b/components/tor/tor_profile_service_impl.h index f591e808fdbf..f6e9446985db 100644 --- a/components/tor/tor_profile_service_impl.h +++ b/components/tor/tor_profile_service_impl.h @@ -11,8 +11,7 @@ #include "base/memory/weak_ptr.h" #include "base/optional.h" -// TODO(darkdh): move to components/tor in a following commit -#include "brave/browser/extensions/brave_tor_client_updater.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" @@ -26,8 +25,6 @@ class ProxyConfigService; class ProxyConfigServiceTor; } -using extensions::BraveTorClientUpdater; - namespace tor { using NewTorCircuitCallback = base::OnceCallback Date: Tue, 20 Oct 2020 10:35:57 -0700 Subject: [PATCH 05/10] Move navigation_throttles and tab_helpers to components/tor --- browser/brave_content_browser_client.cc | 25 +++++-- browser/brave_tab_helpers.cc | 8 ++- browser/profiles/brave_profile_manager.cc | 2 +- .../brave_profile_manager_unittest.cc | 2 +- browser/tor/BUILD.gn | 16 +---- ...ocation_navigation_throttle_browsertest.cc | 2 +- ...n_location_navigation_throttle_delegate.cc | 56 ++++++++++++++++ ...on_location_navigation_throttle_delegate.h | 37 ++++++++++ .../tor/tor_navigation_throttle_unittest.cc | 55 ++++++++------- browser/tor/tor_profile_service_factory.cc | 20 +++--- browser/tor/tor_profile_service_factory.h | 8 +-- browser/ui/browser_commands.cc | 2 +- .../views/location_bar/onion_location_view.cc | 2 +- .../ui/webui/brave_new_tab_message_handler.cc | 2 +- .../browser/net/proxy_config_monitor.cc | 2 +- .../chrome/browser/profiles/profile_window.cc | 2 +- components/tor/BUILD.gn | 12 +++- .../tor/onion_location_navigation_throttle.cc | 67 ++++++++----------- .../tor/onion_location_navigation_throttle.h | 32 ++++++--- .../tor/onion_location_tab_helper.cc | 2 +- .../tor/onion_location_tab_helper.h | 6 +- .../tor/tor_navigation_throttle.cc | 30 ++++----- .../tor/tor_navigation_throttle.h | 17 +++-- {browser => components}/tor/tor_tab_helper.cc | 12 ++-- {browser => components}/tor/tor_tab_helper.h | 9 +-- 25 files changed, 269 insertions(+), 159 deletions(-) create mode 100644 browser/tor/onion_location_navigation_throttle_delegate.cc create mode 100644 browser/tor/onion_location_navigation_throttle_delegate.h rename {browser => components}/tor/onion_location_navigation_throttle.cc (55%) rename {browser => components}/tor/onion_location_navigation_throttle.h (58%) rename {browser => components}/tor/onion_location_tab_helper.cc (93%) rename {browser => components}/tor/onion_location_tab_helper.h (86%) rename {browser => components}/tor/tor_navigation_throttle.cc (65%) rename {browser => components}/tor/tor_navigation_throttle.h (68%) rename {browser => components}/tor/tor_tab_helper.cc (80%) rename {browser => components}/tor/tor_tab_helper.h (85%) diff --git a/browser/brave_content_browser_client.cc b/browser/brave_content_browser_client.cc index 95f13bb5a492..039799eb1a32 100644 --- a/browser/brave_content_browser_client.cc +++ b/browser/brave_content_browser_client.cc @@ -19,6 +19,7 @@ #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/tor_profile_service_factory.h" #include "brave/common/pref_names.h" #include "brave/common/webui_url_constants.h" #include "brave/components/binance/browser/buildflags/buildflags.h" @@ -84,8 +85,9 @@ 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/components/tor/onion_location_navigation_throttle.h" +#include "brave/components/tor/tor_navigation_throttle.h" #endif #if BUILDFLAG(ENABLE_SPEEDREADER) @@ -458,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_tab_helpers.cc b/browser/brave_tab_helpers.cc index cb157f0f5764..7711d296bc33 100644 --- a/browser/brave_tab_helpers.cc +++ b/browser/brave_tab_helpers.cc @@ -9,6 +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/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" @@ -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/profiles/brave_profile_manager.cc b/browser/profiles/brave_profile_manager.cc index 8d593946a948..4c604385101e 100644 --- a/browser/profiles/brave_profile_manager.cc +++ b/browser/profiles/brave_profile_manager.cc @@ -203,7 +203,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_unittest.cc b/browser/profiles/brave_profile_manager_unittest.cc index ddfd096924ab..c4df935e3f8c 100644 --- a/browser/profiles/brave_profile_manager_unittest.cc +++ b/browser/profiles/brave_profile_manager_unittest.cc @@ -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/tor/BUILD.gn b/browser/tor/BUILD.gn index b4a75e999c87..fb8f723c13ff 100644 --- a/browser/tor/BUILD.gn +++ b/browser/tor/BUILD.gn @@ -26,20 +26,8 @@ source_set("tor") { sources += [ "brave_tor_client_updater_delegate.cc", "brave_tor_client_updater_delegate.h", - "onion_location_navigation_throttle.cc", - "onion_location_navigation_throttle.h", - "onion_location_tab_helper.cc", - "onion_location_tab_helper.h", - "tor_navigation_throttle.cc", - "tor_navigation_throttle.h", - "tor_tab_helper.cc", - "tor_tab_helper.h", - ] - - deps += [ - "//content/public/browser", - "//content/public/common", - "//extensions/common:common_constants", + "onion_location_navigation_throttle_delegate.cc", + "onion_location_navigation_throttle_delegate.h", ] # Below dep list are not directly used tor target. diff --git a/browser/tor/onion_location_navigation_throttle_browsertest.cc b/browser/tor/onion_location_navigation_throttle_browsertest.cc index cbc105d98975..56883a549210 100644 --- a/browser/tor/onion_location_navigation_throttle_browsertest.cc +++ b/browser/tor/onion_location_navigation_throttle_browsertest.cc @@ -5,11 +5,11 @@ #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_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/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" 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 c6927170cad8..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" @@ -13,13 +11,14 @@ #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/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_factory.cc b/browser/tor/tor_profile_service_factory.cc index c60550504a9b..3caa6d495a47 100644 --- a/browser/tor/tor_profile_service_factory.cc +++ b/browser/tor/tor_profile_service_factory.cc @@ -13,7 +13,6 @@ #include "brave/components/tor/buildflags/buildflags.h" #include "brave/components/tor/pref_names.h" #include "chrome/browser/profiles/incognito_helpers.h" -#include "chrome/browser/profiles/profile.h" #include "chrome/common/chrome_paths.h" #include "components/keyed_service/content/browser_context_dependency_manager.h" #include "components/prefs/pref_service.h" @@ -28,17 +27,17 @@ std::set g_profile_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 @@ -73,6 +72,8 @@ TorProfileServiceFactory::~TorProfileServiceFactory() {} KeyedService* TorProfileServiceFactory::BuildServiceInstanceFor( content::BrowserContext* context) const { #if BUILDFLAG(ENABLE_TOR) + 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()); @@ -108,8 +109,7 @@ void TorProfileServiceFactory::BrowserContextShutdown( // KillTor when the last Tor incognito profile is shutting down. if (g_profile_set.size() == 1) { auto* service = static_cast( - TorProfileServiceFactory::GetForProfile( - Profile::FromBrowserContext(context), false)); + TorProfileServiceFactory::GetForContext(context, false)); if (service) { service->KillTor(); } else { diff --git a/browser/tor/tor_profile_service_factory.h b/browser/tor/tor_profile_service_factory.h index 2a1197a24621..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,7 +17,8 @@ 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); @@ -28,7 +27,8 @@ class TorProfileServiceFactory : public BrowserContextKeyedServiceFactory { 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/browser_commands.cc b/browser/ui/browser_commands.cc index 45b950c62801..4771d72e9fc4 100644 --- a/browser/ui/browser_commands.cc +++ b/browser/ui/browser_commands.cc @@ -46,7 +46,7 @@ void NewTorConnectionForSite(Browser* browser) { 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(); 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/webui/brave_new_tab_message_handler.cc b/browser/ui/webui/brave_new_tab_message_handler.cc index 0bb10e9b10b0..e1fb9695a230 100644 --- a/browser/ui/webui/brave_new_tab_message_handler.cc +++ b/browser/ui/webui/brave_new_tab_message_handler.cc @@ -180,7 +180,7 @@ BraveNewTabMessageHandler* BraveNewTabMessageHandler::Create( BraveNewTabMessageHandler::BraveNewTabMessageHandler(Profile* profile) : profile_(profile) { if (brave::IsTorProfile(profile)) { - tor_profile_service_ = TorProfileServiceFactory::GetForProfile(profile); + tor_profile_service_ = TorProfileServiceFactory::GetForContext(profile); } } diff --git a/chromium_src/chrome/browser/net/proxy_config_monitor.cc b/chromium_src/chrome/browser/net/proxy_config_monitor.cc index fda6f253eac1..cc5a20d0ac82 100644 --- a/chromium_src/chrome/browser/net/proxy_config_monitor.cc +++ b/chromium_src/chrome/browser/net/proxy_config_monitor.cc @@ -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/profiles/profile_window.cc b/chromium_src/chrome/browser/profiles/profile_window.cc index fa94d40fab5c..cfea608b49a1 100644 --- a/chromium_src/chrome/browser/profiles/profile_window.cc +++ b/chromium_src/chrome/browser/profiles/profile_window.cc @@ -45,7 +45,7 @@ void OpenBrowserWindowForTorProfile(ProfileManager::CreateCallback callback, callback, always_create, is_new_profile, unblock_extensions, profile->GetOffTheRecordProfile(), status); tor::TorProfileService* service = - TorProfileServiceFactory::GetForProfile(profile); + TorProfileServiceFactory::GetForContext(profile); DCHECK(service); service->RegisterTorClientUpdater(); } diff --git a/components/tor/BUILD.gn b/components/tor/BUILD.gn index fd7bfb7ad346..d2e51d3e9880 100644 --- a/components/tor/BUILD.gn +++ b/components/tor/BUILD.gn @@ -29,21 +29,31 @@ source_set("tor") { "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/app:brave_generated_resources_grit", "//brave/components/services/tor/public/interfaces", + "//components/prefs", + "//components/user_prefs", "//content/public/browser", "//extensions/common:common_constants", "//net", "//services/service_manager", - "//third_party/re2", + "//third_party/re2", "//url", ] } diff --git a/browser/tor/onion_location_navigation_throttle.cc b/components/tor/onion_location_navigation_throttle.cc similarity index 55% rename from browser/tor/onion_location_navigation_throttle.cc rename to components/tor/onion_location_navigation_throttle.cc index d0ee348246fb..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_factory.h" +#include "brave/components/tor/onion_location_tab_helper.h" #include "brave/components/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 "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 (TorProfileServiceFactory::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/browser/tor/tor_navigation_throttle.cc b/components/tor/tor_navigation_throttle.cc similarity index 65% rename from browser/tor/tor_navigation_throttle.cc rename to components/tor/tor_navigation_throttle.cc index 7beed84ba68d..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_factory.h" #include "brave/components/tor/tor_profile_service.h" -#include "chrome/browser/profiles/profile.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 68% rename from browser/tor/tor_navigation_throttle.h rename to components/tor/tor_navigation_throttle.h index 01f4ca1c0abb..2b649bf07d40 100644 --- a/browser/tor/tor_navigation_throttle.h +++ b/components/tor/tor_navigation_throttle.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_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 @@ -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/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_ From 4e658bdccbf76cf6de8491e1fefb45e3d908e6a8 Mon Sep 17 00:00:00 2001 From: Anthony Tseng Date: Wed, 21 Oct 2020 13:52:16 -0700 Subject: [PATCH 06/10] Move tor related tests out of test/BUILD.gn and fix minor deps issues --- browser/tor/BUILD.gn | 67 +++++++++++++++++-- .../brave_tor_client_updater_browsertest.cc | 0 browser/tor/tor_profile_service_factory.cc | 3 +- common/tor/tor_test_constants.cc | 17 ----- common/tor/tor_test_constants.h | 20 ------ components/tor/BUILD.gn | 20 ++++++ .../tor/tor_control_unittest.cc | 0 net/proxy_resolution/BUILD.gn | 23 +++++++ test/BUILD.gn | 19 ++---- 9 files changed, 109 insertions(+), 60 deletions(-) rename browser/{extensions => tor}/brave_tor_client_updater_browsertest.cc (100%) delete mode 100644 common/tor/tor_test_constants.cc delete mode 100644 common/tor/tor_test_constants.h rename {browser => components}/tor/tor_control_unittest.cc (100%) create mode 100644 net/proxy_resolution/BUILD.gn diff --git a/browser/tor/BUILD.gn b/browser/tor/BUILD.gn index fb8f723c13ff..b87f43a4b519 100644 --- a/browser/tor/BUILD.gn +++ b/browser/tor/BUILD.gn @@ -3,6 +3,14 @@ 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 + visibility = [ + "//brave/browser:browser_process", + "//brave/browser/profiles", + "//brave/browser/ui", + ":browser_tests", + ":unit_tests", + ] + sources = [ "tor_profile_service_factory.cc", "tor_profile_service_factory.h", @@ -11,15 +19,10 @@ source_set("tor") { deps = [ "//base", "//brave/components/tor", + "//brave/components/tor:pref_names", "//chrome/common", "//components/keyed_service/content", - "//components/keyed_service/core", "//content/public/browser", - "//mojo/public/cpp/bindings", - ] - - public_deps = [ - "//brave/components/tor/buildflags", ] if (enable_tor) { @@ -42,3 +45,55 @@ source_set("tor") { ] } } + +source_set("unit_tests") { + testonly = true + if (enable_tor) { + sources = [ + "tor_navigation_throttle_unittest.cc", + ] + + deps = [ + "//base", + "//brave/browser/profiles", + "//brave/browser/tor", + "//base/test:test_support", + "//brave/test:test_support", + "//brave/components/tor", + "//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_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/extensions/brave_tor_client_updater_browsertest.cc b/browser/tor/brave_tor_client_updater_browsertest.cc similarity index 100% rename from browser/extensions/brave_tor_client_updater_browsertest.cc rename to browser/tor/brave_tor_client_updater_browsertest.cc diff --git a/browser/tor/tor_profile_service_factory.cc b/browser/tor/tor_profile_service_factory.cc index 3caa6d495a47..92e3eeb0e173 100644 --- a/browser/tor/tor_profile_service_factory.cc +++ b/browser/tor/tor_profile_service_factory.cc @@ -12,7 +12,6 @@ #include "brave/browser/brave_browser_process_impl.h" #include "brave/components/tor/buildflags/buildflags.h" #include "brave/components/tor/pref_names.h" -#include "chrome/browser/profiles/incognito_helpers.h" #include "chrome/common/chrome_paths.h" #include "components/keyed_service/content/browser_context_dependency_manager.h" #include "components/prefs/pref_service.h" @@ -100,7 +99,7 @@ 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( 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/components/tor/BUILD.gn b/components/tor/BUILD.gn index d2e51d3e9880..699704439185 100644 --- a/components/tor/BUILD.gn +++ b/components/tor/BUILD.gn @@ -46,7 +46,9 @@ source_set("tor") { deps += [ "//brave/app:brave_generated_resources_grit", + "//brave/components/brave_component_updater/browser", "//brave/components/services/tor/public/interfaces", + "//components/pref_registry", "//components/prefs", "//components/user_prefs", "//content/public/browser", @@ -71,3 +73,21 @@ source_set("switches") { "tor_switches.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/tor/tor_control_unittest.cc b/components/tor/tor_control_unittest.cc similarity index 100% rename from browser/tor/tor_control_unittest.cc rename to components/tor/tor_control_unittest.cc 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/test/BUILD.gn b/test/BUILD.gn index a815fbf0e65e..22132000a73e 100644 --- a/test/BUILD.gn +++ b/test/BUILD.gn @@ -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" ] @@ -535,7 +532,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 +595,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 +694,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" ] } @@ -749,8 +740,6 @@ if (!is_android) { "//brave/components/brave_sync/buildflags:buildflags", "//brave/components/brave_wallet/buildflags:buildflags", "//brave/components/ipfs/buildflags:buildflags", - "//brave/components/tor/buildflags", - "//brave/components/tor:pref_names", "//chrome/browser/ui", "//chrome/test:test_support_ui", "//components/embedder_support", From 560f751c0e58b107018d03ac27a931c917fc4dfa Mon Sep 17 00:00:00 2001 From: Anthony Tseng Date: Thu, 22 Oct 2020 18:32:20 -0700 Subject: [PATCH 07/10] Put TorProfileServiceFactory behind ENABLE_TOR build flag --- browser/brave_content_browser_client.cc | 2 +- ...browser_context_keyed_service_factories.cc | 16 +++++--- browser/policy/brave_policy_browsertest.cc | 3 ++ browser/profiles/brave_profile_manager.cc | 5 ++- browser/profiles/profile_util.cc | 5 ++- browser/tor/BUILD.gn | 39 ++++++++----------- browser/tor/tor_profile_service_factory.cc | 26 ++++--------- ..._browser_command_controller_browsertest.cc | 5 ++- browser/ui/browser_commands.cc | 10 ++++- .../profiles/brave_incognito_menu_view.cc | 2 +- .../ui/webui/brave_new_tab_message_handler.cc | 19 ++++++++- .../ui/webui/brave_new_tab_message_handler.h | 6 +++ .../brave_default_extensions_handler.cc | 2 +- .../chrome/browser/profiles/profile_window.cc | 8 +++- .../render_view_context_menu.cc | 12 +++++- test/BUILD.gn | 10 ++++- 16 files changed, 112 insertions(+), 58 deletions(-) diff --git a/browser/brave_content_browser_client.cc b/browser/brave_content_browser_client.cc index 039799eb1a32..8e49b8c1c771 100644 --- a/browser/brave_content_browser_client.cc +++ b/browser/brave_content_browser_client.cc @@ -19,7 +19,6 @@ #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/tor_profile_service_factory.h" #include "brave/common/pref_names.h" #include "brave/common/webui_url_constants.h" #include "brave/components/binance/browser/buildflags/buildflags.h" @@ -86,6 +85,7 @@ using extensions::ChromeContentBrowserClientExtensionsPart; #if BUILDFLAG(ENABLE_TOR) #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 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/policy/brave_policy_browsertest.cc b/browser/policy/brave_policy_browsertest.cc index 5e790f3010dc..035f07a21648 100644 --- a/browser/policy/brave_policy_browsertest.cc +++ b/browser/policy/brave_policy_browsertest.cc @@ -5,6 +5,7 @@ #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" @@ -35,6 +36,7 @@ class BravePolicyTest : public InProcessBrowserTest { MockConfigurationPolicyProvider provider_; }; +#if BUILDFLAG(ENABLE_TOR) // This policy only exists on Windows. // Sets the tor policy before the browser is started. class TorDisabledPolicyBrowserTest : public BravePolicyTest { @@ -80,5 +82,6 @@ IN_PROC_BROWSER_TEST_F(TorEnabledPolicyBrowserTest, TorDisabledPrefValueTest) { TorProfileServiceFactory::SetTorDisabled(true); EXPECT_FALSE(TorProfileServiceFactory::IsTorDisabled()); } +#endif } // namespace policy diff --git a/browser/profiles/brave_profile_manager.cc b/browser/profiles/brave_profile_manager.cc index 4c604385101e..182e69dae9b1 100644 --- a/browser/profiles/brave_profile_manager.cc +++ b/browser/profiles/brave_profile_manager.cc @@ -12,7 +12,6 @@ #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/tor_profile_service_factory.h" #include "brave/browser/translate/buildflags/buildflags.h" #include "brave/common/pref_names.h" #include "brave/components/brave_ads/browser/ads_service_factory.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) diff --git a/browser/profiles/profile_util.cc b/browser/profiles/profile_util.cc index a832052a0ebe..df39adf0ecc5 100644 --- a/browser/profiles/profile_util.cc +++ b/browser/profiles/profile_util.cc @@ -12,7 +12,6 @@ #include "base/files/file_path.h" #include "base/memory/ptr_util.h" #include "base/no_destructor.h" -#include "brave/browser/tor/tor_profile_service_factory.h" #include "brave/components/tor/buildflags/buildflags.h" #include "brave/components/tor/tor_constants.h" #include "chrome/browser/browser_process.h" @@ -20,6 +19,10 @@ #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_factory.h" +#endif + namespace brave { namespace { diff --git a/browser/tor/BUILD.gn b/browser/tor/BUILD.gn index b87f43a4b519..0d7f4d23ea8a 100644 --- a/browser/tor/BUILD.gn +++ b/browser/tor/BUILD.gn @@ -7,30 +7,27 @@ source_set("tor") { "//brave/browser:browser_process", "//brave/browser/profiles", "//brave/browser/ui", - ":browser_tests", - ":unit_tests", - ] - - sources = [ - "tor_profile_service_factory.cc", - "tor_profile_service_factory.h", - ] - - deps = [ - "//base", - "//brave/components/tor", - "//brave/components/tor:pref_names", - "//chrome/common", - "//components/keyed_service/content", - "//content/public/browser", + "//brave/test:*", + ":*", ] if (enable_tor) { - sources += [ + sources = [ "brave_tor_client_updater_delegate.cc", "brave_tor_client_updater_delegate.h", "onion_location_navigation_throttle_delegate.cc", "onion_location_navigation_throttle_delegate.h", + "tor_profile_service_factory.cc", + "tor_profile_service_factory.h", + ] + + deps = [ + "//base", + "//brave/components/tor", + "//brave/components/tor:pref_names", + "//chrome/common", + "//components/keyed_service/content", + "//content/public/browser", ] # Below dep list are not directly used tor target. @@ -49,17 +46,15 @@ source_set("tor") { source_set("unit_tests") { testonly = true if (enable_tor) { - sources = [ - "tor_navigation_throttle_unittest.cc", - ] + sources = [ "tor_navigation_throttle_unittest.cc" ] deps = [ "//base", + "//base/test:test_support", "//brave/browser/profiles", "//brave/browser/tor", - "//base/test:test_support", - "//brave/test:test_support", "//brave/components/tor", + "//brave/test:test_support", "//chrome/test:test_support", "//content/public/browser", "//content/test:test_support", diff --git a/browser/tor/tor_profile_service_factory.cc b/browser/tor/tor_profile_service_factory.cc index 92e3eeb0e173..1c7b79e9ee7e 100644 --- a/browser/tor/tor_profile_service_factory.cc +++ b/browser/tor/tor_profile_service_factory.cc @@ -10,19 +10,16 @@ #include "base/path_service.h" #include "brave/browser/brave_browser_process_impl.h" -#include "brave/components/tor/buildflags/buildflags.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" -#include "brave/components/tor/tor_profile_service_impl.h" -#endif +#include "content/public/browser/browser_context.h" namespace { -std::set g_profile_set; +std::set g_context_set; } // static @@ -63,14 +60,13 @@ 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) if (!brave::IsTorProfile(context)) return nullptr; base::FilePath user_data_dir; @@ -86,13 +82,10 @@ KeyedService* TorProfileServiceFactory::BuildServiceInstanceFor( // We only care about Tor incognito profiles for deciding whether to KillTor. if (context->IsOffTheRecord()) { - g_profile_set.emplace(Profile::FromBrowserContext(context)); + g_context_set.emplace(context); } return tor_profile_service.release(); -#else - return nullptr; -#endif } content::BrowserContext* TorProfileServiceFactory::GetBrowserContextToUse( @@ -104,9 +97,8 @@ content::BrowserContext* TorProfileServiceFactory::GetBrowserContextToUse( 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::GetForContext(context, false)); if (service) { @@ -115,13 +107,11 @@ void TorProfileServiceFactory::BrowserContextShutdown( 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/ui/brave_browser_command_controller_browsertest.cc b/browser/ui/brave_browser_command_controller_browsertest.cc index 65740d58a1ac..494b9567acfb 100644 --- a/browser/ui/brave_browser_command_controller_browsertest.cc +++ b/browser/ui/brave_browser_command_controller_browsertest.cc @@ -6,7 +6,6 @@ #include #include "brave/browser/profiles/profile_util.h" -#include "brave/browser/tor/tor_profile_service_factory.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" @@ -28,6 +27,10 @@ #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, diff --git a/browser/ui/browser_commands.cc b/browser/ui/browser_commands.cc index 4771d72e9fc4..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_factory.h" #include "brave/components/speedreader/buildflags.h" -#include "brave/components/tor/tor_profile_service.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,6 +47,7 @@ void NewOffTheRecordWindowTor(Browser* browser) { } void NewTorConnectionForSite(Browser* browser) { +#if BUILDFLAG(ENABLE_TOR) Profile* profile = browser->profile(); DCHECK(profile); tor::TorProfileService* service = @@ -53,6 +58,7 @@ void NewTorConnectionForSite(Browser* browser) { if (!current_tab) return; service->SetNewTorCircuit(current_tab); +#endif } void AddNewProfile() { diff --git a/browser/ui/views/profiles/brave_incognito_menu_view.cc b/browser/ui/views/profiles/brave_incognito_menu_view.cc index d4e05a3f0914..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,6 @@ #include "brave/browser/brave_browser_process_impl.h" #include "brave/browser/profiles/profile_util.h" -#include "brave/browser/tor/tor_profile_service_factory.h" #include "brave/components/tor/buildflags/buildflags.h" #include "brave/grit/brave_generated_resources.h" #include "chrome/app/vector_icons/vector_icons.h" @@ -26,6 +25,7 @@ #include "ui/base/models/image_model.h" #if BUILDFLAG(ENABLE_TOR) +#include "brave/browser/tor/tor_profile_service_factory.h" #include "brave/components/tor/brave_tor_client_updater.h" #endif diff --git a/browser/ui/webui/brave_new_tab_message_handler.cc b/browser/ui/webui/brave_new_tab_message_handler.cc index e1fb9695a230..232bd04c058b 100644 --- a/browser/ui/webui/brave_new_tab_message_handler.cc +++ b/browser/ui/webui/brave_new_tab_message_handler.cc @@ -13,7 +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_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" @@ -26,7 +25,6 @@ #include "brave/components/ntp_background_images/browser/url_constants.h" #include "brave/components/ntp_background_images/browser/view_counter_service.h" #include "brave/components/ntp_background_images/common/pref_names.h" -#include "brave/components/tor/tor_profile_service.h" #include "chrome/browser/profiles/profile.h" #include "chrome/common/chrome_features.h" #include "content/public/browser/web_ui_data_source.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::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 295e09115bab..80d8066dcb1a 100644 --- a/browser/ui/webui/brave_new_tab_message_handler.h +++ b/browser/ui/webui/brave_new_tab_message_handler.h @@ -8,6 +8,7 @@ #include +#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 83a61e019204..8e366811ffe1 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/tor_profile_service_factory.h" #include "brave/common/pref_names.h" #include "brave/components/brave_webtorrent/grit/brave_webtorrent_resources.h" #include "chrome/browser/about_flags.h" @@ -36,6 +35,7 @@ #include "extensions/common/feature_switch.h" #if BUILDFLAG(ENABLE_TOR) +#include "brave/browser/tor/tor_profile_service_factory.h" #include "brave/components/tor/pref_names.h" #endif diff --git a/chromium_src/chrome/browser/profiles/profile_window.cc b/chromium_src/chrome/browser/profiles/profile_window.cc index cfea608b49a1..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_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::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 6c90319c3d80..a755ad7ec2ec 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 @@ -7,12 +7,16 @@ #include "brave/browser/autocomplete/brave_autocomplete_scheme_classifier.h" #include "brave/browser/profiles/profile_util.h" #include "brave/browser/renderer_context_menu/brave_spelling_options_submenu_observer.h" -#include "brave/browser/tor/tor_profile_service_factory.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_factory.h" +#endif + // Our .h file creates a masquerade for RenderViewContextMenu. Switch // back to the Chromium one for the Chromium implementation. #undef RenderViewContextMenu @@ -63,12 +67,16 @@ BraveRenderViewContextMenu::BraveRenderViewContextMenu( bool BraveRenderViewContextMenu::IsCommandIdEnabled(int id) const { switch (id) { case IDC_CONTENT_CONTEXT_OPENLINKTOR: +#if BUILDFLAG(ENABLE_TOR) if (brave::IsTorDisabledForProfile(GetProfile())) return false; return params_.link_url.is_valid() && IsURLAllowedInIncognito(params_.link_url, browser_context_) && !brave::IsTorProfile(GetProfile()); +#else + return false; +#endif default: return RenderViewContextMenu_Chromium::IsCommandIdEnabled(id); } @@ -107,6 +115,7 @@ void BraveRenderViewContextMenu::AddSpellCheckServiceItem( void BraveRenderViewContextMenu::InitMenu() { RenderViewContextMenu_Chromium::InitMenu(); +#if BUILDFLAG(ENABLE_TOR) // Add Open Link with Tor int index = -1; if (!TorProfileServiceFactory::IsTorDisabled() && @@ -124,6 +133,7 @@ void BraveRenderViewContextMenu::InitMenu() { is_app ? IDS_CONTENT_CONTEXT_OPENLINKTOR_INAPP : IDS_CONTENT_CONTEXT_OPENLINKTOR); } +#endif // Only show the translate item when go-translate is enabled. #if !BUILDFLAG(ENABLE_BRAVE_TRANSLATE_GO) diff --git a/test/BUILD.gn b/test/BUILD.gn index 22132000a73e..2535231edb28 100644 --- a/test/BUILD.gn +++ b/test/BUILD.gn @@ -494,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", @@ -712,6 +711,15 @@ if (!is_android) { ] } + if (enable_tor) { + sources += [ + "//brave/browser/brave_local_state_browsertest.cc", + ] + deps += [ + "//brave/browser/tor" + ] + } + if (is_win || is_linux) { sources += [ "//brave/browser/renderer_context_menu/brave_spelling_options_submenu_observer_browsertest.cc" ] } From e038c909c487f186ed707fe3556366b73d24926d Mon Sep 17 00:00:00 2001 From: Anthony Tseng Date: Thu, 22 Oct 2020 20:29:30 -0700 Subject: [PATCH 08/10] Fix enable_tor=false --- .../search_engine_tracker_browsertest.cc | 8 +++++++- browser/ui/BUILD.gn | 10 ++++++++-- ..._browser_command_controller_browsertest.cc | 4 +--- .../brave_app_menu_model_browsertest.cc | 4 ++-- .../location_bar/brave_location_bar_view.cc | 19 ++++++++++++++++--- .../location_bar/brave_location_bar_view.h | 10 +++++++++- .../brave_default_extensions_handler.cc | 18 ++++++++++++++---- .../brave_default_extensions_handler.h | 2 -- .../render_view_context_menu.cc | 2 +- 9 files changed, 58 insertions(+), 19 deletions(-) diff --git a/browser/search_engines/search_engine_tracker_browsertest.cc b/browser/search_engines/search_engine_tracker_browsertest.cc index bf0681149160..b115ccd684c2 100644 --- a/browser/search_engines/search_engine_tracker_browsertest.cc +++ b/browser/search_engines/search_engine_tracker_browsertest.cc @@ -9,13 +9,17 @@ #include "brave/browser/profiles/profile_util.h" #include "brave/browser/ui/browser_commands.h" #include "brave/components/search_engines/brave_prepopulated_engines.h" -#include "brave/components/tor/tor_launcher_factory.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/ui/BUILD.gn b/browser/ui/BUILD.gn index ad3302665d6b..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") @@ -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_browsertest.cc b/browser/ui/brave_browser_command_controller_browsertest.cc index 494b9567acfb..8324d258d154 100644 --- a/browser/ui/brave_browser_command_controller_browsertest.cc +++ b/browser/ui/brave_browser_command_controller_browsertest.cc @@ -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,7 +199,6 @@ IN_PROC_BROWSER_TEST_F(BraveBrowserCommandControllerTest, IDC_SHOW_BRAVE_WEBCOMPAT_REPORTER)); // Check tor commands when tor is disabled. -#if BUILDFLAG(ENABLE_TOR) TorProfileServiceFactory::SetTorDisabled(true); command_controller = browser()->command_controller(); EXPECT_FALSE( diff --git a/browser/ui/toolbar/brave_app_menu_model_browsertest.cc b/browser/ui/toolbar/brave_app_menu_model_browsertest.cc index 1d65d880c6c8..45fd3dbc1100 100644 --- a/browser/ui/toolbar/brave_app_menu_model_browsertest.cc +++ b/browser/ui/toolbar/brave_app_menu_model_browsertest.cc @@ -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/webui/settings/brave_default_extensions_handler.cc b/browser/ui/webui/settings/brave_default_extensions_handler.cc index 8e366811ffe1..e3c6115cc2c5 100644 --- a/browser/ui/webui/settings/brave_default_extensions_handler.cc +++ b/browser/ui/webui/settings/brave_default_extensions_handler.cc @@ -83,7 +83,6 @@ void BraveDefaultExtensionsHandler::RegisterMessages() { "getRestartNeeded", base::BindRepeating(&BraveDefaultExtensionsHandler::GetRestartNeeded, base::Unretained(this))); -#if BUILDFLAG(ENABLE_TOR) web_ui()->RegisterMessageCallback( "setTorEnabled", base::BindRepeating( @@ -97,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(); @@ -233,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(); TorProfileServiceFactory::SetTorDisabled(!enabled); +#endif } void BraveDefaultExtensionsHandler::IsTorEnabled( @@ -248,14 +247,22 @@ void BraveDefaultExtensionsHandler::IsTorEnabled( AllowJavascript(); ResolveJavascriptCallback( args->GetList()[0], +#if BUILDFLAG(ENABLE_TOR) base::Value(!TorProfileServiceFactory::IsTorDisabled())); +#else + base::Value(false)); +#endif } void BraveDefaultExtensionsHandler::OnTorEnabledChanged() { if (IsJavascriptAllowed()) { FireWebUIListener( "tor-enabled-changed", +#if BUILDFLAG(ENABLE_TOR) base::Value(!TorProfileServiceFactory::IsTorDisabled())); +#else + base::Value(false)); +#endif } } @@ -263,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 0af0cbcee3f1..dd9e5aa2490b 100644 --- a/browser/ui/webui/settings/brave_default_extensions_handler.h +++ b/browser/ui/webui/settings/brave_default_extensions_handler.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/renderer_context_menu/render_view_context_menu.cc b/chromium_src/chrome/browser/renderer_context_menu/render_view_context_menu.cc index a755ad7ec2ec..81e0588773f7 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 @@ -115,9 +115,9 @@ void BraveRenderViewContextMenu::AddSpellCheckServiceItem( void BraveRenderViewContextMenu::InitMenu() { RenderViewContextMenu_Chromium::InitMenu(); + int index = -1; #if BUILDFLAG(ENABLE_TOR) // Add Open Link with Tor - int index = -1; if (!TorProfileServiceFactory::IsTorDisabled() && !params_.link_url.is_empty()) { const Browser* browser = GetBrowser(); From f7c65a925a77878c44ebd04cdb22eb67987fd370 Mon Sep 17 00:00:00 2001 From: Anthony Tseng Date: Mon, 26 Oct 2020 14:14:48 -0700 Subject: [PATCH 09/10] Remove BraveTorClientUpdaterDelegate and avoid unused variable warning --- browser/brave_browser_process_impl.cc | 5 +-- browser/tor/BUILD.gn | 2 - .../tor/brave_tor_client_updater_delegate.cc | 40 ------------------- .../tor/brave_tor_client_updater_delegate.h | 35 ---------------- .../render_view_context_menu.cc | 2 + components/tor/brave_tor_client_updater.cc | 30 ++++++++++---- components/tor/brave_tor_client_updater.h | 16 +++----- 7 files changed, 32 insertions(+), 98 deletions(-) delete mode 100644 browser/tor/brave_tor_client_updater_delegate.cc delete mode 100644 browser/tor/brave_tor_client_updater_delegate.h diff --git a/browser/brave_browser_process_impl.cc b/browser/brave_browser_process_impl.cc index 57f3596cff33..0dc99dc198d4 100644 --- a/browser/brave_browser_process_impl.cc +++ b/browser/brave_browser_process_impl.cc @@ -69,7 +69,6 @@ #endif #if BUILDFLAG(ENABLE_TOR) -#include "brave/browser/tor/brave_tor_client_updater_delegate.h" #include "brave/components/tor/brave_tor_client_updater.h" #include "brave/components/tor/pref_names.h" #endif @@ -316,11 +315,9 @@ BraveBrowserProcessImpl::tor_client_updater() { base::FilePath user_data_dir; base::PathService::Get(chrome::DIR_USER_DATA, &user_data_dir); - std::unique_ptr delegate = - std::make_unique(user_data_dir); tor_client_updater_.reset(new tor::BraveTorClientUpdater( - brave_component_updater_delegate(), std::move(delegate))); + brave_component_updater_delegate(), local_state(), user_data_dir)); return tor_client_updater_.get(); } diff --git a/browser/tor/BUILD.gn b/browser/tor/BUILD.gn index 0d7f4d23ea8a..862ddc018ada 100644 --- a/browser/tor/BUILD.gn +++ b/browser/tor/BUILD.gn @@ -13,8 +13,6 @@ source_set("tor") { if (enable_tor) { sources = [ - "brave_tor_client_updater_delegate.cc", - "brave_tor_client_updater_delegate.h", "onion_location_navigation_throttle_delegate.cc", "onion_location_navigation_throttle_delegate.h", "tor_profile_service_factory.cc", diff --git a/browser/tor/brave_tor_client_updater_delegate.cc b/browser/tor/brave_tor_client_updater_delegate.cc deleted file mode 100644 index 47f0eb4d26dd..000000000000 --- a/browser/tor/brave_tor_client_updater_delegate.cc +++ /dev/null @@ -1,40 +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/browser/tor/brave_tor_client_updater_delegate.h" - -#include "base/files/file_util.h" -#include "base/task/post_task.h" -#include "base/task_runner.h" -#include "brave/browser/tor/tor_profile_service_factory.h" - -namespace tor { - -namespace { -void DeleteDir(const base::FilePath& path) { - base::DeletePathRecursively(path); -} -} // namespace - -BraveTorClientUpdaterDelegate::BraveTorClientUpdaterDelegate( - const base::FilePath& user_data_dir) - : user_data_dir_(user_data_dir), - task_runner_(base::CreateSequencedTaskRunner( - {base::ThreadPool(), base::MayBlock()})) {} - -BraveTorClientUpdaterDelegate::~BraveTorClientUpdaterDelegate() = default; - -void BraveTorClientUpdaterDelegate::Cleanup(const char* component_id) { - DCHECK(!user_data_dir_.empty()); - base::FilePath tor_component_dir = user_data_dir_.AppendASCII(component_id); - task_runner_->PostTask(FROM_HERE, - base::BindOnce(&DeleteDir, tor_component_dir)); -} - -bool BraveTorClientUpdaterDelegate::IsTorDisabled() { - return TorProfileServiceFactory::IsTorDisabled(); -} - -} // namespace tor diff --git a/browser/tor/brave_tor_client_updater_delegate.h b/browser/tor/brave_tor_client_updater_delegate.h deleted file mode 100644 index c281b3acdd55..000000000000 --- a/browser/tor/brave_tor_client_updater_delegate.h +++ /dev/null @@ -1,35 +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/. */ - -#ifndef BRAVE_BROWSER_TOR_BRAVE_TOR_CLIENT_UPDATER_DELEGATE_H_ -#define BRAVE_BROWSER_TOR_BRAVE_TOR_CLIENT_UPDATER_DELEGATE_H_ - -#include "base/files/file_path.h" -#include "brave/components/tor/brave_tor_client_updater.h" - -namespace tor { - -class BraveTorClientUpdaterDelegate : public BraveTorClientUpdater::Delegate { - public: - explicit BraveTorClientUpdaterDelegate(const base::FilePath& user_data_dir); - ~BraveTorClientUpdaterDelegate() override; - - // BraveTorClientUpdater::Delegate - void Cleanup(const char* component_id) override; - bool IsTorDisabled() override; - - private: - base::FilePath user_data_dir_; - - scoped_refptr task_runner_; - - BraveTorClientUpdaterDelegate(const BraveTorClientUpdaterDelegate&) = delete; - BraveTorClientUpdaterDelegate& operator=( - const BraveTorClientUpdaterDelegate&) = delete; -}; - -} // namespace tor - -#endif // BRAVE_BROWSER_TOR_BRAVE_TOR_CLIENT_UPDATER_DELEGATE_H_ 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 81e0588773f7..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 @@ -115,7 +115,9 @@ 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 if (!TorProfileServiceFactory::IsTorDisabled() && diff --git a/components/tor/brave_tor_client_updater.cc b/components/tor/brave_tor_client_updater.cc index 6fb3293614f3..e5ddd4abef6d 100644 --- a/components/tor/brave_tor_client_updater.cc +++ b/components/tor/brave_tor_client_updater.cc @@ -17,7 +17,9 @@ #include "base/task/post_task.h" #include "base/task_runner.h" #include "base/task_runner_util.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; @@ -61,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) @@ -105,22 +111,22 @@ std::string BraveTorClientUpdater::g_tor_client_component_base64_public_key_( BraveTorClientUpdater::BraveTorClientUpdater( BraveComponent::Delegate* component_delegate, - std::unique_ptr delegate) + PrefService* local_state, + const base::FilePath& user_data_dir) : BraveComponent(component_delegate), task_runner_(base::CreateSequencedTaskRunner( {base::ThreadPool(), base::MayBlock()})), registered_(false), - delegate_(std::move(delegate)), - weak_ptr_factory_(this) { - DCHECK(delegate_); -} + local_state_(local_state), + user_data_dir_(user_data_dir), + weak_ptr_factory_(this) {} BraveTorClientUpdater::~BraveTorClientUpdater() {} void BraveTorClientUpdater::Register() { const base::CommandLine& command_line = *base::CommandLine::ForCurrentProcess(); - if (delegate_->IsTorDisabled() || + if (IsTorDisabled() || command_line.HasSwitch(tor::kDisableTorClientUpdaterExtension) || registered_) { return; @@ -139,7 +145,11 @@ void BraveTorClientUpdater::Unregister() { } void BraveTorClientUpdater::Cleanup() { - delegate_->Cleanup(kTorClientComponentId); + 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) { @@ -163,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); } diff --git a/components/tor/brave_tor_client_updater.h b/components/tor/brave_tor_client_updater.h index d022b5414550..0365ecbf57a0 100644 --- a/components/tor/brave_tor_client_updater.h +++ b/components/tor/brave_tor_client_updater.h @@ -18,6 +18,7 @@ class BraveProfileManagerTest; class BraveProfileManagerExtensionTest; class BraveTorClientUpdaterTest; +class PrefService; class SearchEngineProviderServiceTest; using brave_component_updater::BraveComponent; @@ -47,15 +48,9 @@ class BraveTorClientUpdater : public BraveComponent { ~Observer() override = default; }; - class Delegate { - public: - virtual ~Delegate() = default; - virtual void Cleanup(const char* component_id) = 0; - virtual bool IsTorDisabled() = 0; - }; - BraveTorClientUpdater(BraveComponent::Delegate* component_delegate, - std::unique_ptr delegate); + PrefService* local_state, + const base::FilePath& user_data_dir); ~BraveTorClientUpdater() override; void Register(); @@ -73,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; @@ -92,8 +88,8 @@ class BraveTorClientUpdater : public BraveComponent { bool registered_; base::FilePath executable_path_; base::ObserverList observers_; - - std::unique_ptr delegate_; + PrefService* local_state_; + base::FilePath user_data_dir_; base::WeakPtrFactory weak_ptr_factory_; From 62cfbb85b1d010f05eb47e76d2410d7320620bb2 Mon Sep 17 00:00:00 2001 From: Anthony Tseng Date: Tue, 27 Oct 2020 10:34:23 -0700 Subject: [PATCH 10/10] Use tor_strings in components and address various review feedbacks --- app/brave_generated_resources.grd | 3 --- browser/tor/BUILD.gn | 1 + .../{ => tor}/brave_local_state_browsertest.cc | 0 components/resources/BUILD.gn | 2 ++ components/resources/brave_components_strings.grd | 1 + components/resources/tor_strings.grdp | 8 ++++++++ components/tor/BUILD.gn | 11 ++--------- components/tor/tor_launcher_factory.cc | 2 +- components/tor/tor_profile_service.cc | 6 ++---- components/tor/tor_switches.cc | 15 --------------- components/tor/tor_switches.h | 3 ++- test/BUILD.gn | 9 --------- 12 files changed, 19 insertions(+), 42 deletions(-) rename browser/{ => tor}/brave_local_state_browsertest.cc (100%) create mode 100644 components/resources/tor_strings.grdp delete mode 100644 components/tor/tor_switches.cc 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/tor/BUILD.gn b/browser/tor/BUILD.gn index 862ddc018ada..c37d5eded1a3 100644 --- a/browser/tor/BUILD.gn +++ b/browser/tor/BUILD.gn @@ -66,6 +66,7 @@ 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", ] diff --git a/browser/brave_local_state_browsertest.cc b/browser/tor/brave_local_state_browsertest.cc similarity index 100% rename from browser/brave_local_state_browsertest.cc rename to browser/tor/brave_local_state_browsertest.cc 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/tor/BUILD.gn b/components/tor/BUILD.gn index 699704439185..3317ccaf999c 100644 --- a/components/tor/BUILD.gn +++ b/components/tor/BUILD.gn @@ -9,11 +9,11 @@ source_set("tor") { "tor_constants.cc", "tor_constants.h", "tor_launcher_service_observer.h", + "tor_switches.h", ] deps = [ ":pref_names", - ":switches", "//base", ] @@ -45,8 +45,8 @@ source_set("tor") { } deps += [ - "//brave/app:brave_generated_resources_grit", "//brave/components/brave_component_updater/browser", + "//brave/components/resources:strings", "//brave/components/services/tor/public/interfaces", "//components/pref_registry", "//components/prefs", @@ -67,13 +67,6 @@ source_set("pref_names") { ] } -source_set("switches") { - sources = [ - "tor_switches.cc", - "tor_switches.h", - ] -} - source_set("tor_unit_tests") { testonly = true if (enable_tor) { diff --git a/components/tor/tor_launcher_factory.cc b/components/tor/tor_launcher_factory.cc index d129202022dc..0c0a53544c99 100644 --- a/components/tor/tor_launcher_factory.cc +++ b/components/tor/tor_launcher_factory.cc @@ -12,7 +12,7 @@ #include "base/task/post_task.h" #include "brave/components/tor/service_sandbox_type.h" #include "brave/components/tor/tor_profile_service_impl.h" -#include "brave/grit/brave_generated_resources.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" diff --git a/components/tor/tor_profile_service.cc b/components/tor/tor_profile_service.cc index 6b24be296331..7c4d33ac1090 100644 --- a/components/tor/tor_profile_service.cc +++ b/components/tor/tor_profile_service.cc @@ -15,11 +15,9 @@ namespace tor { -TorProfileService::TorProfileService() { -} +TorProfileService::TorProfileService() = default; -TorProfileService::~TorProfileService() { -} +TorProfileService::~TorProfileService() = default; // static void TorProfileService::RegisterLocalStatePrefs(PrefRegistrySimple* registry) { diff --git a/components/tor/tor_switches.cc b/components/tor/tor_switches.cc deleted file mode 100644 index b7058d4bd39e..000000000000 --- a/components/tor/tor_switches.cc +++ /dev/null @@ -1,15 +0,0 @@ -/* 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/. */ - -#include "brave/components/tor/tor_switches.h" - -namespace tor { - -// Allows disabling the Tor client updater extension. -const char kDisableTorClientUpdaterExtension[] = - "disable-tor-client-updater-extension"; - -} // namespace tor - diff --git a/components/tor/tor_switches.h b/components/tor/tor_switches.h index 830a23774fae..b182bba63559 100644 --- a/components/tor/tor_switches.h +++ b/components/tor/tor_switches.h @@ -8,7 +8,8 @@ namespace tor { -extern const char kDisableTorClientUpdaterExtension[]; +constexpr char kDisableTorClientUpdaterExtension[] = + "disable-tor-client-updater-extension"; } // namespace tor diff --git a/test/BUILD.gn b/test/BUILD.gn index 2535231edb28..0ba0acc327ed 100644 --- a/test/BUILD.gn +++ b/test/BUILD.gn @@ -711,15 +711,6 @@ if (!is_android) { ] } - if (enable_tor) { - sources += [ - "//brave/browser/brave_local_state_browsertest.cc", - ] - deps += [ - "//brave/browser/tor" - ] - } - if (is_win || is_linux) { sources += [ "//brave/browser/renderer_context_menu/brave_spelling_options_submenu_observer_browsertest.cc" ] }