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