henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1 | /* |
kjellander | b24317b | 2016-02-10 07:54:43 -0800 | [diff] [blame] | 2 | * Copyright 2011 The WebRTC project authors. All Rights Reserved. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3 | * |
kjellander | b24317b | 2016-02-10 07:54:43 -0800 | [diff] [blame] | 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 9 | */ |
| 10 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #ifndef API_NOTIFIER_H_ |
| 12 | #define API_NOTIFIER_H_ |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 13 | |
| 14 | #include <list> |
| 15 | |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 16 | #include "api/media_stream_interface.h" |
Byoungchan Lee | b36f689 | 2022-03-03 05:55:22 +0900 | [diff] [blame] | 17 | #include "api/sequence_checker.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 18 | #include "rtc_base/checks.h" |
Byoungchan Lee | b36f689 | 2022-03-03 05:55:22 +0900 | [diff] [blame] | 19 | #include "rtc_base/system/no_unique_address.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 20 | |
| 21 | namespace webrtc { |
| 22 | |
deadbeef | b10f32f | 2017-02-08 01:38:21 -0800 | [diff] [blame] | 23 | // Implements a template version of a notifier. |
| 24 | // TODO(deadbeef): This is an implementation detail; move out of api/. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 25 | template <class T> |
| 26 | class Notifier : public T { |
| 27 | public: |
Byoungchan Lee | b36f689 | 2022-03-03 05:55:22 +0900 | [diff] [blame] | 28 | Notifier() { sequence_checker_.Detach(); } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 29 | |
| 30 | virtual void RegisterObserver(ObserverInterface* observer) { |
Byoungchan Lee | b36f689 | 2022-03-03 05:55:22 +0900 | [diff] [blame] | 31 | RTC_DCHECK_RUN_ON(&sequence_checker_); |
deadbeef | 8d60a94 | 2017-02-27 14:47:33 -0800 | [diff] [blame] | 32 | RTC_DCHECK(observer != nullptr); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 33 | observers_.push_back(observer); |
| 34 | } |
| 35 | |
| 36 | virtual void UnregisterObserver(ObserverInterface* observer) { |
Byoungchan Lee | b36f689 | 2022-03-03 05:55:22 +0900 | [diff] [blame] | 37 | RTC_DCHECK_RUN_ON(&sequence_checker_); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 38 | for (std::list<ObserverInterface*>::iterator it = observers_.begin(); |
| 39 | it != observers_.end(); it++) { |
| 40 | if (*it == observer) { |
| 41 | observers_.erase(it); |
| 42 | break; |
| 43 | } |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | void FireOnChanged() { |
Byoungchan Lee | b36f689 | 2022-03-03 05:55:22 +0900 | [diff] [blame] | 48 | RTC_DCHECK_RUN_ON(&sequence_checker_); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 49 | // Copy the list of observers to avoid a crash if the observer object |
| 50 | // unregisters as a result of the OnChanged() call. If the same list is used |
| 51 | // UnregisterObserver will affect the list make the iterator invalid. |
| 52 | std::list<ObserverInterface*> observers = observers_; |
| 53 | for (std::list<ObserverInterface*>::iterator it = observers.begin(); |
| 54 | it != observers.end(); ++it) { |
| 55 | (*it)->OnChanged(); |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | protected: |
Byoungchan Lee | b36f689 | 2022-03-03 05:55:22 +0900 | [diff] [blame] | 60 | std::list<ObserverInterface*> observers_ RTC_GUARDED_BY(sequence_checker_); |
| 61 | |
| 62 | private: |
| 63 | RTC_NO_UNIQUE_ADDRESS SequenceChecker sequence_checker_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 64 | }; |
| 65 | |
| 66 | } // namespace webrtc |
| 67 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 68 | #endif // API_NOTIFIER_H_ |