diff options
author | Tomaž Vajngerl <tomaz.vajngerl@collabora.co.uk> | 2020-05-31 11:50:20 +0200 |
---|---|---|
committer | Miklos Vajna <vmiklos@collabora.com> | 2020-09-09 15:40:37 +0200 |
commit | b7de766b4dc5b4810277069bcf53a9f3737e87da (patch) | |
tree | 6372a6d6b42bccd47bd5ad09c353b44ccf317483 /vcl/source | |
parent | 047351c2f9997dcd345c00c1bc1f0d72c8d62ed9 (diff) |
pdfium: only init pdfium library once and destroy on LO exit
With more and more usage of PDFium, it is hard to keep track of
the life-time of the PDFium library, so it can happen that a
FPDF_DestroyLibrary happens when we still have another instance
where PDFium is still use. The result of this is a crash. To
prevent this, just initialize the library once and delete, when
on LO exit.
This can be improved in the future to only keep the library
active when in actual use.
[ Leaving out the vector graphic search bits, the motivation is to just
have this in libreoffice-7-0, so that recent pdf sig verify improvements
can be backported. ]
(cherry picked from commit 067a8a954c8e1d8d6465a4ab5fb61e93f16c26c2)
Conflicts:
vcl/source/graphic/VectorGraphicSearch.cxx
Change-Id: I5c7e5de7f8b97d10efb394c67c7a61b976c8d57c
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/102317
Tested-by: Miklos Vajna <vmiklos@collabora.com>
Reviewed-by: Miklos Vajna <vmiklos@collabora.com>
Diffstat (limited to 'vcl/source')
-rw-r--r-- | vcl/source/filter/ipdf/pdfread.cxx | 26 | ||||
-rw-r--r-- | vcl/source/pdf/PDFiumLibrary.cxx | 36 |
2 files changed, 41 insertions, 21 deletions
diff --git a/vcl/source/filter/ipdf/pdfread.cxx b/vcl/source/filter/ipdf/pdfread.cxx index 6ace744ec1eb..5401bf3734a3 100644 --- a/vcl/source/filter/ipdf/pdfread.cxx +++ b/vcl/source/filter/ipdf/pdfread.cxx @@ -21,6 +21,8 @@ #include <bitmapwriteaccess.hxx> #include <unotools/ucbstreamhelper.hxx> +#include <vcl/filter/PDFiumLibrary.hxx> + using namespace com::sun::star; namespace @@ -95,12 +97,7 @@ bool getCompatibleStream(SvStream& rInStream, SvStream& rOutStream, sal_uInt64 n else { // Downconvert to PDF-1.5. - FPDF_LIBRARY_CONFIG aConfig; - aConfig.version = 2; - aConfig.m_pUserFontPaths = nullptr; - aConfig.m_pIsolate = nullptr; - aConfig.m_v8EmbedderSlot = 0; - FPDF_InitLibraryWithConfig(&aConfig); + auto pPdfium = vcl::pdf::PDFiumLibrary::get(); // Read input into a buffer. SvMemoryStream aInBuffer; @@ -121,7 +118,6 @@ bool getCompatibleStream(SvStream& rInStream, SvStream& rOutStream, sal_uInt64 n return false; FPDF_CloseDocument(pPdfDocument); - FPDF_DestroyLibrary(); aWriter.m_aStream.Seek(STREAM_SEEK_TO_BEGIN); rOutStream.WriteStream(aWriter.m_aStream); @@ -152,12 +148,7 @@ size_t RenderPDFBitmaps(const void* pBuffer, int nSize, std::vector<Bitmap>& rBi const size_t nFirstPage, int nPages, const double fResolutionDPI) { #if HAVE_FEATURE_PDFIUM - FPDF_LIBRARY_CONFIG aConfig; - aConfig.version = 2; - aConfig.m_pUserFontPaths = nullptr; - aConfig.m_pIsolate = nullptr; - aConfig.m_v8EmbedderSlot = 0; - FPDF_InitLibraryWithConfig(&aConfig); + auto pPdfium = vcl::pdf::PDFiumLibrary::get(); // Load the buffer using pdfium. FPDF_DOCUMENT pPdfDocument = FPDF_LoadMemDocument(pBuffer, nSize, /*password=*/nullptr); @@ -207,7 +198,6 @@ size_t RenderPDFBitmaps(const void* pBuffer, int nSize, std::vector<Bitmap>& rBi } FPDF_CloseDocument(pPdfDocument); - FPDF_DestroyLibrary(); return rBitmaps.size(); #else @@ -305,12 +295,7 @@ size_t ImportPDFUnloaded(const OUString& rURL, std::vector<std::pair<Graphic, Si std::shared_ptr<GfxLink> pGfxLink(std::make_shared<GfxLink>( std::move(pGraphicContent), nGraphicContentSize, GfxLinkType::NativePdf)); - FPDF_LIBRARY_CONFIG aConfig; - aConfig.version = 2; - aConfig.m_pUserFontPaths = nullptr; - aConfig.m_pIsolate = nullptr; - aConfig.m_v8EmbedderSlot = 0; - FPDF_InitLibraryWithConfig(&aConfig); + auto pPdfium = vcl::pdf::PDFiumLibrary::get(); // Load the buffer using pdfium. FPDF_DOCUMENT pPdfDocument @@ -348,7 +333,6 @@ size_t ImportPDFUnloaded(const OUString& rURL, std::vector<std::pair<Graphic, Si } FPDF_CloseDocument(pPdfDocument); - FPDF_DestroyLibrary(); return rGraphics.size(); #else diff --git a/vcl/source/pdf/PDFiumLibrary.cxx b/vcl/source/pdf/PDFiumLibrary.cxx new file mode 100644 index 000000000000..604807524bf9 --- /dev/null +++ b/vcl/source/pdf/PDFiumLibrary.cxx @@ -0,0 +1,36 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + */ + +#include <config_features.h> + +#if HAVE_FEATURE_PDFIUM + +#include <vcl/filter/PDFiumLibrary.hxx> +#include <fpdf_doc.h> + +namespace vcl::pdf +{ +PDFium::PDFium() +{ + FPDF_LIBRARY_CONFIG aConfig; + aConfig.version = 2; + aConfig.m_pUserFontPaths = nullptr; + aConfig.m_pIsolate = nullptr; + aConfig.m_v8EmbedderSlot = 0; + FPDF_InitLibraryWithConfig(&aConfig); +} + +PDFium::~PDFium() { FPDF_DestroyLibrary(); } + +} // end vcl::pdf + +#endif // HAVE_FEATURE_PDFIUM + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |