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/debug/trace_event.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/message_loop/message_loop.h"
11 #include "chrome/browser/profiles/profile.h"
12 #include "content/public/browser/notification_source.h"
13 #include "content/public/browser/notification_types.h"
14 #include "content/public/browser/render_process_host.h"
19 #if defined(OS_ANDROID)
20 // Set the render host waiting time to 5s on Android, that's the same
22 const int64 kTimerDelaySeconds
= 5;
24 const int64 kTimerDelaySeconds
= 1;
29 std::vector
<ProfileDestroyer
*>* ProfileDestroyer::pending_destroyers_
= NULL
;
32 void ProfileDestroyer::DestroyProfileWhenAppropriate(Profile
* const profile
) {
33 TRACE_EVENT0("shutdown", "ProfileDestroyer::DestroyProfileWhenAppropriate");
36 profile
->MaybeSendDestroyedNotification();
38 std::vector
<content::RenderProcessHost
*> hosts
;
39 // Testing profiles can simply be deleted directly. Some tests don't setup
40 // RenderProcessHost correctly and don't necessary run on the UI thread
41 // anyway, so we can't use the AllHostIterator.
42 if (profile
->AsTestingProfile() == NULL
) {
43 GetHostsForProfile(profile
, &hosts
);
44 if (!profile
->IsOffTheRecord() && profile
->HasOffTheRecordProfile())
45 GetHostsForProfile(profile
->GetOffTheRecordProfile(), &hosts
);
47 // Generally, !hosts.empty() means that there is a leak in a render process
48 // host that MUST BE FIXED!!!
50 // However, off-the-record profiles are destroyed before their
51 // RenderProcessHosts in order to erase private data quickly, and
52 // RenderProcessHostImpl::Release() avoids destroying RenderProcessHosts in
53 // --single-process mode to avoid race conditions.
54 DCHECK(hosts
.empty() || profile
->IsOffTheRecord() ||
55 content::RenderProcessHost::run_renderer_in_process()) << \
56 "Profile still has " << hosts
.size() << " hosts";
57 // Note that we still test for !profile->IsOffTheRecord here even though we
58 // DCHECK'd above because we want to protect Release builds against this even
59 // we need to identify if there are leaks when we run Debug builds.
60 if (hosts
.empty() || !profile
->IsOffTheRecord()) {
61 if (profile
->IsOffTheRecord())
62 profile
->GetOriginalProfile()->DestroyOffTheRecordProfile();
66 // The instance will destroy itself once all render process hosts referring
67 // to it are properly terminated.
68 new ProfileDestroyer(profile
, hosts
);
72 // This can be called to cancel any pending destruction and destroy the profile
73 // now, e.g., if the parent profile is being destroyed while the incognito one
75 void ProfileDestroyer::DestroyOffTheRecordProfileNow(Profile
* const profile
) {
77 DCHECK(profile
->IsOffTheRecord());
78 if (pending_destroyers_
) {
79 for (size_t i
= 0; i
< pending_destroyers_
->size(); ++i
) {
80 if ((*pending_destroyers_
)[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 (*pending_destroyers_
)[i
]->profile_
= NULL
;
90 DCHECK(profile
->GetOriginalProfile());
91 profile
->GetOriginalProfile()->DestroyOffTheRecordProfile();
94 ProfileDestroyer::ProfileDestroyer(
95 Profile
* const profile
,
96 const std::vector
<content::RenderProcessHost
*>& hosts
)
97 : timer_(false, false),
100 weak_ptr_factory_(this) {
101 if (pending_destroyers_
== NULL
)
102 pending_destroyers_
= new std::vector
<ProfileDestroyer
*>;
103 pending_destroyers_
->push_back(this);
104 for (size_t i
= 0; i
< hosts
.size(); ++i
) {
105 registrar_
.Add(this, content::NOTIFICATION_RENDERER_PROCESS_TERMINATED
,
106 content::Source
<content::RenderProcessHost
>(hosts
[i
]));
107 // For each of the notifications, we bump up our reference count.
108 // It will go back to 0 and free us when all hosts are terminated.
111 // If we are going to wait for render process hosts, we don't want to do it
112 // for longer than kTimerDelaySeconds.
114 timer_
.Start(FROM_HERE
,
115 base::TimeDelta::FromSeconds(kTimerDelaySeconds
),
116 base::Bind(&ProfileDestroyer::DestroyProfile
,
117 weak_ptr_factory_
.GetWeakPtr()));
121 ProfileDestroyer::~ProfileDestroyer() {
122 // Check again, in case other render hosts were added while we were
123 // waiting for the previous ones to go away...
125 DestroyProfileWhenAppropriate(profile_
);
127 // We shouldn't be deleted with pending notifications.
128 DCHECK(registrar_
.IsEmpty());
130 DCHECK(pending_destroyers_
!= NULL
);
131 std::vector
<ProfileDestroyer
*>::iterator iter
= std::find(
132 pending_destroyers_
->begin(), pending_destroyers_
->end(), this);
133 DCHECK(iter
!= pending_destroyers_
->end());
134 pending_destroyers_
->erase(iter
);
135 DCHECK(pending_destroyers_
->end() == std::find(pending_destroyers_
->begin(),
136 pending_destroyers_
->end(),
138 if (pending_destroyers_
->empty()) {
139 delete pending_destroyers_
;
140 pending_destroyers_
= NULL
;
144 void ProfileDestroyer::Observe(int type
,
145 const content::NotificationSource
& source
,
146 const content::NotificationDetails
& details
) {
147 DCHECK(type
== content::NOTIFICATION_RENDERER_PROCESS_TERMINATED
);
148 registrar_
.Remove(this, content::NOTIFICATION_RENDERER_PROCESS_TERMINATED
,
150 DCHECK(num_hosts_
> 0);
152 if (num_hosts_
== 0) {
153 // Delay the destruction one step further in case other observers of this
154 // notification need to look at the profile attached to the host.
155 base::MessageLoop::current()->PostTask(
156 FROM_HERE
, base::Bind(
157 &ProfileDestroyer::DestroyProfile
, weak_ptr_factory_
.GetWeakPtr()));
161 void ProfileDestroyer::DestroyProfile() {
162 // We might have been cancelled externally before the timer expired.
163 if (profile_
== NULL
)
165 DCHECK(profile_
->IsOffTheRecord());
166 DCHECK(profile_
->GetOriginalProfile());
167 profile_
->GetOriginalProfile()->DestroyOffTheRecordProfile();
170 // Don't wait for pending registrations, if any, these hosts are buggy.
171 // Note: this can happen, but if so, it's better to crash here than wait
172 // for the host to dereference a deleted Profile. http://crbug.com/248625
173 CHECK(registrar_
.IsEmpty()) << "Some render process hosts were not "
174 << "destroyed early enough!";
176 // And stop the timer so we can be released early too.
183 bool ProfileDestroyer::GetHostsForProfile(
184 Profile
* const profile
, std::vector
<content::RenderProcessHost
*>* hosts
) {
185 for (content::RenderProcessHost::iterator
iter(
186 content::RenderProcessHost::AllHostsIterator());
187 !iter
.IsAtEnd(); iter
.Advance()) {
188 content::RenderProcessHost
* render_process_host
= iter
.GetCurrentValue();
189 if (render_process_host
&& Profile::FromBrowserContext(
190 render_process_host
->GetBrowserContext()) == profile
) {
191 hosts
->push_back(render_process_host
);
194 return !hosts
->empty();