This source file includes following definitions.
- DestroyProfileWhenAppropriate
- DestroyOffTheRecordProfileNow
- weak_ptr_factory_
- RenderProcessHostDestroyed
- DestroyProfile
- GetHostsForProfile
#include "chrome/browser/profiles/profile_destroyer.h"
#include "base/bind.h"
#include "base/debug/trace_event.h"
#include "base/memory/scoped_ptr.h"
#include "base/message_loop/message_loop.h"
#include "chrome/browser/profiles/profile.h"
#include "content/public/browser/render_process_host.h"
namespace {
#if defined(OS_ANDROID)
const int64 kTimerDelaySeconds = 5;
#else
const int64 kTimerDelaySeconds = 1;
#endif
}
ProfileDestroyer::DestroyerSet* ProfileDestroyer::pending_destroyers_ = NULL;
void ProfileDestroyer::DestroyProfileWhenAppropriate(Profile* const profile) {
TRACE_EVENT0("shutdown", "ProfileDestroyer::DestroyProfileWhenAppropriate");
DCHECK(profile);
profile->MaybeSendDestroyedNotification();
HostSet hosts;
if (profile->AsTestingProfile() == NULL) {
GetHostsForProfile(profile, &hosts);
if (!profile->IsOffTheRecord() && profile->HasOffTheRecordProfile())
GetHostsForProfile(profile->GetOffTheRecordProfile(), &hosts);
}
DCHECK(hosts.empty() || profile->IsOffTheRecord() ||
content::RenderProcessHost::run_renderer_in_process()) << \
"Profile still has " << hosts.size() << " hosts";
if (hosts.empty() || !profile->IsOffTheRecord()) {
if (profile->IsOffTheRecord())
profile->GetOriginalProfile()->DestroyOffTheRecordProfile();
else
delete profile;
} else {
new ProfileDestroyer(profile, &hosts);
}
}
void ProfileDestroyer::DestroyOffTheRecordProfileNow(Profile* const profile) {
DCHECK(profile);
DCHECK(profile->IsOffTheRecord());
if (pending_destroyers_) {
for (DestroyerSet::iterator i = pending_destroyers_->begin();
i != pending_destroyers_->end(); ++i) {
if ((*i)->profile_ == profile) {
NOTREACHED() << "A render process host wasn't destroyed early enough.";
(*i)->profile_ = NULL;
break;
}
}
}
DCHECK(profile->GetOriginalProfile());
profile->GetOriginalProfile()->DestroyOffTheRecordProfile();
}
ProfileDestroyer::ProfileDestroyer(Profile* const profile, HostSet* hosts)
: timer_(false, false),
num_hosts_(0),
profile_(profile),
weak_ptr_factory_(this) {
if (pending_destroyers_ == NULL)
pending_destroyers_ = new DestroyerSet;
pending_destroyers_->insert(this);
for (HostSet::iterator i = hosts->begin(); i != hosts->end(); ++i) {
(*i)->AddObserver(this);
++num_hosts_;
}
if (num_hosts_) {
timer_.Start(FROM_HERE,
base::TimeDelta::FromSeconds(kTimerDelaySeconds),
base::Bind(&ProfileDestroyer::DestroyProfile,
weak_ptr_factory_.GetWeakPtr()));
}
}
ProfileDestroyer::~ProfileDestroyer() {
if (profile_)
DestroyProfileWhenAppropriate(profile_);
CHECK_EQ(0U, num_hosts_) << "Some render process hosts were not "
<< "destroyed early enough!";
DCHECK(pending_destroyers_ != NULL);
DestroyerSet::iterator iter = pending_destroyers_->find(this);
DCHECK(iter != pending_destroyers_->end());
pending_destroyers_->erase(iter);
if (pending_destroyers_->empty()) {
delete pending_destroyers_;
pending_destroyers_ = NULL;
}
}
void ProfileDestroyer::RenderProcessHostDestroyed(
content::RenderProcessHost* host) {
DCHECK(num_hosts_ > 0);
--num_hosts_;
if (num_hosts_ == 0) {
base::MessageLoop::current()->PostTask(
FROM_HERE, base::Bind(
&ProfileDestroyer::DestroyProfile, weak_ptr_factory_.GetWeakPtr()));
}
}
void ProfileDestroyer::DestroyProfile() {
if (!profile_) {
delete this;
return;
}
DCHECK(profile_->IsOffTheRecord());
DCHECK(profile_->GetOriginalProfile());
profile_->GetOriginalProfile()->DestroyOffTheRecordProfile();
profile_ = NULL;
timer_.Stop();
delete this;
}
bool ProfileDestroyer::GetHostsForProfile(
Profile* const profile, HostSet* hosts) {
for (content::RenderProcessHost::iterator iter(
content::RenderProcessHost::AllHostsIterator());
!iter.IsAtEnd(); iter.Advance()) {
content::RenderProcessHost* render_process_host = iter.GetCurrentValue();
if (render_process_host &&
render_process_host->GetBrowserContext() == profile) {
hosts->insert(render_process_host);
}
}
return !hosts->empty();
}