From 8e0c0dbc4876e661de6c344c759ffe22d3f13bce Mon Sep 17 00:00:00 2001 From: Andrew Weintraub Date: Tue, 17 Jul 2018 21:42:17 +0000 Subject: Add Deleter and Scoper for FPDF_PAGELINK. Change-Id: Ie997bfa5437d4222a381be3fc2b4334d6c8e2d5b Reviewed-on: https://pdfium-review.googlesource.com/38190 Reviewed-by: Tom Sepez Commit-Queue: Tom Sepez --- fpdfsdk/fpdf_text_embeddertest.cpp | 22 ++++++++++++++-------- public/cpp/fpdf_deleters.h | 6 ++++++ public/cpp/fpdf_scopers.h | 4 ++++ 3 files changed, 24 insertions(+), 8 deletions(-) diff --git a/fpdfsdk/fpdf_text_embeddertest.cpp b/fpdfsdk/fpdf_text_embeddertest.cpp index 112991f896..eafe1a2d11 100644 --- a/fpdfsdk/fpdf_text_embeddertest.cpp +++ b/fpdfsdk/fpdf_text_embeddertest.cpp @@ -7,6 +7,7 @@ #include #include "core/fxcrt/fx_memory.h" +#include "public/cpp/fpdf_scopers.h" #include "public/fpdf_text.h" #include "public/fpdf_transformpage.h" #include "public/fpdfview.h" @@ -321,16 +322,21 @@ TEST_F(FPDFTextEmbeddertest, WebLinks) { FPDF_TEXTPAGE textpage = FPDFText_LoadPage(page); ASSERT_TRUE(textpage); - FPDF_PAGELINK pagelink = FPDFLink_LoadWebLinks(textpage); - EXPECT_TRUE(pagelink); + { + ScopedFPDFPageLink pagelink(FPDFLink_LoadWebLinks(textpage)); + EXPECT_TRUE(pagelink); - // Page contains two HTTP-style URLs. - EXPECT_EQ(2, FPDFLink_CountWebLinks(pagelink)); + // Page contains two HTTP-style URLs. + EXPECT_EQ(2, FPDFLink_CountWebLinks(pagelink.get())); - // Only a terminating NUL required for bogus links. - EXPECT_EQ(1, FPDFLink_GetURL(pagelink, 2, nullptr, 0)); - EXPECT_EQ(1, FPDFLink_GetURL(pagelink, 1400, nullptr, 0)); - EXPECT_EQ(1, FPDFLink_GetURL(pagelink, -1, nullptr, 0)); + // Only a terminating NUL required for bogus links. + EXPECT_EQ(1, FPDFLink_GetURL(pagelink.get(), 2, nullptr, 0)); + EXPECT_EQ(1, FPDFLink_GetURL(pagelink.get(), 1400, nullptr, 0)); + EXPECT_EQ(1, FPDFLink_GetURL(pagelink.get(), -1, nullptr, 0)); + } + + FPDF_PAGELINK pagelink = FPDFLink_LoadWebLinks(textpage); + EXPECT_TRUE(pagelink); // Query the number of characters required for each link (incl NUL). EXPECT_EQ(25, FPDFLink_GetURL(pagelink, 0, nullptr, 0)); diff --git a/public/cpp/fpdf_deleters.h b/public/cpp/fpdf_deleters.h index 51e7cba7f8..d9d95cda3b 100644 --- a/public/cpp/fpdf_deleters.h +++ b/public/cpp/fpdf_deleters.h @@ -45,6 +45,12 @@ struct FPDFPageDeleter { inline void operator()(FPDF_PAGE page) { FPDF_ClosePage(page); } }; +struct FPDFPageLinkDeleter { + inline void operator()(FPDF_PAGELINK pagelink) { + FPDFLink_CloseWebLinks(pagelink); + } +}; + struct FPDFStructTreeDeleter { inline void operator()(FPDF_STRUCTTREE tree) { FPDF_StructTree_Close(tree); } }; diff --git a/public/cpp/fpdf_scopers.h b/public/cpp/fpdf_scopers.h index 1898ac887d..3601f47344 100644 --- a/public/cpp/fpdf_scopers.h +++ b/public/cpp/fpdf_scopers.h @@ -44,6 +44,10 @@ using ScopedFPDFTextPage = using ScopedFPDFPage = std::unique_ptr::type, FPDFPageDeleter>; +using ScopedFPDFPageLink = + std::unique_ptr::type, + FPDFPageLinkDeleter>; + using ScopedFPDFStructTree = std::unique_ptr::type, FPDFStructTreeDeleter>; -- cgit v1.2.3