From 6b1b570af707543ba62cf607cfef618c36e3d7ea Mon Sep 17 00:00:00 2001 From: Jocelyn Liu Date: Tue, 1 Oct 2019 14:58:17 -0700 Subject: [PATCH 01/16] Fix the logic around executable path in BraveTorClientUpdater --- browser/BUILD.gn | 8 ++ browser/brave_browser_process_impl.cc | 7 +- browser/brave_content_browser_client.cc | 5 +- browser/extensions/BUILD.gn | 2 - .../extensions/brave_tor_client_updater.cc | 102 +++++++++++------- browser/extensions/brave_tor_client_updater.h | 21 +++- browser/tor/tor_profile_service.h | 4 - browser/tor/tor_profile_service_impl.cc | 42 ++++---- browser/tor/tor_profile_service_impl.h | 14 ++- 9 files changed, 130 insertions(+), 75 deletions(-) diff --git a/browser/BUILD.gn b/browser/BUILD.gn index 9110163a21ac..21238d839ea4 100644 --- a/browser/BUILD.gn +++ b/browser/BUILD.gn @@ -6,6 +6,7 @@ import("//brave/components/brave_webtorrent/browser/buildflags/buildflags.gni") import("//brave/components/greaselion/browser/buildflags/buildflags.gni") import("//build/buildflag_header.gni") import("//build/config/features.gni") +import("//chrome/common/features.gni") import("//extensions/buildflags/buildflags.gni") import("//third_party/widevine/cdm/widevine.gni") import("//third_party/protobuf/proto_library.gni") @@ -81,6 +82,13 @@ source_set("browser_process") { "update_util.h", ] + if (enable_tor) { + sources += [ + # TODO(bridiver) - change the directory for these + "extensions/brave_tor_client_updater.cc", + "extensions/brave_tor_client_updater.h", + ] + } if (enable_sparkle) { sources += [ "mac/sparkle_glue.mm", diff --git a/browser/brave_browser_process_impl.cc b/browser/brave_browser_process_impl.cc index 58cdd95de522..21d22b824def 100644 --- a/browser/brave_browser_process_impl.cc +++ b/browser/brave_browser_process_impl.cc @@ -14,8 +14,8 @@ #include "brave/browser/brave_stats_updater.h" #include "brave/browser/component_updater/brave_component_updater_configurator.h" #include "brave/browser/component_updater/brave_component_updater_delegate.h" -#include "brave/browser/extensions/brave_tor_client_updater.h" #include "brave/browser/profiles/brave_profile_manager.h" +#include "brave/browser/tor/buildflags.h" #include "brave/components/brave_component_updater/browser/local_data_files_service.h" #include "brave/components/brave_shields/browser/ad_block_custom_filters_service.h" #include "brave/components/brave_shields/browser/ad_block_regional_service_manager.h" @@ -27,6 +27,7 @@ #include "brave/components/p3a/buildflags.h" #include "brave/components/p3a/brave_histogram_rewrite.h" #include "brave/components/p3a/brave_p3a_service.h" +#include "chrome/common/buildflags.h" #include "chrome/common/chrome_paths.h" #include "components/component_updater/component_updater_service.h" #include "components/component_updater/timer_update_scheduler.h" @@ -49,6 +50,10 @@ #include "brave/components/greaselion/browser/greaselion_download_service.h" #endif +#if BUILDFLAG(ENABLE_TOR) +#include "brave/browser/extensions/brave_tor_client_updater.h" +#endif + #if defined(OS_ANDROID) #include "chrome/browser/android/chrome_feature_list.h" #include "chrome/browser/android/component_updater/background_task_update_scheduler.h" diff --git a/browser/brave_content_browser_client.cc b/browser/brave_content_browser_client.cc index dfa4fc3fbc31..fb9759cc5fcd 100644 --- a/browser/brave_content_browser_client.cc +++ b/browser/brave_content_browser_client.cc @@ -209,9 +209,10 @@ void BraveContentBrowserClient::AdjustUtilityServiceProcessCommandLine( if (identity.name() == tor::mojom::kServiceName) { base::FilePath path = g_brave_browser_process->tor_client_updater()->GetExecutablePath(); - DCHECK(!path.empty()); - command_line->AppendSwitchPath(tor::switches::kTorExecutablePath, + if (!path.empty()) { + command_line->AppendSwitchPath(tor::switches::kTorExecutablePath, path.BaseName()); + } } #endif } diff --git a/browser/extensions/BUILD.gn b/browser/extensions/BUILD.gn index c6b09fe0da68..c387a5090fcc 100644 --- a/browser/extensions/BUILD.gn +++ b/browser/extensions/BUILD.gn @@ -38,8 +38,6 @@ source_set("extensions") { "brave_extensions_browser_api_provider.h", "brave_theme_event_router.cc", "brave_theme_event_router.h", - "brave_tor_client_updater.cc", - "brave_tor_client_updater.h", "updater/brave_update_client_config.cc", "updater/brave_update_client_config.h", ] diff --git a/browser/extensions/brave_tor_client_updater.cc b/browser/extensions/brave_tor_client_updater.cc index 43892218e6aa..77f240dd6c0a 100644 --- a/browser/extensions/brave_tor_client_updater.cc +++ b/browser/extensions/brave_tor_client_updater.cc @@ -12,12 +12,54 @@ #include "base/files/file_path.h" #include "base/files/file_util.h" #include "base/task/post_task.h" +#include "base/task_runner.h" +#include "base/task_runner_util.h" #include "third_party/re2/src/re2/re2.h" using brave_component_updater::BraveComponent; namespace extensions { +namespace { + +base::FilePath InitExecutablePath( + const base::FilePath& install_dir) { + base::FilePath executable_path; + base::FileEnumerator traversal(install_dir, false, + base::FileEnumerator::FILES, + FILE_PATH_LITERAL("tor-*")); + for (base::FilePath current = traversal.Next(); !current.empty(); + current = traversal.Next()) { + base::FileEnumerator::FileInfo file_info = traversal.GetInfo(); + if (!RE2::FullMatch(file_info.GetName().MaybeAsASCII(), + "tor-\\d+\\.\\d+\\.\\d+\\.\\d+-\\w+-brave-\\d+")) + continue; + executable_path = current; + break; + } + + if (executable_path.empty()) { + LOG(ERROR) << "Failed to locate Tor client executable in " + << install_dir.value().c_str(); + return base::FilePath(); + } + +#if defined(OS_POSIX) + // Ensure that Tor client executable has appropriate file + // permissions, as CRX unzipping does not preserve them. + // See https://crbug.com/555011 + if (!base::SetPosixFilePermissions(executable_path, 0755)) { + LOG(ERROR) << "Failed to set executable permission on " + << executable_path.value().c_str(); + return base::FilePath(); + } +#endif // defined(OS_POSIX) + + return executable_path; +} + +} // namespace + #if defined(OS_WIN) const char kTorClientComponentName[] = "Brave Tor Client Updater (Windows)"; const char kTorClientComponentId[] = "cpoalefficncklhjfpglfiplenlpccdb"; @@ -62,7 +104,8 @@ BraveTorClientUpdater::BraveTorClientUpdater(BraveComponent::Delegate* delegate) : BraveComponent(delegate), task_runner_( base::CreateSequencedTaskRunnerWithTraits({base::MayBlock()})), - registered_(false) { + registered_(false), + weak_ptr_factory_(this) { } BraveTorClientUpdater::~BraveTorClientUpdater() { @@ -78,53 +121,32 @@ void BraveTorClientUpdater::Register() { registered_ = true; } -base::FilePath BraveTorClientUpdater::GetExecutablePath() const { - return executable_path_; +void BraveTorClientUpdater::SetExecutablePath(const base::FilePath& path) { + executable_path_ = path; + for (Observer& observer : observers_) + observer.OnExecutableReady(path); } -void BraveTorClientUpdater::InitExecutablePath( - const base::FilePath& install_dir) { - base::FilePath executable_path; - base::FileEnumerator traversal(install_dir, false, - base::FileEnumerator::FILES, - FILE_PATH_LITERAL("tor-*")); - for (base::FilePath current = traversal.Next(); !current.empty(); - current = traversal.Next()) { - base::FileEnumerator::FileInfo file_info = traversal.GetInfo(); - if (!RE2::FullMatch(file_info.GetName().MaybeAsASCII(), - "tor-\\d+\\.\\d+\\.\\d+\\.\\d+-\\w+-brave-\\d+")) - continue; - executable_path = current; - break; - } - - if (executable_path.empty()) { - LOG(ERROR) << "Failed to locate Tor client executable in " - << install_dir.value().c_str(); - return; - } - -#if defined(OS_POSIX) - // Ensure that Tor client executable has appropriate file - // permissions, as CRX unzipping does not preserve them. - // See https://crbug.com/555011 - if (!base::SetPosixFilePermissions(executable_path, 0755)) { - LOG(ERROR) << "Failed to set executable permission on " - << executable_path.value().c_str(); - return; - } -#endif // defined(OS_POSIX) - - executable_path_ = executable_path; +base::FilePath BraveTorClientUpdater::GetExecutablePath() const { + return executable_path_; } void BraveTorClientUpdater::OnComponentReady( const std::string& component_id, const base::FilePath& install_dir, const std::string& manifest) { - GetTaskRunner()->PostTask( - FROM_HERE, base::Bind(&BraveTorClientUpdater::InitExecutablePath, - base::Unretained(this), install_dir)); + base::PostTaskAndReplyWithResult(GetTaskRunner().get(), FROM_HERE, + base::BindOnce(&InitExecutablePath, install_dir), + base::BindOnce(&BraveTorClientUpdater::SetExecutablePath, + weak_ptr_factory_.GetWeakPtr())); +} + +void BraveTorClientUpdater::AddObserver(Observer* observer) { + observers_.AddObserver(observer); +} + +void BraveTorClientUpdater::RemoveObserver(Observer* observer) { + observers_.RemoveObserver(observer); } // static diff --git a/browser/extensions/brave_tor_client_updater.h b/browser/extensions/brave_tor_client_updater.h index 63b853aa88b1..7759518dfed8 100644 --- a/browser/extensions/brave_tor_client_updater.h +++ b/browser/extensions/brave_tor_client_updater.h @@ -10,9 +10,12 @@ #include #include "base/files/file_path.h" +#include "base/memory/weak_ptr.h" +#include "base/observer_list.h" #include "base/sequenced_task_runner.h" #include "brave/components/brave_component_updater/browser/brave_component.h" +class BraveProfileManagerTest; class BraveTorClientUpdaterTest; using brave_component_updater::BraveComponent; @@ -35,6 +38,14 @@ extern const char kTorClientComponentBase64PublicKey[]; class BraveTorClientUpdater : public BraveComponent { public: + class Observer : public base::CheckedObserver { + public: + virtual void OnExecutableReady(const base::FilePath& path) = 0; + + protected: + ~Observer() override = default; + }; + explicit BraveTorClientUpdater(BraveComponent::Delegate* delegate); ~BraveTorClientUpdater() override; @@ -44,12 +55,16 @@ class BraveTorClientUpdater : public BraveComponent { return task_runner_; } + void AddObserver(Observer* observer); + void RemoveObserver(Observer* observer); + protected: void OnComponentReady(const std::string& component_id, const base::FilePath& install_dir, const std::string& manifest) override; private: + friend class ::BraveProfileManagerTest; friend class ::BraveTorClientUpdaterTest; static std::string g_tor_client_component_name_; static std::string g_tor_client_component_id_; @@ -57,10 +72,14 @@ class BraveTorClientUpdater : public BraveComponent { static void SetComponentIdAndBase64PublicKeyForTest( const std::string& component_id, const std::string& component_base64_public_key); - void InitExecutablePath(const base::FilePath& install_dir); + void SetExecutablePath(const base::FilePath& path); + scoped_refptr task_runner_; bool registered_; base::FilePath executable_path_; + base::ObserverList observers_; + + base::WeakPtrFactory weak_ptr_factory_; DISALLOW_COPY_AND_ASSIGN(BraveTorClientUpdater); }; diff --git a/browser/tor/tor_profile_service.h b/browser/tor/tor_profile_service.h index 67eff38cb2bb..255ae988819f 100644 --- a/browser/tor/tor_profile_service.h +++ b/browser/tor/tor_profile_service.h @@ -41,11 +41,7 @@ class TorProfileService : public KeyedService { static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry); static void RegisterLocalStatePrefs(PrefRegistrySimple* registry); - virtual void LaunchTor(const TorConfig&) = 0; - virtual void ReLaunchTor(const TorConfig&) = 0; virtual void SetNewTorCircuit(content::WebContents* web_contents) = 0; - virtual const TorConfig& GetTorConfig() = 0; - virtual int64_t GetTorPid() = 0; virtual std::unique_ptr CreateProxyConfigService() = 0; void AddObserver(TorLauncherServiceObserver* observer); diff --git a/browser/tor/tor_profile_service_impl.cc b/browser/tor/tor_profile_service_impl.cc index 10f03ab57fa2..e90276937621 100644 --- a/browser/tor/tor_profile_service_impl.cc +++ b/browser/tor/tor_profile_service_impl.cc @@ -11,6 +11,7 @@ #include "base/bind.h" #include "base/task/post_task.h" +#include "brave/browser/brave_browser_process_impl.h" #include "brave/browser/tor/tor_launcher_service_observer.h" #include "brave/common/tor/pref_names.h" #include "brave/common/tor/tor_constants.h" @@ -120,25 +121,33 @@ void OnNewTorCircuit(std::unique_ptr tracker, TorProfileServiceImpl::TorProfileServiceImpl(Profile* profile) : profile_(profile), weak_ptr_factory_(this) { - tor_launcher_factory_ = TorLauncherFactory::GetInstance(); - tor_launcher_factory_->AddObserver(this); - if (GetTorPid() < 0) { - tor::TorConfig config(GetTorExecutablePath(), GetTorProxyURI()); - LaunchTor(config); - } + g_brave_browser_process->tor_client_updater()->AddObserver(this); + OnExecutableReady(GetTorExecutablePath()); } TorProfileServiceImpl::~TorProfileServiceImpl() { - tor_launcher_factory_->RemoveObserver(this); + if (tor_launcher_factory_) + tor_launcher_factory_->RemoveObserver(this); } -void TorProfileServiceImpl::LaunchTor(const TorConfig& config) { - tor_launcher_factory_->LaunchTorProcess(config); +void TorProfileServiceImpl::OnExecutableReady(const base::FilePath& path) { + if (path.empty()) + return; + + g_brave_browser_process->tor_client_updater()->RemoveObserver(this); + + tor_launcher_factory_ = TorLauncherFactory::GetInstance(); + tor_launcher_factory_->AddObserver(this); + + if (tor_launcher_factory_->GetTorPid() < 0) { + LaunchTor(); + } } -void TorProfileServiceImpl::ReLaunchTor(const TorConfig& config) { - tor_launcher_factory_->ReLaunchTorProcess(config); +void TorProfileServiceImpl::LaunchTor() { + tor::TorConfig config(GetTorExecutablePath(), GetTorProxyURI()); + tor_launcher_factory_->LaunchTorProcess(config); } void TorProfileServiceImpl::SetNewTorCircuit(WebContents* tab) { @@ -166,16 +175,9 @@ void TorProfileServiceImpl::SetNewTorCircuit(WebContents* tab) { url, std::move(proxy_lookup_client_ptr)); } -const TorConfig& TorProfileServiceImpl::GetTorConfig() { - return tor_launcher_factory_->GetTorConfig(); -} - -int64_t TorProfileServiceImpl::GetTorPid() { - return tor_launcher_factory_->GetTorPid(); -} - void TorProfileServiceImpl::KillTor() { - tor_launcher_factory_->KillTorProcess(); + if (tor_launcher_factory_) + tor_launcher_factory_->KillTorProcess(); } void TorProfileServiceImpl::NotifyTorLauncherCrashed() { diff --git a/browser/tor/tor_profile_service_impl.h b/browser/tor/tor_profile_service_impl.h index 29d74929d386..2648fbdc7212 100644 --- a/browser/tor/tor_profile_service_impl.h +++ b/browser/tor/tor_profile_service_impl.h @@ -10,6 +10,7 @@ #include "base/memory/weak_ptr.h" #include "base/optional.h" +#include "brave/browser/extensions/brave_tor_client_updater.h" #include "brave/browser/tor/tor_launcher_factory.h" #include "brave/browser/tor/tor_profile_service.h" #include "net/proxy_resolution/proxy_info.h" @@ -21,23 +22,21 @@ class ProxyConfigService; class ProxyConfigServiceTor; } +using extensions::BraveTorClientUpdater; + namespace tor { using NewTorCircuitCallback = base::OnceCallback& proxy_info)>; class TorProfileServiceImpl : public TorProfileService, - public base::CheckedObserver { + public BraveTorClientUpdater::Observer { public: explicit TorProfileServiceImpl(Profile* profile); ~TorProfileServiceImpl() override; // TorProfileService: - void LaunchTor(const TorConfig&) override; - void ReLaunchTor(const TorConfig&) override; void SetNewTorCircuit(content::WebContents* web_contents) override; - const TorConfig& GetTorConfig() override; - int64_t GetTorPid() override; std::unique_ptr CreateProxyConfigService() override; void KillTor(); @@ -48,6 +47,11 @@ class TorProfileServiceImpl : public TorProfileService, void NotifyTorLaunched(bool result, int64_t pid); private: + void LaunchTor(); + + // BraveTorClientUpdater::Observer + void OnExecutableReady(const base::FilePath& path) override; + Profile* profile_; // NOT OWNED TorLauncherFactory* tor_launcher_factory_; // Singleton net::ProxyConfigServiceTor* proxy_config_service_; // NOT OWNED From 3cec9460b57c1132a967146f3a756f8d4411d600 Mon Sep 17 00:00:00 2001 From: Jocelyn Liu Date: Tue, 1 Oct 2019 15:26:41 -0700 Subject: [PATCH 02/16] Tor profile rework - Remove the guest profile stuff for current Tor profile impl. - When SwitchToTorProfile is called, a dummy Tor regular profile will be created at session-profiles/Tor Profile under current profile's path, and we will open a new window using its incognito profile. - ParentProfileData are implemented for getting Tor profile's parent profile. --- browser/profiles/BUILD.gn | 2 + browser/profiles/brave_profile_impl.cc | 69 ++++++ browser/profiles/brave_profile_impl.h | 41 ++++ browser/profiles/brave_profile_manager.cc | 48 ++++- browser/profiles/brave_profile_manager.h | 1 + browser/profiles/profile_util.cc | 198 +++++++++++++++++- browser/profiles/profile_util.h | 46 +++- .../chrome/browser/profiles/profile.cc | 7 - .../chrome/browser/profiles/profile_impl.cc | 8 +- .../chrome/browser/profiles/profile_impl.h | 20 ++ .../chrome/browser/profiles/profile_window.cc | 17 +- .../chrome/browser/profiles/profiles_state.cc | 23 +- .../chrome-browser-profiles-profile.cc.patch | 11 +- ...ome-browser-profiles-profile_impl.cc.patch | 19 +- ...rome-browser-profiles-profile_impl.h.patch | 12 ++ ...-browser-profiles-profile_manager.cc.patch | 40 ---- 16 files changed, 454 insertions(+), 108 deletions(-) create mode 100644 browser/profiles/brave_profile_impl.cc create mode 100644 browser/profiles/brave_profile_impl.h delete mode 100644 chromium_src/chrome/browser/profiles/profile.cc create mode 100644 chromium_src/chrome/browser/profiles/profile_impl.h create mode 100644 patches/chrome-browser-profiles-profile_impl.h.patch delete mode 100644 patches/chrome-browser-profiles-profile_manager.cc.patch diff --git a/browser/profiles/BUILD.gn b/browser/profiles/BUILD.gn index 32a82129ed5c..70f7551f77f0 100644 --- a/browser/profiles/BUILD.gn +++ b/browser/profiles/BUILD.gn @@ -1,5 +1,7 @@ source_set("profiles") { sources = [ + "brave_profile_impl.cc", + "brave_profile_impl.h", "brave_profile_manager.cc", "brave_profile_manager.h", "profile_util.cc", diff --git a/browser/profiles/brave_profile_impl.cc b/browser/profiles/brave_profile_impl.cc new file mode 100644 index 000000000000..1de206e7446b --- /dev/null +++ b/browser/profiles/brave_profile_impl.cc @@ -0,0 +1,69 @@ +/* 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/profiles/brave_profile_impl.h" + +#include "base/task/post_task.h" +#include "brave/browser/profiles/profile_util.h" +#include "chrome/browser/browser_process.h" +#include "chrome/browser/chrome_notification_types.h" +#include "chrome/browser/profiles/profile_manager.h" +#include "content/public/browser/notification_source.h" + +BraveProfileImpl::BraveProfileImpl( + const base::FilePath& path, + Delegate* delegate, + CreateMode create_mode, + scoped_refptr io_task_runner) + : ProfileImpl(path, delegate, create_mode, io_task_runner), + weak_ptr_factory_(this) { + // In sessions profiles, prefs are created from the original profile like how + // incognito profile works. By the time chromium start to observe prefs + // initialization in ProfileImpl constructor for the async creation case, + // prefs are already initialized and it's too late for the observer to get + // the notification, so we manually trigger the OnPrefsLoaded here. For the + // sync cases, OnPrefsLoaded will always be called at the end of ProfileImpl + // constructor, so there is no need to trigger it here. + // + // This need to be posted instead of running directly here because we need to + // finish the construction and register this profile first in ProfileManager + // before OnPrefsLoaded is called, otherwise we would hit a DCHECK in + // ProfileManager::OnProfileCreated which is called inside OnPrefsLoaded and + // is expecting the profile_info is already added then. + if (brave::IsSessionProfilePath(path) && + create_mode == CREATE_MODE_ASYNCHRONOUS) { + auto* parent_profile = brave::CreateParentProfileData(this); + + notification_registrar_.Add(this, + chrome::NOTIFICATION_PROFILE_DESTROYED, + content::Source(parent_profile)); + base::PostTaskAndReply( + FROM_HERE, base::DoNothing(), + base::BindOnce(&ProfileImpl::OnPrefsLoaded, + weak_ptr_factory_.GetWeakPtr(), create_mode, true)); + } +} + +BraveProfileImpl::~BraveProfileImpl() {} + +void BraveProfileImpl::Observe( + int type, + const content::NotificationSource& source, + const content::NotificationDetails& details) { + switch (type) { + case chrome::NOTIFICATION_PROFILE_DESTROYED: { + // this only happens when a profile is deleted because the profile manager + // ensures that session profiles are destroyed before their parents + // passing false for `success` removes the profile from the info cache + g_browser_process->profile_manager()->OnProfileCreated( + this, false, false); + break; + } + default: { + NOTREACHED(); // Unexpected notification. + break; + } + } +} diff --git a/browser/profiles/brave_profile_impl.h b/browser/profiles/brave_profile_impl.h new file mode 100644 index 000000000000..48b2d871e3cb --- /dev/null +++ b/browser/profiles/brave_profile_impl.h @@ -0,0 +1,41 @@ +/* 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_BROWSER_PROFILES_BRAVE_PROFILE_IMPL_H_ +#define BRAVE_BROWSER_PROFILES_BRAVE_PROFILE_IMPL_H_ + +#include "chrome/browser/profiles/profile_impl.h" + +#include "base/memory/weak_ptr.h" +#include "content/public/browser/notification_observer.h" +#include "content/public/browser/notification_registrar.h" + +class PrefStore; + +class BraveProfileImpl : public ProfileImpl, + public content::NotificationObserver { + public: + BraveProfileImpl(const base::FilePath& path, + Delegate* delegate, + CreateMode create_mode, + scoped_refptr io_task_runner); + ~BraveProfileImpl() override; + + // content::NotificationObserver override. + void Observe(int type, + const content::NotificationSource& source, + const content::NotificationDetails& details) override; + + private: + + // Listens for parent profile destruction. + content::NotificationRegistrar notification_registrar_; + + base::WeakPtrFactory weak_ptr_factory_; + + DISALLOW_COPY_AND_ASSIGN(BraveProfileImpl); +}; + +#endif // BRAVE_BROWSER_PROFILES_BRAVE_PROFILE_IMPL_H_ diff --git a/browser/profiles/brave_profile_manager.cc b/browser/profiles/brave_profile_manager.cc index 7c692e81aa2e..126592d25684 100644 --- a/browser/profiles/brave_profile_manager.cc +++ b/browser/profiles/brave_profile_manager.cc @@ -10,7 +10,7 @@ #include #include "base/metrics/histogram_macros.h" -#include "brave/browser/brave_browser_process_impl.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" @@ -18,16 +18,17 @@ #include "brave/common/pref_names.h" #include "brave/common/tor/pref_names.h" #include "brave/common/tor/tor_constants.h" -#include "brave/components/brave_webtorrent/browser/buildflags/buildflags.h" #include "brave/components/brave_ads/browser/ads_service_factory.h" #include "brave/components/brave_rewards/browser/rewards_service_factory.h" #include "brave/components/brave_shields/browser/ad_block_regional_service.h" #include "brave/components/brave_shields/browser/ad_block_service.h" #include "brave/components/brave_shields/browser/brave_shields_util.h" +#include "brave/components/brave_webtorrent/browser/buildflags/buildflags.h" #include "brave/content/browser/webui/brave_shared_resources_data_source.h" #include "chrome/browser/browser_process.h" #include "chrome/browser/profiles/profile_attributes_storage.h" #include "chrome/browser/profiles/profile_attributes_entry.h" +#include "chrome/browser/profiles/profiles_state.h" #include "chrome/common/chrome_constants.h" #include "chrome/common/pref_names.h" #include "chrome/grit/generated_resources.h" @@ -44,17 +45,32 @@ using content::BrowserThread; BraveProfileManager::BraveProfileManager(const base::FilePath& user_data_dir) - : ProfileManager(user_data_dir) { + : ProfileManager(user_data_dir) { MigrateProfileNames(); } +BraveProfileManager::~BraveProfileManager() { + std::vector profiles = GetLoadedProfiles(); + for (Profile* profile : profiles) { + if (brave::IsSessionProfile(profile)) { + // passing false for `success` removes the profile from the info cache + OnProfileCreated(profile, false, false); + } + } +} + // static +// TODO(bridiver) - this should take the last used profile dir as an argument base::FilePath BraveProfileManager::GetTorProfilePath() { DCHECK_CURRENTLY_ON(BrowserThread::UI); ProfileManager* profile_manager = g_browser_process->profile_manager(); + base::FilePath parent_path = + profile_manager->GetLastUsedProfileDir(profile_manager->user_data_dir()); - base::FilePath tor_path = profile_manager->user_data_dir(); + DCHECK(!brave::IsTorProfilePath(parent_path)); + + base::FilePath tor_path = parent_path.AppendASCII("session_profiles"); return tor_path.Append(tor::kTorProfileDir); } @@ -66,7 +82,6 @@ void BraveProfileManager::InitTorProfileUserPrefs(Profile* profile) { pref_service ->SetString(prefs::kProfileName, l10n_util::GetStringUTF8(IDS_PROFILES_TOR_PROFILE_NAME)); - pref_service->SetBoolean(tor::prefs::kProfileUsingTor, true); pref_service->SetString(prefs::kWebRTCIPHandlingPolicy, content::kWebRTCIPHandlingDisableNonProxiedUdp); pref_service->SetBoolean(prefs::kSafeBrowsingEnabled, false); @@ -83,7 +98,7 @@ void BraveProfileManager::InitTorProfileUserPrefs(Profile* profile) { } void BraveProfileManager::InitProfileUserPrefs(Profile* profile) { - if (profile->GetPath() == GetTorProfilePath()) { + if (brave::IsTorProfile(profile)) { InitTorProfileUserPrefs(profile); } else { ProfileManager::InitProfileUserPrefs(profile); @@ -94,9 +109,9 @@ std::string BraveProfileManager::GetLastUsedProfileName() { PrefService* local_state = g_browser_process->local_state(); DCHECK(local_state); const std::string last_used_profile_name = - local_state->GetString(prefs::kProfileLastUsed); + local_state->GetString(prefs::kProfileLastUsed); if (last_used_profile_name == - base::FilePath(tor::kTorProfileDir).AsUTF8Unsafe()) + base::FilePath(tor::kTorProfileDir).AsUTF8Unsafe()) return chrome::kInitialProfile; return ProfileManager::GetLastUsedProfileName(); } @@ -115,11 +130,22 @@ void BraveProfileManager::OnProfileCreated(Profile* profile, bool is_new_profile) { ProfileManager::OnProfileCreated(profile, success, is_new_profile); + if (!success) + return; + #if BUILDFLAG(ENABLE_TOR) - // we need to wait until OnProfileCreated to - // ensure that the request context is available - if (profile->GetPath() == GetTorProfilePath()) + if (brave::IsTorProfile(profile)) { + ProfileAttributesEntry* entry; + ProfileAttributesStorage& storage = GetProfileAttributesStorage(); + if (storage.GetProfileAttributesWithPath(profile->GetPath(), &entry)) { + profile->GetPrefs()->SetBoolean(prefs::kForceEphemeralProfiles, true); + entry->SetIsEphemeral(true); + } + + // We need to wait until OnProfileCreated to + // ensure that the request context is available. TorProfileServiceFactory::GetForProfile(profile); + } #endif } diff --git a/browser/profiles/brave_profile_manager.h b/browser/profiles/brave_profile_manager.h index cef6f7703241..0ada30697e8e 100644 --- a/browser/profiles/brave_profile_manager.h +++ b/browser/profiles/brave_profile_manager.h @@ -13,6 +13,7 @@ class BraveProfileManager : public ProfileManager { public: explicit BraveProfileManager(const base::FilePath& user_data_dir); + ~BraveProfileManager() override; // Returns the full path to be used for tor profiles. static base::FilePath GetTorProfilePath(); diff --git a/browser/profiles/profile_util.cc b/browser/profiles/profile_util.cc index 36ab7ccaa6c9..6d2c602aaab1 100644 --- a/browser/profiles/profile_util.cc +++ b/browser/profiles/profile_util.cc @@ -5,26 +5,206 @@ #include "brave/browser/profiles/profile_util.h" +#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/pref_names.h" +#include "brave/common/tor/tor_constants.h" +#include "chrome/browser/browser_process.h" #include "chrome/browser/profiles/profile.h" -#include "components/prefs/pref_service.h" +#include "chrome/browser/profiles/profile_key.h" +#include "chrome/browser/profiles/profile_manager.h" namespace brave { -bool IsTorProfile(const Profile* profile) { -#if BUILDFLAG(ENABLE_TOR) +namespace { + +using PathMap = std::map; + +PathMap* GetPathMap() { + static base::NoDestructor provider; + return provider.get(); +} + +Profile* GetFromPath(const base::FilePath& key) { + auto mapping = *GetPathMap(); + const auto& it = mapping.find(key); + if (it == mapping.end()) { + DCHECK(false); + return nullptr; + } + + return it->second; +} + +class ParentProfileData : public base::SupportsUserData::Data { + public: + ~ParentProfileData() override; + static void CreateForProfile(content::BrowserContext* context); + static ParentProfileData* FromProfile( + content::BrowserContext* context); + static const ParentProfileData* FromProfile( + const content::BrowserContext* context); + static const ParentProfileData* FromPath( + const base::FilePath& path); + + Profile* profile() const; + + std::unique_ptr Clone() override; + private: + static const void* const kUserDataKey; + static const void* UserDataKey(); + + ParentProfileData(Profile* profile); + + Profile* profile_; + base::FilePath path_; + + DISALLOW_COPY_AND_ASSIGN(ParentProfileData); +}; + +const void* const ParentProfileData::kUserDataKey = &kUserDataKey; + +// static +void ParentProfileData::CreateForProfile(content::BrowserContext* context) { + DCHECK(context); + if (FromProfile(context)) return; + + auto* profile = Profile::FromBrowserContext(context); + auto* profile_manager = g_browser_process->profile_manager(); + DCHECK(profile_manager); + + auto* parent_profile = + profile_manager->GetProfileByPath(GetParentProfilePath(profile)); + DCHECK(parent_profile); + DCHECK(parent_profile != profile); + + profile->SetUserData( + UserDataKey(), + base::WrapUnique(new ParentProfileData(parent_profile))); + + GetPathMap()->insert( + std::pair(profile->GetPath(), profile)); +} + +// static +ParentProfileData* ParentProfileData::FromProfile( + content::BrowserContext* context) { + DCHECK(context); + auto* profile = Profile::FromBrowserContext(context); + return + static_cast( + profile->GetOriginalProfile()->GetUserData(UserDataKey())); +} + +// static +const ParentProfileData* ParentProfileData::FromProfile( + const content::BrowserContext* context) { + DCHECK(context); + const auto* profile = static_cast(context); + return static_cast( + profile->GetOriginalProfile()->GetUserData(UserDataKey())); +} + +// static +const ParentProfileData* ParentProfileData::FromPath( + const base::FilePath& path) { + auto* profile = GetFromPath(path); DCHECK(profile); - return profile->GetPrefs()->GetBoolean(tor::prefs::kProfileUsingTor); + return FromProfile(profile); +} + +Profile* ParentProfileData::profile() const { + return profile_; +} + +std::unique_ptr +ParentProfileData::Clone() { + return base::WrapUnique(new ParentProfileData(profile_)); +} + +const void* ParentProfileData::UserDataKey() { + return &kUserDataKey; +} + +ParentProfileData::ParentProfileData(Profile* profile) + : profile_(profile), + path_(profile->GetPath()) { +} + +ParentProfileData::~ParentProfileData() { + GetPathMap()->erase(path_); +} + +} // namespace + +Profile* CreateParentProfileData(content::BrowserContext* context) { + ParentProfileData::CreateForProfile(context); + return ParentProfileData::FromProfile(context)->profile(); +} + +base::FilePath GetParentProfilePath(content::BrowserContext* context) { + return GetParentProfilePath(context->GetPath()); +} + +base::FilePath GetParentProfilePath(const base::FilePath& path) { + return path.DirName().DirName(); +} + +bool IsSessionProfile(content::BrowserContext* context) { + DCHECK(context); + return ParentProfileData::FromProfile(context) != nullptr; +} + +bool IsSessionProfilePath(const base::FilePath& path) { + return path.DirName().BaseName() == + base::FilePath(FILE_PATH_LITERAL("session_profiles")); +} + +bool IsTorProfilePath(const base::FilePath& path) { +#if BUILDFLAG(ENABLE_TOR) + return IsSessionProfilePath(path) && + path.BaseName() == base::FilePath(tor::kTorProfileDir); #else return false; #endif } -bool IsGuestProfile(Profile* profile) { - DCHECK(profile); - return profile->GetOriginalProfile()->IsGuestSession() && - !IsTorProfile(profile); +bool IsTorProfile(content::BrowserContext* context) { +#if BUILDFLAG(ENABLE_TOR) + DCHECK(context); + return IsTorProfilePath( + Profile::FromBrowserContext(context)->GetOriginalProfile()->GetPath()); +#else + return false; +#endif +} + +Profile* GetParentProfile(content::BrowserContext* context) { + DCHECK(context); + return ParentProfileData::FromProfile(context)->profile(); +} + +Profile* GetParentProfile(const base::FilePath& path) { + return ParentProfileData::FromPath(path)->profile(); +} + +bool IsGuestProfile(content::BrowserContext* context) { + DCHECK(context); + return Profile::FromBrowserContext(context)->GetOriginalProfile()->IsGuestSession(); } } // namespace brave + +namespace chrome { + +// Get the correct profile for keyed services that use +// GetBrowserContextRedirectedInIncognito or equivalent +content::BrowserContext* GetBrowserContextRedirectedInIncognitoOverride( + content::BrowserContext* context) { + if (brave::IsSessionProfile(context)) \ + context = brave::GetParentProfile(context); + return chrome::GetBrowserContextRedirectedInIncognito(context); +} + +} // namespace chrome diff --git a/browser/profiles/profile_util.h b/browser/profiles/profile_util.h index 4142b7a16b74..0352c8d6b775 100644 --- a/browser/profiles/profile_util.h +++ b/browser/profiles/profile_util.h @@ -6,11 +6,33 @@ #ifndef BRAVE_BROWSER_PROFILES_PROFILE_UTIL_H_ #define BRAVE_BROWSER_PROFILES_PROFILE_UTIL_H_ -class Profile; +#include "base/files/file_path.h" +#include "base/macros.h" +#include "base/supports_user_data.h" +// this needs to be here so we don't accidentally override the real method +#include "chrome/browser/profiles/incognito_helpers.h" +#include "chrome/browser/profiles/profile.h" +#include "content/public/browser/browser_context.h" namespace brave { -bool IsTorProfile(const Profile* profile); +Profile* CreateParentProfileData(content::BrowserContext* context); + +base::FilePath GetParentProfilePath(content::BrowserContext* context); + +base::FilePath GetParentProfilePath(const base::FilePath& file_path); + +bool IsSessionProfile(content::BrowserContext* context); + +bool IsSessionProfilePath(const base::FilePath& path); + +bool IsTorProfilePath(const base::FilePath& path); + +bool IsTorProfile(content::BrowserContext* context); + +Profile* GetParentProfile(content::BrowserContext* context); + +Profile* GetParentProfile(const base::FilePath& path); // This function is similar to Profile::IsGuestSession. It is used to get // around the bug(?) in OffTheRecordProfileImpl::Init() code that calls @@ -19,8 +41,26 @@ bool IsTorProfile(const Profile* profile); // this, in search_engine_provider_service_factory.cc (for example), in // InitializeSearchEngineProviderServiceIfNeeded we can't correctly identify // the guest profile by calling IsGuestSession and have to use this function. -bool IsGuestProfile(Profile* profile); +bool IsGuestProfile(content::BrowserContext* profile); } // namespace brave +namespace chrome { + +// Get the correct profile for keyed services that use +// GetBrowserContextRedirectedInIncognito or equivalent +content::BrowserContext* GetBrowserContextRedirectedInIncognitoOverride( + content::BrowserContext* context); + +} // namespace chrome + +// Get the correct profile for keyed services that do NOT use +// GetBrowserContextRedirectedInIncognito or equivalent +#define BRAVE_GET_BROWSER_CONTEXT_TO_USE_WITHOUT_REDIRECT \ + if (brave::IsSessionProfile(context)) { \ + auto* parent = brave::GetParentProfile(context); \ + context = context->IsOffTheRecord() ? parent->GetOffTheRecordProfile() \ + : parent; \ + } + #endif // BRAVE_BROWSER_PROFILES_PROFILE_UTIL_H_ diff --git a/chromium_src/chrome/browser/profiles/profile.cc b/chromium_src/chrome/browser/profiles/profile.cc deleted file mode 100644 index 69dda1260877..000000000000 --- a/chromium_src/chrome/browser/profiles/profile.cc +++ /dev/null @@ -1,7 +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/profiles/profile_util.h" -#include "../../../../../../chrome/browser/profiles/profile.cc" // NOLINT diff --git a/chromium_src/chrome/browser/profiles/profile_impl.cc b/chromium_src/chrome/browser/profiles/profile_impl.cc index e33228146e76..e0bec7788bed 100644 --- a/chromium_src/chrome/browser/profiles/profile_impl.cc +++ b/chromium_src/chrome/browser/profiles/profile_impl.cc @@ -1,2 +1,8 @@ +/* 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/profiles/brave_profile_impl.h" #define PermissionManagerFactory BravePermissionManagerFactory -#include "../../../../../../chrome/browser/profiles/profile_impl.cc" +#include "../../../../../chrome/browser/profiles/profile_impl.cc" diff --git a/chromium_src/chrome/browser/profiles/profile_impl.h b/chromium_src/chrome/browser/profiles/profile_impl.h new file mode 100644 index 000000000000..dadb9ce645cd --- /dev/null +++ b/chromium_src/chrome/browser/profiles/profile_impl.h @@ -0,0 +1,20 @@ +/* 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_CHROMIUM_SRC_CHROME_BROWSER_PROFILES_PROFILE_IMPL_H_ +#define BRAVE_CHROMIUM_SRC_CHROME_BROWSER_PROFILES_PROFILE_IMPL_H_ + +#define BRAVE_PROFILE_IMPL_H_ \ + private: \ + friend class BraveProfileImpl; \ + \ + public: +// define BRAVE_PROFILE_IMPL_H_ + +#include "../../../../../chrome/browser/profiles/profile_impl.h" + +#undef BRAVE_PROFILE_IMPL_H_ + +#endif // BRAVE_CHROMIUM_SRC_CHROME_BROWSER_PROFILES_PROFILE_IMPL_H_ diff --git a/chromium_src/chrome/browser/profiles/profile_window.cc b/chromium_src/chrome/browser/profiles/profile_window.cc index 1379f8b1b195..2263cda5492d 100644 --- a/chromium_src/chrome/browser/profiles/profile_window.cc +++ b/chromium_src/chrome/browser/profiles/profile_window.cc @@ -32,6 +32,17 @@ void CreateAndSwitchToNewProfile(ProfileManager::CreateCallback callback, ProfileMetrics::LogProfileAddNewUser(metric); } +void OpenBrowserWindowForTorProfile(ProfileManager::CreateCallback callback, + bool always_create, + bool is_new_profile, + bool unblock_extensions, + Profile* profile, + Profile::CreateStatus status) { + profiles::OpenBrowserWindowForProfile( + callback, always_create, is_new_profile, unblock_extensions, + profile->GetOffTheRecordProfile(), status); +} + #if !defined(OS_ANDROID) void SwitchToTorProfile(ProfileManager::CreateCallback callback) { const base::FilePath& path = BraveProfileManager::GetTorProfilePath(); @@ -40,13 +51,15 @@ void SwitchToTorProfile(ProfileManager::CreateCallback callback) { // g_browser_process->profile_manager(), // path); g_browser_process->profile_manager()->CreateProfileAsync( - path, base::Bind(&profiles::OpenBrowserWindowForProfile, - callback, false, false, false), + path, + base::Bind(&profiles::OpenBrowserWindowForTorProfile, callback, false, + false, false), base::string16(), std::string()); } #endif void CloseTorProfileWindows() { + // TODO(bridiver) - use GetLoadedProfiles and check IsTorProfile ProfileManager* profile_manager = g_browser_process->profile_manager(); Profile* profile = profile_manager->GetProfileByPath( BraveProfileManager::GetTorProfilePath()); diff --git a/chromium_src/chrome/browser/profiles/profiles_state.cc b/chromium_src/chrome/browser/profiles/profiles_state.cc index c796d2432cb2..6d1c5766794a 100644 --- a/chromium_src/chrome/browser/profiles/profiles_state.cc +++ b/chromium_src/chrome/browser/profiles/profiles_state.cc @@ -1,19 +1,22 @@ +/* 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/profiles/brave_profile_manager.h" -#include "chrome/browser/profiles/profiles_state.h" +#include "brave/browser/profiles/profile_util.h" #include "ui/gfx/text_elider.h" -#define SetActiveProfileToGuestIfLocked SetActiveProfileToGuestIfLocked_ChromiumImpl #define GetAvatarButtonTextForProfile GetAvatarButtonTextForProfile_ChromiumImpl #define GetAvatarNameForProfile GetAvatarNameForProfile_ChromiumImpl #include "../../../../../../chrome/browser/profiles/profiles_state.cc" #undef GetAvatarNameForProfile #undef GetAvatarButtonTextForProfile -#undef SetActiveProfileToGuestIfLocked namespace profiles { #if !defined(OS_ANDROID) base::string16 GetAvatarNameForProfile(const base::FilePath& profile_path) { - if (profile_path == BraveProfileManager::GetTorProfilePath()) + if (brave::IsTorProfilePath(profile_path)) return l10n_util::GetStringUTF16(IDS_TOR_PROFILE_NAME); return GetAvatarNameForProfile_ChromiumImpl(profile_path); } @@ -31,18 +34,6 @@ base::string16 GetAvatarButtonTextForProfile(Profile* profile) { } return name; } - -bool SetActiveProfileToGuestIfLocked() { - ProfileManager* profile_manager = g_browser_process->profile_manager(); - - const base::FilePath& active_profile_path = - profile_manager->GetLastUsedProfileDir(profile_manager->user_data_dir()); - const base::FilePath& tor_path = BraveProfileManager::GetTorProfilePath(); - if (active_profile_path == tor_path) - return true; - - return SetActiveProfileToGuestIfLocked_ChromiumImpl(); -} #endif // !defined(OS_CHROMEOS) #endif // !defined(OS_ANDROID) diff --git a/patches/chrome-browser-profiles-profile.cc.patch b/patches/chrome-browser-profiles-profile.cc.patch index 92b1243ba5de..ab41b829fd2b 100644 --- a/patches/chrome-browser-profiles-profile.cc.patch +++ b/patches/chrome-browser-profiles-profile.cc.patch @@ -1,5 +1,5 @@ diff --git a/chrome/browser/profiles/profile.cc b/chrome/browser/profiles/profile.cc -index 28d3c597acc9e58bab439badb1d1dc9baa139048..4e4a3742f9133dcba49a2ec68a1181a345c5f63b 100644 +index 28d3c597acc9e58bab439badb1d1dc9baa139048..a23013c78421e07a96a2d0bab2935a76fde35ea9 100644 --- a/chrome/browser/profiles/profile.cc +++ b/chrome/browser/profiles/profile.cc @@ -140,7 +140,7 @@ const char Profile::kProfileKey[] = "__PROFILE__"; @@ -11,12 +11,3 @@ index 28d3c597acc9e58bab439badb1d1dc9baa139048..4e4a3742f9133dcba49a2ec68a1181a3 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF); #if defined(OS_ANDROID) registry->RegisterStringPref( -@@ -257,7 +257,7 @@ bool Profile::IsGuestSession() const { - chromeos::switches::kGuestSession); - return is_guest_session; - #else -- return is_guest_profile_; -+ return is_guest_profile_ || brave::IsTorProfile(this); - #endif - } - diff --git a/patches/chrome-browser-profiles-profile_impl.cc.patch b/patches/chrome-browser-profiles-profile_impl.cc.patch index 596d6c8df28b..ba0a380eab60 100644 --- a/patches/chrome-browser-profiles-profile_impl.cc.patch +++ b/patches/chrome-browser-profiles-profile_impl.cc.patch @@ -1,12 +1,13 @@ diff --git a/chrome/browser/profiles/profile_impl.cc b/chrome/browser/profiles/profile_impl.cc -index 084648e9606f9847ff722901fb95626677a17c1e..0bcb822c794146ceec8c52c01da8c973fa25b808 100644 +index 084648e9606f9847ff722901fb95626677a17c1e..80e5b28e1df1076b18ded2bed182a13e1092d47c 100644 --- a/chrome/browser/profiles/profile_impl.cc +++ b/chrome/browser/profiles/profile_impl.cc -@@ -34,6 +34,7 @@ - #include "base/threading/scoped_blocking_call.h" - #include "base/trace_event/trace_event.h" - #include "base/version.h" -+#include "brave/browser/brave_profile_prefs.h" - #include "build/build_config.h" - #include "chrome/browser/background/background_contents_service_factory.h" - #include "chrome/browser/background_fetch/background_fetch_delegate_factory.h" +@@ -365,7 +365,7 @@ std::unique_ptr Profile::CreateProfile(const base::FilePath& path, + } + + std::unique_ptr profile = base::WrapUnique( +- new ProfileImpl(path, delegate, create_mode, io_task_runner)); ++ new BraveProfileImpl(path, delegate, create_mode, io_task_runner)); + #if BUILDFLAG(ENABLE_SUPERVISED_USERS) && !defined(OS_ANDROID) && \ + !defined(OS_CHROMEOS) + if (create_mode == CREATE_MODE_SYNCHRONOUS && profile->IsLegacySupervised()) diff --git a/patches/chrome-browser-profiles-profile_impl.h.patch b/patches/chrome-browser-profiles-profile_impl.h.patch new file mode 100644 index 000000000000..c50c09cf388d --- /dev/null +++ b/patches/chrome-browser-profiles-profile_impl.h.patch @@ -0,0 +1,12 @@ +diff --git a/chrome/browser/profiles/profile_impl.h b/chrome/browser/profiles/profile_impl.h +index e33619d098783950b33bc970c07d197340bd565e..57eeba6884964250a89b8bd1c20fcfddaa53f7a6 100644 +--- a/chrome/browser/profiles/profile_impl.h ++++ b/chrome/browser/profiles/profile_impl.h +@@ -166,6 +166,7 @@ class ProfileImpl : public Profile { + void OnLogin() override; + void InitChromeOSPreferences() override; + #endif // defined(OS_CHROMEOS) ++ BRAVE_PROFILE_IMPL_H_ + + private: + #if defined(OS_CHROMEOS) diff --git a/patches/chrome-browser-profiles-profile_manager.cc.patch b/patches/chrome-browser-profiles-profile_manager.cc.patch deleted file mode 100644 index 7ae2c1dc431e..000000000000 --- a/patches/chrome-browser-profiles-profile_manager.cc.patch +++ /dev/null @@ -1,40 +0,0 @@ -diff --git a/chrome/browser/profiles/profile_manager.cc b/chrome/browser/profiles/profile_manager.cc -index bd2715738d89bc9a099d3be0614f720303b98086..0cd794e5c9b6d5dea803cc00b899524ba2ac75e6 100644 ---- a/chrome/browser/profiles/profile_manager.cc -+++ b/chrome/browser/profiles/profile_manager.cc -@@ -1419,9 +1419,11 @@ void ProfileManager::EnsureActiveProfileExistsBeforeDeletion( - const base::FilePath last_used_profile_path = - GetLastUsedProfileDir(user_data_dir_); - const base::FilePath guest_profile_path = GetGuestProfilePath(); -+ const base::FilePath tor_profile_path = BraveProfileManager::GetTorProfilePath(); - Profile* last_used_profile = GetProfileByPath(last_used_profile_path); - if (last_used_profile_path != profile_dir && - last_used_profile_path != guest_profile_path && -+ last_used_profile_path != tor_profile_path && - last_used_profile != nullptr && - !last_used_profile->IsLegacySupervised()) { - FinishDeletingProfile(profile_dir, last_used_profile_path); -@@ -1434,6 +1436,7 @@ void ProfileManager::EnsureActiveProfileExistsBeforeDeletion( - base::FilePath cur_path = profile->GetPath(); - if (cur_path != profile_dir && - cur_path != guest_profile_path && -+ cur_path != tor_profile_path && - !profile->IsLegacySupervised() && - !IsProfileDirectoryMarkedForDeletion(cur_path)) { - OnNewActiveProfileLoaded(profile_dir, cur_path, std::move(callback), -@@ -1453,6 +1456,7 @@ void ProfileManager::EnsureActiveProfileExistsBeforeDeletion( - // legacy-supervised. - if (cur_path != profile_dir && - cur_path != guest_profile_path && -+ cur_path != tor_profile_path && - !entry->IsLegacySupervised() && - !IsProfileDirectoryMarkedForDeletion(cur_path)) { - fallback_profile_path = cur_path; -@@ -1867,6 +1871,7 @@ void ProfileManager::ScheduleForcedEphemeralProfileForDeletion( - base::FilePath entry_path = entry->GetPath(); - if (entry_path == profile_dir || - entry_path == GetGuestProfilePath() || -+ entry_path == BraveProfileManager::GetTorProfilePath() || - entry->IsLegacySupervised() || - IsProfileDirectoryMarkedForDeletion(entry_path)) - continue; From 0d490ca9c7976078e743caa3d4b733d56c87750a Mon Sep 17 00:00:00 2001 From: Jocelyn Liu Date: Tue, 1 Oct 2019 15:30:41 -0700 Subject: [PATCH 03/16] Share bookmarks between Tor profile and its parent profile. --- .../chrome/browser/bookmarks/bookmark_model_factory.cc | 9 +++++++++ .../bookmarks/managed_bookmark_service_factory.cc | 9 +++++++++ 2 files changed, 18 insertions(+) create mode 100644 chromium_src/chrome/browser/bookmarks/bookmark_model_factory.cc create mode 100644 chromium_src/chrome/browser/bookmarks/managed_bookmark_service_factory.cc diff --git a/chromium_src/chrome/browser/bookmarks/bookmark_model_factory.cc b/chromium_src/chrome/browser/bookmarks/bookmark_model_factory.cc new file mode 100644 index 000000000000..f72a71fc1122 --- /dev/null +++ b/chromium_src/chrome/browser/bookmarks/bookmark_model_factory.cc @@ -0,0 +1,9 @@ +/* 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/profiles/profile_util.h" + +#define GetBrowserContextRedirectedInIncognito GetBrowserContextRedirectedInIncognitoOverride +#include "../../../../../chrome/browser/bookmarks/bookmark_model_factory.cc" diff --git a/chromium_src/chrome/browser/bookmarks/managed_bookmark_service_factory.cc b/chromium_src/chrome/browser/bookmarks/managed_bookmark_service_factory.cc new file mode 100644 index 000000000000..1050fa834455 --- /dev/null +++ b/chromium_src/chrome/browser/bookmarks/managed_bookmark_service_factory.cc @@ -0,0 +1,9 @@ +/* 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/profiles/profile_util.h" + +#define GetBrowserContextRedirectedInIncognito GetBrowserContextRedirectedInIncognitoOverride +#include "../../../../../chrome/browser/bookmarks/managed_bookmark_service_factory.cc" From cb6d1f0b172f788a9a035d214300411de6093644 Mon Sep 17 00:00:00 2001 From: Jocelyn Liu Date: Tue, 1 Oct 2019 15:37:50 -0700 Subject: [PATCH 04/16] Share prefs between Tor profile and its parent profile. --- .../host_content_settings_map_factory.cc | 9 +++ .../profiles/pref_service_builder_utils.cc | 62 ++++++++++++++++--- ...upervised_user_settings_service_factory.cc | 16 +++++ ...host_content_settings_map_factory.cc.patch | 12 ++++ ...sed_user_settings_service_factory.cc.patch | 12 ++++ 5 files changed, 101 insertions(+), 10 deletions(-) create mode 100644 chromium_src/chrome/browser/content_settings/host_content_settings_map_factory.cc create mode 100644 chromium_src/chrome/browser/supervised_user/supervised_user_settings_service_factory.cc create mode 100644 patches/chrome-browser-content_settings-host_content_settings_map_factory.cc.patch create mode 100644 patches/chrome-browser-supervised_user-supervised_user_settings_service_factory.cc.patch diff --git a/chromium_src/chrome/browser/content_settings/host_content_settings_map_factory.cc b/chromium_src/chrome/browser/content_settings/host_content_settings_map_factory.cc new file mode 100644 index 000000000000..10fe97457e04 --- /dev/null +++ b/chromium_src/chrome/browser/content_settings/host_content_settings_map_factory.cc @@ -0,0 +1,9 @@ +/* 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/profiles/profile_util.h" + +#define BRAVE_BUILD_SERVICE_INSTANCE_FOR brave::IsSessionProfile(profile) || +#include "../../../../../chrome/browser/content_settings/host_content_settings_map_factory.cc" 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 b4ffd6dd94c0..a123da729e11 100644 --- a/chromium_src/chrome/browser/profiles/pref_service_builder_utils.cc +++ b/chromium_src/chrome/browser/profiles/pref_service_builder_utils.cc @@ -5,22 +5,31 @@ #include "chrome/browser/profiles/pref_service_builder_utils.h" -#define RegisterProfilePrefs RegisterProfilePrefs_ChromiumImpl -#include "../../../../../chrome/browser/profiles/pref_service_builder_utils.cc" -#undef RegisterProfilePrefs - #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/themes/brave_theme_service.h" #include "brave/browser/tor/buildflags.h" #include "brave/common/pref_names.h" #include "brave/components/brave_rewards/browser/rewards_service.h" +#include "chrome/browser/browser_process.h" +#include "chrome/browser/prefs/pref_service_syncable_util.h" +#include "chrome/browser/profiles/profile.h" +#include "chrome/browser/profiles/profile_manager.h" #include "chrome/common/pref_names.h" #include "components/spellcheck/browser/pref_names.h" -#if BUILDFLAG(ENABLE_TOR) -#include "brave/browser/tor/tor_profile_service.h" +#if BUILDFLAG(ENABLE_EXTENSIONS) +#include "extensions/browser/extension_pref_store.h" +#include "extensions/browser/extension_pref_value_map_factory.h" #endif +#define CreatePrefService CreatePrefService_ChromiumImpl +#define RegisterProfilePrefs RegisterProfilePrefs_ChromiumImpl +#include "../../../../../chrome/browser/profiles/pref_service_builder_utils.cc" +#undef CreatePrefService +#undef RegisterProfilePrefs + // Prefs for KeyedService void RegisterProfilePrefs(bool is_signin_profile, const std::string& locale, @@ -32,10 +41,6 @@ void RegisterProfilePrefs(bool is_signin_profile, BraveThemeService::RegisterProfilePrefs(registry); #endif -#if BUILDFLAG(ENABLE_TOR) - tor::TorProfileService::RegisterProfilePrefs(registry); -#endif - brave_rewards::RewardsService::RegisterProfilePrefs(registry); // Disable spell check service @@ -53,3 +58,40 @@ void RegisterProfilePrefs(bool is_signin_profile, registry->SetDefaultPrefValue(prefs::kUsesSystemTheme, base::Value(false)); #endif } + +std::unique_ptr CreatePrefService( + scoped_refptr pref_registry, + PrefStore* extension_pref_store, + policy::PolicyService* policy_service, + policy::ChromeBrowserPolicyConnector* browser_policy_connector, + prefs::mojom::TrackedPreferenceValidationDelegatePtr + pref_validation_delegate, + scoped_refptr io_task_runner, + SimpleFactoryKey* key, + const base::FilePath& path, + bool async_prefs) { + // Create prefs using the same approach that chromium used when creating an + // off-the-record profile from its original profile. + if (brave::IsSessionProfilePath(path)) { + base::FilePath original_path = brave::GetParentProfilePath(path); + Profile* original_profile = + g_browser_process->profile_manager()->GetProfileByPath(original_path); + DCHECK(original_profile); + PrefStore* extension_pref_store; +#if BUILDFLAG(ENABLE_EXTENSIONS) + extension_pref_store = new ExtensionPrefStore( + ExtensionPrefValueMapFactory::GetForBrowserContext(original_profile), + true); +#endif + return CreateIncognitoPrefServiceSyncable( + PrefServiceSyncableFromProfile(original_profile), + extension_pref_store, + InProcessPrefServiceFactoryFactory::GetInstanceForKey(key) + ->CreateDelegate()); + } + + return CreatePrefService_ChromiumImpl( + pref_registry, extension_pref_store, policy_service, + browser_policy_connector, std::move(pref_validation_delegate), + io_task_runner, key, path, async_prefs); +} diff --git a/chromium_src/chrome/browser/supervised_user/supervised_user_settings_service_factory.cc b/chromium_src/chrome/browser/supervised_user/supervised_user_settings_service_factory.cc new file mode 100644 index 000000000000..a28f49129450 --- /dev/null +++ b/chromium_src/chrome/browser/supervised_user/supervised_user_settings_service_factory.cc @@ -0,0 +1,16 @@ +/* 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 https://mozilla.org/MPL/2.0/. */ + +#include "brave/browser/profiles/profile_util.h" + +// Use the same SupervisedUserSettingsService, which handles a part of +// preferences, as its parent profile. Chromium's incognito profile also shares +// it with its original profile. +#define BRAVE_GET_KEY_TO_USE \ + if (brave::IsSessionProfilePath(key->GetPath())) { \ + return brave::GetParentProfile(key->GetPath())->GetProfileKey(); \ + } + +#include "../../../../../chrome/browser/supervised_user/supervised_user_settings_service_factory.cc" diff --git a/patches/chrome-browser-content_settings-host_content_settings_map_factory.cc.patch b/patches/chrome-browser-content_settings-host_content_settings_map_factory.cc.patch new file mode 100644 index 000000000000..c6a4ed3e441a --- /dev/null +++ b/patches/chrome-browser-content_settings-host_content_settings_map_factory.cc.patch @@ -0,0 +1,12 @@ +diff --git a/chrome/browser/content_settings/host_content_settings_map_factory.cc b/chrome/browser/content_settings/host_content_settings_map_factory.cc +index a3797e6705b879e917c33bc907ce6482c5c10c68..0d2c8792432e1dfeb80511d9ff54d8df9cd06519 100644 +--- a/chrome/browser/content_settings/host_content_settings_map_factory.cc ++++ b/chrome/browser/content_settings/host_content_settings_map_factory.cc +@@ -80,6 +80,7 @@ scoped_refptr + + scoped_refptr settings_map(new HostContentSettingsMap( + profile->GetPrefs(), ++ BRAVE_BUILD_SERVICE_INSTANCE_FOR + profile->IsIncognitoProfile() || profile->IsGuestSession(), + /*store_last_modified=*/true, + base::FeatureList::IsEnabled(features::kPermissionDelegation))); diff --git a/patches/chrome-browser-supervised_user-supervised_user_settings_service_factory.cc.patch b/patches/chrome-browser-supervised_user-supervised_user_settings_service_factory.cc.patch new file mode 100644 index 000000000000..ed1e7e56e1a4 --- /dev/null +++ b/patches/chrome-browser-supervised_user-supervised_user_settings_service_factory.cc.patch @@ -0,0 +1,12 @@ +diff --git a/chrome/browser/supervised_user/supervised_user_settings_service_factory.cc b/chrome/browser/supervised_user/supervised_user_settings_service_factory.cc +index 5f85a90f2f2800c00bf0367c1de83ff36d8b8fe7..9ab88b8a25b36afdbe3016fd20cc29343395e634 100644 +--- a/chrome/browser/supervised_user/supervised_user_settings_service_factory.cc ++++ b/chrome/browser/supervised_user/supervised_user_settings_service_factory.cc +@@ -36,6 +36,7 @@ SupervisedUserSettingsServiceFactory::BuildServiceInstanceFor( + + SimpleFactoryKey* SupervisedUserSettingsServiceFactory::GetKeyToUse( + SimpleFactoryKey* key) const { ++ BRAVE_GET_KEY_TO_USE + ProfileKey* profile_key = ProfileKey::FromSimpleFactoryKey(key); + return profile_key->GetOriginalKey(); + } From 56677f73ac2688969099b8492bf18a27682f9765 Mon Sep 17 00:00:00 2001 From: Jocelyn Liu Date: Tue, 1 Oct 2019 15:29:52 -0700 Subject: [PATCH 05/16] Don't open non-incognito windows in Tor --- .../chrome/browser/ui/browser_navigator.cc | 44 ++++++++++++++++++- ...rome-browser-ui-browser_navigator.cc.patch | 18 ++++---- 2 files changed, 51 insertions(+), 11 deletions(-) diff --git a/chromium_src/chrome/browser/ui/browser_navigator.cc b/chromium_src/chrome/browser/ui/browser_navigator.cc index e75f53870f98..06e12bc19cca 100644 --- a/chromium_src/chrome/browser/ui/browser_navigator.cc +++ b/chromium_src/chrome/browser/ui/browser_navigator.cc @@ -3,14 +3,33 @@ * 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/profiles/profile_util.h" #include "brave/common/webui_url_constants.h" +#include "chrome/browser/ui/browser.h" +#include "chrome/browser/ui/browser_finder.h" #include "chrome/browser/ui/browser_navigator_params.h" #include "chrome/common/webui_url_constants.h" #include "url/gurl.h" namespace { -void AdjustNavigateParamsForURLBraveImpl(NavigateParams* params) { +bool HandleURLInParent(NavigateParams* params, Profile* profile) { + if (brave::IsTorProfile(profile) && + !params->browser->profile()->IsOffTheRecord()) { + return true; + } + + return false; +} + +// GetOrCreateBrowser is not accessible here +Browser* BraveGetOrCreateBrowser(Profile* profile, bool user_gesture) { + Browser* browser = chrome::FindTabbedBrowser(profile, false); + return browser ? browser + : new Browser(Browser::CreateParams(profile, user_gesture)); +} + +void UpdateBraveScheme(NavigateParams* params) { if (params->url.SchemeIs(content::kBraveUIScheme)) { GURL::Replacements replacements; replacements.SetSchemeStr(content::kChromeUIScheme); @@ -18,6 +37,21 @@ void AdjustNavigateParamsForURLBraveImpl(NavigateParams* params) { } } +void MaybeHandleInParent(NavigateParams* params, bool allow_in_incognito) { + auto* profile = params->initiating_profile; + if (brave::IsSessionProfile(profile)) { + if (!allow_in_incognito) { + params->initiating_profile = profile->IsOffTheRecord() + ? brave::GetParentProfile(profile)->GetOffTheRecordProfile() + : brave::GetParentProfile(profile); + } else if (HandleURLInParent(params, profile)) { + params->browser = BraveGetOrCreateBrowser( + brave::GetParentProfile(profile), + params->user_gesture); + } + } +} + bool IsHostAllowedInIncognitoBraveImpl(const base::StringPiece& host) { if (host == kRewardsPageHost || host == kBraveUISyncHost || @@ -30,4 +64,10 @@ bool IsHostAllowedInIncognitoBraveImpl(const base::StringPiece& host) { } // namespace -#include "../../../../chrome/browser/ui/browser_navigator.cc" // NOLINT +#define BRAVE_ADJUST_NAVIGATE_PARAMS_FOR_URL \ + UpdateBraveScheme(params); \ + MaybeHandleInParent(params, \ + IsURLAllowedInIncognito(params->url, params->initiating_profile)); + +#include "../../../../../chrome/browser/ui/browser_navigator.cc" +#undef BRAVE_ADJUST_NAVIGATE_PARAMS_FOR_URL diff --git a/patches/chrome-browser-ui-browser_navigator.cc.patch b/patches/chrome-browser-ui-browser_navigator.cc.patch index 210ad16d69f7..8abaecb1fc72 100644 --- a/patches/chrome-browser-ui-browser_navigator.cc.patch +++ b/patches/chrome-browser-ui-browser_navigator.cc.patch @@ -1,15 +1,15 @@ diff --git a/chrome/browser/ui/browser_navigator.cc b/chrome/browser/ui/browser_navigator.cc -index 441d74eb9c66b5aca7b728e30ef42304ecac67d1..a32b1023f18a9b8138ebe2a580f4a5ea4f699816 100644 +index 441d74eb9c66b5aca7b728e30ef42304ecac67d1..07e3d16f9fb598cf74e4ed7a2af8d8e60bee937e 100644 --- a/chrome/browser/ui/browser_navigator.cc +++ b/chrome/browser/ui/browser_navigator.cc -@@ -106,6 +106,7 @@ Browser* GetOrCreateBrowser(Profile* profile, bool user_gesture) { - // Returns true on success. Otherwise, if changing params leads the browser into - // an erroneous state, returns false. - bool AdjustNavigateParamsForURL(NavigateParams* params) { -+ AdjustNavigateParamsForURLBraveImpl(params); - if (params->contents_to_insert || params->switch_to_singleton_tab || - IsURLAllowedInIncognito(params->url, params->initiating_profile) || - params->initiating_profile->IsGuestSession()) { +@@ -459,6 +459,7 @@ void Navigate(NavigateParams* params) { + params->initiating_profile = source_browser->profile(); + DCHECK(params->initiating_profile); + ++ BRAVE_ADJUST_NAVIGATE_PARAMS_FOR_URL + if (!AdjustNavigateParamsForURL(params)) + return; + @@ -730,6 +731,7 @@ void Navigate(NavigateParams* params) { bool IsHostAllowedInIncognito(const GURL& url) { std::string scheme = url.scheme(); From d79806de152544df85419256f56ebce3abd418c2 Mon Sep 17 00:00:00 2001 From: Jocelyn Liu Date: Tue, 1 Oct 2019 15:00:30 -0700 Subject: [PATCH 06/16] Delete Tor legacy profile if exists --- browser/brave_browser_main_parts.cc | 28 ++++++++++++++++++++++++++++ browser/brave_browser_main_parts.h | 1 + 2 files changed, 29 insertions(+) diff --git a/browser/brave_browser_main_parts.cc b/browser/brave_browser_main_parts.cc index 01ff554ccf22..c28eedd4e013 100644 --- a/browser/brave_browser_main_parts.cc +++ b/browser/brave_browser_main_parts.cc @@ -6,6 +6,16 @@ #include "brave/browser/brave_browser_main_parts.h" #include "brave/browser/browsing_data/brave_clear_browsing_data.h" +#include "brave/browser/tor/buildflags.h" + +#if BUILDFLAG(ENABLE_TOR) +#include "base/files/file_util.h" +#include "brave/common/tor/tor_constants.h" +#include "chrome/browser/browser_process.h" +#include "chrome/browser/browser_process_impl.h" +#include "chrome/browser/profiles/profile_manager.h" +#include "chrome/browser/profiles/profile_metrics.h" +#endif #if !defined(OS_ANDROID) #include "brave/browser/infobars/brave_confirm_p3a_infobar_delegate.h" @@ -40,3 +50,21 @@ void BraveBrowserMainParts::PostBrowserStart() { void BraveBrowserMainParts::PreShutdown() { content::BraveClearBrowsingData::ClearOnExit(); } + +void BraveBrowserMainParts::PostBrowserStart() { + ChromeBrowserMainParts::PostBrowserStart(); + +#if BUILDFLAG(ENABLE_TOR) + ProfileManager* profile_manager = g_browser_process->profile_manager(); + base::FilePath tor_legacy_path = + profile_manager->user_data_dir().Append(tor::kTorProfileDir); + + // Delete Tor legacy profile if exists. + if (base::PathExists(tor_legacy_path)) { + profile_manager->MaybeScheduleProfileForDeletion( + tor_legacy_path, + base::DoNothing(), + ProfileMetrics::DELETE_PROFILE_SETTINGS); + } +#endif +} diff --git a/browser/brave_browser_main_parts.h b/browser/brave_browser_main_parts.h index 01a049bebb69..ff739da16708 100644 --- a/browser/brave_browser_main_parts.h +++ b/browser/brave_browser_main_parts.h @@ -16,6 +16,7 @@ class BraveBrowserMainParts : public ChromeBrowserMainParts { void PostBrowserStart() override; void PreShutdown() override; + void PostBrowserStart() override; private: DISALLOW_COPY_AND_ASSIGN(BraveBrowserMainParts); From d3710be7ce860393f577a81530760243b3a382ed Mon Sep 17 00:00:00 2001 From: Jocelyn Liu Date: Tue, 1 Oct 2019 15:08:42 -0700 Subject: [PATCH 07/16] Fix greaselion service usage --- browser/greaselion/greaselion_tab_helper.cc | 7 ++++--- browser/greaselion/greaselion_tab_helper.h | 3 --- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/browser/greaselion/greaselion_tab_helper.cc b/browser/greaselion/greaselion_tab_helper.cc index 3fad7e76e770..023659c7e548 100644 --- a/browser/greaselion/greaselion_tab_helper.cc +++ b/browser/greaselion/greaselion_tab_helper.cc @@ -22,8 +22,6 @@ namespace greaselion { GreaselionTabHelper::GreaselionTabHelper(content::WebContents* web_contents) : WebContentsObserver(web_contents) { - greaselion_service_ = GreaselionServiceFactory::GetForBrowserContext( - web_contents->GetBrowserContext()); download_service_ = g_brave_browser_process->greaselion_download_service(); download_service_->AddObserver(this); } @@ -34,7 +32,10 @@ GreaselionTabHelper::~GreaselionTabHelper() { void GreaselionTabHelper::OnRulesReady( GreaselionDownloadService* download_service) { - greaselion_service_->UpdateInstalledExtensions(); + auto* greaselion_service = GreaselionServiceFactory::GetForBrowserContext( + web_contents()->GetBrowserContext()); + if (greaselion_service) + greaselion_service->UpdateInstalledExtensions(); } WEB_CONTENTS_USER_DATA_KEY_IMPL(GreaselionTabHelper) diff --git a/browser/greaselion/greaselion_tab_helper.h b/browser/greaselion/greaselion_tab_helper.h index 09528c528a60..2c66f46915de 100644 --- a/browser/greaselion/greaselion_tab_helper.h +++ b/browser/greaselion/greaselion_tab_helper.h @@ -16,8 +16,6 @@ class WebContents; namespace greaselion { -class GreaselionService; - class GreaselionTabHelper : public greaselion::GreaselionDownloadService::Observer, public content::WebContentsObserver, @@ -33,7 +31,6 @@ class GreaselionTabHelper void OnRulesReady(GreaselionDownloadService* download_service) override; GreaselionDownloadService* download_service_; // NOT OWNED - GreaselionService* greaselion_service_; // NOT OWNED WEB_CONTENTS_USER_DATA_KEY_DECL(); DISALLOW_COPY_AND_ASSIGN(GreaselionTabHelper); From 67c33415c39de80321f23c656f92f5e378af50f2 Mon Sep 17 00:00:00 2001 From: Jocelyn Liu Date: Tue, 1 Oct 2019 16:44:10 -0700 Subject: [PATCH 08/16] Block extensions in Tor profile. --- browser/profiles/brave_profile_manager.cc | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/browser/profiles/brave_profile_manager.cc b/browser/profiles/brave_profile_manager.cc index 126592d25684..7d629c627d47 100644 --- a/browser/profiles/brave_profile_manager.cc +++ b/browser/profiles/brave_profile_manager.cc @@ -40,8 +40,14 @@ #include "content/public/browser/browser_thread.h" #include "content/public/browser/url_data_source.h" #include "content/public/common/webrtc_ip_handling_policy.h" +#include "extensions/buildflags/buildflags.h" #include "ui/base/l10n/l10n_util.h" +#if BUILDFLAG(ENABLE_EXTENSIONS) +#include "chrome/browser/extensions/extension_service.h" +#include "extensions/browser/extension_system.h" +#endif + using content::BrowserThread; BraveProfileManager::BraveProfileManager(const base::FilePath& user_data_dir) @@ -142,6 +148,12 @@ void BraveProfileManager::OnProfileCreated(Profile* profile, entry->SetIsEphemeral(true); } +#if BUILDFLAG(ENABLE_EXTENSIONS) + extensions::ExtensionService* extension_service = + extensions::ExtensionSystem::Get(profile)->extension_service(); + extension_service->BlockAllExtensions(); +#endif + // We need to wait until OnProfileCreated to // ensure that the request context is available. TorProfileServiceFactory::GetForProfile(profile); From b6fe6d83bf4eee58ef43de9ef57c73d3ea7672a2 Mon Sep 17 00:00:00 2001 From: Jocelyn Liu Date: Tue, 1 Oct 2019 16:47:08 -0700 Subject: [PATCH 09/16] Create brave's extensions browser client to remove patching chrome_extensions_browser_client.cc --- browser/extensions/BUILD.gn | 2 + .../brave_extensions_browser_client_impl.cc | 21 ++++++++++ .../brave_extensions_browser_client_impl.h | 25 ++++++++++++ .../chrome/browser/browser_process_impl.cc | 10 +++++ .../chrome_extensions_browser_client.cc | 8 ++-- extensions/browser/BUILD.gn | 12 ++++++ .../brave_extensions_browser_client.cc | 29 ++++++++++++++ .../browser/brave_extensions_browser_client.h | 39 +++++++++++++++++++ ...-chrome_extensions_browser_client.cc.patch | 27 ------------- patches/extensions-browser-BUILD.gn.patch | 12 ++++++ 10 files changed, 155 insertions(+), 30 deletions(-) create mode 100644 browser/extensions/brave_extensions_browser_client_impl.cc create mode 100644 browser/extensions/brave_extensions_browser_client_impl.h create mode 100644 chromium_src/chrome/browser/browser_process_impl.cc create mode 100644 extensions/browser/BUILD.gn create mode 100644 extensions/browser/brave_extensions_browser_client.cc create mode 100644 extensions/browser/brave_extensions_browser_client.h delete mode 100644 patches/chrome-browser-extensions-chrome_extensions_browser_client.cc.patch create mode 100644 patches/extensions-browser-BUILD.gn.patch diff --git a/browser/extensions/BUILD.gn b/browser/extensions/BUILD.gn index c387a5090fcc..262fa2b26b0f 100644 --- a/browser/extensions/BUILD.gn +++ b/browser/extensions/BUILD.gn @@ -36,6 +36,8 @@ source_set("extensions") { "brave_extension_service.h", "brave_extensions_browser_api_provider.cc", "brave_extensions_browser_api_provider.h", + "brave_extensions_browser_client_impl.cc", + "brave_extensions_browser_client_impl.h", "brave_theme_event_router.cc", "brave_theme_event_router.h", "updater/brave_update_client_config.cc", diff --git a/browser/extensions/brave_extensions_browser_client_impl.cc b/browser/extensions/brave_extensions_browser_client_impl.cc new file mode 100644 index 000000000000..c3f1247d93d2 --- /dev/null +++ b/browser/extensions/brave_extensions_browser_client_impl.cc @@ -0,0 +1,21 @@ +/* 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/extensions/brave_extensions_browser_client_impl.h" + +#include + +#include "brave/browser/extensions/brave_extensions_browser_api_provider.h" +#include "brave/browser/profiles/profile_util.h" +#include "components/prefs/pref_service.h" + +namespace extensions { + +BraveExtensionsBrowserClientImpl::BraveExtensionsBrowserClientImpl() { + BraveExtensionsBrowserClient::Set(this); + AddAPIProvider(std::make_unique()); +} + +} // namespace extensions diff --git a/browser/extensions/brave_extensions_browser_client_impl.h b/browser/extensions/brave_extensions_browser_client_impl.h new file mode 100644 index 000000000000..4d872d2a2781 --- /dev/null +++ b/browser/extensions/brave_extensions_browser_client_impl.h @@ -0,0 +1,25 @@ +/* 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_BROWSER_EXTENSIONS_BRAVE_EXTENSIONS_BROWSER_CLIENT_IMPL_H_ +#define BRAVE_BROWSER_EXTENSIONS_BRAVE_EXTENSIONS_BROWSER_CLIENT_IMPL_H_ + +#include "brave/extensions/browser/brave_extensions_browser_client.h" +#include "chrome/browser/extensions/chrome_extensions_browser_client.h" + +namespace extensions { + +class BraveExtensionsBrowserClientImpl : public ChromeExtensionsBrowserClient, + public BraveExtensionsBrowserClient { + public: + BraveExtensionsBrowserClientImpl(); + ~BraveExtensionsBrowserClientImpl() override = default; + + DISALLOW_COPY_AND_ASSIGN(BraveExtensionsBrowserClientImpl); +}; + +} // namespace extensions + +#endif // BRAVE_BROWSER_EXTENSIONS_BRAVE_EXTENSIONS_BROWSER_CLIENT_IMPL_H_ diff --git a/chromium_src/chrome/browser/browser_process_impl.cc b/chromium_src/chrome/browser/browser_process_impl.cc new file mode 100644 index 000000000000..e8a4d5663550 --- /dev/null +++ b/chromium_src/chrome/browser/browser_process_impl.cc @@ -0,0 +1,10 @@ +/* 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/extensions/brave_extensions_browser_client_impl.h" + +#define ChromeExtensionsBrowserClient BraveExtensionsBrowserClientImpl +#include "../../../../chrome/browser/browser_process_impl.cc" +#undef ChromeExtensionsBrowserClient diff --git a/chromium_src/chrome/browser/extensions/chrome_extensions_browser_client.cc b/chromium_src/chrome/browser/extensions/chrome_extensions_browser_client.cc index 6523fe7b4919..54f99019c063 100644 --- a/chromium_src/chrome/browser/extensions/chrome_extensions_browser_client.cc +++ b/chromium_src/chrome/browser/extensions/chrome_extensions_browser_client.cc @@ -4,12 +4,14 @@ * You can obtain one at https://mozilla.org/MPL/2.0/. */ #include "brave/browser/extensions/api/brave_extensions_api_client.h" +#include "brave/browser/extensions/brave_component_extension_resource_manager.h" #include "brave/browser/extensions/updater/brave_update_client_config.h" -#include "chrome/browser/extensions/chrome_extensions_browser_client.h" #include "chrome/browser/extensions/updater/chrome_update_client_config.h" -#define ChromeUpdateClientConfig BraveUpdateClientConfig +#define ChromeComponentExtensionResourceManager BraveComponentExtensionResourceManager // NOLINT #define ChromeExtensionsAPIClient BraveExtensionsAPIClient +#define ChromeUpdateClientConfig BraveUpdateClientConfig #include "../../../../../chrome/browser/extensions/chrome_extensions_browser_client.cc" // NOLINT -#undef ChromeUpdateClientConfig +#undef ChromeComponentExtensionResourceManager #undef ChromeExtensionsAPIClient +#undef ChromeUpdateClientConfig diff --git a/extensions/browser/BUILD.gn b/extensions/browser/BUILD.gn new file mode 100644 index 000000000000..271712edad14 --- /dev/null +++ b/extensions/browser/BUILD.gn @@ -0,0 +1,12 @@ +source_set("browser") { + visibility = [ "//extensions/browser:browser_sources" ] + + sources = [ + "brave_extensions_browser_client.cc", + "brave_extensions_browser_client.h", + ] + + deps = [ + "//base", + ] +} diff --git a/extensions/browser/brave_extensions_browser_client.cc b/extensions/browser/brave_extensions_browser_client.cc new file mode 100644 index 000000000000..e028aae0a675 --- /dev/null +++ b/extensions/browser/brave_extensions_browser_client.cc @@ -0,0 +1,29 @@ +/* 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/extensions/browser/brave_extensions_browser_client.h" + +#include + +namespace extensions { + +namespace { + +BraveExtensionsBrowserClient* g_brave_extension_browser_client = NULL; + +} // namespace + +BraveExtensionsBrowserClient::BraveExtensionsBrowserClient() {} +BraveExtensionsBrowserClient::~BraveExtensionsBrowserClient() = default; + +BraveExtensionsBrowserClient* BraveExtensionsBrowserClient::Get() { + return g_brave_extension_browser_client; +} + +void BraveExtensionsBrowserClient::Set(BraveExtensionsBrowserClient* client) { + g_brave_extension_browser_client = client; +} + +} // namespace exceptions diff --git a/extensions/browser/brave_extensions_browser_client.h b/extensions/browser/brave_extensions_browser_client.h new file mode 100644 index 000000000000..b8c64247d31b --- /dev/null +++ b/extensions/browser/brave_extensions_browser_client.h @@ -0,0 +1,39 @@ +/* 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_EXTENSIONS_BROWSER_BRAVE_EXTENSIONS_BROWSER_CLIENT_H_ +#define BRAVE_EXTENSIONS_BROWSER_BRAVE_EXTENSIONS_BROWSER_CLIENT_H_ + +#include + +#include "base/command_line.h" +#include "base/macros.h" +#include "build/build_config.h" + +namespace content { +class BrowserContext; +} + +class PrefService; + +namespace extensions { + +class EarlyExtensionPrefsObserver; + +class BraveExtensionsBrowserClient { + public: + BraveExtensionsBrowserClient(); + virtual ~BraveExtensionsBrowserClient(); + + // Returns the single instance of |this|. + static BraveExtensionsBrowserClient* Get(); + + // Sets and initializes the single instance. + static void Set(BraveExtensionsBrowserClient* client); +}; + +} // namespace extensions + +#endif // BRAVE_EXTENSIONS_BROWSER_BRAVE_EXTENSIONS_BROWSER_CLIENT_H_ diff --git a/patches/chrome-browser-extensions-chrome_extensions_browser_client.cc.patch b/patches/chrome-browser-extensions-chrome_extensions_browser_client.cc.patch deleted file mode 100644 index f66e57e82f01..000000000000 --- a/patches/chrome-browser-extensions-chrome_extensions_browser_client.cc.patch +++ /dev/null @@ -1,27 +0,0 @@ -diff --git a/chrome/browser/extensions/chrome_extensions_browser_client.cc b/chrome/browser/extensions/chrome_extensions_browser_client.cc -index 37972b557b457ffd69215caf8d2616432fe7a3da..9be4fa73fe51417bc5e8ed88517089a6d6f6592c 100644 ---- a/chrome/browser/extensions/chrome_extensions_browser_client.cc -+++ b/chrome/browser/extensions/chrome_extensions_browser_client.cc -@@ -9,6 +9,8 @@ - #include "base/command_line.h" - #include "base/logging.h" - #include "base/version.h" -+#include "brave/browser/extensions/brave_component_extension_resource_manager.h" -+#include "brave/browser/extensions/brave_extensions_browser_api_provider.h" - #include "build/build_config.h" - #include "chrome/browser/app_mode/app_mode_utils.h" - #include "chrome/browser/browser_process.h" -@@ -92,11 +94,12 @@ bool ExtensionsDisabled(const base::CommandLine& command_line) { - ChromeExtensionsBrowserClient::ChromeExtensionsBrowserClient() { - AddAPIProvider(std::make_unique()); - AddAPIProvider(std::make_unique()); -+ AddAPIProvider(std::make_unique()); - - process_manager_delegate_.reset(new ChromeProcessManagerDelegate); - api_client_.reset(new ChromeExtensionsAPIClient); - SetCurrentChannel(chrome::GetChannel()); -- resource_manager_.reset(new ChromeComponentExtensionResourceManager()); -+ resource_manager_.reset(new BraveComponentExtensionResourceManager()); - } - - ChromeExtensionsBrowserClient::~ChromeExtensionsBrowserClient() {} diff --git a/patches/extensions-browser-BUILD.gn.patch b/patches/extensions-browser-BUILD.gn.patch new file mode 100644 index 000000000000..05bad5bf6553 --- /dev/null +++ b/patches/extensions-browser-BUILD.gn.patch @@ -0,0 +1,12 @@ +diff --git a/extensions/browser/BUILD.gn b/extensions/browser/BUILD.gn +index a4f05021eb990e8339244f3d74fe05cb1e1c1ebb..68aaa8e50f8627335685963c036d0c194ae9bfd8 100644 +--- a/extensions/browser/BUILD.gn ++++ b/extensions/browser/BUILD.gn +@@ -406,6 +406,7 @@ jumbo_source_set("browser_sources") { + "//third_party/blink/public/common", + "//ui/display", + ] ++ if (brave_chromium_build) { deps += [ "//brave/extensions/browser"] } + + public_deps = [ + "//content/public/common", From c708ad329629fee27ca8cfe6cdb5e102c8c3e731 Mon Sep 17 00:00:00 2001 From: Jocelyn Liu Date: Tue, 1 Oct 2019 15:07:59 -0700 Subject: [PATCH 10/16] Fixing and adding tests, also removes kProfileUsingTor pref --- browser/extensions/brave_tor_client_updater.h | 5 + .../brave_tor_client_updater_browsertest.cc | 3 + ...ave_wallet_navigation_throttle_unittest.cc | 6 +- .../brave_profile_manager_browsertest.cc | 262 +++++++++++++++++- .../brave_profile_manager_unittest.cc | 91 +++--- .../profiles/tor_unittest_profile_manager.cc | 15 +- ...rch_engine_provider_service_browsertest.cc | 24 +- .../tor/tor_navigation_throttle_unittest.cc | 2 + browser/tor/tor_profile_service.cc | 6 - browser/tor/tor_profile_service.h | 1 - browser/tor/tor_profile_service_impl.cc | 5 + .../chrome/test/base/testing_profile.cc | 14 + common/tor/pref_names.cc | 1 - common/tor/pref_names.h | 1 - .../chrome-test-base-testing_profile.cc.patch | 12 + test/BUILD.gn | 2 + test/base/brave_testing_profile.cc | 16 ++ test/base/brave_testing_profile.h | 17 ++ test/data/trivial_extension/manifest.json | 6 + 19 files changed, 427 insertions(+), 62 deletions(-) create mode 100644 chromium_src/chrome/test/base/testing_profile.cc create mode 100644 patches/chrome-test-base-testing_profile.cc.patch create mode 100644 test/base/brave_testing_profile.cc create mode 100644 test/base/brave_testing_profile.h create mode 100644 test/data/trivial_extension/manifest.json diff --git a/browser/extensions/brave_tor_client_updater.h b/browser/extensions/brave_tor_client_updater.h index 7759518dfed8..e2ef2629676e 100644 --- a/browser/extensions/brave_tor_client_updater.h +++ b/browser/extensions/brave_tor_client_updater.h @@ -16,7 +16,9 @@ #include "brave/components/brave_component_updater/browser/brave_component.h" class BraveProfileManagerTest; +class BraveProfileManagerExtensionTest; class BraveTorClientUpdaterTest; +class SearchEngineProviderServiceTest; using brave_component_updater::BraveComponent; @@ -65,7 +67,10 @@ class BraveTorClientUpdater : public BraveComponent { private: friend class ::BraveProfileManagerTest; + friend class ::BraveProfileManagerExtensionTest; friend class ::BraveTorClientUpdaterTest; + friend class ::SearchEngineProviderServiceTest; + static std::string g_tor_client_component_name_; static std::string g_tor_client_component_id_; static std::string g_tor_client_component_base64_public_key_; diff --git a/browser/extensions/brave_tor_client_updater_browsertest.cc b/browser/extensions/brave_tor_client_updater_browsertest.cc index ce8c544f84a4..239564667c94 100644 --- a/browser/extensions/brave_tor_client_updater_browsertest.cc +++ b/browser/extensions/brave_tor_client_updater_browsertest.cc @@ -12,6 +12,7 @@ #include "brave/browser/extensions/brave_tor_client_updater.h" #include "brave/common/brave_paths.h" #include "chrome/browser/extensions/extension_browsertest.h" +#include "content/public/test/test_utils.h" using extensions::ExtensionBrowserTest; @@ -109,6 +110,7 @@ IN_PROC_BROWSER_TEST_F(BraveTorClientUpdaterTest, TorClientInstalls) { kTorClientUpdaterComponentTestBase64PublicKey); ASSERT_TRUE(InstallTorClientUpdater()); + content::RunAllTasksUntilIdle(); base::FilePath executable_path = g_brave_browser_process->tor_client_updater()->GetExecutablePath(); ASSERT_TRUE(PathExists(executable_path)); @@ -122,6 +124,7 @@ IN_PROC_BROWSER_TEST_F(BraveTorClientUpdaterTest, TorClientLaunches) { kTorClientUpdaterComponentTestBase64PublicKey); ASSERT_TRUE(InstallTorClientUpdater()); + content::RunAllTasksUntilIdle(); base::FilePath executable_path = g_brave_browser_process->tor_client_updater()->GetExecutablePath(); ASSERT_TRUE(PathExists(executable_path)); diff --git a/browser/extensions/brave_wallet_navigation_throttle_unittest.cc b/browser/extensions/brave_wallet_navigation_throttle_unittest.cc index 33862521bb82..2db095dd4c24 100644 --- a/browser/extensions/brave_wallet_navigation_throttle_unittest.cc +++ b/browser/extensions/brave_wallet_navigation_throttle_unittest.cc @@ -10,6 +10,7 @@ #include #include "brave/browser/profiles/brave_profile_manager.h" +#include "brave/browser/profiles/profile_util.h" #include "brave/browser/profiles/tor_unittest_profile_manager.h" #include "brave/browser/tor/buildflags.h" #include "brave/common/extensions/extension_constants.h" @@ -85,9 +86,6 @@ class BraveWalletNavigationThrottleUnitTest TestingProfile::Builder builder; auto prefs = std::make_unique(); -#if BUILDFLAG(ENABLE_TOR) - prefs->registry()->RegisterBooleanPref(tor::prefs::kProfileUsingTor, false); -#endif RegisterUserProfilePrefs(prefs->registry()); builder.SetPrefService(std::move(prefs)); return builder.Build().release(); @@ -209,8 +207,10 @@ TEST_F(BraveWalletNavigationThrottleUnitTest, ChromeWalletDisabledByPref) { TEST_F(BraveWalletNavigationThrottleUnitTest, ChromeWalletNotAvailInTorProfile) { ProfileManager* profile_manager = g_browser_process->profile_manager(); + Profile* profile = ProfileManager::GetLastUsedProfile(); Profile* tor_profile = profile_manager->GetProfile( BraveProfileManager::GetTorProfilePath()); + ASSERT_EQ(brave::GetParentProfile(tor_profile), profile); std::unique_ptr tor_web_contents = content::WebContentsTester::CreateTestWebContents(tor_profile, nullptr); diff --git a/browser/profiles/brave_profile_manager_browsertest.cc b/browser/profiles/brave_profile_manager_browsertest.cc index 0d9588eb40a1..580371d65f28 100644 --- a/browser/profiles/brave_profile_manager_browsertest.cc +++ b/browser/profiles/brave_profile_manager_browsertest.cc @@ -5,17 +5,44 @@ #include +#include "base/path_service.h" #include "base/strings/utf_string_conversions.h" +#include "brave/browser/brave_browser_process_impl.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 "chrome/browser/bookmarks/bookmark_model_factory.h" #include "chrome/browser/browser_process.h" #include "chrome/browser/chrome_notification_types.h" +#include "chrome/browser/content_settings/host_content_settings_map_factory.h" #include "chrome/browser/profiles/profile.h" #include "chrome/browser/profiles/profile_attributes_entry.h" #include "chrome/browser/profiles/profile_attributes_storage.h" #include "chrome/browser/profiles/profile_manager.h" +#include "chrome/browser/profiles/profile_window.h" +#include "chrome/browser/ui/browser_list.h" #include "chrome/test/base/in_process_browser_test.h" +#include "components/bookmarks/browser/bookmark_model.h" +#include "components/bookmarks/common/bookmark_pref_names.h" +#include "components/bookmarks/test/bookmark_test_helpers.h" +#include "components/content_settings/core/browser/host_content_settings_map.h" +#include "components/content_settings/core/common/content_settings.h" +#include "components/content_settings/core/common/content_settings_types.h" +#include "components/prefs/pref_service.h" #include "content/public/test/test_utils.h" -using BraveProfileManagerTest = InProcessBrowserTest; +#if BUILDFLAG(ENABLE_TOR) +#include "brave/browser/tor/tor_launcher_factory.h" +#endif + +#if BUILDFLAG(ENABLE_EXTENSIONS) +#include "chrome/browser/extensions/extension_browsertest.h" +#include "extensions/browser/extension_prefs.h" +#include "extensions/browser/extension_registry.h" +#include "extensions/browser/extension_util.h" +#include "extensions/common/extension_id.h" +#endif namespace { @@ -57,8 +84,50 @@ std::vector GetTestProfileData( return profile_data; } +#if BUILDFLAG(ENABLE_TOR) +Profile* SwitchToTorProfile() { + base::RunLoop run_loop; + profiles::SwitchToTorProfile( + base::Bind(&OnUnblockOnProfileCreation, &run_loop)); + run_loop.Run(); + + BrowserList* browser_list = BrowserList::GetInstance(); + EXPECT_EQ(2U, browser_list->size()); + return browser_list->get(1)->profile(); +} +#endif + } // namespace +class BraveProfileManagerTest : public InProcessBrowserTest { + public: + void SetUpOnMainThread() override { + InProcessBrowserTest::SetUpOnMainThread(); +#if BUILDFLAG(ENABLE_TOR) + g_brave_browser_process->tor_client_updater()->SetExecutablePath( + base::FilePath(FILE_PATH_LITERAL("test"))); +#endif + } + + void SetScriptSetting(HostContentSettingsMap* content_settings, + const ContentSettingsPattern& primary_pattern, + ContentSetting setting) { + content_settings->SetContentSettingCustomScope( + primary_pattern, ContentSettingsPattern::Wildcard(), + CONTENT_SETTINGS_TYPE_JAVASCRIPT, "", setting); + } + + ContentSetting GetScriptSetting( + HostContentSettingsMap* content_settings, + const GURL& primary_url) { + return content_settings->GetContentSetting( + primary_url, + GURL(), + CONTENT_SETTINGS_TYPE_JAVASCRIPT, + ""); + } +}; + // Test that legacy profile names (Person X) that have // not been user-modified are automatically renamed // to brave profile names (Profile X). @@ -118,3 +187,194 @@ IN_PROC_BROWSER_TEST_F(BraveProfileManagerTest, MigrateProfileNames) { ASSERT_EQ(entries[i]->GetPath(), profile_data[i].profile_path); } } + +#if BUILDFLAG(ENABLE_TOR) +IN_PROC_BROWSER_TEST_F(BraveProfileManagerTest, + SwitchToTorProfileShareBookmarks) { + ScopedTorLaunchPreventerForTest prevent_tor_process; + ProfileManager* profile_manager = g_browser_process->profile_manager(); + ASSERT_TRUE(profile_manager); + Profile* parent_profile = ProfileManager::GetActiveUserProfile(); + + // Add a bookmark in parent profile. + const base::string16 title(base::ASCIIToUTF16("Test")); + const GURL url1("https://www.test1.com"); + bookmarks::BookmarkModel* parent_bookmark_model = + BookmarkModelFactory::GetForBrowserContext(parent_profile); + bookmarks::test::WaitForBookmarkModelToLoad(parent_bookmark_model); + const bookmarks::BookmarkNode* root = + parent_bookmark_model->bookmark_bar_node(); + const bookmarks::BookmarkNode* new_node1 = + parent_bookmark_model->AddURL(root, 0, title, url1); + + Profile* tor_profile = SwitchToTorProfile(); + ASSERT_TRUE(brave::IsTorProfile(tor_profile)); + EXPECT_TRUE(tor_profile->IsOffTheRecord()); + EXPECT_EQ(brave::GetParentProfile(tor_profile), parent_profile); + + // Check if the same node is in Tor profile since we share the bookmark + // service between Tor profile and its parent profile. + bookmarks::BookmarkModel* tor_bookmark_model = + BookmarkModelFactory::GetForBrowserContext(tor_profile); + bookmarks::test::WaitForBookmarkModelToLoad(tor_bookmark_model); + EXPECT_EQ(tor_bookmark_model->GetMostRecentlyAddedUserNodeForURL(url1), + new_node1); + + // Add a new bookmark in parent profile again and check if it would show up + // in Tor profile. + const GURL url2("https://www.test2.com"); + const bookmarks::BookmarkNode* new_node2 = + parent_bookmark_model->AddURL(root, 0, title, url2); + EXPECT_EQ(tor_bookmark_model->GetMostRecentlyAddedUserNodeForURL(url2), + new_node2); + + // Add a new bookmark through tor profile and check if it would show up in + // its parent profile. + const GURL url3("https://www.test3.com"); + const bookmarks::BookmarkNode* tor_root = + tor_bookmark_model->bookmark_bar_node(); + const bookmarks::BookmarkNode* new_node3 = + tor_bookmark_model->AddURL(tor_root, 0, title, url3); + EXPECT_EQ(parent_bookmark_model->GetMostRecentlyAddedUserNodeForURL(url3), + new_node3); +} + +IN_PROC_BROWSER_TEST_F(BraveProfileManagerTest, + SwitchToTorProfileInheritPrefs) { + ScopedTorLaunchPreventerForTest prevent_tor_process; + + ProfileManager* profile_manager = g_browser_process->profile_manager(); + ASSERT_TRUE(profile_manager); + Profile* parent_profile = ProfileManager::GetActiveUserProfile(); + + // Set ShowBookmarkBar preference in the parent profile. + PrefService* parent_prefs = parent_profile->GetPrefs(); + parent_prefs->SetBoolean(bookmarks::prefs::kShowBookmarkBar, true); + EXPECT_TRUE(parent_prefs->GetBoolean(bookmarks::prefs::kShowBookmarkBar)); + + Profile* tor_profile = SwitchToTorProfile(); + ASSERT_TRUE(brave::IsTorProfile(tor_profile)); + EXPECT_TRUE(tor_profile->IsOffTheRecord()); + EXPECT_EQ(brave::GetParentProfile(tor_profile), parent_profile); + + // Check if ShowBookmarkBar preference is the same as Tor's parent profile. + PrefService* tor_prefs = tor_profile->GetPrefs(); + EXPECT_TRUE(tor_prefs->GetBoolean(bookmarks::prefs::kShowBookmarkBar)); + + // Change ShowBookmarkBar pref in parent profile will reflect in Tor profile. + parent_prefs->SetBoolean(bookmarks::prefs::kShowBookmarkBar, false); + EXPECT_FALSE(tor_prefs->GetBoolean(bookmarks::prefs::kShowBookmarkBar)); +} + +IN_PROC_BROWSER_TEST_F(BraveProfileManagerTest, + SwitchToTorProfileInheritContentSettings) { + const GURL brave_url("https://www.brave.com"); + ScopedTorLaunchPreventerForTest prevent_tor_process; + ProfileManager* profile_manager = g_browser_process->profile_manager(); + ASSERT_TRUE(profile_manager); + + Profile* parent_profile = ProfileManager::GetActiveUserProfile(); + + HostContentSettingsMap* parent_content_settings = + HostContentSettingsMapFactory::GetForProfile(parent_profile); + SetScriptSetting(parent_content_settings, + ContentSettingsPattern::Wildcard(), CONTENT_SETTING_BLOCK); + + Profile* tor_profile = SwitchToTorProfile(); + ASSERT_TRUE(brave::IsTorProfile(tor_profile)); + EXPECT_TRUE(tor_profile->IsOffTheRecord()); + EXPECT_EQ(brave::GetParentProfile(tor_profile), parent_profile); + + // Check Tor profile's content settings are inherited from its parent. + HostContentSettingsMap* tor_content_settings = + HostContentSettingsMapFactory::GetForProfile(tor_profile); + ContentSetting setting = GetScriptSetting(tor_content_settings, brave_url); + EXPECT_EQ(setting, CONTENT_SETTING_BLOCK); + + // Check changes of content settings from the parent profile will reflected + // in Tor profile when the setting is not set directly in Tor profile. + SetScriptSetting(parent_content_settings, + ContentSettingsPattern::Wildcard(), CONTENT_SETTING_ALLOW); + setting = GetScriptSetting(tor_content_settings, brave_url); + EXPECT_EQ(setting, CONTENT_SETTING_ALLOW); + + // Check changes of content settings from the parent profile will not + // overwrite the one in Tor profile when it is set directly in Tor profile. + SetScriptSetting(tor_content_settings, + ContentSettingsPattern::FromURL(brave_url), + CONTENT_SETTING_BLOCK); + setting = GetScriptSetting(parent_content_settings, brave_url); + EXPECT_EQ(setting, CONTENT_SETTING_ALLOW); + setting = GetScriptSetting(tor_content_settings, brave_url); + EXPECT_EQ(setting, CONTENT_SETTING_BLOCK); +} + +#if BUILDFLAG(ENABLE_EXTENSIONS) +class BraveProfileManagerExtensionTest + : public extensions::ExtensionBrowserTest { + public: + void SetUpOnMainThread() override { + extensions::ExtensionBrowserTest::SetUpOnMainThread(); + g_brave_browser_process->tor_client_updater()->SetExecutablePath( + base::FilePath(FILE_PATH_LITERAL("test"))); + + // Override extension data dir. + brave::RegisterPathProvider(); + base::PathService::Get(brave::DIR_TEST_DATA, &test_data_dir_); + } + + const extensions::Extension* GetExtension(Profile* profile) { + extensions::ExtensionRegistry* registry = + extensions::ExtensionRegistry::Get(profile); + for (const scoped_refptr& extension : + registry->enabled_extensions()) { + if (extension->name() == "Trivial Test Extension") + return extension.get(); + } + NOTREACHED(); + return NULL; + } +}; + +IN_PROC_BROWSER_TEST_F(BraveProfileManagerExtensionTest, + PRE_SwitchToTorProfileBlockExtensions) { + ScopedTorLaunchPreventerForTest prevent_tor_process; + ProfileManager* profile_manager = g_browser_process->profile_manager(); + ASSERT_TRUE(profile_manager); + Profile* parent_profile = ProfileManager::GetActiveUserProfile(); + ASSERT_TRUE(parent_profile); + + // Install an extension in parent profile and enable in incognito. + const extensions::Extension* extension = InstallExtension( + test_data_dir_.AppendASCII("trivial_extension"), 1); + const std::string id = extension->id(); + extensions::ExtensionPrefs* parent_extension_prefs = + extensions::ExtensionPrefs::Get(parent_profile); + parent_extension_prefs->SetIsIncognitoEnabled(id, true); +} + +IN_PROC_BROWSER_TEST_F(BraveProfileManagerExtensionTest, + SwitchToTorProfileBlockExtensions) { + ScopedTorLaunchPreventerForTest prevent_tor_process; + ProfileManager* profile_manager = g_browser_process->profile_manager(); + ASSERT_TRUE(profile_manager); + Profile* parent_profile = ProfileManager::GetActiveUserProfile(); + ASSERT_TRUE(parent_profile); + + Profile* tor_profile = SwitchToTorProfile(); + ASSERT_TRUE(brave::IsTorProfile(tor_profile)); + EXPECT_TRUE(tor_profile->IsOffTheRecord()); + EXPECT_EQ(brave::GetParentProfile(tor_profile), parent_profile); + + const extensions::Extension* extension = GetExtension(parent_profile); + const std::string id = extension->id(); + + // Extension should be blocked in Tor + EXPECT_TRUE(extensions::util::IsIncognitoEnabled(id, parent_profile)); + extensions::ExtensionRegistry* tor_registry = + extensions::ExtensionRegistry::Get(tor_profile); + EXPECT_TRUE(tor_registry->GetExtensionById( + id, extensions::ExtensionRegistry::BLOCKED)); +} +#endif +#endif diff --git a/browser/profiles/brave_profile_manager_unittest.cc b/browser/profiles/brave_profile_manager_unittest.cc index 0cb3404ec65a..1964db449385 100644 --- a/browser/profiles/brave_profile_manager_unittest.cc +++ b/browser/profiles/brave_profile_manager_unittest.cc @@ -11,6 +11,7 @@ #include "base/files/file_path.h" #include "base/files/scoped_temp_dir.h" #include "base/strings/utf_string_conversions.h" +#include "brave/browser/profiles/profile_util.h" #include "brave/browser/profiles/tor_unittest_profile_manager.h" #include "brave/browser/tor/tor_launcher_factory.h" #include "brave/browser/translate/buildflags/buildflags.h" @@ -76,16 +77,15 @@ class BraveProfileManagerTest : public testing::Test { // Helper function to create a profile with |name| for a profile |manager|. void CreateProfileAsync(ProfileManager* manager, - const std::string& name, + const base::FilePath& path, MockObserver* mock_observer) { - if (temp_dir_.GetPath().AppendASCII(name) == - BraveProfileManager::GetTorProfilePath()) + if (brave::IsTorProfilePath(path)) mock_observer->DidLaunchTorProcess(); - manager->CreateProfileAsync(temp_dir_.GetPath().AppendASCII(name), + + manager->CreateProfileAsync(path, base::Bind(&MockObserver::OnProfileCreated, base::Unretained(mock_observer)), - base::UTF8ToUTF16(name), - profiles::GetDefaultAvatarIconUrl(0)); + base::string16(), std::string()); } // Helper function to set profile ephemeral at prefs and attributes storage. @@ -114,24 +114,29 @@ class BraveProfileManagerTest : public testing::Test { TEST_F(BraveProfileManagerTest, GetTorProfilePath) { base::FilePath tor_path = BraveProfileManager::GetTorProfilePath(); - base::FilePath expected_path = temp_dir_.GetPath(); + base::FilePath user_data_dir = temp_dir_.GetPath(); + base::FilePath last_used_path = g_browser_process->profile_manager() + ->GetLastUsedProfileDir(user_data_dir); + base::FilePath expected_path = + last_used_path.AppendASCII("session_profiles"); expected_path = expected_path.Append(tor::kTorProfileDir); EXPECT_EQ(expected_path, tor_path); } TEST_F(BraveProfileManagerTest, InitProfileUserPrefs) { + ProfileManager* profile_manager = g_browser_process->profile_manager(); base::FilePath dest_path = temp_dir_.GetPath().AppendASCII(TestingProfile::kTestUserProfileDir); - base::FilePath tor_dest_path = - temp_dir_.GetPath().Append(tor::kTorProfileDir); - - ProfileManager* profile_manager = g_browser_process->profile_manager(); + base::FilePath tor_dest_path = BraveProfileManager::GetTorProfilePath(); // Successfully create test profile and tor profile Profile* profile = profile_manager->GetProfile(dest_path); Profile* tor_profile = profile_manager->GetProfile(tor_dest_path); ASSERT_TRUE(profile); ASSERT_TRUE(tor_profile); + EXPECT_EQ(brave::GetParentProfile(tor_profile), profile); + + tor_profile = tor_profile->GetOffTheRecordProfile(); // Check that the tor_profile name is non empty std::string profile_name = @@ -145,9 +150,6 @@ TEST_F(BraveProfileManagerTest, InitProfileUserPrefs) { EXPECT_EQ(avatar_index, size_t(0)); EXPECT_FALSE( tor_profile->GetPrefs()->GetBoolean(prefs::kProfileUsingDefaultName)); - EXPECT_FALSE(profile->GetPrefs()->GetBoolean(tor::prefs::kProfileUsingTor)); - EXPECT_TRUE( - tor_profile->GetPrefs()->GetBoolean(tor::prefs::kProfileUsingTor)); // Check WebRTC IP handling policy. EXPECT_EQ( @@ -163,57 +165,74 @@ TEST_F(BraveProfileManagerTest, InitProfileUserPrefs) { tor_profile->GetPrefs()->GetBoolean(prefs::kOfferTranslateEnabled)); } -// This is for tor guest window, remove it when we have persistent tor profiles -// support +// Dummy regular Tor profile should not show up as last profile. TEST_F(BraveProfileManagerTest, TorProfileDontEndUpAsLastProfile) { - base::FilePath dest_path = temp_dir_.GetPath(); - dest_path = dest_path.Append(tor::kTorProfileDir); + base::FilePath parent_path = + temp_dir_.GetPath().AppendASCII(TestingProfile::kTestUserProfileDir); ProfileManager* profile_manager = g_browser_process->profile_manager(); - TestingProfile* profile = - static_cast(profile_manager->GetProfile(dest_path)); - ASSERT_TRUE(profile); - - // Here the last used profile is still the "Default" profile. + // Create Tor parent profile. + Profile* parent_profile = profile_manager->GetProfile(parent_path); Profile* last_used_profile = profile_manager->GetLastUsedProfile(); - EXPECT_NE(profile, last_used_profile); + EXPECT_EQ(parent_profile, last_used_profile); + + // Create dummy Tor regular profile. + Profile* tor_profile = profile_manager->GetProfile( + BraveProfileManager::GetTorProfilePath()); + + // Here the last used profile is still the parent profile. + last_used_profile = profile_manager->GetLastUsedProfile(); + EXPECT_EQ(parent_profile, last_used_profile); // Create a browser for the profile. - Browser::CreateParams profile_params(profile, true); + Browser::CreateParams profile_params(tor_profile, true); std::unique_ptr browser( CreateBrowserWithTestWindowForParams(&profile_params)); last_used_profile = profile_manager->GetLastUsedProfile(); - EXPECT_NE(profile, last_used_profile); + EXPECT_EQ(parent_profile, last_used_profile); // Close the browser. browser.reset(); last_used_profile = profile_manager->GetLastUsedProfile(); - EXPECT_NE(profile, last_used_profile); + EXPECT_EQ(parent_profile, last_used_profile); } TEST_F(BraveProfileManagerTest, CreateProfilesAsync) { - const std::string profile_name1 = "New Profile 1"; - const std::string profile_name2 = "Tor Profile"; - MockObserver mock_observer; EXPECT_CALL(mock_observer, OnProfileCreated( testing::NotNull(), NotFail())).Times(testing::AtLeast(3)); EXPECT_CALL(mock_observer, DidLaunchTorProcess()).Times(1); + // Create Tor parent profile. + base::FilePath tor_parent_profile_path = + temp_dir_.GetPath().AppendASCII(TestingProfile::kTestUserProfileDir); ProfileManager* profile_manager = g_browser_process->profile_manager(); + Profile* parent_profile = + profile_manager->GetProfile(tor_parent_profile_path); - CreateProfileAsync(profile_manager, profile_name1, &mock_observer); - CreateProfileAsync(profile_manager, profile_name2, &mock_observer); + base::FilePath new_profile_path = + temp_dir_.GetPath().AppendASCII("New Profile 1"); + base::FilePath tor_profile_path = BraveProfileManager::GetTorProfilePath(); + + CreateProfileAsync(profile_manager, new_profile_path, &mock_observer); + CreateProfileAsync(profile_manager, tor_profile_path, &mock_observer); content::RunAllTasksUntilIdle(); + Profile* tor_profile = profile_manager->GetProfile(tor_profile_path); + EXPECT_EQ(brave::GetParentProfile(tor_profile), parent_profile); } TEST_F(BraveProfileManagerTest, NoWebtorrentInTorProfile) { + base::FilePath tor_parent_profile_path = + temp_dir_.GetPath().AppendASCII(TestingProfile::kTestUserProfileDir); ProfileManager* profile_manager = g_browser_process->profile_manager(); base::FilePath tor_path = BraveProfileManager::GetTorProfilePath(); + Profile* parent_profile = + profile_manager->GetProfile(tor_parent_profile_path); Profile* profile = profile_manager->GetProfile(tor_path); ASSERT_TRUE(profile); + EXPECT_EQ(brave::GetParentProfile(profile), parent_profile); EXPECT_FALSE(webtorrent::IsWebtorrentEnabled(profile)); } @@ -221,9 +240,15 @@ TEST_F(BraveProfileManagerTest, NoWebtorrentInTorProfile) { TEST_F(BraveProfileManagerTest, ProxyConfigMonitorInTorProfile) { ScopedTorLaunchPreventerForTest prevent_tor_process; ProfileManager* profile_manager = g_browser_process->profile_manager(); + base::FilePath dest_path = + temp_dir_.GetPath().AppendASCII(TestingProfile::kTestUserProfileDir); + + // Successfully create test profile and tor profile + Profile* parent_profile = profile_manager->GetProfile(dest_path); base::FilePath tor_path = BraveProfileManager::GetTorProfilePath(); - Profile* profile = profile_manager->GetProfile(tor_path); + Profile* profile = profile_manager->GetProfile(tor_path)->GetOffTheRecordProfile(); ASSERT_TRUE(profile); + EXPECT_EQ(brave::GetParentProfile(profile), parent_profile); std::unique_ptr monitor(new ProxyConfigMonitor(profile)); auto* proxy_config_service = monitor->GetProxyConfigServiceForTesting(); diff --git a/browser/profiles/tor_unittest_profile_manager.cc b/browser/profiles/tor_unittest_profile_manager.cc index 037312d6c598..49269f66984d 100644 --- a/browser/profiles/tor_unittest_profile_manager.cc +++ b/browser/profiles/tor_unittest_profile_manager.cc @@ -18,6 +18,8 @@ #include "components/sync_preferences/pref_service_mock_factory.h" #include "components/sync_preferences/testing_pref_service_syncable.h" +#include "brave/test/base/brave_testing_profile.h" + Profile* TorUnittestProfileManager::CreateProfileHelper( const base::FilePath& path) { if (!base::PathExists(path)) { @@ -49,15 +51,6 @@ void TorUnittestProfileManager::InitProfileUserPrefs(Profile* profile) { std::unique_ptr TorUnittestProfileManager::CreateProfile( const base::FilePath& path, Delegate* delegate) { - TestingProfile::Builder profile_builder; - sync_preferences::PrefServiceMockFactory factory; - auto registry = base::MakeRefCounted(); - std::unique_ptr prefs( - factory.CreateSyncable(registry.get())); - RegisterUserProfilePrefs(registry.get()); - tor::TorProfileService::RegisterProfilePrefs(registry.get()); - profile_builder.SetPrefService(std::move(prefs)); - profile_builder.SetPath(path); - profile_builder.SetDelegate(delegate); - return profile_builder.Build(); + return std::unique_ptr(new BraveTestingProfile( + path, delegate)); } diff --git a/browser/search_engines/search_engine_provider_service_browsertest.cc b/browser/search_engines/search_engine_provider_service_browsertest.cc index 53d4dd8e6f9d..a94ef01125ea 100644 --- a/browser/search_engines/search_engine_provider_service_browsertest.cc +++ b/browser/search_engines/search_engine_provider_service_browsertest.cc @@ -4,6 +4,7 @@ * You can obtain one at http://mozilla.org/MPL/2.0/. */ #include "base/strings/utf_string_conversions.h" +#include "brave/browser/brave_browser_process_impl.h" #include "brave/browser/profiles/brave_profile_manager.h" #include "brave/browser/profiles/profile_util.h" #include "brave/browser/search_engines/search_engine_provider_util.h" @@ -23,10 +24,21 @@ #include "content/public/test/test_utils.h" #if BUILDFLAG(ENABLE_TOR) +#include "brave/browser/extensions/brave_tor_client_updater.h" #include "brave/browser/tor/tor_launcher_factory.h" #endif -using SearchEngineProviderServiceTest = InProcessBrowserTest; +// using SearchEngineProviderServiceTest = InProcessBrowserTest; +class SearchEngineProviderServiceTest : public InProcessBrowserTest { + public: + void SetUpOnMainThread() override { + InProcessBrowserTest::SetUpOnMainThread(); +#if BUILDFLAG(ENABLE_TOR) + g_brave_browser_process->tor_client_updater()->SetExecutablePath( + base::FilePath(FILE_PATH_LITERAL("test"))); +#endif + } +}; TemplateURLData CreateTestSearchEngine() { TemplateURLData result; @@ -162,7 +174,8 @@ IN_PROC_BROWSER_TEST_F(SearchEngineProviderServiceTest, service->SetUserSelectedDefaultSearchProvider(&other_url); } -// Check changed provider in tor profile is retained across the sessions. +// Check changed provider in tor profile should not be retained across the +// sessions. IN_PROC_BROWSER_TEST_F(SearchEngineProviderServiceTest, CheckDefaultTorProfileSearchProviderTest) { ScopedTorLaunchPreventerForTest prevent_tor_process; @@ -173,11 +186,12 @@ IN_PROC_BROWSER_TEST_F(SearchEngineProviderServiceTest, Profile* tor_profile = BrowserList::GetInstance()->GetLastActive()->profile(); EXPECT_TRUE(brave::IsTorProfile(tor_profile)); - int expected_provider_id = - TemplateURLPrepopulateData::PREPOPULATED_ENGINE_ID_BING; + int default_provider_id = brave::IsRegionForQwant(tor_profile) ? + TemplateURLPrepopulateData::PREPOPULATED_ENGINE_ID_QWANT : + TemplateURLPrepopulateData::PREPOPULATED_ENGINE_ID_DUCKDUCKGO; auto* service = TemplateURLServiceFactory::GetForProfile(tor_profile); EXPECT_EQ(service->GetDefaultSearchProvider()->data().prepopulate_id, - expected_provider_id); + default_provider_id); } #endif diff --git a/browser/tor/tor_navigation_throttle_unittest.cc b/browser/tor/tor_navigation_throttle_unittest.cc index 1876971f7cc3..3403ea6181a6 100644 --- a/browser/tor/tor_navigation_throttle_unittest.cc +++ b/browser/tor/tor_navigation_throttle_unittest.cc @@ -9,6 +9,7 @@ #include "base/files/scoped_temp_dir.h" #include "brave/browser/profiles/brave_profile_manager.h" +#include "brave/browser/profiles/profile_util.h" #include "brave/browser/profiles/tor_unittest_profile_manager.h" #include "chrome/test/base/scoped_testing_local_state.h" #include "chrome/test/base/testing_browser_process.h" @@ -42,6 +43,7 @@ class TorNavigationThrottleUnitTest : public testing::Test { temp_dir_.GetPath().AppendASCII(TestingProfile::kTestUserProfileDir)); 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_ = diff --git a/browser/tor/tor_profile_service.cc b/browser/tor/tor_profile_service.cc index 381be09e5543..db996a4723fe 100644 --- a/browser/tor/tor_profile_service.cc +++ b/browser/tor/tor_profile_service.cc @@ -33,12 +33,6 @@ TorProfileService::TorProfileService() { TorProfileService::~TorProfileService() { } -// static -void TorProfileService::RegisterProfilePrefs( - user_prefs::PrefRegistrySyncable* registry) { - registry->RegisterBooleanPref(prefs::kProfileUsingTor, false); -} - // static void TorProfileService::RegisterLocalStatePrefs(PrefRegistrySimple* registry) { std::string port; diff --git a/browser/tor/tor_profile_service.h b/browser/tor/tor_profile_service.h index 255ae988819f..f6bacc5e500f 100644 --- a/browser/tor/tor_profile_service.h +++ b/browser/tor/tor_profile_service.h @@ -38,7 +38,6 @@ class TorProfileService : public KeyedService { TorProfileService(); ~TorProfileService() override; - static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry); static void RegisterLocalStatePrefs(PrefRegistrySimple* registry); virtual void SetNewTorCircuit(content::WebContents* web_contents) = 0; diff --git a/browser/tor/tor_profile_service_impl.cc b/browser/tor/tor_profile_service_impl.cc index e90276937621..b45605d1f336 100644 --- a/browser/tor/tor_profile_service_impl.cc +++ b/browser/tor/tor_profile_service_impl.cc @@ -121,6 +121,11 @@ void OnNewTorCircuit(std::unique_ptr tracker, TorProfileServiceImpl::TorProfileServiceImpl(Profile* profile) : profile_(profile), 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; + } g_brave_browser_process->tor_client_updater()->AddObserver(this); OnExecutableReady(GetTorExecutablePath()); diff --git a/chromium_src/chrome/test/base/testing_profile.cc b/chromium_src/chrome/test/base/testing_profile.cc new file mode 100644 index 000000000000..a90d4c0ea7cc --- /dev/null +++ b/chromium_src/chrome/test/base/testing_profile.cc @@ -0,0 +1,14 @@ +/* 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/profiles/profile_util.h" + +#define BRAVE_INIT \ + if (brave::IsSessionProfilePath(GetPath())) { \ + brave::CreateParentProfileData(this); \ + } else + +#include "../../../../../chrome/test/base/testing_profile.cc" +#undef BRAVE_INIT diff --git a/common/tor/pref_names.cc b/common/tor/pref_names.cc index c5e3b61b5616..eeb88ec50a02 100644 --- a/common/tor/pref_names.cc +++ b/common/tor/pref_names.cc @@ -7,7 +7,6 @@ namespace tor { namespace prefs { -extern const char kProfileUsingTor[] = "tor.profile_using_tor"; extern const char kTorProxyString[] = "tor.tor_proxy_string"; } // namespace prefs diff --git a/common/tor/pref_names.h b/common/tor/pref_names.h index 41453bd415e2..d91b99a85bb4 100644 --- a/common/tor/pref_names.h +++ b/common/tor/pref_names.h @@ -8,7 +8,6 @@ namespace tor { namespace prefs { -extern const char kProfileUsingTor[]; extern const char kTorProxyString[]; } // namespace prefs diff --git a/patches/chrome-test-base-testing_profile.cc.patch b/patches/chrome-test-base-testing_profile.cc.patch new file mode 100644 index 000000000000..03a1e530f311 --- /dev/null +++ b/patches/chrome-test-base-testing_profile.cc.patch @@ -0,0 +1,12 @@ +diff --git a/chrome/test/base/testing_profile.cc b/chrome/test/base/testing_profile.cc +index 08fc569a34ee4f25182ae58e97dd1bede9ae6b72..44333edf897f55ee69d3895e01eca498b791e4be 100644 +--- a/chrome/test/base/testing_profile.cc ++++ b/chrome/test/base/testing_profile.cc +@@ -417,6 +417,7 @@ void TestingProfile::Init() { + EnsureBrowserContextKeyedServiceFactoriesBuilt(); + + #if BUILDFLAG(ENABLE_SUPERVISED_USERS) ++ BRAVE_INIT + if (!IsOffTheRecord()) { + SupervisedUserSettingsService* settings_service = + SupervisedUserSettingsServiceFactory::GetForKey(key_.get()); diff --git a/test/BUILD.gn b/test/BUILD.gn index f48d2bcd59a4..acbabd8ee45e 100644 --- a/test/BUILD.gn +++ b/test/BUILD.gn @@ -26,6 +26,8 @@ group("fix_brave_test_install_name_adblock") { static_library("brave_test_support_unit") { testonly = true sources = [ + "base/brave_testing_profile.cc", + "base/brave_testing_profile.h", "base/brave_unit_test_suite.cc", "base/brave_unit_test_suite.h", "base/run_all_unittests.cc", diff --git a/test/base/brave_testing_profile.cc b/test/base/brave_testing_profile.cc new file mode 100644 index 000000000000..c5f4a79f0939 --- /dev/null +++ b/test/base/brave_testing_profile.cc @@ -0,0 +1,16 @@ +/* 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/test/base/brave_testing_profile.h" + +#include "brave/browser/profiles/profile_util.h" + +BraveTestingProfile::BraveTestingProfile( + const base::FilePath& path, Delegate* delegate) + : TestingProfile(path, delegate) { + if (brave::IsSessionProfilePath(path)) { + brave::CreateParentProfileData(this); + } +} diff --git a/test/base/brave_testing_profile.h b/test/base/brave_testing_profile.h new file mode 100644 index 000000000000..38eb27f43a96 --- /dev/null +++ b/test/base/brave_testing_profile.h @@ -0,0 +1,17 @@ +/* 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_TEST_BASE_TESTING_PROFILE_H_ +#define BRAVE_TEST_BASE_TESTING_PROFILE_H_ + +#include "chrome/test/base/testing_profile.h" + +class BraveTestingProfile : public TestingProfile { + public: + BraveTestingProfile(const base::FilePath& path, Delegate* delegate); + ~BraveTestingProfile() override = default; +}; + +#endif // BRAVE_TEST_BASE_TESTING_PROFILE_H_ diff --git a/test/data/trivial_extension/manifest.json b/test/data/trivial_extension/manifest.json new file mode 100644 index 000000000000..5cac7064e794 --- /dev/null +++ b/test/data/trivial_extension/manifest.json @@ -0,0 +1,6 @@ +{ + "name": "Trivial Test Extension", + "version": "1.0", + "manifest_version": 2, + "description": "A minimal extension for testing purposes." +} From 1022dd979827141e921c1434bb7b45acdef29f01 Mon Sep 17 00:00:00 2001 From: Jocelyn Liu Date: Tue, 1 Oct 2019 19:35:58 -0700 Subject: [PATCH 11/16] lint and clang format --- browser/brave_browser_main_parts.cc | 3 +- browser/brave_content_browser_client.cc | 2 +- .../extensions/brave_tor_client_updater.cc | 9 ++-- .../brave_tor_client_updater_browsertest.cc | 10 ++-- browser/profiles/brave_profile_impl.cc | 14 +++-- browser/profiles/brave_profile_impl.h | 1 - browser/profiles/brave_profile_manager.cc | 6 +-- .../brave_profile_manager_browsertest.cc | 52 +++++++++---------- .../brave_profile_manager_unittest.cc | 25 ++++----- browser/profiles/profile_util.cc | 43 +++++++-------- browser/profiles/profile_util.h | 10 ++-- .../profiles/tor_unittest_profile_manager.cc | 4 +- ...rch_engine_provider_service_browsertest.cc | 7 +-- .../bookmarks/bookmark_model_factory.cc | 3 +- .../managed_bookmark_service_factory.cc | 3 +- .../chrome_extensions_browser_client.cc | 3 +- .../profiles/pref_service_builder_utils.cc | 3 +- .../chrome/browser/profiles/profile_impl.h | 8 +-- ...upervised_user_settings_service_factory.cc | 4 +- .../chrome/browser/ui/browser_navigator.cc | 18 +++---- .../chrome/test/base/testing_profile.cc | 8 +-- common/tor/pref_names.cc | 3 +- common/tor/pref_names.h | 3 +- .../brave_extensions_browser_client.cc | 2 +- test/base/brave_testing_profile.cc | 8 +-- test/base/brave_testing_profile.h | 6 +-- 26 files changed, 129 insertions(+), 129 deletions(-) diff --git a/browser/brave_browser_main_parts.cc b/browser/brave_browser_main_parts.cc index c28eedd4e013..7bc7a22fc2f7 100644 --- a/browser/brave_browser_main_parts.cc +++ b/browser/brave_browser_main_parts.cc @@ -62,8 +62,7 @@ void BraveBrowserMainParts::PostBrowserStart() { // Delete Tor legacy profile if exists. if (base::PathExists(tor_legacy_path)) { profile_manager->MaybeScheduleProfileForDeletion( - tor_legacy_path, - base::DoNothing(), + tor_legacy_path, base::DoNothing(), ProfileMetrics::DELETE_PROFILE_SETTINGS); } #endif diff --git a/browser/brave_content_browser_client.cc b/browser/brave_content_browser_client.cc index fb9759cc5fcd..48679b8d0d41 100644 --- a/browser/brave_content_browser_client.cc +++ b/browser/brave_content_browser_client.cc @@ -211,7 +211,7 @@ void BraveContentBrowserClient::AdjustUtilityServiceProcessCommandLine( g_brave_browser_process->tor_client_updater()->GetExecutablePath(); if (!path.empty()) { command_line->AppendSwitchPath(tor::switches::kTorExecutablePath, - path.BaseName()); + path.BaseName()); } } #endif diff --git a/browser/extensions/brave_tor_client_updater.cc b/browser/extensions/brave_tor_client_updater.cc index 77f240dd6c0a..050655a88d35 100644 --- a/browser/extensions/brave_tor_client_updater.cc +++ b/browser/extensions/brave_tor_client_updater.cc @@ -22,8 +22,7 @@ namespace extensions { namespace { -base::FilePath InitExecutablePath( - const base::FilePath& install_dir) { +base::FilePath InitExecutablePath(const base::FilePath& install_dir) { base::FilePath executable_path; base::FileEnumerator traversal(install_dir, false, base::FileEnumerator::FILES, @@ -105,8 +104,7 @@ BraveTorClientUpdater::BraveTorClientUpdater(BraveComponent::Delegate* delegate) task_runner_( base::CreateSequencedTaskRunnerWithTraits({base::MayBlock()})), registered_(false), - weak_ptr_factory_(this) { -} + weak_ptr_factory_(this) {} BraveTorClientUpdater::~BraveTorClientUpdater() { } @@ -135,7 +133,8 @@ void BraveTorClientUpdater::OnComponentReady( const std::string& component_id, const base::FilePath& install_dir, const std::string& manifest) { - base::PostTaskAndReplyWithResult(GetTaskRunner().get(), FROM_HERE, + base::PostTaskAndReplyWithResult( + GetTaskRunner().get(), FROM_HERE, base::BindOnce(&InitExecutablePath, install_dir), base::BindOnce(&BraveTorClientUpdater::SetExecutablePath, weak_ptr_factory_.GetWeakPtr())); diff --git a/browser/extensions/brave_tor_client_updater_browsertest.cc b/browser/extensions/brave_tor_client_updater_browsertest.cc index 239564667c94..2c668a740247 100644 --- a/browser/extensions/brave_tor_client_updater_browsertest.cc +++ b/browser/extensions/brave_tor_client_updater_browsertest.cc @@ -1,4 +1,5 @@ -/* This Source Code Form is subject to the terms of the Mozilla Public +/* 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/. */ @@ -16,8 +17,9 @@ using extensions::ExtensionBrowserTest; -const std::string kTorClientUpdaterComponentTestId("ngicbhhaldfdgmjhilmnleppfpmkgbbk"); -const std::string kTorClientUpdaterComponentTestBase64PublicKey = +const char kTorClientUpdaterComponentTestId[] = + "ngicbhhaldfdgmjhilmnleppfpmkgbbk"; +const char kTorClientUpdaterComponentTestBase64PublicKey[] = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAncFKJWCX6RqCRYOR0t5a" "js7HWIVP3Ne272HZs3MqiaNvo9IikbPd4JnUMeQjLhkXTwxg6Up9Tmrgo3M8T91D" "dggzpAG4OlhKj3l3N5kZnj/CxQ73YVd41jHAF97lZVoD5VTCGtEelzA5eHI4N4Hd" @@ -27,7 +29,7 @@ const std::string kTorClientUpdaterComponentTestBase64PublicKey = "QQIDAQAB"; class BraveTorClientUpdaterTest : public ExtensionBrowserTest { -public: + public: BraveTorClientUpdaterTest() {} void SetUp() override { diff --git a/browser/profiles/brave_profile_impl.cc b/browser/profiles/brave_profile_impl.cc index 1de206e7446b..b8607eead227 100644 --- a/browser/profiles/brave_profile_impl.cc +++ b/browser/profiles/brave_profile_impl.cc @@ -36,8 +36,7 @@ BraveProfileImpl::BraveProfileImpl( create_mode == CREATE_MODE_ASYNCHRONOUS) { auto* parent_profile = brave::CreateParentProfileData(this); - notification_registrar_.Add(this, - chrome::NOTIFICATION_PROFILE_DESTROYED, + notification_registrar_.Add(this, chrome::NOTIFICATION_PROFILE_DESTROYED, content::Source(parent_profile)); base::PostTaskAndReply( FROM_HERE, base::DoNothing(), @@ -48,17 +47,16 @@ BraveProfileImpl::BraveProfileImpl( BraveProfileImpl::~BraveProfileImpl() {} -void BraveProfileImpl::Observe( - int type, - const content::NotificationSource& source, - const content::NotificationDetails& details) { +void BraveProfileImpl::Observe(int type, + const content::NotificationSource& source, + const content::NotificationDetails& details) { switch (type) { case chrome::NOTIFICATION_PROFILE_DESTROYED: { // this only happens when a profile is deleted because the profile manager // ensures that session profiles are destroyed before their parents // passing false for `success` removes the profile from the info cache - g_browser_process->profile_manager()->OnProfileCreated( - this, false, false); + g_browser_process->profile_manager()->OnProfileCreated(this, false, + false); break; } default: { diff --git a/browser/profiles/brave_profile_impl.h b/browser/profiles/brave_profile_impl.h index 48b2d871e3cb..18b56016ed54 100644 --- a/browser/profiles/brave_profile_impl.h +++ b/browser/profiles/brave_profile_impl.h @@ -29,7 +29,6 @@ class BraveProfileImpl : public ProfileImpl, const content::NotificationDetails& details) override; private: - // Listens for parent profile destruction. content::NotificationRegistrar notification_registrar_; diff --git a/browser/profiles/brave_profile_manager.cc b/browser/profiles/brave_profile_manager.cc index 7d629c627d47..4f0a95aa0cc5 100644 --- a/browser/profiles/brave_profile_manager.cc +++ b/browser/profiles/brave_profile_manager.cc @@ -26,8 +26,8 @@ #include "brave/components/brave_webtorrent/browser/buildflags/buildflags.h" #include "brave/content/browser/webui/brave_shared_resources_data_source.h" #include "chrome/browser/browser_process.h" -#include "chrome/browser/profiles/profile_attributes_storage.h" #include "chrome/browser/profiles/profile_attributes_entry.h" +#include "chrome/browser/profiles/profile_attributes_storage.h" #include "chrome/browser/profiles/profiles_state.h" #include "chrome/common/chrome_constants.h" #include "chrome/common/pref_names.h" @@ -115,7 +115,7 @@ std::string BraveProfileManager::GetLastUsedProfileName() { PrefService* local_state = g_browser_process->local_state(); DCHECK(local_state); const std::string last_used_profile_name = - local_state->GetString(prefs::kProfileLastUsed); + local_state->GetString(prefs::kProfileLastUsed); if (last_used_profile_name == base::FilePath(tor::kTorProfileDir).AsUTF8Unsafe()) return chrome::kInitialProfile; @@ -150,7 +150,7 @@ void BraveProfileManager::OnProfileCreated(Profile* profile, #if BUILDFLAG(ENABLE_EXTENSIONS) extensions::ExtensionService* extension_service = - extensions::ExtensionSystem::Get(profile)->extension_service(); + extensions::ExtensionSystem::Get(profile)->extension_service(); extension_service->BlockAllExtensions(); #endif diff --git a/browser/profiles/brave_profile_manager_browsertest.cc b/browser/profiles/brave_profile_manager_browsertest.cc index 580371d65f28..c1368e880bc4 100644 --- a/browser/profiles/brave_profile_manager_browsertest.cc +++ b/browser/profiles/brave_profile_manager_browsertest.cc @@ -110,21 +110,17 @@ class BraveProfileManagerTest : public InProcessBrowserTest { } void SetScriptSetting(HostContentSettingsMap* content_settings, - const ContentSettingsPattern& primary_pattern, - ContentSetting setting) { + const ContentSettingsPattern& primary_pattern, + ContentSetting setting) { content_settings->SetContentSettingCustomScope( primary_pattern, ContentSettingsPattern::Wildcard(), CONTENT_SETTINGS_TYPE_JAVASCRIPT, "", setting); } - ContentSetting GetScriptSetting( - HostContentSettingsMap* content_settings, - const GURL& primary_url) { + ContentSetting GetScriptSetting(HostContentSettingsMap* content_settings, + const GURL& primary_url) { return content_settings->GetContentSetting( - primary_url, - GURL(), - CONTENT_SETTINGS_TYPE_JAVASCRIPT, - ""); + primary_url, GURL(), CONTENT_SETTINGS_TYPE_JAVASCRIPT, ""); } }; @@ -190,7 +186,7 @@ IN_PROC_BROWSER_TEST_F(BraveProfileManagerTest, MigrateProfileNames) { #if BUILDFLAG(ENABLE_TOR) IN_PROC_BROWSER_TEST_F(BraveProfileManagerTest, - SwitchToTorProfileShareBookmarks) { + SwitchToTorProfileShareBookmarks) { ScopedTorLaunchPreventerForTest prevent_tor_process; ProfileManager* profile_manager = g_browser_process->profile_manager(); ASSERT_TRUE(profile_manager); @@ -240,7 +236,7 @@ IN_PROC_BROWSER_TEST_F(BraveProfileManagerTest, } IN_PROC_BROWSER_TEST_F(BraveProfileManagerTest, - SwitchToTorProfileInheritPrefs) { + SwitchToTorProfileInheritPrefs) { ScopedTorLaunchPreventerForTest prevent_tor_process; ProfileManager* profile_manager = g_browser_process->profile_manager(); @@ -267,7 +263,7 @@ IN_PROC_BROWSER_TEST_F(BraveProfileManagerTest, } IN_PROC_BROWSER_TEST_F(BraveProfileManagerTest, - SwitchToTorProfileInheritContentSettings) { + SwitchToTorProfileInheritContentSettings) { const GURL brave_url("https://www.brave.com"); ScopedTorLaunchPreventerForTest prevent_tor_process; ProfileManager* profile_manager = g_browser_process->profile_manager(); @@ -276,9 +272,9 @@ IN_PROC_BROWSER_TEST_F(BraveProfileManagerTest, Profile* parent_profile = ProfileManager::GetActiveUserProfile(); HostContentSettingsMap* parent_content_settings = - HostContentSettingsMapFactory::GetForProfile(parent_profile); - SetScriptSetting(parent_content_settings, - ContentSettingsPattern::Wildcard(), CONTENT_SETTING_BLOCK); + HostContentSettingsMapFactory::GetForProfile(parent_profile); + SetScriptSetting(parent_content_settings, ContentSettingsPattern::Wildcard(), + CONTENT_SETTING_BLOCK); Profile* tor_profile = SwitchToTorProfile(); ASSERT_TRUE(brave::IsTorProfile(tor_profile)); @@ -287,22 +283,22 @@ IN_PROC_BROWSER_TEST_F(BraveProfileManagerTest, // Check Tor profile's content settings are inherited from its parent. HostContentSettingsMap* tor_content_settings = - HostContentSettingsMapFactory::GetForProfile(tor_profile); + HostContentSettingsMapFactory::GetForProfile(tor_profile); ContentSetting setting = GetScriptSetting(tor_content_settings, brave_url); EXPECT_EQ(setting, CONTENT_SETTING_BLOCK); // Check changes of content settings from the parent profile will reflected // in Tor profile when the setting is not set directly in Tor profile. - SetScriptSetting(parent_content_settings, - ContentSettingsPattern::Wildcard(), CONTENT_SETTING_ALLOW); + SetScriptSetting(parent_content_settings, ContentSettingsPattern::Wildcard(), + CONTENT_SETTING_ALLOW); setting = GetScriptSetting(tor_content_settings, brave_url); EXPECT_EQ(setting, CONTENT_SETTING_ALLOW); // Check changes of content settings from the parent profile will not // overwrite the one in Tor profile when it is set directly in Tor profile. SetScriptSetting(tor_content_settings, - ContentSettingsPattern::FromURL(brave_url), - CONTENT_SETTING_BLOCK); + ContentSettingsPattern::FromURL(brave_url), + CONTENT_SETTING_BLOCK); setting = GetScriptSetting(parent_content_settings, brave_url); EXPECT_EQ(setting, CONTENT_SETTING_ALLOW); setting = GetScriptSetting(tor_content_settings, brave_url); @@ -311,7 +307,7 @@ IN_PROC_BROWSER_TEST_F(BraveProfileManagerTest, #if BUILDFLAG(ENABLE_EXTENSIONS) class BraveProfileManagerExtensionTest - : public extensions::ExtensionBrowserTest { + : public extensions::ExtensionBrowserTest { public: void SetUpOnMainThread() override { extensions::ExtensionBrowserTest::SetUpOnMainThread(); @@ -325,9 +321,9 @@ class BraveProfileManagerExtensionTest const extensions::Extension* GetExtension(Profile* profile) { extensions::ExtensionRegistry* registry = - extensions::ExtensionRegistry::Get(profile); + extensions::ExtensionRegistry::Get(profile); for (const scoped_refptr& extension : - registry->enabled_extensions()) { + registry->enabled_extensions()) { if (extension->name() == "Trivial Test Extension") return extension.get(); } @@ -337,7 +333,7 @@ class BraveProfileManagerExtensionTest }; IN_PROC_BROWSER_TEST_F(BraveProfileManagerExtensionTest, - PRE_SwitchToTorProfileBlockExtensions) { + PRE_SwitchToTorProfileBlockExtensions) { ScopedTorLaunchPreventerForTest prevent_tor_process; ProfileManager* profile_manager = g_browser_process->profile_manager(); ASSERT_TRUE(profile_manager); @@ -345,8 +341,8 @@ IN_PROC_BROWSER_TEST_F(BraveProfileManagerExtensionTest, ASSERT_TRUE(parent_profile); // Install an extension in parent profile and enable in incognito. - const extensions::Extension* extension = InstallExtension( - test_data_dir_.AppendASCII("trivial_extension"), 1); + const extensions::Extension* extension = + InstallExtension(test_data_dir_.AppendASCII("trivial_extension"), 1); const std::string id = extension->id(); extensions::ExtensionPrefs* parent_extension_prefs = extensions::ExtensionPrefs::Get(parent_profile); @@ -354,7 +350,7 @@ IN_PROC_BROWSER_TEST_F(BraveProfileManagerExtensionTest, } IN_PROC_BROWSER_TEST_F(BraveProfileManagerExtensionTest, - SwitchToTorProfileBlockExtensions) { + SwitchToTorProfileBlockExtensions) { ScopedTorLaunchPreventerForTest prevent_tor_process; ProfileManager* profile_manager = g_browser_process->profile_manager(); ASSERT_TRUE(profile_manager); @@ -374,7 +370,7 @@ IN_PROC_BROWSER_TEST_F(BraveProfileManagerExtensionTest, extensions::ExtensionRegistry* tor_registry = extensions::ExtensionRegistry::Get(tor_profile); EXPECT_TRUE(tor_registry->GetExtensionById( - id, extensions::ExtensionRegistry::BLOCKED)); + id, extensions::ExtensionRegistry::BLOCKED)); } #endif #endif diff --git a/browser/profiles/brave_profile_manager_unittest.cc b/browser/profiles/brave_profile_manager_unittest.cc index 1964db449385..c2f28e512213 100644 --- a/browser/profiles/brave_profile_manager_unittest.cc +++ b/browser/profiles/brave_profile_manager_unittest.cc @@ -18,11 +18,11 @@ #include "brave/common/tor/pref_names.h" #include "brave/common/tor/tor_constants.h" #include "brave/components/brave_webtorrent/browser/webtorrent_util.h" +#include "chrome/browser/net/proxy_config_monitor.h" #include "chrome/browser/profiles/profile.h" #include "chrome/browser/profiles/profile_avatar_icon_util.h" #include "chrome/browser/profiles/profile_manager.h" #include "chrome/browser/profiles/profiles_state.h" -#include "chrome/browser/net/proxy_config_monitor.h" #include "chrome/browser/ui/browser.h" #include "chrome/common/pref_names.h" #include "chrome/grit/generated_resources.h" @@ -35,10 +35,10 @@ #include "content/public/common/webrtc_ip_handling_policy.h" #include "content/public/test/test_browser_thread_bundle.h" #include "content/public/test/test_utils.h" -#include "testing/gmock/include/gmock/gmock.h" -#include "testing/gtest/include/gtest/gtest.h" #include "net/proxy_resolution/proxy_config_with_annotation.h" #include "net/proxy_resolution/proxy_resolution_service.h" +#include "testing/gmock/include/gmock/gmock.h" +#include "testing/gtest/include/gtest/gtest.h" #include "ui/base/l10n/l10n_util.h" namespace { @@ -115,10 +115,10 @@ class BraveProfileManagerTest : public testing::Test { TEST_F(BraveProfileManagerTest, GetTorProfilePath) { base::FilePath tor_path = BraveProfileManager::GetTorProfilePath(); base::FilePath user_data_dir = temp_dir_.GetPath(); - base::FilePath last_used_path = g_browser_process->profile_manager() - ->GetLastUsedProfileDir(user_data_dir); - base::FilePath expected_path = - last_used_path.AppendASCII("session_profiles"); + base::FilePath last_used_path = + g_browser_process->profile_manager()->GetLastUsedProfileDir( + user_data_dir); + base::FilePath expected_path = last_used_path.AppendASCII("session_profiles"); expected_path = expected_path.Append(tor::kTorProfileDir); EXPECT_EQ(expected_path, tor_path); } @@ -168,7 +168,7 @@ TEST_F(BraveProfileManagerTest, InitProfileUserPrefs) { // Dummy regular Tor profile should not show up as last profile. TEST_F(BraveProfileManagerTest, TorProfileDontEndUpAsLastProfile) { base::FilePath parent_path = - temp_dir_.GetPath().AppendASCII(TestingProfile::kTestUserProfileDir); + temp_dir_.GetPath().AppendASCII(TestingProfile::kTestUserProfileDir); ProfileManager* profile_manager = g_browser_process->profile_manager(); @@ -178,8 +178,8 @@ TEST_F(BraveProfileManagerTest, TorProfileDontEndUpAsLastProfile) { EXPECT_EQ(parent_profile, last_used_profile); // Create dummy Tor regular profile. - Profile* tor_profile = profile_manager->GetProfile( - BraveProfileManager::GetTorProfilePath()); + Profile* tor_profile = + profile_manager->GetProfile(BraveProfileManager::GetTorProfilePath()); // Here the last used profile is still the parent profile. last_used_profile = profile_manager->GetLastUsedProfile(); @@ -241,12 +241,13 @@ TEST_F(BraveProfileManagerTest, ProxyConfigMonitorInTorProfile) { ScopedTorLaunchPreventerForTest prevent_tor_process; ProfileManager* profile_manager = g_browser_process->profile_manager(); base::FilePath dest_path = - temp_dir_.GetPath().AppendASCII(TestingProfile::kTestUserProfileDir); + temp_dir_.GetPath().AppendASCII(TestingProfile::kTestUserProfileDir); // Successfully create test profile and tor profile Profile* parent_profile = profile_manager->GetProfile(dest_path); base::FilePath tor_path = BraveProfileManager::GetTorProfilePath(); - Profile* profile = profile_manager->GetProfile(tor_path)->GetOffTheRecordProfile(); + Profile* profile = + profile_manager->GetProfile(tor_path)->GetOffTheRecordProfile(); ASSERT_TRUE(profile); EXPECT_EQ(brave::GetParentProfile(profile), parent_profile); diff --git a/browser/profiles/profile_util.cc b/browser/profiles/profile_util.cc index 6d2c602aaab1..da55cccb4f0d 100644 --- a/browser/profiles/profile_util.cc +++ b/browser/profiles/profile_util.cc @@ -5,6 +5,10 @@ #include "brave/browser/profiles/profile_util.h" +#include +#include +#include + #include "base/files/file_path.h" #include "base/memory/ptr_util.h" #include "base/no_destructor.h" @@ -41,21 +45,20 @@ class ParentProfileData : public base::SupportsUserData::Data { public: ~ParentProfileData() override; static void CreateForProfile(content::BrowserContext* context); - static ParentProfileData* FromProfile( - content::BrowserContext* context); + static ParentProfileData* FromProfile(content::BrowserContext* context); static const ParentProfileData* FromProfile( const content::BrowserContext* context); - static const ParentProfileData* FromPath( - const base::FilePath& path); + static const ParentProfileData* FromPath(const base::FilePath& path); Profile* profile() const; std::unique_ptr Clone() override; + private: static const void* const kUserDataKey; static const void* UserDataKey(); - ParentProfileData(Profile* profile); + explicit ParentProfileData(Profile* profile); Profile* profile_; base::FilePath path_; @@ -68,20 +71,20 @@ const void* const ParentProfileData::kUserDataKey = &kUserDataKey; // static void ParentProfileData::CreateForProfile(content::BrowserContext* context) { DCHECK(context); - if (FromProfile(context)) return; + if (FromProfile(context)) + return; auto* profile = Profile::FromBrowserContext(context); auto* profile_manager = g_browser_process->profile_manager(); DCHECK(profile_manager); auto* parent_profile = - profile_manager->GetProfileByPath(GetParentProfilePath(profile)); + profile_manager->GetProfileByPath(GetParentProfilePath(profile)); DCHECK(parent_profile); DCHECK(parent_profile != profile); - profile->SetUserData( - UserDataKey(), - base::WrapUnique(new ParentProfileData(parent_profile))); + profile->SetUserData(UserDataKey(), + base::WrapUnique(new ParentProfileData(parent_profile))); GetPathMap()->insert( std::pair(profile->GetPath(), profile)); @@ -92,9 +95,8 @@ ParentProfileData* ParentProfileData::FromProfile( content::BrowserContext* context) { DCHECK(context); auto* profile = Profile::FromBrowserContext(context); - return - static_cast( - profile->GetOriginalProfile()->GetUserData(UserDataKey())); + return static_cast( + profile->GetOriginalProfile()->GetUserData(UserDataKey())); } // static @@ -108,7 +110,7 @@ const ParentProfileData* ParentProfileData::FromProfile( // static const ParentProfileData* ParentProfileData::FromPath( - const base::FilePath& path) { + const base::FilePath& path) { auto* profile = GetFromPath(path); DCHECK(profile); return FromProfile(profile); @@ -118,8 +120,7 @@ Profile* ParentProfileData::profile() const { return profile_; } -std::unique_ptr -ParentProfileData::Clone() { +std::unique_ptr ParentProfileData::Clone() { return base::WrapUnique(new ParentProfileData(profile_)); } @@ -128,9 +129,7 @@ const void* ParentProfileData::UserDataKey() { } ParentProfileData::ParentProfileData(Profile* profile) - : profile_(profile), - path_(profile->GetPath()) { -} + : profile_(profile), path_(profile->GetPath()) {} ParentProfileData::~ParentProfileData() { GetPathMap()->erase(path_); @@ -191,7 +190,9 @@ Profile* GetParentProfile(const base::FilePath& path) { bool IsGuestProfile(content::BrowserContext* context) { DCHECK(context); - return Profile::FromBrowserContext(context)->GetOriginalProfile()->IsGuestSession(); + return Profile::FromBrowserContext(context) + ->GetOriginalProfile() + ->IsGuestSession(); } } // namespace brave @@ -202,7 +203,7 @@ namespace chrome { // GetBrowserContextRedirectedInIncognito or equivalent content::BrowserContext* GetBrowserContextRedirectedInIncognitoOverride( content::BrowserContext* context) { - if (brave::IsSessionProfile(context)) \ + if (brave::IsSessionProfile(context)) context = brave::GetParentProfile(context); return chrome::GetBrowserContextRedirectedInIncognito(context); } diff --git a/browser/profiles/profile_util.h b/browser/profiles/profile_util.h index 0352c8d6b775..8987ea3cba22 100644 --- a/browser/profiles/profile_util.h +++ b/browser/profiles/profile_util.h @@ -56,11 +56,11 @@ content::BrowserContext* GetBrowserContextRedirectedInIncognitoOverride( // Get the correct profile for keyed services that do NOT use // GetBrowserContextRedirectedInIncognito or equivalent -#define BRAVE_GET_BROWSER_CONTEXT_TO_USE_WITHOUT_REDIRECT \ - if (brave::IsSessionProfile(context)) { \ - auto* parent = brave::GetParentProfile(context); \ - context = context->IsOffTheRecord() ? parent->GetOffTheRecordProfile() \ - : parent; \ +#define BRAVE_GET_BROWSER_CONTEXT_TO_USE_WITHOUT_REDIRECT \ + if (brave::IsSessionProfile(context)) { \ + auto* parent = brave::GetParentProfile(context); \ + context = \ + context->IsOffTheRecord() ? parent->GetOffTheRecordProfile() : parent; \ } #endif // BRAVE_BROWSER_PROFILES_PROFILE_UTIL_H_ diff --git a/browser/profiles/tor_unittest_profile_manager.cc b/browser/profiles/tor_unittest_profile_manager.cc index 49269f66984d..630b9e879c31 100644 --- a/browser/profiles/tor_unittest_profile_manager.cc +++ b/browser/profiles/tor_unittest_profile_manager.cc @@ -51,6 +51,6 @@ void TorUnittestProfileManager::InitProfileUserPrefs(Profile* profile) { std::unique_ptr TorUnittestProfileManager::CreateProfile( const base::FilePath& path, Delegate* delegate) { - return std::unique_ptr(new BraveTestingProfile( - path, delegate)); + return std::unique_ptr( + new BraveTestingProfile(path, delegate)); } diff --git a/browser/search_engines/search_engine_provider_service_browsertest.cc b/browser/search_engines/search_engine_provider_service_browsertest.cc index a94ef01125ea..2ea9db22d162 100644 --- a/browser/search_engines/search_engine_provider_service_browsertest.cc +++ b/browser/search_engines/search_engine_provider_service_browsertest.cc @@ -186,9 +186,10 @@ IN_PROC_BROWSER_TEST_F(SearchEngineProviderServiceTest, Profile* tor_profile = BrowserList::GetInstance()->GetLastActive()->profile(); EXPECT_TRUE(brave::IsTorProfile(tor_profile)); - int default_provider_id = brave::IsRegionForQwant(tor_profile) ? - TemplateURLPrepopulateData::PREPOPULATED_ENGINE_ID_QWANT : - TemplateURLPrepopulateData::PREPOPULATED_ENGINE_ID_DUCKDUCKGO; + int default_provider_id = + brave::IsRegionForQwant(tor_profile) + ? TemplateURLPrepopulateData::PREPOPULATED_ENGINE_ID_QWANT + : TemplateURLPrepopulateData::PREPOPULATED_ENGINE_ID_DUCKDUCKGO; auto* service = TemplateURLServiceFactory::GetForProfile(tor_profile); EXPECT_EQ(service->GetDefaultSearchProvider()->data().prepopulate_id, default_provider_id); diff --git a/chromium_src/chrome/browser/bookmarks/bookmark_model_factory.cc b/chromium_src/chrome/browser/bookmarks/bookmark_model_factory.cc index f72a71fc1122..fe8a987253dc 100644 --- a/chromium_src/chrome/browser/bookmarks/bookmark_model_factory.cc +++ b/chromium_src/chrome/browser/bookmarks/bookmark_model_factory.cc @@ -5,5 +5,6 @@ #include "brave/browser/profiles/profile_util.h" -#define GetBrowserContextRedirectedInIncognito GetBrowserContextRedirectedInIncognitoOverride +#define GetBrowserContextRedirectedInIncognito \ + GetBrowserContextRedirectedInIncognitoOverride #include "../../../../../chrome/browser/bookmarks/bookmark_model_factory.cc" diff --git a/chromium_src/chrome/browser/bookmarks/managed_bookmark_service_factory.cc b/chromium_src/chrome/browser/bookmarks/managed_bookmark_service_factory.cc index 1050fa834455..13257b3aea95 100644 --- a/chromium_src/chrome/browser/bookmarks/managed_bookmark_service_factory.cc +++ b/chromium_src/chrome/browser/bookmarks/managed_bookmark_service_factory.cc @@ -5,5 +5,6 @@ #include "brave/browser/profiles/profile_util.h" -#define GetBrowserContextRedirectedInIncognito GetBrowserContextRedirectedInIncognitoOverride +#define GetBrowserContextRedirectedInIncognito \ + GetBrowserContextRedirectedInIncognitoOverride #include "../../../../../chrome/browser/bookmarks/managed_bookmark_service_factory.cc" diff --git a/chromium_src/chrome/browser/extensions/chrome_extensions_browser_client.cc b/chromium_src/chrome/browser/extensions/chrome_extensions_browser_client.cc index 54f99019c063..cef152248b80 100644 --- a/chromium_src/chrome/browser/extensions/chrome_extensions_browser_client.cc +++ b/chromium_src/chrome/browser/extensions/chrome_extensions_browser_client.cc @@ -8,7 +8,8 @@ #include "brave/browser/extensions/updater/brave_update_client_config.h" #include "chrome/browser/extensions/updater/chrome_update_client_config.h" -#define ChromeComponentExtensionResourceManager BraveComponentExtensionResourceManager // NOLINT +#define ChromeComponentExtensionResourceManager \ + BraveComponentExtensionResourceManager // NOLINT #define ChromeExtensionsAPIClient BraveExtensionsAPIClient #define ChromeUpdateClientConfig BraveUpdateClientConfig #include "../../../../../chrome/browser/extensions/chrome_extensions_browser_client.cc" // NOLINT 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 a123da729e11..545f1cb7bf9f 100644 --- a/chromium_src/chrome/browser/profiles/pref_service_builder_utils.cc +++ b/chromium_src/chrome/browser/profiles/pref_service_builder_utils.cc @@ -84,8 +84,7 @@ std::unique_ptr CreatePrefService( true); #endif return CreateIncognitoPrefServiceSyncable( - PrefServiceSyncableFromProfile(original_profile), - extension_pref_store, + PrefServiceSyncableFromProfile(original_profile), extension_pref_store, InProcessPrefServiceFactoryFactory::GetInstanceForKey(key) ->CreateDelegate()); } diff --git a/chromium_src/chrome/browser/profiles/profile_impl.h b/chromium_src/chrome/browser/profiles/profile_impl.h index dadb9ce645cd..af85c31c5bb0 100644 --- a/chromium_src/chrome/browser/profiles/profile_impl.h +++ b/chromium_src/chrome/browser/profiles/profile_impl.h @@ -6,10 +6,10 @@ #ifndef BRAVE_CHROMIUM_SRC_CHROME_BROWSER_PROFILES_PROFILE_IMPL_H_ #define BRAVE_CHROMIUM_SRC_CHROME_BROWSER_PROFILES_PROFILE_IMPL_H_ -#define BRAVE_PROFILE_IMPL_H_ \ - private: \ - friend class BraveProfileImpl; \ - \ +#define BRAVE_PROFILE_IMPL_H_ \ + private: \ + friend class BraveProfileImpl; \ + \ public: // define BRAVE_PROFILE_IMPL_H_ diff --git a/chromium_src/chrome/browser/supervised_user/supervised_user_settings_service_factory.cc b/chromium_src/chrome/browser/supervised_user/supervised_user_settings_service_factory.cc index a28f49129450..e5bdd5a2bb88 100644 --- a/chromium_src/chrome/browser/supervised_user/supervised_user_settings_service_factory.cc +++ b/chromium_src/chrome/browser/supervised_user/supervised_user_settings_service_factory.cc @@ -8,8 +8,8 @@ // Use the same SupervisedUserSettingsService, which handles a part of // preferences, as its parent profile. Chromium's incognito profile also shares // it with its original profile. -#define BRAVE_GET_KEY_TO_USE \ - if (brave::IsSessionProfilePath(key->GetPath())) { \ +#define BRAVE_GET_KEY_TO_USE \ + if (brave::IsSessionProfilePath(key->GetPath())) { \ return brave::GetParentProfile(key->GetPath())->GetProfileKey(); \ } diff --git a/chromium_src/chrome/browser/ui/browser_navigator.cc b/chromium_src/chrome/browser/ui/browser_navigator.cc index 06e12bc19cca..f2c4e700fed3 100644 --- a/chromium_src/chrome/browser/ui/browser_navigator.cc +++ b/chromium_src/chrome/browser/ui/browser_navigator.cc @@ -41,13 +41,13 @@ void MaybeHandleInParent(NavigateParams* params, bool allow_in_incognito) { auto* profile = params->initiating_profile; if (brave::IsSessionProfile(profile)) { if (!allow_in_incognito) { - params->initiating_profile = profile->IsOffTheRecord() - ? brave::GetParentProfile(profile)->GetOffTheRecordProfile() - : brave::GetParentProfile(profile); + params->initiating_profile = + profile->IsOffTheRecord() + ? brave::GetParentProfile(profile)->GetOffTheRecordProfile() + : brave::GetParentProfile(profile); } else if (HandleURLInParent(params, profile)) { params->browser = BraveGetOrCreateBrowser( - brave::GetParentProfile(profile), - params->user_gesture); + brave::GetParentProfile(profile), params->user_gesture); } } } @@ -64,10 +64,10 @@ bool IsHostAllowedInIncognitoBraveImpl(const base::StringPiece& host) { } // namespace -#define BRAVE_ADJUST_NAVIGATE_PARAMS_FOR_URL \ - UpdateBraveScheme(params); \ - MaybeHandleInParent(params, \ - IsURLAllowedInIncognito(params->url, params->initiating_profile)); +#define BRAVE_ADJUST_NAVIGATE_PARAMS_FOR_URL \ + UpdateBraveScheme(params); \ + MaybeHandleInParent(params, IsURLAllowedInIncognito( \ + params->url, params->initiating_profile)); #include "../../../../../chrome/browser/ui/browser_navigator.cc" #undef BRAVE_ADJUST_NAVIGATE_PARAMS_FOR_URL diff --git a/chromium_src/chrome/test/base/testing_profile.cc b/chromium_src/chrome/test/base/testing_profile.cc index a90d4c0ea7cc..4b4f30f19c74 100644 --- a/chromium_src/chrome/test/base/testing_profile.cc +++ b/chromium_src/chrome/test/base/testing_profile.cc @@ -5,10 +5,10 @@ #include "brave/browser/profiles/profile_util.h" -#define BRAVE_INIT \ - if (brave::IsSessionProfilePath(GetPath())) { \ - brave::CreateParentProfileData(this); \ - } else +#define BRAVE_INIT \ + if (brave::IsSessionProfilePath(GetPath())) \ + brave::CreateParentProfileData(this); \ + else #include "../../../../../chrome/test/base/testing_profile.cc" #undef BRAVE_INIT diff --git a/common/tor/pref_names.cc b/common/tor/pref_names.cc index eeb88ec50a02..45792fddd1e5 100644 --- a/common/tor/pref_names.cc +++ b/common/tor/pref_names.cc @@ -1,4 +1,5 @@ -/* This Source Code Form is subject to the terms of the Mozilla Public +/* 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/. */ diff --git a/common/tor/pref_names.h b/common/tor/pref_names.h index d91b99a85bb4..81a8c74e186a 100644 --- a/common/tor/pref_names.h +++ b/common/tor/pref_names.h @@ -1,4 +1,5 @@ -/* This Source Code Form is subject to the terms of the Mozilla Public +/* 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/. */ diff --git a/extensions/browser/brave_extensions_browser_client.cc b/extensions/browser/brave_extensions_browser_client.cc index e028aae0a675..7cc9607da7dd 100644 --- a/extensions/browser/brave_extensions_browser_client.cc +++ b/extensions/browser/brave_extensions_browser_client.cc @@ -26,4 +26,4 @@ void BraveExtensionsBrowserClient::Set(BraveExtensionsBrowserClient* client) { g_brave_extension_browser_client = client; } -} // namespace exceptions +} // namespace extensions diff --git a/test/base/brave_testing_profile.cc b/test/base/brave_testing_profile.cc index c5f4a79f0939..870fb57a02ea 100644 --- a/test/base/brave_testing_profile.cc +++ b/test/base/brave_testing_profile.cc @@ -7,10 +7,10 @@ #include "brave/browser/profiles/profile_util.h" -BraveTestingProfile::BraveTestingProfile( - const base::FilePath& path, Delegate* delegate) - : TestingProfile(path, delegate) { +BraveTestingProfile::BraveTestingProfile(const base::FilePath& path, + Delegate* delegate) + : TestingProfile(path, delegate) { if (brave::IsSessionProfilePath(path)) { - brave::CreateParentProfileData(this); + brave::CreateParentProfileData(this); } } diff --git a/test/base/brave_testing_profile.h b/test/base/brave_testing_profile.h index 38eb27f43a96..aa9a78180ca9 100644 --- a/test/base/brave_testing_profile.h +++ b/test/base/brave_testing_profile.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_TEST_BASE_TESTING_PROFILE_H_ -#define BRAVE_TEST_BASE_TESTING_PROFILE_H_ +#ifndef BRAVE_TEST_BASE_BRAVE_TESTING_PROFILE_H_ +#define BRAVE_TEST_BASE_BRAVE_TESTING_PROFILE_H_ #include "chrome/test/base/testing_profile.h" @@ -14,4 +14,4 @@ class BraveTestingProfile : public TestingProfile { ~BraveTestingProfile() override = default; }; -#endif // BRAVE_TEST_BASE_TESTING_PROFILE_H_ +#endif // BRAVE_TEST_BASE_BRAVE_TESTING_PROFILE_H_ From 83fce342691805f5283c156b4551627af6170df0 Mon Sep 17 00:00:00 2001 From: Jocelyn Liu Date: Tue, 1 Oct 2019 23:52:37 -0700 Subject: [PATCH 12/16] Fix Android build --- .../chrome/browser/profiles/pref_service_builder_utils.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 545f1cb7bf9f..b80aaab40a3c 100644 --- a/chromium_src/chrome/browser/profiles/pref_service_builder_utils.cc +++ b/chromium_src/chrome/browser/profiles/pref_service_builder_utils.cc @@ -77,7 +77,7 @@ std::unique_ptr CreatePrefService( Profile* original_profile = g_browser_process->profile_manager()->GetProfileByPath(original_path); DCHECK(original_profile); - PrefStore* extension_pref_store; + PrefStore* extension_pref_store = nullptr; #if BUILDFLAG(ENABLE_EXTENSIONS) extension_pref_store = new ExtensionPrefStore( ExtensionPrefValueMapFactory::GetForBrowserContext(original_profile), From 2a4daf4cbfcebd1c1394f51d33a966681bd9c08e Mon Sep 17 00:00:00 2001 From: Jocelyn Liu Date: Wed, 2 Oct 2019 11:35:28 -0700 Subject: [PATCH 13/16] Fix BraveProfileManagerTest.ProxyConfigMonitorInTorProfile crash in linux. --- browser/tor/tor_profile_service_impl.cc | 1 + 1 file changed, 1 insertion(+) diff --git a/browser/tor/tor_profile_service_impl.cc b/browser/tor/tor_profile_service_impl.cc index b45605d1f336..8311d9c98c44 100644 --- a/browser/tor/tor_profile_service_impl.cc +++ b/browser/tor/tor_profile_service_impl.cc @@ -120,6 +120,7 @@ void OnNewTorCircuit(std::unique_ptr tracker, TorProfileServiceImpl::TorProfileServiceImpl(Profile* profile) : profile_(profile), + 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. From 2861828de24c4ffff1c20e8535ed75e0409d63d7 Mon Sep 17 00:00:00 2001 From: Jocelyn Liu Date: Wed, 2 Oct 2019 13:21:38 -0700 Subject: [PATCH 14/16] Add AreExtensionsDisabled override to always return true for Tor profiles. --- .../extensions/brave_extensions_browser_client_impl.cc | 10 ++++++++++ .../extensions/brave_extensions_browser_client_impl.h | 3 +++ browser/profiles/brave_profile_manager.cc | 4 ++++ 3 files changed, 17 insertions(+) diff --git a/browser/extensions/brave_extensions_browser_client_impl.cc b/browser/extensions/brave_extensions_browser_client_impl.cc index c3f1247d93d2..216e7ee53dca 100644 --- a/browser/extensions/brave_extensions_browser_client_impl.cc +++ b/browser/extensions/brave_extensions_browser_client_impl.cc @@ -18,4 +18,14 @@ BraveExtensionsBrowserClientImpl::BraveExtensionsBrowserClientImpl() { AddAPIProvider(std::make_unique()); } +bool BraveExtensionsBrowserClientImpl::AreExtensionsDisabled( + const base::CommandLine& command_line, + content::BrowserContext* context) { + if (brave::IsTorProfile(context)) + return true; + + return ChromeExtensionsBrowserClient::AreExtensionsDisabled(command_line, + context); +} + } // namespace extensions diff --git a/browser/extensions/brave_extensions_browser_client_impl.h b/browser/extensions/brave_extensions_browser_client_impl.h index 4d872d2a2781..6aea56be97ee 100644 --- a/browser/extensions/brave_extensions_browser_client_impl.h +++ b/browser/extensions/brave_extensions_browser_client_impl.h @@ -17,6 +17,9 @@ class BraveExtensionsBrowserClientImpl : public ChromeExtensionsBrowserClient, BraveExtensionsBrowserClientImpl(); ~BraveExtensionsBrowserClientImpl() override = default; + bool AreExtensionsDisabled(const base::CommandLine& command_line, + content::BrowserContext* context) override; + DISALLOW_COPY_AND_ASSIGN(BraveExtensionsBrowserClientImpl); }; diff --git a/browser/profiles/brave_profile_manager.cc b/browser/profiles/brave_profile_manager.cc index 4f0a95aa0cc5..94352b6a39ce 100644 --- a/browser/profiles/brave_profile_manager.cc +++ b/browser/profiles/brave_profile_manager.cc @@ -149,6 +149,10 @@ void BraveProfileManager::OnProfileCreated(Profile* profile, } #if BUILDFLAG(ENABLE_EXTENSIONS) + // This is added in addition to our AreExtensionsDisabled override in + // brave_extensions_browser_client_impl because there were extension + // icons briefly showing up when opening Tor windows with that override + // only. extensions::ExtensionService* extension_service = extensions::ExtensionSystem::Get(profile)->extension_service(); extension_service->BlockAllExtensions(); From 1db55054ae1f9853705871d576c9973f184f8c4e Mon Sep 17 00:00:00 2001 From: Jocelyn Liu Date: Wed, 2 Oct 2019 13:30:14 -0700 Subject: [PATCH 15/16] fix auto-merge error --- browser/brave_browser_main_parts.cc | 37 ++++++++++++++--------------- browser/brave_browser_main_parts.h | 1 - 2 files changed, 18 insertions(+), 20 deletions(-) diff --git a/browser/brave_browser_main_parts.cc b/browser/brave_browser_main_parts.cc index 7bc7a22fc2f7..8fdd96de4728 100644 --- a/browser/brave_browser_main_parts.cc +++ b/browser/brave_browser_main_parts.cc @@ -11,7 +11,6 @@ #if BUILDFLAG(ENABLE_TOR) #include "base/files/file_util.h" #include "brave/common/tor/tor_constants.h" -#include "chrome/browser/browser_process.h" #include "chrome/browser/browser_process_impl.h" #include "chrome/browser/profiles/profile_manager.h" #include "chrome/browser/profiles/profile_metrics.h" @@ -20,13 +19,30 @@ #if !defined(OS_ANDROID) #include "brave/browser/infobars/brave_confirm_p3a_infobar_delegate.h" #include "chrome/browser/ui/browser_finder.h" -#include "chrome/browser/browser_process.h" #include "chrome/browser/infobars/infobar_service.h" #include "content/public/browser/web_contents.h" #endif +#if BUILDFLAG(ENABLE_TOR) || !defined(OS_ANDROID) +#include "chrome/browser/browser_process.h" +#endif + void BraveBrowserMainParts::PostBrowserStart() { ChromeBrowserMainParts::PostBrowserStart(); + +#if BUILDFLAG(ENABLE_TOR) + ProfileManager* profile_manager = g_browser_process->profile_manager(); + base::FilePath tor_legacy_path = + profile_manager->user_data_dir().Append(tor::kTorProfileDir); + + // Delete Tor legacy profile if exists. + if (base::PathExists(tor_legacy_path)) { + profile_manager->MaybeScheduleProfileForDeletion( + tor_legacy_path, base::DoNothing(), + ProfileMetrics::DELETE_PROFILE_SETTINGS); + } +#endif + #if !defined(OS_ANDROID) Browser* browser = chrome::FindLastActive(); content::WebContents* active_web_contents = nullptr; @@ -50,20 +66,3 @@ void BraveBrowserMainParts::PostBrowserStart() { void BraveBrowserMainParts::PreShutdown() { content::BraveClearBrowsingData::ClearOnExit(); } - -void BraveBrowserMainParts::PostBrowserStart() { - ChromeBrowserMainParts::PostBrowserStart(); - -#if BUILDFLAG(ENABLE_TOR) - ProfileManager* profile_manager = g_browser_process->profile_manager(); - base::FilePath tor_legacy_path = - profile_manager->user_data_dir().Append(tor::kTorProfileDir); - - // Delete Tor legacy profile if exists. - if (base::PathExists(tor_legacy_path)) { - profile_manager->MaybeScheduleProfileForDeletion( - tor_legacy_path, base::DoNothing(), - ProfileMetrics::DELETE_PROFILE_SETTINGS); - } -#endif -} diff --git a/browser/brave_browser_main_parts.h b/browser/brave_browser_main_parts.h index ff739da16708..01a049bebb69 100644 --- a/browser/brave_browser_main_parts.h +++ b/browser/brave_browser_main_parts.h @@ -16,7 +16,6 @@ class BraveBrowserMainParts : public ChromeBrowserMainParts { void PostBrowserStart() override; void PreShutdown() override; - void PostBrowserStart() override; private: DISALLOW_COPY_AND_ASSIGN(BraveBrowserMainParts); From b1100ba07d8d4dd0e86bdbceb0c9129a6762b4dc Mon Sep 17 00:00:00 2001 From: Jocelyn Liu Date: Wed, 2 Oct 2019 15:30:03 -0700 Subject: [PATCH 16/16] test fixup --- browser/profiles/brave_profile_manager_browsertest.cc | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/browser/profiles/brave_profile_manager_browsertest.cc b/browser/profiles/brave_profile_manager_browsertest.cc index c1368e880bc4..aba66d94ea8b 100644 --- a/browser/profiles/brave_profile_manager_browsertest.cc +++ b/browser/profiles/brave_profile_manager_browsertest.cc @@ -333,7 +333,7 @@ class BraveProfileManagerExtensionTest }; IN_PROC_BROWSER_TEST_F(BraveProfileManagerExtensionTest, - PRE_SwitchToTorProfileBlockExtensions) { + PRE_SwitchToTorProfileDisableExtensions) { ScopedTorLaunchPreventerForTest prevent_tor_process; ProfileManager* profile_manager = g_browser_process->profile_manager(); ASSERT_TRUE(profile_manager); @@ -350,7 +350,7 @@ IN_PROC_BROWSER_TEST_F(BraveProfileManagerExtensionTest, } IN_PROC_BROWSER_TEST_F(BraveProfileManagerExtensionTest, - SwitchToTorProfileBlockExtensions) { + SwitchToTorProfileDisableExtensions) { ScopedTorLaunchPreventerForTest prevent_tor_process; ProfileManager* profile_manager = g_browser_process->profile_manager(); ASSERT_TRUE(profile_manager); @@ -365,12 +365,12 @@ IN_PROC_BROWSER_TEST_F(BraveProfileManagerExtensionTest, const extensions::Extension* extension = GetExtension(parent_profile); const std::string id = extension->id(); - // Extension should be blocked in Tor + // The installed extension should not be accessible in Tor. EXPECT_TRUE(extensions::util::IsIncognitoEnabled(id, parent_profile)); extensions::ExtensionRegistry* tor_registry = extensions::ExtensionRegistry::Get(tor_profile); - EXPECT_TRUE(tor_registry->GetExtensionById( - id, extensions::ExtensionRegistry::BLOCKED)); + EXPECT_FALSE(tor_registry->GetExtensionById( + id, extensions::ExtensionRegistry::EVERYTHING)); } #endif #endif