1 // Copyright 2017 The PDFium Authors 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_memory.h" 10 #include "core/fxcrt/fx_stream.h" 11 #include "core/fxcrt/retain_ptr.h" 12 #include "core/fxcrt/unowned_ptr.h" 13 14 class CPDF_ReadValidator : public IFX_SeekableReadStream { 15 public: 16 class ScopedSession { 17 public: 18 FX_STACK_ALLOCATED(); 19 20 explicit ScopedSession(RetainPtr<CPDF_ReadValidator> validator); 21 ScopedSession(const ScopedSession& that) = delete; 22 ScopedSession& operator=(const ScopedSession& that) = delete; 23 ~ScopedSession(); 24 25 private: 26 RetainPtr<CPDF_ReadValidator> const validator_; 27 const bool saved_read_error_; 28 const bool saved_has_unavailable_data_; 29 }; 30 31 CONSTRUCT_VIA_MAKE_RETAIN; 32 SetDownloadHints(CPDF_DataAvail::DownloadHints * hints)33 void SetDownloadHints(CPDF_DataAvail::DownloadHints* hints) { 34 hints_ = hints; 35 } read_error()36 bool read_error() const { return read_error_; } has_unavailable_data()37 bool has_unavailable_data() const { return has_unavailable_data_; } has_read_problems()38 bool has_read_problems() const { 39 return read_error() || has_unavailable_data(); 40 } 41 42 void ResetErrors(); 43 bool IsWholeFileAvailable(); 44 bool CheckDataRangeAndRequestIfUnavailable(FX_FILESIZE offset, size_t size); 45 bool CheckWholeFileAndRequestIfUnavailable(); 46 47 // IFX_SeekableReadStream overrides: 48 bool ReadBlockAtOffset(pdfium::span<uint8_t> buffer, 49 FX_FILESIZE offset) override; 50 FX_FILESIZE GetSize() override; 51 52 protected: 53 CPDF_ReadValidator(RetainPtr<IFX_SeekableReadStream> file_read, 54 CPDF_DataAvail::FileAvail* file_avail); 55 ~CPDF_ReadValidator() override; 56 57 private: 58 void ScheduleDownload(FX_FILESIZE offset, size_t size); 59 bool IsDataRangeAvailable(FX_FILESIZE offset, size_t size) const; 60 61 RetainPtr<IFX_SeekableReadStream> const file_read_; 62 UnownedPtr<CPDF_DataAvail::FileAvail> const file_avail_; 63 UnownedPtr<CPDF_DataAvail::DownloadHints> hints_; 64 bool read_error_ = false; 65 bool has_unavailable_data_ = false; 66 bool whole_file_already_available_ = false; 67 const FX_FILESIZE file_size_; 68 }; 69 70 #endif // CORE_FPDFAPI_PARSER_CPDF_READ_VALIDATOR_H_ 71