profile_destroyer.cc revision 7d4cd473f85ac64c3747c96c277f9e506a0d2246
15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved. 25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be 35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file. 45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/profiles/profile_destroyer.h" 65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/bind.h" 85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/memory/scoped_ptr.h" 95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/message_loop.h" 105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/profiles/profile.h" 115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/public/browser/notification_source.h" 125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/public/browser/notification_types.h" 135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/public/browser/render_process_host.h" 145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace { 175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)const int64 kTimerDelaySeconds = 1; 195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} // namespace 215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)std::vector<ProfileDestroyer*>* ProfileDestroyer::pending_destroyers_ = NULL; 235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// static 255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void ProfileDestroyer::DestroyProfileWhenAppropriate(Profile* const profile) { 265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) DCHECK(profile); 275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) profile->MaybeSendDestroyedNotification(); 285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) std::vector<content::RenderProcessHost*> hosts; 305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Testing profiles can simply be deleted directly. Some tests don't setup 315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // RenderProcessHost correctly and don't necessary run on the UI thread 325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // anyway, so we can't use the AllHostIterator. 335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if (profile->AsTestingProfile() == NULL) { 345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) GetHostsForProfile(profile, &hosts); 355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if (!profile->IsOffTheRecord() && profile->HasOffTheRecordProfile()) 365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) GetHostsForProfile(profile->GetOffTheRecordProfile(), &hosts); 375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // Generally, !hosts.empty() means that there is a leak in a render process 392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // host that MUST BE FIXED!!! 402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // 412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // However, off-the-record profiles are destroyed before their 422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // RenderProcessHosts in order to erase private data quickly, and 432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // RenderProcessHostImpl::Release() avoids destroying RenderProcessHosts in 442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // --single-process mode to avoid race conditions. 452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) DCHECK(hosts.empty() || profile->IsOffTheRecord() || 462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) content::RenderProcessHost::run_renderer_in_process()); 475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Note that we still test for !profile->IsOffTheRecord here even though we 485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // DCHECK'd above because we want to protect Release builds against this even 495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // we need to identify if there are leaks when we run Debug builds. 505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if (hosts.empty() || !profile->IsOffTheRecord()) { 515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if (profile->IsOffTheRecord()) 525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) profile->GetOriginalProfile()->DestroyOffTheRecordProfile(); 535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) else 545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) delete profile; 555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } else { 565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // The instance will destroy itself once all render process hosts referring 575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // to it are properly terminated. 58868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) new ProfileDestroyer(profile, hosts); 595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// This can be called to cancel any pending destruction and destroy the profile 635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// now, e.g., if the parent profile is being destroyed while the incognito one 645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// still pending... 655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void ProfileDestroyer::DestroyOffTheRecordProfileNow(Profile* const profile) { 665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) DCHECK(profile); 675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) DCHECK(profile->IsOffTheRecord()); 685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if (pending_destroyers_) { 695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) for (size_t i = 0; i < pending_destroyers_->size(); ++i) { 705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if ((*pending_destroyers_)[i]->profile_ == profile) { 715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // We want to signal this in debug builds so that we don't lose sight of 725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // these potential leaks, but we handle it in release so that we don't 735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // crash or corrupt profile data on disk. 745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) NOTREACHED() << "A render process host wasn't destroyed early enough."; 755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) (*pending_destroyers_)[i]->profile_ = NULL; 765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) break; 775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) DCHECK(profile->GetOriginalProfile()); 815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) profile->GetOriginalProfile()->DestroyOffTheRecordProfile(); 825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)ProfileDestroyer::ProfileDestroyer( 855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) Profile* const profile, 865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) const std::vector<content::RenderProcessHost*>& hosts) 875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) : timer_(false, false), 885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) num_hosts_(0), 89868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) profile_(profile), 90868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) weak_ptr_factory_(this) { 915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if (pending_destroyers_ == NULL) 925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) pending_destroyers_ = new std::vector<ProfileDestroyer*>; 935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) pending_destroyers_->push_back(this); 945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) for (size_t i = 0; i < hosts.size(); ++i) { 955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_TERMINATED, 965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) content::Source<content::RenderProcessHost>(hosts[i])); 975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // For each of the notifications, we bump up our reference count. 985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // It will go back to 0 and free us when all hosts are terminated. 995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) ++num_hosts_; 1005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 1015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // If we are going to wait for render process hosts, we don't want to do it 1025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // for longer than kTimerDelaySeconds. 1035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if (num_hosts_) { 1045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) timer_.Start(FROM_HERE, 1055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) base::TimeDelta::FromSeconds(kTimerDelaySeconds), 106868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) base::Bind(&ProfileDestroyer::DestroyProfile, 107868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) weak_ptr_factory_.GetWeakPtr())); 1085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 1095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 1105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)ProfileDestroyer::~ProfileDestroyer() { 1125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Check again, in case other render hosts were added while we were 1135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // waiting for the previous ones to go away... 1145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if (profile_) 1155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) DestroyProfileWhenAppropriate(profile_); 1165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // We shouldn't be deleted with pending notifications. 1185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) DCHECK(registrar_.IsEmpty()); 1195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) DCHECK(pending_destroyers_ != NULL); 1215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) std::vector<ProfileDestroyer*>::iterator iter = std::find( 1225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) pending_destroyers_->begin(), pending_destroyers_->end(), this); 1235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) DCHECK(iter != pending_destroyers_->end()); 1245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) pending_destroyers_->erase(iter); 1255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) DCHECK(pending_destroyers_->end() == std::find(pending_destroyers_->begin(), 1265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) pending_destroyers_->end(), 1275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) this)); 1285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if (pending_destroyers_->empty()) { 1295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) delete pending_destroyers_; 1305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) pending_destroyers_ = NULL; 1315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 1325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 1335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void ProfileDestroyer::Observe(int type, 1355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) const content::NotificationSource& source, 1365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) const content::NotificationDetails& details) { 1375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) DCHECK(type == content::NOTIFICATION_RENDERER_PROCESS_TERMINATED); 1385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) registrar_.Remove(this, content::NOTIFICATION_RENDERER_PROCESS_TERMINATED, 1395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) source); 1405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) DCHECK(num_hosts_ > 0); 1415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) --num_hosts_; 1425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if (num_hosts_ == 0) { 1435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Delay the destruction one step further in case other observers of this 1445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // notification need to look at the profile attached to the host. 14590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) base::MessageLoop::current()->PostTask( 146868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) FROM_HERE, base::Bind( 147868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) &ProfileDestroyer::DestroyProfile, weak_ptr_factory_.GetWeakPtr())); 1485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 1495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 1505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void ProfileDestroyer::DestroyProfile() { 1525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // We might have been cancelled externally before the timer expired. 1535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if (profile_ == NULL) 1545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return; 1555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) DCHECK(profile_->IsOffTheRecord()); 1565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) DCHECK(profile_->GetOriginalProfile()); 1575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) profile_->GetOriginalProfile()->DestroyOffTheRecordProfile(); 1585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) profile_ = NULL; 1595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Don't wait for pending registrations, if any, these hosts are buggy. 1617d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) // Note: this can happen, but if so, it's better to crash here than wait 1627d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) // for the host to dereference a deleted Profile. http://crbug.com/248625 1637d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) CHECK(registrar_.IsEmpty()) << "Some render process hosts were not " 1647d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) << "destroyed early enough!"; 1655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // And stop the timer so we can be released early too. 1675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) timer_.Stop(); 168868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 169868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) delete this; 1705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 1715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// static 1735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool ProfileDestroyer::GetHostsForProfile( 1745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) Profile* const profile, std::vector<content::RenderProcessHost*>* hosts) { 1755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) for (content::RenderProcessHost::iterator iter( 1765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) content::RenderProcessHost::AllHostsIterator()); 1775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) !iter.IsAtEnd(); iter.Advance()) { 1785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) content::RenderProcessHost* render_process_host = iter.GetCurrentValue(); 1795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if (render_process_host && Profile::FromBrowserContext( 1805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) render_process_host->GetBrowserContext()) == profile) { 1815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) hosts->push_back(render_process_host); 1825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 1835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 1845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return !hosts->empty(); 1855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 186