Commit 514c08d9 authored by Evan Stade's avatar Evan Stade Committed by Commit Bot

Remove NotificationService usage from PreferenceEventRouter

Replace NOTIFICATION_PROFILE_CREATED/DESTROYED with ProfileObserver.

Bug: 268984
Change-Id: I77921a45a79d8bdee9e3a6fc786b5ac6e8c9ee38
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1852412
Commit-Queue: Evan Stade <estade@chromium.org>
Reviewed-by: default avatarDevlin <rdevlin.cronin@chromium.org>
Cr-Commit-Position: refs/heads/master@{#706618}
parent 7ae63182
...@@ -17,14 +17,12 @@ ...@@ -17,14 +17,12 @@
#include "base/strings/stringprintf.h" #include "base/strings/stringprintf.h"
#include "base/values.h" #include "base/values.h"
#include "build/build_config.h" #include "build/build_config.h"
#include "chrome/browser/chrome_notification_types.h"
#include "chrome/browser/extensions/api/content_settings/content_settings_service.h" #include "chrome/browser/extensions/api/content_settings/content_settings_service.h"
#include "chrome/browser/extensions/api/preference/preference_api_constants.h" #include "chrome/browser/extensions/api/preference/preference_api_constants.h"
#include "chrome/browser/extensions/api/preference/preference_helpers.h" #include "chrome/browser/extensions/api/preference/preference_helpers.h"
#include "chrome/browser/extensions/api/proxy/proxy_api.h" #include "chrome/browser/extensions/api/proxy/proxy_api.h"
#include "chrome/browser/extensions/extension_service.h" #include "chrome/browser/extensions/extension_service.h"
#include "chrome/browser/net/prediction_options.h" #include "chrome/browser/net/prediction_options.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/common/pref_names.h" #include "chrome/common/pref_names.h"
#include "components/autofill/core/common/autofill_prefs.h" #include "components/autofill/core/common/autofill_prefs.h"
#include "components/content_settings/core/common/pref_names.h" #include "components/content_settings/core/common/pref_names.h"
...@@ -35,9 +33,6 @@ ...@@ -35,9 +33,6 @@
#include "components/safe_browsing/common/safe_browsing_prefs.h" #include "components/safe_browsing/common/safe_browsing_prefs.h"
#include "components/spellcheck/browser/pref_names.h" #include "components/spellcheck/browser/pref_names.h"
#include "components/translate/core/browser/translate_pref_names.h" #include "components/translate/core/browser/translate_pref_names.h"
#include "content/public/browser/notification_details.h"
#include "content/public/browser/notification_service.h"
#include "content/public/browser/notification_source.h"
#include "extensions/browser/extension_pref_value_map.h" #include "extensions/browser/extension_pref_value_map.h"
#include "extensions/browser/extension_pref_value_map_factory.h" #include "extensions/browser/extension_pref_value_map_factory.h"
#include "extensions/browser/extension_prefs.h" #include "extensions/browser/extension_prefs.h"
...@@ -364,14 +359,15 @@ PreferenceEventRouter::PreferenceEventRouter(Profile* profile) ...@@ -364,14 +359,15 @@ PreferenceEventRouter::PreferenceEventRouter(Profile* profile)
base::Bind(&PreferenceEventRouter::OnPrefChanged, base::Bind(&PreferenceEventRouter::OnPrefChanged,
base::Unretained(this), registrar_.prefs())); base::Unretained(this), registrar_.prefs()));
} }
notification_registrar_.Add(this, chrome::NOTIFICATION_PROFILE_CREATED, DCHECK(!profile_->IsOffTheRecord());
content::NotificationService::AllSources()); observed_profiles_.Add(profile_);
notification_registrar_.Add(this, chrome::NOTIFICATION_PROFILE_DESTROYED, if (profile->HasOffTheRecordProfile())
content::NotificationService::AllSources()); OnOffTheRecordProfileCreated(profile->GetOffTheRecordProfile());
OnIncognitoProfileCreated(profile->GetOffTheRecordPrefs()); else
ObserveOffTheRecordPrefs(profile->GetReadOnlyOffTheRecordPrefs());
} }
PreferenceEventRouter::~PreferenceEventRouter() { } PreferenceEventRouter::~PreferenceEventRouter() = default;
void PreferenceEventRouter::OnPrefChanged(PrefService* pref_service, void PreferenceEventRouter::OnPrefChanged(PrefService* pref_service,
const std::string& browser_pref) { const std::string& browser_pref) {
...@@ -423,33 +419,22 @@ void PreferenceEventRouter::OnPrefChanged(PrefService* pref_service, ...@@ -423,33 +419,22 @@ void PreferenceEventRouter::OnPrefChanged(PrefService* pref_service,
browser_pref); browser_pref);
} }
void PreferenceEventRouter::Observe( void PreferenceEventRouter::OnOffTheRecordProfileCreated(
int type, Profile* off_the_record) {
const content::NotificationSource& source, observed_profiles_.Add(off_the_record);
const content::NotificationDetails& details) { ObserveOffTheRecordPrefs(off_the_record->GetPrefs());
switch (type) { }
case chrome::NOTIFICATION_PROFILE_CREATED: {
Profile* profile = content::Source<Profile>(source).ptr(); void PreferenceEventRouter::OnProfileWillBeDestroyed(Profile* profile) {
if (profile != profile_ && profile->GetOriginalProfile() == profile_) { observed_profiles_.Remove(profile);
OnIncognitoProfileCreated(profile->GetPrefs()); if (profile->IsOffTheRecord()) {
} // The real PrefService is about to be destroyed so we must make sure we
break; // get the "dummy" one.
} ObserveOffTheRecordPrefs(profile_->GetReadOnlyOffTheRecordPrefs());
case chrome::NOTIFICATION_PROFILE_DESTROYED: {
Profile* profile = content::Source<Profile>(source).ptr();
if (profile != profile_ && profile->GetOriginalProfile() == profile_) {
// The real PrefService is about to be destroyed so we must make sure we
// get the "dummy" one.
OnIncognitoProfileCreated(profile_->GetReadOnlyOffTheRecordPrefs());
}
break;
}
default:
NOTREACHED();
} }
} }
void PreferenceEventRouter::OnIncognitoProfileCreated(PrefService* prefs) { void PreferenceEventRouter::ObserveOffTheRecordPrefs(PrefService* prefs) {
incognito_registrar_ = std::make_unique<PrefChangeRegistrar>(); incognito_registrar_ = std::make_unique<PrefChangeRegistrar>();
incognito_registrar_->Init(prefs); incognito_registrar_->Init(prefs);
for (const auto& pref : kPrefMapping) { for (const auto& pref : kPrefMapping) {
......
...@@ -10,11 +10,12 @@ ...@@ -10,11 +10,12 @@
#include "base/macros.h" #include "base/macros.h"
#include "base/memory/ref_counted.h" #include "base/memory/ref_counted.h"
#include "base/scoped_observer.h"
#include "chrome/browser/extensions/api/content_settings/content_settings_store.h" #include "chrome/browser/extensions/api/content_settings/content_settings_store.h"
#include "chrome/browser/extensions/chrome_extension_function.h" #include "chrome/browser/extensions/chrome_extension_function.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/profiles/profile_observer.h"
#include "components/prefs/pref_change_registrar.h" #include "components/prefs/pref_change_registrar.h"
#include "content/public/browser/notification_observer.h"
#include "content/public/browser/notification_registrar.h"
#include "extensions/browser/browser_context_keyed_api_factory.h" #include "extensions/browser/browser_context_keyed_api_factory.h"
#include "extensions/browser/event_router.h" #include "extensions/browser/event_router.h"
#include "extensions/browser/extension_prefs_scope.h" #include "extensions/browser/extension_prefs_scope.h"
...@@ -29,7 +30,7 @@ class Value; ...@@ -29,7 +30,7 @@ class Value;
namespace extensions { namespace extensions {
class ExtensionPrefs; class ExtensionPrefs;
class PreferenceEventRouter : public content::NotificationObserver { class PreferenceEventRouter : public ProfileObserver {
public: public:
explicit PreferenceEventRouter(Profile* profile); explicit PreferenceEventRouter(Profile* profile);
~PreferenceEventRouter() override; ~PreferenceEventRouter() override;
...@@ -38,20 +39,20 @@ class PreferenceEventRouter : public content::NotificationObserver { ...@@ -38,20 +39,20 @@ class PreferenceEventRouter : public content::NotificationObserver {
void OnPrefChanged(PrefService* pref_service, void OnPrefChanged(PrefService* pref_service,
const std::string& pref_key); const std::string& pref_key);
// content::NotificationObserver: // ProfileObserver:
void Observe(int type, void OnOffTheRecordProfileCreated(Profile* off_the_record) override;
const content::NotificationSource& source, void OnProfileWillBeDestroyed(Profile* profile) override;
const content::NotificationDetails& details) override;
void OnIncognitoProfileCreated(PrefService* prefs); void ObserveOffTheRecordPrefs(PrefService* prefs);
content::NotificationRegistrar notification_registrar_;
PrefChangeRegistrar registrar_; PrefChangeRegistrar registrar_;
std::unique_ptr<PrefChangeRegistrar> incognito_registrar_; std::unique_ptr<PrefChangeRegistrar> incognito_registrar_;
// Weak, owns us (transitively via ExtensionService). // Weak, owns us (transitively via ExtensionService).
Profile* profile_; Profile* profile_;
ScopedObserver<Profile, ProfileObserver> observed_profiles_{this};
DISALLOW_COPY_AND_ASSIGN(PreferenceEventRouter); DISALLOW_COPY_AND_ASSIGN(PreferenceEventRouter);
}; };
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment