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 // static | 15 // static |
16 void ProfileDestroyer::DestroyOffTheRecordProfile(Profile* const profile) { | 16 void ProfileDestroyer::DestroyProfileWhenAppropriate(Profile* const profile) { |
17 std::vector<content::RenderProcessHost*> hosts; | 17 std::vector<content::RenderProcessHost*> hosts; |
18 if (GetHostsForProfile(profile, &hosts)) { | 18 // Testing profiles can simply be deleted directly. Some tests don't setup |
| 19 // RenderProcessHost correctly and don't necessary run on the UI thread |
| 20 // anyway, so we can't use the AllHostIterator. |
| 21 if (profile->AsTestingProfile() == NULL) { |
| 22 GetHostsForProfile(profile, &hosts); |
| 23 if (!profile->IsOffTheRecord() && profile->HasOffTheRecordProfile()) |
| 24 GetHostsForProfile(profile->GetOffTheRecordProfile(), &hosts); |
| 25 } |
| 26 if (hosts.empty()) { |
| 27 if (profile->IsOffTheRecord()) |
| 28 profile->GetOriginalProfile()->DestroyOffTheRecordProfile(); |
| 29 else |
| 30 delete profile; |
| 31 } else { |
19 // The instance will destroy itself once all render process hosts referring | 32 // The instance will destroy itself once all render process hosts referring |
20 // to it are properly terminated. | 33 // to it and/or it's off the record profile are properly terminated. |
21 scoped_refptr<ProfileDestroyer> profile_destroyer( | 34 scoped_refptr<ProfileDestroyer> profile_destroyer( |
22 new ProfileDestroyer(profile, hosts)); | 35 new ProfileDestroyer(profile, hosts)); |
23 } else { | |
24 // Safe to destroy now... We're done... | |
25 profile->GetOriginalProfile()->DestroyOffTheRecordProfile(); | |
26 } | 36 } |
27 } | 37 } |
28 | 38 |
29 ProfileDestroyer::ProfileDestroyer( | 39 ProfileDestroyer::ProfileDestroyer( |
30 Profile* const profile, | 40 Profile* const profile, |
31 const std::vector<content::RenderProcessHost*>& hosts) : profile_(profile) { | 41 const std::vector<content::RenderProcessHost*>& hosts) : profile_(profile) { |
32 for (size_t i = 0; i < hosts.size(); ++i) { | 42 for (size_t i = 0; i < hosts.size(); ++i) { |
33 registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_TERMINATED, | 43 registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_TERMINATED, |
34 content::Source<content::RenderProcessHost>(hosts[i])); | 44 content::Source<content::RenderProcessHost>(hosts[i])); |
35 // For each of the notifications, we bump up our reference count. | 45 // 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. | 46 // It will go back to 0 and free us when all hosts are terminated. |
37 AddRef(); | 47 AddRef(); |
38 } | 48 } |
39 } | 49 } |
40 | 50 |
41 ProfileDestroyer::~ProfileDestroyer() { | 51 ProfileDestroyer::~ProfileDestroyer() { |
42 // Check again, in case other render hosts were added while we were | 52 // Check again, in case other render hosts were added while we were |
43 // waiting for the previous ones to go away... | 53 // waiting for the previous ones to go away... |
44 DestroyOffTheRecordProfile(profile_); | 54 DestroyProfileWhenAppropriate(profile_); |
45 } | 55 } |
46 | 56 |
47 void ProfileDestroyer::Observe(int type, | 57 void ProfileDestroyer::Observe(int type, |
48 const content::NotificationSource& source, | 58 const content::NotificationSource& source, |
49 const content::NotificationDetails& details) { | 59 const content::NotificationDetails& details) { |
50 DCHECK(type == content::NOTIFICATION_RENDERER_PROCESS_TERMINATED); | 60 DCHECK(type == content::NOTIFICATION_RENDERER_PROCESS_TERMINATED); |
51 // Delay the destruction one step further in case other observers of this | 61 // Delay the destruction one step further in case other observers of this |
52 // notification need to look at the profile attached to the host. | 62 // notification need to look at the profile attached to the host. |
53 MessageLoop::current()->PostTask( | 63 MessageLoop::current()->PostTask( |
54 FROM_HERE, base::Bind(&ProfileDestroyer::Release, this)); | 64 FROM_HERE, base::Bind(&ProfileDestroyer::Release, this)); |
55 } | 65 } |
56 | 66 |
57 // static | 67 // static |
58 bool ProfileDestroyer::GetHostsForProfile( | 68 bool ProfileDestroyer::GetHostsForProfile( |
59 Profile* const profile, std::vector<content::RenderProcessHost*>* hosts) { | 69 Profile* const profile, std::vector<content::RenderProcessHost*>* hosts) { |
60 for (content::RenderProcessHost::iterator iter( | 70 for (content::RenderProcessHost::iterator iter( |
61 content::RenderProcessHost::AllHostsIterator()); | 71 content::RenderProcessHost::AllHostsIterator()); |
62 !iter.IsAtEnd(); iter.Advance()) { | 72 !iter.IsAtEnd(); iter.Advance()) { |
63 content::RenderProcessHost* render_process_host = iter.GetCurrentValue(); | 73 content::RenderProcessHost* render_process_host = iter.GetCurrentValue(); |
64 if (render_process_host && Profile::FromBrowserContext( | 74 if (render_process_host && Profile::FromBrowserContext( |
65 render_process_host->GetBrowserContext()) == profile) { | 75 render_process_host->GetBrowserContext()) == profile) { |
66 hosts->push_back(render_process_host); | 76 hosts->push_back(render_process_host); |
67 } | 77 } |
68 } | 78 } |
69 return !hosts->empty(); | 79 return !hosts->empty(); |
70 } | 80 } |
OLD | NEW |