Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(68)

Side by Side Diff: chrome/browser/profiles/profile_destroyer.cc

Issue 10645005: Modify ProfileDestroyer to prevent leaks. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Created 8 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « chrome/browser/profiles/profile_destroyer.h ('k') | chrome/browser/profiles/profile_impl.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/profiles/profile_destroyer.h" 5 #include "chrome/browser/profiles/profile_destroyer.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/memory/scoped_ptr.h" 8 #include "base/memory/scoped_ptr.h"
9 #include "base/message_loop.h" 9 #include "base/message_loop.h"
10 #include "chrome/browser/profiles/profile.h" 10 #include "chrome/browser/profiles/profile.h"
11 #include "content/public/browser/notification_source.h" 11 #include "content/public/browser/notification_source.h"
12 #include "content/public/browser/notification_types.h" 12 #include "content/public/browser/notification_types.h"
13 #include "content/public/browser/render_process_host.h" 13 #include "content/public/browser/render_process_host.h"
14 14
15
16 namespace {
17
18 const int64 kTimerDelaySeconds = 1;
19
20 } // namespace
21
22 std::vector<ProfileDestroyer*>* ProfileDestroyer::pending_destroyers_ = NULL;
23
15 // static 24 // static
16 void ProfileDestroyer::DestroyProfileWhenAppropriate(Profile* const profile) { 25 void ProfileDestroyer::DestroyProfileWhenAppropriate(Profile* const profile) {
26 DCHECK(profile);
17 std::vector<content::RenderProcessHost*> hosts; 27 std::vector<content::RenderProcessHost*> hosts;
18 // Testing profiles can simply be deleted directly. Some tests don't setup 28 // Testing profiles can simply be deleted directly. Some tests don't setup
19 // RenderProcessHost correctly and don't necessary run on the UI thread 29 // RenderProcessHost correctly and don't necessary run on the UI thread
20 // anyway, so we can't use the AllHostIterator. 30 // anyway, so we can't use the AllHostIterator.
21 if (profile->AsTestingProfile() == NULL) { 31 if (profile->AsTestingProfile() == NULL) {
22 GetHostsForProfile(profile, &hosts); 32 GetHostsForProfile(profile, &hosts);
23 if (!profile->IsOffTheRecord() && profile->HasOffTheRecordProfile()) 33 if (!profile->IsOffTheRecord() && profile->HasOffTheRecordProfile())
24 GetHostsForProfile(profile->GetOffTheRecordProfile(), &hosts); 34 GetHostsForProfile(profile->GetOffTheRecordProfile(), &hosts);
25 } 35 }
26 if (hosts.empty()) { 36 // This should never happen for non Off the record profile, this means that
37 // there is a leak in a render process host that MUST BE FIXED!!!
38 DCHECK(hosts.empty() || profile->IsOffTheRecord());
39 // Note that we still test for !profile->IsOffTheRecord here even though we
40 // DCHECK'd above because we want to protect Release builds against this even
41 // we need to identify if there are leaks when we run Debug builds.
42 if (hosts.empty() || !profile->IsOffTheRecord()) {
27 if (profile->IsOffTheRecord()) 43 if (profile->IsOffTheRecord())
28 profile->GetOriginalProfile()->DestroyOffTheRecordProfile(); 44 profile->GetOriginalProfile()->DestroyOffTheRecordProfile();
29 else 45 else
30 delete profile; 46 delete profile;
31 } else { 47 } else {
32 // The instance will destroy itself once all render process hosts referring 48 // The instance will destroy itself once all render process hosts referring
33 // to it and/or it's off the record profile are properly terminated. 49 // to it are properly terminated.
34 scoped_refptr<ProfileDestroyer> profile_destroyer( 50 scoped_refptr<ProfileDestroyer> profile_destroyer(
35 new ProfileDestroyer(profile, hosts)); 51 new ProfileDestroyer(profile, hosts));
36 } 52 }
37 } 53 }
38 54
55 // This can be called to cancel any pending destruction and destroy the profile
56 // now, e.g., if the parent profile is being destroyed while the incognito one
57 // still pending...
58 void ProfileDestroyer::DestroyOffTheRecordProfileNow(Profile* const profile) {
59 DCHECK(profile);
60 DCHECK(profile->IsOffTheRecord());
61 if (pending_destroyers_) {
62 for (size_t i = 0; i < pending_destroyers_->size(); ++i) {
63 if ((*pending_destroyers_)[i]->profile_ == profile) {
64 // We want to signal this in debug builds so that we don't lose sight of
65 // these potential leaks, but we handle it in release so that we don't
66 // crash or corrupt profile data on disk.
67 NOTREACHED() << "A render process host wasn't destroyed early enough.";
68 (*pending_destroyers_)[i]->profile_ = NULL;
69 break;
70 }
71 }
72 }
73 DCHECK(profile->GetOriginalProfile());
74 profile->GetOriginalProfile()->DestroyOffTheRecordProfile();
75 }
76
39 ProfileDestroyer::ProfileDestroyer( 77 ProfileDestroyer::ProfileDestroyer(
40 Profile* const profile, 78 Profile* const profile,
41 const std::vector<content::RenderProcessHost*>& hosts) : profile_(profile) { 79 const std::vector<content::RenderProcessHost*>& hosts)
80 : timer_(false, false),
81 num_hosts_(0),
82 profile_(profile) {
83 if (pending_destroyers_ == NULL)
84 pending_destroyers_ = new std::vector<ProfileDestroyer*>;
85 pending_destroyers_->push_back(this);
42 for (size_t i = 0; i < hosts.size(); ++i) { 86 for (size_t i = 0; i < hosts.size(); ++i) {
43 registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_TERMINATED, 87 registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_TERMINATED,
44 content::Source<content::RenderProcessHost>(hosts[i])); 88 content::Source<content::RenderProcessHost>(hosts[i]));
45 // For each of the notifications, we bump up our reference count. 89 // For each of the notifications, we bump up our reference count.
46 // It will go back to 0 and free us when all hosts are terminated. 90 // It will go back to 0 and free us when all hosts are terminated.
47 AddRef(); 91 ++num_hosts_;
92 }
93 // If we are going to wait for render process hosts, we don't want to do it
94 // for longer than kTimerDelaySeconds.
95 if (num_hosts_) {
96 timer_.Start(FROM_HERE,
97 base::TimeDelta::FromSeconds(kTimerDelaySeconds),
98 base::Bind(&ProfileDestroyer::DestroyProfile, this));
48 } 99 }
49 } 100 }
50 101
51 ProfileDestroyer::~ProfileDestroyer() { 102 ProfileDestroyer::~ProfileDestroyer() {
52 // Check again, in case other render hosts were added while we were 103 // Check again, in case other render hosts were added while we were
53 // waiting for the previous ones to go away... 104 // waiting for the previous ones to go away...
54 DestroyProfileWhenAppropriate(profile_); 105 if (profile_)
106 DestroyProfileWhenAppropriate(profile_);
107
108 // We shouldn't be deleted with pending notifications.
109 DCHECK(registrar_.IsEmpty());
110
111 DCHECK(pending_destroyers_ != NULL);
112 std::vector<ProfileDestroyer*>::iterator iter = std::find(
113 pending_destroyers_->begin(), pending_destroyers_->end(), this);
114 DCHECK(iter != pending_destroyers_->end());
115 pending_destroyers_->erase(iter);
116 DCHECK(pending_destroyers_->end() == std::find(pending_destroyers_->begin(),
117 pending_destroyers_->end(),
118 this));
119 if (pending_destroyers_->empty()) {
120 delete pending_destroyers_;
121 pending_destroyers_ = NULL;
122 }
55 } 123 }
56 124
57 void ProfileDestroyer::Observe(int type, 125 void ProfileDestroyer::Observe(int type,
58 const content::NotificationSource& source, 126 const content::NotificationSource& source,
59 const content::NotificationDetails& details) { 127 const content::NotificationDetails& details) {
60 DCHECK(type == content::NOTIFICATION_RENDERER_PROCESS_TERMINATED); 128 DCHECK(type == content::NOTIFICATION_RENDERER_PROCESS_TERMINATED);
61 // Delay the destruction one step further in case other observers of this 129 DCHECK(num_hosts_ > 0);
62 // notification need to look at the profile attached to the host. 130 --num_hosts_;
63 MessageLoop::current()->PostTask( 131 if (num_hosts_ == 0) {
64 FROM_HERE, base::Bind(&ProfileDestroyer::Release, this)); 132 // Delay the destruction one step further in case other observers of this
133 // notification need to look at the profile attached to the host.
134 MessageLoop::current()->PostTask(
135 FROM_HERE, base::Bind(&ProfileDestroyer::DestroyProfile, this));
136 }
137 }
138
139 void ProfileDestroyer::DestroyProfile() {
140 // We might have been cancelled externally before the timer expired.
141 if (profile_ == NULL)
142 return;
143 DCHECK(profile_->IsOffTheRecord());
144 DCHECK(profile_->GetOriginalProfile());
145 profile_->GetOriginalProfile()->DestroyOffTheRecordProfile();
146 profile_ = NULL;
147
148 // Don't wait for pending registrations, if any, these hosts are buggy.
149 DCHECK(registrar_.IsEmpty()) << "Some render process hosts where not "
150 << "destroyed early enough!";
151 registrar_.RemoveAll();
152
153 // And stop the timer so we can be released early too.
154 timer_.Stop();
65 } 155 }
66 156
67 // static 157 // static
68 bool ProfileDestroyer::GetHostsForProfile( 158 bool ProfileDestroyer::GetHostsForProfile(
69 Profile* const profile, std::vector<content::RenderProcessHost*>* hosts) { 159 Profile* const profile, std::vector<content::RenderProcessHost*>* hosts) {
70 for (content::RenderProcessHost::iterator iter( 160 for (content::RenderProcessHost::iterator iter(
71 content::RenderProcessHost::AllHostsIterator()); 161 content::RenderProcessHost::AllHostsIterator());
72 !iter.IsAtEnd(); iter.Advance()) { 162 !iter.IsAtEnd(); iter.Advance()) {
73 content::RenderProcessHost* render_process_host = iter.GetCurrentValue(); 163 content::RenderProcessHost* render_process_host = iter.GetCurrentValue();
74 if (render_process_host && Profile::FromBrowserContext( 164 if (render_process_host && Profile::FromBrowserContext(
75 render_process_host->GetBrowserContext()) == profile) { 165 render_process_host->GetBrowserContext()) == profile) {
76 hosts->push_back(render_process_host); 166 hosts->push_back(render_process_host);
77 } 167 }
78 } 168 }
79 return !hosts->empty(); 169 return !hosts->empty();
80 } 170 }
OLDNEW
« no previous file with comments | « chrome/browser/profiles/profile_destroyer.h ('k') | chrome/browser/profiles/profile_impl.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698