1 // Copyright 2017 PDFium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef CORE_FPDFAPI_PARSER_CPDF_READ_VALIDATOR_H_ 6 #define CORE_FPDFAPI_PARSER_CPDF_READ_VALIDATOR_H_ 7 8 #include "core/fpdfapi/parser/cpdf_data_avail.h" 9 #include "core/fxcrt/fx_stream.h" 10 11 class CPDF_ReadValidator : public IFX_SeekableReadStream { 12 public: 13 template <typename T, typename... Args> 14 friend RetainPtr<T> pdfium::MakeRetain(Args&&... args); 15 16 class Session { 17 public: 18 explicit Session(CPDF_ReadValidator* validator); 19 ~Session(); 20 21 private: 22 UnownedPtr<CPDF_ReadValidator> validator_; 23 bool saved_read_error_; 24 bool saved_has_unavailable_data_; 25 }; 26 27 void SetDownloadHints(CPDF_DataAvail::DownloadHints* hints) { 28 hints_ = hints; 29 } 30 31 bool read_error() const { return read_error_; } 32 bool has_unavailable_data() const { return has_unavailable_data_; } 33 34 bool has_read_problems() const { 35 return read_error() || has_unavailable_data(); 36 } 37 38 void ResetErrors(); 39 40 bool IsWholeFileAvailable(); 41 42 bool CheckDataRangeAndRequestIfUnavailable(FX_FILESIZE offset, size_t size); 43 bool CheckWholeFileAndRequestIfUnavailable(); 44 45 // IFX_SeekableReadStream overrides: 46 bool ReadBlock(void* buffer, FX_FILESIZE offset, size_t size) override; 47 FX_FILESIZE GetSize() override; 48 49 protected: 50 CPDF_ReadValidator(const RetainPtr<IFX_SeekableReadStream>& file_read, 51 CPDF_DataAvail::FileAvail* file_avail); 52 ~CPDF_ReadValidator() override; 53 54 private: 55 void ScheduleDownload(FX_FILESIZE offset, size_t size); 56 bool IsDataRangeAvailable(FX_FILESIZE offset, size_t size) const; 57 58 RetainPtr<IFX_SeekableReadStream> file_read_; 59 UnownedPtr<CPDF_DataAvail::FileAvail> file_avail_; 60 61 UnownedPtr<CPDF_DataAvail::DownloadHints> hints_; 62 63 bool read_error_; 64 bool has_unavailable_data_; 65 bool whole_file_already_available_; 66 const FX_FILESIZE file_size_; 67 }; 68 69 #endif // CORE_FPDFAPI_PARSER_CPDF_READ_VALIDATOR_H_ 70