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 <string> | |
8 | |
9 #include "base/files/file_util.h" | |
10 #include "base/strings/string_number_conversions.h" | |
11 #include "content/common/media/peer_connection_tracker_messages.h" | |
12 #include "content/public/browser/browser_thread.h" | |
13 #include "content/public/browser/render_process_host.h" | |
14 #include "ipc/ipc_platform_file.h" | |
15 | |
16 namespace content { | |
17 | |
18 int WebRTCEventLogHost::number_active_log_files_ = 0; | |
19 | |
20 namespace { | |
21 | |
22 #if defined(OS_ANDROID) | |
23 const int kMaxNumberLogFiles = 3; | |
24 #else | |
25 const int kMaxNumberLogFiles = 5; | |
26 #endif | |
27 | |
28 // Appends the IDs to the RTC event log file name. | |
29 base::FilePath GetWebRtcEventLogPath(const base::FilePath& base_file, | |
30 int render_process_id, | |
31 int connection_id) { | |
32 return base_file.AddExtension(base::IntToString(render_process_id)) | |
33 .AddExtension(base::IntToString(connection_id)); | |
34 } | |
35 | |
36 // Opens a logfile to pass on to the renderer. | |
37 IPC::PlatformFileForTransit CreateFileForProcess( | |
38 const base::FilePath& base_path, | |
39 int render_process_id, | |
40 int connection_id) { | |
41 DCHECK_CURRENTLY_ON(BrowserThread::FILE); | |
42 base::FilePath file_path = | |
43 GetWebRtcEventLogPath(base_path, render_process_id, connection_id); | |
44 base::File event_log_file( | |
45 file_path, base::File::FLAG_OPEN_ALWAYS | base::File::FLAG_APPEND); | |
46 if (!event_log_file.IsValid()) { | |
47 PLOG(ERROR) << "Could not open WebRTC event log file, error=" | |
48 << event_log_file.error_details(); | |
49 return IPC::InvalidPlatformFileForTransit(); | |
50 } | |
51 return IPC::TakePlatformFileForTransit(std::move(event_log_file)); | |
52 } | |
53 | |
54 void SendEventLogFileToRenderer(content::RenderProcessHost* rph, | |
55 int local_id, | |
56 IPC::PlatformFileForTransit file_for_transit) { | |
57 if (file_for_transit == IPC::InvalidPlatformFileForTransit()) | |
58 return; | |
59 | |
60 rph->Send( | |
61 new PeerConnectionTracker_StartEventLog(local_id, file_for_transit)); | |
62 } | |
63 | |
64 } // namespace | |
65 | |
66 WebRTCEventLogHost::WebRTCEventLogHost(int render_process_id) | |
67 : render_process_id_(render_process_id), rtc_event_logging_enabled_(false) { | |
68 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
69 } | |
70 | |
71 WebRTCEventLogHost::~WebRTCEventLogHost() {} | |
72 | |
73 void WebRTCEventLogHost::PeerConnectionAdded(int peer_connection_local_id) { | |
74 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
75 DCHECK(active_peer_connection_local_id_.count(peer_connection_local_id) == 0); | |
tommi (sloooow) - chröme
2016/05/30 15:37:16
It looks like unordered_set is also too complex fo
Ivo-OOO until feb 6
2016/05/31 08:18:30
Okay, that makes sense, I'll change to vector.
| |
76 active_peer_connection_local_id_.insert(peer_connection_local_id); | |
77 if (rtc_event_logging_enabled_ && | |
78 number_active_log_files_ < kMaxNumberLogFiles) { | |
79 content::RenderProcessHost* host = | |
80 content::RenderProcessHost::FromID(render_process_id_); | |
81 if (host) { | |
82 BrowserThread::PostTaskAndReplyWithResult( | |
83 BrowserThread::FILE, FROM_HERE, | |
84 base::Bind(&CreateFileForProcess, base_file_path_, render_process_id_, | |
85 peer_connection_local_id), | |
86 base::Bind(&SendEventLogFileToRenderer, host, | |
87 peer_connection_local_id)); | |
88 ++number_active_log_files_; | |
89 } | |
90 } | |
91 } | |
92 | |
93 void WebRTCEventLogHost::PeerConnectionRemoved(int peer_connection_local_id) { | |
94 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
95 DCHECK(active_peer_connection_local_id_.count(peer_connection_local_id) == 1); | |
96 active_peer_connection_local_id_.erase(peer_connection_local_id); | |
97 } | |
98 | |
99 bool WebRTCEventLogHost::StartWebRTCEventLog(const base::FilePath& file_path) { | |
100 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
101 if (rtc_event_logging_enabled_) | |
102 return false; | |
103 RenderProcessHost* host = RenderProcessHost::FromID(render_process_id_); | |
104 if (host) { | |
105 rtc_event_logging_enabled_ = true; | |
106 base_file_path_ = file_path; | |
107 for (int local_id : active_peer_connection_local_id_) { | |
108 if (number_active_log_files_ < kMaxNumberLogFiles) { | |
109 BrowserThread::PostTaskAndReplyWithResult( | |
110 BrowserThread::FILE, FROM_HERE, | |
111 base::Bind(&CreateFileForProcess, file_path, render_process_id_, | |
112 local_id), | |
113 base::Bind(&SendEventLogFileToRenderer, host, local_id)); | |
114 ++number_active_log_files_; | |
115 } | |
116 } | |
117 } | |
118 return true; | |
119 } | |
120 | |
121 bool WebRTCEventLogHost::StopWebRTCEventLog() { | |
122 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
123 if (!rtc_event_logging_enabled_) | |
124 return false; | |
125 rtc_event_logging_enabled_ = false; | |
126 number_active_log_files_ = 0; | |
127 RenderProcessHost* host = RenderProcessHost::FromID(render_process_id_); | |
128 if (host) { | |
129 for (int local_id : active_peer_connection_local_id_) | |
130 host->Send(new PeerConnectionTracker_StopEventLog(local_id)); | |
131 } | |
132 return true; | |
133 } | |
134 | |
135 } // namespace content | |
OLD | NEW |