1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/browser/profiles/profile_destroyer.h"
8 #include "base/location.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/single_thread_task_runner.h"
11 #include "base/thread_task_runner_handle.h"
12 #include "base/trace_event/trace_event.h"
13 #include "chrome/browser/profiles/profile.h"
14 #include "content/public/browser/render_process_host.h"
18 #if defined(OS_ANDROID)
19 // Set the render host waiting time to 5s on Android, that's the same
20 // as an "Application Not Responding" timeout.
21 const int64 kTimerDelaySeconds
= 5;
23 const int64 kTimerDelaySeconds
= 1;
28 ProfileDestroyer::DestroyerSet
* ProfileDestroyer::pending_destroyers_
= NULL
;
31 void ProfileDestroyer::DestroyProfileWhenAppropriate(Profile
* const profile
) {
32 TRACE_EVENT0("shutdown", "ProfileDestroyer::DestroyProfileWhenAppropriate");
35 profile
->MaybeSendDestroyedNotification();
38 // Testing profiles can simply be deleted directly. Some tests don't setup
39 // RenderProcessHost correctly and don't necessary run on the UI thread
40 // anyway, so we can't use the AllHostIterator.
41 if (profile
->AsTestingProfile() == NULL
) {
42 GetHostsForProfile(profile
, &hosts
);
43 if (!profile
->IsOffTheRecord() && profile
->HasOffTheRecordProfile())
44 GetHostsForProfile(profile
->GetOffTheRecordProfile(), &hosts
);
46 // Generally, !hosts.empty() means that there is a leak in a render process
47 // host that MUST BE FIXED!!!
49 // However, off-the-record profiles are destroyed before their
50 // RenderProcessHosts in order to erase private data quickly, and
51 // RenderProcessHostImpl::Release() avoids destroying RenderProcessHosts in
52 // --single-process mode to avoid race conditions.
53 DCHECK(hosts
.empty() || profile
->IsOffTheRecord() ||
54 content::RenderProcessHost::run_renderer_in_process()) << \
55 "Profile still has " << hosts
.size() << " hosts";
56 // Note that we still test for !profile->IsOffTheRecord here even though we
57 // DCHECK'd above because we want to protect Release builds against this even
58 // we need to identify if there are leaks when we run Debug builds.
59 if (hosts
.empty() || !profile
->IsOffTheRecord()) {
60 if (profile
->IsOffTheRecord())
61 profile
->GetOriginalProfile()->DestroyOffTheRecordProfile();
65 // The instance will destroy itself once all render process hosts referring
66 // to it are properly terminated.
67 new ProfileDestroyer(profile
, &hosts
);
71 // This can be called to cancel any pending destruction and destroy the profile
72 // now, e.g., if the parent profile is being destroyed while the incognito one
74 void ProfileDestroyer::DestroyOffTheRecordProfileNow(Profile
* const profile
) {
76 DCHECK(profile
->IsOffTheRecord());
77 if (pending_destroyers_
) {
78 for (DestroyerSet::iterator i
= pending_destroyers_
->begin();
79 i
!= pending_destroyers_
->end(); ++i
) {
80 if ((*i
)->profile_
== profile
) {
81 // We want to signal this in debug builds so that we don't lose sight of
82 // these potential leaks, but we handle it in release so that we don't
83 // crash or corrupt profile data on disk.
84 NOTREACHED() << "A render process host wasn't destroyed early enough.";
85 (*i
)->profile_
= NULL
;
90 DCHECK(profile
->GetOriginalProfile());
91 profile
->GetOriginalProfile()->DestroyOffTheRecordProfile();
94 ProfileDestroyer::ProfileDestroyer(Profile
* const profile
, HostSet
* hosts
)
95 : timer_(false, false),
98 weak_ptr_factory_(this) {
99 if (pending_destroyers_
== NULL
)
100 pending_destroyers_
= new DestroyerSet
;
101 pending_destroyers_
->insert(this);
102 for (HostSet::iterator i
= hosts
->begin(); i
!= hosts
->end(); ++i
) {
103 (*i
)->AddObserver(this);
104 // For each of the observations, we bump up our reference count.
105 // It will go back to 0 and free us when all hosts are terminated.
108 // If we are going to wait for render process hosts, we don't want to do it
109 // for longer than kTimerDelaySeconds.
111 timer_
.Start(FROM_HERE
,
112 base::TimeDelta::FromSeconds(kTimerDelaySeconds
),
113 base::Bind(&ProfileDestroyer::DestroyProfile
,
114 weak_ptr_factory_
.GetWeakPtr()));
118 ProfileDestroyer::~ProfileDestroyer() {
119 // Check again, in case other render hosts were added while we were
120 // waiting for the previous ones to go away...
122 DestroyProfileWhenAppropriate(profile_
);
124 // Don't wait for pending registrations, if any, these hosts are buggy.
125 // Note: this can happen, but if so, it's better to crash here than wait
126 // for the host to dereference a deleted Profile. http://crbug.com/248625
127 CHECK_EQ(0U, num_hosts_
) << "Some render process hosts were not "
128 << "destroyed early enough!";
130 DCHECK(pending_destroyers_
!= NULL
);
131 DestroyerSet::iterator iter
= pending_destroyers_
->find(this);
132 DCHECK(iter
!= pending_destroyers_
->end());
133 pending_destroyers_
->erase(iter
);
134 if (pending_destroyers_
->empty()) {
135 delete pending_destroyers_
;
136 pending_destroyers_
= NULL
;
140 void ProfileDestroyer::RenderProcessHostDestroyed(
141 content::RenderProcessHost
* host
) {
142 DCHECK(num_hosts_
> 0);
144 if (num_hosts_
== 0) {
145 // Delay the destruction one step further in case other observers need to
146 // look at the profile attached to the host.
147 base::ThreadTaskRunnerHandle::Get()->PostTask(
148 FROM_HERE
, base::Bind(&ProfileDestroyer::DestroyProfile
,
149 weak_ptr_factory_
.GetWeakPtr()));
153 void ProfileDestroyer::DestroyProfile() {
154 // We might have been cancelled externally before the timer expired.
160 DCHECK(profile_
->IsOffTheRecord());
161 DCHECK(profile_
->GetOriginalProfile());
162 profile_
->GetOriginalProfile()->DestroyOffTheRecordProfile();
165 // And stop the timer so we can be released early too.
172 bool ProfileDestroyer::GetHostsForProfile(
173 Profile
* const profile
, HostSet
* hosts
) {
174 for (content::RenderProcessHost::iterator
iter(
175 content::RenderProcessHost::AllHostsIterator());
176 !iter
.IsAtEnd(); iter
.Advance()) {
177 content::RenderProcessHost
* render_process_host
= iter
.GetCurrentValue();
178 if (render_process_host
&&
179 render_process_host
->GetBrowserContext() == profile
) {
180 hosts
->insert(render_process_host
);
183 return !hosts
->empty();