#ifndef CHROME_UTILITY_SAFE_BROWSING_ARCHIVE_ANALYZER_H_
#define CHROME_UTILITY_SAFE_BROWSING_ARCHIVE_ANALYZER_H_
#include <optional>
#include "base/files/file.h"
#include "base/functional/callback.h"
#include "base/memory/raw_ptr.h"
#include "chrome/common/safe_browsing/archive_analyzer_results.h"
#include "components/safe_browsing/content/common/proto/download_file_types.pb.h"
namespace safe_browsing {
using FinishedAnalysisCallback = base::OnceCallback<void()>;
using GetTempFileCallback =
base::RepeatingCallback<void(base::OnceCallback<void(base::File)>)>;
class ArchiveAnalyzer {
public:
static std::unique_ptr<ArchiveAnalyzer> CreateForArchiveType(
DownloadFileType_InspectionType file_type);
ArchiveAnalyzer();
virtual ~ArchiveAnalyzer();
ArchiveAnalyzer(const ArchiveAnalyzer&) = delete;
ArchiveAnalyzer& operator=(const ArchiveAnalyzer&) = delete;
void Analyze(base::File archive_file,
base::FilePath relative_path,
const std::optional<std::string>& password,
FinishedAnalysisCallback finished_analysis_callback,
GetTempFileCallback get_temp_file_callback,
ArchiveAnalyzerResults* results);
void SetResultsForTesting(ArchiveAnalyzerResults* results);
void SetFinishedCallbackForTesting(FinishedAnalysisCallback callback);
protected:
virtual void Init() = 0;
virtual bool ResumeExtraction() = 0;
virtual base::WeakPtr<ArchiveAnalyzer> GetWeakPtr() = 0;
base::File& GetArchiveFile();
const base::FilePath& GetRootPath() const;
ArchiveAnalyzerResults* results() { return results_; }
const std::optional<std::string>& password() const { return password_; }
void GetTempFile(base::OnceCallback<void(base::File)> callback);
bool UpdateResultsForEntry(base::File entry,
base::FilePath path,
int file_length,
bool is_encrypted,
bool is_directory,
bool contents_valid);
void InitComplete(ArchiveAnalysisResult result);
void NestedAnalysisFinished(base::File entry,
base::FilePath path,
int entry_size);
bool IsTopLevelArchive() const;
private:
base::FilePath root_path_;
base::File archive_file_;
raw_ptr<ArchiveAnalyzerResults> results_;
FinishedAnalysisCallback finished_analysis_callback_;
GetTempFileCallback get_temp_file_callback_;
std::optional<std::string> password_;
std::unique_ptr<safe_browsing::ArchiveAnalyzer> nested_analyzer_;
};
}
#endif