Commit cc3efefb authored by Evan Stade's avatar Evan Stade Committed by Commit Bot

Add ProfileObserver

Use it to remove NotificationService usage from DriveIntegrationService
(others to follow).

See code comments for usage notes.

Bug: 268984
Change-Id: I64f153def34e5d08bc3b247ed0b865037d18af69
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1853024
Commit-Queue: Evan Stade <estade@chromium.org>
Reviewed-by: default avatarSylvain Defresne <sdefresne@chromium.org>
Reviewed-by: default avatarDavid Roger <droger@chromium.org>
Cr-Commit-Position: refs/heads/master@{#705986}
parent 1c1107d4
......@@ -1421,6 +1421,7 @@ jumbo_split_static_library("browser") {
"profiles/profile_manager_observer.h",
"profiles/profile_metrics.cc",
"profiles/profile_metrics.h",
"profiles/profile_observer.h",
"profiles/profile_shortcut_manager_win.cc",
"profiles/profile_shortcut_manager_win.h",
"profiles/profiles_state.cc",
......
......@@ -24,7 +24,6 @@
#include "base/timer/timer.h"
#include "base/unguessable_token.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/chrome_notification_types.h"
#include "chrome/browser/chromeos/drive/debug_info_collector.h"
#include "chrome/browser/chromeos/drive/download_handler.h"
#include "chrome/browser/chromeos/drive/file_system_util.h"
......@@ -61,7 +60,6 @@
#include "content/public/browser/browser_context.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/network_service_instance.h"
#include "content/public/browser/notification_service.h"
#include "content/public/browser/system_connector.h"
#include "content/public/common/user_agent.h"
#include "google_apis/drive/auth_service.h"
......@@ -1185,12 +1183,7 @@ void DriveIntegrationService::InitializeAfterMetadataInitialized(
BrowserContext::GetDownloadManager(
profile_->GetOffTheRecordProfile()));
}
profile_notification_registrar_ =
std::make_unique<content::NotificationRegistrar>();
profile_notification_registrar_->Add(
this,
chrome::NOTIFICATION_PROFILE_CREATED,
content::NotificationService::AllSources());
observed_profiles_.Add(profile_);
}
// Register for Google Drive invalidation notifications.
......@@ -1233,17 +1226,11 @@ bool DriveIntegrationService::DownloadDirectoryPreferenceIsInDrive() {
GetMountPointPath().IsParent(downloads_path);
}
void DriveIntegrationService::Observe(
int type,
const content::NotificationSource& source,
const content::NotificationDetails& details) {
DCHECK_EQ(chrome::NOTIFICATION_PROFILE_CREATED, type);
Profile* created_profile = content::Source<Profile>(source).ptr();
if (created_profile->IsOffTheRecord() &&
created_profile->IsSameProfile(profile_)) {
download_handler_->ObserveIncognitoDownloadManager(
BrowserContext::GetDownloadManager(created_profile));
}
void DriveIntegrationService::OnOffTheRecordProfileCreated(
Profile* off_the_record) {
DCHECK_EQ(profile_, off_the_record->GetOriginalProfile());
download_handler_->ObserveIncognitoDownloadManager(
BrowserContext::GetDownloadManager(off_the_record));
}
void DriveIntegrationService::MigratePinnedFiles() {
......
......@@ -16,6 +16,7 @@
#include "base/memory/weak_ptr.h"
#include "base/observer_list.h"
#include "base/scoped_observer.h"
#include "chrome/browser/profiles/profile_observer.h"
#include "chromeos/components/drivefs/drivefs_host.h"
#include "chromeos/dbus/power/power_manager_client.h"
#include "components/drive/drive_notification_observer.h"
......@@ -24,8 +25,6 @@
#include "components/drive/job_scheduler.h"
#include "components/keyed_service/content/browser_context_keyed_service_factory.h"
#include "components/keyed_service/core/keyed_service.h"
#include "content/public/browser/notification_observer.h"
#include "content/public/browser/notification_registrar.h"
class Profile;
......@@ -104,7 +103,7 @@ class DriveIntegrationServiceObserver {
// created per-profile.
class DriveIntegrationService : public KeyedService,
public DriveNotificationObserver,
public content::NotificationObserver,
public ProfileObserver,
public drivefs::DriveFsHost::MountObserver,
public chromeos::PowerManagerClient::Observer {
public:
......@@ -249,10 +248,8 @@ class DriveIntegrationService : public KeyedService,
bool DownloadDirectoryPreferenceIsInDrive();
// content::NotificationObserver overrides.
void Observe(int type,
const content::NotificationSource& source,
const content::NotificationDetails& details) override;
// ProfileObserver:
void OnOffTheRecordProfileCreated(Profile* off_the_record) override;
// Migrate pinned files from the old Drive integration to DriveFS.
void MigratePinnedFiles();
......@@ -293,8 +290,6 @@ class DriveIntegrationService : public KeyedService,
std::unique_ptr<DebugInfoCollector> debug_info_collector_;
base::ObserverList<DriveIntegrationServiceObserver>::Unchecked observers_;
std::unique_ptr<content::NotificationRegistrar>
profile_notification_registrar_;
std::unique_ptr<DriveFsHolder> drivefs_holder_;
std::unique_ptr<PreferenceWatcher> preference_watcher_;
......@@ -308,6 +303,7 @@ class DriveIntegrationService : public KeyedService,
ScopedObserver<chromeos::PowerManagerClient,
chromeos::PowerManagerClient::Observer>
power_manager_observer_{this};
ScopedObserver<Profile, ProfileObserver> observed_profiles_{this};
// Note: This should remain the last member so it'll be destroyed and
// invalidate its weak pointers before any other members are destroyed.
......
......@@ -639,6 +639,7 @@ Profile* Profile::CreateOffTheRecordProfile() {
if (!profile)
profile = new OffTheRecordProfileImpl(this);
profile->Init();
NotifyOffTheRecordProfileCreated(profile);
return profile;
}
......
......@@ -13,6 +13,7 @@
#include "chrome/browser/net/profile_network_context_service.h"
#include "chrome/browser/net/profile_network_context_service_factory.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "chrome/browser/profiles/profile_observer.h"
#include "chrome/browser/sync/profile_sync_service_factory.h"
#include "chrome/common/buildflags.h"
#include "chrome/common/pref_names.h"
......@@ -116,6 +117,14 @@ Profile* Profile::FromWebUI(content::WebUI* web_ui) {
return FromBrowserContext(web_ui->GetWebContents()->GetBrowserContext());
}
void Profile::AddObserver(ProfileObserver* observer) {
observers_.AddObserver(observer);
}
void Profile::RemoveObserver(ProfileObserver* observer) {
observers_.RemoveObserver(observer);
}
TestingProfile* Profile::AsTestingProfile() {
return nullptr;
}
......@@ -318,6 +327,10 @@ void Profile::MaybeSendDestroyedNotification() {
sent_destroyed_notification_ = true;
NotifyWillBeDestroyed(this);
for (auto& observer : observers_)
observer.OnProfileWillBeDestroyed(this);
content::NotificationService::current()->Notify(
chrome::NOTIFICATION_PROFILE_DESTROYED,
content::Source<Profile>(this),
......@@ -325,6 +338,7 @@ void Profile::MaybeSendDestroyedNotification() {
}
}
// static
PrefStore* Profile::CreateExtensionPrefStore(Profile* profile,
bool incognito_pref_store) {
#if BUILDFLAG(ENABLE_EXTENSIONS)
......@@ -357,3 +371,10 @@ void Profile::Wipe() {
ChromeBrowsingDataRemoverDelegate::WIPE_PROFILE,
ChromeBrowsingDataRemoverDelegate::ALL_ORIGIN_TYPES);
}
void Profile::NotifyOffTheRecordProfileCreated(Profile* off_the_record) {
DCHECK_EQ(off_the_record->GetOriginalProfile(), this);
DCHECK(off_the_record->IsOffTheRecord());
for (auto& observer : observers_)
observer.OnOffTheRecordProfileCreated(off_the_record);
}
......@@ -13,6 +13,7 @@
#include "base/files/file_path.h"
#include "base/macros.h"
#include "base/memory/scoped_refptr.h"
#include "base/observer_list.h"
#include "base/time/time.h"
#include "build/build_config.h"
#include "content/public/browser/browser_context.h"
......@@ -64,6 +65,7 @@ class PrefRegistrySyncable;
}
class OffTheRecordProfileIOData;
class ProfileObserver;
// Instead of adding more members to Profile, consider creating a
// KeyedService. See
......@@ -142,6 +144,9 @@ class Profile : public content::BrowserContext {
// Returns the profile corresponding to the given WebUI.
static Profile* FromWebUI(content::WebUI* web_ui);
void AddObserver(ProfileObserver* observer);
void RemoveObserver(ProfileObserver* observer);
// content::BrowserContext implementation ------------------------------------
// Returns the path of the directory where this context's data is stored.
......@@ -449,6 +454,8 @@ class Profile : public content::BrowserContext {
bool incognito_pref_store);
private:
void NotifyOffTheRecordProfileCreated(Profile* off_the_record);
bool restored_last_session_;
// Used to prevent the notification that this Profile is destroyed from
......@@ -466,6 +473,8 @@ class Profile : public content::BrowserContext {
// A non-browsing profile not associated to a user. Sample use: User-Manager.
bool is_system_profile_;
base::ObserverList<ProfileObserver> observers_;
DISALLOW_COPY_AND_ASSIGN(Profile);
};
......
......@@ -11,8 +11,14 @@ class Profile;
class ProfileManagerObserver : public base::CheckedObserver {
public:
// Called when a profile is added to the manager. The profile is fully created
// and registered with the ProfileManager.
// Called when a Profile is added to the manager, the profile is fully created
// and registered with the ProfileManager. This is only called for normal
// (on-the-record) profiles as the ProfileManager doesn't own the OTR profile.
// For OTR profile creation, see
// ProfileObserver::OnOffTheRecordProfileCreated(). Unlike
// ProfileInfoCacheObserver::OnProfileAdded(), which is only called when a new
// user is first created, this is called once on every run of Chrome, provided
// that the Profile is in use.
virtual void OnProfileAdded(Profile* profile) {}
// Called when the user deletes a profile and all associated data should be
......
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROME_BROWSER_PROFILES_PROFILE_OBSERVER_H_
#define CHROME_BROWSER_PROFILES_PROFILE_OBSERVER_H_
#include "base/observer_list_types.h"
class Profile;
class ProfileObserver : public base::CheckedObserver {
public:
// The observed profile has spawned a new off the record profile (whether
// owned or independent). This is called just before ownership of the new
// OTR profile is taken by the original profile or the
// IndependentOTRProfileManager, so |original_profile->GetOffRecordProfile()|
// should not be called, but |off_the_record->GetOriginalProfile()| will
// return |original_profile|.
virtual void OnOffTheRecordProfileCreated(Profile* off_the_record) {}
// The observed profile will be destroyed soon. All KeyedServices are still
// valid. The shutdown sequence for a profile is:
// 1. BrowserContext related shutdown occurs via
// BrowserContext::NotifyWillBeDestroyed()
// 2. OnProfileWillBeDestroyed called for |profile|
// 3. OTR profile (if any) goes through shutdown in same sequence
// 4. KeyedServices are shut down for |profile|
virtual void OnProfileWillBeDestroyed(Profile* profile) {}
};
#endif // CHROME_BROWSER_PROFILES_PROFILE_OBSERVER_H_
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