profile_destroyer.cc revision 5821806d5e7f356e8fa4b058a389a808ea183019
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)  }
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // This should never happen for non Off the record profile, this means that
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // there is a leak in a render process host that MUST BE FIXED!!!
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(hosts.empty() || profile->IsOffTheRecord());
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Note that we still test for !profile->IsOffTheRecord here even though we
425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // DCHECK'd above because we want to protect Release builds against this even
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // we need to identify if there are leaks when we run Debug builds.
445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (hosts.empty() || !profile->IsOffTheRecord()) {
455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (profile->IsOffTheRecord())
465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      profile->GetOriginalProfile()->DestroyOffTheRecordProfile();
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    else
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      delete profile;
495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  } else {
505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // The instance will destroy itself once all render process hosts referring
515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // to it are properly terminated.
525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    scoped_refptr<ProfileDestroyer> profile_destroyer(
535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        new ProfileDestroyer(profile, hosts));
545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// This can be called to cancel any pending destruction and destroy the profile
585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// now, e.g., if the parent profile is being destroyed while the incognito one
595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// still pending...
605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void ProfileDestroyer::DestroyOffTheRecordProfileNow(Profile* const profile) {
615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(profile);
625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(profile->IsOffTheRecord());
635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (pending_destroyers_) {
645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    for (size_t i = 0; i < pending_destroyers_->size(); ++i) {
655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      if ((*pending_destroyers_)[i]->profile_ == profile) {
665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        // We want to signal this in debug builds so that we don't lose sight of
675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        // these potential leaks, but we handle it in release so that we don't
685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        // crash or corrupt profile data on disk.
695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        NOTREACHED() << "A render process host wasn't destroyed early enough.";
705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        (*pending_destroyers_)[i]->profile_ = NULL;
715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        break;
725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      }
735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(profile->GetOriginalProfile());
765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  profile->GetOriginalProfile()->DestroyOffTheRecordProfile();
775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)ProfileDestroyer::ProfileDestroyer(
805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    Profile* const profile,
815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const std::vector<content::RenderProcessHost*>& hosts)
825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    : timer_(false, false),
835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      num_hosts_(0),
845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      profile_(profile) {
855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (pending_destroyers_ == NULL)
865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    pending_destroyers_ = new std::vector<ProfileDestroyer*>;
875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  pending_destroyers_->push_back(this);
885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  for (size_t i = 0; i < hosts.size(); ++i) {
895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_TERMINATED,
905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                   content::Source<content::RenderProcessHost>(hosts[i]));
915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // For each of the notifications, we bump up our reference count.
925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // It will go back to 0 and free us when all hosts are terminated.
935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    ++num_hosts_;
945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // If we are going to wait for render process hosts, we don't want to do it
965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // for longer than kTimerDelaySeconds.
975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (num_hosts_) {
985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    timer_.Start(FROM_HERE,
995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                 base::TimeDelta::FromSeconds(kTimerDelaySeconds),
1005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                 base::Bind(&ProfileDestroyer::DestroyProfile, this));
1015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
1025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)ProfileDestroyer::~ProfileDestroyer() {
1055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Check again, in case other render hosts were added while we were
1065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // waiting for the previous ones to go away...
1075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (profile_)
1085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    DestroyProfileWhenAppropriate(profile_);
1095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // We shouldn't be deleted with pending notifications.
1115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(registrar_.IsEmpty());
1125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(pending_destroyers_ != NULL);
1145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::vector<ProfileDestroyer*>::iterator iter = std::find(
1155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      pending_destroyers_->begin(), pending_destroyers_->end(), this);
1165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(iter != pending_destroyers_->end());
1175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  pending_destroyers_->erase(iter);
1185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(pending_destroyers_->end() == std::find(pending_destroyers_->begin(),
1195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                                 pending_destroyers_->end(),
1205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                                 this));
1215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (pending_destroyers_->empty()) {
1225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    delete pending_destroyers_;
1235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    pending_destroyers_ = NULL;
1245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
1255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void ProfileDestroyer::Observe(int type,
1285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                               const content::NotificationSource& source,
1295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                               const content::NotificationDetails& details) {
1305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(type == content::NOTIFICATION_RENDERER_PROCESS_TERMINATED);
1315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  registrar_.Remove(this, content::NOTIFICATION_RENDERER_PROCESS_TERMINATED,
1325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                    source);
1335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(num_hosts_ > 0);
1345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  --num_hosts_;
1355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (num_hosts_ == 0) {
1365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // Delay the destruction one step further in case other observers of this
1375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // notification need to look at the profile attached to the host.
1385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    MessageLoop::current()->PostTask(
1395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        FROM_HERE, base::Bind(&ProfileDestroyer::DestroyProfile, this));
1405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
1415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void ProfileDestroyer::DestroyProfile() {
1445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // We might have been cancelled externally before the timer expired.
1455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (profile_ == NULL)
1465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return;
1475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(profile_->IsOffTheRecord());
1485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(profile_->GetOriginalProfile());
1495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  profile_->GetOriginalProfile()->DestroyOffTheRecordProfile();
1505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  profile_ = NULL;
1515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Don't wait for pending registrations, if any, these hosts are buggy.
1535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(registrar_.IsEmpty()) << "Some render process hosts where not "
1545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                               << "destroyed early enough!";
1555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  registrar_.RemoveAll();
1565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // And stop the timer so we can be released early too.
1585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  timer_.Stop();
1595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// static
1625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool ProfileDestroyer::GetHostsForProfile(
1635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    Profile* const profile, std::vector<content::RenderProcessHost*>* hosts) {
1645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  for (content::RenderProcessHost::iterator iter(
1655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        content::RenderProcessHost::AllHostsIterator());
1665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      !iter.IsAtEnd(); iter.Advance()) {
1675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    content::RenderProcessHost* render_process_host = iter.GetCurrentValue();
1685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (render_process_host && Profile::FromBrowserContext(
1695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          render_process_host->GetBrowserContext()) == profile) {
1705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      hosts->push_back(render_process_host);
1715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
1725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
1735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return !hosts->empty();
1745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
175