summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTomaž Vajngerl <tomaz.vajngerl@collabora.co.uk>2020-05-31 11:50:20 +0200
committerMiklos Vajna <vmiklos@collabora.com>2020-12-21 10:07:07 +0100
commitf11d01696bb18616482804f789307b24ddf36174 (patch)
treee521bf7d565876eaf97d5d15468188e83f63e8cb
parent9ce29950a971a88a785dd03f9bc07813b8248ba2 (diff)
pdfium: only init pdfium library one 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. Reviewed-on: https://gerrit.libreoffice.org/c/core/+/95391 Tested-by: Jenkins Reviewed-by: Tomaž Vajngerl <quikee@gmail.com> (cherry picked from commit 067a8a954c8e1d8d6465a4ab5fb61e93f16c26c2) Reviewed-on: https://gerrit.libreoffice.org/c/core/+/95933 Tested-by: Tomaž Vajngerl <quikee@gmail.com> Reviewed-by: Tomaž Vajngerl <quikee@gmail.com> (cherry picked from commit 3538b83c8d83e66f63c745bd769d118117704026) Conflicts: vcl/qa/cppunit/pdfexport/pdfexport.cxx vcl/source/filter/ipdf/pdfread.cxx vcl/source/graphic/VectorGraphicSearch.cxx Change-Id: I5c7e5de7f8b97d10efb394c67c7a61b976c8d57c Reviewed-on: https://gerrit.libreoffice.org/c/core/+/107943 Tested-by: Jenkins CollaboraOffice <jenkinscollaboraoffice@gmail.com> Reviewed-by: Miklos Vajna <vmiklos@collabora.com>
-rw-r--r--include/vcl/filter/PDFiumLibrary.hxx46
-rw-r--r--svx/source/svdraw/svdpdf.cxx14
-rw-r--r--svx/source/svdraw/svdpdf.hxx4
-rw-r--r--vcl/Library_vcl.mk1
-rw-r--r--vcl/qa/cppunit/pdfexport/pdfexport.cxx11
-rw-r--r--vcl/source/filter/ipdf/pdfread.cxx26
-rw-r--r--vcl/source/pdf/PDFiumLibrary.cxx39
7 files changed, 101 insertions, 40 deletions
diff --git a/include/vcl/filter/PDFiumLibrary.hxx b/include/vcl/filter/PDFiumLibrary.hxx
new file mode 100644
index 000000000000..1f6d97045088
--- /dev/null
+++ b/include/vcl/filter/PDFiumLibrary.hxx
@@ -0,0 +1,46 @@
+/* -*- 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/.
+ *
+ */
+
+#pragma once
+
+#include <config_features.h>
+
+#if HAVE_FEATURE_PDFIUM
+
+#include <memory>
+#include <rtl/instance.hxx>
+#include <vcl/dllapi.h>
+
+namespace vcl
+{
+namespace pdf
+{
+class VCL_DLLPUBLIC PDFium final
+{
+private:
+ PDFium(const PDFium&) = delete;
+ PDFium& operator=(const PDFium&) = delete;
+
+public:
+ PDFium();
+ ~PDFium();
+};
+
+struct PDFiumLibrary : public rtl::StaticWithInit<std::shared_ptr<PDFium>, PDFiumLibrary>
+{
+ std::shared_ptr<PDFium> operator()() { return std::make_shared<PDFium>(); }
+};
+
+} // namespace pdf
+} // namespace pdf
+
+#endif // HAVE_FEATURE_PDFIUM
+
+/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/svx/source/svdraw/svdpdf.cxx b/svx/source/svdraw/svdpdf.cxx
index 9e49a3d1e90a..78437f247123 100644
--- a/svx/source/svdraw/svdpdf.cxx
+++ b/svx/source/svdraw/svdpdf.cxx
@@ -147,6 +147,7 @@ ImpSdrPdfImport::ImpSdrPdfImport(SdrModel& rModel, SdrLayerID nLay, const tools:
, mnPageCount(0)
, mdPageWidthPts(0)
, mdPageHeightPts(0)
+ , mpPDFium(vcl::pdf::PDFiumLibrary::get())
{
mpVD->EnableOutput(false);
mpVD->SetLineColor();
@@ -160,13 +161,6 @@ ImpSdrPdfImport::ImpSdrPdfImport(SdrModel& rModel, SdrLayerID nLay, const tools:
svl::Items<EE_ITEMS_START, EE_ITEMS_END>{});
checkClip();
- FPDF_LIBRARY_CONFIG aConfig;
- aConfig.version = 2;
- aConfig.m_pUserFontPaths = nullptr;
- aConfig.m_pIsolate = nullptr;
- aConfig.m_v8EmbedderSlot = 0;
- FPDF_InitLibraryWithConfig(&aConfig);
-
// Load the buffer using pdfium.
mpPdfDocument = FPDF_LoadMemDocument(mpPdfData->data(), mpPdfData->size(),
/*password=*/nullptr);
@@ -199,11 +193,7 @@ ImpSdrPdfImport::ImpSdrPdfImport(SdrModel& rModel, SdrLayerID nLay, const tools:
mnPageCount = FPDF_GetPageCount(mpPdfDocument);
}
-ImpSdrPdfImport::~ImpSdrPdfImport()
-{
- FPDF_CloseDocument(mpPdfDocument);
- FPDF_DestroyLibrary();
-}
+ImpSdrPdfImport::~ImpSdrPdfImport() { FPDF_CloseDocument(mpPdfDocument); }
void ImpSdrPdfImport::DoObjects(SvdProgressInfo* pProgrInfo, sal_uInt32* pActionsToReport,
int nPageIndex)
diff --git a/svx/source/svdraw/svdpdf.hxx b/svx/source/svdraw/svdpdf.hxx
index df8eac35bbe7..ab880b930649 100644
--- a/svx/source/svdraw/svdpdf.hxx
+++ b/svx/source/svdraw/svdpdf.hxx
@@ -37,6 +37,8 @@
#include <fpdfview.h>
+#include <vcl/filter/PDFiumLibrary.hxx>
+
// Forward Declarations
class SfxItemSet;
@@ -203,6 +205,8 @@ class ImpSdrPdfImport final
tools::Rectangle PointsToLogic(double left, double right, double top, double bottom) const;
Point PointsToLogic(double x, double y) const;
+ std::shared_ptr<vcl::pdf::PDFium> mpPDFium;
+
// check for clip and evtl. fill maClip
void checkClip();
bool isClip() const;
diff --git a/vcl/Library_vcl.mk b/vcl/Library_vcl.mk
index 337dba4803df..64d56b45a9d0 100644
--- a/vcl/Library_vcl.mk
+++ b/vcl/Library_vcl.mk
@@ -304,6 +304,7 @@ $(eval $(call gb_Library_add_exception_objects,vcl,\
vcl/source/gdi/wall \
vcl/source/gdi/scrptrun \
vcl/source/gdi/CommonSalLayout \
+ vcl/source/pdf/PDFiumLibrary \
vcl/source/graphic/GraphicLoader \
vcl/source/bitmap/bitmapfilter \
vcl/source/bitmap/bitmapscalesuper \
diff --git a/vcl/qa/cppunit/pdfexport/pdfexport.cxx b/vcl/qa/cppunit/pdfexport/pdfexport.cxx
index 0ac88bc96db9..d2a46756b89b 100644
--- a/vcl/qa/cppunit/pdfexport/pdfexport.cxx
+++ b/vcl/qa/cppunit/pdfexport/pdfexport.cxx
@@ -28,6 +28,8 @@
#include <fpdf_text.h>
#include <fpdfview.h>
+#include <vcl/filter/PDFiumLibrary.hxx>
+
using namespace ::com::sun::star;
namespace
@@ -44,6 +46,7 @@ class PdfExportTest : public test::BootstrapFixture, public unotest::MacrosTest
SvMemoryStream maMemory;
// Export the document as PDF, then parse it with PDFium.
void exportAndParse(const OUString& rURL, const utl::MediaDescriptor& rDescriptor);
+ std::shared_ptr<vcl::pdf::PDFium> mpPDFium;
public:
PdfExportTest();
@@ -141,19 +144,13 @@ void PdfExportTest::setUp()
mxComponentContext.set(comphelper::getComponentContext(getMultiServiceFactory()));
mxDesktop.set(frame::Desktop::create(mxComponentContext));
- FPDF_LIBRARY_CONFIG config;
- config.version = 2;
- config.m_pUserFontPaths = nullptr;
- config.m_pIsolate = nullptr;
- config.m_v8EmbedderSlot = 0;
- FPDF_InitLibraryWithConfig(&config);
+ mpPDFium = vcl::pdf::PDFiumLibrary::get();
}
void PdfExportTest::tearDown()
{
FPDF_ClosePage(mpPdfPage);
FPDF_CloseDocument(mpPdfDocument);
- FPDF_DestroyLibrary();
if (mxComponent.is())
mxComponent->dispose();
diff --git a/vcl/source/filter/ipdf/pdfread.cxx b/vcl/source/filter/ipdf/pdfread.cxx
index e1d5c5b9b890..b26b4157e54c 100644
--- a/vcl/source/filter/ipdf/pdfread.cxx
+++ b/vcl/source/filter/ipdf/pdfread.cxx
@@ -23,6 +23,8 @@
#include <vcl/bitmapaccess.hxx>
#include <unotools/ucbstreamhelper.hxx>
+#include <vcl/filter/PDFiumLibrary.hxx>
+
using namespace com::sun::star;
namespace
@@ -107,12 +109,7 @@ bool getCompatibleStream(SvStream& rInStream, SvStream& rOutStream,
else
{
// Downconvert to PDF-1.4.
- 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;
@@ -129,7 +126,6 @@ bool getCompatibleStream(SvStream& rInStream, SvStream& rOutStream,
return false;
FPDF_CloseDocument(pPdfDocument);
- FPDF_DestroyLibrary();
aWriter.m_aStream.Seek(STREAM_SEEK_TO_BEGIN);
rOutStream.WriteStream(aWriter.m_aStream);
@@ -163,12 +159,7 @@ size_t RenderPDFBitmaps(const void* pBuffer, int nSize, std::vector<Bitmap>& rBi
const size_t nFirstPage, int nPages,
const double fResolutionDPI)
{
- 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);
@@ -217,7 +208,6 @@ size_t RenderPDFBitmaps(const void* pBuffer, int nSize, std::vector<Bitmap>& rBi
}
FPDF_CloseDocument(pPdfDocument);
- FPDF_DestroyLibrary();
return rBitmaps.size();
}
@@ -314,12 +304,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
@@ -357,7 +342,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..9d822c34642c
--- /dev/null
+++ b/vcl/source/pdf/PDFiumLibrary.cxx
@@ -0,0 +1,39 @@
+/* -*- 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
+{
+namespace 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 pdf
+} // end vcl
+
+#endif // HAVE_FEATURE_PDFIUM
+
+/* vim:set shiftwidth=4 softtabstop=4 expandtab: */