Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(388)

Side by Side Diff: fpdfsdk/fpdfview.cpp

Issue 2451493002: Refcount all the IFX_ stream classes all the time. (Closed)
Patch Set: Clean up cast expression Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « fpdfsdk/fpdfsave.cpp ('k') | fpdfsdk/fpdfxfa/cpdfxfa_context.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2014 PDFium Authors. All rights reserved. 1 // Copyright 2014 PDFium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com 5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6 6
7 #include "public/fpdfview.h" 7 #include "public/fpdfview.h"
8 8
9 #include <memory> 9 #include <memory>
10 #include <utility> 10 #include <utility>
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after
112 pContext->m_pRenderer = pdfium::MakeUnique<CPDF_ProgressiveRenderer>( 112 pContext->m_pRenderer = pdfium::MakeUnique<CPDF_ProgressiveRenderer>(
113 pContext->m_pContext.get(), pContext->m_pDevice.get(), 113 pContext->m_pContext.get(), pContext->m_pDevice.get(),
114 pContext->m_pOptions.get()); 114 pContext->m_pOptions.get());
115 pContext->m_pRenderer->Start(pause); 115 pContext->m_pRenderer->Start(pause);
116 if (bNeedToRestore) 116 if (bNeedToRestore)
117 pContext->m_pDevice->RestoreState(false); 117 pContext->m_pDevice->RestoreState(false);
118 } 118 }
119 119
120 class CPDF_CustomAccess final : public IFX_SeekableReadStream { 120 class CPDF_CustomAccess final : public IFX_SeekableReadStream {
121 public: 121 public:
122 explicit CPDF_CustomAccess(FPDF_FILEACCESS* pFileAccess); 122 static CFX_RetainPtr<CPDF_CustomAccess> Create(FPDF_FILEACCESS* pFileAccess) {
123 ~CPDF_CustomAccess() override {} 123 return CFX_RetainPtr<CPDF_CustomAccess>(new CPDF_CustomAccess(pFileAccess));
124 }
124 125
125 // IFX_SeekableReadStream 126 // IFX_SeekableReadStream
126 FX_FILESIZE GetSize() override; 127 FX_FILESIZE GetSize() override;
127 void Release() override;
128 bool ReadBlock(void* buffer, FX_FILESIZE offset, size_t size) override; 128 bool ReadBlock(void* buffer, FX_FILESIZE offset, size_t size) override;
129 129
130 private: 130 private:
131 explicit CPDF_CustomAccess(FPDF_FILEACCESS* pFileAccess);
132
131 FPDF_FILEACCESS m_FileAccess; 133 FPDF_FILEACCESS m_FileAccess;
132 }; 134 };
133 135
134 CPDF_CustomAccess::CPDF_CustomAccess(FPDF_FILEACCESS* pFileAccess) 136 CPDF_CustomAccess::CPDF_CustomAccess(FPDF_FILEACCESS* pFileAccess)
135 : m_FileAccess(*pFileAccess) {} 137 : m_FileAccess(*pFileAccess) {}
136 138
137 FX_FILESIZE CPDF_CustomAccess::GetSize() { 139 FX_FILESIZE CPDF_CustomAccess::GetSize() {
138 return m_FileAccess.m_FileLen; 140 return m_FileAccess.m_FileLen;
139 } 141 }
140 142
141 void CPDF_CustomAccess::Release() {
142 delete this;
143 }
144
145 bool CPDF_CustomAccess::ReadBlock(void* buffer, 143 bool CPDF_CustomAccess::ReadBlock(void* buffer,
146 FX_FILESIZE offset, 144 FX_FILESIZE offset,
147 size_t size) { 145 size_t size) {
148 if (offset < 0) 146 if (offset < 0)
149 return false; 147 return false;
150 148
151 FX_SAFE_FILESIZE newPos = pdfium::base::checked_cast<FX_FILESIZE>(size); 149 FX_SAFE_FILESIZE newPos = pdfium::base::checked_cast<FX_FILESIZE>(size);
152 newPos += offset; 150 newPos += offset;
153 if (!newPos.IsValid() || 151 if (!newPos.IsValid() ||
154 newPos.ValueOrDie() > static_cast<FX_FILESIZE>(m_FileAccess.m_FileLen)) { 152 newPos.ValueOrDie() > static_cast<FX_FILESIZE>(m_FileAccess.m_FileLen)) {
155 return false; 153 return false;
156 } 154 }
157 return !!m_FileAccess.m_GetBlock(m_FileAccess.m_Param, offset, 155 return !!m_FileAccess.m_GetBlock(m_FileAccess.m_Param, offset,
158 reinterpret_cast<uint8_t*>(buffer), size); 156 reinterpret_cast<uint8_t*>(buffer), size);
159 } 157 }
160 158
161 #ifdef PDF_ENABLE_XFA 159 #ifdef PDF_ENABLE_XFA
162 class CFPDF_FileStream : public IFX_SeekableStream { 160 class CFPDF_FileStream : public IFX_SeekableStream {
163 public: 161 public:
164 explicit CFPDF_FileStream(FPDF_FILEHANDLER* pFS); 162 static CFX_RetainPtr<CFPDF_FileStream> Create(FPDF_FILEHANDLER* pFS) {
165 ~CFPDF_FileStream() override {} 163 return CFX_RetainPtr<CFPDF_FileStream>(new CFPDF_FileStream(pFS));
164 }
165 ~CFPDF_FileStream() override;
166 166
167 // IFX_SeekableStream: 167 // IFX_SeekableStream:
168 IFX_SeekableStream* Retain() override;
169 void Release() override;
170 FX_FILESIZE GetSize() override; 168 FX_FILESIZE GetSize() override;
171 bool IsEOF() override; 169 bool IsEOF() override;
172 FX_FILESIZE GetPosition() override; 170 FX_FILESIZE GetPosition() override;
173 bool ReadBlock(void* buffer, FX_FILESIZE offset, size_t size) override; 171 bool ReadBlock(void* buffer, FX_FILESIZE offset, size_t size) override;
174 size_t ReadBlock(void* buffer, size_t size) override; 172 size_t ReadBlock(void* buffer, size_t size) override;
175 bool WriteBlock(const void* buffer, FX_FILESIZE offset, size_t size) override; 173 bool WriteBlock(const void* buffer, FX_FILESIZE offset, size_t size) override;
176 bool Flush() override; 174 bool Flush() override;
177 175
178 void SetPosition(FX_FILESIZE pos) { m_nCurPos = pos; } 176 void SetPosition(FX_FILESIZE pos) { m_nCurPos = pos; }
179 177
180 protected: 178 protected:
179 explicit CFPDF_FileStream(FPDF_FILEHANDLER* pFS);
180
181 FPDF_FILEHANDLER* m_pFS; 181 FPDF_FILEHANDLER* m_pFS;
182 FX_FILESIZE m_nCurPos; 182 FX_FILESIZE m_nCurPos;
183 }; 183 };
184 184
185 CFPDF_FileStream::CFPDF_FileStream(FPDF_FILEHANDLER* pFS) { 185 CFPDF_FileStream::CFPDF_FileStream(FPDF_FILEHANDLER* pFS) {
186 m_pFS = pFS; 186 m_pFS = pFS;
187 m_nCurPos = 0; 187 m_nCurPos = 0;
188 } 188 }
189 189
190 IFX_SeekableStream* CFPDF_FileStream::Retain() { 190 CFPDF_FileStream::~CFPDF_FileStream() {
191 return this;
192 }
193
194 void CFPDF_FileStream::Release() {
195 if (m_pFS && m_pFS->Release) 191 if (m_pFS && m_pFS->Release)
196 m_pFS->Release(m_pFS->clientData); 192 m_pFS->Release(m_pFS->clientData);
197 delete this;
198 } 193 }
199 194
200 FX_FILESIZE CFPDF_FileStream::GetSize() { 195 FX_FILESIZE CFPDF_FileStream::GetSize() {
201 if (m_pFS && m_pFS->GetSize) 196 if (m_pFS && m_pFS->GetSize)
202 return (FX_FILESIZE)m_pFS->GetSize(m_pFS->clientData); 197 return (FX_FILESIZE)m_pFS->GetSize(m_pFS->clientData);
203 return 0; 198 return 0;
204 } 199 }
205 200
206 bool CFPDF_FileStream::IsEOF() { 201 bool CFPDF_FileStream::IsEOF() {
207 return m_nCurPos >= GetSize(); 202 return m_nCurPos >= GetSize();
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after
303 return page ? UnderlyingFromFPDFPage(page)->GetPDFPage() : nullptr; 298 return page ? UnderlyingFromFPDFPage(page)->GetPDFPage() : nullptr;
304 #else // PDF_ENABLE_XFA 299 #else // PDF_ENABLE_XFA
305 return UnderlyingFromFPDFPage(page); 300 return UnderlyingFromFPDFPage(page);
306 #endif // PDF_ENABLE_XFA 301 #endif // PDF_ENABLE_XFA
307 } 302 }
308 303
309 CFX_DIBitmap* CFXBitmapFromFPDFBitmap(FPDF_BITMAP bitmap) { 304 CFX_DIBitmap* CFXBitmapFromFPDFBitmap(FPDF_BITMAP bitmap) {
310 return static_cast<CFX_DIBitmap*>(bitmap); 305 return static_cast<CFX_DIBitmap*>(bitmap);
311 } 306 }
312 307
313 IFX_SeekableReadStream* MakeSeekableReadStream(FPDF_FILEACCESS* pFileAccess) { 308 CFX_RetainPtr<IFX_SeekableReadStream> MakeSeekableReadStream(
314 return new CPDF_CustomAccess(pFileAccess); 309 FPDF_FILEACCESS* pFileAccess) {
310 return CPDF_CustomAccess::Create(pFileAccess);
315 } 311 }
316 312
317 #ifdef PDF_ENABLE_XFA 313 #ifdef PDF_ENABLE_XFA
318 IFX_SeekableStream* MakeSeekableStream(FPDF_FILEHANDLER* pFilehandler) { 314 CFX_RetainPtr<IFX_SeekableStream> MakeSeekableStream(
319 return new CFPDF_FileStream(pFilehandler); 315 FPDF_FILEHANDLER* pFilehandler) {
316 return CFPDF_FileStream::Create(pFilehandler);
320 } 317 }
321 #endif // PDF_ENABLE_XFA 318 #endif // PDF_ENABLE_XFA
322 319
323 // 0 bit: FPDF_POLICY_MACHINETIME_ACCESS 320 // 0 bit: FPDF_POLICY_MACHINETIME_ACCESS
324 static uint32_t foxit_sandbox_policy = 0xFFFFFFFF; 321 static uint32_t foxit_sandbox_policy = 0xFFFFFFFF;
325 322
326 void FSDK_SetSandBoxPolicy(FPDF_DWORD policy, FPDF_BOOL enable) { 323 void FSDK_SetSandBoxPolicy(FPDF_DWORD policy, FPDF_BOOL enable) {
327 switch (policy) { 324 switch (policy) {
328 case FPDF_POLICY_MACHINETIME_ACCESS: { 325 case FPDF_POLICY_MACHINETIME_ACCESS: {
329 if (enable) 326 if (enable)
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after
440 437
441 DLLEXPORT void STDCALL FPDF_SetPrintTextWithGDI(FPDF_BOOL use_gdi) { 438 DLLEXPORT void STDCALL FPDF_SetPrintTextWithGDI(FPDF_BOOL use_gdi) {
442 g_pdfium_print_text_with_gdi = !!use_gdi; 439 g_pdfium_print_text_with_gdi = !!use_gdi;
443 } 440 }
444 #endif 441 #endif
445 442
446 DLLEXPORT FPDF_DOCUMENT STDCALL FPDF_LoadDocument(FPDF_STRING file_path, 443 DLLEXPORT FPDF_DOCUMENT STDCALL FPDF_LoadDocument(FPDF_STRING file_path,
447 FPDF_BYTESTRING password) { 444 FPDF_BYTESTRING password) {
448 // NOTE: the creation of the file needs to be by the embedder on the 445 // NOTE: the creation of the file needs to be by the embedder on the
449 // other side of this API. 446 // other side of this API.
450 IFX_SeekableReadStream* pFileAccess = 447 CFX_RetainPtr<IFX_SeekableReadStream> pFileAccess =
451 IFX_SeekableReadStream::CreateFromFilename((const FX_CHAR*)file_path); 448 IFX_SeekableReadStream::CreateFromFilename((const FX_CHAR*)file_path);
452 if (!pFileAccess) 449 if (!pFileAccess)
453 return nullptr; 450 return nullptr;
454 451
455 auto pParser = pdfium::MakeUnique<CPDF_Parser>(); 452 auto pParser = pdfium::MakeUnique<CPDF_Parser>();
456 pParser->SetPassword(password); 453 pParser->SetPassword(password);
457 454
458 auto pDocument = pdfium::MakeUnique<CPDF_Document>(std::move(pParser)); 455 auto pDocument = pdfium::MakeUnique<CPDF_Document>(std::move(pParser));
459 CPDF_Parser::Error error = 456 CPDF_Parser::Error error =
460 pDocument->GetParser()->StartParse(pFileAccess, pDocument.get()); 457 pDocument->GetParser()->StartParse(pFileAccess, pDocument.get());
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
493 return true; 490 return true;
494 } 491 }
495 492
496 DLLEXPORT FPDF_BOOL STDCALL FPDF_LoadXFA(FPDF_DOCUMENT document) { 493 DLLEXPORT FPDF_BOOL STDCALL FPDF_LoadXFA(FPDF_DOCUMENT document) {
497 return document && (static_cast<CPDFXFA_Context*>(document))->LoadXFADoc(); 494 return document && (static_cast<CPDFXFA_Context*>(document))->LoadXFADoc();
498 } 495 }
499 #endif // PDF_ENABLE_XFA 496 #endif // PDF_ENABLE_XFA
500 497
501 class CMemFile final : public IFX_SeekableReadStream { 498 class CMemFile final : public IFX_SeekableReadStream {
502 public: 499 public:
503 CMemFile(uint8_t* pBuf, FX_FILESIZE size) : m_pBuf(pBuf), m_size(size) {} 500 static CFX_RetainPtr<CMemFile> Create(uint8_t* pBuf, FX_FILESIZE size) {
501 return CFX_RetainPtr<CMemFile>(new CMemFile(pBuf, size));
502 }
504 503
505 void Release() override { delete this; }
506 FX_FILESIZE GetSize() override { return m_size; } 504 FX_FILESIZE GetSize() override { return m_size; }
507 bool ReadBlock(void* buffer, FX_FILESIZE offset, size_t size) override { 505 bool ReadBlock(void* buffer, FX_FILESIZE offset, size_t size) override {
508 if (offset < 0) { 506 if (offset < 0)
509 return false; 507 return false;
510 } 508
511 FX_SAFE_FILESIZE newPos = pdfium::base::checked_cast<FX_FILESIZE>(size); 509 FX_SAFE_FILESIZE newPos = pdfium::base::checked_cast<FX_FILESIZE>(size);
512 newPos += offset; 510 newPos += offset;
513 if (!newPos.IsValid() || newPos.ValueOrDie() > m_size) { 511 if (!newPos.IsValid() || newPos.ValueOrDie() > m_size)
514 return false; 512 return false;
515 } 513
516 FXSYS_memcpy(buffer, m_pBuf + offset, size); 514 FXSYS_memcpy(buffer, m_pBuf + offset, size);
517 return true; 515 return true;
518 } 516 }
519 517
520 private: 518 private:
521 ~CMemFile() override {} 519 CMemFile(uint8_t* pBuf, FX_FILESIZE size) : m_pBuf(pBuf), m_size(size) {}
522 520
523 uint8_t* const m_pBuf; 521 uint8_t* const m_pBuf;
524 const FX_FILESIZE m_size; 522 const FX_FILESIZE m_size;
525 }; 523 };
526 524
527 DLLEXPORT FPDF_DOCUMENT STDCALL FPDF_LoadMemDocument(const void* data_buf, 525 DLLEXPORT FPDF_DOCUMENT STDCALL FPDF_LoadMemDocument(const void* data_buf,
528 int size, 526 int size,
529 FPDF_BYTESTRING password) { 527 FPDF_BYTESTRING password) {
530 CMemFile* pMemFile = new CMemFile((uint8_t*)data_buf, size); 528 CFX_RetainPtr<CMemFile> pMemFile = CMemFile::Create((uint8_t*)data_buf, size);
531 std::unique_ptr<CPDF_Parser> pParser(new CPDF_Parser); 529 auto pParser = pdfium::MakeUnique<CPDF_Parser>();
532 pParser->SetPassword(password); 530 pParser->SetPassword(password);
533 531
534 std::unique_ptr<CPDF_Document> pDocument( 532 auto pDocument = pdfium::MakeUnique<CPDF_Document>(std::move(pParser));
535 new CPDF_Document(std::move(pParser)));
536 CPDF_Parser::Error error = 533 CPDF_Parser::Error error =
537 pDocument->GetParser()->StartParse(pMemFile, pDocument.get()); 534 pDocument->GetParser()->StartParse(pMemFile, pDocument.get());
538 if (error != CPDF_Parser::SUCCESS) { 535 if (error != CPDF_Parser::SUCCESS) {
539 ProcessParseError(error); 536 ProcessParseError(error);
540 return nullptr; 537 return nullptr;
541 } 538 }
542 CheckUnSupportError(pDocument.get(), error); 539 CheckUnSupportError(pDocument.get(), error);
543 return FPDFDocumentFromCPDFDocument(pDocument.release()); 540 return FPDFDocumentFromCPDFDocument(pDocument.release());
544 } 541 }
545 542
546 DLLEXPORT FPDF_DOCUMENT STDCALL 543 DLLEXPORT FPDF_DOCUMENT STDCALL
547 FPDF_LoadCustomDocument(FPDF_FILEACCESS* pFileAccess, 544 FPDF_LoadCustomDocument(FPDF_FILEACCESS* pFileAccess,
548 FPDF_BYTESTRING password) { 545 FPDF_BYTESTRING password) {
549 CPDF_CustomAccess* pFile = new CPDF_CustomAccess(pFileAccess); 546 CFX_RetainPtr<CPDF_CustomAccess> pFile =
550 std::unique_ptr<CPDF_Parser> pParser(new CPDF_Parser); 547 CPDF_CustomAccess::Create(pFileAccess);
548 auto pParser = pdfium::MakeUnique<CPDF_Parser>();
551 pParser->SetPassword(password); 549 pParser->SetPassword(password);
552 550
553 std::unique_ptr<CPDF_Document> pDocument( 551 auto pDocument = pdfium::MakeUnique<CPDF_Document>(std::move(pParser));
554 new CPDF_Document(std::move(pParser)));
555 CPDF_Parser::Error error = 552 CPDF_Parser::Error error =
556 pDocument->GetParser()->StartParse(pFile, pDocument.get()); 553 pDocument->GetParser()->StartParse(pFile, pDocument.get());
557 if (error != CPDF_Parser::SUCCESS) { 554 if (error != CPDF_Parser::SUCCESS) {
558 ProcessParseError(error); 555 ProcessParseError(error);
559 return nullptr; 556 return nullptr;
560 } 557 }
561 CheckUnSupportError(pDocument.get(), error); 558 CheckUnSupportError(pDocument.get(), error);
562 return FPDFDocumentFromCPDFDocument(pDocument.release()); 559 return FPDFDocumentFromCPDFDocument(pDocument.release());
563 } 560 }
564 561
(...skipping 659 matching lines...) Expand 10 before | Expand all | Expand 10 after
1224 if (!buffer) { 1221 if (!buffer) {
1225 *buflen = len; 1222 *buflen = len;
1226 } else if (len <= *buflen) { 1223 } else if (len <= *buflen) {
1227 memcpy(buffer, utf16Name.c_str(), len); 1224 memcpy(buffer, utf16Name.c_str(), len);
1228 *buflen = len; 1225 *buflen = len;
1229 } else { 1226 } else {
1230 *buflen = -1; 1227 *buflen = -1;
1231 } 1228 }
1232 return (FPDF_DEST)pDestObj; 1229 return (FPDF_DEST)pDestObj;
1233 } 1230 }
OLDNEW
« no previous file with comments | « fpdfsdk/fpdfsave.cpp ('k') | fpdfsdk/fpdfxfa/cpdfxfa_context.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698