profile_destroyer.cc revision a36e5920737c6adbddd3e43b760e5de8431db6e0
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"
99ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch#include "base/message_loop/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() ||
46a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)      content::RenderProcessHost::run_renderer_in_process()) << \
47a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)      "Profile still has " << hosts.size() << " hosts";
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Note that we still test for !profile->IsOffTheRecord here even though we
495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // DCHECK'd above because we want to protect Release builds against this even
505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // we need to identify if there are leaks when we run Debug builds.
515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (hosts.empty() || !profile->IsOffTheRecord()) {
525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (profile->IsOffTheRecord())
535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      profile->GetOriginalProfile()->DestroyOffTheRecordProfile();
545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    else
555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      delete profile;
565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  } else {
575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // The instance will destroy itself once all render process hosts referring
585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // to it are properly terminated.
59868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    new ProfileDestroyer(profile, hosts);
605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// This can be called to cancel any pending destruction and destroy the profile
645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// now, e.g., if the parent profile is being destroyed while the incognito one
655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// still pending...
665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void ProfileDestroyer::DestroyOffTheRecordProfileNow(Profile* const profile) {
675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(profile);
685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(profile->IsOffTheRecord());
695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (pending_destroyers_) {
705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    for (size_t i = 0; i < pending_destroyers_->size(); ++i) {
715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      if ((*pending_destroyers_)[i]->profile_ == profile) {
725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        // We want to signal this in debug builds so that we don't lose sight of
735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        // these potential leaks, but we handle it in release so that we don't
745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        // crash or corrupt profile data on disk.
755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        NOTREACHED() << "A render process host wasn't destroyed early enough.";
765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        (*pending_destroyers_)[i]->profile_ = NULL;
775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        break;
785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      }
795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(profile->GetOriginalProfile());
825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  profile->GetOriginalProfile()->DestroyOffTheRecordProfile();
835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)ProfileDestroyer::ProfileDestroyer(
865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    Profile* const profile,
875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const std::vector<content::RenderProcessHost*>& hosts)
885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    : timer_(false, false),
895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      num_hosts_(0),
90868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      profile_(profile),
91868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      weak_ptr_factory_(this) {
925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (pending_destroyers_ == NULL)
935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    pending_destroyers_ = new std::vector<ProfileDestroyer*>;
945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  pending_destroyers_->push_back(this);
955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  for (size_t i = 0; i < hosts.size(); ++i) {
965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_TERMINATED,
975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                   content::Source<content::RenderProcessHost>(hosts[i]));
985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // For each of the notifications, we bump up our reference count.
995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // It will go back to 0 and free us when all hosts are terminated.
1005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    ++num_hosts_;
1015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
1025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // If we are going to wait for render process hosts, we don't want to do it
1035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // for longer than kTimerDelaySeconds.
1045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (num_hosts_) {
1055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    timer_.Start(FROM_HERE,
1065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                 base::TimeDelta::FromSeconds(kTimerDelaySeconds),
107868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                 base::Bind(&ProfileDestroyer::DestroyProfile,
108868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                            weak_ptr_factory_.GetWeakPtr()));
1095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
1105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)ProfileDestroyer::~ProfileDestroyer() {
1135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Check again, in case other render hosts were added while we were
1145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // waiting for the previous ones to go away...
1155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (profile_)
1165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    DestroyProfileWhenAppropriate(profile_);
1175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // We shouldn't be deleted with pending notifications.
1195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(registrar_.IsEmpty());
1205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(pending_destroyers_ != NULL);
1225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::vector<ProfileDestroyer*>::iterator iter = std::find(
1235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      pending_destroyers_->begin(), pending_destroyers_->end(), this);
1245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(iter != pending_destroyers_->end());
1255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  pending_destroyers_->erase(iter);
1265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(pending_destroyers_->end() == std::find(pending_destroyers_->begin(),
1275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                                 pending_destroyers_->end(),
1285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                                 this));
1295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (pending_destroyers_->empty()) {
1305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    delete pending_destroyers_;
1315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    pending_destroyers_ = NULL;
1325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
1335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void ProfileDestroyer::Observe(int type,
1365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                               const content::NotificationSource& source,
1375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                               const content::NotificationDetails& details) {
1385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(type == content::NOTIFICATION_RENDERER_PROCESS_TERMINATED);
1395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  registrar_.Remove(this, content::NOTIFICATION_RENDERER_PROCESS_TERMINATED,
1405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                    source);
1415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(num_hosts_ > 0);
1425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  --num_hosts_;
1435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (num_hosts_ == 0) {
1445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // Delay the destruction one step further in case other observers of this
1455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // notification need to look at the profile attached to the host.
14690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    base::MessageLoop::current()->PostTask(
147868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        FROM_HERE, base::Bind(
148868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)            &ProfileDestroyer::DestroyProfile, weak_ptr_factory_.GetWeakPtr()));
1495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
1505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void ProfileDestroyer::DestroyProfile() {
1535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // We might have been cancelled externally before the timer expired.
1545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (profile_ == NULL)
1555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return;
1565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(profile_->IsOffTheRecord());
1575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(profile_->GetOriginalProfile());
1585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  profile_->GetOriginalProfile()->DestroyOffTheRecordProfile();
1595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  profile_ = NULL;
1605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Don't wait for pending registrations, if any, these hosts are buggy.
1627d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  // Note: this can happen, but if so, it's better to crash here than wait
1637d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  // for the host to dereference a deleted Profile. http://crbug.com/248625
1647d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  CHECK(registrar_.IsEmpty()) << "Some render process hosts were not "
1657d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)                              << "destroyed early enough!";
1665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // And stop the timer so we can be released early too.
1685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  timer_.Stop();
169868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
170868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  delete this;
1715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// static
1745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool ProfileDestroyer::GetHostsForProfile(
1755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    Profile* const profile, std::vector<content::RenderProcessHost*>* hosts) {
1765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  for (content::RenderProcessHost::iterator iter(
1775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        content::RenderProcessHost::AllHostsIterator());
1785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      !iter.IsAtEnd(); iter.Advance()) {
1795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    content::RenderProcessHost* render_process_host = iter.GetCurrentValue();
1805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (render_process_host && Profile::FromBrowserContext(
1815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          render_process_host->GetBrowserContext()) == profile) {
1825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      hosts->push_back(render_process_host);
1835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
1845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
1855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return !hosts->empty();
1865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
187