diff --git a/BUILD.gn b/BUILD.gn index 2626e9497428..02ce468e4647 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -17,6 +17,7 @@ group("child_dependencies") { group("browser_dependencies") { public_deps = [ "//brave/browser", + "//brave/extensions", "common", ":brave_framework_resources", "chromium_src:browser", diff --git a/browser/extensions/BUILD.gn b/browser/extensions/BUILD.gn index 9c1fcc0931cd..28aa14896911 100644 --- a/browser/extensions/BUILD.gn +++ b/browser/extensions/BUILD.gn @@ -4,6 +4,8 @@ source_set("extensions") { sources = [ "api/brave_shields_api.cc", "api/brave_shields_api.h", + "api/content_settings/brave_content_settings_store.cc", + "api/content_settings/brave_content_settings_store.h", "brave_component_extension_resource_manager.cc", "brave_component_extension_resource_manager.h", "brave_component_loader.cc", diff --git a/browser/extensions/api/content_settings/brave_content_settings_store.cc b/browser/extensions/api/content_settings/brave_content_settings_store.cc new file mode 100644 index 000000000000..98b60200a3a4 --- /dev/null +++ b/browser/extensions/api/content_settings/brave_content_settings_store.cc @@ -0,0 +1,46 @@ +/* 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/api/content_settings/brave_content_settings_store.h" + +namespace extensions { + +BraveContentSettingsStore::BraveContentSettingsStore() + : suppress_notifications_(false) { +} + +BraveContentSettingsStore::~BraveContentSettingsStore() { +} + +void BraveContentSettingsStore::SetExtensionContentSettingFromList( + const std::string& extension_id, + const base::ListValue* list, + ExtensionPrefsScope scope) { + if (list->GetList().empty()) return; + + // For SetExtensionContentSettingFromList use case, we delay the notification + // after the values in the list from extension_prefs are all stored in the + // content setting store. The delay of notification is needed because the + // values saved in extension_prefs will be overwritten by the one saved in + // content setting store in PreferenceAPI::OnContentSettingChanged. Without + // the delay, only the first entry in the list could be saved into content + // setting store. + suppress_notifications_ = true; + ContentSettingsStore::SetExtensionContentSettingFromList( + extension_id, list, scope); + suppress_notifications_ = false; + + // Send a single notification for the entire list. + NotifyOfContentSettingChanged(extension_id, + scope != kExtensionPrefsScopeRegular); +} + +void BraveContentSettingsStore::NotifyOfContentSettingChanged( + const std::string& extension_id, + bool incognito) { + if (suppress_notifications_) return; + ContentSettingsStore::NotifyOfContentSettingChanged(extension_id, incognito); +} + +} // namespace extensions diff --git a/browser/extensions/api/content_settings/brave_content_settings_store.h b/browser/extensions/api/content_settings/brave_content_settings_store.h new file mode 100644 index 000000000000..ecfab54711b8 --- /dev/null +++ b/browser/extensions/api/content_settings/brave_content_settings_store.h @@ -0,0 +1,39 @@ +/* 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_API_BREVE_CONTENT_SETTINGS_CONTENT_SETTINGS_STORE_H_ +#define BRAVE_BROWSER_EXTENSIONS_API_BREVE_CONTENT_SETTINGS_CONTENT_SETTINGS_STORE_H_ + +#include "chrome/browser/extensions/api/content_settings/content_settings_store.h" + +namespace extensions { + +// This class is the backend for extension-defined content settings. It is used +// by the content_settings::CustomExtensionProvider to integrate its settings +// into the HostContentSettingsMap and by the content settings extension API to +// provide extensions with access to content settings. +class BraveContentSettingsStore : public ContentSettingsStore { + public: + BraveContentSettingsStore(); + + // Deserializes content settings rules from |list| and applies them as set by + // the extension with ID |extension_id|. + void SetExtensionContentSettingFromList(const std::string& extension_id, + const base::ListValue* list, + ExtensionPrefsScope scope) override; + + void NotifyOfContentSettingChanged(const std::string& extension_id, + bool incognito) override; + private: + friend class base::RefCountedThreadSafe; + ~BraveContentSettingsStore() override; + + bool suppress_notifications_; + + DISALLOW_COPY_AND_ASSIGN(BraveContentSettingsStore); +}; + +} // namespace extensions + +#endif // BRAVE_BROWSER_EXTENSIONS_API_BREVE_CONTENT_SETTINGS_CONTENT_SETTINGS_STORE_H_ diff --git a/extensions/BUILD.gn b/extensions/BUILD.gn new file mode 100644 index 000000000000..8ddae52cea55 --- /dev/null +++ b/extensions/BUILD.gn @@ -0,0 +1,11 @@ + +source_set("extensions") { + sources = [ + "browser/brave_extension_prefs.cc", + "browser/brave_extension_prefs.h", + ] + + public_deps = [ + "//extensions/browser", + ] +} diff --git a/extensions/browser/brave_extension_prefs.cc b/extensions/browser/brave_extension_prefs.cc new file mode 100644 index 000000000000..c0fedd5e033a --- /dev/null +++ b/extensions/browser/brave_extension_prefs.cc @@ -0,0 +1,37 @@ +/* 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 "base/time/clock.h" +#include "brave/extensions/browser/brave_extension_prefs.h" +#include "extensions/browser/extension_prefs_observer.h" + +namespace extensions { + +BraveExtensionPrefs::BraveExtensionPrefs( + content::BrowserContext* browser_context, + PrefService* prefs, + const base::FilePath& root_dir, + ExtensionPrefValueMap* extension_pref_value_map, + std::unique_ptr clock, + bool extensions_disabled, + const std::vector& early_observers) + : ExtensionPrefs(browser_context, + prefs, + root_dir, + extension_pref_value_map, + std::move(clock), + extensions_disabled, + early_observers) { +} + +BraveExtensionPrefs::~BraveExtensionPrefs() { +} + +void BraveExtensionPrefs::NotifyExtensionPrefsLoaded( + const std::string& extension_id) { + for (auto& observer : observer_list_) + observer.OnExtensionPrefsLoaded(extension_id, this); +} + +} // namespace extensions diff --git a/extensions/browser/brave_extension_prefs.h b/extensions/browser/brave_extension_prefs.h new file mode 100644 index 000000000000..de71b6f5d7e3 --- /dev/null +++ b/extensions/browser/brave_extension_prefs.h @@ -0,0 +1,32 @@ +/* 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_BREVE_EXTENSION_PREFS_H_ +#define BRAVE_EXTENSIONS_BROWSER_BREVE_EXTENSION_PREFS_H_ + +#include "extensions/browser/extension_prefs.h" + +namespace extensions { + +class BraveExtensionPrefs : public ExtensionPrefs { + public: + BraveExtensionPrefs( + content::BrowserContext* browser_context, + PrefService* prefs, + const base::FilePath& root_dir, + ExtensionPrefValueMap* extension_pref_value_map, + std::unique_ptr clock, + bool extensions_disabled, + const std::vector& early_observers); + + ~BraveExtensionPrefs() override; + + void NotifyExtensionPrefsLoaded(const std::string& extension_id); + + DISALLOW_COPY_AND_ASSIGN(BraveExtensionPrefs); +}; + +} // namespace extensions + +#endif // BRAVE_EXTENSIONS_BROWSER_BREVE_EXTENSION_PREFS_H_ diff --git a/patches/chrome-browser-extensions-api-content_settings-content_settings_service.cc.patch b/patches/chrome-browser-extensions-api-content_settings-content_settings_service.cc.patch new file mode 100644 index 000000000000..4bc90187840f --- /dev/null +++ b/patches/chrome-browser-extensions-api-content_settings-content_settings_service.cc.patch @@ -0,0 +1,21 @@ +diff --git a/chrome/browser/extensions/api/content_settings/content_settings_service.cc b/chrome/browser/extensions/api/content_settings/content_settings_service.cc +index 83169fca6d7c243a0ea8289835c4e81038a395b4..7a2e6081363024ab67f4c39211514b9a7930459f 100644 +--- a/chrome/browser/extensions/api/content_settings/content_settings_service.cc ++++ b/chrome/browser/extensions/api/content_settings/content_settings_service.cc +@@ -5,6 +5,7 @@ + #include "chrome/browser/extensions/api/content_settings/content_settings_service.h" + + #include "base/lazy_instance.h" ++#include "brave/browser/extensions/api/content_settings/brave_content_settings_store.h" + #include "chrome/browser/extensions/api/content_settings/content_settings_store.h" + #include "extensions/browser/extension_prefs.h" + #include "extensions/browser/extension_prefs_scope.h" +@@ -13,7 +14,7 @@ + namespace extensions { + + ContentSettingsService::ContentSettingsService(content::BrowserContext* context) +- : content_settings_store_(new ContentSettingsStore()) {} ++ : content_settings_store_(new BraveContentSettingsStore()) {} + + ContentSettingsService::~ContentSettingsService() {} + diff --git a/patches/chrome-browser-extensions-api-content_settings-content_settings_store.h.patch b/patches/chrome-browser-extensions-api-content_settings-content_settings_store.h.patch new file mode 100644 index 000000000000..e2ba64914592 --- /dev/null +++ b/patches/chrome-browser-extensions-api-content_settings-content_settings_store.h.patch @@ -0,0 +1,30 @@ +diff --git a/chrome/browser/extensions/api/content_settings/content_settings_store.h b/chrome/browser/extensions/api/content_settings/content_settings_store.h +index 3a9033a4747026713e1016ec07743af0f6081ff5..f28ee9804d8f229004c2c3ec347755f0ab46390e 100644 +--- a/chrome/browser/extensions/api/content_settings/content_settings_store.h ++++ b/chrome/browser/extensions/api/content_settings/content_settings_store.h +@@ -86,7 +86,7 @@ class ContentSettingsStore + + // Deserializes content settings rules from |list| and applies them as set by + // the extension with ID |extension_id|. +- void SetExtensionContentSettingFromList(const std::string& extension_id, ++ virtual void SetExtensionContentSettingFromList(const std::string& extension_id, + const base::ListValue* list, + ExtensionPrefsScope scope); + +@@ -111,6 +111,7 @@ class ContentSettingsStore + void RemoveObserver(Observer* observer); + + private: ++ friend class BraveContentSettingsStore; + friend class base::RefCountedThreadSafe; + + struct ExtensionEntry; +@@ -129,7 +130,7 @@ class ContentSettingsStore + const std::string& ext_id, + ExtensionPrefsScope scope) const; + +- void NotifyOfContentSettingChanged(const std::string& extension_id, ++ virtual void NotifyOfContentSettingChanged(const std::string& extension_id, + bool incognito); + + bool OnCorrectThread(); diff --git a/patches/chrome-browser-extensions-extension_service.cc.patch b/patches/chrome-browser-extensions-extension_service.cc.patch index 9a893c7b3a93..cbf623d7349e 100644 --- a/patches/chrome-browser-extensions-extension_service.cc.patch +++ b/patches/chrome-browser-extensions-extension_service.cc.patch @@ -1,16 +1,17 @@ diff --git a/chrome/browser/extensions/extension_service.cc b/chrome/browser/extensions/extension_service.cc -index 9410c6daeabccba17d72244f2395c150aa84283a..9bece6bbc64ee01befe7ca8fb0a83a6f5e1d8ad8 100644 +index 9410c6daeabccba17d72244f2395c150aa84283a..417180bfe370c611a591a768d5fb5076bd7955ad 100644 --- a/chrome/browser/extensions/extension_service.cc +++ b/chrome/browser/extensions/extension_service.cc -@@ -28,6 +28,7 @@ +@@ -28,6 +28,8 @@ #include "base/threading/thread_task_runner_handle.h" #include "base/time/time.h" #include "base/trace_event/trace_event.h" +#include "brave/browser/extensions/brave_component_loader.h" ++#include "brave/extensions/browser/brave_extension_prefs.h" #include "build/build_config.h" #include "chrome/browser/browser_process.h" #include "chrome/browser/chrome_notification_types.h" -@@ -350,7 +351,7 @@ ExtensionService::ExtensionService(Profile* profile, +@@ -350,7 +352,7 @@ ExtensionService::ExtensionService(Profile* profile, } component_loader_.reset( @@ -19,7 +20,7 @@ index 9410c6daeabccba17d72244f2395c150aa84283a..9bece6bbc64ee01befe7ca8fb0a83a6f profile->GetPrefs(), g_browser_process->local_state(), profile)); -@@ -1374,6 +1375,19 @@ void ExtensionService::AddComponentExtension(const Extension* extension) { +@@ -1374,6 +1376,23 @@ void ExtensionService::AddComponentExtension(const Extension* extension) { } AddExtension(extension); @@ -35,6 +36,10 @@ index 9410c6daeabccba17d72244f2395c150aa84283a..9bece6bbc64ee01befe7ca8fb0a83a6f + extensions::kInstallFlagNone, + std::string(), + base::nullopt); ++ // Trigger ContentSettingsService::OnExtensionPrefsLoaded to populate pref ++ // values into content setting store. ++ static_cast(extension_prefs_) ++ ->NotifyExtensionPrefsLoaded(extension->id()); +#endif } diff --git a/patches/extensions-browser-extension_prefs.cc.patch b/patches/extensions-browser-extension_prefs.cc.patch new file mode 100644 index 000000000000..c08a5c69cb9e --- /dev/null +++ b/patches/extensions-browser-extension_prefs.cc.patch @@ -0,0 +1,21 @@ +diff --git a/extensions/browser/extension_prefs.cc b/extensions/browser/extension_prefs.cc +index cafc90ed44e2ff5d6c844fbea89798684909c268..7ad517ade16f4fcc6e2b9d8aabeadff10ea5c30b 100644 +--- a/extensions/browser/extension_prefs.cc ++++ b/extensions/browser/extension_prefs.cc +@@ -19,6 +19,7 @@ + #include "base/time/clock.h" + #include "base/time/default_clock.h" + #include "base/trace_event/trace_event.h" ++#include "brave/extensions/browser/brave_extension_prefs.h" + #include "build/build_config.h" + #include "components/crx_file/id_util.h" + #include "components/pref_registry/pref_registry_syncable.h" +@@ -347,7 +348,7 @@ ExtensionPrefs* ExtensionPrefs::Create( + bool extensions_disabled, + const std::vector& early_observers, + std::unique_ptr clock) { +- return new ExtensionPrefs(browser_context, pref_service, root_dir, ++ return new BraveExtensionPrefs(browser_context, pref_service, root_dir, + extension_pref_value_map, std::move(clock), + extensions_disabled, early_observers); + } diff --git a/patches/extensions-browser-extension_prefs.h.patch b/patches/extensions-browser-extension_prefs.h.patch new file mode 100644 index 000000000000..8f565623f9d2 --- /dev/null +++ b/patches/extensions-browser-extension_prefs.h.patch @@ -0,0 +1,12 @@ +diff --git a/extensions/browser/extension_prefs.h b/extensions/browser/extension_prefs.h +index 0f921039ce0e9b24bc939105182645214beac8c3..12a24f2530cdc1874f45813ac40b793f0ea340b1 100644 +--- a/extensions/browser/extension_prefs.h ++++ b/extensions/browser/extension_prefs.h +@@ -565,6 +565,7 @@ class ExtensionPrefs : public ExtensionScopedPrefs, public KeyedService { + void ClearExternalUninstallForTesting(const ExtensionId& id); + + private: ++ friend class BraveExtensionPrefs; + friend class ExtensionPrefsBlacklistedExtensions; // Unit test. + friend class ExtensionPrefsComponentExtension; // Unit test. + friend class ExtensionPrefsUninstallExtension; // Unit test.