Chromium Code Reviews| 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 | |
| 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 if (hosts.empty() || !profile->IsOffTheRecord()) { | |
|
rpetterson
2012/06/26 18:35:34
I don't see the need for this added "!profile->IsO
MAD
2012/06/26 18:53:20
Safegard for release builds... For those cases whe
| |
| 27 if (profile->IsOffTheRecord()) | 40 if (profile->IsOffTheRecord()) |
| 28 profile->GetOriginalProfile()->DestroyOffTheRecordProfile(); | 41 profile->GetOriginalProfile()->DestroyOffTheRecordProfile(); |
| 29 else | 42 else |
| 30 delete profile; | 43 delete profile; |
|
rpetterson
2012/06/26 18:35:34
Is there any chance we could be deleting a profile
MAD
2012/06/26 18:53:20
Yes, but then the profile_impl destructor will cal
| |
| 31 } else { | 44 } else { |
| 32 // The instance will destroy itself once all render process hosts referring | 45 // 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. | 46 // to it are properly terminated. |
| 34 scoped_refptr<ProfileDestroyer> profile_destroyer( | 47 scoped_refptr<ProfileDestroyer> profile_destroyer( |
| 35 new ProfileDestroyer(profile, hosts)); | 48 new ProfileDestroyer(profile, hosts)); |
| 36 } | 49 } |
| 37 } | 50 } |
| 38 | 51 |
| 52 // This can be called to cancel any pending destruction and destroy the profile | |
| 53 // now, e.g., if the parent profile is being destroyed while the incognito one | |
| 54 // still pending... | |
| 55 void ProfileDestroyer::DestroyOffTheRecordProfileNow(Profile* const profile) { | |
| 56 DCHECK(profile); | |
| 57 DCHECK(profile->IsOffTheRecord()); | |
| 58 if (pending_destroyers_) { | |
| 59 for (size_t i = 0; i < pending_destroyers_->size(); ++i) { | |
| 60 if ((*pending_destroyers_)[i]->profile_ == profile) { | |
| 61 // We want to signal this in debug builds so that we don't lose sight of | |
| 62 // these potential leaks, but we handle it in release so that we don't | |
| 63 // crash or corrupt profile data on disk. | |
| 64 NOTREACHED() << "A render process host wasn't destroyed early enough."; | |
| 65 (*pending_destroyers_)[i]->profile_ = NULL; | |
| 66 break; | |
| 67 } | |
| 68 } | |
| 69 } | |
| 70 DCHECK(profile->GetOriginalProfile()); | |
| 71 profile->GetOriginalProfile()->DestroyOffTheRecordProfile(); | |
| 72 } | |
| 73 | |
| 39 ProfileDestroyer::ProfileDestroyer( | 74 ProfileDestroyer::ProfileDestroyer( |
| 40 Profile* const profile, | 75 Profile* const profile, |
| 41 const std::vector<content::RenderProcessHost*>& hosts) : profile_(profile) { | 76 const std::vector<content::RenderProcessHost*>& hosts) |
| 77 : timer_(false, false), | |
| 78 num_hosts_(0), | |
| 79 profile_(profile) { | |
| 80 if (pending_destroyers_ == NULL) | |
| 81 pending_destroyers_ = new std::vector<ProfileDestroyer*>; | |
| 82 pending_destroyers_->push_back(this); | |
| 42 for (size_t i = 0; i < hosts.size(); ++i) { | 83 for (size_t i = 0; i < hosts.size(); ++i) { |
| 43 registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_TERMINATED, | 84 registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_TERMINATED, |
| 44 content::Source<content::RenderProcessHost>(hosts[i])); | 85 content::Source<content::RenderProcessHost>(hosts[i])); |
| 45 // For each of the notifications, we bump up our reference count. | 86 // 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. | 87 // It will go back to 0 and free us when all hosts are terminated. |
| 47 AddRef(); | 88 ++num_hosts_; |
| 89 } | |
| 90 // If we are going to wait for render process hosts, we don't want to do it | |
| 91 // for longer than kTimerDelaySeconds. | |
| 92 if (num_hosts_) { | |
| 93 timer_.Start(FROM_HERE, | |
| 94 base::TimeDelta::FromSeconds(kTimerDelaySeconds), | |
| 95 base::Bind(&ProfileDestroyer::DestroyProfile, this)); | |
| 48 } | 96 } |
| 49 } | 97 } |
| 50 | 98 |
| 51 ProfileDestroyer::~ProfileDestroyer() { | 99 ProfileDestroyer::~ProfileDestroyer() { |
| 52 // Check again, in case other render hosts were added while we were | 100 // Check again, in case other render hosts were added while we were |
| 53 // waiting for the previous ones to go away... | 101 // waiting for the previous ones to go away... |
| 54 DestroyProfileWhenAppropriate(profile_); | 102 if (profile_) |
| 103 DestroyProfileWhenAppropriate(profile_); | |
| 104 | |
| 105 // We shouldn't be deleted with pending notifications. | |
| 106 DCHECK(registrar_.IsEmpty()); | |
| 107 | |
| 108 DCHECK(pending_destroyers_ != NULL); | |
| 109 std::vector<ProfileDestroyer*>::iterator iter = std::find( | |
| 110 pending_destroyers_->begin(), pending_destroyers_->end(), this); | |
| 111 DCHECK(iter != pending_destroyers_->end()); | |
| 112 pending_destroyers_->erase(iter); | |
| 113 DCHECK(pending_destroyers_->end() == std::find(pending_destroyers_->begin(), | |
| 114 pending_destroyers_->end(), | |
| 115 this)); | |
| 116 if (pending_destroyers_->empty()) { | |
| 117 delete pending_destroyers_; | |
| 118 pending_destroyers_ = NULL; | |
| 119 } | |
| 55 } | 120 } |
| 56 | 121 |
| 57 void ProfileDestroyer::Observe(int type, | 122 void ProfileDestroyer::Observe(int type, |
| 58 const content::NotificationSource& source, | 123 const content::NotificationSource& source, |
| 59 const content::NotificationDetails& details) { | 124 const content::NotificationDetails& details) { |
| 60 DCHECK(type == content::NOTIFICATION_RENDERER_PROCESS_TERMINATED); | 125 DCHECK(type == content::NOTIFICATION_RENDERER_PROCESS_TERMINATED); |
| 61 // Delay the destruction one step further in case other observers of this | 126 DCHECK(num_hosts_ > 0); |
| 62 // notification need to look at the profile attached to the host. | 127 --num_hosts_; |
| 63 MessageLoop::current()->PostTask( | 128 if (num_hosts_ == 0) { |
| 64 FROM_HERE, base::Bind(&ProfileDestroyer::Release, this)); | 129 // Delay the destruction one step further in case other observers of this |
| 130 // notification need to look at the profile attached to the host. | |
| 131 MessageLoop::current()->PostTask( | |
| 132 FROM_HERE, base::Bind(&ProfileDestroyer::DestroyProfile, this)); | |
| 133 } | |
| 134 } | |
| 135 | |
| 136 void ProfileDestroyer::DestroyProfile() { | |
| 137 // We might have been cancelled externally before the timer expired. | |
| 138 if (profile_ == NULL) | |
| 139 return; | |
| 140 DCHECK(profile_->IsOffTheRecord()); | |
| 141 DCHECK(profile_->GetOriginalProfile()); | |
| 142 profile_->GetOriginalProfile()->DestroyOffTheRecordProfile(); | |
| 143 profile_ = NULL; | |
| 144 | |
| 145 // Don't wait for pending registrations, if any, these hosts are buggy. | |
| 146 DCHECK(registrar_.IsEmpty()) << "Some render process hosts where not " | |
| 147 << "destroyed early enough!"; | |
| 148 registrar_.RemoveAll(); | |
| 149 | |
| 150 // And stop the timer so we can be released early too. | |
| 151 timer_.Stop(); | |
| 65 } | 152 } |
| 66 | 153 |
| 67 // static | 154 // static |
| 68 bool ProfileDestroyer::GetHostsForProfile( | 155 bool ProfileDestroyer::GetHostsForProfile( |
| 69 Profile* const profile, std::vector<content::RenderProcessHost*>* hosts) { | 156 Profile* const profile, std::vector<content::RenderProcessHost*>* hosts) { |
| 70 for (content::RenderProcessHost::iterator iter( | 157 for (content::RenderProcessHost::iterator iter( |
| 71 content::RenderProcessHost::AllHostsIterator()); | 158 content::RenderProcessHost::AllHostsIterator()); |
| 72 !iter.IsAtEnd(); iter.Advance()) { | 159 !iter.IsAtEnd(); iter.Advance()) { |
| 73 content::RenderProcessHost* render_process_host = iter.GetCurrentValue(); | 160 content::RenderProcessHost* render_process_host = iter.GetCurrentValue(); |
| 74 if (render_process_host && Profile::FromBrowserContext( | 161 if (render_process_host && Profile::FromBrowserContext( |
| 75 render_process_host->GetBrowserContext()) == profile) { | 162 render_process_host->GetBrowserContext()) == profile) { |
| 76 hosts->push_back(render_process_host); | 163 hosts->push_back(render_process_host); |
| 77 } | 164 } |
| 78 } | 165 } |
| 79 return !hosts->empty(); | 166 return !hosts->empty(); |
| 80 } | 167 } |
| OLD | NEW |