Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
fake_file_access.cpp
Go to the documentation of this file.
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#include "testing/fake_file_access.h"
6
7#include <utility>
8
9#include "core/fxcrt/fx_system.h"
10#include "third_party/base/check.h"
11
12class FileAccessWrapper final : public FPDF_FILEACCESS {
13 public:
14 explicit FileAccessWrapper(FakeFileAccess* simulator)
16 m_FileLen = simulator_->GetFileSize();
17 m_GetBlock = &GetBlockImpl;
18 m_Param = this;
19 }
20
21 static int GetBlockImpl(void* param,
22 unsigned long position,
23 unsigned char* pBuf,
24 unsigned long size) {
25 return static_cast<FileAccessWrapper*>(param)->simulator_->GetBlock(
26 position, pBuf, size);
27 }
28
29 private:
30 UnownedPtr<FakeFileAccess> simulator_;
31};
32
33class FileAvailImpl final : public FX_FILEAVAIL {
34 public:
36 version = 1;
37 IsDataAvail = &IsDataAvailImpl;
38 }
39
40 static FPDF_BOOL IsDataAvailImpl(FX_FILEAVAIL* pThis,
41 size_t offset,
42 size_t size) {
43 return static_cast<FileAvailImpl*>(pThis)->simulator_->IsDataAvail(offset,
44 size);
45 }
46
47 private:
48 UnownedPtr<FakeFileAccess> simulator_;
49};
50
51class DownloadHintsImpl final : public FX_DOWNLOADHINTS {
52 public:
53 explicit DownloadHintsImpl(FakeFileAccess* simulator)
55 version = 1;
56 AddSegment = &AddSegmentImpl;
57 }
58
59 static void AddSegmentImpl(FX_DOWNLOADHINTS* pThis,
60 size_t offset,
61 size_t size) {
62 return static_cast<DownloadHintsImpl*>(pThis)->simulator_->AddSegment(
63 offset, size);
64 }
65
66 private:
67 UnownedPtr<FakeFileAccess> simulator_;
68};
69
77
78FakeFileAccess::~FakeFileAccess() = default;
79
80FPDF_FILEACCESS* FakeFileAccess::GetFileAccess() const {
81 return file_access_wrapper_.get();
82}
83
84FX_FILEAVAIL* FakeFileAccess::GetFileAvail() const {
85 return file_avail_.get();
86}
87
88FX_DOWNLOADHINTS* FakeFileAccess::GetDownloadHints() const {
89 return download_hints_.get();
90}
91
92FPDF_BOOL FakeFileAccess::IsDataAvail(size_t offset, size_t size) const {
93 return available_data_.Contains(RangeSet::Range(offset, offset + size));
94}
95
96void FakeFileAccess::AddSegment(size_t offset, size_t size) {
97 requested_data_.Union(RangeSet::Range(offset, offset + size));
98}
99
100unsigned long FakeFileAccess::GetFileSize() {
101 return file_access_->m_FileLen;
102}
103
104int FakeFileAccess::GetBlock(unsigned long position,
105 unsigned char* pBuf,
106 unsigned long size) {
107 if (!pBuf || !size)
108 return false;
109
110 if (!IsDataAvail(static_cast<size_t>(position), static_cast<size_t>(size)))
111 return false;
112
113 return file_access_->m_GetBlock(file_access_->m_Param, position, pBuf, size);
114}
115
117 available_data_.Union(requested_data_);
118 requested_data_.Clear();
119}
120
122 available_data_.Union(RangeSet::Range(0, static_cast<size_t>(GetFileSize())));
123 requested_data_.Clear();
124}
DownloadHintsImpl(FakeFileAccess *simulator)
static void AddSegmentImpl(FX_DOWNLOADHINTS *pThis, size_t offset, size_t size)
void AddSegment(size_t offset, size_t size)
FPDF_BOOL IsDataAvail(size_t offset, size_t size) const
int GetBlock(unsigned long position, unsigned char *pBuf, unsigned long size)
FakeFileAccess(FPDF_FILEACCESS *file_access)
FX_DOWNLOADHINTS * GetDownloadHints() const
unsigned long GetFileSize()
FX_FILEAVAIL * GetFileAvail() const
FPDF_FILEACCESS * GetFileAccess() const
FileAccessWrapper(FakeFileAccess *simulator)
static int GetBlockImpl(void *param, unsigned long position, unsigned char *pBuf, unsigned long size)
FileAvailImpl(FakeFileAccess *simulator)
static FPDF_BOOL IsDataAvailImpl(FX_FILEAVAIL *pThis, size_t offset, size_t size)
void(* AddSegment)(struct _FX_DOWNLOADHINTS *pThis, size_t offset, size_t size)