michael@0: // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. michael@0: // Use of this source code is governed by a BSD-style license that can be michael@0: // found in the LICENSE file. michael@0: michael@0: #include "base/revocable_store.h" michael@0: michael@0: #include "base/logging.h" michael@0: michael@0: RevocableStore::Revocable::Revocable(RevocableStore* store) michael@0: : store_reference_(store->owning_reference_) { michael@0: // We AddRef() the owning reference. michael@0: DCHECK(store_reference_->store()); michael@0: store_reference_->store()->Add(this); michael@0: } michael@0: michael@0: RevocableStore::Revocable::~Revocable() { michael@0: if (!revoked()) { michael@0: // Notify the store of our destruction. michael@0: --(store_reference_->store()->count_); michael@0: } michael@0: } michael@0: michael@0: RevocableStore::RevocableStore() : count_(0) { michael@0: // Create a new owning reference. michael@0: owning_reference_ = new StoreRef(this); michael@0: } michael@0: michael@0: RevocableStore::~RevocableStore() { michael@0: // Revoke all the items in the store. michael@0: owning_reference_->set_store(NULL); michael@0: } michael@0: michael@0: void RevocableStore::Add(Revocable* item) { michael@0: DCHECK(!item->revoked()); michael@0: ++count_; michael@0: } michael@0: michael@0: void RevocableStore::RevokeAll() { michael@0: // We revoke all the existing items in the store and reset our count. michael@0: owning_reference_->set_store(NULL); michael@0: count_ = 0; michael@0: michael@0: // Then we create a new owning reference for new items that get added. michael@0: // This Release()s the old owning reference, allowing it to be freed after michael@0: // all the items that were in the store are eventually destroyed. michael@0: owning_reference_ = new StoreRef(this); michael@0: }