1*635a8641SAndroid Build Coastguard Worker // Copyright (c) 2012 The Chromium Authors. All rights reserved. 2*635a8641SAndroid Build Coastguard Worker // Use of this source code is governed by a BSD-style license that can be 3*635a8641SAndroid Build Coastguard Worker // found in the LICENSE file. 4*635a8641SAndroid Build Coastguard Worker 5*635a8641SAndroid Build Coastguard Worker #ifndef BASE_SCOPED_OBSERVER_H_ 6*635a8641SAndroid Build Coastguard Worker #define BASE_SCOPED_OBSERVER_H_ 7*635a8641SAndroid Build Coastguard Worker 8*635a8641SAndroid Build Coastguard Worker #include <stddef.h> 9*635a8641SAndroid Build Coastguard Worker 10*635a8641SAndroid Build Coastguard Worker #include <algorithm> 11*635a8641SAndroid Build Coastguard Worker #include <vector> 12*635a8641SAndroid Build Coastguard Worker 13*635a8641SAndroid Build Coastguard Worker #include "base/logging.h" 14*635a8641SAndroid Build Coastguard Worker #include "base/macros.h" 15*635a8641SAndroid Build Coastguard Worker #include "base/stl_util.h" 16*635a8641SAndroid Build Coastguard Worker 17*635a8641SAndroid Build Coastguard Worker // ScopedObserver is used to keep track of the set of sources an object has 18*635a8641SAndroid Build Coastguard Worker // attached itself to as an observer. When ScopedObserver is destroyed it 19*635a8641SAndroid Build Coastguard Worker // removes the object as an observer from all sources it has been added to. 20*635a8641SAndroid Build Coastguard Worker template <class Source, class Observer> 21*635a8641SAndroid Build Coastguard Worker class ScopedObserver { 22*635a8641SAndroid Build Coastguard Worker public: ScopedObserver(Observer * observer)23*635a8641SAndroid Build Coastguard Worker explicit ScopedObserver(Observer* observer) : observer_(observer) {} 24*635a8641SAndroid Build Coastguard Worker ~ScopedObserver()25*635a8641SAndroid Build Coastguard Worker ~ScopedObserver() { 26*635a8641SAndroid Build Coastguard Worker RemoveAll(); 27*635a8641SAndroid Build Coastguard Worker } 28*635a8641SAndroid Build Coastguard Worker 29*635a8641SAndroid Build Coastguard Worker // Adds the object passed to the constructor as an observer on |source|. Add(Source * source)30*635a8641SAndroid Build Coastguard Worker void Add(Source* source) { 31*635a8641SAndroid Build Coastguard Worker sources_.push_back(source); 32*635a8641SAndroid Build Coastguard Worker source->AddObserver(observer_); 33*635a8641SAndroid Build Coastguard Worker } 34*635a8641SAndroid Build Coastguard Worker 35*635a8641SAndroid Build Coastguard Worker // Remove the object passed to the constructor as an observer from |source|. Remove(Source * source)36*635a8641SAndroid Build Coastguard Worker void Remove(Source* source) { 37*635a8641SAndroid Build Coastguard Worker auto it = std::find(sources_.begin(), sources_.end(), source); 38*635a8641SAndroid Build Coastguard Worker DCHECK(it != sources_.end()); 39*635a8641SAndroid Build Coastguard Worker sources_.erase(it); 40*635a8641SAndroid Build Coastguard Worker source->RemoveObserver(observer_); 41*635a8641SAndroid Build Coastguard Worker } 42*635a8641SAndroid Build Coastguard Worker RemoveAll()43*635a8641SAndroid Build Coastguard Worker void RemoveAll() { 44*635a8641SAndroid Build Coastguard Worker for (size_t i = 0; i < sources_.size(); ++i) 45*635a8641SAndroid Build Coastguard Worker sources_[i]->RemoveObserver(observer_); 46*635a8641SAndroid Build Coastguard Worker sources_.clear(); 47*635a8641SAndroid Build Coastguard Worker } 48*635a8641SAndroid Build Coastguard Worker IsObserving(Source * source)49*635a8641SAndroid Build Coastguard Worker bool IsObserving(Source* source) const { 50*635a8641SAndroid Build Coastguard Worker return base::ContainsValue(sources_, source); 51*635a8641SAndroid Build Coastguard Worker } 52*635a8641SAndroid Build Coastguard Worker IsObservingSources()53*635a8641SAndroid Build Coastguard Worker bool IsObservingSources() const { return !sources_.empty(); } 54*635a8641SAndroid Build Coastguard Worker 55*635a8641SAndroid Build Coastguard Worker private: 56*635a8641SAndroid Build Coastguard Worker Observer* observer_; 57*635a8641SAndroid Build Coastguard Worker 58*635a8641SAndroid Build Coastguard Worker std::vector<Source*> sources_; 59*635a8641SAndroid Build Coastguard Worker 60*635a8641SAndroid Build Coastguard Worker DISALLOW_COPY_AND_ASSIGN(ScopedObserver); 61*635a8641SAndroid Build Coastguard Worker }; 62*635a8641SAndroid Build Coastguard Worker 63*635a8641SAndroid Build Coastguard Worker #endif // BASE_SCOPED_OBSERVER_H_ 64