OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 #include "chrome/browser/prefs/pref_change_registrar.h" | 5 #include "chrome/browser/prefs/pref_change_registrar.h" |
6 | 6 |
7 #include "chrome/browser/prefs/pref_service.h" | 7 #include "chrome/browser/prefs/pref_service.h" |
8 | 8 |
9 PrefChangeRegistrar::PrefChangeRegistrar() : service_(NULL) {} | 9 PrefChangeRegistrar::PrefChangeRegistrar() : service_(NULL) {} |
10 | 10 |
11 PrefChangeRegistrar::~PrefChangeRegistrar() { | 11 PrefChangeRegistrar::~PrefChangeRegistrar() { |
12 if (service_) { | 12 RemoveAll(); |
13 for (std::set<ObserverRegistration>::const_iterator it = observers_.begin(); | |
14 it != observers_.end(); ++it) { | |
15 service_->RemovePrefObserver(it->first.c_str(), it->second); | |
16 } | |
17 } | |
18 } | 13 } |
19 | 14 |
20 void PrefChangeRegistrar::Init(PrefService* service) { | 15 void PrefChangeRegistrar::Init(PrefService* service) { |
21 DCHECK(!service_); | 16 DCHECK(IsEmpty() || service_ == service); |
22 service_ = service; | 17 service_ = service; |
23 } | 18 } |
24 | 19 |
25 void PrefChangeRegistrar::Add(const char* path, NotificationObserver* obs) { | 20 void PrefChangeRegistrar::Add(const char* path, NotificationObserver* obs) { |
26 if (!service_) { | 21 if (!service_) { |
27 NOTREACHED(); | 22 NOTREACHED(); |
28 return; | 23 return; |
29 } | 24 } |
30 ObserverRegistration registration(path, obs); | 25 ObserverRegistration registration(path, obs); |
31 if (observers_.find(registration) != observers_.end()) { | 26 if (observers_.find(registration) != observers_.end()) { |
(...skipping 12 matching lines...) Expand all Loading... |
44 ObserverRegistration registration(path, obs); | 39 ObserverRegistration registration(path, obs); |
45 std::set<ObserverRegistration>::iterator it = | 40 std::set<ObserverRegistration>::iterator it = |
46 observers_.find(registration); | 41 observers_.find(registration); |
47 if (it == observers_.end()) { | 42 if (it == observers_.end()) { |
48 NOTREACHED(); | 43 NOTREACHED(); |
49 return; | 44 return; |
50 } | 45 } |
51 service_->RemovePrefObserver(it->first.c_str(), it->second); | 46 service_->RemovePrefObserver(it->first.c_str(), it->second); |
52 observers_.erase(it); | 47 observers_.erase(it); |
53 } | 48 } |
| 49 |
| 50 void PrefChangeRegistrar::RemoveAll() { |
| 51 if (service_) { |
| 52 for (std::set<ObserverRegistration>::const_iterator it = observers_.begin(); |
| 53 it != observers_.end(); ++it) { |
| 54 service_->RemovePrefObserver(it->first.c_str(), it->second); |
| 55 } |
| 56 observers_.clear(); |
| 57 } |
| 58 } |
| 59 |
| 60 bool PrefChangeRegistrar::IsEmpty() const { |
| 61 return observers_.empty(); |
| 62 } |
OLD | NEW |