From 2991d545f2bea055b81edd4602c752e6bc7f96b5 Mon Sep 17 00:00:00 2001 From: Jocelyn Liu Date: Tue, 23 Apr 2019 19:10:50 -0700 Subject: [PATCH] Update ipfs pref when extension loaded or unloaded --- browser/extensions/BUILD.gn | 3 +++ .../extensions/brave_extension_management.cc | 25 ++++++++++++++++++- .../extensions/brave_extension_management.h | 20 ++++++++++++++- 3 files changed, 46 insertions(+), 2 deletions(-) diff --git a/browser/extensions/BUILD.gn b/browser/extensions/BUILD.gn index f782b2117177..57aedb616a7a 100644 --- a/browser/extensions/BUILD.gn +++ b/browser/extensions/BUILD.gn @@ -42,6 +42,7 @@ source_set("extensions") { deps = [ "//base", + "//brave/common:pref_names", "//brave/common/extensions/api", "//brave/components/brave_extension:static_resources", "//brave/components/brave_extension:generated_resources", @@ -53,7 +54,9 @@ source_set("extensions") { "//brave/components/brave_webtorrent:generated_resources", "//brave/components/brave_webtorrent:static_resources", "//chrome/browser", + "//components/prefs", "//content/public/browser", "//extensions/browser", + "//extensions/common", ] } diff --git a/browser/extensions/brave_extension_management.cc b/browser/extensions/brave_extension_management.cc index 736ac83696a3..1ad23174b665 100644 --- a/browser/extensions/brave_extension_management.cc +++ b/browser/extensions/brave_extension_management.cc @@ -8,15 +8,23 @@ #include "brave/browser/brave_browser_process_impl.h" #include "brave/common/brave_switches.h" #include "brave/common/extensions/extension_constants.h" +#include "brave/common/pref_names.h" #include "brave/browser/extensions/brave_extension_provider.h" #include "brave/browser/extensions/brave_tor_client_updater.h" #include "chrome/browser/extensions/external_policy_loader.h" +#include "chrome/browser/profiles/profile.h" +#include "components/prefs/pref_service.h" +#include "extensions/browser/extension_registry.h" +#include "extensions/common/extension.h" #include "extensions/common/extension_urls.h" namespace extensions { BraveExtensionManagement::BraveExtensionManagement(Profile* profile) - : ExtensionManagement(profile) { + : ExtensionManagement(profile), + extension_registry_observer_(this) { + extension_registry_observer_.Add(ExtensionRegistry::Get( + static_cast(profile))); providers_.push_back( std::make_unique()); RegisterBraveExtensions(); @@ -32,4 +40,19 @@ void BraveExtensionManagement::RegisterBraveExtensions() { g_brave_browser_process->tor_client_updater()->Register(); } +void BraveExtensionManagement::OnExtensionLoaded( + content::BrowserContext* browser_context, + const Extension* extension) { + if (extension->id() == ipfs_companion_extension_id) + pref_service_->SetBoolean(kIPFSCompanionEnabled, true); +} + +void BraveExtensionManagement::OnExtensionUnloaded( + content::BrowserContext* browser_context, + const Extension* extension, + UnloadedExtensionReason reason) { + if (extension->id() == ipfs_companion_extension_id) + pref_service_->SetBoolean(kIPFSCompanionEnabled, false); +} + } // namespace extensions diff --git a/browser/extensions/brave_extension_management.h b/browser/extensions/brave_extension_management.h index d2035d5688e5..be8327ab8ce2 100644 --- a/browser/extensions/brave_extension_management.h +++ b/browser/extensions/brave_extension_management.h @@ -5,17 +5,35 @@ #ifndef BRAVE_BROWSER_EXTENSIONS_EXTENSION_MANAGEMENT_H_ #define BRAVE_BROWSER_EXTENSIONS_EXTENSION_MANAGEMENT_H_ +#include "base/scoped_observer.h" #include "chrome/browser/extensions/extension_management.h" +#include "extensions/browser/extension_registry_observer.h" namespace extensions { -class BraveExtensionManagement : public ExtensionManagement { +class ExtensionRegistry; + +class BraveExtensionManagement : public ExtensionManagement, + public ExtensionRegistryObserver { public: BraveExtensionManagement(Profile* profile); ~BraveExtensionManagement() override; private: void RegisterBraveExtensions(); + + // ExtensionRegistryObserver implementation. + void OnExtensionLoaded( + content::BrowserContext* browser_context, + const Extension* extension) override; + void OnExtensionUnloaded( + content::BrowserContext* browser_context, + const Extension* extension, + UnloadedExtensionReason reason) override; + + ScopedObserver + extension_registry_observer_; + DISALLOW_COPY_AND_ASSIGN(BraveExtensionManagement); };