OLD | NEW |
---|---|
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 | 15 |
16 namespace { | 16 namespace { |
17 | 17 |
18 const int64 kTimerDelaySeconds = 1; | 18 const int64 kTimerDelaySeconds = 1; |
19 | 19 |
20 } // namespace | 20 } // namespace |
21 | 21 |
22 std::vector<ProfileDestroyer*>* ProfileDestroyer::pending_destroyers_ = NULL; | 22 std::vector<ProfileDestroyer*>* ProfileDestroyer::pending_destroyers_ = NULL; |
23 | 23 |
24 // static | 24 // static |
25 void ProfileDestroyer::DestroyProfileWhenAppropriate(Profile* const profile) { | 25 void ProfileDestroyer::DestroyProfileWhenAppropriate(Profile* const profile) { |
26 DCHECK(profile); | 26 DCHECK(profile); |
27 profile->MaybeSendDestroyedNotification(); | |
28 | |
27 std::vector<content::RenderProcessHost*> hosts; | 29 std::vector<content::RenderProcessHost*> hosts; |
28 // Testing profiles can simply be deleted directly. Some tests don't setup | 30 // Testing profiles can simply be deleted directly. Some tests don't setup |
29 // RenderProcessHost correctly and don't necessary run on the UI thread | 31 // RenderProcessHost correctly and don't necessary run on the UI thread |
30 // anyway, so we can't use the AllHostIterator. | 32 // anyway, so we can't use the AllHostIterator. |
31 if (profile->AsTestingProfile() == NULL) { | 33 if (profile->AsTestingProfile() == NULL) { |
32 GetHostsForProfile(profile, &hosts); | 34 GetHostsForProfile(profile, &hosts); |
33 if (!profile->IsOffTheRecord() && profile->HasOffTheRecordProfile()) | 35 if (!profile->IsOffTheRecord() && profile->HasOffTheRecordProfile()) |
34 GetHostsForProfile(profile->GetOffTheRecordProfile(), &hosts); | 36 GetHostsForProfile(profile->GetOffTheRecordProfile(), &hosts); |
35 } | 37 } |
Matt Perry
2012/07/27 00:37:09
What if we just moved this test and the new notifi
Yoyo Zhou
2012/07/27 00:45:13
Did you mean the test right below this line? (Oops
Matt Perry
2012/07/27 00:51:24
Ah, nevermind then.
| |
36 // This should never happen for non Off the record profile, this means that | 38 // 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!!! | 39 // there is a leak in a render process host that MUST BE FIXED!!! |
38 DCHECK(hosts.empty() || profile->IsOffTheRecord()); | 40 DCHECK(hosts.empty() || profile->IsOffTheRecord()); |
39 // Note that we still test for !profile->IsOffTheRecord here even though we | 41 // 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 | 42 // 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. | 43 // we need to identify if there are leaks when we run Debug builds. |
42 if (hosts.empty() || !profile->IsOffTheRecord()) { | 44 if (hosts.empty() || !profile->IsOffTheRecord()) { |
43 if (profile->IsOffTheRecord()) | 45 if (profile->IsOffTheRecord()) |
44 profile->GetOriginalProfile()->DestroyOffTheRecordProfile(); | 46 profile->GetOriginalProfile()->DestroyOffTheRecordProfile(); |
45 else | 47 else |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
78 Profile* const profile, | 80 Profile* const profile, |
79 const std::vector<content::RenderProcessHost*>& hosts) | 81 const std::vector<content::RenderProcessHost*>& hosts) |
80 : timer_(false, false), | 82 : timer_(false, false), |
81 num_hosts_(0), | 83 num_hosts_(0), |
82 profile_(profile) { | 84 profile_(profile) { |
83 if (pending_destroyers_ == NULL) | 85 if (pending_destroyers_ == NULL) |
84 pending_destroyers_ = new std::vector<ProfileDestroyer*>; | 86 pending_destroyers_ = new std::vector<ProfileDestroyer*>; |
85 pending_destroyers_->push_back(this); | 87 pending_destroyers_->push_back(this); |
86 for (size_t i = 0; i < hosts.size(); ++i) { | 88 for (size_t i = 0; i < hosts.size(); ++i) { |
87 registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_TERMINATED, | 89 registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_TERMINATED, |
88 content::Source<content::RenderProcessHost>(hosts[i])); | 90 content::Source<content::RenderProcessHost>(hosts[i])); |
89 // For each of the notifications, we bump up our reference count. | 91 // For each of the notifications, we bump up our reference count. |
90 // It will go back to 0 and free us when all hosts are terminated. | 92 // It will go back to 0 and free us when all hosts are terminated. |
91 ++num_hosts_; | 93 ++num_hosts_; |
92 } | 94 } |
93 // If we are going to wait for render process hosts, we don't want to do it | 95 // If we are going to wait for render process hosts, we don't want to do it |
94 // for longer than kTimerDelaySeconds. | 96 // for longer than kTimerDelaySeconds. |
95 if (num_hosts_) { | 97 if (num_hosts_) { |
96 timer_.Start(FROM_HERE, | 98 timer_.Start(FROM_HERE, |
97 base::TimeDelta::FromSeconds(kTimerDelaySeconds), | 99 base::TimeDelta::FromSeconds(kTimerDelaySeconds), |
98 base::Bind(&ProfileDestroyer::DestroyProfile, this)); | 100 base::Bind(&ProfileDestroyer::DestroyProfile, this)); |
(...skipping 20 matching lines...) Expand all Loading... | |
119 if (pending_destroyers_->empty()) { | 121 if (pending_destroyers_->empty()) { |
120 delete pending_destroyers_; | 122 delete pending_destroyers_; |
121 pending_destroyers_ = NULL; | 123 pending_destroyers_ = NULL; |
122 } | 124 } |
123 } | 125 } |
124 | 126 |
125 void ProfileDestroyer::Observe(int type, | 127 void ProfileDestroyer::Observe(int type, |
126 const content::NotificationSource& source, | 128 const content::NotificationSource& source, |
127 const content::NotificationDetails& details) { | 129 const content::NotificationDetails& details) { |
128 DCHECK(type == content::NOTIFICATION_RENDERER_PROCESS_TERMINATED); | 130 DCHECK(type == content::NOTIFICATION_RENDERER_PROCESS_TERMINATED); |
131 registrar_.Remove(this, content::NOTIFICATION_RENDERER_PROCESS_TERMINATED, | |
132 source); | |
129 DCHECK(num_hosts_ > 0); | 133 DCHECK(num_hosts_ > 0); |
130 --num_hosts_; | 134 --num_hosts_; |
131 if (num_hosts_ == 0) { | 135 if (num_hosts_ == 0) { |
132 // Delay the destruction one step further in case other observers of this | 136 // Delay the destruction one step further in case other observers of this |
133 // notification need to look at the profile attached to the host. | 137 // notification need to look at the profile attached to the host. |
134 MessageLoop::current()->PostTask( | 138 MessageLoop::current()->PostTask( |
135 FROM_HERE, base::Bind(&ProfileDestroyer::DestroyProfile, this)); | 139 FROM_HERE, base::Bind(&ProfileDestroyer::DestroyProfile, this)); |
136 } | 140 } |
137 } | 141 } |
138 | 142 |
(...skipping 22 matching lines...) Expand all Loading... | |
161 content::RenderProcessHost::AllHostsIterator()); | 165 content::RenderProcessHost::AllHostsIterator()); |
162 !iter.IsAtEnd(); iter.Advance()) { | 166 !iter.IsAtEnd(); iter.Advance()) { |
163 content::RenderProcessHost* render_process_host = iter.GetCurrentValue(); | 167 content::RenderProcessHost* render_process_host = iter.GetCurrentValue(); |
164 if (render_process_host && Profile::FromBrowserContext( | 168 if (render_process_host && Profile::FromBrowserContext( |
165 render_process_host->GetBrowserContext()) == profile) { | 169 render_process_host->GetBrowserContext()) == profile) { |
166 hosts->push_back(render_process_host); | 170 hosts->push_back(render_process_host); |
167 } | 171 } |
168 } | 172 } |
169 return !hosts->empty(); | 173 return !hosts->empty(); |
170 } | 174 } |
OLD | NEW |