Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Call ContentSettingsService::OnExtensionPrefsLoaded when adding component extensions #62

Merged
merged 3 commits into from
Mar 23, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ group("child_dependencies") {
group("browser_dependencies") {
public_deps = [
"//brave/browser",
"//brave/extensions",
"common",
":brave_framework_resources",
"chromium_src:browser",
Expand Down
2 changes: 2 additions & 0 deletions browser/extensions/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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>;
~BraveContentSettingsStore() override;

bool suppress_notifications_;

DISALLOW_COPY_AND_ASSIGN(BraveContentSettingsStore);
};

} // namespace extensions

#endif // BRAVE_BROWSER_EXTENSIONS_API_BREVE_CONTENT_SETTINGS_CONTENT_SETTINGS_STORE_H_
11 changes: 11 additions & 0 deletions extensions/BUILD.gn
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

source_set("extensions") {
sources = [
"browser/brave_extension_prefs.cc",
"browser/brave_extension_prefs.h",
]

public_deps = [
"//extensions/browser",
]
}
37 changes: 37 additions & 0 deletions extensions/browser/brave_extension_prefs.cc
Original file line number Diff line number Diff line change
@@ -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<base::Clock> clock,
bool extensions_disabled,
const std::vector<ExtensionPrefsObserver*>& 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
32 changes: 32 additions & 0 deletions extensions/browser/brave_extension_prefs.h
Original file line number Diff line number Diff line change
@@ -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<base::Clock> clock,
bool extensions_disabled,
const std::vector<ExtensionPrefsObserver*>& 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_
Original file line number Diff line number Diff line change
@@ -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() {}

Original file line number Diff line number Diff line change
@@ -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<ContentSettingsStore>;

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();
13 changes: 9 additions & 4 deletions patches/chrome-browser-extensions-extension_service.cc.patch
Original file line number Diff line number Diff line change
@@ -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(
Expand All @@ -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);
Expand All @@ -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<extensions::BraveExtensionPrefs*>(extension_prefs_)
+ ->NotifyExtensionPrefsLoaded(extension->id());
+#endif
}

Expand Down
21 changes: 21 additions & 0 deletions patches/extensions-browser-extension_prefs.cc.patch
Original file line number Diff line number Diff line change
@@ -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<ExtensionPrefsObserver*>& early_observers,
std::unique_ptr<base::Clock> 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);
}
12 changes: 12 additions & 0 deletions patches/extensions-browser-extension_prefs.h.patch
Original file line number Diff line number Diff line change
@@ -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.