Commit 7cb1f9cb authored by Evan Stade's avatar Evan Stade Committed by Commit Bot

Remove NOTIFICATION_PROFILE_DESTROYED from IndependentOTRProfileManager

Bug: 268984
Change-Id: Id59283a4bde7a87a88345672b10d9db9bf9b9c17
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1869829Reviewed-by: default avatarBrandon Tolsch <btolsch@chromium.org>
Commit-Queue: Evan Stade <estade@chromium.org>
Cr-Commit-Position: refs/heads/master@{#708037}
parent 704e8e0c
...@@ -43,17 +43,15 @@ IndependentOTRProfileManager* IndependentOTRProfileManager::GetInstance() { ...@@ -43,17 +43,15 @@ IndependentOTRProfileManager* IndependentOTRProfileManager::GetInstance() {
std::unique_ptr<IndependentOTRProfileManager::OTRProfileRegistration> std::unique_ptr<IndependentOTRProfileManager::OTRProfileRegistration>
IndependentOTRProfileManager::CreateFromOriginalProfile( IndependentOTRProfileManager::CreateFromOriginalProfile(
Profile* profile, Profile* original_profile,
ProfileDestroyedCallback callback) { ProfileDestroyedCallback callback) {
DCHECK_CURRENTLY_ON(BrowserThread::UI); DCHECK_CURRENTLY_ON(BrowserThread::UI);
DCHECK(profile); DCHECK(original_profile);
DCHECK(!profile->IsOffTheRecord()); DCHECK(!original_profile->IsOffTheRecord());
DCHECK(!callback.is_null()); DCHECK(!callback.is_null());
if (!HasDependentProfiles(profile)) { if (!HasDependentProfiles(original_profile))
registrar_.Add(this, chrome::NOTIFICATION_PROFILE_DESTROYED, observed_original_profiles_.Add(original_profile);
content::Source<Profile>(profile)); auto* otr_profile = original_profile->CreateOffTheRecordProfile();
}
auto* otr_profile = profile->CreateOffTheRecordProfile();
auto entry = refcounts_map_.emplace(otr_profile, 1); auto entry = refcounts_map_.emplace(otr_profile, 1);
auto callback_entry = auto callback_entry =
callbacks_map_.emplace(otr_profile, std::move(callback)); callbacks_map_.emplace(otr_profile, std::move(callback));
...@@ -97,10 +95,8 @@ void IndependentOTRProfileManager::OnBrowserRemoved(Browser* browser) { ...@@ -97,10 +95,8 @@ void IndependentOTRProfileManager::OnBrowserRemoved(Browser* browser) {
base::ThreadTaskRunnerHandle::Get()->DeleteSoon(FROM_HERE, entry->first); base::ThreadTaskRunnerHandle::Get()->DeleteSoon(FROM_HERE, entry->first);
auto* original_profile = entry->first->GetOriginalProfile(); auto* original_profile = entry->first->GetOriginalProfile();
refcounts_map_.erase(entry); refcounts_map_.erase(entry);
if (!HasDependentProfiles(original_profile)) { if (!HasDependentProfiles(original_profile))
registrar_.Remove(this, chrome::NOTIFICATION_PROFILE_DESTROYED, observed_original_profiles_.Remove(original_profile);
content::Source<Profile>(original_profile));
}
} }
} }
...@@ -138,28 +134,22 @@ void IndependentOTRProfileManager::UnregisterProfile(Profile* profile) { ...@@ -138,28 +134,22 @@ void IndependentOTRProfileManager::UnregisterProfile(Profile* profile) {
auto* original_profile = profile->GetOriginalProfile(); auto* original_profile = profile->GetOriginalProfile();
ProfileDestroyer::DestroyProfileWhenAppropriate(entry->first); ProfileDestroyer::DestroyProfileWhenAppropriate(entry->first);
refcounts_map_.erase(entry); refcounts_map_.erase(entry);
if (!HasDependentProfiles(original_profile)) { if (!HasDependentProfiles(original_profile))
registrar_.Remove(this, chrome::NOTIFICATION_PROFILE_DESTROYED, observed_original_profiles_.Remove(original_profile);
content::Source<Profile>(original_profile));
}
} }
} }
void IndependentOTRProfileManager::Observe( void IndependentOTRProfileManager::OnProfileWillBeDestroyed(Profile* profile) {
int type,
const content::NotificationSource& source,
const content::NotificationDetails& details) {
DCHECK_EQ(chrome::NOTIFICATION_PROFILE_DESTROYED, type);
for (auto it = callbacks_map_.begin(); it != callbacks_map_.end();) { for (auto it = callbacks_map_.begin(); it != callbacks_map_.end();) {
if (source != content::Source<Profile>(it->first->GetOriginalProfile())) { if (profile != it->first->GetOriginalProfile()) {
++it; ++it;
continue; continue;
} }
// If the registration is destroyed from within this callback, we don't want // If the registration is destroyed from within this callback, we don't want
// to double-erase. If it isn't, we still need to erase the callback entry. // to double-erase. If it isn't, we still need to erase the callback entry.
auto* profile = it->first; auto* otr_profile = it->first;
auto callback = std::move(it->second); auto callback = std::move(it->second);
it = callbacks_map_.erase(it); it = callbacks_map_.erase(it);
std::move(callback).Run(profile); std::move(callback).Run(otr_profile);
} }
} }
...@@ -12,9 +12,10 @@ ...@@ -12,9 +12,10 @@
#include "base/containers/flat_map.h" #include "base/containers/flat_map.h"
#include "base/macros.h" #include "base/macros.h"
#include "base/memory/singleton.h" #include "base/memory/singleton.h"
#include "base/scoped_observer.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/profiles/profile_observer.h"
#include "chrome/browser/ui/browser_list_observer.h" #include "chrome/browser/ui/browser_list_observer.h"
#include "content/public/browser/notification_observer.h"
#include "content/public/browser/notification_registrar.h"
class Profile; class Profile;
...@@ -42,9 +43,8 @@ class Profile; ...@@ -42,9 +43,8 @@ class Profile;
// original profile is being destroyed. // original profile is being destroyed.
// //
// All methods must be called on the UI thread. // All methods must be called on the UI thread.
class IndependentOTRProfileManager final class IndependentOTRProfileManager final : public BrowserListObserver,
: public BrowserListObserver, public ProfileObserver {
public content::NotificationObserver {
public: public:
class OTRProfileRegistration { class OTRProfileRegistration {
public: public:
...@@ -84,21 +84,19 @@ class IndependentOTRProfileManager final ...@@ -84,21 +84,19 @@ class IndependentOTRProfileManager final
bool HasDependentProfiles(Profile* profile) const; bool HasDependentProfiles(Profile* profile) const;
void UnregisterProfile(Profile* profile); void UnregisterProfile(Profile* profile);
// chrome::BrowserListObserver overrides. // chrome::BrowserListObserver:
void OnBrowserAdded(Browser* browser) final; void OnBrowserAdded(Browser* browser) final;
void OnBrowserRemoved(Browser* browser) final; void OnBrowserRemoved(Browser* browser) final;
// content::NotificationObserver overrides. // ProfileObserver:
void Observe(int type, void OnProfileWillBeDestroyed(Profile* profile) final;
const content::NotificationSource& source,
const content::NotificationDetails& details) final;
// Counts the number of Browser instances referencing an independent OTR // Counts the number of Browser instances referencing an independent OTR
// profile plus 1 for the OTRProfileRegistration object that created it. // profile plus 1 for the OTRProfileRegistration object that created it.
base::flat_map<Profile*, int32_t> refcounts_map_; base::flat_map<Profile*, int32_t> refcounts_map_;
base::flat_map<Profile*, ProfileDestroyedCallback> callbacks_map_; base::flat_map<Profile*, ProfileDestroyedCallback> callbacks_map_;
content::NotificationRegistrar registrar_; ScopedObserver<Profile, ProfileObserver> observed_original_profiles_{this};
DISALLOW_COPY_AND_ASSIGN(IndependentOTRProfileManager); DISALLOW_COPY_AND_ASSIGN(IndependentOTRProfileManager);
}; };
......
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