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 <algorithm> | |
8 #include <string> | |
9 | |
10 #include "base/files/file_util.h" | |
11 #include "base/strings/string_number_conversions.h" | |
12 #include "content/common/media/peer_connection_tracker_messages.h" | |
13 #include "content/public/browser/browser_thread.h" | |
14 #include "content/public/browser/render_process_host.h" | |
15 #include "ipc/ipc_platform_file.h" | |
16 | |
17 namespace content { | |
18 | |
19 int WebRTCEventLogHost::number_active_log_files_ = 0; | |
20 | |
21 namespace { | |
22 | |
23 #if defined(OS_ANDROID) | |
24 const int kMaxNumberLogFiles = 3; | |
Henrik Grunell
2016/06/01 08:31:11
Nit: Given the much smaller storage on Android, sh
Ivo-OOO until feb 6
2016/06/02 14:43:38
On android the max file size limit is set to 10MB
Henrik Grunell
2016/06/03 07:40:52
Ah OK, that sounds fine.
Nit: Could you comment o
Ivo-OOO until feb 6
2016/06/03 16:07:01
Done.
| |
25 #else | |
26 const int kMaxNumberLogFiles = 5; | |
27 #endif | |
28 | |
29 // Appends the IDs to the RTC event log file name. | |
30 base::FilePath GetWebRtcEventLogPath(const base::FilePath& base_file, | |
31 int render_process_id, | |
32 int connection_id) { | |
33 return base_file.AddExtension(base::IntToString(render_process_id)) | |
34 .AddExtension(base::IntToString(connection_id)); | |
35 } | |
36 | |
37 // Opens a logfile to pass on to the renderer. | |
38 IPC::PlatformFileForTransit CreateFileForProcess( | |
39 const base::FilePath& base_path, | |
40 int render_process_id, | |
41 int connection_id) { | |
42 DCHECK_CURRENTLY_ON(BrowserThread::FILE); | |
43 base::FilePath file_path = | |
44 GetWebRtcEventLogPath(base_path, render_process_id, connection_id); | |
45 base::File event_log_file( | |
46 file_path, base::File::FLAG_OPEN_ALWAYS | base::File::FLAG_APPEND); | |
Henrik Grunell
2016/06/01 08:31:11
Should it overwrite instead perhaps?
Ivo-OOO until feb 6
2016/06/02 14:43:38
Good point, I guess that makes more sense.
| |
47 if (!event_log_file.IsValid()) { | |
48 PLOG(ERROR) << "Could not open WebRTC event log file, error=" | |
49 << event_log_file.error_details(); | |
50 return IPC::InvalidPlatformFileForTransit(); | |
51 } | |
52 return IPC::TakePlatformFileForTransit(std::move(event_log_file)); | |
53 } | |
54 | |
55 void SendEventLogFileToRenderer(content::RenderProcessHost* rph, | |
56 int local_id, | |
57 IPC::PlatformFileForTransit file_for_transit) { | |
58 if (file_for_transit == IPC::InvalidPlatformFileForTransit()) | |
59 return; | |
60 | |
61 rph->Send( | |
62 new PeerConnectionTracker_StartEventLog(local_id, file_for_transit)); | |
63 } | |
64 | |
65 } // namespace | |
66 | |
67 WebRTCEventLogHost::WebRTCEventLogHost(int render_process_id) | |
68 : render_process_id_(render_process_id), rtc_event_logging_enabled_(false) { | |
69 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
70 } | |
71 | |
72 WebRTCEventLogHost::~WebRTCEventLogHost() {} | |
73 | |
74 void WebRTCEventLogHost::PeerConnectionAdded(int peer_connection_local_id) { | |
75 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
76 DCHECK(std::find(active_peer_connection_local_id_.begin(), | |
77 active_peer_connection_local_id_.end(), | |
78 peer_connection_local_id) == | |
79 active_peer_connection_local_id_.end()); | |
80 active_peer_connection_local_id_.push_back(peer_connection_local_id); | |
81 if (rtc_event_logging_enabled_ && | |
82 number_active_log_files_ < kMaxNumberLogFiles) { | |
83 content::RenderProcessHost* host = | |
84 content::RenderProcessHost::FromID(render_process_id_); | |
85 if (host) { | |
86 BrowserThread::PostTaskAndReplyWithResult( | |
87 BrowserThread::FILE, FROM_HERE, | |
88 base::Bind(&CreateFileForProcess, base_file_path_, render_process_id_, | |
89 peer_connection_local_id), | |
90 base::Bind(&SendEventLogFileToRenderer, host, | |
Henrik Grunell
2016/06/01 08:31:10
How is it guaranteed that |host| is valid when Sen
Ivo-OOO until feb 6
2016/06/02 14:43:38
Good catch, I did not consider that. I changed the
| |
91 peer_connection_local_id)); | |
92 ++number_active_log_files_; | |
Henrik Grunell
2016/06/01 08:31:10
If creating the file fails, this will be incorrect
Ivo-OOO until feb 6
2016/06/02 14:43:38
Done.
| |
93 } | |
94 } | |
95 } | |
96 | |
97 void WebRTCEventLogHost::PeerConnectionRemoved(int peer_connection_local_id) { | |
98 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
99 const auto found = std::find(active_peer_connection_local_id_.begin(), | |
100 active_peer_connection_local_id_.end(), | |
101 peer_connection_local_id) != | |
102 active_peer_connection_local_id_.end(); | |
103 DCHECK(found != active_peer_connection_local_id_.end()); | |
104 active_peer_connection_local_id_.erase(found); | |
Henrik Grunell
2016/06/01 08:31:10
This means that the log has stopped for that PC, r
Ivo-OOO until feb 6
2016/06/02 14:43:38
I guess that depends on what kind of behavior we w
Henrik Grunell
2016/06/03 07:40:52
That's a good point. OK, let's keep it this way. P
Ivo-OOO until feb 6
2016/06/03 16:07:01
Done.
| |
105 } | |
106 | |
107 bool WebRTCEventLogHost::StartWebRTCEventLog(const base::FilePath& file_path) { | |
108 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
109 if (rtc_event_logging_enabled_) | |
110 return false; | |
111 RenderProcessHost* host = RenderProcessHost::FromID(render_process_id_); | |
112 if (host) { | |
113 rtc_event_logging_enabled_ = true; | |
114 base_file_path_ = file_path; | |
115 for (int local_id : active_peer_connection_local_id_) { | |
116 if (number_active_log_files_ < kMaxNumberLogFiles) { | |
117 BrowserThread::PostTaskAndReplyWithResult( | |
Henrik Grunell
2016/06/01 08:31:10
Nit: to make it more readable and decrease redunda
Ivo-OOO until feb 6
2016/06/02 14:43:38
Done.
| |
118 BrowserThread::FILE, FROM_HERE, | |
119 base::Bind(&CreateFileForProcess, file_path, render_process_id_, | |
120 local_id), | |
121 base::Bind(&SendEventLogFileToRenderer, host, local_id)); | |
122 ++number_active_log_files_; | |
123 } | |
124 } | |
125 } | |
126 return true; | |
127 } | |
128 | |
129 bool WebRTCEventLogHost::StopWebRTCEventLog() { | |
130 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
131 if (!rtc_event_logging_enabled_) | |
132 return false; | |
133 rtc_event_logging_enabled_ = false; | |
134 number_active_log_files_ = 0; | |
135 RenderProcessHost* host = RenderProcessHost::FromID(render_process_id_); | |
136 if (host) { | |
137 for (int local_id : active_peer_connection_local_id_) | |
138 host->Send(new PeerConnectionTracker_StopEventLog(local_id)); | |
139 } | |
140 return true; | |
141 } | |
142 | |
143 } // namespace content | |
OLD | NEW |