OLD | NEW |
(Empty) | |
| 1 // Copyright 2013 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 "chrome/browser/media/webrtc_internal_log_handler_host.h" |
| 6 |
| 7 #include <string> |
| 8 #include <utility> |
| 9 |
| 10 #include "base/bind.h" |
| 11 #include "base/command_line.h" |
| 12 #include "base/cpu.h" |
| 13 #include "base/files/file_util.h" |
| 14 #include "base/logging.h" |
| 15 #include "base/prefs/pref_service.h" |
| 16 #include "base/strings/string_number_conversions.h" |
| 17 #include "base/sys_info.h" |
| 18 #include "base/time/time.h" |
| 19 #include "build/build_config.h" |
| 20 #include "chrome/browser/bad_message.h" |
| 21 #include "chrome/browser/browser_process.h" |
| 22 #include "chrome/browser/chromeos/settings/cros_settings.h" |
| 23 #include "chrome/browser/media/webrtc_log_list.h" |
| 24 #include "chrome/browser/media/webrtc_log_uploader.h" |
| 25 #include "chrome/browser/media/webrtc_rtp_dump_handler.h" |
| 26 #include "chrome/browser/profiles/profile.h" |
| 27 #include "chrome/common/channel_info.h" |
| 28 #include "chrome/common/chrome_switches.h" |
| 29 #include "chrome/common/media/webrtc_logging_messages.h" |
| 30 #include "chromeos/settings/cros_settings_names.h" |
| 31 #include "components/version_info/version_info.h" |
| 32 #include "content/public/browser/browser_thread.h" |
| 33 #include "content/public/browser/content_browser_client.h" |
| 34 #include "content/public/browser/gpu_data_manager.h" |
| 35 #include "content/public/browser/render_process_host.h" |
| 36 #include "gpu/config/gpu_info.h" |
| 37 #include "net/base/address_family.h" |
| 38 #include "net/base/ip_address_number.h" |
| 39 #include "net/base/net_util.h" |
| 40 #include "net/url_request/url_request_context_getter.h" |
| 41 |
| 42 #if defined(OS_LINUX) |
| 43 #include "base/linux_util.h" |
| 44 #endif |
| 45 |
| 46 #if defined(OS_MACOSX) |
| 47 #include "base/mac/mac_util.h" |
| 48 #endif |
| 49 |
| 50 #if defined(OS_CHROMEOS) |
| 51 #include "chromeos/system/statistics_provider.h" |
| 52 #endif |
| 53 |
| 54 using content::BrowserThread; |
| 55 |
| 56 namespace { |
| 57 |
| 58 // Returns a path name to be used as prefix for audio debug recordings files. |
| 59 base::FilePath GetAudioDebugRecordingsPrefixPath( |
| 60 const base::FilePath& directory, |
| 61 uint64_t audio_debug_recordings_id) { |
| 62 static const char kAudioDebugRecordingsFilePrefix[] = "AudioDebugRecordings."; |
| 63 return directory.AppendASCII(kAudioDebugRecordingsFilePrefix + |
| 64 base::Int64ToString(audio_debug_recordings_id)); |
| 65 } |
| 66 |
| 67 // Returns a path name to be used as prefix for RTC event log files. |
| 68 base::FilePath GetRtcEventLogPrefixPath(const base::FilePath& directory, |
| 69 uint64_t rtc_event_log_id) { |
| 70 static const char kRtcEventLogFilePrefix[] = "RtcEventLog."; |
| 71 return directory.AppendASCII(kRtcEventLogFilePrefix + |
| 72 base::Int64ToString(rtc_event_log_id)); |
| 73 } |
| 74 |
| 75 } // namespace |
| 76 |
| 77 WebRtcInternalLogHandlerHost::WebRtcInternalLogHandlerHost(Profile* profile) |
| 78 : BrowserMessageFilter(WebRtcLoggingMsgStart), |
| 79 profile_(profile), |
| 80 is_audio_debug_recordings_in_progress_(false), |
| 81 current_audio_debug_recordings_id_(0), |
| 82 is_rtc_event_logging_in_progress_(false), |
| 83 current_rtc_event_log_id_(0) { |
| 84 DCHECK(profile_); |
| 85 } |
| 86 |
| 87 WebRtcInternalLogHandlerHost::~WebRtcInternalLogHandlerHost() { |
| 88 // TODO: Can we check that we dont hold any references to any Javascript |
| 89 // callbacks? |
| 90 } |
| 91 |
| 92 void WebRtcInternalLogHandlerHost::StartAudioDebugRecordings( |
| 93 content::RenderProcessHost* host, |
| 94 base::TimeDelta delay, |
| 95 const TimeLimitedRecordingCallback& callback, |
| 96 const TimeLimitedRecordingErrorCallback& error_callback) { |
| 97 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 98 |
| 99 BrowserThread::PostTaskAndReplyWithResult( |
| 100 BrowserThread::FILE, FROM_HERE, |
| 101 base::Bind(&WebRtcInternalLogHandlerHost::GetLogDirectoryAndEnsureExists, |
| 102 this), |
| 103 base::Bind(&WebRtcInternalLogHandlerHost::DoStartAudioDebugRecordings, |
| 104 this, host, delay, callback, error_callback)); |
| 105 } |
| 106 |
| 107 void WebRtcInternalLogHandlerHost::StopAudioDebugRecordings( |
| 108 content::RenderProcessHost* host, |
| 109 const TimeLimitedRecordingCallback& callback, |
| 110 const TimeLimitedRecordingErrorCallback& error_callback) { |
| 111 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 112 BrowserThread::PostTaskAndReplyWithResult( |
| 113 BrowserThread::FILE, FROM_HERE, |
| 114 base::Bind(&WebRtcInternalLogHandlerHost::GetLogDirectoryAndEnsureExists, |
| 115 this), |
| 116 base::Bind(&WebRtcInternalLogHandlerHost::DoStopAudioDebugRecordings, |
| 117 this, host, true /* manual stop */, |
| 118 current_audio_debug_recordings_id_, callback, error_callback)); |
| 119 } |
| 120 |
| 121 void WebRtcInternalLogHandlerHost::StartRtcEventLogging( |
| 122 content::RenderProcessHost* host, |
| 123 base::TimeDelta delay, |
| 124 const TimeLimitedRecordingCallback& callback, |
| 125 const TimeLimitedRecordingErrorCallback& error_callback) { |
| 126 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 127 |
| 128 BrowserThread::PostTaskAndReplyWithResult( |
| 129 BrowserThread::FILE, FROM_HERE, |
| 130 base::Bind(&WebRtcInternalLogHandlerHost::GetLogDirectoryAndEnsureExists, |
| 131 this), |
| 132 base::Bind(&WebRtcInternalLogHandlerHost::DoStartRtcEventLogging, this, |
| 133 host, delay, callback, error_callback)); |
| 134 } |
| 135 |
| 136 void WebRtcInternalLogHandlerHost::StopRtcEventLogging( |
| 137 content::RenderProcessHost* host, |
| 138 const TimeLimitedRecordingCallback& callback, |
| 139 const TimeLimitedRecordingErrorCallback& error_callback) { |
| 140 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 141 BrowserThread::PostTaskAndReplyWithResult( |
| 142 BrowserThread::FILE, FROM_HERE, |
| 143 base::Bind(&WebRtcInternalLogHandlerHost::GetLogDirectoryAndEnsureExists, |
| 144 this), |
| 145 base::Bind(&WebRtcInternalLogHandlerHost::DoStopRtcEventLogging, this, |
| 146 host, true /* manual stop */, |
| 147 current_audio_debug_recordings_id_, callback, error_callback)); |
| 148 } |
| 149 |
| 150 void WebRtcInternalLogHandlerHost::OnChannelClosing() { |
| 151 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 152 // We might want to check whether we have created any log and |
| 153 // if so upload it. |
| 154 content::BrowserMessageFilter::OnChannelClosing(); |
| 155 } |
| 156 |
| 157 void WebRtcInternalLogHandlerHost::OnDestruct() const { |
| 158 BrowserThread::DeleteOnIOThread::Destruct(this); |
| 159 } |
| 160 |
| 161 bool WebRtcInternalLogHandlerHost::OnMessageReceived( |
| 162 const IPC::Message& message) { |
| 163 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 164 bool handled = false; |
| 165 return handled; |
| 166 } |
| 167 |
| 168 base::FilePath WebRtcInternalLogHandlerHost::GetLogDirectoryAndEnsureExists() { |
| 169 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
| 170 base::FilePath log_dir_path = |
| 171 WebRtcLogList::GetWebRtcLogDirectoryForProfile(profile_->GetPath()); |
| 172 base::File::Error error; |
| 173 if (!base::CreateDirectoryAndGetError(log_dir_path, &error)) { |
| 174 DLOG(ERROR) << "Could not create WebRTC log directory, error: " << error; |
| 175 return base::FilePath(); |
| 176 } |
| 177 return log_dir_path; |
| 178 } |
| 179 |
| 180 void WebRtcInternalLogHandlerHost::DoStartAudioDebugRecordings( |
| 181 content::RenderProcessHost* host, |
| 182 base::TimeDelta delay, |
| 183 const TimeLimitedRecordingCallback& callback, |
| 184 const TimeLimitedRecordingErrorCallback& error_callback, |
| 185 const base::FilePath& log_directory) { |
| 186 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 187 |
| 188 if (is_audio_debug_recordings_in_progress_) { |
| 189 error_callback.Run("Audio debug recordings already in progress"); |
| 190 return; |
| 191 } |
| 192 |
| 193 is_audio_debug_recordings_in_progress_ = true; |
| 194 base::FilePath prefix_path = GetAudioDebugRecordingsPrefixPath( |
| 195 log_directory, ++current_audio_debug_recordings_id_); |
| 196 host->EnableAudioDebugRecordings(prefix_path); |
| 197 |
| 198 if (delay.is_zero()) { |
| 199 callback.Run(prefix_path.AsUTF8Unsafe(), false /* not stopped */, |
| 200 false /* not manually stopped */); |
| 201 return; |
| 202 } |
| 203 |
| 204 BrowserThread::PostDelayedTask( |
| 205 BrowserThread::UI, FROM_HERE, |
| 206 base::Bind(&WebRtcInternalLogHandlerHost::DoStopAudioDebugRecordings, |
| 207 this, host, false /* no manual stop */, |
| 208 current_audio_debug_recordings_id_, callback, error_callback, |
| 209 prefix_path), |
| 210 delay); |
| 211 } |
| 212 |
| 213 void WebRtcInternalLogHandlerHost::DoStopAudioDebugRecordings( |
| 214 content::RenderProcessHost* host, |
| 215 bool is_manual_stop, |
| 216 uint64_t audio_debug_recordings_id, |
| 217 const TimeLimitedRecordingCallback& callback, |
| 218 const TimeLimitedRecordingErrorCallback& error_callback, |
| 219 const base::FilePath& log_directory) { |
| 220 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 221 DCHECK_LE(audio_debug_recordings_id, current_audio_debug_recordings_id_); |
| 222 |
| 223 base::FilePath prefix_path = GetAudioDebugRecordingsPrefixPath( |
| 224 log_directory, audio_debug_recordings_id); |
| 225 // Prevent an old posted StopAudioDebugRecordings() call to stop a newer dump. |
| 226 // This could happen in a sequence like: |
| 227 // Start(10); //Start dump 1. Post Stop() to run after 10 seconds. |
| 228 // Stop(); // Manually stop dump 1 before 10 seconds; |
| 229 // Start(20); // Start dump 2. Posted Stop() for 1 should not stop dump 2. |
| 230 if (audio_debug_recordings_id < current_audio_debug_recordings_id_) { |
| 231 callback.Run(prefix_path.AsUTF8Unsafe(), false /* not stopped */, |
| 232 is_manual_stop); |
| 233 return; |
| 234 } |
| 235 |
| 236 if (!is_audio_debug_recordings_in_progress_) { |
| 237 error_callback.Run("No audio debug recording in progress"); |
| 238 return; |
| 239 } |
| 240 |
| 241 host->DisableAudioDebugRecordings(); |
| 242 is_audio_debug_recordings_in_progress_ = false; |
| 243 callback.Run(prefix_path.AsUTF8Unsafe(), true /* stopped */, is_manual_stop); |
| 244 } |
| 245 |
| 246 void WebRtcInternalLogHandlerHost::DoStartRtcEventLogging( |
| 247 content::RenderProcessHost* host, |
| 248 base::TimeDelta delay, |
| 249 const TimeLimitedRecordingCallback& callback, |
| 250 const TimeLimitedRecordingErrorCallback& error_callback, |
| 251 const base::FilePath& log_directory) { |
| 252 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 253 |
| 254 if (is_rtc_event_logging_in_progress_) { |
| 255 error_callback.Run("RTC event logging already in progress"); |
| 256 return; |
| 257 } |
| 258 |
| 259 is_rtc_event_logging_in_progress_ = true; |
| 260 base::FilePath prefix_path = |
| 261 GetRtcEventLogPrefixPath(log_directory, ++current_rtc_event_log_id_); |
| 262 host->EnableEventLogRecordings(prefix_path); |
| 263 |
| 264 if (delay.is_zero()) { |
| 265 callback.Run(prefix_path.AsUTF8Unsafe(), false /* not stopped */, |
| 266 false /* not manually stopped */); |
| 267 return; |
| 268 } |
| 269 |
| 270 BrowserThread::PostDelayedTask( |
| 271 BrowserThread::UI, FROM_HERE, |
| 272 base::Bind(&WebRtcInternalLogHandlerHost::DoStopRtcEventLogging, this, |
| 273 host, false /* no manual stop */, current_rtc_event_log_id_, |
| 274 callback, error_callback, prefix_path), |
| 275 delay); |
| 276 } |
| 277 |
| 278 void WebRtcInternalLogHandlerHost::DoStopRtcEventLogging( |
| 279 content::RenderProcessHost* host, |
| 280 bool is_manual_stop, |
| 281 uint64_t rtc_event_log_id, |
| 282 const TimeLimitedRecordingCallback& callback, |
| 283 const TimeLimitedRecordingErrorCallback& error_callback, |
| 284 const base::FilePath& log_directory) { |
| 285 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 286 DCHECK_LE(rtc_event_log_id, current_rtc_event_log_id_); |
| 287 |
| 288 base::FilePath prefix_path = |
| 289 GetRtcEventLogPrefixPath(log_directory, rtc_event_log_id); |
| 290 // Prevent an old posted DoStopRtcEventLogging() call to stop a newer dump. |
| 291 // This could happen in a sequence like: |
| 292 // Start(10); //Start dump 1. Post Stop() to run after 10 seconds. |
| 293 // Stop(); // Manually stop dump 1 before 10 seconds; |
| 294 // Start(20); // Start dump 2. Posted Stop() for 1 should not stop dump 2. |
| 295 if (rtc_event_log_id < current_rtc_event_log_id_) { |
| 296 callback.Run(prefix_path.AsUTF8Unsafe(), false /* not stopped */, |
| 297 is_manual_stop); |
| 298 return; |
| 299 } |
| 300 |
| 301 if (!is_rtc_event_logging_in_progress_) { |
| 302 error_callback.Run("No RTC event logging in progress"); |
| 303 return; |
| 304 } |
| 305 |
| 306 host->DisableEventLogRecordings(); |
| 307 is_rtc_event_logging_in_progress_ = false; |
| 308 callback.Run(prefix_path.AsUTF8Unsafe(), true /* stopped */, is_manual_stop); |
| 309 } |
OLD | NEW |