1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "pdf/pdfium/pdfium_engine.h"
6 
7 #include <math.h>
8 #include <stddef.h>
9 #include <stdint.h>
10 
11 #include <algorithm>
12 #include <limits>
13 #include <memory>
14 #include <set>
15 #include <string>
16 #include <utility>
17 
18 #include "base/auto_reset.h"
19 #include "base/bind.h"
20 #include "base/check_op.h"
21 #include "base/debug/alias.h"
22 #include "base/feature_list.h"
23 #include "base/notreached.h"
24 #include "base/stl_util.h"
25 #include "base/strings/string_util.h"
26 #include "base/strings/stringprintf.h"
27 #include "base/strings/utf_string_conversions.h"
28 #include "base/threading/thread_task_runner_handle.h"
29 #include "build/build_config.h"
30 #include "gin/array_buffer.h"
31 #include "gin/public/gin_embedders.h"
32 #include "gin/public/isolate_holder.h"
33 #include "gin/public/v8_platform.h"
34 #include "pdf/document_loader_impl.h"
35 #include "pdf/draw_utils/coordinates.h"
36 #include "pdf/draw_utils/shadow.h"
37 #include "pdf/pdf_features.h"
38 #include "pdf/pdf_transform.h"
39 #include "pdf/pdfium/pdfium_api_string_buffer_adapter.h"
40 #include "pdf/pdfium/pdfium_document.h"
41 #include "pdf/pdfium/pdfium_mem_buffer_file_read.h"
42 #include "pdf/pdfium/pdfium_mem_buffer_file_write.h"
43 #include "pdf/pdfium/pdfium_permissions.h"
44 #include "pdf/pdfium/pdfium_unsupported_features.h"
45 #include "pdf/ppapi_migration/bitmap.h"
46 #include "pdf/ppapi_migration/geometry_conversions.h"
47 #include "pdf/ppapi_migration/input_event_conversions.h"
48 #include "pdf/ppapi_migration/url_loader.h"
49 #include "pdf/url_loader_wrapper_impl.h"
50 #include "ppapi/cpp/instance.h"
51 #include "ppapi/cpp/private/pdf.h"
52 #include "printing/units.h"
53 #include "third_party/pdfium/public/cpp/fpdf_scopers.h"
54 #include "third_party/pdfium/public/fpdf_annot.h"
55 #include "third_party/pdfium/public/fpdf_attachment.h"
56 #include "third_party/pdfium/public/fpdf_catalog.h"
57 #include "third_party/pdfium/public/fpdf_ext.h"
58 #include "third_party/pdfium/public/fpdf_fwlevent.h"
59 #include "third_party/pdfium/public/fpdf_ppo.h"
60 #include "third_party/pdfium/public/fpdf_searchex.h"
61 #include "third_party/skia/include/core/SkBitmap.h"
62 #include "ui/events/keycodes/keyboard_codes.h"
63 #include "ui/gfx/geometry/point.h"
64 #include "ui/gfx/geometry/point_conversions.h"
65 #include "ui/gfx/geometry/rect.h"
66 #include "ui/gfx/geometry/rect_f.h"
67 #include "ui/gfx/geometry/size.h"
68 #include "ui/gfx/geometry/vector2d.h"
69 #include "v8/include/v8.h"
70 
71 #if defined(OS_LINUX) || defined(OS_CHROMEOS) || defined(OS_BSD)
72 #include "pdf/pdfium/pdfium_font_linux.h"
73 #endif
74 
75 using printing::ConvertUnit;
76 using printing::ConvertUnitDouble;
77 using printing::kPixelsPerInch;
78 using printing::kPointsPerInch;
79 
80 namespace chrome_pdf {
81 
82 namespace {
83 
84 constexpr int32_t kHighlightColorR = 153;
85 constexpr int32_t kHighlightColorG = 193;
86 constexpr int32_t kHighlightColorB = 218;
87 
88 constexpr uint32_t kPendingPageColor = 0xFFEEEEEE;
89 
90 constexpr uint32_t kFormHighlightColor = 0xFFE4DD;
91 constexpr int32_t kFormHighlightAlpha = 100;
92 
93 constexpr int kMaxPasswordTries = 3;
94 
95 constexpr base::TimeDelta kTouchLongPressTimeout =
96     base::TimeDelta::FromMilliseconds(300);
97 
98 // Windows has native panning capabilities. No need to use our own.
99 #if defined(OS_WIN)
100 constexpr bool kViewerImplementedPanning = false;
101 #else
102 constexpr bool kViewerImplementedPanning = true;
103 #endif
104 
105 constexpr int32_t kLoadingTextVerticalOffset = 50;
106 
107 // The maximum amount of time we'll spend doing a paint before we give back
108 // control of the thread.
109 constexpr base::TimeDelta kMaxProgressivePaintTime =
110     base::TimeDelta::FromMilliseconds(300);
111 
112 // The maximum amount of time we'll spend doing the first paint. This is less
113 // than the above to keep things smooth if the user is scrolling quickly. This
114 // is set to 250 ms to give enough time for most PDFs to render, while avoiding
115 // adding too much latency to the display of the final image when the user
116 // stops scrolling.
117 // Setting a higher value has minimal benefit (scrolling at less than 4 fps will
118 // never be a great experience) and there is some cost, since when the user
119 // stops scrolling the in-progress painting has to complete or timeout before
120 // the final painting can start.
121 // The scrollbar will always be responsive since it is managed by a separate
122 // process.
123 constexpr base::TimeDelta kMaxInitialProgressivePaintTime =
124     base::TimeDelta::FromMilliseconds(250);
125 
126 template <class S>
IsAboveOrDirectlyLeftOf(const S & lhs,const S & rhs)127 bool IsAboveOrDirectlyLeftOf(const S& lhs, const S& rhs) {
128   return lhs.y() < rhs.y() || (lhs.y() == rhs.y() && lhs.x() < rhs.x());
129 }
130 
CalculateCenterForZoom(int center,int length,double zoom)131 int CalculateCenterForZoom(int center, int length, double zoom) {
132   int adjusted_center =
133       static_cast<int>(center * zoom) - static_cast<int>(length * zoom / 2);
134   return std::max(adjusted_center, 0);
135 }
136 
137 // This formats a string with special 0xfffe end-of-line hyphens the same way
138 // as Adobe Reader. When a hyphen is encountered, the next non-CR/LF whitespace
139 // becomes CR+LF and the hyphen is erased. If there is no whitespace between
140 // two hyphens, the latter hyphen is erased and ignored.
FormatStringWithHyphens(base::string16 * text)141 void FormatStringWithHyphens(base::string16* text) {
142   // First pass marks all the hyphen positions.
143   struct HyphenPosition {
144     HyphenPosition() : position(0), next_whitespace_position(0) {}
145     size_t position;
146     size_t next_whitespace_position;  // 0 for none
147   };
148   std::vector<HyphenPosition> hyphen_positions;
149   HyphenPosition current_hyphen_position;
150   bool current_hyphen_position_is_valid = false;
151   constexpr base::char16 kPdfiumHyphenEOL = 0xfffe;
152 
153   for (size_t i = 0; i < text->size(); ++i) {
154     const base::char16& current_char = (*text)[i];
155     if (current_char == kPdfiumHyphenEOL) {
156       if (current_hyphen_position_is_valid)
157         hyphen_positions.push_back(current_hyphen_position);
158       current_hyphen_position = HyphenPosition();
159       current_hyphen_position.position = i;
160       current_hyphen_position_is_valid = true;
161     } else if (base::IsUnicodeWhitespace(current_char)) {
162       if (current_hyphen_position_is_valid) {
163         if (current_char != L'\r' && current_char != L'\n')
164           current_hyphen_position.next_whitespace_position = i;
165         hyphen_positions.push_back(current_hyphen_position);
166         current_hyphen_position_is_valid = false;
167       }
168     }
169   }
170   if (current_hyphen_position_is_valid)
171     hyphen_positions.push_back(current_hyphen_position);
172 
173   // With all the hyphen positions, do the search and replace.
174   while (!hyphen_positions.empty()) {
175     static constexpr base::char16 kCr[] = {L'\r', L'\0'};
176     const HyphenPosition& position = hyphen_positions.back();
177     if (position.next_whitespace_position != 0) {
178       (*text)[position.next_whitespace_position] = L'\n';
179       text->insert(position.next_whitespace_position, kCr);
180     }
181     text->erase(position.position, 1);
182     hyphen_positions.pop_back();
183   }
184 
185   // Adobe Reader also get rid of trailing spaces right before a CRLF.
186   static constexpr base::char16 kSpaceCrCn[] = {L' ', L'\r', L'\n', L'\0'};
187   static constexpr base::char16 kCrCn[] = {L'\r', L'\n', L'\0'};
188   base::ReplaceSubstringsAfterOffset(text, 0, kSpaceCrCn, kCrCn);
189 }
190 
191 // Replace CR/LF with just LF on POSIX.
FormatStringForOS(base::string16 * text)192 void FormatStringForOS(base::string16* text) {
193 #if defined(OS_POSIX)
194   static constexpr base::char16 kCr[] = {L'\r', L'\0'};
195   static constexpr base::char16 kBlank[] = {L'\0'};
196   base::ReplaceChars(*text, kCr, kBlank, text);
197 #elif defined(OS_WIN)
198   // Do nothing
199 #else
200   NOTIMPLEMENTED();
201 #endif
202 }
203 
204 // Returns true if |cur| is a character to break on.
205 // For double clicks, look for work breaks.
206 // For triple clicks, look for line breaks.
207 // The actual algorithm used in Blink is much more complicated, so do a simple
208 // approximation.
FindMultipleClickBoundary(bool is_double_click,base::char16 cur)209 bool FindMultipleClickBoundary(bool is_double_click, base::char16 cur) {
210   if (!is_double_click)
211     return cur == '\n';
212 
213   // Deal with ASCII characters.
214   if (base::IsAsciiAlpha(cur) || base::IsAsciiDigit(cur) || cur == '_')
215     return false;
216   if (cur < 128)
217     return true;
218 
219   if (cur == kZeroWidthSpace)
220     return true;
221 
222   return false;
223 }
224 
225 #if defined(PDF_ENABLE_V8)
226 gin::IsolateHolder* g_isolate_holder = nullptr;
227 
IsV8Initialized()228 bool IsV8Initialized() {
229   return !!g_isolate_holder;
230 }
231 
SetUpV8()232 void SetUpV8() {
233   const char* recommended = FPDF_GetRecommendedV8Flags();
234   v8::V8::SetFlagsFromString(recommended, strlen(recommended));
235   gin::IsolateHolder::Initialize(
236       gin::IsolateHolder::kNonStrictMode,
237       static_cast<v8::ArrayBuffer::Allocator*>(
238           FPDF_GetArrayBufferAllocatorSharedInstance()));
239   DCHECK(!g_isolate_holder);
240   g_isolate_holder = new gin::IsolateHolder(
241       base::ThreadTaskRunnerHandle::Get(), gin::IsolateHolder::kSingleThread,
242       gin::IsolateHolder::IsolateType::kUtility);
243   g_isolate_holder->isolate()->Enter();
244 }
245 
TearDownV8()246 void TearDownV8() {
247   g_isolate_holder->isolate()->Exit();
248   delete g_isolate_holder;
249   g_isolate_holder = nullptr;
250 }
251 #endif  // defined(PDF_ENABLE_V8)
252 
253 // Returns true if the given |area| and |form_type| combination from
254 // PDFiumEngine::GetCharIndex() indicates it is a form text area.
IsFormTextArea(PDFiumPage::Area area,int form_type)255 bool IsFormTextArea(PDFiumPage::Area area, int form_type) {
256   if (form_type == FPDF_FORMFIELD_UNKNOWN)
257     return false;
258 
259   DCHECK_EQ(area, PDFiumPage::FormTypeToArea(form_type));
260   return area == PDFiumPage::FORM_TEXT_AREA;
261 }
262 
263 // Checks whether or not focus is in an editable form text area given the
264 // form field annotation flags and form type.
CheckIfEditableFormTextArea(int flags,int form_type)265 bool CheckIfEditableFormTextArea(int flags, int form_type) {
266   if (!!(flags & FPDF_FORMFLAG_READONLY))
267     return false;
268   if (form_type == FPDF_FORMFIELD_TEXTFIELD)
269     return true;
270   if (form_type == FPDF_FORMFIELD_COMBOBOX &&
271       (!!(flags & FPDF_FORMFLAG_CHOICE_EDIT))) {
272     return true;
273   }
274   return false;
275 }
276 
IsLinkArea(PDFiumPage::Area area)277 bool IsLinkArea(PDFiumPage::Area area) {
278   return area == PDFiumPage::WEBLINK_AREA || area == PDFiumPage::DOCLINK_AREA;
279 }
280 
281 // Normalize a MouseInputEvent. For Mac, this means transforming ctrl + left
282 // button down events into a right button down events.
NormalizeMouseEvent(const MouseInputEvent & event)283 MouseInputEvent NormalizeMouseEvent(const MouseInputEvent& event) {
284   MouseInputEvent normalized_event = event;
285 #if defined(OS_MAC)
286   uint32_t modifiers = event.GetModifiers();
287   if ((event.GetModifiers() & kInputEventModifierControlKey) &&
288       event.GetButton() == InputEventMouseButtonType::kLeft &&
289       event.GetEventType() == InputEventType::kMouseDown) {
290     uint32_t new_modifiers = modifiers & ~kInputEventModifierControlKey;
291     normalized_event =
292         MouseInputEvent(InputEventType::kMouseDown, event.GetTimeStamp(),
293                         new_modifiers, InputEventMouseButtonType::kRight,
294                         event.GetPosition(), 1, event.GetMovement());
295   }
296 #endif
297   return normalized_event;
298 }
299 
300 // These values are intended for the JS to handle, and it doesn't have access
301 // to the PDFDEST_VIEW_* defines.
ConvertViewIntToViewString(unsigned long view_int)302 std::string ConvertViewIntToViewString(unsigned long view_int) {
303   switch (view_int) {
304     case PDFDEST_VIEW_XYZ:
305       return "XYZ";
306     case PDFDEST_VIEW_FIT:
307       return "Fit";
308     case PDFDEST_VIEW_FITH:
309       return "FitH";
310     case PDFDEST_VIEW_FITV:
311       return "FitV";
312     case PDFDEST_VIEW_FITR:
313       return "FitR";
314     case PDFDEST_VIEW_FITB:
315       return "FitB";
316     case PDFDEST_VIEW_FITBH:
317       return "FitBH";
318     case PDFDEST_VIEW_FITBV:
319       return "FitBV";
320     case PDFDEST_VIEW_UNKNOWN_MODE:
321       return "";
322     default:
323       NOTREACHED();
324       return "";
325   }
326 }
327 
328 // Simplify to \" for searching
329 constexpr wchar_t kHebrewPunctuationGershayimCharacter = 0x05F4;
330 constexpr wchar_t kLeftDoubleQuotationMarkCharacter = 0x201C;
331 constexpr wchar_t kRightDoubleQuotationMarkCharacter = 0x201D;
332 
333 // Simplify \' for searching
334 constexpr wchar_t kHebrewPunctuationGereshCharacter = 0x05F3;
335 constexpr wchar_t kLeftSingleQuotationMarkCharacter = 0x2018;
336 constexpr wchar_t kRightSingleQuotationMarkCharacter = 0x2019;
337 
SimplifyForSearch(wchar_t c)338 wchar_t SimplifyForSearch(wchar_t c) {
339   switch (c) {
340     case kHebrewPunctuationGershayimCharacter:
341     case kLeftDoubleQuotationMarkCharacter:
342     case kRightDoubleQuotationMarkCharacter:
343       return L'\"';
344     case kHebrewPunctuationGereshCharacter:
345     case kLeftSingleQuotationMarkCharacter:
346     case kRightSingleQuotationMarkCharacter:
347       return L'\'';
348     default:
349       return c;
350   }
351 }
352 
GetAnnotationFocusType(FPDF_ANNOTATION_SUBTYPE annot_type)353 PP_PrivateFocusObjectType GetAnnotationFocusType(
354     FPDF_ANNOTATION_SUBTYPE annot_type) {
355   switch (annot_type) {
356     case FPDF_ANNOT_LINK:
357       return PP_PrivateFocusObjectType::PP_PRIVATEFOCUSOBJECT_LINK;
358     case FPDF_ANNOT_HIGHLIGHT:
359       return PP_PrivateFocusObjectType::PP_PRIVATEFOCUSOBJECT_HIGHLIGHT;
360     case FPDF_ANNOT_WIDGET:
361       return PP_PrivateFocusObjectType::PP_PRIVATEFOCUSOBJECT_TEXT_FIELD;
362     default:
363       return PP_PrivateFocusObjectType::PP_PRIVATEFOCUSOBJECT_NONE;
364   }
365 }
366 
GetAttachmentAttribute(FPDF_ATTACHMENT attachment,FPDF_BYTESTRING field)367 base::string16 GetAttachmentAttribute(FPDF_ATTACHMENT attachment,
368                                       FPDF_BYTESTRING field) {
369   return CallPDFiumWideStringBufferApi(
370       base::BindRepeating(&FPDFAttachment_GetStringValue, attachment, field),
371       /*check_expected_size=*/true);
372 }
373 
GetAttachmentName(FPDF_ATTACHMENT attachment)374 base::string16 GetAttachmentName(FPDF_ATTACHMENT attachment) {
375   return CallPDFiumWideStringBufferApi(
376       base::BindRepeating(&FPDFAttachment_GetName, attachment),
377       /*check_expected_size=*/true);
378 }
379 
380 }  // namespace
381 
InitializeSDK(bool enable_v8)382 void InitializeSDK(bool enable_v8) {
383   FPDF_LIBRARY_CONFIG config;
384   config.version = 3;
385   config.m_pUserFontPaths = nullptr;
386   config.m_pIsolate = nullptr;
387   config.m_pPlatform = nullptr;
388   config.m_v8EmbedderSlot = gin::kEmbedderPDFium;
389 
390 #if defined(PDF_ENABLE_V8)
391   if (enable_v8) {
392     SetUpV8();
393     config.m_pIsolate = v8::Isolate::GetCurrent();
394     // NOTE: static_cast<> prior to assigning to (void*) is safer since it
395     // will manipulate the pointer value should gin::V8Platform someday have
396     // multiple base classes.
397     config.m_pPlatform = static_cast<v8::Platform*>(gin::V8Platform::Get());
398   }
399 #endif  // defined(PDF_ENABLE_V8)
400 
401   FPDF_InitLibraryWithConfig(&config);
402 
403 #if defined(OS_LINUX) || defined(OS_CHROMEOS) || defined(OS_BSD)
404   InitializeLinuxFontMapper();
405 #endif
406 
407   InitializeUnsupportedFeaturesHandler();
408 }
409 
ShutdownSDK()410 void ShutdownSDK() {
411   FPDF_DestroyLibrary();
412 #if defined(PDF_ENABLE_V8)
413   if (IsV8Initialized())
414     TearDownV8();
415 #endif  // defined(PDF_ENABLE_V8)
416 }
417 
418 PDFEngine::AccessibilityLinkInfo::AccessibilityLinkInfo() = default;
419 
420 PDFEngine::AccessibilityLinkInfo::AccessibilityLinkInfo(
421     const AccessibilityLinkInfo& that) = default;
422 
423 PDFEngine::AccessibilityLinkInfo::~AccessibilityLinkInfo() = default;
424 
425 PDFEngine::AccessibilityImageInfo::AccessibilityImageInfo() = default;
426 
427 PDFEngine::AccessibilityImageInfo::AccessibilityImageInfo(
428     const AccessibilityImageInfo& that) = default;
429 
430 PDFEngine::AccessibilityImageInfo::~AccessibilityImageInfo() = default;
431 
432 PDFEngine::AccessibilityHighlightInfo::AccessibilityHighlightInfo() = default;
433 
434 PDFEngine::AccessibilityHighlightInfo::AccessibilityHighlightInfo(
435     const AccessibilityHighlightInfo& that) = default;
436 
437 PDFEngine::AccessibilityHighlightInfo::~AccessibilityHighlightInfo() = default;
438 
439 PDFEngine::AccessibilityTextFieldInfo::AccessibilityTextFieldInfo() = default;
440 
441 PDFEngine::AccessibilityTextFieldInfo::AccessibilityTextFieldInfo(
442     const AccessibilityTextFieldInfo& that) = default;
443 
444 PDFEngine::AccessibilityTextFieldInfo::~AccessibilityTextFieldInfo() = default;
445 
PDFiumEngine(PDFEngine::Client * client,PDFiumFormFiller::ScriptOption script_option)446 PDFiumEngine::PDFiumEngine(PDFEngine::Client* client,
447                            PDFiumFormFiller::ScriptOption script_option)
448     : client_(client),
449       form_filler_(this, script_option),
450       mouse_down_state_(PDFiumPage::NONSELECTABLE_AREA,
451                         PDFiumPage::LinkTarget()),
452       print_(this) {
453 #if defined(PDF_ENABLE_V8)
454   if (script_option != PDFiumFormFiller::ScriptOption::kNoJavaScript)
455     DCHECK(IsV8Initialized());
456 #endif  // defined(PDF_ENABLE_V8)
457 
458   IFSDK_PAUSE::version = 1;
459   IFSDK_PAUSE::user = nullptr;
460   IFSDK_PAUSE::NeedToPauseNow = Pause_NeedToPauseNow;
461 
462 #if defined(OS_LINUX) || defined(OS_CHROMEOS) || defined(OS_BSD)
463   // PreviewModeClient does not know its pp::Instance.
464   SetLastInstance(client_->GetPluginInstance());
465 #endif
466 }
467 
~PDFiumEngine()468 PDFiumEngine::~PDFiumEngine() {
469   for (auto& page : pages_)
470     page->Unload();
471 
472   if (doc())
473     FORM_DoDocumentAAction(form(), FPDFDOC_AACTION_WC);
474 }
475 
SetDocumentLoaderForTesting(std::unique_ptr<DocumentLoader> loader)476 void PDFiumEngine::SetDocumentLoaderForTesting(
477     std::unique_ptr<DocumentLoader> loader) {
478   DCHECK(loader);
479   DCHECK(!doc_loader_);
480   doc_loader_ = std::move(loader);
481   doc_loader_set_for_testing_ = true;
482 }
483 
New(const char * url,const char * headers)484 bool PDFiumEngine::New(const char* url, const char* headers) {
485   url_ = url;
486   if (headers)
487     headers_ = headers;
488   else
489     headers_.clear();
490   return true;
491 }
492 
PageOffsetUpdated(const gfx::Vector2d & page_offset)493 void PDFiumEngine::PageOffsetUpdated(const gfx::Vector2d& page_offset) {
494   page_offset_ = page_offset;
495 }
496 
PluginSizeUpdated(const gfx::Size & size)497 void PDFiumEngine::PluginSizeUpdated(const gfx::Size& size) {
498   CancelPaints();
499 
500   plugin_size_ = size;
501   CalculateVisiblePages();
502 }
503 
ScrolledToXPosition(int position)504 void PDFiumEngine::ScrolledToXPosition(int position) {
505   CancelPaints();
506 
507   int old_x = position_.x();
508   position_.set_x(position);
509   CalculateVisiblePages();
510   client_->DidScroll(gfx::Vector2d(old_x - position, 0));
511   OnSelectionPositionChanged();
512 }
513 
ScrolledToYPosition(int position)514 void PDFiumEngine::ScrolledToYPosition(int position) {
515   CancelPaints();
516 
517   int old_y = position_.y();
518   position_.set_y(position);
519   CalculateVisiblePages();
520   client_->DidScroll(gfx::Vector2d(0, old_y - position));
521   OnSelectionPositionChanged();
522 }
523 
PrePaint()524 void PDFiumEngine::PrePaint() {
525   for (auto& paint : progressive_paints_)
526     paint.set_painted(false);
527 }
528 
Paint(const gfx::Rect & rect,SkBitmap & image_data,std::vector<gfx::Rect> & ready,std::vector<gfx::Rect> & pending)529 void PDFiumEngine::Paint(const gfx::Rect& rect,
530                          SkBitmap& image_data,
531                          std::vector<gfx::Rect>& ready,
532                          std::vector<gfx::Rect>& pending) {
533   gfx::Rect leftover = rect;
534   for (size_t i = 0; i < visible_pages_.size(); ++i) {
535     int index = visible_pages_[i];
536     // Convert the current page's rectangle to screen rectangle.  We do this
537     // instead of the reverse (converting the dirty rectangle from screen to
538     // page coordinates) because then we'd have to convert back to screen
539     // coordinates, and the rounding errors sometime leave pixels dirty or even
540     // move the text up or down a pixel when zoomed.
541     gfx::Rect page_rect_in_screen = GetPageScreenRect(index);
542     gfx::Rect dirty_in_screen =
543         gfx::IntersectRects(page_rect_in_screen, leftover);
544     if (dirty_in_screen.IsEmpty())
545       continue;
546 
547     // Compute the leftover dirty region. The first page may have blank space
548     // above it, in which case we also need to subtract that space from the
549     // dirty region.
550     // If two-up view is enabled, we don't need to recompute |leftover| since
551     // subtracting |leftover| with a two-up view page won't result in a
552     // rectangle.
553     if (!layout_.options().two_up_view_enabled()) {
554       if (i == 0) {
555         gfx::Rect blank_space_in_screen = dirty_in_screen;
556         blank_space_in_screen.set_y(0);
557         blank_space_in_screen.set_height(dirty_in_screen.y());
558         leftover.Subtract(blank_space_in_screen);
559       }
560 
561       leftover.Subtract(dirty_in_screen);
562     }
563 
564     if (pages_[index]->available()) {
565       int progressive = GetProgressiveIndex(index);
566       if (progressive != -1) {
567         DCHECK_GE(progressive, 0);
568         DCHECK_LT(static_cast<size_t>(progressive), progressive_paints_.size());
569         if (progressive_paints_[progressive].rect() != dirty_in_screen) {
570           // The PDFium code can only handle one progressive paint at a time, so
571           // queue this up. Previously we used to merge the rects when this
572           // happened, but it made scrolling up on complex PDFs very slow since
573           // there would be a damaged rect at the top (from scroll) and at the
574           // bottom (from toolbar).
575           pending.push_back(dirty_in_screen);
576           continue;
577         }
578       }
579 
580       if (progressive == -1) {
581         progressive = StartPaint(index, dirty_in_screen);
582         progressive_paint_timeout_ = kMaxInitialProgressivePaintTime;
583       } else {
584         progressive_paint_timeout_ = kMaxProgressivePaintTime;
585       }
586 
587       progressive_paints_[progressive].set_painted(true);
588       if (ContinuePaint(progressive, image_data)) {
589         FinishPaint(progressive, image_data);
590         ready.push_back(dirty_in_screen);
591       } else {
592         pending.push_back(dirty_in_screen);
593       }
594     } else {
595       PaintUnavailablePage(index, dirty_in_screen, image_data);
596       ready.push_back(dirty_in_screen);
597     }
598   }
599 }
600 
PostPaint()601 void PDFiumEngine::PostPaint() {
602   for (size_t i = 0; i < progressive_paints_.size(); ++i) {
603     if (progressive_paints_[i].painted())
604       continue;
605 
606     // This rectangle must have been merged with another one, that's why we
607     // weren't asked to paint it. Remove it or otherwise we'll never finish
608     // painting.
609     FPDF_RenderPage_Close(
610         pages_[progressive_paints_[i].page_index()]->GetPage());
611     progressive_paints_.erase(progressive_paints_.begin() + i);
612     --i;
613   }
614 }
615 
HandleDocumentLoad(std::unique_ptr<UrlLoader> loader)616 bool PDFiumEngine::HandleDocumentLoad(std::unique_ptr<UrlLoader> loader) {
617   password_tries_remaining_ = kMaxPasswordTries;
618   process_when_pending_request_complete_ =
619       base::FeatureList::IsEnabled(features::kPdfIncrementalLoading);
620 
621   if (!doc_loader_set_for_testing_) {
622     auto loader_wrapper =
623         std::make_unique<URLLoaderWrapperImpl>(std::move(loader));
624     loader_wrapper->SetResponseHeaders(headers_);
625 
626     doc_loader_ = std::make_unique<DocumentLoaderImpl>(this);
627     if (!doc_loader_->Init(std::move(loader_wrapper), url_))
628       return false;
629   }
630   document_ = std::make_unique<PDFiumDocument>(doc_loader_.get());
631 
632   // request initial data.
633   doc_loader_->RequestData(0, 1);
634   return true;
635 }
636 
GetPluginInstance()637 pp::Instance* PDFiumEngine::GetPluginInstance() {
638   return client_->GetPluginInstance();
639 }
640 
CreateURLLoader()641 std::unique_ptr<URLLoaderWrapper> PDFiumEngine::CreateURLLoader() {
642   return std::make_unique<URLLoaderWrapperImpl>(client_->CreateUrlLoader());
643 }
644 
AppendPage(PDFEngine * engine,int index)645 void PDFiumEngine::AppendPage(PDFEngine* engine, int index) {
646   // Unload and delete the blank page before appending.
647   pages_[index]->Unload();
648   pages_[index]->set_calculated_links(false);
649   gfx::Size curr_page_size = GetPageSize(index);
650   FPDFPage_Delete(doc(), index);
651   FPDF_ImportPages(doc(), static_cast<PDFiumEngine*>(engine)->doc(), "1",
652                    index);
653   gfx::Size new_page_size = GetPageSize(index);
654   if (curr_page_size != new_page_size) {
655     DCHECK(document_loaded_);
656     LoadPageInfo();
657   }
658   client_->Invalidate(GetPageScreenRect(index));
659 }
660 
GetSaveData()661 std::vector<uint8_t> PDFiumEngine::GetSaveData() {
662   PDFiumMemBufferFileWrite output_file_write;
663   if (!FPDF_SaveAsCopy(doc(), &output_file_write, 0))
664     return std::vector<uint8_t>();
665   return output_file_write.TakeBuffer();
666 }
667 
OnPendingRequestComplete()668 void PDFiumEngine::OnPendingRequestComplete() {
669   if (!process_when_pending_request_complete_)
670     return;
671 
672   if (!fpdf_availability()) {
673     document_->file_access().m_FileLen = doc_loader_->GetDocumentSize();
674     document_->CreateFPDFAvailability();
675     DCHECK(fpdf_availability());
676     // Currently engine does not deal efficiently with some non-linearized
677     // files.
678     // See http://code.google.com/p/chromium/issues/detail?id=59400
679     // To improve user experience we download entire file for non-linearized
680     // PDF.
681     if (FPDFAvail_IsLinearized(fpdf_availability()) != PDF_LINEARIZED) {
682       // Wait complete document.
683       process_when_pending_request_complete_ = false;
684       document_->ResetFPDFAvailability();
685       return;
686     }
687   }
688 
689   if (!doc()) {
690     LoadDocument();
691     return;
692   }
693 
694   if (pages_.empty()) {
695     LoadBody();
696     return;
697   }
698 
699   // LoadDocument() will result in |pending_pages_| being reset so there's no
700   // need to run the code below in that case.
701   bool update_pages = false;
702   std::vector<int> still_pending;
703   for (int pending_page : pending_pages_) {
704     if (CheckPageAvailable(pending_page, &still_pending)) {
705       update_pages = true;
706       if (IsPageVisible(pending_page))
707         client_->Invalidate(GetPageScreenRect(pending_page));
708     }
709   }
710   pending_pages_.swap(still_pending);
711   if (update_pages) {
712     DCHECK(!document_loaded_);
713     LoadPageInfo();
714   }
715 }
716 
OnNewDataReceived()717 void PDFiumEngine::OnNewDataReceived() {
718   client_->DocumentLoadProgress(doc_loader_->BytesReceived(),
719                                 doc_loader_->GetDocumentSize());
720 }
721 
OnDocumentComplete()722 void PDFiumEngine::OnDocumentComplete() {
723   if (doc())
724     return FinishLoadingDocument();
725 
726   document_->file_access().m_FileLen = doc_loader_->GetDocumentSize();
727   if (!fpdf_availability()) {
728     document_->CreateFPDFAvailability();
729     DCHECK(fpdf_availability());
730   }
731   LoadDocument();
732 }
733 
OnDocumentCanceled()734 void PDFiumEngine::OnDocumentCanceled() {
735   if (visible_pages_.empty())
736     client_->DocumentLoadFailed();
737   else
738     OnDocumentComplete();
739 }
740 
FinishLoadingDocument()741 void PDFiumEngine::FinishLoadingDocument() {
742   DCHECK(doc());
743   DCHECK(doc_loader_->IsDocumentComplete());
744 
745   LoadBody();
746 
747   FX_DOWNLOADHINTS& download_hints = document_->download_hints();
748   bool need_update = false;
749   for (size_t i = 0; i < pages_.size(); ++i) {
750     if (pages_[i]->available())
751       continue;
752 
753     pages_[i]->MarkAvailable();
754     // We still need to call IsPageAvail() even if the whole document is
755     // already downloaded.
756     FPDFAvail_IsPageAvail(fpdf_availability(), i, &download_hints);
757     need_update = true;
758     if (IsPageVisible(i))
759       client_->Invalidate(GetPageScreenRect(i));
760   }
761 
762   // Transition |document_loaded_| to true after finishing any calls to
763   // FPDFAvail_IsPageAvail(), since we no longer need to defer calls to this
764   // function from LoadPageInfo(). Note that LoadBody() calls LoadPageInfo()
765   // indirectly, so we cannot make this transition earlier.
766   document_loaded_ = true;
767 
768   if (need_update)
769     LoadPageInfo();
770 
771   LoadDocumentAttachmentInfoList();
772 
773   LoadDocumentMetadata();
774 
775   if (called_do_document_action_)
776     return;
777   called_do_document_action_ = true;
778 
779   // These can only be called now, as the JS might end up needing a page.
780   FORM_DoDocumentJSAction(form());
781   FORM_DoDocumentOpenAction(form());
782   if (most_visible_page_ != -1) {
783     FPDF_PAGE new_page = pages_[most_visible_page_]->GetPage();
784     FORM_DoPageAAction(new_page, form(), FPDFPAGE_AACTION_OPEN);
785   }
786 
787   if (doc()) {
788     DocumentFeatures document_features;
789     document_features.page_count = pages_.size();
790     document_features.has_attachments = !doc_attachment_info_list_.empty();
791     document_features.is_tagged = FPDFCatalog_IsTagged(doc());
792     document_features.form_type =
793         static_cast<FormType>(FPDF_GetFormType(doc()));
794     client_->DocumentLoadComplete(document_features);
795   }
796 }
797 
UnsupportedFeature(const std::string & feature)798 void PDFiumEngine::UnsupportedFeature(const std::string& feature) {
799   client_->DocumentHasUnsupportedFeature(feature);
800 }
801 
fpdf_availability() const802 FPDF_AVAIL PDFiumEngine::fpdf_availability() const {
803   return document_ ? document_->fpdf_availability() : nullptr;
804 }
805 
doc() const806 FPDF_DOCUMENT PDFiumEngine::doc() const {
807   return document_ ? document_->doc() : nullptr;
808 }
809 
form() const810 FPDF_FORMHANDLE PDFiumEngine::form() const {
811   return document_ ? document_->form() : nullptr;
812 }
813 
IsValidLink(const std::string & url)814 bool PDFiumEngine::IsValidLink(const std::string& url) {
815   return client_->IsValidLink(url);
816 }
817 
ContinueFind(int32_t result)818 void PDFiumEngine::ContinueFind(int32_t result) {
819   StartFind(current_find_text_, result != 0);
820 }
821 
HandleEvent(const InputEvent & event)822 bool PDFiumEngine::HandleEvent(const InputEvent& event) {
823   DCHECK(!defer_page_unload_);
824   defer_page_unload_ = true;
825   bool rv = false;
826   switch (event.GetEventType()) {
827     case InputEventType::kMouseDown:
828       rv = OnMouseDown(static_cast<const MouseInputEvent&>(event));
829       break;
830     case InputEventType::kMouseUp:
831       rv = OnMouseUp(static_cast<const MouseInputEvent&>(event));
832       break;
833     case InputEventType::kMouseMove:
834       rv = OnMouseMove(static_cast<const MouseInputEvent&>(event));
835       break;
836     case InputEventType::kMouseEnter:
837       OnMouseEnter(static_cast<const MouseInputEvent&>(event));
838       break;
839     case InputEventType::kKeyDown:
840       rv = OnKeyDown(static_cast<const KeyboardInputEvent&>(event));
841       break;
842     case InputEventType::kKeyUp:
843       rv = OnKeyUp(static_cast<const KeyboardInputEvent&>(event));
844       break;
845     case InputEventType::kChar:
846       rv = OnChar(static_cast<const KeyboardInputEvent&>(event));
847       break;
848     case InputEventType::kTouchStart: {
849       KillTouchTimer();
850 
851       const auto& touch_event = static_cast<const TouchInputEvent&>(event);
852       if (touch_event.GetTouchCount() == 1)
853         ScheduleTouchTimer(touch_event);
854       break;
855     }
856     case InputEventType::kTouchEnd:
857       KillTouchTimer();
858       break;
859     case InputEventType::kTouchMove:
860       // TODO(dsinclair): This should allow a little bit of movement (up to the
861       // touch radii) to account for finger jiggle.
862       KillTouchTimer();
863       break;
864     default:
865       break;
866   }
867 
868   DCHECK(defer_page_unload_);
869   defer_page_unload_ = false;
870 
871   // Store the pages to unload away because the act of unloading pages can cause
872   // there to be more pages to unload. We leave those extra pages to be unloaded
873   // on the next go around.
874   std::vector<int> pages_to_unload;
875   std::swap(pages_to_unload, deferred_page_unloads_);
876   for (int page_index : pages_to_unload)
877     pages_[page_index]->Unload();
878 
879   return rv;
880 }
881 
QuerySupportedPrintOutputFormats()882 uint32_t PDFiumEngine::QuerySupportedPrintOutputFormats() {
883   if (HasPermission(PERMISSION_PRINT_HIGH_QUALITY))
884     return PP_PRINTOUTPUTFORMAT_PDF | PP_PRINTOUTPUTFORMAT_RASTER;
885   if (HasPermission(PERMISSION_PRINT_LOW_QUALITY))
886     return PP_PRINTOUTPUTFORMAT_RASTER;
887   return 0;
888 }
889 
PrintBegin()890 void PDFiumEngine::PrintBegin() {
891   FORM_DoDocumentAAction(form(), FPDFDOC_AACTION_WP);
892 }
893 
PrintPages(const PP_PrintPageNumberRange_Dev * page_ranges,uint32_t page_range_count,const PP_PrintSettings_Dev & print_settings,const PP_PdfPrintSettings_Dev & pdf_print_settings)894 pp::Resource PDFiumEngine::PrintPages(
895     const PP_PrintPageNumberRange_Dev* page_ranges,
896     uint32_t page_range_count,
897     const PP_PrintSettings_Dev& print_settings,
898     const PP_PdfPrintSettings_Dev& pdf_print_settings) {
899   if (!page_range_count)
900     return pp::Resource();
901 
902   if ((print_settings.format & PP_PRINTOUTPUTFORMAT_PDF) &&
903       HasPermission(PERMISSION_PRINT_HIGH_QUALITY)) {
904     return PrintPagesAsPdf(page_ranges, page_range_count, print_settings,
905                            pdf_print_settings);
906   }
907   if (HasPermission(PERMISSION_PRINT_LOW_QUALITY)) {
908     return PrintPagesAsRasterPdf(page_ranges, page_range_count, print_settings,
909                                  pdf_print_settings);
910   }
911   return pp::Resource();
912 }
913 
PrintPagesAsRasterPdf(const PP_PrintPageNumberRange_Dev * page_ranges,uint32_t page_range_count,const PP_PrintSettings_Dev & print_settings,const PP_PdfPrintSettings_Dev & pdf_print_settings)914 pp::Buffer_Dev PDFiumEngine::PrintPagesAsRasterPdf(
915     const PP_PrintPageNumberRange_Dev* page_ranges,
916     uint32_t page_range_count,
917     const PP_PrintSettings_Dev& print_settings,
918     const PP_PdfPrintSettings_Dev& pdf_print_settings) {
919   DCHECK(page_range_count);
920 
921   // If document is not downloaded yet, disable printing.
922   if (doc() && !doc_loader_->IsDocumentComplete())
923     return pp::Buffer_Dev();
924 
925   KillFormFocus();
926 
927 #if defined(OS_LINUX) || defined(OS_CHROMEOS) || defined(OS_BSD)
928   SetLastInstance(client_->GetPluginInstance());
929 #endif
930 
931   return ConvertPdfToBufferDev(
932       print_.PrintPagesAsPdf(page_ranges, page_range_count, print_settings,
933                              pdf_print_settings, /*raster=*/true));
934 }
935 
PrintPagesAsPdf(const PP_PrintPageNumberRange_Dev * page_ranges,uint32_t page_range_count,const PP_PrintSettings_Dev & print_settings,const PP_PdfPrintSettings_Dev & pdf_print_settings)936 pp::Buffer_Dev PDFiumEngine::PrintPagesAsPdf(
937     const PP_PrintPageNumberRange_Dev* page_ranges,
938     uint32_t page_range_count,
939     const PP_PrintSettings_Dev& print_settings,
940     const PP_PdfPrintSettings_Dev& pdf_print_settings) {
941   DCHECK(page_range_count);
942   DCHECK(doc());
943 
944   KillFormFocus();
945 
946   std::vector<uint32_t> page_numbers =
947       PDFiumPrint::GetPageNumbersFromPrintPageNumberRange(page_ranges,
948                                                           page_range_count);
949   for (uint32_t page_number : page_numbers) {
950     pages_[page_number]->GetPage();
951     if (!IsPageVisible(page_number))
952       pages_[page_number]->Unload();
953   }
954 
955   return ConvertPdfToBufferDev(
956       print_.PrintPagesAsPdf(page_ranges, page_range_count, print_settings,
957                              pdf_print_settings, /*raster=*/false));
958 }
959 
ConvertPdfToBufferDev(const std::vector<uint8_t> & pdf_data)960 pp::Buffer_Dev PDFiumEngine::ConvertPdfToBufferDev(
961     const std::vector<uint8_t>& pdf_data) {
962   pp::Buffer_Dev buffer;
963   if (!pdf_data.empty()) {
964     buffer = pp::Buffer_Dev(GetPluginInstance(), pdf_data.size());
965     if (!buffer.is_null())
966       memcpy(buffer.data(), pdf_data.data(), pdf_data.size());
967   }
968   return buffer;
969 }
970 
KillFormFocus()971 void PDFiumEngine::KillFormFocus() {
972   FORM_ForceToKillFocus(form());
973   SetInFormTextArea(false);
974 }
975 
UpdateFocus(bool has_focus)976 void PDFiumEngine::UpdateFocus(bool has_focus) {
977   base::AutoReset<bool> updating_focus_guard(&updating_focus_, true);
978   if (has_focus) {
979     UpdateFocusItemType(last_focused_item_type_);
980     if (focus_item_type_ == FocusElementType::kPage &&
981         PageIndexInBounds(last_focused_page_) &&
982         last_focused_annot_index_ != -1) {
983       ScopedFPDFAnnotation last_focused_annot(FPDFPage_GetAnnot(
984           pages_[last_focused_page_]->GetPage(), last_focused_annot_index_));
985       if (last_focused_annot) {
986         FPDF_BOOL ret = FORM_SetFocusedAnnot(form(), last_focused_annot.get());
987         DCHECK(ret);
988       }
989     }
990   } else {
991     last_focused_item_type_ = focus_item_type_;
992     if (focus_item_type_ == FocusElementType::kDocument) {
993       UpdateFocusItemType(FocusElementType::kNone);
994     } else if (focus_item_type_ == FocusElementType::kPage) {
995       FPDF_ANNOTATION last_focused_annot = nullptr;
996       FPDF_BOOL ret = FORM_GetFocusedAnnot(form(), &last_focused_page_,
997                                            &last_focused_annot);
998       DCHECK(ret);
999       if (PageIndexInBounds(last_focused_page_) && last_focused_annot) {
1000         last_focused_annot_index_ = FPDFPage_GetAnnotIndex(
1001             pages_[last_focused_page_]->GetPage(), last_focused_annot);
1002       } else {
1003         last_focused_annot_index_ = -1;
1004       }
1005       FPDFPage_CloseAnnot(last_focused_annot);
1006     }
1007     KillFormFocus();
1008   }
1009 }
1010 
GetFocusInfo()1011 PP_PrivateAccessibilityFocusInfo PDFiumEngine::GetFocusInfo() {
1012   PP_PrivateAccessibilityFocusInfo focus_info = {
1013       PP_PrivateFocusObjectType::PP_PRIVATEFOCUSOBJECT_NONE, 0, 0};
1014 
1015   switch (focus_item_type_) {
1016     case FocusElementType::kNone: {
1017       break;
1018     }
1019     case FocusElementType::kPage: {
1020       int page_index;
1021       FPDF_ANNOTATION focused_annot;
1022       FPDF_BOOL ret = FORM_GetFocusedAnnot(form(), &page_index, &focused_annot);
1023       DCHECK(ret);
1024 
1025       if (PageIndexInBounds(page_index) && focused_annot) {
1026         PP_PrivateFocusObjectType type =
1027             GetAnnotationFocusType(FPDFAnnot_GetSubtype(focused_annot));
1028         int annot_index = FPDFPage_GetAnnotIndex(pages_[page_index]->GetPage(),
1029                                                  focused_annot);
1030         if (type != PP_PrivateFocusObjectType::PP_PRIVATEFOCUSOBJECT_NONE &&
1031             annot_index >= 0) {
1032           focus_info.focused_object_type = type;
1033           focus_info.focused_object_page_index = page_index;
1034           focus_info.focused_annotation_index_in_page = annot_index;
1035         }
1036       }
1037       FPDFPage_CloseAnnot(focused_annot);
1038       break;
1039     }
1040     case FocusElementType::kDocument: {
1041       focus_info.focused_object_type =
1042           PP_PrivateFocusObjectType::PP_PRIVATEFOCUSOBJECT_DOCUMENT;
1043       break;
1044     }
1045   }
1046   return focus_info;
1047 }
1048 
GetLoadedByteSize()1049 uint32_t PDFiumEngine::GetLoadedByteSize() {
1050   return doc_loader_->GetDocumentSize();
1051 }
1052 
ReadLoadedBytes(uint32_t length,void * buffer)1053 bool PDFiumEngine::ReadLoadedBytes(uint32_t length, void* buffer) {
1054   DCHECK_LE(length, GetLoadedByteSize());
1055   return doc_loader_->GetBlock(0, length, buffer);
1056 }
1057 
SetFormSelectedText(FPDF_FORMHANDLE form_handle,FPDF_PAGE page)1058 void PDFiumEngine::SetFormSelectedText(FPDF_FORMHANDLE form_handle,
1059                                        FPDF_PAGE page) {
1060   base::string16 selected_form_text16 = CallPDFiumWideStringBufferApi(
1061       base::BindRepeating(&FORM_GetSelectedText, form_handle, page),
1062       /*check_expected_size=*/false);
1063 
1064   // If form selected text is empty and there was no previous form text
1065   // selection, exit early because nothing has changed.
1066   if (selected_form_text16.empty() && selected_form_text_.empty())
1067     return;
1068 
1069   // Update previous and current selections, then compare them to check if
1070   // selection has changed. If so, set plugin text selection.
1071   std::string selected_form_text = selected_form_text_;
1072   selected_form_text_ = base::UTF16ToUTF8(selected_form_text16);
1073   if (selected_form_text != selected_form_text_) {
1074     DCHECK(in_form_text_area_);
1075     client_->SetSelectedText(selected_form_text_);
1076   }
1077 }
1078 
PrintEnd()1079 void PDFiumEngine::PrintEnd() {
1080   FORM_DoDocumentAAction(form(), FPDFDOC_AACTION_DP);
1081 }
1082 
GetCharIndex(const gfx::Point & point,int * page_index,int * char_index,int * form_type,PDFiumPage::LinkTarget * target)1083 PDFiumPage::Area PDFiumEngine::GetCharIndex(const gfx::Point& point,
1084                                             int* page_index,
1085                                             int* char_index,
1086                                             int* form_type,
1087                                             PDFiumPage::LinkTarget* target) {
1088   int page = -1;
1089   gfx::Point point_in_page(
1090       static_cast<int>((point.x() + position_.x()) / current_zoom_),
1091       static_cast<int>((point.y() + position_.y()) / current_zoom_));
1092   for (int visible_page : visible_pages_) {
1093     if (pages_[visible_page]->rect().Contains(point_in_page)) {
1094       page = visible_page;
1095       break;
1096     }
1097   }
1098   if (page == -1)
1099     return PDFiumPage::NONSELECTABLE_AREA;
1100 
1101   // If the page hasn't finished rendering, calling into the page sometimes
1102   // leads to hangs.
1103   for (const auto& paint : progressive_paints_) {
1104     if (paint.page_index() == page)
1105       return PDFiumPage::NONSELECTABLE_AREA;
1106   }
1107 
1108   *page_index = page;
1109   PDFiumPage::Area result = pages_[page]->GetCharIndex(
1110       point_in_page, layout_.options().default_page_orientation(), char_index,
1111       form_type, target);
1112   return (client_->IsPrintPreview() && result == PDFiumPage::WEBLINK_AREA)
1113              ? PDFiumPage::NONSELECTABLE_AREA
1114              : result;
1115 }
1116 
OnMouseDown(const MouseInputEvent & event)1117 bool PDFiumEngine::OnMouseDown(const MouseInputEvent& event) {
1118   MouseInputEvent normalized_event = NormalizeMouseEvent(event);
1119   if (normalized_event.GetButton() == InputEventMouseButtonType::kLeft)
1120     return OnLeftMouseDown(normalized_event);
1121   if (normalized_event.GetButton() == InputEventMouseButtonType::kMiddle)
1122     return OnMiddleMouseDown(normalized_event);
1123   if (normalized_event.GetButton() == InputEventMouseButtonType::kRight)
1124     return OnRightMouseDown(normalized_event);
1125   return false;
1126 }
1127 
OnSingleClick(int page_index,int char_index)1128 void PDFiumEngine::OnSingleClick(int page_index, int char_index) {
1129   SetSelecting(true);
1130   selection_.push_back(PDFiumRange(pages_[page_index].get(), char_index, 0));
1131 }
1132 
OnMultipleClick(int click_count,int page_index,int char_index)1133 void PDFiumEngine::OnMultipleClick(int click_count,
1134                                    int page_index,
1135                                    int char_index) {
1136   DCHECK_GE(click_count, 2);
1137   bool is_double_click = click_count == 2;
1138 
1139   // It would be more efficient if the SDK could support finding a space, but
1140   // now it doesn't.
1141   int start_index = char_index;
1142   do {
1143     base::char16 cur = pages_[page_index]->GetCharAtIndex(start_index);
1144     if (FindMultipleClickBoundary(is_double_click, cur))
1145       break;
1146   } while (--start_index >= 0);
1147   if (start_index)
1148     start_index++;
1149 
1150   int end_index = char_index;
1151   int total = pages_[page_index]->GetCharCount();
1152   while (end_index++ <= total) {
1153     base::char16 cur = pages_[page_index]->GetCharAtIndex(end_index);
1154     if (FindMultipleClickBoundary(is_double_click, cur))
1155       break;
1156   }
1157 
1158   selection_.push_back(PDFiumRange(pages_[page_index].get(), start_index,
1159                                    end_index - start_index));
1160 
1161   if (handling_long_press_)
1162     client_->NotifyTouchSelectionOccurred();
1163 }
1164 
OnLeftMouseDown(const MouseInputEvent & event)1165 bool PDFiumEngine::OnLeftMouseDown(const MouseInputEvent& event) {
1166   SetMouseLeftButtonDown(true);
1167 
1168   auto selection_invalidator =
1169       std::make_unique<SelectionChangeInvalidator>(this);
1170   selection_.clear();
1171 
1172   int page_index = -1;
1173   int char_index = -1;
1174   int form_type = FPDF_FORMFIELD_UNKNOWN;
1175   PDFiumPage::LinkTarget target;
1176   gfx::Point point = event.GetPosition();
1177   PDFiumPage::Area area =
1178       GetCharIndex(point, &page_index, &char_index, &form_type, &target);
1179   DCHECK_GE(form_type, FPDF_FORMFIELD_UNKNOWN);
1180   mouse_down_state_.Set(area, target);
1181 
1182   // Decide whether to open link or not based on user action in mouse up and
1183   // mouse move events.
1184   if (IsLinkArea(area))
1185     return true;
1186 
1187   if (page_index != -1) {
1188     UpdateFocusItemType(FocusElementType::kPage);
1189     last_focused_page_ = page_index;
1190     double page_x;
1191     double page_y;
1192     DeviceToPage(page_index, point, &page_x, &page_y);
1193 
1194     if (form_type != FPDF_FORMFIELD_UNKNOWN) {
1195       // FORM_OnLButton*() will trigger a callback to
1196       // OnFocusedAnnotationUpdated() which will call SetInFormTextArea().
1197       // Destroy SelectionChangeInvalidator object before SetInFormTextArea()
1198       // changes plugin's focus to be in form text area. This way, regular text
1199       // selection can be cleared when a user clicks into a form text area
1200       // because the pp::PDF::SetSelectedText() call in
1201       // ~SelectionChangeInvalidator() still goes to the Mimehandler
1202       // (not the Renderer).
1203       selection_invalidator.reset();
1204     }
1205 
1206     FPDF_PAGE page = pages_[page_index]->GetPage();
1207 
1208     if (event.GetClickCount() == 1) {
1209       FORM_OnLButtonDown(form(), page, event.GetModifiers(), page_x, page_y);
1210     } else if (event.GetClickCount() == 2) {
1211       FORM_OnLButtonDoubleClick(form(), page, event.GetModifiers(), page_x,
1212                                 page_y);
1213     }
1214     if (form_type != FPDF_FORMFIELD_UNKNOWN)
1215       return true;  // Return now before we get into the selection code.
1216   }
1217   SetInFormTextArea(false);
1218 
1219   if (area != PDFiumPage::TEXT_AREA)
1220     return true;  // Return true so WebKit doesn't do its own highlighting.
1221 
1222   if (event.GetClickCount() == 1)
1223     OnSingleClick(page_index, char_index);
1224   else if (event.GetClickCount() == 2 || event.GetClickCount() == 3)
1225     OnMultipleClick(event.GetClickCount(), page_index, char_index);
1226 
1227   return true;
1228 }
1229 
OnMiddleMouseDown(const MouseInputEvent & event)1230 bool PDFiumEngine::OnMiddleMouseDown(const MouseInputEvent& event) {
1231   SetMouseLeftButtonDown(false);
1232   mouse_middle_button_down_ = true;
1233   mouse_middle_button_last_position_ = event.GetPosition();
1234 
1235   SelectionChangeInvalidator selection_invalidator(this);
1236   selection_.clear();
1237 
1238   int unused_page_index = -1;
1239   int unused_char_index = -1;
1240   int unused_form_type = FPDF_FORMFIELD_UNKNOWN;
1241   PDFiumPage::LinkTarget target;
1242   PDFiumPage::Area area =
1243       GetCharIndex(event.GetPosition(), &unused_page_index, &unused_char_index,
1244                    &unused_form_type, &target);
1245   mouse_down_state_.Set(area, target);
1246 
1247   // Decide whether to open link or not based on user action in mouse up and
1248   // mouse move events.
1249   if (IsLinkArea(area))
1250     return true;
1251 
1252   if (kViewerImplementedPanning) {
1253     // Switch to hand cursor when panning.
1254     client_->UpdateCursor(PP_CURSORTYPE_HAND);
1255   }
1256 
1257   // Prevent middle mouse button from selecting texts.
1258   return false;
1259 }
1260 
OnRightMouseDown(const MouseInputEvent & event)1261 bool PDFiumEngine::OnRightMouseDown(const MouseInputEvent& event) {
1262   DCHECK_EQ(InputEventMouseButtonType::kRight, event.GetButton());
1263 
1264   gfx::Point point = event.GetPosition();
1265   int page_index = -1;
1266   int char_index = -1;
1267   int form_type = FPDF_FORMFIELD_UNKNOWN;
1268   PDFiumPage::LinkTarget target;
1269   PDFiumPage::Area area =
1270       GetCharIndex(point, &page_index, &char_index, &form_type, &target);
1271   DCHECK_GE(form_type, FPDF_FORMFIELD_UNKNOWN);
1272 
1273   bool is_form_text_area = IsFormTextArea(area, form_type);
1274 
1275   double page_x = -1;
1276   double page_y = -1;
1277   FPDF_PAGE page = nullptr;
1278   if (is_form_text_area) {
1279     DCHECK_NE(page_index, -1);
1280 
1281     DeviceToPage(page_index, point, &page_x, &page_y);
1282     page = pages_[page_index]->GetPage();
1283   }
1284 
1285   // Handle the case when focus starts inside a form text area.
1286   if (in_form_text_area_) {
1287     if (is_form_text_area) {
1288       FORM_OnFocus(form(), page, 0, page_x, page_y);
1289     } else {
1290       // Transition out of a form text area.
1291       FORM_ForceToKillFocus(form());
1292       SetInFormTextArea(false);
1293     }
1294     return true;
1295   }
1296 
1297   // Handle the case when focus starts outside a form text area and transitions
1298   // into a form text area.
1299   if (is_form_text_area) {
1300     FORM_OnFocus(form(), page, 0, page_x, page_y);
1301     return true;
1302   }
1303 
1304   // Handle the case when focus starts outside a form text area and stays
1305   // outside.
1306   if (selection_.empty())
1307     return false;
1308 
1309   std::vector<gfx::Rect> selection_rect_vector =
1310       GetAllScreenRectsUnion(selection_, GetVisibleRect().origin());
1311   for (const auto& rect : selection_rect_vector) {
1312     if (rect.Contains(point))
1313       return false;
1314   }
1315   SelectionChangeInvalidator selection_invalidator(this);
1316   selection_.clear();
1317   return true;
1318 }
1319 
NavigateToLinkDestination(PDFiumPage::Area area,const PDFiumPage::LinkTarget & target,WindowOpenDisposition disposition)1320 bool PDFiumEngine::NavigateToLinkDestination(
1321     PDFiumPage::Area area,
1322     const PDFiumPage::LinkTarget& target,
1323     WindowOpenDisposition disposition) {
1324   if (area == PDFiumPage::WEBLINK_AREA) {
1325     client_->NavigateTo(target.url, disposition);
1326     SetInFormTextArea(false);
1327     return true;
1328   }
1329   if (area == PDFiumPage::DOCLINK_AREA) {
1330     if (!PageIndexInBounds(target.page))
1331       return true;
1332 
1333     if (disposition == WindowOpenDisposition::CURRENT_TAB) {
1334       client_->NavigateToDestination(
1335           target.page, base::OptionalOrNullptr(target.x_in_pixels),
1336           base::OptionalOrNullptr(target.y_in_pixels),
1337           base::OptionalOrNullptr(target.zoom));
1338     } else {
1339       std::string parameters = base::StringPrintf("#page=%d", target.page + 1);
1340       parameters += base::StringPrintf(
1341           "&zoom=%d,%d,%d", static_cast<int>(target.zoom.value_or(1.0) * 100),
1342           static_cast<int>(target.x_in_pixels.value_or(0)),
1343           static_cast<int>(target.y_in_pixels.value_or(0)));
1344 
1345       client_->NavigateTo(parameters, disposition);
1346     }
1347     SetInFormTextArea(false);
1348     return true;
1349   }
1350   return false;
1351 }
1352 
OnMouseUp(const MouseInputEvent & event)1353 bool PDFiumEngine::OnMouseUp(const MouseInputEvent& event) {
1354   if (event.GetButton() != InputEventMouseButtonType::kLeft &&
1355       event.GetButton() != InputEventMouseButtonType::kMiddle) {
1356     return false;
1357   }
1358 
1359   if (event.GetButton() == InputEventMouseButtonType::kLeft)
1360     SetMouseLeftButtonDown(false);
1361   else if (event.GetButton() == InputEventMouseButtonType::kMiddle)
1362     mouse_middle_button_down_ = false;
1363 
1364   int page_index = -1;
1365   int char_index = -1;
1366   int form_type = FPDF_FORMFIELD_UNKNOWN;
1367   PDFiumPage::LinkTarget target;
1368   gfx::Point point = event.GetPosition();
1369   PDFiumPage::Area area =
1370       GetCharIndex(point, &page_index, &char_index, &form_type, &target);
1371 
1372   // Open link on mouse up for same link for which mouse down happened earlier.
1373   if (mouse_down_state_.Matches(area, target)) {
1374     uint32_t modifiers = event.GetModifiers();
1375     bool middle_button = !!(modifiers & kInputEventModifierMiddleButtonDown);
1376     bool alt_key = !!(modifiers & kInputEventModifierAltKey);
1377     bool ctrl_key = !!(modifiers & kInputEventModifierControlKey);
1378     bool meta_key = !!(modifiers & kInputEventModifierMetaKey);
1379     bool shift_key = !!(modifiers & kInputEventModifierShiftKey);
1380 
1381     WindowOpenDisposition disposition = ui::DispositionFromClick(
1382         middle_button, alt_key, ctrl_key, meta_key, shift_key);
1383 
1384     if (NavigateToLinkDestination(area, target, disposition))
1385       return true;
1386   }
1387 
1388   if (event.GetButton() == InputEventMouseButtonType::kMiddle) {
1389     if (kViewerImplementedPanning) {
1390       // Update the cursor when panning stops.
1391       client_->UpdateCursor(DetermineCursorType(area, form_type));
1392     }
1393 
1394     // Prevent middle mouse button from selecting texts.
1395     return false;
1396   }
1397 
1398   if (page_index != -1) {
1399     double page_x;
1400     double page_y;
1401     DeviceToPage(page_index, point, &page_x, &page_y);
1402     FORM_OnLButtonUp(form(), pages_[page_index]->GetPage(),
1403                      event.GetModifiers(), page_x, page_y);
1404   }
1405 
1406   if (!selecting_)
1407     return false;
1408 
1409   SetSelecting(false);
1410   return true;
1411 }
1412 
OnMouseMove(const MouseInputEvent & event)1413 bool PDFiumEngine::OnMouseMove(const MouseInputEvent& event) {
1414   int page_index = -1;
1415   int char_index = -1;
1416   int form_type = FPDF_FORMFIELD_UNKNOWN;
1417   PDFiumPage::LinkTarget target;
1418   gfx::Point point = event.GetPosition();
1419   PDFiumPage::Area area =
1420       GetCharIndex(point, &page_index, &char_index, &form_type, &target);
1421 
1422   // Clear |mouse_down_state_| if mouse moves away from where the mouse down
1423   // happened.
1424   if (!mouse_down_state_.Matches(area, target))
1425     mouse_down_state_.Reset();
1426 
1427   if (!selecting_) {
1428     client_->UpdateCursor(DetermineCursorType(area, form_type));
1429 
1430     if (page_index != -1) {
1431       double page_x;
1432       double page_y;
1433       DeviceToPage(page_index, point, &page_x, &page_y);
1434       FORM_OnMouseMove(form(), pages_[page_index]->GetPage(), 0, page_x,
1435                        page_y);
1436     }
1437 
1438     UpdateLinkUnderCursor(GetLinkAtPosition(point));
1439 
1440     // If in form text area while left mouse button is held down, check if form
1441     // text selection needs to be updated.
1442     if (mouse_left_button_down_ && area == PDFiumPage::FORM_TEXT_AREA &&
1443         last_focused_page_ != -1) {
1444       SetFormSelectedText(form(), pages_[last_focused_page_]->GetPage());
1445     }
1446 
1447     if (kViewerImplementedPanning && mouse_middle_button_down_) {
1448       // Subtract (origin - destination) so delta is already the delta for
1449       // moving the page, rather than the delta the mouse moved.
1450       // GetMovement() does not work here, as small mouse movements are
1451       // considered zero.
1452       gfx::Vector2d page_position_delta =
1453           mouse_middle_button_last_position_ - event.GetPosition();
1454       if (page_position_delta.x() != 0 || page_position_delta.y() != 0) {
1455         client_->ScrollBy(page_position_delta);
1456         mouse_middle_button_last_position_ = event.GetPosition();
1457       }
1458     }
1459 
1460     // No need to swallow the event, since this might interfere with the
1461     // scrollbars if the user is dragging them.
1462     return false;
1463   }
1464 
1465   // We're selecting but right now we're not over text, so don't change the
1466   // current selection.
1467   if (area != PDFiumPage::TEXT_AREA && !IsLinkArea(area))
1468     return false;
1469 
1470   SelectionChangeInvalidator selection_invalidator(this);
1471   return ExtendSelection(page_index, char_index);
1472 }
1473 
DetermineCursorType(PDFiumPage::Area area,int form_type) const1474 PP_CursorType_Dev PDFiumEngine::DetermineCursorType(PDFiumPage::Area area,
1475                                                     int form_type) const {
1476   if (kViewerImplementedPanning && mouse_middle_button_down_) {
1477     return PP_CURSORTYPE_HAND;
1478   }
1479 
1480   switch (area) {
1481     case PDFiumPage::TEXT_AREA:
1482       return PP_CURSORTYPE_IBEAM;
1483     case PDFiumPage::WEBLINK_AREA:
1484     case PDFiumPage::DOCLINK_AREA:
1485       return PP_CURSORTYPE_HAND;
1486     case PDFiumPage::NONSELECTABLE_AREA:
1487     case PDFiumPage::FORM_TEXT_AREA:
1488     default:
1489       switch (form_type) {
1490         case FPDF_FORMFIELD_PUSHBUTTON:
1491         case FPDF_FORMFIELD_CHECKBOX:
1492         case FPDF_FORMFIELD_RADIOBUTTON:
1493         case FPDF_FORMFIELD_COMBOBOX:
1494         case FPDF_FORMFIELD_LISTBOX:
1495           return PP_CURSORTYPE_HAND;
1496         case FPDF_FORMFIELD_TEXTFIELD:
1497           return PP_CURSORTYPE_IBEAM;
1498 #if defined(PDF_ENABLE_XFA)
1499         case FPDF_FORMFIELD_XFA_CHECKBOX:
1500         case FPDF_FORMFIELD_XFA_COMBOBOX:
1501         case FPDF_FORMFIELD_XFA_IMAGEFIELD:
1502         case FPDF_FORMFIELD_XFA_LISTBOX:
1503         case FPDF_FORMFIELD_XFA_PUSHBUTTON:
1504         case FPDF_FORMFIELD_XFA_SIGNATURE:
1505           return PP_CURSORTYPE_HAND;
1506         case FPDF_FORMFIELD_XFA_TEXTFIELD:
1507           return PP_CURSORTYPE_IBEAM;
1508 #endif
1509         default:
1510           return PP_CURSORTYPE_POINTER;
1511       }
1512   }
1513 }
1514 
OnMouseEnter(const MouseInputEvent & event)1515 void PDFiumEngine::OnMouseEnter(const MouseInputEvent& event) {
1516   if (event.GetModifiers() & kInputEventModifierMiddleButtonDown) {
1517     if (!mouse_middle_button_down_) {
1518       mouse_middle_button_down_ = true;
1519       mouse_middle_button_last_position_ = event.GetPosition();
1520     }
1521   } else {
1522     if (mouse_middle_button_down_) {
1523       mouse_middle_button_down_ = false;
1524     }
1525   }
1526 }
1527 
ExtendSelection(int page_index,int char_index)1528 bool PDFiumEngine::ExtendSelection(int page_index, int char_index) {
1529   // Check if the user has decreased their selection area and we need to remove
1530   // pages from |selection_|.
1531   for (size_t i = 0; i < selection_.size(); ++i) {
1532     if (selection_[i].page_index() == page_index) {
1533       // There should be no other pages after this.
1534       selection_.erase(selection_.begin() + i + 1, selection_.end());
1535       break;
1536     }
1537   }
1538   if (selection_.empty())
1539     return false;
1540 
1541   const int last_page_index = selection_.back().page_index();
1542   const int last_char_index = selection_.back().char_index();
1543   if (last_page_index == page_index) {
1544     // Selecting within a page.
1545     int count = char_index - last_char_index;
1546     if (count >= 0) {
1547       // Selecting forward.
1548       ++count;
1549     } else {
1550       --count;
1551     }
1552     selection_.back().SetCharCount(count);
1553   } else if (last_page_index < page_index) {
1554     // Selecting into the next page.
1555 
1556     // Save the current last selection for use below.
1557     // Warning: Do not use references / pointers into |selection_|, as the code
1558     // below can modify |selection_| and invalidate those references / pointers.
1559     const size_t last_selection_index = selection_.size() - 1;
1560 
1561     // First make sure that there are no gaps in selection, i.e. if mousedown on
1562     // page one but we only get mousemove over page three, we want page two.
1563     for (int i = last_page_index + 1; i < page_index; ++i) {
1564       selection_.push_back(
1565           PDFiumRange(pages_[i].get(), 0, pages_[i]->GetCharCount()));
1566     }
1567 
1568     int count = pages_[last_page_index]->GetCharCount();
1569     selection_[last_selection_index].SetCharCount(count - last_char_index);
1570     selection_.push_back(PDFiumRange(pages_[page_index].get(), 0, char_index));
1571   } else {
1572     // Selecting into the previous page.
1573     // The selection's char_index is 0-based, so the character count is one
1574     // more than the index. The character count needs to be negative to
1575     // indicate a backwards selection.
1576     selection_.back().SetCharCount(-last_char_index - 1);
1577 
1578     // First make sure that there are no gaps in selection, i.e. if mousedown on
1579     // page three but we only get mousemove over page one, we want page two.
1580     for (int i = last_page_index - 1; i > page_index; --i) {
1581       selection_.push_back(
1582           PDFiumRange(pages_[i].get(), 0, pages_[i]->GetCharCount()));
1583     }
1584 
1585     int count = pages_[page_index]->GetCharCount();
1586     selection_.push_back(
1587         PDFiumRange(pages_[page_index].get(), count, count - char_index));
1588   }
1589 
1590   return true;
1591 }
1592 
OnKeyDown(const KeyboardInputEvent & event)1593 bool PDFiumEngine::OnKeyDown(const KeyboardInputEvent& event) {
1594   // Handle tab events first as we might need to transition focus to an
1595   // annotation in PDF.
1596   if (event.GetKeyCode() == FWL_VKEY_Tab)
1597     return HandleTabEvent(event.GetModifiers());
1598 
1599   if (last_focused_page_ == -1)
1600     return false;
1601 
1602   bool rv = !!FORM_OnKeyDown(form(), pages_[last_focused_page_]->GetPage(),
1603                              event.GetKeyCode(), event.GetModifiers());
1604 
1605   if (event.GetKeyCode() == ui::VKEY_BACK ||
1606       event.GetKeyCode() == ui::VKEY_ESCAPE) {
1607     // Blink does not send char events for backspace or escape keys, see
1608     // WebKeyboardEvent::IsCharacterKey() and b/961192 for more information.
1609     // So just fake one since PDFium uses it.
1610     std::string str;
1611     str.push_back(event.GetKeyCode());
1612     KeyboardInputEvent synthesized(InputEventType::kChar, event.GetTimeStamp(),
1613                                    event.GetModifiers(), event.GetKeyCode(),
1614                                    str);
1615     OnChar(synthesized);
1616   }
1617 
1618 #if !defined(OS_MAC)
1619   // macOS doesn't have keyboard-triggered context menus.
1620   // Scroll focused annotation into view when context menu is invoked through
1621   // keyboard <Shift-F10>.
1622   if (event.GetKeyCode() == FWL_VKEY_F10 &&
1623       (event.GetModifiers() & kInputEventModifierShiftKey)) {
1624     DCHECK(!rv);
1625     ScrollFocusedAnnotationIntoView();
1626   }
1627 #endif
1628 
1629   return rv;
1630 }
1631 
OnKeyUp(const KeyboardInputEvent & event)1632 bool PDFiumEngine::OnKeyUp(const KeyboardInputEvent& event) {
1633   if (last_focused_page_ == -1)
1634     return false;
1635 
1636   // Check if form text selection needs to be updated.
1637   FPDF_PAGE page = pages_[last_focused_page_]->GetPage();
1638   if (in_form_text_area_)
1639     SetFormSelectedText(form(), page);
1640 
1641   return !!FORM_OnKeyUp(form(), page, event.GetKeyCode(), event.GetModifiers());
1642 }
1643 
OnChar(const KeyboardInputEvent & event)1644 bool PDFiumEngine::OnChar(const KeyboardInputEvent& event) {
1645   if (last_focused_page_ == -1)
1646     return false;
1647 
1648   base::string16 str = base::UTF8ToUTF16(event.GetKeyChar());
1649   bool rv = !!FORM_OnChar(form(), pages_[last_focused_page_]->GetPage(), str[0],
1650                           event.GetModifiers());
1651 
1652   // Scroll editable form text into view on char events. We should not scroll
1653   // focused annotation on escape char event since escape char is used to
1654   // dismiss focus from form controls.
1655   if (rv && editable_form_text_area_ && event.GetKeyCode() != ui::VKEY_ESCAPE) {
1656     ScrollFocusedAnnotationIntoView();
1657   }
1658 
1659   return rv;
1660 }
1661 
StartFind(const std::string & text,bool case_sensitive)1662 void PDFiumEngine::StartFind(const std::string& text, bool case_sensitive) {
1663   // If the caller asks StartFind() to search for no text, then this is an
1664   // error on the part of the caller. The PPAPI Find_Private interface
1665   // guarantees it is not empty, so this should never happen.
1666   DCHECK(!text.empty());
1667 
1668   // If StartFind() gets called before we have any page information (i.e.
1669   // before the first call to LoadDocument has happened). Handle this case.
1670   if (pages_.empty()) {
1671     client_->NotifyNumberOfFindResultsChanged(0, true);
1672     return;
1673   }
1674 
1675   bool first_search = (current_find_text_ != text);
1676   int character_to_start_searching_from = 0;
1677   if (first_search) {
1678     std::vector<PDFiumRange> old_selection = selection_;
1679     StopFind();
1680     current_find_text_ = text;
1681 
1682     if (old_selection.empty()) {
1683       // Start searching from the beginning of the document.
1684       next_page_to_search_ = 0;
1685       last_page_to_search_ = pages_.size() - 1;
1686       last_character_index_to_search_ = -1;
1687     } else {
1688       // There's a current selection, so start from it.
1689       next_page_to_search_ = old_selection[0].page_index();
1690       last_character_index_to_search_ = old_selection[0].char_index();
1691       character_to_start_searching_from = old_selection[0].char_index();
1692       last_page_to_search_ = next_page_to_search_;
1693     }
1694     search_in_progress_ = true;
1695   }
1696 
1697   int current_page = next_page_to_search_;
1698 
1699   if (pages_[current_page]->available()) {
1700     base::string16 str = base::UTF8ToUTF16(text);
1701     // Don't use PDFium to search for now, since it doesn't support unicode
1702     // text. Leave the code for now to avoid bit-rot, in case it's fixed later.
1703     // The extra parens suppress a -Wunreachable-code warning.
1704     if ((0)) {
1705       SearchUsingPDFium(str, case_sensitive, first_search,
1706                         character_to_start_searching_from, current_page);
1707     } else {
1708       SearchUsingICU(str, case_sensitive, first_search,
1709                      character_to_start_searching_from, current_page);
1710     }
1711 
1712     if (!IsPageVisible(current_page))
1713       pages_[current_page]->Unload();
1714   }
1715 
1716   if (next_page_to_search_ != last_page_to_search_ ||
1717       (first_search && last_character_index_to_search_ != -1)) {
1718     ++next_page_to_search_;
1719   }
1720 
1721   if (next_page_to_search_ == static_cast<int>(pages_.size()))
1722     next_page_to_search_ = 0;
1723   // If there's only one page in the document and we start searching midway,
1724   // then we'll want to search the page one more time.
1725   bool end_of_search =
1726       next_page_to_search_ == last_page_to_search_ &&
1727       // Only one page but didn't start midway.
1728       ((pages_.size() == 1 && last_character_index_to_search_ == -1) ||
1729        // Started midway, but only 1 page and we already looped around.
1730        (pages_.size() == 1 && !first_search) ||
1731        // Started midway, and we've just looped around.
1732        (pages_.size() > 1 && current_page == next_page_to_search_));
1733 
1734   if (end_of_search) {
1735     search_in_progress_ = false;
1736 
1737     // Send the final notification.
1738     client_->NotifyNumberOfFindResultsChanged(find_results_.size(), true);
1739     return;
1740   }
1741 
1742   // In unit tests, PPAPI is not initialized, so just call ContinueFind()
1743   // directly.
1744   if (doc_loader_set_for_testing_) {
1745     ContinueFind(case_sensitive ? 1 : 0);
1746   } else {
1747     pp::Module::Get()->core()->CallOnMainThread(
1748         0,
1749         PPCompletionCallbackFromResultCallback(base::BindOnce(
1750             &PDFiumEngine::ContinueFind, find_weak_factory_.GetWeakPtr())),
1751         case_sensitive ? 1 : 0);
1752   }
1753 }
1754 
SearchUsingPDFium(const base::string16 & term,bool case_sensitive,bool first_search,int character_to_start_searching_from,int current_page)1755 void PDFiumEngine::SearchUsingPDFium(const base::string16& term,
1756                                      bool case_sensitive,
1757                                      bool first_search,
1758                                      int character_to_start_searching_from,
1759                                      int current_page) {
1760   // Find all the matches in the current page.
1761   unsigned long flags = case_sensitive ? FPDF_MATCHCASE : 0;
1762   FPDF_SCHHANDLE find =
1763       FPDFText_FindStart(pages_[current_page]->GetTextPage(),
1764                          reinterpret_cast<const unsigned short*>(term.c_str()),
1765                          flags, character_to_start_searching_from);
1766 
1767   // Note: since we search one page at a time, we don't find matches across
1768   // page boundaries.  We could do this manually ourself, but it seems low
1769   // priority since Reader itself doesn't do it.
1770   while (FPDFText_FindNext(find)) {
1771     PDFiumRange result(pages_[current_page].get(),
1772                        FPDFText_GetSchResultIndex(find),
1773                        FPDFText_GetSchCount(find));
1774 
1775     if (!first_search && last_character_index_to_search_ != -1 &&
1776         result.page_index() == last_page_to_search_ &&
1777         result.char_index() >= last_character_index_to_search_) {
1778       break;
1779     }
1780 
1781     AddFindResult(result);
1782   }
1783 
1784   FPDFText_FindClose(find);
1785 }
1786 
SearchUsingICU(const base::string16 & term,bool case_sensitive,bool first_search,int character_to_start_searching_from,int current_page)1787 void PDFiumEngine::SearchUsingICU(const base::string16& term,
1788                                   bool case_sensitive,
1789                                   bool first_search,
1790                                   int character_to_start_searching_from,
1791                                   int current_page) {
1792   DCHECK(!term.empty());
1793 
1794   // Various types of quotions marks need to be converted to the simple ASCII
1795   // version for searching to get better matching.
1796   base::string16 adjusted_term = term;
1797   for (base::char16& c : adjusted_term)
1798     c = SimplifyForSearch(c);
1799 
1800   const int original_text_length = pages_[current_page]->GetCharCount();
1801   int text_length = original_text_length;
1802   if (character_to_start_searching_from) {
1803     text_length -= character_to_start_searching_from;
1804   } else if (!first_search && last_character_index_to_search_ != -1 &&
1805              current_page == last_page_to_search_) {
1806     text_length = last_character_index_to_search_;
1807   }
1808   if (text_length <= 0)
1809     return;
1810 
1811   base::string16 page_text;
1812   PDFiumAPIStringBufferAdapter<base::string16> api_string_adapter(
1813       &page_text, text_length, false);
1814   unsigned short* data =
1815       reinterpret_cast<unsigned short*>(api_string_adapter.GetData());
1816   int written =
1817       FPDFText_GetText(pages_[current_page]->GetTextPage(),
1818                        character_to_start_searching_from, text_length, data);
1819   api_string_adapter.Close(written);
1820 
1821   base::string16 adjusted_page_text;
1822   adjusted_page_text.reserve(page_text.size());
1823   // Values in |removed_indices| are in the adjusted text index space and
1824   // indicate a character was removed from the page text before the given
1825   // index. If multiple characters are removed in a row then there will be
1826   // multiple entries with the same value.
1827   std::vector<size_t> removed_indices;
1828   // When walking through the page text collapse any whitespace regions,
1829   // including \r and \n, down to a single ' ' character. This code does
1830   // not use base::CollapseWhitespace(), because that function does not
1831   // return where the collapsing occurs, but uses the same underlying list of
1832   // whitespace characters. Calculating where the collapsed regions are after
1833   // the fact is as complex as collapsing them manually.
1834   for (size_t i = 0; i < page_text.size(); i++) {
1835     base::char16 c = page_text[i];
1836     // Collapse whitespace regions by inserting a ' ' into the
1837     // adjusted text and recording any removed whitespace indices as preceding
1838     // it.
1839     if (base::IsUnicodeWhitespace(c)) {
1840       size_t whitespace_region_begin = i;
1841       while (i < page_text.size() && base::IsUnicodeWhitespace(page_text[i]))
1842         ++i;
1843 
1844       size_t count = i - whitespace_region_begin - 1;
1845       removed_indices.insert(removed_indices.end(), count,
1846                              adjusted_page_text.size());
1847       adjusted_page_text.push_back(' ');
1848       if (i >= page_text.size())
1849         break;
1850       c = page_text[i];
1851     }
1852 
1853     if (IsIgnorableCharacter(c))
1854       removed_indices.push_back(adjusted_page_text.size());
1855     else
1856       adjusted_page_text.push_back(SimplifyForSearch(c));
1857   }
1858 
1859   std::vector<PDFEngine::Client::SearchStringResult> results =
1860       client_->SearchString(adjusted_page_text.c_str(), adjusted_term.c_str(),
1861                             case_sensitive);
1862   for (const auto& result : results) {
1863     // Need to convert from adjusted page text start to page text start, by
1864     // incrementing for all the characters adjusted before it in the string.
1865     auto removed_indices_begin = std::upper_bound(
1866         removed_indices.begin(), removed_indices.end(), result.start_index);
1867     size_t page_text_result_start_index =
1868         result.start_index +
1869         std::distance(removed_indices.begin(), removed_indices_begin);
1870 
1871     // Need to convert the adjusted page length into a page text length, since
1872     // the matching range may have adjusted characters within it. This
1873     // conversion only cares about skipped characters in the result interval.
1874     auto removed_indices_end =
1875         std::upper_bound(removed_indices_begin, removed_indices.end(),
1876                          result.start_index + result.length);
1877     int term_removed_count =
1878         std::distance(removed_indices_begin, removed_indices_end);
1879     int page_text_result_length = result.length + term_removed_count;
1880 
1881     // Need to map the indexes from the page text, which may have generated
1882     // characters like space etc, to character indices from the page.
1883     int text_to_start_searching_from = FPDFText_GetTextIndexFromCharIndex(
1884         pages_[current_page]->GetTextPage(), character_to_start_searching_from);
1885     int temp_start =
1886         page_text_result_start_index + text_to_start_searching_from;
1887     int start = FPDFText_GetCharIndexFromTextIndex(
1888         pages_[current_page]->GetTextPage(), temp_start);
1889     int end = FPDFText_GetCharIndexFromTextIndex(
1890         pages_[current_page]->GetTextPage(),
1891         temp_start + page_text_result_length);
1892 
1893     // If |term| occurs at the end of a page, then |end| will be -1 due to the
1894     // index being out of bounds. Compensate for this case so the range
1895     // character count calculation below works out.
1896     if (temp_start + page_text_result_length == original_text_length) {
1897       DCHECK_EQ(-1, end);
1898       end = original_text_length;
1899     }
1900     DCHECK_LT(start, end);
1901     DCHECK_EQ(term.size() + term_removed_count,
1902               static_cast<size_t>(end - start));
1903     AddFindResult(PDFiumRange(pages_[current_page].get(), start, end - start));
1904   }
1905 }
1906 
AddFindResult(const PDFiumRange & result)1907 void PDFiumEngine::AddFindResult(const PDFiumRange& result) {
1908   bool first_result = find_results_.empty();
1909   // Figure out where to insert the new location, since we could have
1910   // started searching midway and now we wrapped.
1911   size_t result_index;
1912   int page_index = result.page_index();
1913   int char_index = result.char_index();
1914   for (result_index = 0; result_index < find_results_.size(); ++result_index) {
1915     if (find_results_[result_index].page_index() > page_index ||
1916         (find_results_[result_index].page_index() == page_index &&
1917          find_results_[result_index].char_index() > char_index)) {
1918       break;
1919     }
1920   }
1921   find_results_.insert(find_results_.begin() + result_index, result);
1922   UpdateTickMarks();
1923   client_->NotifyNumberOfFindResultsChanged(find_results_.size(), false);
1924   if (first_result) {
1925     DCHECK(!resume_find_index_);
1926     DCHECK(!current_find_index_);
1927     SelectFindResult(/*forward=*/true);
1928   }
1929 }
1930 
SelectFindResult(bool forward)1931 bool PDFiumEngine::SelectFindResult(bool forward) {
1932   if (find_results_.empty())
1933     return false;
1934 
1935   SelectionChangeInvalidator selection_invalidator(this);
1936 
1937   // Move back/forward through the search locations we previously found.
1938   size_t new_index;
1939   const size_t last_index = find_results_.size() - 1;
1940 
1941   if (resume_find_index_) {
1942     new_index = resume_find_index_.value();
1943     resume_find_index_.reset();
1944   } else if (current_find_index_) {
1945     size_t current_index = current_find_index_.value();
1946     if ((forward && current_index >= last_index) ||
1947         (!forward && current_index == 0)) {
1948       current_find_index_.reset();
1949       client_->NotifySelectedFindResultChanged(-1);
1950       client_->NotifyNumberOfFindResultsChanged(find_results_.size(), true);
1951       return true;
1952     }
1953     int increment = forward ? 1 : -1;
1954     new_index = current_index + increment;
1955   } else {
1956     new_index = forward ? 0 : last_index;
1957   }
1958   current_find_index_ = new_index;
1959 
1960   // Update the selection before telling the client to scroll, since it could
1961   // paint then.
1962   selection_.clear();
1963   selection_.push_back(find_results_[current_find_index_.value()]);
1964 
1965   // If the result is not in view, scroll to it.
1966   gfx::Rect bounding_rect;
1967   gfx::Rect visible_rect = GetVisibleRect();
1968 
1969   // TODO(crbug.com/1108574): Remove after fixing the issue.
1970   size_t find_results_size = find_results_.size();
1971   base::debug::Alias(&find_results_size);
1972   size_t current_find_index_value = current_find_index_.value();
1973   base::debug::Alias(&current_find_index_value);
1974 
1975   // Use zoom of 1.0 since |visible_rect| is without zoom.
1976   const std::vector<gfx::Rect>& rects =
1977       find_results_[current_find_index_.value()].GetScreenRects(
1978           gfx::Point(), 1.0, layout_.options().default_page_orientation());
1979   for (const auto& rect : rects)
1980     bounding_rect.Union(rect);
1981   if (!visible_rect.Contains(bounding_rect)) {
1982     gfx::Point center = bounding_rect.CenterPoint();
1983     // Make the page centered.
1984     int new_y = CalculateCenterForZoom(center.y(), visible_rect.height(),
1985                                        current_zoom_);
1986     client_->ScrollToY(new_y, /*compensate_for_toolbar=*/false);
1987 
1988     // Only move horizontally if it's not visible.
1989     if (center.x() < visible_rect.x() || center.x() > visible_rect.right()) {
1990       int new_x = CalculateCenterForZoom(center.x(), visible_rect.width(),
1991                                          current_zoom_);
1992       client_->ScrollToX(new_x);
1993     }
1994   }
1995 
1996   client_->NotifySelectedFindResultChanged(current_find_index_.value());
1997   if (!search_in_progress_)
1998     client_->NotifyNumberOfFindResultsChanged(find_results_.size(), true);
1999   return true;
2000 }
2001 
StopFind()2002 void PDFiumEngine::StopFind() {
2003   SelectionChangeInvalidator selection_invalidator(this);
2004   selection_.clear();
2005   selecting_ = false;
2006 
2007   find_results_.clear();
2008   search_in_progress_ = false;
2009   next_page_to_search_ = -1;
2010   last_page_to_search_ = -1;
2011   last_character_index_to_search_ = -1;
2012   current_find_index_.reset();
2013   current_find_text_.clear();
2014 
2015   UpdateTickMarks();
2016   find_weak_factory_.InvalidateWeakPtrs();
2017 }
2018 
GetAllScreenRectsUnion(const std::vector<PDFiumRange> & rect_range,const gfx::Point & point) const2019 std::vector<gfx::Rect> PDFiumEngine::GetAllScreenRectsUnion(
2020     const std::vector<PDFiumRange>& rect_range,
2021     const gfx::Point& point) const {
2022   std::vector<gfx::Rect> rect_vector;
2023   rect_vector.reserve(rect_range.size());
2024   for (const auto& range : rect_range) {
2025     gfx::Rect result_rect;
2026     const std::vector<gfx::Rect>& rects = range.GetScreenRects(
2027         point, current_zoom_, layout_.options().default_page_orientation());
2028     for (const auto& rect : rects)
2029       result_rect.Union(rect);
2030     rect_vector.push_back(result_rect);
2031   }
2032   return rect_vector;
2033 }
2034 
UpdateTickMarks()2035 void PDFiumEngine::UpdateTickMarks() {
2036   std::vector<gfx::Rect> tickmarks =
2037       GetAllScreenRectsUnion(find_results_, gfx::Point());
2038   client_->UpdateTickMarks(tickmarks);
2039 }
2040 
ZoomUpdated(double new_zoom_level)2041 void PDFiumEngine::ZoomUpdated(double new_zoom_level) {
2042   CancelPaints();
2043 
2044   current_zoom_ = new_zoom_level;
2045 
2046   CalculateVisiblePages();
2047   UpdateTickMarks();
2048 }
2049 
RotateClockwise()2050 void PDFiumEngine::RotateClockwise() {
2051   desired_layout_options_.RotatePagesClockwise();
2052   ProposeNextDocumentLayout();
2053 }
2054 
RotateCounterclockwise()2055 void PDFiumEngine::RotateCounterclockwise() {
2056   desired_layout_options_.RotatePagesCounterclockwise();
2057   ProposeNextDocumentLayout();
2058 }
2059 
SetTwoUpView(bool enable)2060 void PDFiumEngine::SetTwoUpView(bool enable) {
2061   desired_layout_options_.set_two_up_view_enabled(enable);
2062   ProposeNextDocumentLayout();
2063 }
2064 
DisplayAnnotations(bool display)2065 void PDFiumEngine::DisplayAnnotations(bool display) {
2066   if (render_annots_ == display)
2067     return;
2068 
2069   render_annots_ = display;
2070   InvalidateAllPages();
2071 }
2072 
InvalidateAllPages()2073 void PDFiumEngine::InvalidateAllPages() {
2074   CancelPaints();
2075   StopFind();
2076   DCHECK(document_loaded_);
2077   RefreshCurrentDocumentLayout();
2078   client_->Invalidate(gfx::Rect(plugin_size_));
2079 }
2080 
GetSelectedText()2081 std::string PDFiumEngine::GetSelectedText() {
2082   if (!HasPermission(PDFEngine::PERMISSION_COPY))
2083     return std::string();
2084 
2085   base::string16 result;
2086   for (size_t i = 0; i < selection_.size(); ++i) {
2087     static constexpr base::char16 kNewLineChar = L'\n';
2088     base::string16 current_selection_text = selection_[i].GetText();
2089     if (i != 0) {
2090       if (selection_[i - 1].page_index() > selection_[i].page_index())
2091         std::swap(current_selection_text, result);
2092       result.push_back(kNewLineChar);
2093     }
2094     result.append(current_selection_text);
2095   }
2096 
2097   FormatStringWithHyphens(&result);
2098   FormatStringForOS(&result);
2099   return base::UTF16ToUTF8(result);
2100 }
2101 
CanEditText()2102 bool PDFiumEngine::CanEditText() {
2103   return editable_form_text_area_;
2104 }
2105 
HasEditableText()2106 bool PDFiumEngine::HasEditableText() {
2107   DCHECK(CanEditText());
2108   if (last_focused_page_ == -1)
2109     return false;
2110   FPDF_PAGE page = pages_[last_focused_page_]->GetPage();
2111   // If the return value is 2, that corresponds to "\0\0".
2112   return FORM_GetFocusedText(form(), page, nullptr, 0) > 2;
2113 }
2114 
ReplaceSelection(const std::string & text)2115 void PDFiumEngine::ReplaceSelection(const std::string& text) {
2116   DCHECK(CanEditText());
2117   if (last_focused_page_ != -1) {
2118     base::string16 text_wide = base::UTF8ToUTF16(text);
2119     FPDF_WIDESTRING text_pdf_wide =
2120         reinterpret_cast<FPDF_WIDESTRING>(text_wide.c_str());
2121 
2122     FORM_ReplaceSelection(form(), pages_[last_focused_page_]->GetPage(),
2123                           text_pdf_wide);
2124   }
2125 }
2126 
CanUndo()2127 bool PDFiumEngine::CanUndo() {
2128   if (last_focused_page_ == -1)
2129     return false;
2130   return !!FORM_CanUndo(form(), pages_[last_focused_page_]->GetPage());
2131 }
2132 
CanRedo()2133 bool PDFiumEngine::CanRedo() {
2134   if (last_focused_page_ == -1)
2135     return false;
2136   return !!FORM_CanRedo(form(), pages_[last_focused_page_]->GetPage());
2137 }
2138 
Undo()2139 void PDFiumEngine::Undo() {
2140   if (last_focused_page_ != -1)
2141     FORM_Undo(form(), pages_[last_focused_page_]->GetPage());
2142 }
2143 
Redo()2144 void PDFiumEngine::Redo() {
2145   if (last_focused_page_ != -1)
2146     FORM_Redo(form(), pages_[last_focused_page_]->GetPage());
2147 }
2148 
HandleAccessibilityAction(const PP_PdfAccessibilityActionData & action_data)2149 void PDFiumEngine::HandleAccessibilityAction(
2150     const PP_PdfAccessibilityActionData& action_data) {
2151   switch (action_data.action) {
2152     case PP_PdfAccessibilityAction::PP_PDF_SCROLL_TO_MAKE_VISIBLE: {
2153       ScrollBasedOnScrollAlignment(RectFromPPRect(action_data.target_rect),
2154                                    action_data.horizontal_scroll_alignment,
2155                                    action_data.vertical_scroll_alignment);
2156       break;
2157     }
2158     case PP_PdfAccessibilityAction::PP_PDF_DO_DEFAULT_ACTION: {
2159       if (PageIndexInBounds(action_data.page_index)) {
2160         if (action_data.annotation_type ==
2161             PP_PdfAccessibilityAnnotationType::PP_PDF_LINK) {
2162           PDFiumPage::LinkTarget target;
2163           PDFiumPage::Area area =
2164               pages_[action_data.page_index]->GetLinkTargetAtIndex(
2165                   action_data.annotation_index, &target);
2166           NavigateToLinkDestination(area, target,
2167                                     WindowOpenDisposition::CURRENT_TAB);
2168         }
2169       }
2170       break;
2171     }
2172     case PP_PdfAccessibilityAction::PP_PDF_SCROLL_TO_GLOBAL_POINT: {
2173       ScrollToGlobalPoint(RectFromPPRect(action_data.target_rect),
2174                           PointFromPPPoint(action_data.target_point));
2175       break;
2176     }
2177     case PP_PdfAccessibilityAction::PP_PDF_SET_SELECTION: {
2178       if (IsPageCharacterIndexInBounds(action_data.selection_start_index) &&
2179           IsPageCharacterIndexInBounds(action_data.selection_end_index)) {
2180         SetSelection(action_data.selection_start_index,
2181                      action_data.selection_end_index);
2182       }
2183       break;
2184     }
2185     default:
2186       NOTREACHED();
2187       break;
2188   }
2189 }
2190 
GetLinkAtPosition(const gfx::Point & point)2191 std::string PDFiumEngine::GetLinkAtPosition(const gfx::Point& point) {
2192   std::string url;
2193   int temp;
2194   int page_index = -1;
2195   int form_type = FPDF_FORMFIELD_UNKNOWN;
2196   PDFiumPage::LinkTarget target;
2197   PDFiumPage::Area area =
2198       GetCharIndex(point, &page_index, &temp, &form_type, &target);
2199   if (area == PDFiumPage::WEBLINK_AREA)
2200     url = target.url;
2201   return url;
2202 }
2203 
HasPermission(DocumentPermission permission) const2204 bool PDFiumEngine::HasPermission(DocumentPermission permission) const {
2205   // No |permissions_| means no restrictions.
2206   if (!permissions_)
2207     return true;
2208   return permissions_->HasPermission(permission);
2209 }
2210 
SelectAll()2211 void PDFiumEngine::SelectAll() {
2212   if (in_form_text_area_)
2213     return;
2214 
2215   SelectionChangeInvalidator selection_invalidator(this);
2216 
2217   selection_.clear();
2218   for (const auto& page : pages_) {
2219     if (page->available())
2220       selection_.push_back(PDFiumRange(page.get(), 0, page->GetCharCount()));
2221   }
2222 }
2223 
2224 const std::vector<DocumentAttachmentInfo>&
GetDocumentAttachmentInfoList() const2225 PDFiumEngine::GetDocumentAttachmentInfoList() const {
2226   DCHECK(document_loaded_);
2227   return doc_attachment_info_list_;
2228 }
2229 
GetAttachmentData(size_t index)2230 std::vector<uint8_t> PDFiumEngine::GetAttachmentData(size_t index) {
2231   DCHECK_LT(index, doc_attachment_info_list_.size());
2232   DCHECK(doc_attachment_info_list_[index].is_readable);
2233   unsigned long length_bytes = doc_attachment_info_list_[index].size_bytes;
2234   DCHECK_NE(length_bytes, 0u);
2235 
2236   FPDF_ATTACHMENT attachment = FPDFDoc_GetAttachment(doc(), index);
2237   std::vector<uint8_t> content_buf(length_bytes);
2238   unsigned long data_size_bytes;
2239   bool is_attachment_readable = FPDFAttachment_GetFile(
2240       attachment, content_buf.data(), length_bytes, &data_size_bytes);
2241   if (!is_attachment_readable || length_bytes != data_size_bytes) {
2242     NOTREACHED();
2243     return std::vector<uint8_t>();
2244   }
2245 
2246   return content_buf;
2247 }
2248 
GetDocumentMetadata() const2249 const DocumentMetadata& PDFiumEngine::GetDocumentMetadata() const {
2250   DCHECK(document_loaded_);
2251   return doc_metadata_;
2252 }
2253 
GetNumberOfPages() const2254 int PDFiumEngine::GetNumberOfPages() const {
2255   return pages_.size();
2256 }
2257 
GetBookmarks()2258 base::Value PDFiumEngine::GetBookmarks() {
2259   base::Value dict = TraverseBookmarks(nullptr, 0);
2260   DCHECK(dict.is_dict());
2261   // The root bookmark contains no useful information.
2262   base::Value* children = dict.FindListKey("children");
2263   DCHECK(children);
2264   return std::move(*children);
2265 }
2266 
TraverseBookmarks(FPDF_BOOKMARK bookmark,unsigned int depth)2267 base::Value PDFiumEngine::TraverseBookmarks(FPDF_BOOKMARK bookmark,
2268                                             unsigned int depth) {
2269   base::Value dict(base::Value::Type::DICTIONARY);
2270   base::string16 title = CallPDFiumWideStringBufferApi(
2271       base::BindRepeating(&FPDFBookmark_GetTitle, bookmark),
2272       /*check_expected_size=*/true);
2273   dict.SetStringKey("title", title);
2274 
2275   FPDF_DEST dest = FPDFBookmark_GetDest(doc(), bookmark);
2276   // Some bookmarks don't have a page to select.
2277   if (dest) {
2278     int page_index = FPDFDest_GetDestPageIndex(doc(), dest);
2279     if (PageIndexInBounds(page_index)) {
2280       dict.SetIntKey("page", page_index);
2281 
2282       base::Optional<gfx::PointF> xy;
2283       base::Optional<float> zoom;
2284       pages_[page_index]->GetPageDestinationTarget(dest, &xy, &zoom);
2285       if (xy) {
2286         dict.SetIntKey("x", static_cast<int>(xy.value().x()));
2287         dict.SetIntKey("y", static_cast<int>(xy.value().y()));
2288       }
2289       if (zoom) {
2290         dict.SetDoubleKey("zoom", zoom.value());
2291       }
2292     }
2293   } else {
2294     // Extract URI for bookmarks linking to an external page.
2295     FPDF_ACTION action = FPDFBookmark_GetAction(bookmark);
2296     std::string uri = CallPDFiumStringBufferApi(
2297         base::BindRepeating(&FPDFAction_GetURIPath, doc(), action),
2298         /*check_expected_size=*/true);
2299     if (!uri.empty())
2300       dict.SetStringKey("uri", uri);
2301   }
2302 
2303   base::Value children(base::Value::Type::LIST);
2304 
2305   // Don't trust PDFium to handle circular bookmarks.
2306   constexpr unsigned int kMaxDepth = 128;
2307   if (depth < kMaxDepth) {
2308     std::set<FPDF_BOOKMARK> seen_bookmarks;
2309     for (FPDF_BOOKMARK child_bookmark =
2310              FPDFBookmark_GetFirstChild(doc(), bookmark);
2311          child_bookmark;
2312          child_bookmark = FPDFBookmark_GetNextSibling(doc(), child_bookmark)) {
2313       if (base::Contains(seen_bookmarks, child_bookmark))
2314         break;
2315 
2316       seen_bookmarks.insert(child_bookmark);
2317       children.Append(TraverseBookmarks(child_bookmark, depth + 1));
2318     }
2319   }
2320   dict.SetKey("children", std::move(children));
2321   return dict;
2322 }
2323 
ScrollBasedOnScrollAlignment(const gfx::Rect & scroll_rect,const PP_PdfAccessibilityScrollAlignment & horizontal_scroll_alignment,const PP_PdfAccessibilityScrollAlignment & vertical_scroll_alignment)2324 void PDFiumEngine::ScrollBasedOnScrollAlignment(
2325     const gfx::Rect& scroll_rect,
2326     const PP_PdfAccessibilityScrollAlignment& horizontal_scroll_alignment,
2327     const PP_PdfAccessibilityScrollAlignment& vertical_scroll_alignment) {
2328   gfx::Vector2d scroll_offset = GetScreenRect(scroll_rect).OffsetFromOrigin();
2329   switch (horizontal_scroll_alignment) {
2330     case PP_PdfAccessibilityScrollAlignment::PP_PDF_SCROLL_ALIGNMENT_RIGHT:
2331       scroll_offset.set_x(scroll_offset.x() - plugin_size_.width());
2332       break;
2333     case PP_PDF_SCROLL_ALIGNMENT_CENTER:
2334       scroll_offset.set_x(scroll_offset.x() - (plugin_size_.width() / 2));
2335       break;
2336     case PP_PDF_SCROLL_ALIGNMENT_CLOSEST_EDGE: {
2337       scroll_offset.set_x((std::abs(scroll_offset.x()) <=
2338                            std::abs(scroll_offset.x() - plugin_size_.width()))
2339                               ? scroll_offset.x()
2340                               : scroll_offset.x() - plugin_size_.width());
2341       break;
2342     }
2343     case PP_PDF_SCROLL_NONE:
2344       scroll_offset.set_x(0);
2345       break;
2346     case PP_PDF_SCROLL_ALIGNMENT_LEFT:
2347     case PP_PDF_SCROLL_ALIGNMENT_TOP:
2348     case PP_PDF_SCROLL_ALIGNMENT_BOTTOM:
2349     default:
2350       break;
2351   }
2352 
2353   switch (vertical_scroll_alignment) {
2354     case PP_PDF_SCROLL_ALIGNMENT_BOTTOM:
2355       scroll_offset.set_y(scroll_offset.y() - plugin_size_.height());
2356       break;
2357     case PP_PDF_SCROLL_ALIGNMENT_CENTER:
2358       scroll_offset.set_y(scroll_offset.y() - (plugin_size_.height() / 2));
2359       break;
2360     case PP_PDF_SCROLL_ALIGNMENT_CLOSEST_EDGE: {
2361       scroll_offset.set_y((std::abs(scroll_offset.y()) <=
2362                            std::abs(scroll_offset.y() - plugin_size_.height()))
2363                               ? scroll_offset.y()
2364                               : scroll_offset.y() - plugin_size_.height());
2365       break;
2366     }
2367     case PP_PDF_SCROLL_NONE:
2368       scroll_offset.set_y(0);
2369       break;
2370     case PP_PDF_SCROLL_ALIGNMENT_TOP:
2371     case PP_PDF_SCROLL_ALIGNMENT_LEFT:
2372     case PP_PDF_SCROLL_ALIGNMENT_RIGHT:
2373     default:
2374       break;
2375   }
2376 
2377   client_->ScrollBy(scroll_offset);
2378 }
2379 
ScrollToGlobalPoint(const gfx::Rect & target_rect,const gfx::Point & global_point)2380 void PDFiumEngine::ScrollToGlobalPoint(const gfx::Rect& target_rect,
2381                                        const gfx::Point& global_point) {
2382   gfx::Point scroll_offset = GetScreenRect(target_rect).origin();
2383   client_->ScrollBy(scroll_offset - global_point);
2384 }
2385 
GetNamedDestination(const std::string & destination)2386 base::Optional<PDFEngine::NamedDestination> PDFiumEngine::GetNamedDestination(
2387     const std::string& destination) {
2388   // Look for the destination.
2389   FPDF_DEST dest = FPDF_GetNamedDestByName(doc(), destination.c_str());
2390   if (!dest) {
2391     // Look for a bookmark with the same name.
2392     base::string16 destination_wide = base::UTF8ToUTF16(destination);
2393     FPDF_WIDESTRING destination_pdf_wide =
2394         reinterpret_cast<FPDF_WIDESTRING>(destination_wide.c_str());
2395     FPDF_BOOKMARK bookmark = FPDFBookmark_Find(doc(), destination_pdf_wide);
2396     if (bookmark)
2397       dest = FPDFBookmark_GetDest(doc(), bookmark);
2398   }
2399 
2400   if (!dest)
2401     return {};
2402 
2403   int page = FPDFDest_GetDestPageIndex(doc(), dest);
2404   if (page < 0)
2405     return {};
2406 
2407   PDFEngine::NamedDestination result;
2408   result.page = page;
2409   unsigned long view_int =
2410       FPDFDest_GetView(dest, &result.num_params, result.params);
2411   result.view = ConvertViewIntToViewString(view_int);
2412   return result;
2413 }
2414 
GetMostVisiblePage()2415 int PDFiumEngine::GetMostVisiblePage() {
2416   if (in_flight_visible_page_)
2417     return *in_flight_visible_page_;
2418 
2419   // We can call GetMostVisiblePage through a callback from PDFium. We have
2420   // to defer the page deletion otherwise we could potentially delete the page
2421   // that originated the calling JS request and destroy the objects that are
2422   // currently being used.
2423   base::AutoReset<bool> defer_page_unload_guard(&defer_page_unload_, true);
2424   CalculateVisiblePages();
2425   return most_visible_page_;
2426 }
2427 
GetPageBoundsRect(int index)2428 gfx::Rect PDFiumEngine::GetPageBoundsRect(int index) {
2429   return pages_[index]->rect();
2430 }
2431 
GetPageContentsRect(int index)2432 gfx::Rect PDFiumEngine::GetPageContentsRect(int index) {
2433   return GetScreenRect(pages_[index]->rect());
2434 }
2435 
GetVerticalScrollbarYPosition()2436 int PDFiumEngine::GetVerticalScrollbarYPosition() {
2437   return position_.y();
2438 }
2439 
SetGrayscale(bool grayscale)2440 void PDFiumEngine::SetGrayscale(bool grayscale) {
2441   render_grayscale_ = grayscale;
2442 }
2443 
HandleLongPress(const TouchInputEvent & event)2444 void PDFiumEngine::HandleLongPress(const TouchInputEvent& event) {
2445   base::AutoReset<bool> handling_long_press_guard(&handling_long_press_, true);
2446   gfx::Point point = gfx::ToRoundedPoint(event.GetTargetTouchPoint());
2447 
2448   // Send a fake mouse down to trigger the multi-click selection code.
2449   MouseInputEvent mouse_event(
2450       InputEventType::kMouseDown, event.GetTimeStamp(), event.GetModifiers(),
2451       InputEventMouseButtonType::kLeft, point, 2, point);
2452 
2453   OnMouseDown(mouse_event);
2454 }
2455 
GetCharCount(int page_index)2456 int PDFiumEngine::GetCharCount(int page_index) {
2457   DCHECK(PageIndexInBounds(page_index));
2458   return pages_[page_index]->GetCharCount();
2459 }
2460 
GetCharBounds(int page_index,int char_index)2461 gfx::RectF PDFiumEngine::GetCharBounds(int page_index, int char_index) {
2462   DCHECK(PageIndexInBounds(page_index));
2463   return pages_[page_index]->GetCharBounds(char_index);
2464 }
2465 
GetCharUnicode(int page_index,int char_index)2466 uint32_t PDFiumEngine::GetCharUnicode(int page_index, int char_index) {
2467   DCHECK(PageIndexInBounds(page_index));
2468   return pages_[page_index]->GetCharUnicode(char_index);
2469 }
2470 
2471 base::Optional<pp::PDF::PrivateAccessibilityTextRunInfo>
GetTextRunInfo(int page_index,int start_char_index)2472 PDFiumEngine::GetTextRunInfo(int page_index, int start_char_index) {
2473   DCHECK(PageIndexInBounds(page_index));
2474   auto info = pages_[page_index]->GetTextRunInfo(start_char_index);
2475   if (!client_->IsPrintPreview() && start_char_index >= 0)
2476     pages_[page_index]->LogOverlappingAnnotations();
2477   return info;
2478 }
2479 
GetLinkInfo(int page_index)2480 std::vector<PDFEngine::AccessibilityLinkInfo> PDFiumEngine::GetLinkInfo(
2481     int page_index) {
2482   DCHECK(PageIndexInBounds(page_index));
2483   return pages_[page_index]->GetLinkInfo();
2484 }
2485 
GetImageInfo(int page_index)2486 std::vector<PDFEngine::AccessibilityImageInfo> PDFiumEngine::GetImageInfo(
2487     int page_index) {
2488   DCHECK(PageIndexInBounds(page_index));
2489   return pages_[page_index]->GetImageInfo();
2490 }
2491 
2492 std::vector<PDFEngine::AccessibilityHighlightInfo>
GetHighlightInfo(int page_index)2493 PDFiumEngine::GetHighlightInfo(int page_index) {
2494   DCHECK(PageIndexInBounds(page_index));
2495   return pages_[page_index]->GetHighlightInfo();
2496 }
2497 
2498 std::vector<PDFEngine::AccessibilityTextFieldInfo>
GetTextFieldInfo(int page_index)2499 PDFiumEngine::GetTextFieldInfo(int page_index) {
2500   DCHECK(PageIndexInBounds(page_index));
2501   return pages_[page_index]->GetTextFieldInfo();
2502 }
2503 
GetPrintScaling()2504 bool PDFiumEngine::GetPrintScaling() {
2505   return !!FPDF_VIEWERREF_GetPrintScaling(doc());
2506 }
2507 
GetCopiesToPrint()2508 int PDFiumEngine::GetCopiesToPrint() {
2509   return FPDF_VIEWERREF_GetNumCopies(doc());
2510 }
2511 
GetDuplexType()2512 int PDFiumEngine::GetDuplexType() {
2513   return static_cast<int>(FPDF_VIEWERREF_GetDuplex(doc()));
2514 }
2515 
GetPageSizeAndUniformity(gfx::Size * size)2516 bool PDFiumEngine::GetPageSizeAndUniformity(gfx::Size* size) {
2517   if (pages_.empty())
2518     return false;
2519 
2520   gfx::Size page_size = GetPageSize(0);
2521   for (size_t i = 1; i < pages_.size(); ++i) {
2522     if (page_size != GetPageSize(i))
2523       return false;
2524   }
2525 
2526   // Convert |page_size| back to points.
2527   size->set_width(
2528       ConvertUnit(page_size.width(), kPixelsPerInch, kPointsPerInch));
2529   size->set_height(
2530       ConvertUnit(page_size.height(), kPixelsPerInch, kPointsPerInch));
2531   return true;
2532 }
2533 
AppendBlankPages(size_t num_pages)2534 void PDFiumEngine::AppendBlankPages(size_t num_pages) {
2535   DCHECK_GT(num_pages, 0U);
2536 
2537   if (!doc())
2538     return;
2539 
2540   selection_.clear();
2541   pending_pages_.clear();
2542 
2543   // Delete all pages except the first one.
2544   while (pages_.size() > 1) {
2545     pages_.pop_back();
2546     FPDFPage_Delete(doc(), pages_.size());
2547   }
2548 
2549   // Create blank pages with the same size as the first page.
2550   gfx::Size page_0_size = GetPageSize(0);
2551   double page_0_width_in_points =
2552       ConvertUnitDouble(page_0_size.width(), kPixelsPerInch, kPointsPerInch);
2553   double page_0_height_in_points =
2554       ConvertUnitDouble(page_0_size.height(), kPixelsPerInch, kPointsPerInch);
2555 
2556   for (size_t i = 1; i < num_pages; ++i) {
2557     {
2558       // Add a new page to the document, but delete the FPDF_PAGE object.
2559       ScopedFPDFPage temp_page(FPDFPage_New(doc(), i, page_0_width_in_points,
2560                                             page_0_height_in_points));
2561     }
2562 
2563     auto page = std::make_unique<PDFiumPage>(this, i);
2564     page->MarkAvailable();
2565     pages_.push_back(std::move(page));
2566   }
2567 
2568   DCHECK(document_loaded_);
2569   LoadPageInfo();
2570 }
2571 
LoadDocument()2572 void PDFiumEngine::LoadDocument() {
2573   // Check if the document is ready for loading. If it isn't just bail for now,
2574   // we will call LoadDocument() again later.
2575   if (!doc() && !doc_loader_->IsDocumentComplete()) {
2576     FX_DOWNLOADHINTS& download_hints = document_->download_hints();
2577     if (!FPDFAvail_IsDocAvail(fpdf_availability(), &download_hints))
2578       return;
2579   }
2580 
2581   // If we're in the middle of getting a password, just return. We will retry
2582   // loading the document after we get the password anyway.
2583   if (getting_password_)
2584     return;
2585 
2586   bool needs_password = false;
2587   if (TryLoadingDoc(std::string(), &needs_password)) {
2588     ContinueLoadingDocument(std::string());
2589     return;
2590   }
2591   if (needs_password)
2592     GetPasswordAndLoad();
2593   else
2594     client_->DocumentLoadFailed();
2595 }
2596 
TryLoadingDoc(const std::string & password,bool * needs_password)2597 bool PDFiumEngine::TryLoadingDoc(const std::string& password,
2598                                  bool* needs_password) {
2599   *needs_password = false;
2600   if (doc()) {
2601     // This is probably not necessary, because it should have already been
2602     // called below in the |doc_| initialization path. However, the previous
2603     // call may have failed, so call it again for good measure.
2604     FX_DOWNLOADHINTS& download_hints = document_->download_hints();
2605     FPDFAvail_IsDocAvail(fpdf_availability(), &download_hints);
2606     return true;
2607   }
2608 
2609   if (!password.empty())
2610     password_tries_remaining_--;
2611 
2612   {
2613     ScopedUnsupportedFeature scoped_unsupported_feature(this);
2614     document_->LoadDocument(password);
2615   }
2616 
2617   if (!doc()) {
2618     if (FPDF_GetLastError() == FPDF_ERR_PASSWORD)
2619       *needs_password = true;
2620     return false;
2621   }
2622 
2623   // Always call FPDFAvail_IsDocAvail() so PDFium initializes internal data
2624   // structures.
2625   FX_DOWNLOADHINTS& download_hints = document_->download_hints();
2626   FPDFAvail_IsDocAvail(fpdf_availability(), &download_hints);
2627   return true;
2628 }
2629 
GetPasswordAndLoad()2630 void PDFiumEngine::GetPasswordAndLoad() {
2631   getting_password_ = true;
2632   DCHECK(!doc());
2633   DCHECK_EQ(static_cast<unsigned long>(FPDF_ERR_PASSWORD), FPDF_GetLastError());
2634   client_->GetDocumentPassword(base::BindOnce(
2635       &PDFiumEngine::OnGetPasswordComplete, weak_factory_.GetWeakPtr()));
2636 }
2637 
OnGetPasswordComplete(const std::string & password)2638 void PDFiumEngine::OnGetPasswordComplete(const std::string& password) {
2639   getting_password_ = false;
2640   ContinueLoadingDocument(password);
2641 }
2642 
ContinueLoadingDocument(const std::string & password)2643 void PDFiumEngine::ContinueLoadingDocument(const std::string& password) {
2644   bool needs_password = false;
2645   bool loaded = TryLoadingDoc(password, &needs_password);
2646   bool password_incorrect = !loaded && needs_password && !password.empty();
2647   if (password_incorrect && password_tries_remaining_ > 0) {
2648     GetPasswordAndLoad();
2649     return;
2650   }
2651 
2652   if (!doc()) {
2653     client_->DocumentLoadFailed();
2654     return;
2655   }
2656 
2657   if (FPDFDoc_GetPageMode(doc()) == PAGEMODE_USEOUTLINES)
2658     client_->DocumentHasUnsupportedFeature("Bookmarks");
2659 
2660   permissions_ = std::make_unique<PDFiumPermissions>(doc());
2661 
2662   LoadBody();
2663 
2664   if (doc_loader_->IsDocumentComplete())
2665     FinishLoadingDocument();
2666 }
2667 
LoadPageInfo()2668 void PDFiumEngine::LoadPageInfo() {
2669   RefreshCurrentDocumentLayout();
2670 
2671   // TODO(crbug.com/1013800): RefreshCurrentDocumentLayout() should send some
2672   // sort of "current layout changed" notification, instead of proposing a new
2673   // layout. Proposals are never rejected currently, so this is OK for now.
2674   ProposeNextDocumentLayout();
2675 }
2676 
RefreshCurrentDocumentLayout()2677 void PDFiumEngine::RefreshCurrentDocumentLayout() {
2678   UpdateDocumentLayout(&layout_);
2679   if (!layout_.dirty())
2680     return;
2681 
2682   DCHECK_EQ(pages_.size(), layout_.page_count());
2683   for (size_t i = 0; i < layout_.page_count(); ++i) {
2684     // TODO(kmoon): This should be the only place that sets |PDFiumPage::rect_|.
2685     pages_[i]->set_rect(layout_.page_bounds_rect(i));
2686   }
2687 
2688   layout_.clear_dirty();
2689 
2690   CalculateVisiblePages();
2691 }
2692 
ProposeNextDocumentLayout()2693 void PDFiumEngine::ProposeNextDocumentLayout() {
2694   DocumentLayout next_layout;
2695   next_layout.SetOptions(desired_layout_options_);
2696   UpdateDocumentLayout(&next_layout);
2697 
2698   // The time windows between proposal and application may overlap, so we must
2699   // always propose a new layout, regardless of the current layout state.
2700   client_->ProposeDocumentLayout(next_layout);
2701 }
2702 
UpdateDocumentLayout(DocumentLayout * layout)2703 void PDFiumEngine::UpdateDocumentLayout(DocumentLayout* layout) {
2704   std::vector<gfx::Size> page_sizes = LoadPageSizes(layout->options());
2705   if (page_sizes.empty())
2706     return;
2707 
2708   if (layout->options().two_up_view_enabled()) {
2709     layout->ComputeTwoUpViewLayout(page_sizes);
2710   } else {
2711     layout->ComputeSingleViewLayout(page_sizes);
2712   }
2713 }
2714 
LoadPageSizes(const DocumentLayout::Options & layout_options)2715 std::vector<gfx::Size> PDFiumEngine::LoadPageSizes(
2716     const DocumentLayout::Options& layout_options) {
2717   std::vector<gfx::Size> page_sizes;
2718   if (!doc_loader_)
2719     return page_sizes;
2720   if (pages_.empty() && document_loaded_)
2721     return page_sizes;
2722 
2723   pending_pages_.clear();
2724   size_t new_page_count = FPDF_GetPageCount(doc());
2725 
2726   bool doc_complete = doc_loader_->IsDocumentComplete();
2727   bool is_linear =
2728       FPDFAvail_IsLinearized(fpdf_availability()) == PDF_LINEARIZED;
2729   for (size_t i = 0; i < new_page_count; ++i) {
2730     // Get page availability. If |document_loaded_| == true and the page is not
2731     // new, then the page has been constructed already. Get page availability
2732     // flag from already existing PDFiumPage object. If |document_loaded_| ==
2733     // false or the page is new, then the page may not be fully loaded yet.
2734     bool page_available;
2735     if (document_loaded_ && i < pages_.size()) {
2736       page_available = pages_[i]->available();
2737     } else if (is_linear) {
2738       FX_DOWNLOADHINTS& download_hints = document_->download_hints();
2739       int linear_page_avail =
2740           FPDFAvail_IsPageAvail(fpdf_availability(), i, &download_hints);
2741       page_available = linear_page_avail == PDF_DATA_AVAIL;
2742     } else {
2743       page_available = doc_complete;
2744     }
2745 
2746     // TODO(crbug.com/1013800): It'd be better if page size were independent of
2747     // layout options, and handled in the layout code.
2748     gfx::Size size = page_available ? GetPageSizeForLayout(i, layout_options)
2749                                     : default_page_size_;
2750     EnlargePage(layout_options, i, new_page_count, &size);
2751     page_sizes.push_back(size);
2752   }
2753 
2754   // Add new pages. If |document_loaded_| == false, do not mark page as
2755   // available even if |doc_complete| is true because FPDFAvail_IsPageAvail()
2756   // still has to be called for this page, which will be done in
2757   // FinishLoadingDocument().
2758   for (size_t i = pages_.size(); i < new_page_count; ++i) {
2759     auto page = std::make_unique<PDFiumPage>(this, i);
2760     if (document_loaded_ &&
2761         FPDFAvail_IsPageAvail(fpdf_availability(), i, nullptr))
2762       page->MarkAvailable();
2763     pages_.push_back(std::move(page));
2764   }
2765 
2766   // Remove pages that do not exist anymore.
2767   if (pages_.size() > new_page_count) {
2768     for (size_t i = new_page_count; i < pages_.size(); ++i)
2769       pages_[i]->Unload();
2770 
2771     pages_.resize(new_page_count);
2772   }
2773 
2774   return page_sizes;
2775 }
2776 
LoadBody()2777 void PDFiumEngine::LoadBody() {
2778   DCHECK(doc());
2779   DCHECK(fpdf_availability());
2780   if (doc_loader_->IsDocumentComplete()) {
2781     LoadForm();
2782   } else if (FPDFAvail_IsLinearized(fpdf_availability()) == PDF_LINEARIZED &&
2783              FPDF_GetPageCount(doc()) == 1) {
2784     // If we have only one page we should load form first, bacause it is may be
2785     // XFA document. And after loading form the page count and its contents may
2786     // be changed.
2787     LoadForm();
2788     if (document_->form_status() == PDF_FORM_NOTAVAIL)
2789       return;
2790   }
2791   LoadPages();
2792 }
2793 
LoadPages()2794 void PDFiumEngine::LoadPages() {
2795   if (pages_.empty()) {
2796     if (!doc_loader_->IsDocumentComplete()) {
2797       // Check if the first page is available.  In a linearized PDF, that is not
2798       // always page 0.  Doing this gives us the default page size, since when
2799       // the document is available, the first page is available as well.
2800       CheckPageAvailable(FPDFAvail_GetFirstPageNum(doc()), &pending_pages_);
2801     }
2802     DCHECK(!document_loaded_);
2803     LoadPageInfo();
2804   }
2805 }
2806 
LoadForm()2807 void PDFiumEngine::LoadForm() {
2808   if (form())
2809     return;
2810 
2811   DCHECK(doc());
2812   document_->SetFormStatus();
2813   if (document_->form_status() != PDF_FORM_NOTAVAIL ||
2814       doc_loader_->IsDocumentComplete()) {
2815     {
2816       ScopedUnsupportedFeature scoped_unsupported_feature(this);
2817       document_->InitializeForm(&form_filler_);
2818     }
2819 
2820     if (form_filler_.script_option() ==
2821         PDFiumFormFiller::ScriptOption::kJavaScriptAndXFA) {
2822       FPDF_LoadXFA(doc());
2823     }
2824     FPDF_SetFormFieldHighlightColor(form(), FPDF_FORMFIELD_UNKNOWN,
2825                                     kFormHighlightColor);
2826     FPDF_SetFormFieldHighlightAlpha(form(), kFormHighlightAlpha);
2827 
2828     if (base::FeatureList::IsEnabled(features::kTabAcrossPDFAnnotations) &&
2829         !client_->IsPrintPreview()) {
2830       static constexpr FPDF_ANNOTATION_SUBTYPE kFocusableAnnotSubtypes[] = {
2831           FPDF_ANNOT_LINK, FPDF_ANNOT_HIGHLIGHT, FPDF_ANNOT_WIDGET};
2832       FPDF_BOOL ret = FPDFAnnot_SetFocusableSubtypes(
2833           form(), kFocusableAnnotSubtypes, base::size(kFocusableAnnotSubtypes));
2834       DCHECK(ret);
2835     }
2836   }
2837 }
2838 
CalculateVisiblePages()2839 void PDFiumEngine::CalculateVisiblePages() {
2840   // Early return if the PDF isn't being loaded or if we don't have the document
2841   // info yet. The latter is important because otherwise as the PDF is being
2842   // initialized by the renderer there could be races that call this method
2843   // before we get the initial network responses. The document loader depends on
2844   // the list of pending requests to be valid for progressive loading to
2845   // function.
2846   if (!doc_loader_ || pages_.empty())
2847     return;
2848   // Clear pending requests queue, since it may contain requests to the pages
2849   // that are already invisible (after scrolling for example).
2850   pending_pages_.clear();
2851   doc_loader_->ClearPendingRequests();
2852 
2853   visible_pages_.clear();
2854   gfx::Rect visible_rect(plugin_size_);
2855   for (int i = 0; i < static_cast<int>(pages_.size()); ++i) {
2856     // Check an entire PageScreenRect, since we might need to repaint side
2857     // borders and shadows even if the page itself is not visible.
2858     // For example, when user use pdf with different page sizes and zoomed in
2859     // outside page area.
2860     if (visible_rect.Intersects(GetPageScreenRect(i))) {
2861       visible_pages_.push_back(i);
2862       CheckPageAvailable(i, &pending_pages_);
2863     } else {
2864       // Need to unload pages when we're not using them, since some PDFs use a
2865       // lot of memory.  See http://crbug.com/48791
2866       if (defer_page_unload_) {
2867         deferred_page_unloads_.push_back(i);
2868       } else {
2869         pages_[i]->Unload();
2870       }
2871     }
2872   }
2873 
2874   // Any pending highlighting of form fields will be invalid since these are in
2875   // screen coordinates.
2876   form_highlights_.clear();
2877 
2878   std::vector<draw_utils::IndexedPage> visible_pages_rects;
2879   visible_pages_rects.reserve(visible_pages_.size());
2880   for (int visible_page_index : visible_pages_) {
2881     visible_pages_rects.emplace_back(visible_page_index,
2882                                      pages_[visible_page_index]->rect());
2883   }
2884 
2885   int most_visible_page =
2886       draw_utils::GetMostVisiblePage(visible_pages_rects, GetVisibleRect());
2887   SetCurrentPage(most_visible_page);
2888 }
2889 
IsPageVisible(int index) const2890 bool PDFiumEngine::IsPageVisible(int index) const {
2891   return base::Contains(visible_pages_, index);
2892 }
2893 
ScrollToPage(int page)2894 void PDFiumEngine::ScrollToPage(int page) {
2895   if (!PageIndexInBounds(page))
2896     return;
2897 
2898   in_flight_visible_page_ = page;
2899   client_->ScrollToPage(page);
2900 }
2901 
CheckPageAvailable(int index,std::vector<int> * pending)2902 bool PDFiumEngine::CheckPageAvailable(int index, std::vector<int>* pending) {
2903   if (!doc())
2904     return false;
2905 
2906   const int num_pages = static_cast<int>(pages_.size());
2907   if (index < num_pages && pages_[index]->available())
2908     return true;
2909 
2910   FX_DOWNLOADHINTS& download_hints = document_->download_hints();
2911   if (!FPDFAvail_IsPageAvail(fpdf_availability(), index, &download_hints)) {
2912     if (!base::Contains(*pending, index))
2913       pending->push_back(index);
2914     return false;
2915   }
2916 
2917   if (index < num_pages)
2918     pages_[index]->MarkAvailable();
2919   if (default_page_size_.IsEmpty())
2920     default_page_size_ = GetPageSize(index);
2921   return true;
2922 }
2923 
GetPageSize(int index)2924 gfx::Size PDFiumEngine::GetPageSize(int index) {
2925   return GetPageSizeForLayout(index, layout_.options());
2926 }
2927 
GetPageSizeForLayout(int index,const DocumentLayout::Options & layout_options)2928 gfx::Size PDFiumEngine::GetPageSizeForLayout(
2929     int index,
2930     const DocumentLayout::Options& layout_options) {
2931   FS_SIZEF size_in_points;
2932   if (!FPDF_GetPageSizeByIndexF(doc(), index, &size_in_points))
2933     return gfx::Size();
2934 
2935   int width_in_pixels = static_cast<int>(
2936       ConvertUnitDouble(size_in_points.width, kPointsPerInch, kPixelsPerInch));
2937   int height_in_pixels = static_cast<int>(
2938       ConvertUnitDouble(size_in_points.height, kPointsPerInch, kPixelsPerInch));
2939 
2940   switch (layout_options.default_page_orientation()) {
2941     case PageOrientation::kOriginal:
2942     case PageOrientation::kClockwise180:
2943       // No axis swap needed.
2944       break;
2945     case PageOrientation::kClockwise90:
2946     case PageOrientation::kClockwise270:
2947       // Rotated 90 degrees: swap axes.
2948       std::swap(width_in_pixels, height_in_pixels);
2949       break;
2950   }
2951 
2952   return gfx::Size(width_in_pixels, height_in_pixels);
2953 }
2954 
GetInsetSizes(const DocumentLayout::Options & layout_options,size_t page_index,size_t num_of_pages) const2955 draw_utils::PageInsetSizes PDFiumEngine::GetInsetSizes(
2956     const DocumentLayout::Options& layout_options,
2957     size_t page_index,
2958     size_t num_of_pages) const {
2959   DCHECK_LT(page_index, num_of_pages);
2960 
2961   if (layout_options.two_up_view_enabled()) {
2962     return draw_utils::GetPageInsetsForTwoUpView(
2963         page_index, num_of_pages, DocumentLayout::kSingleViewInsets,
2964         DocumentLayout::kHorizontalSeparator);
2965   }
2966 
2967   return DocumentLayout::kSingleViewInsets;
2968 }
2969 
EnlargePage(const DocumentLayout::Options & layout_options,size_t page_index,size_t num_of_pages,gfx::Size * page_size) const2970 void PDFiumEngine::EnlargePage(const DocumentLayout::Options& layout_options,
2971                                size_t page_index,
2972                                size_t num_of_pages,
2973                                gfx::Size* page_size) const {
2974   draw_utils::PageInsetSizes inset_sizes =
2975       GetInsetSizes(layout_options, page_index, num_of_pages);
2976   page_size->Enlarge(inset_sizes.left + inset_sizes.right,
2977                      inset_sizes.top + inset_sizes.bottom);
2978 }
2979 
InsetPage(const DocumentLayout::Options & layout_options,size_t page_index,size_t num_of_pages,double multiplier,gfx::Rect & rect) const2980 void PDFiumEngine::InsetPage(const DocumentLayout::Options& layout_options,
2981                              size_t page_index,
2982                              size_t num_of_pages,
2983                              double multiplier,
2984                              gfx::Rect& rect) const {
2985   draw_utils::PageInsetSizes inset_sizes =
2986       GetInsetSizes(layout_options, page_index, num_of_pages);
2987   rect.Inset(static_cast<int>(ceil(inset_sizes.left * multiplier)),
2988              static_cast<int>(ceil(inset_sizes.top * multiplier)),
2989              static_cast<int>(ceil(inset_sizes.right * multiplier)),
2990              static_cast<int>(ceil(inset_sizes.bottom * multiplier)));
2991 }
2992 
GetAdjacentPageIndexForTwoUpView(size_t page_index,size_t num_of_pages) const2993 base::Optional<size_t> PDFiumEngine::GetAdjacentPageIndexForTwoUpView(
2994     size_t page_index,
2995     size_t num_of_pages) const {
2996   DCHECK_LT(page_index, num_of_pages);
2997 
2998   if (!layout_.options().two_up_view_enabled())
2999     return base::nullopt;
3000 
3001   int adjacent_page_offset = page_index % 2 ? -1 : 1;
3002   size_t adjacent_page_index = page_index + adjacent_page_offset;
3003   if (adjacent_page_index >= num_of_pages)
3004     return base::nullopt;
3005 
3006   return adjacent_page_index;
3007 }
3008 
StartPaint(int page_index,const gfx::Rect & dirty)3009 int PDFiumEngine::StartPaint(int page_index, const gfx::Rect& dirty) {
3010   // For the first time we hit paint, do nothing and just record the paint for
3011   // the next callback.  This keeps the UI responsive in case the user is doing
3012   // a lot of scrolling.
3013   progressive_paints_.emplace_back(page_index, dirty);
3014   return progressive_paints_.size() - 1;
3015 }
3016 
ContinuePaint(int progressive_index,SkBitmap & image_data)3017 bool PDFiumEngine::ContinuePaint(int progressive_index, SkBitmap& image_data) {
3018   DCHECK_GE(progressive_index, 0);
3019   DCHECK_LT(static_cast<size_t>(progressive_index), progressive_paints_.size());
3020 
3021   last_progressive_start_time_ = base::Time::Now();
3022 #if defined(OS_LINUX) || defined(OS_CHROMEOS) || defined(OS_BSD)
3023   SetLastInstance(client_->GetPluginInstance());
3024 #endif
3025 
3026   int page_index = progressive_paints_[progressive_index].page_index();
3027   DCHECK(PageIndexInBounds(page_index));
3028 
3029   int rv;
3030   FPDF_PAGE page = pages_[page_index]->GetPage();
3031   if (progressive_paints_[progressive_index].bitmap()) {
3032     rv = FPDF_RenderPage_Continue(page, this);
3033   } else {
3034     int start_x;
3035     int start_y;
3036     int size_x;
3037     int size_y;
3038     gfx::Rect dirty = progressive_paints_[progressive_index].rect();
3039     GetPDFiumRect(page_index, dirty, &start_x, &start_y, &size_x, &size_y);
3040 
3041     ScopedFPDFBitmap new_bitmap = CreateBitmap(dirty, image_data);
3042     FPDFBitmap_FillRect(new_bitmap.get(), start_x, start_y, size_x, size_y,
3043                         0xFFFFFFFF);
3044     rv = FPDF_RenderPageBitmap_Start(
3045         new_bitmap.get(), page, start_x, start_y, size_x, size_y,
3046         ToPDFiumRotation(layout_.options().default_page_orientation()),
3047         GetRenderingFlags(), this);
3048     progressive_paints_[progressive_index].SetBitmapAndImageData(
3049         std::move(new_bitmap), image_data);
3050   }
3051   return rv != FPDF_RENDER_TOBECONTINUED;
3052 }
3053 
FinishPaint(int progressive_index,SkBitmap & image_data)3054 void PDFiumEngine::FinishPaint(int progressive_index, SkBitmap& image_data) {
3055   DCHECK_GE(progressive_index, 0);
3056   DCHECK_LT(static_cast<size_t>(progressive_index), progressive_paints_.size());
3057 
3058   int page_index = progressive_paints_[progressive_index].page_index();
3059   gfx::Rect dirty_in_screen = progressive_paints_[progressive_index].rect();
3060 
3061   int start_x;
3062   int start_y;
3063   int size_x;
3064   int size_y;
3065   FPDF_BITMAP bitmap = progressive_paints_[progressive_index].bitmap();
3066   GetPDFiumRect(page_index, dirty_in_screen, &start_x, &start_y, &size_x,
3067                 &size_y);
3068 
3069   // Draw the forms.
3070   FPDF_FFLDraw(form(), bitmap, pages_[page_index]->GetPage(), start_x, start_y,
3071                size_x, size_y,
3072                ToPDFiumRotation(layout_.options().default_page_orientation()),
3073                GetRenderingFlags());
3074 
3075   FillPageSides(progressive_index);
3076 
3077   // Paint the page shadows.
3078   PaintPageShadow(progressive_index, image_data);
3079 
3080   DrawSelections(progressive_index, image_data);
3081   form_highlights_.clear();
3082 
3083   FPDF_RenderPage_Close(pages_[page_index]->GetPage());
3084   progressive_paints_.erase(progressive_paints_.begin() + progressive_index);
3085 }
3086 
CancelPaints()3087 void PDFiumEngine::CancelPaints() {
3088   for (const auto& paint : progressive_paints_)
3089     FPDF_RenderPage_Close(pages_[paint.page_index()]->GetPage());
3090 
3091   progressive_paints_.clear();
3092 }
3093 
FillPageSides(int progressive_index)3094 void PDFiumEngine::FillPageSides(int progressive_index) {
3095   DCHECK_GE(progressive_index, 0);
3096   DCHECK_LT(static_cast<size_t>(progressive_index), progressive_paints_.size());
3097 
3098   int page_index = progressive_paints_[progressive_index].page_index();
3099   gfx::Rect dirty_in_screen = progressive_paints_[progressive_index].rect();
3100   FPDF_BITMAP bitmap = progressive_paints_[progressive_index].bitmap();
3101   draw_utils::PageInsetSizes inset_sizes =
3102       GetInsetSizes(layout_.options(), page_index, pages_.size());
3103 
3104   gfx::Rect page_rect = pages_[page_index]->rect();
3105   const bool is_two_up_view = layout_.options().two_up_view_enabled();
3106   if (page_rect.x() > 0 && (!is_two_up_view || page_index % 2 == 0)) {
3107     // If in two-up view, only need to draw the left empty space for left pages
3108     // since the gap between the left and right page will be drawn by the left
3109     // page.
3110     gfx::Rect left_in_screen = GetScreenRect(draw_utils::GetLeftFillRect(
3111         page_rect, inset_sizes, DocumentLayout::kBottomSeparator));
3112     left_in_screen.Intersect(dirty_in_screen);
3113 
3114     FPDFBitmap_FillRect(bitmap, left_in_screen.x() - dirty_in_screen.x(),
3115                         left_in_screen.y() - dirty_in_screen.y(),
3116                         left_in_screen.width(), left_in_screen.height(),
3117                         client_->GetBackgroundColor());
3118   }
3119 
3120   if (page_rect.right() < layout_.size().width()) {
3121     gfx::Rect right_in_screen = GetScreenRect(draw_utils::GetRightFillRect(
3122         page_rect, inset_sizes, layout_.size().width(),
3123         DocumentLayout::kBottomSeparator));
3124     right_in_screen.Intersect(dirty_in_screen);
3125 
3126     FPDFBitmap_FillRect(bitmap, right_in_screen.x() - dirty_in_screen.x(),
3127                         right_in_screen.y() - dirty_in_screen.y(),
3128                         right_in_screen.width(), right_in_screen.height(),
3129                         client_->GetBackgroundColor());
3130   }
3131 
3132   gfx::Rect bottom_in_screen;
3133   if (is_two_up_view) {
3134     gfx::Rect page_in_screen = GetScreenRect(page_rect);
3135     bottom_in_screen = draw_utils::GetBottomGapBetweenRects(
3136         page_in_screen.bottom(), dirty_in_screen);
3137 
3138     if (page_index % 2 == 1) {
3139       draw_utils::AdjustBottomGapForRightSidePage(page_in_screen.x(),
3140                                                   &bottom_in_screen);
3141     }
3142 
3143     bottom_in_screen.Intersect(dirty_in_screen);
3144   } else {
3145     bottom_in_screen = GetScreenRect(draw_utils::GetBottomFillRect(
3146         page_rect, inset_sizes, DocumentLayout::kBottomSeparator));
3147     bottom_in_screen.Intersect(dirty_in_screen);
3148   }
3149 
3150   FPDFBitmap_FillRect(bitmap, bottom_in_screen.x() - dirty_in_screen.x(),
3151                       bottom_in_screen.y() - dirty_in_screen.y(),
3152                       bottom_in_screen.width(), bottom_in_screen.height(),
3153                       client_->GetBackgroundColor());
3154 }
3155 
PaintPageShadow(int progressive_index,SkBitmap & image_data)3156 void PDFiumEngine::PaintPageShadow(int progressive_index,
3157                                    SkBitmap& image_data) {
3158   DCHECK_GE(progressive_index, 0);
3159   DCHECK_LT(static_cast<size_t>(progressive_index), progressive_paints_.size());
3160 
3161   int page_index = progressive_paints_[progressive_index].page_index();
3162   gfx::Rect dirty_in_screen = progressive_paints_[progressive_index].rect();
3163   gfx::Rect page_rect = pages_[page_index]->rect();
3164   gfx::Rect shadow_rect(page_rect);
3165   InsetPage(layout_.options(), page_index, pages_.size(), /*multiplier=*/-1,
3166             shadow_rect);
3167 
3168   // Due to the rounding errors of the GetScreenRect it is possible to get
3169   // different size shadows on the left and right sides even they are defined
3170   // the same. To fix this issue let's calculate shadow rect and then shrink
3171   // it by the size of the shadows.
3172   shadow_rect = GetScreenRect(shadow_rect);
3173   page_rect = shadow_rect;
3174   InsetPage(layout_.options(), page_index, pages_.size(),
3175             /*multiplier=*/current_zoom_, page_rect);
3176 
3177   DrawPageShadow(page_rect, shadow_rect, dirty_in_screen, image_data);
3178 }
3179 
DrawSelections(int progressive_index,SkBitmap & image_data) const3180 void PDFiumEngine::DrawSelections(int progressive_index,
3181                                   SkBitmap& image_data) const {
3182   DCHECK_GE(progressive_index, 0);
3183   DCHECK_LT(static_cast<size_t>(progressive_index), progressive_paints_.size());
3184 
3185   int page_index = progressive_paints_[progressive_index].page_index();
3186   gfx::Rect dirty_in_screen = progressive_paints_[progressive_index].rect();
3187 
3188   void* region = nullptr;
3189   int stride;
3190   GetRegion(dirty_in_screen.origin(), image_data, region, stride);
3191 
3192   std::vector<gfx::Rect> highlighted_rects;
3193   gfx::Rect visible_rect = GetVisibleRect();
3194   for (const auto& range : selection_) {
3195     if (range.page_index() != page_index)
3196       continue;
3197 
3198     const std::vector<gfx::Rect>& rects =
3199         range.GetScreenRects(visible_rect.origin(), current_zoom_,
3200                              layout_.options().default_page_orientation());
3201     for (const auto& rect : rects) {
3202       gfx::Rect visible_selection = gfx::IntersectRects(rect, dirty_in_screen);
3203       if (visible_selection.IsEmpty())
3204         continue;
3205 
3206       visible_selection.Offset(-dirty_in_screen.OffsetFromOrigin());
3207       Highlight(region, stride, visible_selection, kHighlightColorR,
3208                 kHighlightColorG, kHighlightColorB, highlighted_rects);
3209     }
3210   }
3211 
3212   for (const auto& highlight : form_highlights_) {
3213     gfx::Rect visible_selection =
3214         gfx::IntersectRects(highlight, dirty_in_screen);
3215     if (visible_selection.IsEmpty())
3216       continue;
3217 
3218     visible_selection.Offset(-dirty_in_screen.OffsetFromOrigin());
3219     Highlight(region, stride, visible_selection, kHighlightColorR,
3220               kHighlightColorG, kHighlightColorB, highlighted_rects);
3221   }
3222 }
3223 
PaintUnavailablePage(int page_index,const gfx::Rect & dirty,SkBitmap & image_data)3224 void PDFiumEngine::PaintUnavailablePage(int page_index,
3225                                         const gfx::Rect& dirty,
3226                                         SkBitmap& image_data) {
3227   int start_x;
3228   int start_y;
3229   int size_x;
3230   int size_y;
3231   GetPDFiumRect(page_index, dirty, &start_x, &start_y, &size_x, &size_y);
3232   ScopedFPDFBitmap bitmap(CreateBitmap(dirty, image_data));
3233   FPDFBitmap_FillRect(bitmap.get(), start_x, start_y, size_x, size_y,
3234                       kPendingPageColor);
3235 
3236   gfx::Rect loading_text_in_screen(
3237       pages_[page_index]->rect().width() / 2,
3238       pages_[page_index]->rect().y() + kLoadingTextVerticalOffset, 0, 0);
3239   loading_text_in_screen = GetScreenRect(loading_text_in_screen);
3240 }
3241 
GetProgressiveIndex(int page_index) const3242 int PDFiumEngine::GetProgressiveIndex(int page_index) const {
3243   for (size_t i = 0; i < progressive_paints_.size(); ++i) {
3244     if (progressive_paints_[i].page_index() == page_index)
3245       return i;
3246   }
3247   return -1;
3248 }
3249 
CreateBitmap(const gfx::Rect & rect,SkBitmap & image_data) const3250 ScopedFPDFBitmap PDFiumEngine::CreateBitmap(const gfx::Rect& rect,
3251                                             SkBitmap& image_data) const {
3252   void* region;
3253   int stride;
3254   GetRegion(rect.origin(), image_data, region, stride);
3255   if (!region)
3256     return nullptr;
3257   return ScopedFPDFBitmap(FPDFBitmap_CreateEx(rect.width(), rect.height(),
3258                                               FPDFBitmap_BGRx, region, stride));
3259 }
3260 
GetPDFiumRect(int page_index,const gfx::Rect & rect,int * start_x,int * start_y,int * size_x,int * size_y) const3261 void PDFiumEngine::GetPDFiumRect(int page_index,
3262                                  const gfx::Rect& rect,
3263                                  int* start_x,
3264                                  int* start_y,
3265                                  int* size_x,
3266                                  int* size_y) const {
3267   gfx::Rect page_rect = GetScreenRect(pages_[page_index]->rect());
3268   page_rect.Offset(-rect.x(), -rect.y());
3269 
3270   *start_x = page_rect.x();
3271   *start_y = page_rect.y();
3272   *size_x = page_rect.width();
3273   *size_y = page_rect.height();
3274 }
3275 
GetRenderingFlags() const3276 int PDFiumEngine::GetRenderingFlags() const {
3277   int flags = FPDF_LCD_TEXT;
3278   if (render_grayscale_)
3279     flags |= FPDF_GRAYSCALE;
3280   if (client_->IsPrintPreview())
3281     flags |= FPDF_PRINTING;
3282   if (render_annots_)
3283     flags |= FPDF_ANNOT;
3284   return flags;
3285 }
3286 
GetVisibleRect() const3287 gfx::Rect PDFiumEngine::GetVisibleRect() const {
3288   gfx::Rect rv;
3289   rv.set_x(static_cast<int>(position_.x() / current_zoom_));
3290   rv.set_y(static_cast<int>(position_.y() / current_zoom_));
3291   rv.set_width(static_cast<int>(ceil(plugin_size_.width() / current_zoom_)));
3292   rv.set_height(static_cast<int>(ceil(plugin_size_.height() / current_zoom_)));
3293   return rv;
3294 }
3295 
GetPageScreenRect(int page_index) const3296 gfx::Rect PDFiumEngine::GetPageScreenRect(int page_index) const {
3297   gfx::Rect page_rect = pages_[page_index]->rect();
3298   draw_utils::PageInsetSizes inset_sizes =
3299       GetInsetSizes(layout_.options(), page_index, pages_.size());
3300 
3301   int max_page_height = page_rect.height();
3302   base::Optional<size_t> adjacent_page_index =
3303       GetAdjacentPageIndexForTwoUpView(page_index, pages_.size());
3304   if (adjacent_page_index.has_value()) {
3305     max_page_height = std::max(
3306         max_page_height, pages_[adjacent_page_index.value()]->rect().height());
3307   }
3308 
3309   return GetScreenRect(draw_utils::GetSurroundingRect(
3310       page_rect.y(), max_page_height, inset_sizes, layout_.size().width(),
3311       DocumentLayout::kBottomSeparator));
3312 }
3313 
GetScreenRect(const gfx::Rect & rect) const3314 gfx::Rect PDFiumEngine::GetScreenRect(const gfx::Rect& rect) const {
3315   return draw_utils::GetScreenRect(rect, position_, current_zoom_);
3316 }
3317 
Highlight(void * buffer,int stride,const gfx::Rect & rect,int color_red,int color_green,int color_blue,std::vector<gfx::Rect> & highlighted_rects) const3318 void PDFiumEngine::Highlight(void* buffer,
3319                              int stride,
3320                              const gfx::Rect& rect,
3321                              int color_red,
3322                              int color_green,
3323                              int color_blue,
3324                              std::vector<gfx::Rect>& highlighted_rects) const {
3325   if (!buffer)
3326     return;
3327 
3328   gfx::Rect new_rect = rect;
3329   for (const auto& highlighted : highlighted_rects)
3330     new_rect.Subtract(highlighted);
3331   if (new_rect.IsEmpty())
3332     return;
3333 
3334   std::vector<size_t> overlapping_rect_indices;
3335   for (size_t i = 0; i < highlighted_rects.size(); ++i) {
3336     if (new_rect.Intersects((highlighted_rects)[i]))
3337       overlapping_rect_indices.push_back(i);
3338   }
3339 
3340   highlighted_rects.push_back(new_rect);
3341   int l = new_rect.x();
3342   int t = new_rect.y();
3343   int w = new_rect.width();
3344   int h = new_rect.height();
3345 
3346   for (int y = t; y < t + h; ++y) {
3347     for (int x = l; x < l + w; ++x) {
3348       bool overlaps = false;
3349       for (size_t i : overlapping_rect_indices) {
3350         const auto& highlighted = (highlighted_rects)[i];
3351         if (highlighted.Contains(x, y)) {
3352           overlaps = true;
3353           break;
3354         }
3355       }
3356       if (overlaps)
3357         continue;
3358 
3359       uint8_t* pixel = static_cast<uint8_t*>(buffer) + y * stride + x * 4;
3360       pixel[0] = static_cast<uint8_t>(pixel[0] * (color_blue / 255.0));
3361       pixel[1] = static_cast<uint8_t>(pixel[1] * (color_green / 255.0));
3362       pixel[2] = static_cast<uint8_t>(pixel[2] * (color_red / 255.0));
3363     }
3364   }
3365 }
3366 
SelectionChangeInvalidator(PDFiumEngine * engine)3367 PDFiumEngine::SelectionChangeInvalidator::SelectionChangeInvalidator(
3368     PDFiumEngine* engine)
3369     : engine_(engine),
3370       previous_origin_(engine_->GetVisibleRect().origin()),
3371       old_selections_(GetVisibleSelections()) {}
3372 
~SelectionChangeInvalidator()3373 PDFiumEngine::SelectionChangeInvalidator::~SelectionChangeInvalidator() {
3374   // Offset the old selections if the document scrolled since we recorded them.
3375   gfx::Vector2d offset = previous_origin_ - engine_->GetVisibleRect().origin();
3376   for (auto& old_selection : old_selections_)
3377     old_selection.Offset(offset);
3378 
3379   std::vector<gfx::Rect> new_selections = GetVisibleSelections();
3380   for (auto& new_selection : new_selections) {
3381     for (auto& old_selection : old_selections_) {
3382       if (!old_selection.IsEmpty() && new_selection == old_selection) {
3383         // Rectangle was selected before and after, so no need to invalidate it.
3384         // Mark the rectangles by setting them to empty.
3385         new_selection = old_selection = gfx::Rect();
3386         break;
3387       }
3388     }
3389   }
3390 
3391   bool selection_changed = false;
3392   for (const auto& old_selection : old_selections_) {
3393     if (!old_selection.IsEmpty()) {
3394       Invalidate(old_selection);
3395       selection_changed = true;
3396     }
3397   }
3398   for (const auto& new_selection : new_selections) {
3399     if (!new_selection.IsEmpty()) {
3400       Invalidate(new_selection);
3401       selection_changed = true;
3402     }
3403   }
3404 
3405   if (selection_changed) {
3406     engine_->OnSelectionTextChanged();
3407     engine_->OnSelectionPositionChanged();
3408   }
3409 }
3410 
3411 std::vector<gfx::Rect>
GetVisibleSelections() const3412 PDFiumEngine::SelectionChangeInvalidator::GetVisibleSelections() const {
3413   std::vector<gfx::Rect> rects;
3414   gfx::Point visible_point = engine_->GetVisibleRect().origin();
3415   for (const auto& range : engine_->selection_) {
3416     // Exclude selections on pages that's not currently visible.
3417     if (!engine_->IsPageVisible(range.page_index()))
3418       continue;
3419 
3420     const std::vector<gfx::Rect>& selection_rects = range.GetScreenRects(
3421         visible_point, engine_->current_zoom_,
3422         engine_->layout_.options().default_page_orientation());
3423     rects.insert(rects.end(), selection_rects.begin(), selection_rects.end());
3424   }
3425   return rects;
3426 }
3427 
Invalidate(const gfx::Rect & selection)3428 void PDFiumEngine::SelectionChangeInvalidator::Invalidate(
3429     const gfx::Rect& selection) {
3430   gfx::Rect expanded_selection = selection;
3431   expanded_selection.Inset(-1, -1);
3432   engine_->client_->Invalidate(expanded_selection);
3433 }
3434 
MouseDownState(const PDFiumPage::Area & area,const PDFiumPage::LinkTarget & target)3435 PDFiumEngine::MouseDownState::MouseDownState(
3436     const PDFiumPage::Area& area,
3437     const PDFiumPage::LinkTarget& target)
3438     : area_(area), target_(target) {}
3439 
3440 PDFiumEngine::MouseDownState::~MouseDownState() = default;
3441 
Set(const PDFiumPage::Area & area,const PDFiumPage::LinkTarget & target)3442 void PDFiumEngine::MouseDownState::Set(const PDFiumPage::Area& area,
3443                                        const PDFiumPage::LinkTarget& target) {
3444   area_ = area;
3445   target_ = target;
3446 }
3447 
Reset()3448 void PDFiumEngine::MouseDownState::Reset() {
3449   area_ = PDFiumPage::NONSELECTABLE_AREA;
3450   target_ = PDFiumPage::LinkTarget();
3451 }
3452 
Matches(const PDFiumPage::Area & area,const PDFiumPage::LinkTarget & target) const3453 bool PDFiumEngine::MouseDownState::Matches(
3454     const PDFiumPage::Area& area,
3455     const PDFiumPage::LinkTarget& target) const {
3456   if (area_ != area)
3457     return false;
3458 
3459   if (area == PDFiumPage::WEBLINK_AREA)
3460     return target_.url == target.url;
3461 
3462   if (area == PDFiumPage::DOCLINK_AREA)
3463     return target_.page == target.page;
3464 
3465   return true;
3466 }
3467 
DeviceToPage(int page_index,const gfx::Point & device_point,double * page_x,double * page_y)3468 void PDFiumEngine::DeviceToPage(int page_index,
3469                                 const gfx::Point& device_point,
3470                                 double* page_x,
3471                                 double* page_y) {
3472   *page_x = *page_y = 0;
3473   float device_x = device_point.x();
3474   float device_y = device_point.y();
3475   int temp_x = static_cast<int>((device_x + position_.x()) / current_zoom_ -
3476                                 pages_[page_index]->rect().x());
3477   int temp_y = static_cast<int>((device_y + position_.y()) / current_zoom_ -
3478                                 pages_[page_index]->rect().y());
3479   FPDF_BOOL ret = FPDF_DeviceToPage(
3480       pages_[page_index]->GetPage(), 0, 0, pages_[page_index]->rect().width(),
3481       pages_[page_index]->rect().height(),
3482       ToPDFiumRotation(layout_.options().default_page_orientation()), temp_x,
3483       temp_y, page_x, page_y);
3484   DCHECK(ret);
3485 }
3486 
GetVisiblePageIndex(FPDF_PAGE page)3487 int PDFiumEngine::GetVisiblePageIndex(FPDF_PAGE page) {
3488   // Copy |visible_pages_| since it can change as a result of loading the page
3489   // in GetPage(). See https://crbug.com/822091.
3490   std::vector<int> visible_pages_copy(visible_pages_);
3491   for (int page_index : visible_pages_copy) {
3492     if (pages_[page_index]->GetPage() == page)
3493       return page_index;
3494   }
3495   return -1;
3496 }
3497 
SetCurrentPage(int index)3498 void PDFiumEngine::SetCurrentPage(int index) {
3499   in_flight_visible_page_.reset();
3500 
3501   if (index == most_visible_page_ || !form())
3502     return;
3503 
3504   if (most_visible_page_ != -1 && called_do_document_action_) {
3505     FPDF_PAGE old_page = pages_[most_visible_page_]->GetPage();
3506     FORM_DoPageAAction(old_page, form(), FPDFPAGE_AACTION_CLOSE);
3507   }
3508   most_visible_page_ = index;
3509 #if defined(OS_LINUX) || defined(OS_CHROMEOS) || defined(OS_BSD)
3510   SetLastInstance(client_->GetPluginInstance());
3511 #endif
3512   if (most_visible_page_ != -1 && called_do_document_action_) {
3513     FPDF_PAGE new_page = pages_[most_visible_page_]->GetPage();
3514     FORM_DoPageAAction(new_page, form(), FPDFPAGE_AACTION_OPEN);
3515   }
3516 }
3517 
DrawPageShadow(const gfx::Rect & page_rc,const gfx::Rect & shadow_rc,const gfx::Rect & clip_rc,SkBitmap & image_data)3518 void PDFiumEngine::DrawPageShadow(const gfx::Rect& page_rc,
3519                                   const gfx::Rect& shadow_rc,
3520                                   const gfx::Rect& clip_rc,
3521                                   SkBitmap& image_data) {
3522   gfx::Rect page_rect(page_rc);
3523   page_rect.Offset(page_offset_);
3524 
3525   gfx::Rect shadow_rect(shadow_rc);
3526   shadow_rect.Offset(page_offset_);
3527 
3528   gfx::Rect clip_rect(clip_rc);
3529   clip_rect.Offset(page_offset_);
3530 
3531   // Page drop shadow parameters.
3532   constexpr double factor = 0.5;
3533   uint32_t depth = std::max({page_rect.x() - shadow_rect.x(),
3534                              page_rect.y() - shadow_rect.y(),
3535                              shadow_rect.right() - page_rect.right(),
3536                              shadow_rect.bottom() - page_rect.bottom()});
3537   depth = static_cast<uint32_t>(depth * 1.5) + 1;
3538 
3539   // We need to check depth only to verify our copy of shadow matrix is correct.
3540   if (!page_shadow_ || page_shadow_->depth() != depth) {
3541     page_shadow_ = std::make_unique<draw_utils::ShadowMatrix>(
3542         depth, factor, client_->GetBackgroundColor());
3543   }
3544 
3545   DCHECK(!image_data.isNull());
3546   DrawShadow(image_data, shadow_rect, page_rect, clip_rect, *page_shadow_);
3547 }
3548 
GetRegion(const gfx::Point & location,SkBitmap & image_data,void * & region,int & stride) const3549 void PDFiumEngine::GetRegion(const gfx::Point& location,
3550                              SkBitmap& image_data,
3551                              void*& region,
3552                              int& stride) const {
3553   if (image_data.isNull()) {
3554     DCHECK(plugin_size_.IsEmpty());
3555     stride = 0;
3556     region = nullptr;
3557     return;
3558   }
3559   char* buffer = static_cast<char*>(image_data.getPixels());
3560   stride = image_data.rowBytes();
3561 
3562   gfx::Point offset_location = location + page_offset_;
3563   // TODO: update this when we support BIDI and scrollbars can be on the left.
3564   if (!buffer ||
3565       !gfx::Rect(gfx::PointAtOffsetFromOrigin(page_offset_), plugin_size_)
3566            .Contains(offset_location)) {
3567     region = nullptr;
3568     return;
3569   }
3570 
3571   buffer += location.y() * stride;
3572   buffer += (location.x() + page_offset_.x()) * 4;
3573   region = buffer;
3574 }
3575 
OnSelectionTextChanged()3576 void PDFiumEngine::OnSelectionTextChanged() {
3577   DCHECK(!in_form_text_area_);
3578   client_->SetSelectedText(GetSelectedText());
3579 }
3580 
OnSelectionPositionChanged()3581 void PDFiumEngine::OnSelectionPositionChanged() {
3582   // We need to determine the top-left and bottom-right points of the selection
3583   // in order to report those to the embedder. This code assumes that the
3584   // selection list is out of order.
3585   gfx::Rect left(std::numeric_limits<int32_t>::max(),
3586                  std::numeric_limits<int32_t>::max(), 0, 0);
3587   gfx::Rect right;
3588   for (const auto& sel : selection_) {
3589     const std::vector<gfx::Rect>& screen_rects =
3590         sel.GetScreenRects(GetVisibleRect().origin(), current_zoom_,
3591                            layout_.options().default_page_orientation());
3592     for (const auto& rect : screen_rects) {
3593       if (IsAboveOrDirectlyLeftOf(rect, left))
3594         left = rect;
3595       if (IsAboveOrDirectlyLeftOf(right, rect))
3596         right = rect;
3597     }
3598   }
3599   right.set_x(right.x() + right.width());
3600   if (left.IsEmpty()) {
3601     left.set_x(0);
3602     left.set_y(0);
3603   }
3604   client_->SelectionChanged(left, right);
3605 }
3606 
ApplyDocumentLayout(const DocumentLayout::Options & options)3607 gfx::Size PDFiumEngine::ApplyDocumentLayout(
3608     const DocumentLayout::Options& options) {
3609   // We need to return early if the layout would not change, otherwise calling
3610   // client_->ScrollToPage() would send another "viewport" message, triggering
3611   // an infinite loop.
3612   //
3613   // TODO(crbug.com/1013800): The current implementation computes layout twice
3614   // (here, and in InvalidateAllPages()). This shouldn't be too expensive at
3615   // realistic page counts, but could be avoided.
3616   layout_.SetOptions(options);
3617   UpdateDocumentLayout(&layout_);
3618   if (!layout_.dirty())
3619     return layout_.size();
3620 
3621   // Store the current find index so that we can resume finding at that
3622   // particular index after we have recomputed the find results.
3623   std::string current_find_text = current_find_text_;
3624   resume_find_index_ = current_find_index_;
3625 
3626   // Save the current page.
3627   int most_visible_page = most_visible_page_;
3628 
3629   InvalidateAllPages();
3630 
3631   // Restore find results.
3632   if (!current_find_text.empty()) {
3633     // Clear the UI.
3634     client_->NotifyNumberOfFindResultsChanged(0, false);
3635     StartFind(current_find_text, false);
3636   }
3637 
3638   // Restore current page. After a rotation, the page heights have changed but
3639   // the scroll position has not. Re-adjust.
3640   // TODO(thestig): It would be better to also restore the position on the page.
3641   client_->ScrollToPage(most_visible_page);
3642 
3643   return layout_.size();
3644 }
3645 
SetSelecting(bool selecting)3646 void PDFiumEngine::SetSelecting(bool selecting) {
3647   bool was_selecting = selecting_;
3648   selecting_ = selecting;
3649   if (selecting_ != was_selecting)
3650     client_->IsSelectingChanged(selecting);
3651 }
3652 
EnteredEditMode()3653 void PDFiumEngine::EnteredEditMode() {
3654   if (edit_mode_)
3655     return;
3656 
3657   edit_mode_ = true;
3658   client_->EnteredEditMode();
3659 }
3660 
SetInFormTextArea(bool in_form_text_area)3661 void PDFiumEngine::SetInFormTextArea(bool in_form_text_area) {
3662   // If focus was previously in form text area, clear form text selection.
3663   // Clearing needs to be done before changing focus to ensure the correct
3664   // observer is notified of the change in selection. When |in_form_text_area_|
3665   // is true, this is the Renderer. After it flips, the MimeHandler is notified.
3666   if (in_form_text_area_) {
3667     client_->SetSelectedText("");
3668   }
3669 
3670   client_->FormTextFieldFocusChange(in_form_text_area);
3671   in_form_text_area_ = in_form_text_area;
3672 
3673   // Clear |editable_form_text_area_| when focus no longer in form text area.
3674   if (!in_form_text_area_)
3675     editable_form_text_area_ = false;
3676 }
3677 
SetMouseLeftButtonDown(bool is_mouse_left_button_down)3678 void PDFiumEngine::SetMouseLeftButtonDown(bool is_mouse_left_button_down) {
3679   mouse_left_button_down_ = is_mouse_left_button_down;
3680 }
3681 
IsAnnotationAnEditableFormTextArea(FPDF_ANNOTATION annot,int form_type) const3682 bool PDFiumEngine::IsAnnotationAnEditableFormTextArea(FPDF_ANNOTATION annot,
3683                                                       int form_type) const {
3684 #if defined(PDF_ENABLE_XFA)
3685   if (IS_XFA_FORMFIELD(form_type)) {
3686     return form_type == FPDF_FORMFIELD_XFA_TEXTFIELD ||
3687            form_type == FPDF_FORMFIELD_XFA_COMBOBOX;
3688   }
3689 #endif  // defined(PDF_ENABLE_XFA)
3690 
3691   if (!annot)
3692     return false;
3693 
3694   int flags = FPDFAnnot_GetFormFieldFlags(form(), annot);
3695   return CheckIfEditableFormTextArea(flags, form_type);
3696 }
3697 
ScheduleTouchTimer(const TouchInputEvent & evt)3698 void PDFiumEngine::ScheduleTouchTimer(const TouchInputEvent& evt) {
3699   touch_timer_.Start(FROM_HERE, kTouchLongPressTimeout,
3700                      base::BindOnce(&PDFiumEngine::HandleLongPress,
3701                                     base::Unretained(this), evt));
3702 }
3703 
KillTouchTimer()3704 void PDFiumEngine::KillTouchTimer() {
3705   touch_timer_.Stop();
3706 }
3707 
PageIndexInBounds(int index) const3708 bool PDFiumEngine::PageIndexInBounds(int index) const {
3709   return index >= 0 && index < static_cast<int>(pages_.size());
3710 }
3711 
IsPageCharacterIndexInBounds(const PP_PdfPageCharacterIndex & index) const3712 bool PDFiumEngine::IsPageCharacterIndexInBounds(
3713     const PP_PdfPageCharacterIndex& index) const {
3714   return PageIndexInBounds(index.page_index) &&
3715          pages_[index.page_index]->IsCharIndexInBounds(index.char_index);
3716 }
3717 
GetToolbarHeightInScreenCoords()3718 float PDFiumEngine::GetToolbarHeightInScreenCoords() {
3719   return client_->GetToolbarHeightInScreenCoords();
3720 }
3721 
Pause_NeedToPauseNow(IFSDK_PAUSE * param)3722 FPDF_BOOL PDFiumEngine::Pause_NeedToPauseNow(IFSDK_PAUSE* param) {
3723   PDFiumEngine* engine = static_cast<PDFiumEngine*>(param);
3724   return base::Time::Now() - engine->last_progressive_start_time_ >
3725          engine->progressive_paint_timeout_;
3726 }
3727 
SetSelection(const PP_PdfPageCharacterIndex & selection_start_index,const PP_PdfPageCharacterIndex & selection_end_index)3728 void PDFiumEngine::SetSelection(
3729     const PP_PdfPageCharacterIndex& selection_start_index,
3730     const PP_PdfPageCharacterIndex& selection_end_index) {
3731   SelectionChangeInvalidator selection_invalidator(this);
3732   selection_.clear();
3733 
3734   PP_PdfPageCharacterIndex sel_start_index = selection_start_index;
3735   PP_PdfPageCharacterIndex sel_end_index = selection_end_index;
3736   if (sel_end_index.page_index < sel_start_index.page_index) {
3737     std::swap(sel_end_index.page_index, sel_start_index.page_index);
3738     std::swap(sel_end_index.char_index, sel_start_index.char_index);
3739   }
3740 
3741   if (sel_end_index.page_index == sel_start_index.page_index &&
3742       sel_end_index.char_index < sel_start_index.char_index) {
3743     std::swap(sel_end_index.char_index, sel_start_index.char_index);
3744   }
3745 
3746   for (uint32_t i = sel_start_index.page_index; i <= sel_end_index.page_index;
3747        ++i) {
3748     int32_t char_count = pages_[i]->GetCharCount();
3749     if (char_count <= 0)
3750       continue;
3751     int32_t start_char_index = 0;
3752     int32_t end_char_index = char_count;
3753     if (i == sel_start_index.page_index)
3754       start_char_index = sel_start_index.char_index;
3755     if (i == sel_end_index.page_index)
3756       end_char_index = sel_end_index.char_index;
3757     selection_.push_back(PDFiumRange(pages_[i].get(), start_char_index,
3758                                      end_char_index - start_char_index));
3759   }
3760 }
3761 
ScrollFocusedAnnotationIntoView()3762 void PDFiumEngine::ScrollFocusedAnnotationIntoView() {
3763   FPDF_ANNOTATION annot;
3764   int page_index;
3765   if (!FORM_GetFocusedAnnot(form(), &page_index, &annot))
3766     return;
3767 
3768   ScrollAnnotationIntoView(annot, page_index);
3769   FPDFPage_CloseAnnot(annot);
3770 }
3771 
ScrollAnnotationIntoView(FPDF_ANNOTATION annot,int page_index)3772 void PDFiumEngine::ScrollAnnotationIntoView(FPDF_ANNOTATION annot,
3773                                             int page_index) {
3774   if (!PageIndexInBounds(page_index))
3775     return;
3776 
3777   FS_RECTF annot_rect;
3778   if (!FPDFAnnot_GetRect(annot, &annot_rect))
3779     return;
3780 
3781   gfx::Rect rect = pages_[page_index]->PageToScreen(
3782       gfx::Point(), /*zoom=*/1.0, annot_rect.left, annot_rect.top,
3783       annot_rect.right, annot_rect.bottom,
3784       layout_.options().default_page_orientation());
3785 
3786   gfx::Rect visible_rect = GetVisibleRect();
3787   if (visible_rect.Contains(rect))
3788     return;
3789   // Since the focus rect is not already in the visible area, scrolling
3790   // horizontally and/or vertically is required.
3791   if (rect.y() < visible_rect.y() || rect.bottom() > visible_rect.bottom()) {
3792     // Scroll the viewport vertically to align the top of focus rect to
3793     // centre.
3794     client_->ScrollToY(rect.y() * current_zoom_ - plugin_size_.height() / 2,
3795                        /*compensate_for_toolbar=*/false);
3796   }
3797   if (rect.x() < visible_rect.x() || rect.right() > visible_rect.right()) {
3798     // Scroll the viewport horizontally to align the left of focus rect to
3799     // centre.
3800     client_->ScrollToX(rect.x() * current_zoom_ - plugin_size_.width() / 2);
3801   }
3802 }
3803 
OnFocusedAnnotationUpdated(FPDF_ANNOTATION annot,int page_index)3804 void PDFiumEngine::OnFocusedAnnotationUpdated(FPDF_ANNOTATION annot,
3805                                               int page_index) {
3806   SetLinkUnderCursorForAnnotation(annot, page_index);
3807   int form_type = FPDFAnnot_GetFormFieldType(form(), annot);
3808   if (form_type <= FPDF_FORMFIELD_UNKNOWN) {
3809     SetInFormTextArea(false);
3810     return;
3811   }
3812   bool is_form_text_area =
3813       PDFiumPage::FormTypeToArea(form_type) == PDFiumPage::FORM_TEXT_AREA;
3814   if (is_form_text_area) {
3815     SelectionChangeInvalidator selection_invalidator(this);
3816     selection_.clear();
3817   }
3818   SetInFormTextArea(is_form_text_area);
3819   editable_form_text_area_ =
3820       is_form_text_area && IsAnnotationAnEditableFormTextArea(annot, form_type);
3821 }
3822 
SetCaretPosition(const gfx::Point & position)3823 void PDFiumEngine::SetCaretPosition(const gfx::Point& position) {
3824   // TODO(dsinclair): Handle caret position ...
3825 }
3826 
MoveRangeSelectionExtent(const gfx::Point & extent)3827 void PDFiumEngine::MoveRangeSelectionExtent(const gfx::Point& extent) {
3828   int page_index = -1;
3829   int char_index = -1;
3830   int form_type = FPDF_FORMFIELD_UNKNOWN;
3831   PDFiumPage::LinkTarget target;
3832   GetCharIndex(extent, &page_index, &char_index, &form_type, &target);
3833   if (page_index < 0 || char_index < 0)
3834     return;
3835 
3836   SelectionChangeInvalidator selection_invalidator(this);
3837   if (range_selection_direction_ == RangeSelectionDirection::Right) {
3838     ExtendSelection(page_index, char_index);
3839     return;
3840   }
3841 
3842   // For a left selection we clear the current selection and set a new starting
3843   // point based on the new left position. We then extend that selection out to
3844   // the previously provided base location.
3845   selection_.clear();
3846   selection_.push_back(PDFiumRange(pages_[page_index].get(), char_index, 0));
3847 
3848   // This should always succeeed because the range selection base should have
3849   // already been selected.
3850   GetCharIndex(range_selection_base_, &page_index, &char_index, &form_type,
3851                &target);
3852   ExtendSelection(page_index, char_index);
3853 }
3854 
SetSelectionBounds(const gfx::Point & base,const gfx::Point & extent)3855 void PDFiumEngine::SetSelectionBounds(const gfx::Point& base,
3856                                       const gfx::Point& extent) {
3857   range_selection_base_ = base;
3858   range_selection_direction_ = IsAboveOrDirectlyLeftOf(base, extent)
3859                                    ? RangeSelectionDirection::Left
3860                                    : RangeSelectionDirection::Right;
3861 }
3862 
GetSelection(uint32_t * selection_start_page_index,uint32_t * selection_start_char_index,uint32_t * selection_end_page_index,uint32_t * selection_end_char_index)3863 void PDFiumEngine::GetSelection(uint32_t* selection_start_page_index,
3864                                 uint32_t* selection_start_char_index,
3865                                 uint32_t* selection_end_page_index,
3866                                 uint32_t* selection_end_char_index) {
3867   size_t len = selection_.size();
3868   if (len == 0) {
3869     *selection_start_page_index = 0;
3870     *selection_start_char_index = 0;
3871     *selection_end_page_index = 0;
3872     *selection_end_char_index = 0;
3873     return;
3874   }
3875 
3876   *selection_start_page_index = selection_[0].page_index();
3877   *selection_start_char_index = selection_[0].char_index();
3878   *selection_end_page_index = selection_[len - 1].page_index();
3879 
3880   // If the selection is all within one page, the end index is the
3881   // start index plus the char count. But if the selection spans
3882   // multiple pages, the selection starts at the beginning of the
3883   // last page in |selection_| and goes to the char count.
3884   if (len == 1) {
3885     *selection_end_char_index =
3886         selection_[0].char_index() + selection_[0].char_count();
3887   } else {
3888     *selection_end_char_index = selection_[len - 1].char_count();
3889   }
3890 }
3891 
LoadDocumentAttachmentInfoList()3892 void PDFiumEngine::LoadDocumentAttachmentInfoList() {
3893   DCHECK(document_loaded_);
3894 
3895   int attachment_count = FPDFDoc_GetAttachmentCount(doc());
3896   if (attachment_count <= 0)
3897     return;
3898 
3899   doc_attachment_info_list_.resize(attachment_count);
3900   for (int i = 0; i < attachment_count; ++i) {
3901     FPDF_ATTACHMENT attachment = FPDFDoc_GetAttachment(doc(), i);
3902     DCHECK(attachment);
3903 
3904     doc_attachment_info_list_[i].name = GetAttachmentName(attachment);
3905     doc_attachment_info_list_[i].creation_date =
3906         GetAttachmentAttribute(attachment, "CreationDate");
3907     doc_attachment_info_list_[i].modified_date =
3908         GetAttachmentAttribute(attachment, "ModDate");
3909 
3910     unsigned long actual_length_bytes;
3911     doc_attachment_info_list_[i].is_readable =
3912         FPDFAttachment_GetFile(attachment, /*buffer=*/nullptr,
3913                                /*buflen=*/0, &actual_length_bytes);
3914     if (doc_attachment_info_list_[i].is_readable)
3915       doc_attachment_info_list_[i].size_bytes = actual_length_bytes;
3916   }
3917 }
3918 
LoadDocumentMetadata()3919 void PDFiumEngine::LoadDocumentMetadata() {
3920   DCHECK(document_loaded_);
3921 
3922   // Document information dictionary entries
3923   doc_metadata_.version = GetDocumentVersion();
3924   doc_metadata_.title = GetMetadataByField("Title");
3925   doc_metadata_.author = GetMetadataByField("Author");
3926   doc_metadata_.subject = GetMetadataByField("Subject");
3927   doc_metadata_.creator = GetMetadataByField("Creator");
3928   doc_metadata_.producer = GetMetadataByField("Producer");
3929 }
3930 
GetMetadataByField(FPDF_BYTESTRING field) const3931 std::string PDFiumEngine::GetMetadataByField(FPDF_BYTESTRING field) const {
3932   DCHECK(doc());
3933 
3934   return base::UTF16ToUTF8(CallPDFiumWideStringBufferApi(
3935       base::BindRepeating(&FPDF_GetMetaText, doc(), field),
3936       /*check_expected_size=*/false));
3937 }
3938 
GetDocumentVersion() const3939 PdfVersion PDFiumEngine::GetDocumentVersion() const {
3940   DCHECK(doc());
3941 
3942   int version;
3943   if (!FPDF_GetFileVersion(doc(), &version))
3944     return PdfVersion::kUnknown;
3945 
3946   switch (version) {
3947     case 10:
3948       return PdfVersion::k1_0;
3949     case 11:
3950       return PdfVersion::k1_1;
3951     case 12:
3952       return PdfVersion::k1_2;
3953     case 13:
3954       return PdfVersion::k1_3;
3955     case 14:
3956       return PdfVersion::k1_4;
3957     case 15:
3958       return PdfVersion::k1_5;
3959     case 16:
3960       return PdfVersion::k1_6;
3961     case 17:
3962       return PdfVersion::k1_7;
3963     case 18:
3964       return PdfVersion::k1_8;
3965     case 20:
3966       return PdfVersion::k2_0;
3967     default:
3968       return PdfVersion::kUnknown;
3969   }
3970 }
3971 
HandleTabEvent(uint32_t modifiers)3972 bool PDFiumEngine::HandleTabEvent(uint32_t modifiers) {
3973   bool alt_key = !!(modifiers & kInputEventModifierAltKey);
3974   bool ctrl_key = !!(modifiers & kInputEventModifierControlKey);
3975   if (alt_key || ctrl_key)
3976     return HandleTabEventWithModifiers(modifiers);
3977 
3978   return modifiers & kInputEventModifierShiftKey ? HandleTabBackward(modifiers)
3979                                                  : HandleTabForward(modifiers);
3980 }
3981 
HandleTabEventWithModifiers(uint32_t modifiers)3982 bool PDFiumEngine::HandleTabEventWithModifiers(uint32_t modifiers) {
3983   // Only handle cases when a page is focused, else return false.
3984   switch (focus_item_type_) {
3985     case FocusElementType::kNone:
3986     case FocusElementType::kDocument:
3987       return false;
3988     case FocusElementType::kPage:
3989       if (last_focused_page_ == -1)
3990         return false;
3991       return !!FORM_OnKeyDown(form(), pages_[last_focused_page_]->GetPage(),
3992                               FWL_VKEY_Tab, modifiers);
3993     default:
3994       NOTREACHED();
3995       return false;
3996   }
3997 }
3998 
HandleTabForward(uint32_t modifiers)3999 bool PDFiumEngine::HandleTabForward(uint32_t modifiers) {
4000   if (focus_item_type_ == FocusElementType::kNone) {
4001     UpdateFocusItemType(FocusElementType::kDocument);
4002     return true;
4003   }
4004 
4005   int page_index = last_focused_page_;
4006   if (page_index == -1)
4007     page_index = 0;
4008 
4009   bool did_tab_forward = false;
4010   while (!did_tab_forward && PageIndexInBounds(page_index)) {
4011     did_tab_forward = !!FORM_OnKeyDown(form(), pages_[page_index]->GetPage(),
4012                                        FWL_VKEY_Tab, modifiers);
4013     if (!did_tab_forward)
4014       ++page_index;
4015   }
4016 
4017   if (did_tab_forward) {
4018     last_focused_page_ = page_index;
4019     UpdateFocusItemType(FocusElementType::kPage);
4020   } else {
4021     last_focused_page_ = -1;
4022     UpdateFocusItemType(FocusElementType::kNone);
4023   }
4024   return did_tab_forward;
4025 }
4026 
HandleTabBackward(uint32_t modifiers)4027 bool PDFiumEngine::HandleTabBackward(uint32_t modifiers) {
4028   if (focus_item_type_ == FocusElementType::kDocument) {
4029     UpdateFocusItemType(FocusElementType::kNone);
4030     return false;
4031   }
4032 
4033   int page_index = last_focused_page_;
4034   if (page_index == -1)
4035     page_index = GetNumberOfPages() - 1;
4036 
4037   bool did_tab_backward = false;
4038   while (!did_tab_backward && PageIndexInBounds(page_index)) {
4039     did_tab_backward = !!FORM_OnKeyDown(form(), pages_[page_index]->GetPage(),
4040                                         FWL_VKEY_Tab, modifiers);
4041     if (!did_tab_backward)
4042       --page_index;
4043   }
4044 
4045   if (did_tab_backward) {
4046     last_focused_page_ = page_index;
4047     UpdateFocusItemType(FocusElementType::kPage);
4048   } else {
4049     // No focusable annotation found in pages. Possible scenarios:
4050     // Case 1: |focus_item_type_| is None. Since no object in any page can take
4051     // the focus, the document should take focus.
4052     // Case 2: |focus_item_type_| is Page. Since there aren't any objects that
4053     // could take focus, the document should take focus.
4054     // Case 3: |focus_item_type_| is Document. Move focus_item_type_ to None.
4055     switch (focus_item_type_) {
4056       case FocusElementType::kPage:
4057       case FocusElementType::kNone:
4058         did_tab_backward = true;
4059         last_focused_page_ = -1;
4060         UpdateFocusItemType(FocusElementType::kDocument);
4061         KillFormFocus();
4062         break;
4063       case FocusElementType::kDocument:
4064         UpdateFocusItemType(FocusElementType::kNone);
4065         break;
4066       default:
4067         NOTREACHED();
4068         break;
4069     }
4070   }
4071   return did_tab_backward;
4072 }
4073 
UpdateFocusItemType(FocusElementType focus_item_type)4074 void PDFiumEngine::UpdateFocusItemType(FocusElementType focus_item_type) {
4075   if (focus_item_type_ == focus_item_type)
4076     return;
4077   if (focus_item_type_ == FocusElementType::kDocument)
4078     client_->DocumentFocusChanged(false);
4079   focus_item_type_ = focus_item_type;
4080   if (focus_item_type_ == FocusElementType::kDocument)
4081     client_->DocumentFocusChanged(true);
4082 }
4083 
4084 #if defined(PDF_ENABLE_XFA)
UpdatePageCount()4085 void PDFiumEngine::UpdatePageCount() {
4086   InvalidateAllPages();
4087 }
4088 #endif  // defined(PDF_ENABLE_XFA)
4089 
UpdateLinkUnderCursor(const std::string & target_url)4090 void PDFiumEngine::UpdateLinkUnderCursor(const std::string& target_url) {
4091   if (link_under_cursor_ == target_url)
4092     return;
4093 
4094   link_under_cursor_ = target_url;
4095   client_->SetLinkUnderCursor(link_under_cursor_);
4096 }
4097 
SetLinkUnderCursorForAnnotation(FPDF_ANNOTATION annot,int page_index)4098 void PDFiumEngine::SetLinkUnderCursorForAnnotation(FPDF_ANNOTATION annot,
4099                                                    int page_index) {
4100   if (!PageIndexInBounds(page_index)) {
4101     UpdateLinkUnderCursor("");
4102     return;
4103   }
4104 
4105   PDFiumPage::LinkTarget target;
4106   pages_[page_index]->GetLinkTarget(FPDFAnnot_GetLink(annot), &target);
4107   UpdateLinkUnderCursor(target.url);
4108 }
4109 
RequestThumbnail(int page_index,float device_pixel_ratio,SendThumbnailCallback send_callback)4110 void PDFiumEngine::RequestThumbnail(int page_index,
4111                                     float device_pixel_ratio,
4112                                     SendThumbnailCallback send_callback) {
4113   DCHECK(PageIndexInBounds(page_index));
4114   pages_[page_index]->RequestThumbnail(device_pixel_ratio,
4115                                        std::move(send_callback));
4116 }
4117 
ProgressivePaint(int index,const gfx::Rect & rect)4118 PDFiumEngine::ProgressivePaint::ProgressivePaint(int index,
4119                                                  const gfx::Rect& rect)
4120     : page_index_(index), rect_(rect) {}
4121 
4122 PDFiumEngine::ProgressivePaint::ProgressivePaint(ProgressivePaint&& that) =
4123     default;
4124 
4125 PDFiumEngine::ProgressivePaint::~ProgressivePaint() = default;
4126 
4127 PDFiumEngine::ProgressivePaint& PDFiumEngine::ProgressivePaint::operator=(
4128     ProgressivePaint&& that) = default;
4129 
SetBitmapAndImageData(ScopedFPDFBitmap bitmap,SkBitmap image_data)4130 void PDFiumEngine::ProgressivePaint::SetBitmapAndImageData(
4131     ScopedFPDFBitmap bitmap,
4132     SkBitmap image_data) {
4133   bitmap_ = std::move(bitmap);
4134   image_data_ = std::move(image_data);
4135 }
4136 
4137 }  // namespace chrome_pdf
4138