From 41bf74fd379462cec5247c73672a956ae9784298 Mon Sep 17 00:00:00 2001 From: Emerick Rogul Date: Fri, 13 Nov 2020 08:22:25 -0500 Subject: [PATCH] Store Greaselion extensions in user data dir and delete on exit --- .../greaselion/greaselion_service_factory.cc | 11 ++++------ .../browser/greaselion_service_impl.cc | 21 ++++++++++++------- .../browser/greaselion_service_impl.h | 3 +++ 3 files changed, 20 insertions(+), 15 deletions(-) diff --git a/browser/greaselion/greaselion_service_factory.cc b/browser/greaselion/greaselion_service_factory.cc index 32624d1819c5..cac29b19ed1e 100644 --- a/browser/greaselion/greaselion_service_factory.cc +++ b/browser/greaselion/greaselion_service_factory.cc @@ -9,10 +9,11 @@ #include #include "base/memory/singleton.h" +#include "base/path_service.h" #include "brave/browser/brave_browser_process_impl.h" #include "brave/components/greaselion/browser/greaselion_service.h" #include "brave/components/greaselion/browser/greaselion_service_impl.h" -#include "chrome/browser/extensions/extension_service.h" +#include "chrome/common/chrome_paths.h" #include "components/keyed_service/content/browser_context_dependency_manager.h" #include "components/keyed_service/core/keyed_service.h" #include "extensions/browser/extension_file_task_runner.h" @@ -51,15 +52,11 @@ KeyedService* GreaselionServiceFactory::BuildServiceInstanceFor( extensions::ExtensionSystem* extension_system = extensions::ExtensionSystem::Get(context); extension_system->InitForRegularProfile(true /* extensions_enabled */); - extensions::ExtensionService* extension_service = - extension_system->extension_service(); extensions::ExtensionRegistry* extension_registry = extensions::ExtensionRegistry::Get(context); base::FilePath install_directory; - // Extension service may be null even after calling InitForRegularProfile if - // we are being created within a unit test. - if (extension_service) - install_directory = extension_service->install_directory(); + base::PathService::Get(chrome::DIR_USER_DATA, &install_directory); + install_directory = install_directory.AppendASCII("Greaselion"); scoped_refptr task_runner = extensions::GetExtensionFileTaskRunner(); greaselion::GreaselionDownloadService* download_service = nullptr; diff --git a/components/greaselion/browser/greaselion_service_impl.cc b/components/greaselion/browser/greaselion_service_impl.cc index 121a8cb908d0..df1f9edf45a3 100644 --- a/components/greaselion/browser/greaselion_service_impl.cc +++ b/components/greaselion/browser/greaselion_service_impl.cc @@ -18,7 +18,6 @@ #include "base/feature_list.h" #include "base/files/file_path.h" #include "base/files/file_util.h" -#include "base/files/scoped_temp_dir.h" #include "base/json/json_file_value_serializer.h" #include "base/one_shot_event.h" #include "base/sequenced_task_runner.h" @@ -48,18 +47,19 @@ using extensions::Manifest; namespace { -// Wraps a Greaselion rule in a component. The component is stored as an -// unpacked extension in the system temp dir. Returns a valid extension that the -// caller should take ownership of, or nullptr. +// Wraps a Greaselion rule in a component. The component is stored as +// an unpacked extension in the user data dir. Returns a valid +// extension that the caller should take ownership of, or nullptr. // // NOTE: This function does file IO and should not be called on the UI thread. // NOTE: The caller takes ownership of the directory at extension->path() on the // returned object. scoped_refptr ConvertGreaselionRuleToExtensionOnTaskRunner( greaselion::GreaselionRule* rule, - const base::FilePath& extensions_dir) { + const base::FilePath& install_dir, + std::vector* extension_dirs) { base::FilePath install_temp_dir = - extensions::file_util::GetInstallTempDir(extensions_dir); + extensions::file_util::GetInstallTempDir(install_dir); if (install_temp_dir.empty()) { LOG(ERROR) << "Could not get path to profile temp directory"; return nullptr; @@ -175,7 +175,12 @@ scoped_refptr ConvertGreaselionRuleToExtensionOnTaskRunner( return nullptr; } - temp_dir.Take(); // The caller takes ownership of the directory. + // Take ownership of this temporary directory so it's deleted when + // the service exits + if (extension_dirs) { + extension_dirs->push_back(std::move(temp_dir)); + } + return extension; } } // namespace @@ -274,7 +279,7 @@ void GreaselionServiceImpl::CreateAndInstallExtensions() { base::PostTaskAndReplyWithResult( task_runner_.get(), FROM_HERE, base::BindOnce(&ConvertGreaselionRuleToExtensionOnTaskRunner, - rule.get(), install_directory_), + rule.get(), install_directory_, &extension_dirs_), base::BindOnce(&GreaselionServiceImpl::PostConvert, weak_factory_.GetWeakPtr())); } diff --git a/components/greaselion/browser/greaselion_service_impl.h b/components/greaselion/browser/greaselion_service_impl.h index e4f17108c9ae..8c385dafe103 100644 --- a/components/greaselion/browser/greaselion_service_impl.h +++ b/components/greaselion/browser/greaselion_service_impl.h @@ -11,8 +11,10 @@ #include #include "base/files/file_path.h" +#include "base/files/scoped_temp_dir.h" #include "base/macros.h" #include "base/memory/weak_ptr.h" +#include "base/path_service.h" #include "base/version.h" #include "brave/components/greaselion/browser/greaselion_service.h" #include "extensions/common/extension_id.h" @@ -79,6 +81,7 @@ class GreaselionServiceImpl : public GreaselionService { scoped_refptr task_runner_; base::ObserverList observers_; std::vector greaselion_extensions_; + std::vector extension_dirs_; base::Version browser_version_; base::WeakPtrFactory weak_factory_;