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   class Session {
14    public:
15     explicit Session(const RetainPtr<CPDF_ReadValidator>& validator);
16     ~Session();
17 
18    private:
19     UnownedPtr<CPDF_ReadValidator> validator_;
20     bool saved_read_error_;
21     bool saved_has_unavailable_data_;
22   };
23 
24   template <typename T, typename... Args>
25   friend RetainPtr<T> pdfium::MakeRetain(Args&&... args);
26 
SetDownloadHints(CPDF_DataAvail::DownloadHints * hints)27   void SetDownloadHints(CPDF_DataAvail::DownloadHints* hints) {
28     hints_ = hints;
29   }
read_error()30   bool read_error() const { return read_error_; }
has_unavailable_data()31   bool has_unavailable_data() const { return has_unavailable_data_; }
has_read_problems()32   bool has_read_problems() const {
33     return read_error() || has_unavailable_data();
34   }
35 
36   void ResetErrors();
37 
38   bool IsWholeFileAvailable();
39 
40   bool CheckDataRangeAndRequestIfUnavailable(FX_FILESIZE offset, size_t size);
41   bool CheckWholeFileAndRequestIfUnavailable();
42 
43   // IFX_SeekableReadStream overrides:
44   bool ReadBlockAtOffset(void* buffer,
45                          FX_FILESIZE offset,
46                          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