Fix build break
[chromium-blink-merge.git] / chrome / browser / profiles / profile_destroyer.cc
blob8c98be2d0669b0884a0712511309e7981390d126
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"
7 #include "base/bind.h"
8 #include "base/memory/scoped_ptr.h"
9 #include "base/message_loop.h"
10 #include "chrome/browser/profiles/profile.h"
11 #include "content/public/browser/notification_source.h"
12 #include "content/public/browser/notification_types.h"
13 #include "content/public/browser/render_process_host.h"
16 namespace {
18 const int64 kTimerDelaySeconds = 1;
20 } // namespace
22 std::vector<ProfileDestroyer*>* ProfileDestroyer::pending_destroyers_ = NULL;
24 // static
25 void ProfileDestroyer::DestroyProfileWhenAppropriate(Profile* const profile) {
26 DCHECK(profile);
27 profile->MaybeSendDestroyedNotification();
29 std::vector<content::RenderProcessHost*> hosts;
30 // Testing profiles can simply be deleted directly. Some tests don't setup
31 // RenderProcessHost correctly and don't necessary run on the UI thread
32 // anyway, so we can't use the AllHostIterator.
33 if (profile->AsTestingProfile() == NULL) {
34 GetHostsForProfile(profile, &hosts);
35 if (!profile->IsOffTheRecord() && profile->HasOffTheRecordProfile())
36 GetHostsForProfile(profile->GetOffTheRecordProfile(), &hosts);
38 // Generally, !hosts.empty() means that there is a leak in a render process
39 // host that MUST BE FIXED!!!
41 // However, off-the-record profiles are destroyed before their
42 // RenderProcessHosts in order to erase private data quickly, and
43 // RenderProcessHostImpl::Release() avoids destroying RenderProcessHosts in
44 // --single-process mode to avoid race conditions.
45 DCHECK(hosts.empty() || profile->IsOffTheRecord() ||
46 content::RenderProcessHost::run_renderer_in_process());
47 // Note that we still test for !profile->IsOffTheRecord here even though we
48 // DCHECK'd above because we want to protect Release builds against this even
49 // we need to identify if there are leaks when we run Debug builds.
50 if (hosts.empty() || !profile->IsOffTheRecord()) {
51 if (profile->IsOffTheRecord())
52 profile->GetOriginalProfile()->DestroyOffTheRecordProfile();
53 else
54 delete profile;
55 } else {
56 // The instance will destroy itself once all render process hosts referring
57 // to it are properly terminated.
58 scoped_refptr<ProfileDestroyer> profile_destroyer(
59 new ProfileDestroyer(profile, hosts));
63 // This can be called to cancel any pending destruction and destroy the profile
64 // now, e.g., if the parent profile is being destroyed while the incognito one
65 // still pending...
66 void ProfileDestroyer::DestroyOffTheRecordProfileNow(Profile* const profile) {
67 DCHECK(profile);
68 DCHECK(profile->IsOffTheRecord());
69 if (pending_destroyers_) {
70 for (size_t i = 0; i < pending_destroyers_->size(); ++i) {
71 if ((*pending_destroyers_)[i]->profile_ == profile) {
72 // We want to signal this in debug builds so that we don't lose sight of
73 // these potential leaks, but we handle it in release so that we don't
74 // crash or corrupt profile data on disk.
75 NOTREACHED() << "A render process host wasn't destroyed early enough.";
76 (*pending_destroyers_)[i]->profile_ = NULL;
77 break;
81 DCHECK(profile->GetOriginalProfile());
82 profile->GetOriginalProfile()->DestroyOffTheRecordProfile();
85 ProfileDestroyer::ProfileDestroyer(
86 Profile* const profile,
87 const std::vector<content::RenderProcessHost*>& hosts)
88 : timer_(false, false),
89 num_hosts_(0),
90 profile_(profile) {
91 if (pending_destroyers_ == NULL)
92 pending_destroyers_ = new std::vector<ProfileDestroyer*>;
93 pending_destroyers_->push_back(this);
94 for (size_t i = 0; i < hosts.size(); ++i) {
95 registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_TERMINATED,
96 content::Source<content::RenderProcessHost>(hosts[i]));
97 // For each of the notifications, we bump up our reference count.
98 // It will go back to 0 and free us when all hosts are terminated.
99 ++num_hosts_;
101 // If we are going to wait for render process hosts, we don't want to do it
102 // for longer than kTimerDelaySeconds.
103 if (num_hosts_) {
104 timer_.Start(FROM_HERE,
105 base::TimeDelta::FromSeconds(kTimerDelaySeconds),
106 base::Bind(&ProfileDestroyer::DestroyProfile, this));
110 ProfileDestroyer::~ProfileDestroyer() {
111 // Check again, in case other render hosts were added while we were
112 // waiting for the previous ones to go away...
113 if (profile_)
114 DestroyProfileWhenAppropriate(profile_);
116 // We shouldn't be deleted with pending notifications.
117 DCHECK(registrar_.IsEmpty());
119 DCHECK(pending_destroyers_ != NULL);
120 std::vector<ProfileDestroyer*>::iterator iter = std::find(
121 pending_destroyers_->begin(), pending_destroyers_->end(), this);
122 DCHECK(iter != pending_destroyers_->end());
123 pending_destroyers_->erase(iter);
124 DCHECK(pending_destroyers_->end() == std::find(pending_destroyers_->begin(),
125 pending_destroyers_->end(),
126 this));
127 if (pending_destroyers_->empty()) {
128 delete pending_destroyers_;
129 pending_destroyers_ = NULL;
133 void ProfileDestroyer::Observe(int type,
134 const content::NotificationSource& source,
135 const content::NotificationDetails& details) {
136 DCHECK(type == content::NOTIFICATION_RENDERER_PROCESS_TERMINATED);
137 registrar_.Remove(this, content::NOTIFICATION_RENDERER_PROCESS_TERMINATED,
138 source);
139 DCHECK(num_hosts_ > 0);
140 --num_hosts_;
141 if (num_hosts_ == 0) {
142 // Delay the destruction one step further in case other observers of this
143 // notification need to look at the profile attached to the host.
144 MessageLoop::current()->PostTask(
145 FROM_HERE, base::Bind(&ProfileDestroyer::DestroyProfile, this));
149 void ProfileDestroyer::DestroyProfile() {
150 // We might have been cancelled externally before the timer expired.
151 if (profile_ == NULL)
152 return;
153 DCHECK(profile_->IsOffTheRecord());
154 DCHECK(profile_->GetOriginalProfile());
155 profile_->GetOriginalProfile()->DestroyOffTheRecordProfile();
156 profile_ = NULL;
158 // Don't wait for pending registrations, if any, these hosts are buggy.
159 DCHECK(registrar_.IsEmpty()) << "Some render process hosts where not "
160 << "destroyed early enough!";
161 registrar_.RemoveAll();
163 // And stop the timer so we can be released early too.
164 timer_.Stop();
167 // static
168 bool ProfileDestroyer::GetHostsForProfile(
169 Profile* const profile, std::vector<content::RenderProcessHost*>* hosts) {
170 for (content::RenderProcessHost::iterator iter(
171 content::RenderProcessHost::AllHostsIterator());
172 !iter.IsAtEnd(); iter.Advance()) {
173 content::RenderProcessHost* render_process_host = iter.GetCurrentValue();
174 if (render_process_host && Profile::FromBrowserContext(
175 render_process_host->GetBrowserContext()) == profile) {
176 hosts->push_back(render_process_host);
179 return !hosts->empty();