OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 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 #include "content/browser/media/webrtc/webrtc_eventlog_host.h" |
| 6 |
| 7 #include <algorithm> |
| 8 #include <string> |
| 9 |
| 10 #include "base/files/file_util.h" |
| 11 #include "base/strings/string_number_conversions.h" |
| 12 #include "content/browser/media/webrtc/webrtc_internals.h" |
| 13 #include "content/common/media/peer_connection_tracker_messages.h" |
| 14 #include "content/public/browser/browser_thread.h" |
| 15 #include "content/public/browser/render_process_host.h" |
| 16 |
| 17 namespace content { |
| 18 |
| 19 int WebRTCEventLogHost::number_active_log_files_ = 0; |
| 20 |
| 21 namespace { |
| 22 |
| 23 // In addition to the limit to the number of files given below, the size of the |
| 24 // files is also capped, see content/renderer/media/peer_connection_tracker.cc. |
| 25 #if defined(OS_ANDROID) |
| 26 const int kMaxNumberLogFiles = 3; |
| 27 #else |
| 28 const int kMaxNumberLogFiles = 5; |
| 29 #endif |
| 30 |
| 31 // Appends the IDs to the RTC event log file name. |
| 32 base::FilePath GetWebRtcEventLogPath(const base::FilePath& base_file, |
| 33 int render_process_id, |
| 34 int connection_id) { |
| 35 return base_file.AddExtension(base::IntToString(render_process_id)) |
| 36 .AddExtension(base::IntToString(connection_id)); |
| 37 } |
| 38 |
| 39 // Opens a logfile to pass on to the renderer. |
| 40 IPC::PlatformFileForTransit CreateFileForProcess( |
| 41 const base::FilePath& base_path, |
| 42 int render_process_id, |
| 43 int connection_id) { |
| 44 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
| 45 base::FilePath file_path = |
| 46 GetWebRtcEventLogPath(base_path, render_process_id, connection_id); |
| 47 base::File event_log_file( |
| 48 file_path, base::File::FLAG_CREATE_ALWAYS | base::File::FLAG_WRITE); |
| 49 if (!event_log_file.IsValid()) { |
| 50 PLOG(ERROR) << "Could not open WebRTC event log file, error=" |
| 51 << event_log_file.error_details(); |
| 52 return IPC::InvalidPlatformFileForTransit(); |
| 53 } |
| 54 return IPC::TakePlatformFileForTransit(std::move(event_log_file)); |
| 55 } |
| 56 |
| 57 } // namespace |
| 58 |
| 59 WebRTCEventLogHost::WebRTCEventLogHost(int render_process_id) |
| 60 : render_process_id_(render_process_id), |
| 61 rtc_event_logging_enabled_(false), |
| 62 weak_ptr_factory_(this) { |
| 63 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 64 auto webrtc_internals = WebRTCInternals::GetInstance(); |
| 65 if (webrtc_internals->IsEventLogRecordingsEnabled()) |
| 66 StartWebRTCEventLog(webrtc_internals->GetEventLogFilePath()); |
| 67 } |
| 68 |
| 69 WebRTCEventLogHost::~WebRTCEventLogHost() { |
| 70 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 71 } |
| 72 |
| 73 void WebRTCEventLogHost::PeerConnectionAdded(int peer_connection_local_id) { |
| 74 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 75 DCHECK(std::find(active_peer_connection_local_ids_.begin(), |
| 76 active_peer_connection_local_ids_.end(), |
| 77 peer_connection_local_id) == |
| 78 active_peer_connection_local_ids_.end()); |
| 79 active_peer_connection_local_ids_.push_back(peer_connection_local_id); |
| 80 if (rtc_event_logging_enabled_ && |
| 81 number_active_log_files_ < kMaxNumberLogFiles) { |
| 82 StartEventLogForPeerConnection(peer_connection_local_id); |
| 83 } |
| 84 } |
| 85 |
| 86 void WebRTCEventLogHost::PeerConnectionRemoved(int peer_connection_local_id) { |
| 87 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 88 const auto found = std::find(active_peer_connection_local_ids_.begin(), |
| 89 active_peer_connection_local_ids_.end(), |
| 90 peer_connection_local_id); |
| 91 DCHECK(found != active_peer_connection_local_ids_.end()); |
| 92 active_peer_connection_local_ids_.erase(found); |
| 93 } |
| 94 |
| 95 bool WebRTCEventLogHost::StartWebRTCEventLog(const base::FilePath& file_path) { |
| 96 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 97 if (rtc_event_logging_enabled_) |
| 98 return false; |
| 99 rtc_event_logging_enabled_ = true; |
| 100 base_file_path_ = file_path; |
| 101 for (int local_id : active_peer_connection_local_ids_) |
| 102 StartEventLogForPeerConnection(local_id); |
| 103 return true; |
| 104 } |
| 105 |
| 106 bool WebRTCEventLogHost::StopWebRTCEventLog() { |
| 107 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 108 if (!rtc_event_logging_enabled_) |
| 109 return false; |
| 110 rtc_event_logging_enabled_ = false; |
| 111 RenderProcessHost* host = RenderProcessHost::FromID(render_process_id_); |
| 112 if (host) { |
| 113 for (int local_id : active_peer_connection_local_ids_) |
| 114 host->Send(new PeerConnectionTracker_StopEventLog(local_id)); |
| 115 } |
| 116 return true; |
| 117 } |
| 118 |
| 119 bool WebRTCEventLogHost::StartEventLogForPeerConnection( |
| 120 int peer_connection_local_id) { |
| 121 if (number_active_log_files_ < kMaxNumberLogFiles) { |
| 122 ++number_active_log_files_; |
| 123 BrowserThread::PostTaskAndReplyWithResult( |
| 124 BrowserThread::FILE, FROM_HERE, |
| 125 base::Bind(&CreateFileForProcess, base_file_path_, render_process_id_, |
| 126 peer_connection_local_id), |
| 127 base::Bind(&WebRTCEventLogHost::SendEventLogFileToRenderer, |
| 128 weak_ptr_factory_.GetWeakPtr(), peer_connection_local_id)); |
| 129 } |
| 130 return true; |
| 131 } |
| 132 |
| 133 void WebRTCEventLogHost::SendEventLogFileToRenderer( |
| 134 int peer_connection_local_id, |
| 135 IPC::PlatformFileForTransit file_for_transit) { |
| 136 if (file_for_transit == IPC::InvalidPlatformFileForTransit()) { |
| 137 --number_active_log_files_; |
| 138 return; |
| 139 } |
| 140 RenderProcessHost* rph = RenderProcessHost::FromID(render_process_id_); |
| 141 if (rph) { |
| 142 rph->Send(new PeerConnectionTracker_StartEventLog(peer_connection_local_id, |
| 143 file_for_transit)); |
| 144 } else { |
| 145 --number_active_log_files_; |
| 146 IPC::PlatformFileForTransitToFile(file_for_transit).Close(); |
| 147 } |
| 148 } |
| 149 |
| 150 } // namespace content |
OLD | NEW |