#ifdef UNSAFE_BUFFERS_BUILD
#pragma allow_unsafe_buffers
#endif
#include "components/sessions/core/command_storage_backend.h"
#include <stdint.h>
#include <algorithm>
#include <limits>
#include <string_view>
#include <utility>
#include "base/feature_list.h"
#include "base/features.h"
#include "base/files/file.h"
#include "base/files/file_enumerator.h"
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/logging.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_split.h"
#include "base/strings/string_util.h"
#include "base/task/sequenced_task_runner.h"
#include "base/task/single_thread_task_runner.h"
#include "base/time/default_clock.h"
#include "build/build_config.h"
#include "components/sessions/core/session_constants.h"
#include "components/sessions/core/session_service_commands.h"
#include "crypto/aead.h"
namespace sessions {
SessionType;
namespace {
constexpr int32_t kFileVersion1 = …;
constexpr int32_t kEncryptedFileVersion = …;
constexpr int32_t kFileVersionWithMarker = …;
constexpr int32_t kEncryptedFileVersionWithMarker = …;
constexpr int32_t kFileSignature = …;
constexpr int kNonceLength = …;
BASE_FEATURE(…);
struct FileHeader { … };
const SessionCommand::id_type kInitialStateMarkerCommandId = …;
class SessionFileReader { … };
CommandStorageBackend::ReadCommandsResult SessionFileReader::Read() { … }
bool SessionFileReader::ReadHeader() { … }
bool SessionFileReader::ReadToMarker() { … }
SessionFileReader::ReadResult SessionFileReader::ReadCommand() { … }
std::unique_ptr<sessions::SessionCommand>
SessionFileReader::CreateCommandFromEncrypted(const char* data,
size_type length) { … }
std::unique_ptr<sessions::SessionCommand> SessionFileReader::CreateCommand(
const char* data,
size_type length) { … }
bool SessionFileReader::FillBuffer() { … }
base::FilePath::StringType TimestampToString(const base::Time time) { … }
base::FilePath GetSessionDirName(CommandStorageManager::SessionType type,
const base::FilePath& supplied_path) { … }
base::FilePath::StringType GetSessionBaseName(
CommandStorageManager::SessionType type,
const base::FilePath& supplied_path) { … }
base::FilePath::StringType GetSessionFilename(
CommandStorageManager::SessionType type,
const base::FilePath& supplied_path,
const base::FilePath::StringType& timestamp_str) { … }
base::FilePath GetLegacySessionPath(CommandStorageManager::SessionType type,
const base::FilePath& base_path,
bool current) { … }
}
CommandStorageBackend::ReadCommandsResult::ReadCommandsResult() = default;
CommandStorageBackend::ReadCommandsResult::ReadCommandsResult(
CommandStorageBackend::ReadCommandsResult&& other) = default;
CommandStorageBackend::ReadCommandsResult&
CommandStorageBackend::ReadCommandsResult::operator=(
CommandStorageBackend::ReadCommandsResult&& other) = default;
CommandStorageBackend::ReadCommandsResult::~ReadCommandsResult() = default;
CommandStorageBackend::OpenFile::OpenFile() = default;
CommandStorageBackend::OpenFile::~OpenFile() = default;
const int CommandStorageBackend::kFileReadBufferSize = …;
const SessionCommand::size_type
CommandStorageBackend::kEncryptionOverheadInBytes = …;
CommandStorageBackend::CommandStorageBackend(
scoped_refptr<base::SequencedTaskRunner> owning_task_runner,
const base::FilePath& path,
SessionType type,
const std::vector<uint8_t>& decryption_key,
base::Clock* clock)
: … { … }
bool CommandStorageBackend::IsValidFile(const base::FilePath& path) { … }
void CommandStorageBackend::AppendCommands(
std::vector<std::unique_ptr<SessionCommand>> commands,
bool truncate,
base::OnceClosure error_callback,
const std::vector<uint8_t>& crypto_key) { … }
bool CommandStorageBackend::TimestampFromPath(const base::FilePath& path,
base::Time& timestamp_result) { … }
std::set<base::FilePath> CommandStorageBackend::GetSessionFilePaths(
const base::FilePath& path,
CommandStorageManager::SessionType type) { … }
CommandStorageBackend::ReadCommandsResult
CommandStorageBackend::ReadLastSessionCommands() { … }
void CommandStorageBackend::DeleteLastSession() { … }
void CommandStorageBackend::MoveCurrentSessionToLastSession() { … }
void CommandStorageBackend::ForceAppendCommandsToFailForTesting() { … }
bool CommandStorageBackend::AppendCommandsToFile(
base::File* file,
const std::vector<std::unique_ptr<sessions::SessionCommand>>& commands) { … }
CommandStorageBackend::~CommandStorageBackend() = default;
void CommandStorageBackend::InitIfNecessary() { … }
base::FilePath CommandStorageBackend::FilePathFromTime(
const SessionType type,
const base::FilePath& path,
base::Time time) { … }
CommandStorageBackend::ReadCommandsResult
CommandStorageBackend::ReadCommandsFromFile(
const base::FilePath& path,
const std::vector<uint8_t>& crypto_key) { … }
void CommandStorageBackend::CloseFile() { … }
void CommandStorageBackend::TruncateOrOpenFile() { … }
std::unique_ptr<base::File> CommandStorageBackend::OpenAndWriteHeader(
const base::FilePath& path) const { … }
bool CommandStorageBackend::AppendCommandToFile(
base::File* file,
const sessions::SessionCommand& command) { … }
bool CommandStorageBackend::AppendEncryptedCommandToFile(
base::File* file,
const sessions::SessionCommand& command) { … }
std::optional<CommandStorageBackend::SessionInfo>
CommandStorageBackend::FindLastSessionFile() const { … }
void CommandStorageBackend::DeleteLastSessionFiles() const { … }
std::vector<CommandStorageBackend::SessionInfo>
CommandStorageBackend::GetSessionFilesSortedByReverseTimestamp(
const base::FilePath& path,
CommandStorageManager::SessionType type) { … }
bool CommandStorageBackend::CanUseFileForLastSession(
const base::FilePath& path) const { … }
}