File: webrtc_event_log_manager_local.h

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (119 lines) | stat: -rw-r--r-- 4,688 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
// Copyright 2017 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_BROWSER_MEDIA_WEBRTC_WEBRTC_EVENT_LOG_MANAGER_LOCAL_H_
#define CHROME_BROWSER_MEDIA_WEBRTC_WEBRTC_EVENT_LOG_MANAGER_LOCAL_H_

#include <map>
#include <optional>
#include <set>
#include <string>

#include "base/files/file_path.h"
#include "base/memory/raw_ptr.h"
#include "base/sequence_checker.h"
#include "base/time/clock.h"
#include "chrome/browser/media/webrtc/webrtc_event_log_manager_common.h"

namespace webrtc_event_logging {

class WebRtcLocalEventLogManager final {
  struct LogFiles;
  using PeerConnectionKey = WebRtcEventLogPeerConnectionKey;
  using LogFilesMap = std::map<PeerConnectionKey, LogFiles>;

 public:
  explicit WebRtcLocalEventLogManager(WebRtcLocalEventLogsObserver* observer);

  WebRtcLocalEventLogManager(const WebRtcLocalEventLogManager&) = delete;
  WebRtcLocalEventLogManager& operator=(const WebRtcLocalEventLogManager&) =
      delete;

  ~WebRtcLocalEventLogManager();

  bool OnPeerConnectionAdded(const PeerConnectionKey& key);
  bool OnPeerConnectionRemoved(const PeerConnectionKey& key);

  bool EnableEventLogging(const base::FilePath& base_path,
                          size_t max_file_size_bytes);
  bool DisableEventLogging();

  bool EventLogWrite(const PeerConnectionKey& key, const std::string& message);

  bool EnableDataChannelLogging(const base::FilePath& base_path,
                                size_t max_file_size_bytes);
  bool DisableDataChannelLogging();

  bool DataChannelLogWrite(const PeerConnectionKey& key,
                           const std::string& message);

  void RenderProcessHostExitedDestroyed(int render_process_id);

  // This function is public, but this entire class is a protected
  // implementation detail of WebRtcEventLogManager, which hides this
  // function from everybody except its own unit tests.
  void SetClockForTesting(base::Clock* clock);

 private:
  struct LogTypeState {
    LogTypeState(const base::FilePath& base_path,
                 std::optional<size_t> max_size_bytes,
                 std::optional<size_t> max_logs_active,
                 std::optional<size_t> max_logs_created)
        : base_path(base_path),
          max_size_bytes(max_size_bytes),
          max_logs_active(max_logs_active),
          max_logs_created(max_logs_created) {}
    const base::FilePath base_path;
    const std::optional<size_t> max_size_bytes;
    const std::optional<size_t> max_logs_active;
    const std::optional<size_t> max_logs_created;
    size_t logs_active = 0;
    size_t logs_created = 0;
  };

  void MaybeStartEventLogFile(LogFilesMap::iterator log_it);
  void StopEventLogFileIfStarted(LogFilesMap::iterator);
  void MaybeStartDataChannelLogFile(LogFilesMap::iterator log_it);
  void StopDataChannelLogFileIfStarted(LogFilesMap::iterator);

  std::unique_ptr<LogFileWriter> CreateLogFile(
      const PeerConnectionKey& key,
      const base::FilePath& base_path,
      std::optional<size_t> max_log_size_bytes);

  // Derives the name of a local log file. The format is:
  // [user_defined]_[date]_[time]_[render_process_id]_[lid].[extension]
  base::FilePath GetFilePath(const base::FilePath& base_path,
                             const PeerConnectionKey& key) const;

  // This object is expected to be created and destroyed on the UI thread,
  // but live on its owner's internal, IO-capable task queue.
  SEQUENCE_CHECKER(io_task_sequence_checker_);

  // Produces LogFileWriter instances, for writing the logs to files.
  BaseLogFileWriterFactory log_file_writer_factory_;

  // Observer which will be informed whenever a local log file is started or
  // stopped. Through this, the owning WebRtcEventLogManager can be informed,
  // and decide whether it wants to turn notifications from WebRTC on/off.
  const raw_ptr<WebRtcLocalEventLogsObserver> observer_;

  // For unit tests only, and specifically for unit tests that verify the
  // filename format (derived from the current time as well as the renderer PID
  // and PeerConnection local ID), we want to make sure that the time and date
  // cannot change between the time the clock is read by the unit under test
  // (namely WebRtcEventLogManager) and the time it's read by the test.
  raw_ptr<base::Clock> clock_for_testing_;

  // Local log files, stored at the behest of the user (via WebRTCInternals).
  LogFilesMap log_files_;

  std::optional<LogTypeState> event_log_state_;
  std::optional<LogTypeState> data_channel_log_state_;
};

}  // namespace webrtc_event_logging

#endif  // CHROME_BROWSER_MEDIA_WEBRTC_WEBRTC_EVENT_LOG_MANAGER_LOCAL_H_