1 : // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
2 : // Use of this source code is governed by a BSD-style license that can be
3 : // found in the LICENSE file.
4 :
5 : #include "base/revocable_store.h"
6 :
7 : #include "base/logging.h"
8 :
9 0 : RevocableStore::Revocable::Revocable(RevocableStore* store)
10 0 : : store_reference_(store->owning_reference_) {
11 : // We AddRef() the owning reference.
12 0 : DCHECK(store_reference_->store());
13 0 : store_reference_->store()->Add(this);
14 0 : }
15 :
16 0 : RevocableStore::Revocable::~Revocable() {
17 0 : if (!revoked()) {
18 : // Notify the store of our destruction.
19 0 : --(store_reference_->store()->count_);
20 : }
21 0 : }
22 :
23 1 : RevocableStore::RevocableStore() : count_(0) {
24 : // Create a new owning reference.
25 1 : owning_reference_ = new StoreRef(this);
26 1 : }
27 :
28 0 : RevocableStore::~RevocableStore() {
29 : // Revoke all the items in the store.
30 0 : owning_reference_->set_store(NULL);
31 0 : }
32 :
33 0 : void RevocableStore::Add(Revocable* item) {
34 0 : DCHECK(!item->revoked());
35 0 : ++count_;
36 0 : }
37 :
38 0 : void RevocableStore::RevokeAll() {
39 : // We revoke all the existing items in the store and reset our count.
40 0 : owning_reference_->set_store(NULL);
41 0 : count_ = 0;
42 :
43 : // Then we create a new owning reference for new items that get added.
44 : // This Release()s the old owning reference, allowing it to be freed after
45 : // all the items that were in the store are eventually destroyed.
46 0 : owning_reference_ = new StoreRef(this);
47 0 : }
|