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