blob: fc2480e00ab00419f563a14886897152fafd0294 [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
kjellanderb24317b2016-02-10 07:54:43 -08002 * Copyright 2011 The WebRTC project authors. All Rights Reserved.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003 *
kjellanderb24317b2016-02-10 07:54:43 -08004 * 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.org28e20752013-07-10 00:45:36 +00009 */
10
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef API_NOTIFIER_H_
12#define API_NOTIFIER_H_
henrike@webrtc.org28e20752013-07-10 00:45:36 +000013
14#include <list>
15
Steve Anton10542f22019-01-11 09:11:00 -080016#include "api/media_stream_interface.h"
Byoungchan Leeb36f6892022-03-03 05:55:22 +090017#include "api/sequence_checker.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#include "rtc_base/checks.h"
Byoungchan Leeb36f6892022-03-03 05:55:22 +090019#include "rtc_base/system/no_unique_address.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000020
21namespace webrtc {
22
deadbeefb10f32f2017-02-08 01:38:21 -080023// Implements a template version of a notifier.
24// TODO(deadbeef): This is an implementation detail; move out of api/.
henrike@webrtc.org28e20752013-07-10 00:45:36 +000025template <class T>
26class Notifier : public T {
27 public:
Byoungchan Leeb36f6892022-03-03 05:55:22 +090028 Notifier() { sequence_checker_.Detach(); }
henrike@webrtc.org28e20752013-07-10 00:45:36 +000029
30 virtual void RegisterObserver(ObserverInterface* observer) {
Byoungchan Leeb36f6892022-03-03 05:55:22 +090031 RTC_DCHECK_RUN_ON(&sequence_checker_);
deadbeef8d60a942017-02-27 14:47:33 -080032 RTC_DCHECK(observer != nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000033 observers_.push_back(observer);
34 }
35
36 virtual void UnregisterObserver(ObserverInterface* observer) {
Byoungchan Leeb36f6892022-03-03 05:55:22 +090037 RTC_DCHECK_RUN_ON(&sequence_checker_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000038 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 Leeb36f6892022-03-03 05:55:22 +090048 RTC_DCHECK_RUN_ON(&sequence_checker_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000049 // 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 Leeb36f6892022-03-03 05:55:22 +090060 std::list<ObserverInterface*> observers_ RTC_GUARDED_BY(sequence_checker_);
61
62 private:
63 RTC_NO_UNIQUE_ADDRESS SequenceChecker sequence_checker_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000064};
65
66} // namespace webrtc
67
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020068#endif // API_NOTIFIER_H_