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 @@
#include "base/strings/stringprintf.h"
#include "base/values.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/preference/preference_api_constants.h"
#include "chrome/browser/extensions/api/preference/preference_helpers.h"
#include "chrome/browser/extensions/api/proxy/proxy_api.h"
#include "chrome/browser/extensions/extension_service.h"
#include "chrome/browser/net/prediction_options.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/common/pref_names.h"
#include "components/autofill/core/common/autofill_prefs.h"
#include "components/content_settings/core/common/pref_names.h"
......@@ -35,9 +33,6 @@
#include "components/safe_browsing/common/safe_browsing_prefs.h"
#include "components/spellcheck/browser/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_factory.h"
#include "extensions/browser/extension_prefs.h"
......@@ -364,14 +359,15 @@ PreferenceEventRouter::PreferenceEventRouter(Profile* profile)
base::Bind(&PreferenceEventRouter::OnPrefChanged,
base::Unretained(this), registrar_.prefs()));
}
notification_registrar_.Add(this, chrome::NOTIFICATION_PROFILE_CREATED,
content::NotificationService::AllSources());
notification_registrar_.Add(this, chrome::NOTIFICATION_PROFILE_DESTROYED,
content::NotificationService::AllSources());
OnIncognitoProfileCreated(profile->GetOffTheRecordPrefs());
DCHECK(!profile_->IsOffTheRecord());
observed_profiles_.Add(profile_);
if (profile->HasOffTheRecordProfile())
OnOffTheRecordProfileCreated(profile->GetOffTheRecordProfile());
else
ObserveOffTheRecordPrefs(profile->GetReadOnlyOffTheRecordPrefs());
}
PreferenceEventRouter::~PreferenceEventRouter() { }
PreferenceEventRouter::~PreferenceEventRouter() = default;
void PreferenceEventRouter::OnPrefChanged(PrefService* pref_service,
const std::string& browser_pref) {
......@@ -423,33 +419,22 @@ void PreferenceEventRouter::OnPrefChanged(PrefService* pref_service,
browser_pref);
}
void PreferenceEventRouter::Observe(
int type,
const content::NotificationSource& source,
const content::NotificationDetails& details) {
switch (type) {
case chrome::NOTIFICATION_PROFILE_CREATED: {
Profile* profile = content::Source<Profile>(source).ptr();
if (profile != profile_ && profile->GetOriginalProfile() == profile_) {
OnIncognitoProfileCreated(profile->GetPrefs());
}
break;
}
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::OnOffTheRecordProfileCreated(
Profile* off_the_record) {
observed_profiles_.Add(off_the_record);
ObserveOffTheRecordPrefs(off_the_record->GetPrefs());
}
void PreferenceEventRouter::OnProfileWillBeDestroyed(Profile* profile) {
observed_profiles_.Remove(profile);
if (profile->IsOffTheRecord()) {
// The real PrefService is about to be destroyed so we must make sure we
// get the "dummy" one.
ObserveOffTheRecordPrefs(profile_->GetReadOnlyOffTheRecordPrefs());
}
}
void PreferenceEventRouter::OnIncognitoProfileCreated(PrefService* prefs) {
void PreferenceEventRouter::ObserveOffTheRecordPrefs(PrefService* prefs) {
incognito_registrar_ = std::make_unique<PrefChangeRegistrar>();
incognito_registrar_->Init(prefs);
for (const auto& pref : kPrefMapping) {
......
......@@ -10,11 +10,12 @@
#include "base/macros.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/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 "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/event_router.h"
#include "extensions/browser/extension_prefs_scope.h"
......@@ -29,7 +30,7 @@ class Value;
namespace extensions {
class ExtensionPrefs;
class PreferenceEventRouter : public content::NotificationObserver {
class PreferenceEventRouter : public ProfileObserver {
public:
explicit PreferenceEventRouter(Profile* profile);
~PreferenceEventRouter() override;
......@@ -38,20 +39,20 @@ class PreferenceEventRouter : public content::NotificationObserver {
void OnPrefChanged(PrefService* pref_service,
const std::string& pref_key);
// content::NotificationObserver:
void Observe(int type,
const content::NotificationSource& source,
const content::NotificationDetails& details) override;
// ProfileObserver:
void OnOffTheRecordProfileCreated(Profile* off_the_record) override;
void OnProfileWillBeDestroyed(Profile* profile) override;
void OnIncognitoProfileCreated(PrefService* prefs);
void ObserveOffTheRecordPrefs(PrefService* prefs);
content::NotificationRegistrar notification_registrar_;
PrefChangeRegistrar registrar_;
std::unique_ptr<PrefChangeRegistrar> incognito_registrar_;
// Weak, owns us (transitively via ExtensionService).
Profile* profile_;
ScopedObserver<Profile, ProfileObserver> observed_profiles_{this};
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