Skip to content

Commit

Permalink
Disable Greaselion if Rewards is off (#25535)
Browse files Browse the repository at this point in the history
* Disable Greaselion if Rewards is off
* update deps
  • Loading branch information
atuchin-m authored Sep 12, 2024
1 parent 6b0800a commit 81199be
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 0 deletions.
1 change: 1 addition & 0 deletions browser/greaselion/greaselion_browsertest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ class GreaselionServiceTest : public BaseLocalDataFilesBrowserTest {
BaseLocalDataFilesBrowserTest::SetUpOnMainThread();
base::ScopedAllowBlockingForTesting allow_blocking;
response_->LoadMocks();
profile()->GetPrefs()->SetBoolean(brave_rewards::prefs::kEnabled, true);
}

// BaseLocalDataFilesBrowserTest overrides
Expand Down
24 changes: 24 additions & 0 deletions browser/greaselion/greaselion_service_factory.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@
#include "base/no_destructor.h"
#include "base/path_service.h"
#include "brave/browser/brave_browser_process.h"
#include "brave/components/brave_rewards/common/pref_names.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/browser/profiles/profile.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"
Expand All @@ -36,6 +38,19 @@ class GreaselionServiceDelegateImpl
content::BrowserContext* browser_context)
: browser_context_(browser_context) {
DCHECK(browser_context_);
pref_change_registrar_.Init(
Profile::FromBrowserContext(browser_context)->GetPrefs());
pref_change_registrar_.Add(
brave_rewards::prefs::kEnabled,
base::BindRepeating(
&GreaselionServiceDelegateImpl::UpdateGreaselionExtensions,
base::Unretained(this)));
}

bool IsEnabled() const override {
return Profile::FromBrowserContext(browser_context_)
->GetPrefs()
->GetBoolean(brave_rewards::prefs::kEnabled);
}

void AddExtension(extensions::Extension* extension) override {
Expand All @@ -56,7 +71,16 @@ class GreaselionServiceDelegateImpl
}

private:
void UpdateGreaselionExtensions() {
auto* service =
GreaselionServiceFactory::GetForBrowserContext(browser_context_);
if (service) {
service->UpdateInstalledExtensions();
}
}

raw_ptr<content::BrowserContext> browser_context_; // Not owned
PrefChangeRegistrar pref_change_registrar_;
};

} // namespace
Expand Down
2 changes: 2 additions & 0 deletions browser/greaselion/sources.gni
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ if (enable_greaselion) {

brave_browser_greaselion_deps += [
"//base",
"//brave/components/brave_rewards/common",
"//brave/components/greaselion/browser",
"//chrome/browser/profiles:profile",
"//chrome/common",
"//components/keyed_service/content",
"//extensions/browser",
Expand Down
1 change: 1 addition & 0 deletions components/greaselion/browser/greaselion_service.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class GreaselionService : public KeyedService,
class Delegate {
public:
virtual ~Delegate() = default;
virtual bool IsEnabled() const = 0;
virtual void AddExtension(extensions::Extension* extension) = 0;
virtual void UnloadExtension(const std::string& extension_id) = 0;
};
Expand Down
3 changes: 3 additions & 0 deletions components/greaselion/browser/greaselion_service_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,9 @@ GreaselionServiceImpl::GetExtensionIdsForTesting() {
}

void GreaselionServiceImpl::UpdateInstalledExtensions() {
if (!delegate_->IsEnabled()) {
return;
}
if (update_in_progress_) {
update_pending_ = true;
return;
Expand Down

0 comments on commit 81199be

Please sign in to comment.