tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

rtc_event_log_impl.h (4691B)


      1 /*
      2 *  Copyright (c) 2019 The WebRTC project authors. All Rights Reserved.
      3 *
      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.
      9 */
     10 
     11 #ifndef LOGGING_RTC_EVENT_LOG_RTC_EVENT_LOG_IMPL_H_
     12 #define LOGGING_RTC_EVENT_LOG_RTC_EVENT_LOG_IMPL_H_
     13 
     14 #include <cstddef>
     15 #include <cstdint>
     16 #include <deque>
     17 #include <functional>
     18 #include <memory>
     19 
     20 #include "absl/strings/string_view.h"
     21 #include "api/environment/environment.h"
     22 #include "api/rtc_event_log/rtc_event.h"
     23 #include "api/rtc_event_log/rtc_event_log.h"
     24 #include "api/rtc_event_log_output.h"
     25 #include "api/sequence_checker.h"
     26 #include "api/task_queue/task_queue_base.h"
     27 #include "logging/rtc_event_log/encoder/rtc_event_log_encoder.h"
     28 #include "rtc_base/synchronization/mutex.h"
     29 #include "rtc_base/system/no_unique_address.h"
     30 #include "rtc_base/thread_annotations.h"
     31 
     32 namespace webrtc {
     33 
     34 class RtcEventLogImpl final : public RtcEventLog {
     35 public:
     36  // The max number of events that the history can store.
     37  static constexpr size_t kMaxEventsInHistory = 10000;
     38  // The max number of events that the config history can store.
     39  // The config-history is supposed to be unbounded, but needs to have some
     40  // bound to prevent an attack via unreasonable memory use.
     41  static constexpr size_t kMaxEventsInConfigHistory = 1000;
     42 
     43  explicit RtcEventLogImpl(const Environment& env);
     44  RtcEventLogImpl(
     45      const Environment& env,
     46      std::unique_ptr<RtcEventLogEncoder> encoder,
     47      size_t max_events_in_history = kMaxEventsInHistory,
     48      size_t max_config_events_in_history = kMaxEventsInConfigHistory);
     49  RtcEventLogImpl(const RtcEventLogImpl&) = delete;
     50  RtcEventLogImpl& operator=(const RtcEventLogImpl&) = delete;
     51 
     52  ~RtcEventLogImpl() override;
     53 
     54  // TODO(eladalon): We should change these name to reflect that what we're
     55  // actually starting/stopping is the output of the log, not the log itself.
     56  bool StartLogging(std::unique_ptr<RtcEventLogOutput> output,
     57                    int64_t output_period_ms) override;
     58  void StopLogging() override;
     59  void StopLogging(std::function<void()> callback) override;
     60 
     61  // Records event into `recent_` on current thread, and schedules the output on
     62  // task queue if the buffers are full or `output_period_ms_` is expired.
     63  void Log(std::unique_ptr<RtcEvent> event) override;
     64 
     65 private:
     66  using EventDeque = std::deque<std::unique_ptr<RtcEvent>>;
     67 
     68  struct EventHistories {
     69    EventDeque config_history;
     70    EventDeque history;
     71  };
     72 
     73  // Helper to extract and clear `recent_`.
     74  EventHistories ExtractRecentHistories() RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
     75  void LogToMemory(std::unique_ptr<RtcEvent> event)
     76      RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
     77  void LogEventsToOutput(EventHistories histories) RTC_RUN_ON(task_queue_);
     78 
     79  void StopOutput() RTC_RUN_ON(task_queue_);
     80 
     81  void WriteConfigsAndHistoryToOutput(absl::string_view encoded_configs,
     82                                      absl::string_view encoded_history)
     83      RTC_RUN_ON(task_queue_);
     84  void WriteToOutput(absl::string_view output_string) RTC_RUN_ON(task_queue_);
     85 
     86  void StopLoggingInternal() RTC_RUN_ON(task_queue_);
     87 
     88  bool ShouldOutputImmediately() RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
     89  void ScheduleOutput() RTC_RUN_ON(task_queue_);
     90 
     91  const Environment env_;
     92 
     93  // Max size of event history.
     94  const size_t max_events_in_history_;
     95 
     96  // Max size of config event history.
     97  const size_t max_config_events_in_history_;
     98 
     99  // History containing all past configuration events.
    100  EventDeque all_config_history_ RTC_GUARDED_BY(task_queue_);
    101 
    102  // `config_history` containing the most recent configuration events.
    103  // `history` containing the most recent (non-configuration) events (~10s).
    104  EventHistories recent_ RTC_GUARDED_BY(mutex_);
    105 
    106  std::unique_ptr<RtcEventLogEncoder> event_encoder_
    107      RTC_GUARDED_BY(task_queue_);
    108  std::unique_ptr<RtcEventLogOutput> event_output_ RTC_GUARDED_BY(task_queue_);
    109 
    110  int64_t output_period_ms_ RTC_GUARDED_BY(task_queue_);
    111  int64_t last_output_ms_ RTC_GUARDED_BY(task_queue_);
    112 
    113  RTC_NO_UNIQUE_ADDRESS SequenceChecker logging_state_checker_;
    114  bool logging_state_started_ RTC_GUARDED_BY(mutex_) = false;
    115  bool immediately_output_mode_ RTC_GUARDED_BY(mutex_) = false;
    116  bool need_schedule_output_ RTC_GUARDED_BY(mutex_) = false;
    117 
    118  std::unique_ptr<TaskQueueBase, TaskQueueDeleter> task_queue_;
    119 
    120  Mutex mutex_;
    121 };
    122 
    123 }  // namespace webrtc
    124 
    125 #endif  //  LOGGING_RTC_EVENT_LOG_RTC_EVENT_LOG_IMPL_H_