Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. |
michael@0 | 2 | // Use of this source code is governed by a BSD-style license that can be |
michael@0 | 3 | // found in the LICENSE file. |
michael@0 | 4 | |
michael@0 | 5 | #ifndef BASE_REVOCABLE_STORE_H_ |
michael@0 | 6 | #define BASE_REVOCABLE_STORE_H_ |
michael@0 | 7 | |
michael@0 | 8 | #include "base/ref_counted.h" |
michael@0 | 9 | |
michael@0 | 10 | // |RevocableStore| is a container of items that can be removed from the store. |
michael@0 | 11 | class RevocableStore { |
michael@0 | 12 | public: |
michael@0 | 13 | // A |StoreRef| is used to link the |RevocableStore| to its items. There is |
michael@0 | 14 | // one StoreRef per store, and each item holds a reference to it. If the |
michael@0 | 15 | // store wishes to revoke its items, it sets |store_| to null. Items are |
michael@0 | 16 | // permitted to release their reference to the |StoreRef| when they no longer |
michael@0 | 17 | // require the store. |
michael@0 | 18 | class StoreRef : public base::RefCounted<StoreRef> { |
michael@0 | 19 | public: |
michael@0 | 20 | StoreRef(RevocableStore* store) : store_(store) { } |
michael@0 | 21 | |
michael@0 | 22 | void set_store(RevocableStore* store) { store_ = store; } |
michael@0 | 23 | RevocableStore* store() const { return store_; } |
michael@0 | 24 | |
michael@0 | 25 | private: |
michael@0 | 26 | RevocableStore* store_; |
michael@0 | 27 | |
michael@0 | 28 | DISALLOW_EVIL_CONSTRUCTORS(StoreRef); |
michael@0 | 29 | }; |
michael@0 | 30 | |
michael@0 | 31 | // An item in the store. On construction, the object adds itself to the |
michael@0 | 32 | // store. |
michael@0 | 33 | class Revocable { |
michael@0 | 34 | public: |
michael@0 | 35 | Revocable(RevocableStore* store); |
michael@0 | 36 | ~Revocable(); |
michael@0 | 37 | |
michael@0 | 38 | // This item has been revoked if it no longer has a pointer to the store. |
michael@0 | 39 | bool revoked() const { return !store_reference_->store(); } |
michael@0 | 40 | |
michael@0 | 41 | private: |
michael@0 | 42 | // We hold a reference to the store through this ref pointer. We release |
michael@0 | 43 | // this reference on destruction. |
michael@0 | 44 | scoped_refptr<StoreRef> store_reference_; |
michael@0 | 45 | |
michael@0 | 46 | DISALLOW_EVIL_CONSTRUCTORS(Revocable); |
michael@0 | 47 | }; |
michael@0 | 48 | |
michael@0 | 49 | RevocableStore(); |
michael@0 | 50 | ~RevocableStore(); |
michael@0 | 51 | |
michael@0 | 52 | // Revokes all the items in the store. |
michael@0 | 53 | void RevokeAll(); |
michael@0 | 54 | |
michael@0 | 55 | // Returns true if there are no items in the store. |
michael@0 | 56 | bool empty() const { return count_ == 0; } |
michael@0 | 57 | |
michael@0 | 58 | private: |
michael@0 | 59 | friend class Revocable; |
michael@0 | 60 | |
michael@0 | 61 | // Adds an item to the store. To add an item to the store, construct it |
michael@0 | 62 | // with a pointer to the store. |
michael@0 | 63 | void Add(Revocable* item); |
michael@0 | 64 | |
michael@0 | 65 | // This is the reference the unrevoked items in the store hold. |
michael@0 | 66 | scoped_refptr<StoreRef> owning_reference_; |
michael@0 | 67 | |
michael@0 | 68 | // The number of unrevoked items in the store. |
michael@0 | 69 | int count_; |
michael@0 | 70 | |
michael@0 | 71 | DISALLOW_EVIL_CONSTRUCTORS(RevocableStore); |
michael@0 | 72 | }; |
michael@0 | 73 | |
michael@0 | 74 | #endif // BASE_REVOCABLE_STORE_H_ |