Add SequenceChecker on Notifier

Bug: None
Change-Id: I85e80576d92ddae55a3fbd144338d9c57fb80065
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/252520
Reviewed-by: Artem Titov <titovartem@webrtc.org>
Auto-Submit: Daniel.L (Byoungchan) Lee <daniel.l@hpcnt.com>
Reviewed-by: Harald Alvestrand <hta@webrtc.org>
Commit-Queue: Harald Alvestrand <hta@webrtc.org>
Cr-Commit-Position: refs/heads/main@{#36116}
diff --git a/api/BUILD.gn b/api/BUILD.gn
index 12e0c5c..9d9bf55 100644
--- a/api/BUILD.gn
+++ b/api/BUILD.gn
@@ -116,10 +116,12 @@
     ":audio_options_api",
     ":rtp_parameters",
     ":scoped_refptr",
+    ":sequence_checker",
     ":video_track_source_constraints",
     "../modules/audio_processing:audio_processing_statistics",
     "../rtc_base:checks",
     "../rtc_base:refcount",
+    "../rtc_base/system:no_unique_address",
     "../rtc_base/system:rtc_export",
     "video:recordable_encoded_frame",
     "video:video_frame",
diff --git a/api/DEPS b/api/DEPS
index ff493bf..bdeab0a 100644
--- a/api/DEPS
+++ b/api/DEPS
@@ -274,6 +274,10 @@
     "+rtc_base/ref_counted_object.h",
   ],
 
+  "notifier\.h": [
+    "+rtc_base/system/no_unique_address.h",
+  ],
+
   "simulated_network\.h": [
     "+rtc_base/random.h",
     "+rtc_base/thread_annotations.h",
diff --git a/api/notifier.h b/api/notifier.h
index c03b104..fc2480e 100644
--- a/api/notifier.h
+++ b/api/notifier.h
@@ -14,7 +14,9 @@
 #include <list>
 
 #include "api/media_stream_interface.h"
+#include "api/sequence_checker.h"
 #include "rtc_base/checks.h"
+#include "rtc_base/system/no_unique_address.h"
 
 namespace webrtc {
 
@@ -23,14 +25,16 @@
 template <class T>
 class Notifier : public T {
  public:
-  Notifier() {}
+  Notifier() { sequence_checker_.Detach(); }
 
   virtual void RegisterObserver(ObserverInterface* observer) {
+    RTC_DCHECK_RUN_ON(&sequence_checker_);
     RTC_DCHECK(observer != nullptr);
     observers_.push_back(observer);
   }
 
   virtual void UnregisterObserver(ObserverInterface* observer) {
+    RTC_DCHECK_RUN_ON(&sequence_checker_);
     for (std::list<ObserverInterface*>::iterator it = observers_.begin();
          it != observers_.end(); it++) {
       if (*it == observer) {
@@ -41,6 +45,7 @@
   }
 
   void FireOnChanged() {
+    RTC_DCHECK_RUN_ON(&sequence_checker_);
     // Copy the list of observers to avoid a crash if the observer object
     // unregisters as a result of the OnChanged() call. If the same list is used
     // UnregisterObserver will affect the list make the iterator invalid.
@@ -52,7 +57,10 @@
   }
 
  protected:
-  std::list<ObserverInterface*> observers_;
+  std::list<ObserverInterface*> observers_ RTC_GUARDED_BY(sequence_checker_);
+
+ private:
+  RTC_NO_UNIQUE_ADDRESS SequenceChecker sequence_checker_;
 };
 
 }  // namespace webrtc