Skip to content

Commit

Permalink
Merge pull request #7141 from brave/greaselion-localizations-race
Browse files Browse the repository at this point in the history
Store Greaselion extensions in user data dir and delete on exit
  • Loading branch information
emerick authored Nov 16, 2020
2 parents 6b3a9ba + 41bf74f commit 97e1f4f
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 15 deletions.
11 changes: 4 additions & 7 deletions browser/greaselion/greaselion_service_factory.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@
#include <string>

#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"
Expand Down Expand Up @@ -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<base::SequencedTaskRunner> task_runner =
extensions::GetExtensionFileTaskRunner();
greaselion::GreaselionDownloadService* download_service = nullptr;
Expand Down
21 changes: 13 additions & 8 deletions components/greaselion/browser/greaselion_service_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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<Extension> ConvertGreaselionRuleToExtensionOnTaskRunner(
greaselion::GreaselionRule* rule,
const base::FilePath& extensions_dir) {
const base::FilePath& install_dir,
std::vector<base::ScopedTempDir>* 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;
Expand Down Expand Up @@ -175,7 +175,12 @@ scoped_refptr<Extension> 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
Expand Down Expand Up @@ -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()));
}
Expand Down
3 changes: 3 additions & 0 deletions components/greaselion/browser/greaselion_service_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@
#include <vector>

#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"
Expand Down Expand Up @@ -79,6 +81,7 @@ class GreaselionServiceImpl : public GreaselionService {
scoped_refptr<base::SequencedTaskRunner> task_runner_;
base::ObserverList<Observer> observers_;
std::vector<extensions::ExtensionId> greaselion_extensions_;
std::vector<base::ScopedTempDir> extension_dirs_;
base::Version browser_version_;
base::WeakPtrFactory<GreaselionServiceImpl> weak_factory_;

Expand Down

0 comments on commit 97e1f4f

Please sign in to comment.