Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(585)

Side by Side Diff: base/scoped_observer.h

Issue 676083003: Listen to Off The Record profiles in ContentSettingsHandler. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@lkcr
Patch Set: Fix GuestModeOptionsBrowserTest.LoadOptionsByURL by checking duplicate observer. Created 6 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | chrome/browser/ui/webui/options/content_settings_handler.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #ifndef BASE_SCOPED_OBSERVER_H_ 5 #ifndef BASE_SCOPED_OBSERVER_H_
6 #define BASE_SCOPED_OBSERVER_H_ 6 #define BASE_SCOPED_OBSERVER_H_
7 7
8 #include <algorithm> 8 #include <algorithm>
9 #include <vector> 9 #include <vector>
10 10
11 #include "base/basictypes.h" 11 #include "base/basictypes.h"
12 #include "base/logging.h"
12 13
13 // ScopedObserver is used to keep track of the set of sources an object has 14 // ScopedObserver is used to keep track of the set of sources an object has
14 // attached itself to as an observer. When ScopedObserver is destroyed it 15 // attached itself to as an observer. When ScopedObserver is destroyed it
15 // removes the object as an observer from all sources it has been added to. 16 // removes the object as an observer from all sources it has been added to.
16 template <class Source, class Observer> 17 template <class Source, class Observer>
17 class ScopedObserver { 18 class ScopedObserver {
18 public: 19 public:
19 explicit ScopedObserver(Observer* observer) : observer_(observer) {} 20 explicit ScopedObserver(Observer* observer) : observer_(observer) {}
20 21
21 ~ScopedObserver() { 22 ~ScopedObserver() {
22 RemoveAll(); 23 RemoveAll();
23 } 24 }
24 25
25 // Adds the object passed to the constructor as an observer on |source|. 26 // Adds the object passed to the constructor as an observer on |source|.
26 void Add(Source* source) { 27 void Add(Source* source) {
27 sources_.push_back(source); 28 sources_.push_back(source);
28 source->AddObserver(observer_); 29 source->AddObserver(observer_);
29 } 30 }
30 31
31 // Remove the object passed to the constructor as an observer from |source|. 32 // Remove the object passed to the constructor as an observer from |source|.
32 void Remove(Source* source) { 33 void Remove(Source* source) {
33 sources_.erase(std::find(sources_.begin(), sources_.end(), source)); 34 auto it = std::find(sources_.begin(), sources_.end(), source);
35 DCHECK(it != sources_.end());
36 sources_.erase(it);
34 source->RemoveObserver(observer_); 37 source->RemoveObserver(observer_);
35 } 38 }
36 39
37 void RemoveAll() { 40 void RemoveAll() {
38 for (size_t i = 0; i < sources_.size(); ++i) 41 for (size_t i = 0; i < sources_.size(); ++i)
39 sources_[i]->RemoveObserver(observer_); 42 sources_[i]->RemoveObserver(observer_);
40 sources_.clear(); 43 sources_.clear();
41 } 44 }
42 45
43 bool IsObserving(Source* source) const { 46 bool IsObserving(Source* source) const {
44 return std::find(sources_.begin(), sources_.end(), source) != 47 return std::find(sources_.begin(), sources_.end(), source) !=
45 sources_.end(); 48 sources_.end();
46 } 49 }
47 50
48 private: 51 private:
49 Observer* observer_; 52 Observer* observer_;
50 53
51 std::vector<Source*> sources_; 54 std::vector<Source*> sources_;
52 55
53 DISALLOW_COPY_AND_ASSIGN(ScopedObserver); 56 DISALLOW_COPY_AND_ASSIGN(ScopedObserver);
54 }; 57 };
55 58
56 #endif // BASE_SCOPED_OBSERVER_H_ 59 #endif // BASE_SCOPED_OBSERVER_H_
OLDNEW
« no previous file with comments | « no previous file | chrome/browser/ui/webui/options/content_settings_handler.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698