Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/media/webrtc_event_log_handler.h" | 5 #include "chrome/browser/media/webrtc_event_log_handler.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 #include <utility> | 8 #include <utility> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| 11 #include "base/command_line.h" | 11 #include "base/command_line.h" |
| 12 #include "base/files/file_util.h" | 12 #include "base/files/file_util.h" |
| 13 #include "base/strings/string_number_conversions.h" | 13 #include "base/strings/string_number_conversions.h" |
| 14 #include "base/time/time.h" | 14 #include "base/time/time.h" |
| 15 #include "chrome/browser/media/webrtc_log_list.h" | 15 #include "chrome/browser/media/webrtc_log_list.h" |
| 16 #include "chrome/browser/profiles/profile.h" | 16 #include "chrome/browser/profiles/profile.h" |
| 17 #include "chrome/common/chrome_switches.h" | 17 #include "chrome/common/chrome_switches.h" |
| 18 #include "content/common/media/peer_connection_tracker_messages.h" | |
|
ncarter (slow)
2016/05/18 21:49:56
As general a rule, IPC messages are not part of th
Henrik Grunell
2016/05/19 14:10:41
Ah, good point. The IPC should be done over conten
Ivo-OOO until feb 6
2016/05/20 14:58:48
I moved the IPC sending code into content/, in a n
| |
| 18 #include "content/public/browser/browser_thread.h" | 19 #include "content/public/browser/browser_thread.h" |
| 19 #include "content/public/browser/render_process_host.h" | 20 #include "content/public/browser/render_process_host.h" |
| 21 #include "ipc/ipc_platform_file.h" | |
| 20 | 22 |
| 21 using content::BrowserThread; | 23 using content::BrowserThread; |
| 22 | 24 |
| 23 // Keys used to attach handler to the RenderProcessHost | 25 // Keys used to attach handler to the RenderProcessHost |
| 24 const char WebRtcEventLogHandler::kWebRtcEventLogHandlerKey[] = | 26 const char WebRtcEventLogHandler::kWebRtcEventLogHandlerKey[] = |
| 25 "kWebRtcEventLogHandlerKey"; | 27 "kWebRtcEventLogHandlerKey"; |
| 26 | 28 |
| 27 namespace { | 29 namespace { |
| 28 | 30 |
| 31 #if defined(OS_ANDROID) | |
| 32 const int kMaxNumberLogFiles = 3; | |
| 33 #else | |
| 34 const int kMaxNumberLogFiles = 10; | |
| 35 #endif | |
| 36 | |
| 29 // Returns a path name to be used as prefix for RTC event log files. | 37 // Returns a path name to be used as prefix for RTC event log files. |
| 30 base::FilePath GetWebRtcEventLogPrefixPath(const base::FilePath& directory, | 38 base::FilePath GetWebRtcEventLogPrefixPath(const base::FilePath& directory) { |
| 31 uint64_t rtc_event_log_id) { | |
| 32 static const char kWebRtcEventLogFilePrefix[] = "WebRtcEventLog."; | 39 static const char kWebRtcEventLogFilePrefix[] = "WebRtcEventLog."; |
| 33 return directory.AppendASCII(kWebRtcEventLogFilePrefix + | 40 return directory.AppendASCII(kWebRtcEventLogFilePrefix); |
| 34 base::Int64ToString(rtc_event_log_id)); | |
| 35 } | 41 } |
| 36 | 42 |
| 43 // Appends the IDs to the RTC event log file name. | |
| 44 base::FilePath GetWebRtcEventLogPath(const base::FilePath& base_file, | |
| 45 int render_process_id, | |
| 46 uint64_t rtc_event_log_id) { | |
| 47 return base_file.AddExtension(base::IntToString(render_process_id)) | |
| 48 .AddExtension(base::Uint64ToString(rtc_event_log_id)); | |
| 49 } | |
| 50 | |
| 51 // Opens a logfile to pass on to the renderer. | |
| 52 IPC::PlatformFileForTransit CreateFileForProcess( | |
| 53 const base::FilePath& base_path, | |
| 54 int render_process_id, | |
| 55 uint64_t rtc_event_log_id) { | |
| 56 DCHECK_CURRENTLY_ON(BrowserThread::FILE); | |
| 57 base::FilePath file_path = | |
| 58 GetWebRtcEventLogPath(base_path, render_process_id, rtc_event_log_id); | |
| 59 base::File event_log_file( | |
| 60 file_path, base::File::FLAG_OPEN_ALWAYS | base::File::FLAG_APPEND); | |
| 61 if (!event_log_file.IsValid()) { | |
| 62 VLOG(1) << "Could not open WebRTC event log file, error=" | |
| 63 << event_log_file.error_details(); | |
| 64 return IPC::InvalidPlatformFileForTransit(); | |
| 65 } | |
| 66 return IPC::TakePlatformFileForTransit(std::move(event_log_file)); | |
| 67 } | |
| 68 | |
| 69 void SendEventLogFileToRenderer(content::RenderProcessHost* rph, | |
| 70 int local_id, | |
| 71 IPC::PlatformFileForTransit file_for_transit) { | |
| 72 if (file_for_transit == IPC::InvalidPlatformFileForTransit()) { | |
| 73 return; | |
| 74 } | |
| 75 rph->Send( | |
| 76 new PeerConnectionTracker_StartEventLog(local_id, file_for_transit)); | |
| 77 } | |
| 37 } // namespace | 78 } // namespace |
| 38 | 79 |
| 39 WebRtcEventLogHandler::WebRtcEventLogHandler(Profile* profile) | 80 WebRtcEventLogHandler::WebRtcEventLogHandler(int process_id, Profile* profile) |
| 40 : profile_(profile), | 81 : render_process_id(process_id), |
| 82 profile_(profile), | |
| 41 is_rtc_event_logging_in_progress_(false), | 83 is_rtc_event_logging_in_progress_(false), |
| 84 number_active_log_files_(0), | |
| 42 current_rtc_event_log_id_(0) { | 85 current_rtc_event_log_id_(0) { |
| 43 DCHECK(profile_); | 86 DCHECK(profile_); |
| 44 thread_checker_.DetachFromThread(); | 87 thread_checker_.DetachFromThread(); |
| 88 content::RenderProcessHost* host = | |
| 89 content::RenderProcessHost::FromID(process_id); | |
| 90 using StartEventLogFn = | |
| 91 void (WebRtcEventLogHandler::*)(const base::FilePath& file_path); | |
| 92 using StopEventLogFn = void (WebRtcEventLogHandler::*)(); | |
| 93 StartEventLogFn start_fn = &WebRtcEventLogHandler::StartWebRtcEventLogging; | |
| 94 StopEventLogFn stop_fn = &WebRtcEventLogHandler::StopWebRtcEventLogging; | |
| 95 if (host) { | |
| 96 host->RegisterEventLogHandler(base::Bind(start_fn, this), | |
| 97 base::Bind(stop_fn, this)); | |
| 98 host->RegisterPeerConnectionCallbacks( | |
| 99 base::Bind(&WebRtcEventLogHandler::OnPeerConnectionAdded, this), | |
| 100 base::Bind(&WebRtcEventLogHandler::OnPeerConnectionRemoved, this)); | |
| 101 } | |
| 45 } | 102 } |
| 46 | 103 |
| 47 WebRtcEventLogHandler::~WebRtcEventLogHandler() {} | 104 WebRtcEventLogHandler::~WebRtcEventLogHandler() {} |
| 48 | 105 |
| 49 void WebRtcEventLogHandler::StartWebRtcEventLogging( | 106 void WebRtcEventLogHandler::StartWebRtcEventLogging( |
| 50 content::RenderProcessHost* host, | 107 content::RenderProcessHost* host, |
| 51 base::TimeDelta delay, | 108 base::TimeDelta delay, |
| 52 const RecordingDoneCallback& callback, | 109 const RecordingDoneCallback& callback, |
| 53 const RecordingErrorCallback& error_callback) { | 110 const RecordingErrorCallback& error_callback) { |
| 54 DCHECK(thread_checker_.CalledOnValidThread()); | 111 DCHECK(thread_checker_.CalledOnValidThread()); |
| 55 | 112 |
| 56 BrowserThread::PostTaskAndReplyWithResult( | 113 BrowserThread::PostTaskAndReplyWithResult( |
| 57 BrowserThread::FILE, FROM_HERE, | 114 BrowserThread::FILE, FROM_HERE, |
| 58 base::Bind(&WebRtcEventLogHandler::GetLogDirectoryAndEnsureExists, this), | 115 base::Bind(&WebRtcEventLogHandler::GetLogDirectoryAndEnsureExists, this), |
| 59 base::Bind(&WebRtcEventLogHandler::DoStartWebRtcEventLogging, this, host, | 116 base::Bind(&WebRtcEventLogHandler::DoStartWebRtcEventLogging, this, host, |
| 60 delay, callback, error_callback)); | 117 delay, callback, error_callback)); |
| 61 } | 118 } |
| 62 | 119 |
| 120 void WebRtcEventLogHandler::StartWebRtcEventLogging( | |
| 121 const base::FilePath& file_path) { | |
| 122 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 123 content::RenderProcessHost* host = | |
| 124 content::RenderProcessHost::FromID(render_process_id); | |
| 125 if (host) { | |
| 126 is_rtc_event_logging_in_progress_ = true; | |
| 127 base_file_path_ = file_path; | |
| 128 number_active_log_files_ = 0; | |
| 129 for (int local_id : active_peer_connection_local_id_) { | |
| 130 if (number_active_log_files_ < kMaxNumberLogFiles) { | |
| 131 BrowserThread::PostTaskAndReplyWithResult( | |
| 132 BrowserThread::FILE, FROM_HERE, | |
| 133 base::Bind(&CreateFileForProcess, file_path, render_process_id, | |
| 134 local_id), | |
| 135 base::Bind(&SendEventLogFileToRenderer, host, local_id)); | |
| 136 number_active_log_files_++; | |
| 137 } | |
| 138 } | |
| 139 } | |
| 140 } | |
| 141 | |
| 63 void WebRtcEventLogHandler::StopWebRtcEventLogging( | 142 void WebRtcEventLogHandler::StopWebRtcEventLogging( |
| 64 content::RenderProcessHost* host, | 143 content::RenderProcessHost* host, |
| 65 const RecordingDoneCallback& callback, | 144 const RecordingDoneCallback& callback, |
| 66 const RecordingErrorCallback& error_callback) { | 145 const RecordingErrorCallback& error_callback) { |
| 67 DCHECK(thread_checker_.CalledOnValidThread()); | 146 DCHECK(thread_checker_.CalledOnValidThread()); |
| 68 const bool is_manual_stop = true; | 147 const bool is_manual_stop = true; |
| 69 BrowserThread::PostTaskAndReplyWithResult( | 148 BrowserThread::PostTaskAndReplyWithResult( |
| 70 BrowserThread::FILE, FROM_HERE, | 149 BrowserThread::FILE, FROM_HERE, |
| 71 base::Bind(&WebRtcEventLogHandler::GetLogDirectoryAndEnsureExists, this), | 150 base::Bind(&WebRtcEventLogHandler::GetLogDirectoryAndEnsureExists, this), |
| 72 base::Bind(&WebRtcEventLogHandler::DoStopWebRtcEventLogging, this, host, | 151 base::Bind(&WebRtcEventLogHandler::DoStopWebRtcEventLogging, this, host, |
| 73 is_manual_stop, current_rtc_event_log_id_, callback, | 152 is_manual_stop, current_rtc_event_log_id_, callback, |
| 74 error_callback)); | 153 error_callback)); |
| 75 } | 154 } |
| 76 | 155 |
| 156 void WebRtcEventLogHandler::StopWebRtcEventLogging() { | |
| 157 is_rtc_event_logging_in_progress_ = false; | |
| 158 content::RenderProcessHost* host = | |
| 159 content::RenderProcessHost::FromID(render_process_id); | |
| 160 if (host) { | |
| 161 for (int local_id : active_peer_connection_local_id_) { | |
| 162 host->Send(new PeerConnectionTracker_StopEventLog(local_id)); | |
| 163 } | |
| 164 } | |
| 165 } | |
| 166 | |
| 167 void WebRtcEventLogHandler::OnPeerConnectionAdded(int connection_id) { | |
| 168 active_peer_connection_local_id_.insert(connection_id); | |
| 169 if (is_rtc_event_logging_in_progress_ && | |
| 170 number_active_log_files_ < kMaxNumberLogFiles) { | |
| 171 content::RenderProcessHost* host = | |
| 172 content::RenderProcessHost::FromID(render_process_id); | |
| 173 if (host) { | |
| 174 BrowserThread::PostTaskAndReplyWithResult( | |
| 175 BrowserThread::FILE, FROM_HERE, | |
| 176 base::Bind(&CreateFileForProcess, base_file_path_, render_process_id, | |
| 177 connection_id), | |
| 178 base::Bind(&SendEventLogFileToRenderer, host, connection_id)); | |
| 179 number_active_log_files_++; | |
| 180 } | |
| 181 } | |
| 182 } | |
| 183 | |
| 184 void WebRtcEventLogHandler::OnPeerConnectionRemoved(int connection_id) { | |
| 185 active_peer_connection_local_id_.erase(connection_id); | |
| 186 } | |
| 187 | |
| 77 base::FilePath WebRtcEventLogHandler::GetLogDirectoryAndEnsureExists() { | 188 base::FilePath WebRtcEventLogHandler::GetLogDirectoryAndEnsureExists() { |
| 78 DCHECK_CURRENTLY_ON(BrowserThread::FILE); | 189 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
| 79 base::FilePath log_dir_path = | 190 base::FilePath log_dir_path = |
| 80 WebRtcLogList::GetWebRtcLogDirectoryForProfile(profile_->GetPath()); | 191 WebRtcLogList::GetWebRtcLogDirectoryForProfile(profile_->GetPath()); |
| 81 base::File::Error error; | 192 base::File::Error error; |
| 82 if (!base::CreateDirectoryAndGetError(log_dir_path, &error)) { | 193 if (!base::CreateDirectoryAndGetError(log_dir_path, &error)) { |
| 83 DLOG(ERROR) << "Could not create WebRTC log directory, error: " << error; | 194 DLOG(ERROR) << "Could not create WebRTC log directory, error: " << error; |
| 84 return base::FilePath(); | 195 return base::FilePath(); |
| 85 } | 196 } |
| 86 return log_dir_path; | 197 return log_dir_path; |
| 87 } | 198 } |
| 88 | 199 |
| 89 void WebRtcEventLogHandler::DoStartWebRtcEventLogging( | 200 void WebRtcEventLogHandler::DoStartWebRtcEventLogging( |
| 90 content::RenderProcessHost* host, | 201 content::RenderProcessHost* host, |
| 91 base::TimeDelta delay, | 202 base::TimeDelta delay, |
| 92 const RecordingDoneCallback& callback, | 203 const RecordingDoneCallback& callback, |
| 93 const RecordingErrorCallback& error_callback, | 204 const RecordingErrorCallback& error_callback, |
| 94 const base::FilePath& log_directory) { | 205 const base::FilePath& log_directory) { |
| 95 DCHECK(thread_checker_.CalledOnValidThread()); | 206 DCHECK(thread_checker_.CalledOnValidThread()); |
| 96 | 207 |
| 97 if (is_rtc_event_logging_in_progress_) { | 208 if (is_rtc_event_logging_in_progress_) { |
| 98 error_callback.Run("RTC event logging already in progress"); | 209 error_callback.Run("RTC event logging already in progress"); |
| 99 return; | 210 return; |
| 100 } | 211 } |
| 101 | 212 base::FilePath prefix_path = GetWebRtcEventLogPrefixPath(log_directory); |
| 102 is_rtc_event_logging_in_progress_ = true; | 213 StartWebRtcEventLogging(prefix_path); |
| 103 base::FilePath prefix_path = | |
| 104 GetWebRtcEventLogPrefixPath(log_directory, ++current_rtc_event_log_id_); | |
| 105 host->EnableEventLogRecordings(prefix_path); | |
| 106 | 214 |
| 107 if (delay.is_zero()) { | 215 if (delay.is_zero()) { |
| 108 const bool is_stopped = false, is_manual_stop = false; | 216 const bool is_stopped = false, is_manual_stop = false; |
| 109 callback.Run(prefix_path.AsUTF8Unsafe(), is_stopped, is_manual_stop); | 217 callback.Run(prefix_path.AsUTF8Unsafe(), is_stopped, is_manual_stop); |
| 110 return; | 218 return; |
| 111 } | 219 } |
| 112 | 220 |
| 113 const bool is_manual_stop = false; | 221 const bool is_manual_stop = false; |
| 114 BrowserThread::PostDelayedTask( | 222 BrowserThread::PostDelayedTask( |
| 115 BrowserThread::UI, FROM_HERE, | 223 BrowserThread::UI, FROM_HERE, |
| 116 base::Bind(&WebRtcEventLogHandler::DoStopWebRtcEventLogging, this, host, | 224 base::Bind(&WebRtcEventLogHandler::DoStopWebRtcEventLogging, this, host, |
| 117 is_manual_stop, current_rtc_event_log_id_, callback, | 225 is_manual_stop, current_rtc_event_log_id_, callback, |
| 118 error_callback, log_directory), | 226 error_callback, log_directory), |
| 119 delay); | 227 delay); |
| 120 } | 228 } |
| 121 | 229 |
| 122 void WebRtcEventLogHandler::DoStopWebRtcEventLogging( | 230 void WebRtcEventLogHandler::DoStopWebRtcEventLogging( |
| 123 content::RenderProcessHost* host, | 231 content::RenderProcessHost* host, |
| 124 bool is_manual_stop, | 232 bool is_manual_stop, |
| 125 uint64_t rtc_event_log_id, | 233 uint64_t rtc_event_log_id, |
| 126 const RecordingDoneCallback& callback, | 234 const RecordingDoneCallback& callback, |
| 127 const RecordingErrorCallback& error_callback, | 235 const RecordingErrorCallback& error_callback, |
| 128 const base::FilePath& log_directory) { | 236 const base::FilePath& log_directory) { |
| 129 DCHECK(thread_checker_.CalledOnValidThread()); | 237 DCHECK(thread_checker_.CalledOnValidThread()); |
| 130 DCHECK_LE(rtc_event_log_id, current_rtc_event_log_id_); | 238 DCHECK_LE(rtc_event_log_id, current_rtc_event_log_id_); |
| 131 | 239 |
| 132 base::FilePath prefix_path = | 240 base::FilePath prefix_path = GetWebRtcEventLogPrefixPath(log_directory); |
| 133 GetWebRtcEventLogPrefixPath(log_directory, rtc_event_log_id); | 241 base::FilePath file_path = |
| 242 GetWebRtcEventLogPath(prefix_path, host->GetID(), rtc_event_log_id); | |
| 134 // Prevent an old posted DoStopWebRtcEventLogging() call to stop a newer dump. | 243 // Prevent an old posted DoStopWebRtcEventLogging() call to stop a newer dump. |
| 135 // This could happen in a sequence like: | 244 // This could happen in a sequence like: |
| 136 // Start(10); // Start dump 1. Post Stop() to run after 10 seconds. | 245 // Start(10); // Start dump 1. Post Stop() to run after 10 seconds. |
| 137 // Stop(); // Manually stop dump 1 before 10 seconds; | 246 // Stop(); // Manually stop dump 1 before 10 seconds; |
| 138 // Start(20); // Start dump 2. Posted Stop() for 1 should not stop dump 2. | 247 // Start(20); // Start dump 2. Posted Stop() for 1 should not stop dump 2. |
| 139 if (rtc_event_log_id < current_rtc_event_log_id_) { | 248 if (rtc_event_log_id < current_rtc_event_log_id_) { |
| 140 const bool is_stopped = false; | 249 const bool is_stopped = false; |
| 141 callback.Run(prefix_path.AsUTF8Unsafe(), is_stopped, is_manual_stop); | 250 callback.Run(file_path.AsUTF8Unsafe(), is_stopped, is_manual_stop); |
| 142 return; | 251 return; |
| 143 } | 252 } |
| 144 | 253 |
| 145 if (!is_rtc_event_logging_in_progress_) { | 254 if (!is_rtc_event_logging_in_progress_) { |
| 146 error_callback.Run("No RTC event logging in progress"); | 255 error_callback.Run("No RTC event logging in progress"); |
| 147 return; | 256 return; |
| 148 } | 257 } |
| 149 | 258 |
| 150 host->DisableEventLogRecordings(); | 259 StopWebRtcEventLogging(); |
| 151 is_rtc_event_logging_in_progress_ = false; | |
| 152 const bool is_stopped = true; | 260 const bool is_stopped = true; |
| 153 callback.Run(prefix_path.AsUTF8Unsafe(), is_stopped, is_manual_stop); | 261 callback.Run(file_path.AsUTF8Unsafe(), is_stopped, is_manual_stop); |
| 154 } | 262 } |
| OLD | NEW |