OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "pdf/pdfium/pdfium_engine.h" | 5 #include "pdf/pdfium/pdfium_engine.h" |
6 | 6 |
7 #include <math.h> | 7 #include <math.h> |
8 #include <stddef.h> | 8 #include <stddef.h> |
9 #include <stdint.h> | 9 #include <stdint.h> |
10 | 10 |
(...skipping 1635 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1646 base::string16 selected_form_text16; | 1646 base::string16 selected_form_text16; |
1647 PDFiumAPIStringBufferSizeInBytesAdapter<base::string16> string_adapter( | 1647 PDFiumAPIStringBufferSizeInBytesAdapter<base::string16> string_adapter( |
1648 &selected_form_text16, form_sel_text_len, false); | 1648 &selected_form_text16, form_sel_text_len, false); |
1649 string_adapter.Close(FORM_GetSelectedText( | 1649 string_adapter.Close(FORM_GetSelectedText( |
1650 form_handle, page, string_adapter.GetData(), form_sel_text_len)); | 1650 form_handle, page, string_adapter.GetData(), form_sel_text_len)); |
1651 | 1651 |
1652 // Update previous and current selections, then compare them to check if | 1652 // Update previous and current selections, then compare them to check if |
1653 // selection has changed. If so, set plugin text selection. | 1653 // selection has changed. If so, set plugin text selection. |
1654 std::string selected_form_text = selected_form_text_; | 1654 std::string selected_form_text = selected_form_text_; |
1655 selected_form_text_ = base::UTF16ToUTF8(selected_form_text16); | 1655 selected_form_text_ = base::UTF16ToUTF8(selected_form_text16); |
1656 if (selected_form_text != selected_form_text_) | 1656 if (selected_form_text != selected_form_text_) { |
| 1657 DCHECK(in_form_text_area_); |
1657 pp::PDF::SetSelectedText(GetPluginInstance(), selected_form_text_.c_str()); | 1658 pp::PDF::SetSelectedText(GetPluginInstance(), selected_form_text_.c_str()); |
| 1659 } |
1658 } | 1660 } |
1659 | 1661 |
1660 void PDFiumEngine::PrintEnd() { | 1662 void PDFiumEngine::PrintEnd() { |
1661 FORM_DoDocumentAAction(form_, FPDFDOC_AACTION_DP); | 1663 FORM_DoDocumentAAction(form_, FPDFDOC_AACTION_DP); |
1662 } | 1664 } |
1663 | 1665 |
1664 PDFiumPage::Area PDFiumEngine::GetCharIndex(const pp::MouseInputEvent& event, | 1666 PDFiumPage::Area PDFiumEngine::GetCharIndex(const pp::MouseInputEvent& event, |
1665 int* page_index, | 1667 int* page_index, |
1666 int* char_index, | 1668 int* char_index, |
1667 int* form_type, | 1669 int* form_type, |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1718 return true; | 1720 return true; |
1719 } | 1721 } |
1720 | 1722 |
1721 if (event.GetButton() != PP_INPUTEVENT_MOUSEBUTTON_LEFT && | 1723 if (event.GetButton() != PP_INPUTEVENT_MOUSEBUTTON_LEFT && |
1722 event.GetButton() != PP_INPUTEVENT_MOUSEBUTTON_MIDDLE) { | 1724 event.GetButton() != PP_INPUTEVENT_MOUSEBUTTON_MIDDLE) { |
1723 return false; | 1725 return false; |
1724 } | 1726 } |
1725 | 1727 |
1726 SetMouseLeftButtonDown(event.GetButton() == PP_INPUTEVENT_MOUSEBUTTON_LEFT); | 1728 SetMouseLeftButtonDown(event.GetButton() == PP_INPUTEVENT_MOUSEBUTTON_LEFT); |
1727 | 1729 |
1728 SelectionChangeInvalidator selection_invalidator(this); | 1730 auto selection_invalidator = |
| 1731 base::MakeUnique<SelectionChangeInvalidator>(this); |
1729 selection_.clear(); | 1732 selection_.clear(); |
1730 | 1733 |
1731 int page_index = -1; | 1734 int page_index = -1; |
1732 int char_index = -1; | 1735 int char_index = -1; |
1733 int form_type = FPDF_FORMFIELD_UNKNOWN; | 1736 int form_type = FPDF_FORMFIELD_UNKNOWN; |
1734 PDFiumPage::LinkTarget target; | 1737 PDFiumPage::LinkTarget target; |
1735 PDFiumPage::Area area = | 1738 PDFiumPage::Area area = |
1736 GetCharIndex(event, &page_index, &char_index, &form_type, &target); | 1739 GetCharIndex(event, &page_index, &char_index, &form_type, &target); |
1737 mouse_down_state_.Set(area, target); | 1740 mouse_down_state_.Set(area, target); |
1738 | 1741 |
1739 // Decide whether to open link or not based on user action in mouse up and | 1742 // Decide whether to open link or not based on user action in mouse up and |
1740 // mouse move events. | 1743 // mouse move events. |
1741 if (area == PDFiumPage::WEBLINK_AREA || area == PDFiumPage::DOCLINK_AREA) | 1744 if (area == PDFiumPage::WEBLINK_AREA || area == PDFiumPage::DOCLINK_AREA) |
1742 return true; | 1745 return true; |
1743 | 1746 |
1744 // Prevent middle mouse button from selecting texts. | 1747 // Prevent middle mouse button from selecting texts. |
1745 if (event.GetButton() == PP_INPUTEVENT_MOUSEBUTTON_MIDDLE) | 1748 if (event.GetButton() == PP_INPUTEVENT_MOUSEBUTTON_MIDDLE) |
1746 return false; | 1749 return false; |
1747 | 1750 |
1748 if (page_index != -1) { | 1751 if (page_index != -1) { |
1749 last_page_mouse_down_ = page_index; | 1752 last_page_mouse_down_ = page_index; |
1750 double page_x, page_y; | 1753 double page_x, page_y; |
1751 pp::Point point = event.GetPosition(); | 1754 pp::Point point = event.GetPosition(); |
1752 DeviceToPage(page_index, point.x(), point.y(), &page_x, &page_y); | 1755 DeviceToPage(page_index, point.x(), point.y(), &page_x, &page_y); |
1753 | 1756 |
1754 FORM_OnLButtonDown(form_, pages_[page_index]->GetPage(), 0, page_x, page_y); | 1757 FORM_OnLButtonDown(form_, pages_[page_index]->GetPage(), 0, page_x, page_y); |
1755 if (form_type > FPDF_FORMFIELD_UNKNOWN) { // returns -1 sometimes... | 1758 if (form_type > FPDF_FORMFIELD_UNKNOWN) { // returns -1 sometimes... |
1756 mouse_down_state_.Set(PDFiumPage::FormTypeToArea(form_type), target); | 1759 mouse_down_state_.Set(PDFiumPage::FormTypeToArea(form_type), target); |
1757 | 1760 |
| 1761 // Destroy SelectionChangeInvalidator object before SetInFormTextArea() |
| 1762 // changes plugin's focus to be in form text area. This way, regular text |
| 1763 // selection can be cleared when a user clicks into a form text area |
| 1764 // because the pp::PDF::SetSelectedText() call in |
| 1765 // ~SelectionChangeInvalidator() still goes to the Mimehandler |
| 1766 // (not the Renderer). |
| 1767 selection_invalidator.reset(); |
| 1768 |
1758 bool is_valid_control = (form_type == FPDF_FORMFIELD_TEXTFIELD || | 1769 bool is_valid_control = (form_type == FPDF_FORMFIELD_TEXTFIELD || |
1759 form_type == FPDF_FORMFIELD_COMBOBOX); | 1770 form_type == FPDF_FORMFIELD_COMBOBOX); |
1760 | 1771 |
1761 // TODO(bug_62400): figure out selection and copying | 1772 // TODO(bug_62400): figure out selection and copying |
1762 // for XFA fields | 1773 // for XFA fields |
1763 #if defined(PDF_ENABLE_XFA) | 1774 #if defined(PDF_ENABLE_XFA) |
1764 is_valid_control |= (form_type == FPDF_FORMFIELD_XFA); | 1775 is_valid_control |= (form_type == FPDF_FORMFIELD_XFA); |
1765 #endif | 1776 #endif |
1766 SetInFormTextArea(is_valid_control); | 1777 SetInFormTextArea(is_valid_control); |
1767 return true; // Return now before we get into the selection code. | 1778 return true; // Return now before we get into the selection code. |
(...skipping 1808 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3576 *region = nullptr; | 3587 *region = nullptr; |
3577 return; | 3588 return; |
3578 } | 3589 } |
3579 | 3590 |
3580 buffer += location.y() * (*stride); | 3591 buffer += location.y() * (*stride); |
3581 buffer += (location.x() + page_offset_.x()) * 4; | 3592 buffer += (location.x() + page_offset_.x()) * 4; |
3582 *region = buffer; | 3593 *region = buffer; |
3583 } | 3594 } |
3584 | 3595 |
3585 void PDFiumEngine::OnSelectionChanged() { | 3596 void PDFiumEngine::OnSelectionChanged() { |
| 3597 DCHECK(!in_form_text_area_); |
3586 pp::PDF::SetSelectedText(GetPluginInstance(), GetSelectedText().c_str()); | 3598 pp::PDF::SetSelectedText(GetPluginInstance(), GetSelectedText().c_str()); |
3587 } | 3599 } |
3588 | 3600 |
3589 void PDFiumEngine::RotateInternal() { | 3601 void PDFiumEngine::RotateInternal() { |
3590 // Store the current find index so that we can resume finding at that | 3602 // Store the current find index so that we can resume finding at that |
3591 // particular index after we have recomputed the find results. | 3603 // particular index after we have recomputed the find results. |
3592 std::string current_find_text = current_find_text_; | 3604 std::string current_find_text = current_find_text_; |
3593 if (current_find_index_.valid()) | 3605 if (current_find_index_.valid()) |
3594 resume_find_index_.SetIndex(current_find_index_.GetIndex()); | 3606 resume_find_index_.SetIndex(current_find_index_.GetIndex()); |
3595 else | 3607 else |
(...skipping 18 matching lines...) Expand all Loading... |
3614 } | 3626 } |
3615 | 3627 |
3616 void PDFiumEngine::SetSelecting(bool selecting) { | 3628 void PDFiumEngine::SetSelecting(bool selecting) { |
3617 bool was_selecting = selecting_; | 3629 bool was_selecting = selecting_; |
3618 selecting_ = selecting; | 3630 selecting_ = selecting; |
3619 if (selecting_ != was_selecting) | 3631 if (selecting_ != was_selecting) |
3620 client_->IsSelectingChanged(selecting); | 3632 client_->IsSelectingChanged(selecting); |
3621 } | 3633 } |
3622 | 3634 |
3623 void PDFiumEngine::SetInFormTextArea(bool in_form_text_area) { | 3635 void PDFiumEngine::SetInFormTextArea(bool in_form_text_area) { |
| 3636 // If focus was previously in form text area, clear form text selection. |
| 3637 // Clearing needs to be done before changing focus to ensure the correct |
| 3638 // observer is notified of the change in selection. When |in_form_text_area_| |
| 3639 // is true, this is the Renderer. After it flips, the MimeHandler is notified. |
| 3640 if (in_form_text_area_) |
| 3641 pp::PDF::SetSelectedText(GetPluginInstance(), ""); |
| 3642 |
3624 client_->FormTextFieldFocusChange(in_form_text_area); | 3643 client_->FormTextFieldFocusChange(in_form_text_area); |
3625 in_form_text_area_ = in_form_text_area; | 3644 in_form_text_area_ = in_form_text_area; |
3626 } | 3645 } |
3627 | 3646 |
3628 void PDFiumEngine::SetMouseLeftButtonDown(bool is_mouse_left_button_down) { | 3647 void PDFiumEngine::SetMouseLeftButtonDown(bool is_mouse_left_button_down) { |
3629 mouse_left_button_down_ = is_mouse_left_button_down; | 3648 mouse_left_button_down_ = is_mouse_left_button_down; |
3630 } | 3649 } |
3631 | 3650 |
3632 void PDFiumEngine::ScheduleTouchTimer(const pp::TouchInputEvent& evt) { | 3651 void PDFiumEngine::ScheduleTouchTimer(const pp::TouchInputEvent& evt) { |
3633 touch_timers_[++next_touch_timer_id_] = evt; | 3652 touch_timers_[++next_touch_timer_id_] = evt; |
(...skipping 577 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
4211 FPDF_DOCUMENT doc = | 4230 FPDF_DOCUMENT doc = |
4212 FPDF_LoadMemDocument(pdf_buffer, pdf_buffer_size, nullptr); | 4231 FPDF_LoadMemDocument(pdf_buffer, pdf_buffer_size, nullptr); |
4213 if (!doc) | 4232 if (!doc) |
4214 return false; | 4233 return false; |
4215 bool success = FPDF_GetPageSizeByIndex(doc, page_number, width, height) != 0; | 4234 bool success = FPDF_GetPageSizeByIndex(doc, page_number, width, height) != 0; |
4216 FPDF_CloseDocument(doc); | 4235 FPDF_CloseDocument(doc); |
4217 return success; | 4236 return success; |
4218 } | 4237 } |
4219 | 4238 |
4220 } // namespace chrome_pdf | 4239 } // namespace chrome_pdf |
OLD | NEW |