blob: 290a10d46d08298d2df2acd6e2016d0e4c683b08 [file] [log] [blame]
Artem Strygin0e60b9e2017-09-28 18:46:03 +03001// 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#include "testing/fake_file_access.h"
6
7#include <algorithm>
8#include <set>
9#include <utility>
10#include <vector>
11
12#include "core/fxcrt/fx_system.h"
13#include "third_party/base/ptr_util.h"
14
15namespace {
16
Tom Sepez55865452018-08-27 20:18:04 +000017class FileAccessWrapper final : public FPDF_FILEACCESS {
Artem Strygin0e60b9e2017-09-28 18:46:03 +030018 public:
19 explicit FileAccessWrapper(FakeFileAccess* simulator)
20 : simulator_(simulator) {
21 m_FileLen = simulator_->GetFileSize();
22 m_GetBlock = &GetBlockImpl;
23 m_Param = this;
24 }
25
26 static int GetBlockImpl(void* param,
27 unsigned long position,
28 unsigned char* pBuf,
29 unsigned long size) {
30 return static_cast<FileAccessWrapper*>(param)->simulator_->GetBlock(
31 position, pBuf, size);
32 }
33
34 private:
35 fxcrt::UnownedPtr<FakeFileAccess> simulator_;
36};
37
Tom Sepez55865452018-08-27 20:18:04 +000038class FileAvailImpl final : public FX_FILEAVAIL {
Artem Strygin0e60b9e2017-09-28 18:46:03 +030039 public:
40 explicit FileAvailImpl(FakeFileAccess* simulator) : simulator_(simulator) {
41 version = 1;
42 IsDataAvail = &IsDataAvailImpl;
43 }
44
45 static FPDF_BOOL IsDataAvailImpl(FX_FILEAVAIL* pThis,
46 size_t offset,
47 size_t size) {
48 return static_cast<FileAvailImpl*>(pThis)->simulator_->IsDataAvail(offset,
49 size);
50 }
51
52 private:
53 fxcrt::UnownedPtr<FakeFileAccess> simulator_;
54};
55
Tom Sepez55865452018-08-27 20:18:04 +000056class DownloadHintsImpl final : public FX_DOWNLOADHINTS {
Artem Strygin0e60b9e2017-09-28 18:46:03 +030057 public:
58 explicit DownloadHintsImpl(FakeFileAccess* simulator)
59 : simulator_(simulator) {
60 version = 1;
61 AddSegment = &AddSegmentImpl;
62 }
63
64 static void AddSegmentImpl(FX_DOWNLOADHINTS* pThis,
65 size_t offset,
66 size_t size) {
67 return static_cast<DownloadHintsImpl*>(pThis)->simulator_->AddSegment(
68 offset, size);
69 }
70
71 private:
72 fxcrt::UnownedPtr<FakeFileAccess> simulator_;
73};
74
75} // namespace
76
77FakeFileAccess::FakeFileAccess(FPDF_FILEACCESS* file_access)
78 : file_access_(file_access),
79 file_access_wrapper_(pdfium::MakeUnique<FileAccessWrapper>(this)),
80 file_avail_(pdfium::MakeUnique<FileAvailImpl>(this)),
81 download_hints_(pdfium::MakeUnique<DownloadHintsImpl>(this)) {
82 ASSERT(file_access_);
83}
84
85FakeFileAccess::~FakeFileAccess() {}
86
87FPDF_BOOL FakeFileAccess::IsDataAvail(size_t offset, size_t size) const {
88 return available_data_.Contains(RangeSet::Range(offset, offset + size));
89}
90
91void FakeFileAccess::AddSegment(size_t offset, size_t size) {
92 requested_data_.Union(RangeSet::Range(offset, offset + size));
93}
94
95unsigned long FakeFileAccess::GetFileSize() {
96 return file_access_->m_FileLen;
97}
98
99int FakeFileAccess::GetBlock(unsigned long position,
100 unsigned char* pBuf,
101 unsigned long size) {
Lei Zhangcef91f12019-01-08 21:32:51 +0000102 if (!pBuf || !size)
103 return false;
104
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300105 if (!IsDataAvail(static_cast<size_t>(position), static_cast<size_t>(size)))
106 return false;
Lei Zhangcef91f12019-01-08 21:32:51 +0000107
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300108 return file_access_->m_GetBlock(file_access_->m_Param, position, pBuf, size);
109}
110
111void FakeFileAccess::SetRequestedDataAvailable() {
112 available_data_.Union(requested_data_);
113 requested_data_.Clear();
114}
115
116void FakeFileAccess::SetWholeFileAvailable() {
117 available_data_.Union(RangeSet::Range(0, static_cast<size_t>(GetFileSize())));
118 requested_data_.Clear();
119}