#include "net/log/file_net_log_observer.h"
#include <algorithm>
#include <memory>
#include <string>
#include <string_view>
#include <utility>
#include "base/containers/queue.h"
#include "base/containers/span.h"
#include "base/files/file.h"
#include "base/files/file_util.h"
#include "base/functional/bind.h"
#include "base/json/json_writer.h"
#include "base/logging.h"
#include "base/memory/ptr_util.h"
#include "base/numerics/clamped_math.h"
#include "base/strings/string_number_conversions.h"
#include "base/synchronization/lock.h"
#include "base/task/sequenced_task_runner.h"
#include "base/task/thread_pool.h"
#include "base/values.h"
#include "net/log/net_log_capture_mode.h"
#include "net/log/net_log_entry.h"
#include "net/log/net_log_util.h"
#include "net/url_request/url_request_context.h"
namespace {
const int kNumWriteQueueEvents = …;
const int kDefaultNumFiles = …;
scoped_refptr<base::SequencedTaskRunner> CreateFileTaskRunner() { … }
void TruncateFile(base::File* file) { … }
base::File OpenFileForWrite(const base::FilePath& path) { … }
size_t WriteToFile(base::File* file,
std::string_view data1,
std::string_view data2 = std::string_view(),
std::string_view data3 = std::string_view()) { … }
void AppendToFileThenDelete(const base::FilePath& source_path,
base::File* destination_file,
char* read_buffer,
size_t read_buffer_size) { … }
base::FilePath SiblingInprogressDirectory(const base::FilePath& log_path) { … }
}
namespace net {
EventQueue;
class FileNetLogObserver::WriteQueue
: public base::RefCountedThreadSafe<WriteQueue> { … };
class FileNetLogObserver::FileWriter { … };
std::unique_ptr<FileNetLogObserver> FileNetLogObserver::CreateBounded(
const base::FilePath& log_path,
uint64_t max_total_size,
NetLogCaptureMode capture_mode,
std::unique_ptr<base::Value::Dict> constants) { … }
std::unique_ptr<FileNetLogObserver> FileNetLogObserver::CreateUnbounded(
const base::FilePath& log_path,
NetLogCaptureMode capture_mode,
std::unique_ptr<base::Value::Dict> constants) { … }
std::unique_ptr<FileNetLogObserver>
FileNetLogObserver::CreateBoundedPreExisting(
const base::FilePath& inprogress_dir_path,
base::File output_file,
uint64_t max_total_size,
NetLogCaptureMode capture_mode,
std::unique_ptr<base::Value::Dict> constants) { … }
std::unique_ptr<FileNetLogObserver>
FileNetLogObserver::CreateUnboundedPreExisting(
base::File output_file,
NetLogCaptureMode capture_mode,
std::unique_ptr<base::Value::Dict> constants) { … }
std::unique_ptr<FileNetLogObserver> FileNetLogObserver::CreateBoundedFile(
base::File output_file,
uint64_t max_total_size,
NetLogCaptureMode capture_mode,
std::unique_ptr<base::Value::Dict> constants) { … }
FileNetLogObserver::~FileNetLogObserver() { … }
void FileNetLogObserver::StartObserving(NetLog* net_log) { … }
void FileNetLogObserver::StopObserving(std::unique_ptr<base::Value> polled_data,
base::OnceClosure optional_callback) { … }
void FileNetLogObserver::OnAddEntry(const NetLogEntry& entry) { … }
std::unique_ptr<FileNetLogObserver> FileNetLogObserver::CreateBoundedForTests(
const base::FilePath& log_path,
uint64_t max_total_size,
size_t total_num_event_files,
NetLogCaptureMode capture_mode,
std::unique_ptr<base::Value::Dict> constants) { … }
std::unique_ptr<FileNetLogObserver> FileNetLogObserver::CreateInternal(
const base::FilePath& log_path,
const base::FilePath& inprogress_dir_path,
std::optional<base::File> pre_existing_log_file,
uint64_t max_total_size,
size_t total_num_event_files,
NetLogCaptureMode capture_mode,
std::unique_ptr<base::Value::Dict> constants) { … }
FileNetLogObserver::FileNetLogObserver(
scoped_refptr<base::SequencedTaskRunner> file_task_runner,
std::unique_ptr<FileWriter> file_writer,
scoped_refptr<WriteQueue> write_queue,
NetLogCaptureMode capture_mode,
std::unique_ptr<base::Value::Dict> constants)
: … { … }
std::string FileNetLogObserver::CaptureModeToString(NetLogCaptureMode mode) { … }
FileNetLogObserver::WriteQueue::WriteQueue(uint64_t memory_max)
: … { … }
size_t FileNetLogObserver::WriteQueue::AddEntryToQueue(
std::unique_ptr<std::string> event) { … }
void FileNetLogObserver::WriteQueue::SwapQueue(EventQueue* local_queue) { … }
FileNetLogObserver::WriteQueue::~WriteQueue() = default;
FileNetLogObserver::FileWriter::FileWriter(
const base::FilePath& log_path,
const base::FilePath& inprogress_dir_path,
std::optional<base::File> pre_existing_log_file,
uint64_t max_event_file_size,
size_t total_num_event_files,
scoped_refptr<base::SequencedTaskRunner> task_runner)
: … { … }
FileNetLogObserver::FileWriter::~FileWriter() = default;
void FileNetLogObserver::FileWriter::Initialize(
std::unique_ptr<base::Value::Dict> constants_value) { … }
void FileNetLogObserver::FileWriter::Stop(
std::unique_ptr<base::Value> polled_data) { … }
void FileNetLogObserver::FileWriter::Flush(
scoped_refptr<FileNetLogObserver::WriteQueue> write_queue) { … }
void FileNetLogObserver::FileWriter::DeleteAllFiles() { … }
void FileNetLogObserver::FileWriter::FlushThenStop(
scoped_refptr<FileNetLogObserver::WriteQueue> write_queue,
std::unique_ptr<base::Value> polled_data) { … }
bool FileNetLogObserver::FileWriter::IsUnbounded() const { … }
bool FileNetLogObserver::FileWriter::IsBounded() const { … }
bool FileNetLogObserver::FileWriter::IsBoundedAndStitchable() const { … }
void FileNetLogObserver::FileWriter::IncrementCurrentEventFile() { … }
base::FilePath FileNetLogObserver::FileWriter::GetEventFilePath(
size_t index) const { … }
base::FilePath FileNetLogObserver::FileWriter::GetConstantsFilePath() const { … }
base::FilePath FileNetLogObserver::FileWriter::GetClosingFilePath() const { … }
size_t FileNetLogObserver::FileWriter::FileNumberToIndex(
size_t file_number) const { … }
void FileNetLogObserver::FileWriter::WriteConstantsToFile(
std::unique_ptr<base::Value::Dict> constants_value,
base::File* file) { … }
void FileNetLogObserver::FileWriter::WritePolledDataToFile(
std::unique_ptr<base::Value> polled_data,
base::File* file) { … }
void FileNetLogObserver::FileWriter::RewindIfWroteEventBytes(
base::File* file) const { … }
void FileNetLogObserver::FileWriter::StitchFinalLogFile() { … }
void FileNetLogObserver::FileWriter::CreateInprogressDirectory() { … }
std::string SerializeNetLogValueToJson(const base::ValueView& value) { … }
}