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() {
std::unique_ptr<IndependentOTRProfileManager::OTRProfileRegistration>
IndependentOTRProfileManager::CreateFromOriginalProfile(
Profile* profile,
Profile* original_profile,
ProfileDestroyedCallback callback) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
DCHECK(profile);
DCHECK(!profile->IsOffTheRecord());
DCHECK(original_profile);
DCHECK(!original_profile->IsOffTheRecord());
DCHECK(!callback.is_null());
if (!HasDependentProfiles(profile)) {
registrar_.Add(this, chrome::NOTIFICATION_PROFILE_DESTROYED,
content::Source<Profile>(profile));
}
auto* otr_profile = profile->CreateOffTheRecordProfile();
if (!HasDependentProfiles(original_profile))
observed_original_profiles_.Add(original_profile);
auto* otr_profile = original_profile->CreateOffTheRecordProfile();
auto entry = refcounts_map_.emplace(otr_profile, 1);
auto callback_entry =
callbacks_map_.emplace(otr_profile, std::move(callback));
......@@ -97,10 +95,8 @@ void IndependentOTRProfileManager::OnBrowserRemoved(Browser* browser) {
base::ThreadTaskRunnerHandle::Get()->DeleteSoon(FROM_HERE, entry->first);
auto* original_profile = entry->first->GetOriginalProfile();
refcounts_map_.erase(entry);
if (!HasDependentProfiles(original_profile)) {
registrar_.Remove(this, chrome::NOTIFICATION_PROFILE_DESTROYED,
content::Source<Profile>(original_profile));
}
if (!HasDependentProfiles(original_profile))
observed_original_profiles_.Remove(original_profile);
}
}
......@@ -138,28 +134,22 @@ void IndependentOTRProfileManager::UnregisterProfile(Profile* profile) {
auto* original_profile = profile->GetOriginalProfile();
ProfileDestroyer::DestroyProfileWhenAppropriate(entry->first);
refcounts_map_.erase(entry);
if (!HasDependentProfiles(original_profile)) {
registrar_.Remove(this, chrome::NOTIFICATION_PROFILE_DESTROYED,
content::Source<Profile>(original_profile));
}
if (!HasDependentProfiles(original_profile))
observed_original_profiles_.Remove(original_profile);
}
}
void IndependentOTRProfileManager::Observe(
int type,
const content::NotificationSource& source,
const content::NotificationDetails& details) {
DCHECK_EQ(chrome::NOTIFICATION_PROFILE_DESTROYED, type);
void IndependentOTRProfileManager::OnProfileWillBeDestroyed(Profile* profile) {
for (auto it = callbacks_map_.begin(); it != callbacks_map_.end();) {
if (source != content::Source<Profile>(it->first->GetOriginalProfile())) {
if (profile != it->first->GetOriginalProfile()) {
++it;
continue;
}
// 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.
auto* profile = it->first;
auto* otr_profile = it->first;
auto callback = std::move(it->second);
it = callbacks_map_.erase(it);
std::move(callback).Run(profile);
std::move(callback).Run(otr_profile);
}
}
......@@ -12,9 +12,10 @@
#include "base/containers/flat_map.h"
#include "base/macros.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 "content/public/browser/notification_observer.h"
#include "content/public/browser/notification_registrar.h"
class Profile;
......@@ -42,9 +43,8 @@ class Profile;
// original profile is being destroyed.
//
// All methods must be called on the UI thread.
class IndependentOTRProfileManager final
: public BrowserListObserver,
public content::NotificationObserver {
class IndependentOTRProfileManager final : public BrowserListObserver,
public ProfileObserver {
public:
class OTRProfileRegistration {
public:
......@@ -84,21 +84,19 @@ class IndependentOTRProfileManager final
bool HasDependentProfiles(Profile* profile) const;
void UnregisterProfile(Profile* profile);
// chrome::BrowserListObserver overrides.
// chrome::BrowserListObserver:
void OnBrowserAdded(Browser* browser) final;
void OnBrowserRemoved(Browser* browser) final;
// content::NotificationObserver overrides.
void Observe(int type,
const content::NotificationSource& source,
const content::NotificationDetails& details) final;
// ProfileObserver:
void OnProfileWillBeDestroyed(Profile* profile) final;
// Counts the number of Browser instances referencing an independent OTR
// profile plus 1 for the OTRProfileRegistration object that created it.
base::flat_map<Profile*, int32_t> refcounts_map_;
base::flat_map<Profile*, ProfileDestroyedCallback> callbacks_map_;
content::NotificationRegistrar registrar_;
ScopedObserver<Profile, ProfileObserver> observed_original_profiles_{this};
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