Chromium Code Reviews| 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 number_active_log_files_ = 0; | |
| 111 rtc_event_logging_enabled_ = false; | |
| 112 RenderProcessHost* host = RenderProcessHost::FromID(render_process_id_); | |
| 113 if (host) { | |
|
Henrik Grunell
2016/06/09 10:28:22
Just a note that if a render process goes away, we
Ivo-OOO until feb 6
2016/06/09 12:59:40
Acknowledged.
| |
| 114 for (int local_id : active_peer_connection_local_ids_) | |
| 115 host->Send(new PeerConnectionTracker_StopEventLog(local_id)); | |
| 116 } | |
| 117 return true; | |
| 118 } | |
| 119 | |
| 120 bool WebRTCEventLogHost::StartEventLogForPeerConnection( | |
| 121 int peer_connection_local_id) { | |
| 122 if (number_active_log_files_ < kMaxNumberLogFiles) { | |
| 123 ++number_active_log_files_; | |
| 124 BrowserThread::PostTaskAndReplyWithResult( | |
| 125 BrowserThread::FILE, FROM_HERE, | |
| 126 base::Bind(&CreateFileForProcess, base_file_path_, render_process_id_, | |
| 127 peer_connection_local_id), | |
| 128 base::Bind(&WebRTCEventLogHost::SendEventLogFileToRenderer, | |
| 129 weak_ptr_factory_.GetWeakPtr(), peer_connection_local_id)); | |
| 130 } | |
| 131 return true; | |
| 132 } | |
| 133 | |
| 134 void WebRTCEventLogHost::SendEventLogFileToRenderer( | |
| 135 int peer_connection_local_id, | |
| 136 IPC::PlatformFileForTransit file_for_transit) { | |
| 137 if (file_for_transit == IPC::InvalidPlatformFileForTransit()) { | |
| 138 --number_active_log_files_; | |
| 139 return; | |
| 140 } | |
| 141 RenderProcessHost* rph = RenderProcessHost::FromID(render_process_id_); | |
| 142 if (rph) { | |
| 143 rph->Send(new PeerConnectionTracker_StartEventLog(peer_connection_local_id, | |
| 144 file_for_transit)); | |
| 145 } else { | |
| 146 --number_active_log_files_; | |
| 147 IPC::PlatformFileForTransitToFile(file_for_transit).Close(); | |
| 148 } | |
| 149 } | |
| 150 | |
| 151 } // namespace content | |
| OLD | NEW |