1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343
|
// Copyright 2017 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/media/webrtc/webrtc_event_log_manager_local.h"
#include "base/files/file_util.h"
#include "base/i18n/time_formatting.h"
#include "base/logging.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_util.h"
#include "base/strings/stringprintf.h"
#include "base/time/time.h"
#include "build/build_config.h"
#include "content/public/browser/browser_thread.h"
#if BUILDFLAG(IS_WIN)
#define NumberToStringType base::NumberToString16
#else
#define NumberToStringType base::NumberToString
#endif
namespace webrtc_event_logging {
#if BUILDFLAG(IS_ANDROID)
const size_t kDefaultMaxLocalEventLogFileSizeBytes = 10'000'000;
const size_t kMaxNumberLocalWebRtcEventLogFiles = 3;
const size_t kDefaultMaxLocalDataChannelFileSizeBytes = 10'000'000;
const size_t kMaxNumberLocalWebRtcDataChannelLogFiles = 3;
#else
const size_t kDefaultMaxLocalEventLogFileSizeBytes = 60'000'000;
const size_t kMaxNumberLocalWebRtcEventLogFiles = 5;
const size_t kDefaultMaxLocalDataChannelFileSizeBytes = 100'000'000;
const size_t kMaxNumberLocalWebRtcDataChannelLogFiles = 5;
#endif
struct WebRtcLocalEventLogManager::LogFiles {
std::unique_ptr<LogFileWriter> event_log;
std::unique_ptr<LogFileWriter> data_channel_log;
};
WebRtcLocalEventLogManager::WebRtcLocalEventLogManager(
WebRtcLocalEventLogsObserver* observer)
: observer_(observer), clock_for_testing_(nullptr) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
DETACH_FROM_SEQUENCE(io_task_sequence_checker_);
}
WebRtcLocalEventLogManager::~WebRtcLocalEventLogManager() {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
}
bool WebRtcLocalEventLogManager::OnPeerConnectionAdded(
const PeerConnectionKey& key) {
DCHECK_CALLED_ON_VALID_SEQUENCE(io_task_sequence_checker_);
const auto insertion_result = log_files_.try_emplace(key);
if (!insertion_result.second) {
return false; // Attempt to re-add the PeerConnection.
}
MaybeStartEventLogFile(insertion_result.first);
MaybeStartDataChannelLogFile(insertion_result.first);
return true;
}
bool WebRtcLocalEventLogManager::OnPeerConnectionRemoved(
const PeerConnectionKey& key) {
DCHECK_CALLED_ON_VALID_SEQUENCE(io_task_sequence_checker_);
auto it = log_files_.find(key);
if (it == log_files_.end()) {
return false;
}
StopEventLogFileIfStarted(it);
StopDataChannelLogFileIfStarted(it);
log_files_.erase(it);
return true;
}
bool WebRtcLocalEventLogManager::EnableEventLogging(
const base::FilePath& base_path,
size_t max_file_size_bytes) {
DCHECK_CALLED_ON_VALID_SEQUENCE(io_task_sequence_checker_);
if (event_log_state_.has_value()) {
return false;
}
std::optional<size_t> max_size_bytes =
(max_file_size_bytes == kWebRtcEventLogManagerUnlimitedFileSize)
? std::optional<size_t>()
: std::optional<size_t>(max_file_size_bytes);
event_log_state_.emplace(
base_path, max_size_bytes,
/*max_logs_active=*/kMaxNumberLocalWebRtcEventLogFiles,
/*max_logs_created=*/std::nullopt);
for (auto it = log_files_.begin(); it != log_files_.end(); ++it) {
MaybeStartEventLogFile(it);
}
return true;
}
bool WebRtcLocalEventLogManager::DisableEventLogging() {
DCHECK_CALLED_ON_VALID_SEQUENCE(io_task_sequence_checker_);
if (!event_log_state_.has_value()) {
return false;
}
for (auto it = log_files_.begin(); it != log_files_.end(); ++it) {
StopEventLogFileIfStarted(it);
}
CHECK_EQ(event_log_state_->logs_active, 0U);
event_log_state_.reset();
return true;
}
bool WebRtcLocalEventLogManager::EventLogWrite(const PeerConnectionKey& key,
const std::string& message) {
DCHECK_CALLED_ON_VALID_SEQUENCE(io_task_sequence_checker_);
auto it = log_files_.find(key);
if (it == log_files_.end() || it->second.event_log == nullptr) {
return false;
}
const bool write_successful = it->second.event_log->Write(message);
if (!write_successful || it->second.event_log->MaxSizeReached()) {
StopEventLogFileIfStarted(it);
}
return write_successful;
}
bool WebRtcLocalEventLogManager::EnableDataChannelLogging(
const base::FilePath& base_path,
size_t max_file_size_bytes) {
DCHECK_CALLED_ON_VALID_SEQUENCE(io_task_sequence_checker_);
if (data_channel_log_state_.has_value()) {
return false;
}
std::optional<size_t> max_size_bytes =
(max_file_size_bytes == kWebRtcEventLogManagerUnlimitedFileSize)
? std::optional<size_t>()
: std::optional<size_t>(max_file_size_bytes);
data_channel_log_state_.emplace(
base_path, max_size_bytes,
/*max_logs_active=*/std::nullopt,
/*max_logs_created=*/kMaxNumberLocalWebRtcDataChannelLogFiles);
for (auto it = log_files_.begin(); it != log_files_.end(); ++it) {
MaybeStartDataChannelLogFile(it);
}
return true;
}
bool WebRtcLocalEventLogManager::DisableDataChannelLogging() {
DCHECK_CALLED_ON_VALID_SEQUENCE(io_task_sequence_checker_);
if (!data_channel_log_state_.has_value()) {
return false;
}
for (auto it = log_files_.begin(); it != log_files_.end(); ++it) {
StopDataChannelLogFileIfStarted(it);
}
CHECK_EQ(data_channel_log_state_->logs_active, 0U);
data_channel_log_state_.reset();
return true;
}
bool WebRtcLocalEventLogManager::DataChannelLogWrite(
const PeerConnectionKey& key,
const std::string& message) {
DCHECK_CALLED_ON_VALID_SEQUENCE(io_task_sequence_checker_);
auto it = log_files_.find(key);
if (it == log_files_.end() || it->second.data_channel_log == nullptr) {
return false;
}
const bool write_successful = it->second.data_channel_log->Write(message);
if (!write_successful || it->second.data_channel_log->MaxSizeReached()) {
StopDataChannelLogFileIfStarted(it);
}
return write_successful;
}
void WebRtcLocalEventLogManager::RenderProcessHostExitedDestroyed(
int render_process_id) {
DCHECK_CALLED_ON_VALID_SEQUENCE(io_task_sequence_checker_);
// Remove all of the peer connections associated with this render process.
for (auto it = log_files_.begin(); it != log_files_.end();) {
if (it->first.render_process_id == render_process_id) {
StopEventLogFileIfStarted(it);
StopDataChannelLogFileIfStarted(it);
it = log_files_.erase(it);
} else {
++it;
}
}
}
void WebRtcLocalEventLogManager::SetClockForTesting(base::Clock* clock) {
DCHECK_CALLED_ON_VALID_SEQUENCE(io_task_sequence_checker_);
clock_for_testing_ = clock;
}
void WebRtcLocalEventLogManager::MaybeStartEventLogFile(
LogFilesMap::iterator log_it) {
if (!event_log_state_.has_value()) {
return;
}
LogTypeState& state = *event_log_state_;
if (state.logs_active >= state.max_logs_active ||
log_it->second.event_log != nullptr) {
return;
}
std::unique_ptr<LogFileWriter> log_file =
CreateLogFile(log_it->first, state.base_path, state.max_size_bytes);
if (!log_file) {
return;
}
log_it->second.event_log = std::move(log_file);
++state.logs_active;
if (observer_) {
observer_->OnLocalEventLogStarted(log_it->first,
log_it->second.event_log->path());
}
}
void WebRtcLocalEventLogManager::StopEventLogFileIfStarted(
LogFilesMap::iterator log_it) {
if (!log_it->second.event_log) {
return;
}
log_it->second.event_log.reset();
--event_log_state_->logs_active;
if (observer_) {
observer_->OnLocalEventLogStopped(log_it->first);
}
}
void WebRtcLocalEventLogManager::MaybeStartDataChannelLogFile(
LogFilesMap::iterator log_it) {
if (!data_channel_log_state_.has_value()) {
return;
}
LogTypeState& state = *data_channel_log_state_;
if (state.logs_created >= state.max_logs_created ||
log_it->second.data_channel_log != nullptr) {
return;
}
std::unique_ptr<LogFileWriter> log_file =
CreateLogFile(log_it->first, state.base_path, state.max_size_bytes);
if (!log_file) {
return;
}
log_it->second.data_channel_log = std::move(log_file);
++state.logs_created;
if (observer_) {
observer_->OnLocalDataChannelLogStarted(
log_it->first, log_it->second.data_channel_log->path());
}
}
void WebRtcLocalEventLogManager::StopDataChannelLogFileIfStarted(
LogFilesMap::iterator log_it) {
if (!log_it->second.data_channel_log) {
return;
}
log_it->second.data_channel_log.reset();
if (observer_) {
observer_->OnLocalDataChannelLogStopped(log_it->first);
}
}
std::unique_ptr<LogFileWriter> WebRtcLocalEventLogManager::CreateLogFile(
const PeerConnectionKey& key,
const base::FilePath& base_path,
std::optional<size_t> max_log_size_bytes) {
DCHECK_CALLED_ON_VALID_SEQUENCE(io_task_sequence_checker_);
// Add some information to the name given by the caller.
base::FilePath file_path = GetFilePath(base_path, key);
CHECK(!file_path.empty()) << "Couldn't set path for local log file.";
// In the unlikely case that this filename is already taken, find a unique
// number to append to the filename, if possible.
file_path = base::GetUniquePath(file_path);
if (file_path.empty()) {
return nullptr; // No available file path was found.
}
auto log_file =
log_file_writer_factory_.Create(file_path, max_log_size_bytes);
if (!log_file) {
LOG(WARNING) << "Couldn't open " << file_path << " for logging.";
return nullptr;
}
return log_file;
}
base::FilePath WebRtcLocalEventLogManager::GetFilePath(
const base::FilePath& base_path,
const PeerConnectionKey& key) const {
DCHECK_CALLED_ON_VALID_SEQUENCE(io_task_sequence_checker_);
// [user_defined]_[date]_[time]_[render_process_id]_[lid].[extension]
const base::Time now =
clock_for_testing_ ? clock_for_testing_->Now() : base::Time::Now();
base::Time::Exploded exploded;
now.LocalExplode(&exploded);
const std::string timestamp =
base::UnlocalizedTimeFormatWithPattern(now, "yyyyMMdd_HHmm");
return base_path.InsertBeforeExtension(FILE_PATH_LITERAL("_"))
.AddExtension(log_file_writer_factory_.Extension())
.InsertBeforeExtensionASCII(base::StringPrintf(
"%s_%d_%d", timestamp.c_str(), key.render_process_id, key.lid));
}
} // namespace webrtc_event_logging
|