#ifdef UNSAFE_BUFFERS_BUILD
#pragma allow_unsafe_buffers
#endif
#include "chrome/browser/media/webrtc/webrtc_event_log_manager_common.h"
#include <limits>
#include <string_view>
#include "base/files/file_util.h"
#include "base/logging.h"
#include "base/memory/scoped_refptr.h"
#include "base/metrics/histogram_functions.h"
#include "base/ranges/algorithm.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_util.h"
#include "base/strings/stringprintf.h"
#include "base/task/sequenced_task_runner.h"
#include "base/unguessable_token.h"
#include "build/chromeos_buildflags.h"
#include "chrome/browser/policy/profile_policy_connector.h"
#include "chrome/browser/profiles/profile.h"
#include "components/policy/core/common/policy_service.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/render_process_host.h"
#include "third_party/abseil-cpp/absl/strings/ascii.h"
#include "third_party/zlib/zlib.h"
#if BUILDFLAG(IS_CHROMEOS_ASH)
#include "chrome/browser/ash/profiles/profile_helper.h"
#include "components/user_manager/user.h"
#include "components/user_manager/user_type.h"
#endif
namespace webrtc_event_logging {
BrowserContextId;
const size_t kWebRtcEventLogManagerUnlimitedFileSize = …;
const size_t kWebRtcEventLogIdLength = …;
const size_t kMinWebRtcEventLogWebAppId = …;
const size_t kMaxWebRtcEventLogWebAppId = …;
const size_t kInvalidWebRtcEventLogWebAppId = …;
static_assert …;
const char kRemoteBoundWebRtcEventLogFileNamePrefix[] = …;
const char kStartRemoteLoggingFailureAlreadyLogging[] = …;
const char kStartRemoteLoggingFailureDeadRenderProcessHost[] = …;
const char kStartRemoteLoggingFailureFeatureDisabled[] = …;
const char kStartRemoteLoggingFailureFileCreationError[] = …;
const char kStartRemoteLoggingFailureFilePathUsedHistory[] = …;
const char kStartRemoteLoggingFailureFilePathUsedLog[] = …;
const char kStartRemoteLoggingFailureIllegalWebAppId[] = …;
const char kStartRemoteLoggingFailureLoggingDisabledBrowserContext[] = …;
const char kStartRemoteLoggingFailureMaxSizeTooLarge[] = …;
const char kStartRemoteLoggingFailureMaxSizeTooSmall[] = …;
const char kStartRemoteLoggingFailureNoAdditionalActiveLogsAllowed[] = …;
const char kStartRemoteLoggingFailureOutputPeriodMsTooLarge[] = …;
const char kStartRemoteLoggingFailureUnknownOrInactivePeerConnection[] = …;
const char kStartRemoteLoggingFailureUnlimitedSizeDisallowed[] = …;
const BrowserContextId kNullBrowserContextId = …;
void UmaRecordWebRtcEventLoggingApi(WebRtcEventLoggingApiUma result) { … }
void UmaRecordWebRtcEventLoggingUpload(WebRtcEventLoggingUploadUma result) { … }
void UmaRecordWebRtcEventLoggingNetErrorType(int net_error) { … }
namespace {
constexpr int kDefaultMemLevel = …;
constexpr size_t kGzipHeaderBytes = …;
constexpr size_t kGzipFooterBytes = …;
constexpr size_t kWebAppIdLength = …;
class Budget { … };
class BaseLogFileWriter : public LogFileWriter { … };
BaseLogFileWriter::BaseLogFileWriter(const base::FilePath& path,
std::optional<size_t> max_file_size_bytes)
: … { … }
BaseLogFileWriter::~BaseLogFileWriter() { … }
bool BaseLogFileWriter::Init() { … }
const base::FilePath& BaseLogFileWriter::path() const { … }
bool BaseLogFileWriter::MaxSizeReached() const { … }
bool BaseLogFileWriter::Write(const std::string& input) { … }
bool BaseLogFileWriter::Close() { … }
void BaseLogFileWriter::Delete() { … }
void BaseLogFileWriter::SetState(State state) { … }
bool BaseLogFileWriter::WithinBudget(size_t bytes) const { … }
bool BaseLogFileWriter::WriteInternal(const std::string& input, bool metadata) { … }
bool BaseLogFileWriter::Finalize() { … }
class GzippedLogFileWriter : public BaseLogFileWriter { … };
GzippedLogFileWriter::GzippedLogFileWriter(
const base::FilePath& path,
std::optional<size_t> max_file_size_bytes,
std::unique_ptr<LogCompressor> compressor)
: … { … }
bool GzippedLogFileWriter::Init() { … }
bool GzippedLogFileWriter::MaxSizeReached() const { … }
bool GzippedLogFileWriter::Write(const std::string& input) { … }
bool GzippedLogFileWriter::Finalize() { … }
class GzipLogCompressor : public LogCompressor { … };
GzipLogCompressor::GzipLogCompressor(
std::optional<size_t> max_size_bytes,
std::unique_ptr<CompressedSizeEstimator> compressed_size_estimator)
: … { … }
GzipLogCompressor::~GzipLogCompressor() { … }
void GzipLogCompressor::CreateHeader(std::string* output) { … }
LogCompressor::Result GzipLogCompressor::Compress(const std::string& input,
std::string* output) { … }
bool GzipLogCompressor::CreateFooter(std::string* output) { … }
std::optional<size_t> GzipLogCompressor::SizeAfterOverheadReservation(
std::optional<size_t> max_size_bytes) { … }
LogCompressor::Result GzipLogCompressor::CompressInternal(
const std::string& input,
std::string* output,
bool budgeted,
bool last) { … }
bool GzipLogCompressor::Deflate(int flush, std::string* output) { … }
size_t ExtractWebAppId(std::string_view str) { … }
}
const size_t kGzipOverheadBytes = …;
const base::FilePath::CharType kWebRtcEventLogUncompressedExtension[] = …);
const base::FilePath::CharType kWebRtcEventLogGzippedExtension[] = …);
const base::FilePath::CharType kWebRtcEventLogHistoryExtension[] = …);
size_t BaseLogFileWriterFactory::MinFileSizeBytes() const { … }
base::FilePath::StringPieceType BaseLogFileWriterFactory::Extension() const { … }
std::unique_ptr<LogFileWriter> BaseLogFileWriterFactory::Create(
const base::FilePath& path,
std::optional<size_t> max_file_size_bytes) const { … }
std::unique_ptr<CompressedSizeEstimator>
DefaultGzippedSizeEstimator::Factory::Create() const { … }
size_t DefaultGzippedSizeEstimator::EstimateCompressedSize(
const std::string& input) const { … }
GzipLogCompressorFactory::GzipLogCompressorFactory(
std::unique_ptr<CompressedSizeEstimator::Factory> estimator_factory)
: … { … }
GzipLogCompressorFactory::~GzipLogCompressorFactory() = default;
size_t GzipLogCompressorFactory::MinSizeBytes() const { … }
std::unique_ptr<LogCompressor> GzipLogCompressorFactory::Create(
std::optional<size_t> max_size_bytes) const { … }
GzippedLogFileWriterFactory::GzippedLogFileWriterFactory(
std::unique_ptr<GzipLogCompressorFactory> gzip_compressor_factory)
: … { … }
GzippedLogFileWriterFactory::~GzippedLogFileWriterFactory() = default;
size_t GzippedLogFileWriterFactory::MinFileSizeBytes() const { … }
base::FilePath::StringPieceType GzippedLogFileWriterFactory::Extension() const { … }
std::unique_ptr<LogFileWriter> GzippedLogFileWriterFactory::Create(
const base::FilePath& path,
std::optional<size_t> max_file_size_bytes) const { … }
std::string CreateWebRtcEventLogId() { … }
BrowserContextId GetBrowserContextId(
const content::BrowserContext* browser_context) { … }
BrowserContextId GetBrowserContextId(int render_process_id) { … }
base::FilePath GetRemoteBoundWebRtcEventLogsDir(
const base::FilePath& browser_context_dir) { … }
base::FilePath WebRtcEventLogPath(
const base::FilePath& remote_logs_dir,
const std::string& log_id,
size_t web_app_id,
const base::FilePath::StringPieceType& extension) { … }
bool IsValidRemoteBoundLogFilename(const std::string& filename) { … }
bool IsValidRemoteBoundLogFilePath(const base::FilePath& path) { … }
base::FilePath GetWebRtcEventLogHistoryFilePath(const base::FilePath& path) { … }
std::string ExtractRemoteBoundWebRtcEventLogLocalIdFromPath(
const base::FilePath& path) { … }
size_t ExtractRemoteBoundWebRtcEventLogWebAppIdFromPath(
const base::FilePath& path) { … }
bool DoesProfileDefaultToLoggingEnabled(const Profile* const profile) { … }
}