Commit ad577c62 authored by Lei Zhang's avatar Lei Zhang Committed by Commit Bot

Add PDFiumPage::SetIsValidLinkFunctionForTesting().

Make it possible to write pdf_unittests test cases that call
PDFiumPage::CalculateLinks() without requiring PPAPI initialization.

Change-Id: I8433baf810128039efa4705a8862b95f97efd9ac
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1725427Reviewed-by: default avatarHenrique Nakashima <hnakashima@chromium.org>
Commit-Queue: Lei Zhang <thestig@chromium.org>
Cr-Commit-Position: refs/heads/master@{#683727}
parent 226a360f
...@@ -27,8 +27,21 @@ using printing::ConvertUnitDouble; ...@@ -27,8 +27,21 @@ using printing::ConvertUnitDouble;
using printing::kPointsPerInch; using printing::kPointsPerInch;
using printing::kPixelsPerInch; using printing::kPixelsPerInch;
namespace chrome_pdf {
namespace { namespace {
PDFiumPage::IsValidLinkFunction g_is_valid_link_func_for_testing = nullptr;
// If the link cannot be converted to a pp::Var, then it is not possible to
// pass it to JS. In this case, ignore the link like other PDF viewers.
// See https://crbug.com/312882 for an example.
// TODO(crbug.com/702993): Get rid of the PPAPI usage here, as well as
// SetIsValidLinkFunctionForTesting() and related code.
bool IsValidLink(const std::string& url) {
return pp::Var(url).is_string();
}
pp::FloatRect FloatPageRectToPixelRect(FPDF_PAGE page, pp::FloatRect FloatPageRectToPixelRect(FPDF_PAGE page,
const pp::FloatRect& input) { const pp::FloatRect& input) {
int output_width = FPDF_GetPageWidth(page); int output_width = FPDF_GetPageWidth(page);
...@@ -81,8 +94,6 @@ bool OverlapsOnYAxis(const pp::FloatRect& a, const pp::FloatRect& b) { ...@@ -81,8 +94,6 @@ bool OverlapsOnYAxis(const pp::FloatRect& a, const pp::FloatRect& b) {
} // namespace } // namespace
namespace chrome_pdf {
PDFiumPage::LinkTarget::LinkTarget() : page(-1) {} PDFiumPage::LinkTarget::LinkTarget() : page(-1) {}
PDFiumPage::LinkTarget::LinkTarget(const LinkTarget& other) = default; PDFiumPage::LinkTarget::LinkTarget(const LinkTarget& other) = default;
...@@ -104,6 +115,12 @@ PDFiumPage::~PDFiumPage() { ...@@ -104,6 +115,12 @@ PDFiumPage::~PDFiumPage() {
DCHECK_EQ(0, preventing_unload_count_); DCHECK_EQ(0, preventing_unload_count_);
} }
// static
void PDFiumPage::SetIsValidLinkFunctionForTesting(
IsValidLinkFunction function) {
g_is_valid_link_func_for_testing = function;
}
void PDFiumPage::Unload() { void PDFiumPage::Unload() {
// Do not unload while in the middle of a load. // Do not unload while in the middle of a load.
if (preventing_unload_count_) if (preventing_unload_count_)
...@@ -444,11 +461,10 @@ void PDFiumPage::CalculateLinks() { ...@@ -444,11 +461,10 @@ void PDFiumPage::CalculateLinks() {
Link link; Link link;
link.url = base::UTF16ToUTF8(url); link.url = base::UTF16ToUTF8(url);
// If the link cannot be converted to a pp::Var, then it is not possible to IsValidLinkFunction is_valid_link_func =
// pass it to JS. In this case, ignore the link like other PDF viewers. g_is_valid_link_func_for_testing ? g_is_valid_link_func_for_testing
// See http://crbug.com/312882 for an example. : &IsValidLink;
pp::Var link_var(link.url); if (!is_valid_link_func(link.url))
if (!link_var.is_string())
continue; continue;
// Make sure all the characters in the URL are valid per RFC 1738. // Make sure all the characters in the URL are valid per RFC 1738.
......
...@@ -29,6 +29,9 @@ class PDFiumPage { ...@@ -29,6 +29,9 @@ class PDFiumPage {
PDFiumPage(PDFiumPage&& that); PDFiumPage(PDFiumPage&& that);
~PDFiumPage(); ~PDFiumPage();
using IsValidLinkFunction = bool (*)(const std::string& url);
static void SetIsValidLinkFunctionForTesting(IsValidLinkFunction function);
// Unloads the PDFium data for this page from memory. // Unloads the PDFium data for this page from memory.
void Unload(); void Unload();
// Gets the FPDF_PAGE for this page, loading and parsing it if necessary. // Gets the FPDF_PAGE for this page, loading and parsing it if necessary.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment