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" 858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)#include "base/debug/trace_event.h" 95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/memory/scoped_ptr.h" 109ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch#include "base/message_loop/message_loop.h" 115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/profiles/profile.h" 125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/public/browser/render_process_host.h" 135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace { 155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 165d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#if defined(OS_ANDROID) 175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Set the render host waiting time to 5s on Android, that's the same 185d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// as an "Application Not Responding" timeout. 195d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)const int64 kTimerDelaySeconds = 5; 205d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#else 215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)const int64 kTimerDelaySeconds = 1; 225d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#endif 235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} // namespace 255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 265d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)ProfileDestroyer::DestroyerSet* ProfileDestroyer::pending_destroyers_ = NULL; 275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// static 295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void ProfileDestroyer::DestroyProfileWhenAppropriate(Profile* const profile) { 3058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) TRACE_EVENT0("shutdown", "ProfileDestroyer::DestroyProfileWhenAppropriate"); 3158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) 325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) DCHECK(profile); 335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) profile->MaybeSendDestroyedNotification(); 345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 355d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) HostSet hosts; 365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Testing profiles can simply be deleted directly. Some tests don't setup 375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // RenderProcessHost correctly and don't necessary run on the UI thread 385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // anyway, so we can't use the AllHostIterator. 395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if (profile->AsTestingProfile() == NULL) { 405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) GetHostsForProfile(profile, &hosts); 415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if (!profile->IsOffTheRecord() && profile->HasOffTheRecordProfile()) 425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) GetHostsForProfile(profile->GetOffTheRecordProfile(), &hosts); 435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // Generally, !hosts.empty() means that there is a leak in a render process 452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // host that MUST BE FIXED!!! 462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // 472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // However, off-the-record profiles are destroyed before their 482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // RenderProcessHosts in order to erase private data quickly, and 492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // RenderProcessHostImpl::Release() avoids destroying RenderProcessHosts in 502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // --single-process mode to avoid race conditions. 512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) DCHECK(hosts.empty() || profile->IsOffTheRecord() || 52a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles) content::RenderProcessHost::run_renderer_in_process()) << \ 53a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles) "Profile still has " << hosts.size() << " hosts"; 545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Note that we still test for !profile->IsOffTheRecord here even though we 555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // DCHECK'd above because we want to protect Release builds against this even 565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // we need to identify if there are leaks when we run Debug builds. 575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if (hosts.empty() || !profile->IsOffTheRecord()) { 585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if (profile->IsOffTheRecord()) 595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) profile->GetOriginalProfile()->DestroyOffTheRecordProfile(); 605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) else 615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) delete profile; 625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } else { 635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // The instance will destroy itself once all render process hosts referring 645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // to it are properly terminated. 655d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) new ProfileDestroyer(profile, &hosts); 665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// This can be called to cancel any pending destruction and destroy the profile 705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// now, e.g., if the parent profile is being destroyed while the incognito one 715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// still pending... 725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void ProfileDestroyer::DestroyOffTheRecordProfileNow(Profile* const profile) { 735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) DCHECK(profile); 745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) DCHECK(profile->IsOffTheRecord()); 755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if (pending_destroyers_) { 765d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) for (DestroyerSet::iterator i = pending_destroyers_->begin(); 775d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) i != pending_destroyers_->end(); ++i) { 785d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) if ((*i)->profile_ == profile) { 795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // We want to signal this in debug builds so that we don't lose sight of 805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // these potential leaks, but we handle it in release so that we don't 815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // crash or corrupt profile data on disk. 825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) NOTREACHED() << "A render process host wasn't destroyed early enough."; 835d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) (*i)->profile_ = NULL; 845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) break; 855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) DCHECK(profile->GetOriginalProfile()); 895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) profile->GetOriginalProfile()->DestroyOffTheRecordProfile(); 905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 925d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)ProfileDestroyer::ProfileDestroyer(Profile* const profile, HostSet* hosts) 935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) : timer_(false, false), 945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) num_hosts_(0), 95868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) profile_(profile), 96868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) weak_ptr_factory_(this) { 975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if (pending_destroyers_ == NULL) 985d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) pending_destroyers_ = new DestroyerSet; 995d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) pending_destroyers_->insert(this); 1005d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) for (HostSet::iterator i = hosts->begin(); i != hosts->end(); ++i) { 1015d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) (*i)->AddObserver(this); 1025d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // For each of the observations, we bump up our reference count. 1035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // It will go back to 0 and free us when all hosts are terminated. 1045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) ++num_hosts_; 1055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 1065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // If we are going to wait for render process hosts, we don't want to do it 1075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // for longer than kTimerDelaySeconds. 1085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if (num_hosts_) { 1095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) timer_.Start(FROM_HERE, 1105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) base::TimeDelta::FromSeconds(kTimerDelaySeconds), 111868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) base::Bind(&ProfileDestroyer::DestroyProfile, 112868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) weak_ptr_factory_.GetWeakPtr())); 1135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 1145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 1155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)ProfileDestroyer::~ProfileDestroyer() { 1175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Check again, in case other render hosts were added while we were 1185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // waiting for the previous ones to go away... 1195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if (profile_) 1205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) DestroyProfileWhenAppropriate(profile_); 1215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1225d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // Don't wait for pending registrations, if any, these hosts are buggy. 1235d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // Note: this can happen, but if so, it's better to crash here than wait 1245d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // for the host to dereference a deleted Profile. http://crbug.com/248625 1255d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) CHECK_EQ(0U, num_hosts_) << "Some render process hosts were not " 1265d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) << "destroyed early enough!"; 1275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) DCHECK(pending_destroyers_ != NULL); 1295d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) DestroyerSet::iterator iter = pending_destroyers_->find(this); 1305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) DCHECK(iter != pending_destroyers_->end()); 1315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) pending_destroyers_->erase(iter); 1325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if (pending_destroyers_->empty()) { 1335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) delete pending_destroyers_; 1345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) pending_destroyers_ = NULL; 1355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 1365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 1375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1385d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)void ProfileDestroyer::RenderProcessHostDestroyed( 1395d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) content::RenderProcessHost* host) { 1405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) DCHECK(num_hosts_ > 0); 1415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) --num_hosts_; 1425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if (num_hosts_ == 0) { 1435d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // Delay the destruction one step further in case other observers need to 1445d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // 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. 1535d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) if (!profile_) { 1545d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) delete this; 1555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return; 1565d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) } 1575d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 1585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) DCHECK(profile_->IsOffTheRecord()); 1595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) DCHECK(profile_->GetOriginalProfile()); 1605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) profile_->GetOriginalProfile()->DestroyOffTheRecordProfile(); 1615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) profile_ = NULL; 1625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // And stop the timer so we can be released early too. 1645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) timer_.Stop(); 165868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 166868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) delete this; 1675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 1685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// static 1705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool ProfileDestroyer::GetHostsForProfile( 1715d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) Profile* const profile, HostSet* hosts) { 1725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) for (content::RenderProcessHost::iterator iter( 1735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) content::RenderProcessHost::AllHostsIterator()); 1745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) !iter.IsAtEnd(); iter.Advance()) { 1755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) content::RenderProcessHost* render_process_host = iter.GetCurrentValue(); 1765d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) if (render_process_host && 1775d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) render_process_host->GetBrowserContext() == profile) { 1785d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) hosts->insert(render_process_host); 1795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 1805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 1815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return !hosts->empty(); 1825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 183