Index: content/browser/media/webrtc/webrtc_eventlog_host.cc |
diff --git a/content/browser/media/webrtc/webrtc_eventlog_host.cc b/content/browser/media/webrtc/webrtc_eventlog_host.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..541cd1ebc10ac3a4298efc39ee1412a10559504b |
--- /dev/null |
+++ b/content/browser/media/webrtc/webrtc_eventlog_host.cc |
@@ -0,0 +1,135 @@ |
+// Copyright 2016 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "content/browser/media/webrtc/webrtc_eventlog_host.h" |
+ |
+#include <string> |
+ |
+#include "base/files/file_util.h" |
+#include "base/strings/string_number_conversions.h" |
+#include "content/common/media/peer_connection_tracker_messages.h" |
+#include "content/public/browser/browser_thread.h" |
+#include "content/public/browser/render_process_host.h" |
+#include "ipc/ipc_platform_file.h" |
+ |
+namespace content { |
+ |
+namespace { |
+ |
+#if defined(OS_ANDROID) |
+const int kMaxNumberLogFiles = 3; |
+#else |
+const int kMaxNumberLogFiles = 10; |
+#endif |
+ |
+// Appends the IDs to the RTC event log file name. |
+base::FilePath GetWebRtcEventLogPath(const base::FilePath& base_file, |
+ int render_process_id, |
+ int connection_id) { |
+ return base_file.AddExtension(base::IntToString(render_process_id)) |
+ .AddExtension(base::IntToString(connection_id)); |
+} |
+ |
+// Opens a logfile to pass on to the renderer. |
+IPC::PlatformFileForTransit CreateFileForProcess( |
+ const base::FilePath& base_path, |
+ int render_process_id, |
+ int connection_id) { |
+ DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
+ base::FilePath file_path = |
+ GetWebRtcEventLogPath(base_path, render_process_id, connection_id); |
+ base::File event_log_file( |
+ file_path, base::File::FLAG_OPEN_ALWAYS | base::File::FLAG_APPEND); |
+ if (!event_log_file.IsValid()) { |
+ VLOG(1) << "Could not open WebRTC event log file, error=" |
tommi (sloooow) - chröme
2016/05/25 15:16:20
LOGP(ERROR)?
Ivo-OOO until feb 6
2016/05/30 15:04:14
Done.
|
+ << event_log_file.error_details(); |
+ return IPC::InvalidPlatformFileForTransit(); |
+ } |
+ return IPC::TakePlatformFileForTransit(std::move(event_log_file)); |
+} |
+ |
+void SendEventLogFileToRenderer(content::RenderProcessHost* rph, |
+ int local_id, |
+ IPC::PlatformFileForTransit file_for_transit) { |
+ if (file_for_transit == IPC::InvalidPlatformFileForTransit()) { |
tommi (sloooow) - chröme
2016/05/25 15:16:20
nit: skip {} in these cases. it makes the code sli
Ivo-OOO until feb 6
2016/05/30 15:04:14
Done.
|
+ return; |
+ } |
+ rph->Send( |
+ new PeerConnectionTracker_StartEventLog(local_id, file_for_transit)); |
+} |
+ |
+} // namespace |
+ |
+WebRTCEventLogHost::WebRTCEventLogHost(int render_process_id) |
+ : render_process_id_(render_process_id), |
+ number_active_log_files_(0), |
+ is_rtc_event_logging_in_progress_(false) { |
+ thread_checker_.DetachFromThread(); |
+} |
+ |
+WebRTCEventLogHost::~WebRTCEventLogHost() {} |
+ |
+void WebRTCEventLogHost::PeerConnectionAdded(int connection_id) { |
+ DCHECK(thread_checker_.CalledOnValidThread()); |
+ DCHECK(active_peer_connection_local_id_.count(connection_id) == 0); |
+ active_peer_connection_local_id_.insert(connection_id); |
+ if (is_rtc_event_logging_in_progress_ && |
+ number_active_log_files_ < kMaxNumberLogFiles) { |
+ content::RenderProcessHost* host = |
+ content::RenderProcessHost::FromID(render_process_id_); |
+ if (host) { |
+ BrowserThread::PostTaskAndReplyWithResult( |
+ BrowserThread::FILE, FROM_HERE, |
+ base::Bind(&CreateFileForProcess, base_file_path_, render_process_id_, |
+ connection_id), |
+ base::Bind(&SendEventLogFileToRenderer, host, connection_id)); |
+ ++number_active_log_files_; |
+ } |
+ } |
+} |
+ |
+void WebRTCEventLogHost::PeerConnectionRemoved(int connection_id) { |
+ DCHECK(thread_checker_.CalledOnValidThread()); |
+ DCHECK(active_peer_connection_local_id_.count(connection_id) == 1); |
+ active_peer_connection_local_id_.erase(connection_id); |
+} |
+ |
+bool WebRTCEventLogHost::StartWebRTCEventLog(const base::FilePath& file_path) { |
+ DCHECK(thread_checker_.CalledOnValidThread()); |
+ if (is_rtc_event_logging_in_progress_) |
+ return false; |
+ RenderProcessHost* host = RenderProcessHost::FromID(render_process_id_); |
+ if (host) { |
+ is_rtc_event_logging_in_progress_ = true; |
+ base_file_path_ = file_path; |
+ number_active_log_files_ = 0; |
+ for (int local_id : active_peer_connection_local_id_) { |
+ if (number_active_log_files_ < kMaxNumberLogFiles) { |
+ BrowserThread::PostTaskAndReplyWithResult( |
+ BrowserThread::FILE, FROM_HERE, |
+ base::Bind(&CreateFileForProcess, file_path, render_process_id_, |
+ local_id), |
+ base::Bind(&SendEventLogFileToRenderer, host, local_id)); |
+ number_active_log_files_++; |
tommi (sloooow) - chröme
2016/05/25 15:16:20
++foo
Ivo-OOO until feb 6
2016/05/30 15:04:14
Done.
|
+ } |
+ } |
+ } |
+ return true; |
+} |
+ |
+bool WebRTCEventLogHost::StopWebRTCEventLog() { |
+ DCHECK(thread_checker_.CalledOnValidThread()); |
+ if (!is_rtc_event_logging_in_progress_) |
+ return false; |
+ is_rtc_event_logging_in_progress_ = false; |
+ RenderProcessHost* host = RenderProcessHost::FromID(render_process_id_); |
+ if (host) { |
+ for (int local_id : active_peer_connection_local_id_) { |
tommi (sloooow) - chröme
2016/05/25 15:16:20
nit: can skip {}
Ivo-OOO until feb 6
2016/05/30 15:04:14
Done.
|
+ host->Send(new PeerConnectionTracker_StopEventLog(local_id)); |
+ } |
+ } |
+ return true; |
+} |
+ |
+} // namespace content |