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/browser_thread.h" | |
11 #include "content/public/browser/notification_source.h" | 12 #include "content/public/browser/notification_source.h" |
12 #include "content/public/browser/notification_types.h" | 13 #include "content/public/browser/notification_types.h" |
13 #include "content/public/browser/render_process_host.h" | 14 #include "content/public/browser/render_process_host.h" |
14 | 15 |
15 // static | 16 // static |
16 void ProfileDestroyer::DestroyOffTheRecordProfile(Profile* const profile) { | 17 void ProfileDestroyer::DestroyProfileWhenAppropriate(Profile* const profile) { |
17 std::vector<content::RenderProcessHost*> hosts; | 18 std::vector<content::RenderProcessHost*> hosts; |
18 if (GetHostsForProfile(profile, &hosts)) { | 19 // Some tests try to destroy their profile on other threads than the UI. |
sky
2012/02/17 16:53:09
I hate to have to change around code like this for
MAD
2012/02/17 17:06:09
Yeah, I agree with you... I was lazy... I was scar
| |
20 // Ignore those, since we can't enumerate the hosts unless we're on the UI | |
21 // thread. | |
22 if (content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)) { | |
23 GetHostsForProfile(profile, &hosts); | |
24 if (!profile->IsOffTheRecord() && profile->HasOffTheRecordProfile()) | |
25 GetHostsForProfile(profile->GetOffTheRecordProfile(), &hosts); | |
26 } | |
27 | |
28 if (hosts.empty()) { | |
29 if (profile->IsOffTheRecord()) | |
30 profile->GetOriginalProfile()->DestroyOffTheRecordProfile(); | |
31 else | |
32 delete profile; | |
33 } else { | |
19 // The instance will destroy itself once all render process hosts referring | 34 // The instance will destroy itself once all render process hosts referring |
20 // to it are properly terminated. | 35 // to it and/or it's off the record profile are properly terminated. |
21 scoped_refptr<ProfileDestroyer> profile_destroyer( | 36 scoped_refptr<ProfileDestroyer> profile_destroyer( |
22 new ProfileDestroyer(profile, hosts)); | 37 new ProfileDestroyer(profile, hosts)); |
23 } else { | |
24 // Safe to destroy now... We're done... | |
25 profile->GetOriginalProfile()->DestroyOffTheRecordProfile(); | |
26 } | 38 } |
27 } | 39 } |
28 | 40 |
29 ProfileDestroyer::ProfileDestroyer( | 41 ProfileDestroyer::ProfileDestroyer( |
30 Profile* const profile, | 42 Profile* const profile, |
31 const std::vector<content::RenderProcessHost*>& hosts) : profile_(profile) { | 43 const std::vector<content::RenderProcessHost*>& hosts) : profile_(profile) { |
32 for (size_t i = 0; i < hosts.size(); ++i) { | 44 for (size_t i = 0; i < hosts.size(); ++i) { |
33 registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_TERMINATED, | 45 registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_TERMINATED, |
34 content::Source<content::RenderProcessHost>(hosts[i])); | 46 content::Source<content::RenderProcessHost>(hosts[i])); |
35 // For each of the notifications, we bump up our reference count. | 47 // For each of the notifications, we bump up our reference count. |
36 // It will go back to 0 and free us when all hosts are terminated. | 48 // It will go back to 0 and free us when all hosts are terminated. |
37 AddRef(); | 49 AddRef(); |
38 } | 50 } |
39 } | 51 } |
40 | 52 |
41 ProfileDestroyer::~ProfileDestroyer() { | 53 ProfileDestroyer::~ProfileDestroyer() { |
42 // Check again, in case other render hosts were added while we were | 54 // Check again, in case other render hosts were added while we were |
43 // waiting for the previous ones to go away... | 55 // waiting for the previous ones to go away... |
44 DestroyOffTheRecordProfile(profile_); | 56 DestroyProfileWhenAppropriate(profile_); |
45 } | 57 } |
46 | 58 |
47 void ProfileDestroyer::Observe(int type, | 59 void ProfileDestroyer::Observe(int type, |
48 const content::NotificationSource& source, | 60 const content::NotificationSource& source, |
49 const content::NotificationDetails& details) { | 61 const content::NotificationDetails& details) { |
50 DCHECK(type == content::NOTIFICATION_RENDERER_PROCESS_TERMINATED); | 62 DCHECK(type == content::NOTIFICATION_RENDERER_PROCESS_TERMINATED); |
51 // Delay the destruction one step further in case other observers of this | 63 // Delay the destruction one step further in case other observers of this |
52 // notification need to look at the profile attached to the host. | 64 // notification need to look at the profile attached to the host. |
53 MessageLoop::current()->PostTask( | 65 MessageLoop::current()->PostTask( |
54 FROM_HERE, base::Bind(&ProfileDestroyer::Release, this)); | 66 FROM_HERE, base::Bind(&ProfileDestroyer::Release, this)); |
55 } | 67 } |
56 | 68 |
57 // static | 69 // static |
58 bool ProfileDestroyer::GetHostsForProfile( | 70 bool ProfileDestroyer::GetHostsForProfile( |
59 Profile* const profile, std::vector<content::RenderProcessHost*>* hosts) { | 71 Profile* const profile, std::vector<content::RenderProcessHost*>* hosts) { |
60 for (content::RenderProcessHost::iterator iter( | 72 for (content::RenderProcessHost::iterator iter( |
61 content::RenderProcessHost::AllHostsIterator()); | 73 content::RenderProcessHost::AllHostsIterator()); |
62 !iter.IsAtEnd(); iter.Advance()) { | 74 !iter.IsAtEnd(); iter.Advance()) { |
63 content::RenderProcessHost* render_process_host = iter.GetCurrentValue(); | 75 content::RenderProcessHost* render_process_host = iter.GetCurrentValue(); |
64 if (render_process_host && Profile::FromBrowserContext( | 76 if (render_process_host && Profile::FromBrowserContext( |
65 render_process_host->GetBrowserContext()) == profile) { | 77 render_process_host->GetBrowserContext()) == profile) { |
66 hosts->push_back(render_process_host); | 78 hosts->push_back(render_process_host); |
67 } | 79 } |
68 } | 80 } |
69 return !hosts->empty(); | 81 return !hosts->empty(); |
70 } | 82 } |
OLD | NEW |