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: #ifndef BASE_REVOCABLE_STORE_H_ michael@0: #define BASE_REVOCABLE_STORE_H_ michael@0: michael@0: #include "base/ref_counted.h" michael@0: michael@0: // |RevocableStore| is a container of items that can be removed from the store. michael@0: class RevocableStore { michael@0: public: michael@0: // A |StoreRef| is used to link the |RevocableStore| to its items. There is michael@0: // one StoreRef per store, and each item holds a reference to it. If the michael@0: // store wishes to revoke its items, it sets |store_| to null. Items are michael@0: // permitted to release their reference to the |StoreRef| when they no longer michael@0: // require the store. michael@0: class StoreRef : public base::RefCounted { michael@0: public: michael@0: StoreRef(RevocableStore* store) : store_(store) { } michael@0: michael@0: void set_store(RevocableStore* store) { store_ = store; } michael@0: RevocableStore* store() const { return store_; } michael@0: michael@0: private: michael@0: RevocableStore* store_; michael@0: michael@0: DISALLOW_EVIL_CONSTRUCTORS(StoreRef); michael@0: }; michael@0: michael@0: // An item in the store. On construction, the object adds itself to the michael@0: // store. michael@0: class Revocable { michael@0: public: michael@0: Revocable(RevocableStore* store); michael@0: ~Revocable(); michael@0: michael@0: // This item has been revoked if it no longer has a pointer to the store. michael@0: bool revoked() const { return !store_reference_->store(); } michael@0: michael@0: private: michael@0: // We hold a reference to the store through this ref pointer. We release michael@0: // this reference on destruction. michael@0: scoped_refptr store_reference_; michael@0: michael@0: DISALLOW_EVIL_CONSTRUCTORS(Revocable); michael@0: }; michael@0: michael@0: RevocableStore(); michael@0: ~RevocableStore(); michael@0: michael@0: // Revokes all the items in the store. michael@0: void RevokeAll(); michael@0: michael@0: // Returns true if there are no items in the store. michael@0: bool empty() const { return count_ == 0; } michael@0: michael@0: private: michael@0: friend class Revocable; michael@0: michael@0: // Adds an item to the store. To add an item to the store, construct it michael@0: // with a pointer to the store. michael@0: void Add(Revocable* item); michael@0: michael@0: // This is the reference the unrevoked items in the store hold. michael@0: scoped_refptr owning_reference_; michael@0: michael@0: // The number of unrevoked items in the store. michael@0: int count_; michael@0: michael@0: DISALLOW_EVIL_CONSTRUCTORS(RevocableStore); michael@0: }; michael@0: michael@0: #endif // BASE_REVOCABLE_STORE_H_