1 // Copyright 2017 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef CHROME_BROWSER_MEDIA_WEBRTC_WEBRTC_EVENT_LOG_MANAGER_LOCAL_H_ 6 #define CHROME_BROWSER_MEDIA_WEBRTC_WEBRTC_EVENT_LOG_MANAGER_LOCAL_H_ 7 8 #include <map> 9 #include <set> 10 #include <string> 11 12 #include "base/files/file_path.h" 13 #include "base/optional.h" 14 #include "base/sequence_checker.h" 15 #include "base/time/clock.h" 16 #include "chrome/browser/media/webrtc/webrtc_event_log_manager_common.h" 17 18 namespace webrtc_event_logging { 19 20 class WebRtcLocalEventLogManager final { 21 using LogFilesMap = 22 std::map<WebRtcEventLogPeerConnectionKey, std::unique_ptr<LogFileWriter>>; 23 using PeerConnectionKey = WebRtcEventLogPeerConnectionKey; 24 25 public: 26 explicit WebRtcLocalEventLogManager(WebRtcLocalEventLogsObserver* observer); 27 ~WebRtcLocalEventLogManager(); 28 29 bool PeerConnectionAdded(const PeerConnectionKey& key); 30 bool PeerConnectionRemoved(const PeerConnectionKey& key); 31 32 bool EnableLogging(const base::FilePath& base_path, 33 size_t max_file_size_bytes); 34 bool DisableLogging(); 35 36 bool EventLogWrite(const PeerConnectionKey& key, const std::string& message); 37 38 void RenderProcessHostExitedDestroyed(int render_process_id); 39 40 // This function is public, but this entire class is a protected 41 // implementation detail of WebRtcEventLogManager, which hides this 42 // function from everybody except its own unit tests. 43 void SetClockForTesting(base::Clock* clock); 44 45 private: 46 // Create a local log file. 47 void StartLogFile(const PeerConnectionKey& key); 48 49 // Closes an active log file. 50 // Returns an iterator to the next active log file. 51 LogFilesMap::iterator CloseLogFile(LogFilesMap::iterator it); 52 53 // Derives the name of a local log file. The format is: 54 // [user_defined]_[date]_[time]_[render_process_id]_[lid].[extension] 55 base::FilePath GetFilePath(const base::FilePath& base_path, 56 const PeerConnectionKey& key) const; 57 58 // This object is expected to be created and destroyed on the UI thread, 59 // but live on its owner's internal, IO-capable task queue. 60 SEQUENCE_CHECKER(io_task_sequence_checker_); 61 62 // Produces LogFileWriter instances, for writing the logs to files. 63 BaseLogFileWriterFactory log_file_writer_factory_; 64 65 // Observer which will be informed whenever a local log file is started or 66 // stopped. Through this, the owning WebRtcEventLogManager can be informed, 67 // and decide whether it wants to turn notifications from WebRTC on/off. 68 WebRtcLocalEventLogsObserver* const observer_; 69 70 // For unit tests only, and specifically for unit tests that verify the 71 // filename format (derived from the current time as well as the renderer PID 72 // and PeerConnection local ID), we want to make sure that the time and date 73 // cannot change between the time the clock is read by the unit under test 74 // (namely WebRtcEventLogManager) and the time it's read by the test. 75 base::Clock* clock_for_testing_; 76 77 // Currently active peer connections. PeerConnections which have been closed 78 // are not considered active, regardless of whether they have been torn down. 79 std::set<PeerConnectionKey> active_peer_connections_; 80 81 // Local log files, stored at the behest of the user (via WebRTCInternals). 82 LogFilesMap log_files_; 83 84 // If |base_path_| is empty, local logging is disabled. 85 // If nonempty, local logging is enabled, and all local logs will be saved 86 // to this directory. 87 base::FilePath base_path_; 88 89 // The maximum size for local logs, in bytes. 90 // If !has_value(), the value is unlimited. 91 base::Optional<size_t> max_log_file_size_bytes_; 92 93 DISALLOW_COPY_AND_ASSIGN(WebRtcLocalEventLogManager); 94 }; 95 96 } // namespace webrtc_event_logging 97 98 #endif // CHROME_BROWSER_MEDIA_WEBRTC_WEBRTC_EVENT_LOG_MANAGER_LOCAL_H_ 99