Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(58)

Side by Side Diff: pdf/pdfium/pdfium_engine.cc

Issue 2924343005: Add functionality for copying text within form text fields and form combobox text fields (Closed)
Patch Set: Changed combobox selection and fix for text selection bug Created 3 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 1607 matching lines...) Expand 10 before | Expand all | Expand 10 after
1618 FPDF_PAGE page = FPDF_LoadPage(doc, i); 1618 FPDF_PAGE page = FPDF_LoadPage(doc, i);
1619 TransformPDFPageForPrinting(page, print_settings); 1619 TransformPDFPageForPrinting(page, print_settings);
1620 FPDF_ClosePage(page); 1620 FPDF_ClosePage(page);
1621 } 1621 }
1622 } 1622 }
1623 } 1623 }
1624 1624
1625 void PDFiumEngine::SaveSelectedFormForPrint() { 1625 void PDFiumEngine::SaveSelectedFormForPrint() {
1626 FORM_ForceToKillFocus(form_); 1626 FORM_ForceToKillFocus(form_);
1627 client_->FormTextFieldFocusChange(false); 1627 client_->FormTextFieldFocusChange(false);
1628 SetInFormTextArea(false);
1629 }
1630
1631 void PDFiumEngine::SetFormSelectedText(FPDF_FORMHANDLE form_handle,
1632 FPDF_PAGE page) {
1633 size_t form_sel_text_len =
1634 FORM_GetSelectedText(form_handle, page, nullptr, 0);
1635
1636 // Initial length = 2 (not 0) because chars are two bytes and
Lei Zhang 2017/06/20 00:26:24 This statement is confusing. It can be read as: ch
drgage 2017/06/20 23:14:27 Done.
1637 // NUL char is included in count.
1638 if (form_sel_text_len <= 2)
dsinclair 2017/06/20 15:21:44 Is this check needed? Won't we just end up creatin
Lei Zhang 2017/06/20 21:21:22 Might as well bale out early if we knows continuin
drgage 2017/06/20 23:14:27 Done.
drgage 2017/06/20 23:14:27 Yeah, you're right. I just wasn't sure if I should
dsinclair 2017/06/21 13:03:28 Leaving it is fine, early exiting does make it a b
drgage 2017/06/23 22:16:13 Done.
1639 return;
1640
1641 base::string16 selected_form_text16;
1642 PDFiumAPIStringBufferSizeInBytesAdapter<base::string16> string_adapter(
1643 &selected_form_text16, form_sel_text_len, false);
1644 string_adapter.Close(FORM_GetSelectedText(
1645 form_handle, page, string_adapter.GetData(), form_sel_text_len));
1646
1647 std::string selected_form_text = UTF16ToUTF8(selected_form_text16);
1648 if (!selected_form_text.empty()) {
1649 pp::PDF::SetSelectedText(GetPluginInstance(), selected_form_text.c_str());
1650 }
1628 } 1651 }
1629 1652
1630 void PDFiumEngine::PrintEnd() { 1653 void PDFiumEngine::PrintEnd() {
1631 FORM_DoDocumentAAction(form_, FPDFDOC_AACTION_DP); 1654 FORM_DoDocumentAAction(form_, FPDFDOC_AACTION_DP);
1632 } 1655 }
1633 1656
1634 PDFiumPage::Area PDFiumEngine::GetCharIndex(const pp::MouseInputEvent& event, 1657 PDFiumPage::Area PDFiumEngine::GetCharIndex(const pp::MouseInputEvent& event,
1635 int* page_index, 1658 int* page_index,
1636 int* char_index, 1659 int* char_index,
1637 int* form_type, 1660 int* form_type,
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after
1714 return false; 1737 return false;
1715 1738
1716 if (page_index != -1) { 1739 if (page_index != -1) {
1717 last_page_mouse_down_ = page_index; 1740 last_page_mouse_down_ = page_index;
1718 double page_x, page_y; 1741 double page_x, page_y;
1719 pp::Point point = event.GetPosition(); 1742 pp::Point point = event.GetPosition();
1720 DeviceToPage(page_index, point.x(), point.y(), &page_x, &page_y); 1743 DeviceToPage(page_index, point.x(), point.y(), &page_x, &page_y);
1721 1744
1722 FORM_OnLButtonDown(form_, pages_[page_index]->GetPage(), 0, page_x, page_y); 1745 FORM_OnLButtonDown(form_, pages_[page_index]->GetPage(), 0, page_x, page_y);
1723 if (form_type > FPDF_FORMFIELD_UNKNOWN) { // returns -1 sometimes... 1746 if (form_type > FPDF_FORMFIELD_UNKNOWN) { // returns -1 sometimes...
1724 mouse_down_state_.Set(PDFiumPage::NONSELECTABLE_AREA, target); 1747 mouse_down_state_.Set(form_type == FPDF_FORMFIELD_TEXTFIELD
Lei Zhang 2017/06/20 00:26:24 What about FPDF_FORMFIELD_COMBOBOX?
drgage 2017/06/20 23:14:27 I think I got confused while refactoring this yest
1748 ? PDFiumPage::FORM_TEXT_AREA
1749 : PDFiumPage::NONSELECTABLE_AREA,
1750 target);
1751
1725 bool is_valid_control = (form_type == FPDF_FORMFIELD_TEXTFIELD || 1752 bool is_valid_control = (form_type == FPDF_FORMFIELD_TEXTFIELD ||
1726 form_type == FPDF_FORMFIELD_COMBOBOX); 1753 form_type == FPDF_FORMFIELD_COMBOBOX);
1754
1755 // TODO(bug_62400): figure out selection and copying
1756 // for XFA fields
1727 #if defined(PDF_ENABLE_XFA) 1757 #if defined(PDF_ENABLE_XFA)
1728 is_valid_control |= (form_type == FPDF_FORMFIELD_XFA); 1758 is_valid_control |= (form_type == FPDF_FORMFIELD_XFA);
1729 #endif 1759 #endif
1730 client_->FormTextFieldFocusChange(is_valid_control); 1760 client_->FormTextFieldFocusChange(is_valid_control);
1761 SetInFormTextArea(is_valid_control);
1731 return true; // Return now before we get into the selection code. 1762 return true; // Return now before we get into the selection code.
1732 } 1763 }
1733 } 1764 }
1734
1735 client_->FormTextFieldFocusChange(false); 1765 client_->FormTextFieldFocusChange(false);
1766 SetInFormTextArea(false);
1736 1767
1737 if (area != PDFiumPage::TEXT_AREA) 1768 if (area != PDFiumPage::TEXT_AREA)
1738 return true; // Return true so WebKit doesn't do its own highlighting. 1769 return true; // Return true so WebKit doesn't do its own highlighting.
1739 1770
1740 if (event.GetClickCount() == 1) { 1771 if (event.GetClickCount() == 1) {
1741 OnSingleClick(page_index, char_index); 1772 OnSingleClick(page_index, char_index);
1742 } else if (event.GetClickCount() == 2 || event.GetClickCount() == 3) { 1773 } else if (event.GetClickCount() == 2 || event.GetClickCount() == 3) {
1743 OnMultipleClick(event.GetClickCount(), page_index, char_index); 1774 OnMultipleClick(event.GetClickCount(), page_index, char_index);
1744 } 1775 }
1745 1776
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
1801 bool alt_key = !!(modifiers & PP_INPUTEVENT_MODIFIER_ALTKEY); 1832 bool alt_key = !!(modifiers & PP_INPUTEVENT_MODIFIER_ALTKEY);
1802 bool ctrl_key = !!(modifiers & PP_INPUTEVENT_MODIFIER_CONTROLKEY); 1833 bool ctrl_key = !!(modifiers & PP_INPUTEVENT_MODIFIER_CONTROLKEY);
1803 bool meta_key = !!(modifiers & PP_INPUTEVENT_MODIFIER_METAKEY); 1834 bool meta_key = !!(modifiers & PP_INPUTEVENT_MODIFIER_METAKEY);
1804 bool shift_key = !!(modifiers & PP_INPUTEVENT_MODIFIER_SHIFTKEY); 1835 bool shift_key = !!(modifiers & PP_INPUTEVENT_MODIFIER_SHIFTKEY);
1805 1836
1806 WindowOpenDisposition disposition = ui::DispositionFromClick( 1837 WindowOpenDisposition disposition = ui::DispositionFromClick(
1807 middle_button, alt_key, ctrl_key, meta_key, shift_key); 1838 middle_button, alt_key, ctrl_key, meta_key, shift_key);
1808 1839
1809 client_->NavigateTo(target.url, disposition); 1840 client_->NavigateTo(target.url, disposition);
1810 client_->FormTextFieldFocusChange(false); 1841 client_->FormTextFieldFocusChange(false);
1842 SetInFormTextArea(false);
1811 return true; 1843 return true;
1812 } 1844 }
1813 if (area == PDFiumPage::DOCLINK_AREA) { 1845 if (area == PDFiumPage::DOCLINK_AREA) {
1814 client_->ScrollToPage(target.page); 1846 client_->ScrollToPage(target.page);
1815 client_->FormTextFieldFocusChange(false); 1847 client_->FormTextFieldFocusChange(false);
1848 SetInFormTextArea(false);
dsinclair 2017/06/20 15:21:43 Should we add a SetFormTextAreaStatus(bool) or som
drgage 2017/06/20 23:14:27 I could do this, or I could add the line "client_-
dsinclair 2017/06/21 13:03:28 Moving the FormTextFieldFocusChange into SetInForm
drgage 2017/06/23 22:16:12 Done.
1816 return true; 1849 return true;
1817 } 1850 }
1818 } 1851 }
1819 1852
1820 // Prevent middle mouse button from selecting texts. 1853 // Prevent middle mouse button from selecting texts.
1821 if (event.GetButton() == PP_INPUTEVENT_MOUSEBUTTON_MIDDLE) 1854 if (event.GetButton() == PP_INPUTEVENT_MOUSEBUTTON_MIDDLE)
1822 return false; 1855 return false;
1823 1856
1824 if (page_index != -1) { 1857 if (page_index != -1) {
1825 double page_x, page_y; 1858 double page_x, page_y;
1826 pp::Point point = event.GetPosition(); 1859 pp::Point point = event.GetPosition();
1827 DeviceToPage(page_index, point.x(), point.y(), &page_x, &page_y); 1860 DeviceToPage(page_index, point.x(), point.y(), &page_x, &page_y);
1828 FORM_OnLButtonUp(form_, pages_[page_index]->GetPage(), 0, page_x, page_y); 1861 FORM_OnLButtonUp(form_, pages_[page_index]->GetPage(), 0, page_x, page_y);
1829 } 1862 }
1830 1863
1864 if (area == PDFiumPage::FORM_TEXT_AREA)
1865 SetFormSelectedText(form_, pages_[last_page_mouse_down_]->GetPage());
1866
1831 if (!selecting_) 1867 if (!selecting_)
1832 return false; 1868 return false;
1833 1869
1834 SetSelecting(false); 1870 SetSelecting(false);
1835 return true; 1871 return true;
1836 } 1872 }
1837 1873
1838 bool PDFiumEngine::OnMouseMove(const pp::MouseInputEvent& event) { 1874 bool PDFiumEngine::OnMouseMove(const pp::MouseInputEvent& event) {
1839 int page_index = -1; 1875 int page_index = -1;
1840 int char_index = -1; 1876 int char_index = -1;
(...skipping 11 matching lines...) Expand all
1852 PP_CursorType_Dev cursor; 1888 PP_CursorType_Dev cursor;
1853 switch (area) { 1889 switch (area) {
1854 case PDFiumPage::TEXT_AREA: 1890 case PDFiumPage::TEXT_AREA:
1855 cursor = PP_CURSORTYPE_IBEAM; 1891 cursor = PP_CURSORTYPE_IBEAM;
1856 break; 1892 break;
1857 case PDFiumPage::WEBLINK_AREA: 1893 case PDFiumPage::WEBLINK_AREA:
1858 case PDFiumPage::DOCLINK_AREA: 1894 case PDFiumPage::DOCLINK_AREA:
1859 cursor = PP_CURSORTYPE_HAND; 1895 cursor = PP_CURSORTYPE_HAND;
1860 break; 1896 break;
1861 case PDFiumPage::NONSELECTABLE_AREA: 1897 case PDFiumPage::NONSELECTABLE_AREA:
1898 case PDFiumPage::FORM_TEXT_AREA:
1862 default: 1899 default:
1863 switch (form_type) { 1900 switch (form_type) {
1864 case FPDF_FORMFIELD_PUSHBUTTON: 1901 case FPDF_FORMFIELD_PUSHBUTTON:
1865 case FPDF_FORMFIELD_CHECKBOX: 1902 case FPDF_FORMFIELD_CHECKBOX:
1866 case FPDF_FORMFIELD_RADIOBUTTON: 1903 case FPDF_FORMFIELD_RADIOBUTTON:
1867 case FPDF_FORMFIELD_COMBOBOX: 1904 case FPDF_FORMFIELD_COMBOBOX:
1868 case FPDF_FORMFIELD_LISTBOX: 1905 case FPDF_FORMFIELD_LISTBOX:
1869 cursor = PP_CURSORTYPE_HAND; 1906 cursor = PP_CURSORTYPE_HAND;
1870 break; 1907 break;
1871 case FPDF_FORMFIELD_TEXTFIELD: 1908 case FPDF_FORMFIELD_TEXTFIELD:
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after
1978 // http://chrome-corpsvn.mtv.corp.google.com/viewvc?view=rev&root=chrome&rev ision=31805 2015 // http://chrome-corpsvn.mtv.corp.google.com/viewvc?view=rev&root=chrome&rev ision=31805
1979 // for more information. So just fake one since PDFium uses it. 2016 // for more information. So just fake one since PDFium uses it.
1980 std::string str; 2017 std::string str;
1981 str.push_back(event.GetKeyCode()); 2018 str.push_back(event.GetKeyCode());
1982 pp::KeyboardInputEvent synthesized(pp::KeyboardInputEvent( 2019 pp::KeyboardInputEvent synthesized(pp::KeyboardInputEvent(
1983 client_->GetPluginInstance(), PP_INPUTEVENT_TYPE_CHAR, 2020 client_->GetPluginInstance(), PP_INPUTEVENT_TYPE_CHAR,
1984 event.GetTimeStamp(), event.GetModifiers(), event.GetKeyCode(), str)); 2021 event.GetTimeStamp(), event.GetModifiers(), event.GetKeyCode(), str));
1985 OnChar(synthesized); 2022 OnChar(synthesized);
1986 } 2023 }
1987 2024
2025 // If form selected text is empty and key pressed within form text area,
2026 // plugin text selection should be cleared.
2027 if (FORM_GetSelectedText(form_, pages_[last_page_mouse_down_]->GetPage(),
Lei Zhang 2017/06/20 00:26:24 Since the conditional is evaluated from left to ri
drgage 2017/06/20 23:14:27 Done.
2028 nullptr, 0) <= 2 &&
2029 in_form_text_area_)
2030 pp::PDF::SetSelectedText(GetPluginInstance(), "");
2031
1988 return rv; 2032 return rv;
1989 } 2033 }
1990 2034
1991 bool PDFiumEngine::OnKeyUp(const pp::KeyboardInputEvent& event) { 2035 bool PDFiumEngine::OnKeyUp(const pp::KeyboardInputEvent& event) {
1992 if (last_page_mouse_down_ == -1) 2036 if (last_page_mouse_down_ == -1)
1993 return false; 2037 return false;
1994 2038
2039 if (in_form_text_area_) {
2040 if (event.GetKeyCode() == ui::VKEY_SHIFT)
2041 SetFormSelectedText(form_, pages_[last_page_mouse_down_]->GetPage());
2042 }
2043
1995 return !!FORM_OnKeyUp(form_, pages_[last_page_mouse_down_]->GetPage(), 2044 return !!FORM_OnKeyUp(form_, pages_[last_page_mouse_down_]->GetPage(),
1996 event.GetKeyCode(), event.GetModifiers()); 2045 event.GetKeyCode(), event.GetModifiers());
1997 } 2046 }
1998 2047
1999 bool PDFiumEngine::OnChar(const pp::KeyboardInputEvent& event) { 2048 bool PDFiumEngine::OnChar(const pp::KeyboardInputEvent& event) {
2000 if (last_page_mouse_down_ == -1) 2049 if (last_page_mouse_down_ == -1)
2001 return false; 2050 return false;
2002 2051
2003 base::string16 str = base::UTF8ToUTF16(event.GetCharacterText().AsString()); 2052 base::string16 str = base::UTF8ToUTF16(event.GetCharacterText().AsString());
2004 return !!FORM_OnChar(form_, pages_[last_page_mouse_down_]->GetPage(), str[0], 2053 return !!FORM_OnChar(form_, pages_[last_page_mouse_down_]->GetPage(), str[0],
(...skipping 1541 matching lines...) Expand 10 before | Expand all | Expand 10 after
3546 client_->ScrollToPage(most_visible_page); 3595 client_->ScrollToPage(most_visible_page);
3547 } 3596 }
3548 3597
3549 void PDFiumEngine::SetSelecting(bool selecting) { 3598 void PDFiumEngine::SetSelecting(bool selecting) {
3550 bool was_selecting = selecting_; 3599 bool was_selecting = selecting_;
3551 selecting_ = selecting; 3600 selecting_ = selecting;
3552 if (selecting_ != was_selecting) 3601 if (selecting_ != was_selecting)
3553 client_->IsSelectingChanged(selecting); 3602 client_->IsSelectingChanged(selecting);
3554 } 3603 }
3555 3604
3605 void PDFiumEngine::SetInFormTextArea(bool in_form_text_area) {
3606 in_form_text_area_ = in_form_text_area;
3607 }
3608
3556 void PDFiumEngine::ScheduleTouchTimer(const pp::TouchInputEvent& evt) { 3609 void PDFiumEngine::ScheduleTouchTimer(const pp::TouchInputEvent& evt) {
3557 touch_timers_[++next_touch_timer_id_] = evt; 3610 touch_timers_[++next_touch_timer_id_] = evt;
3558 client_->ScheduleTouchTimerCallback(next_touch_timer_id_, 3611 client_->ScheduleTouchTimerCallback(next_touch_timer_id_,
3559 kTouchLongPressTimeoutMs); 3612 kTouchLongPressTimeoutMs);
3560 } 3613 }
3561 3614
3562 void PDFiumEngine::KillTouchTimer(int timer_id) { 3615 void PDFiumEngine::KillTouchTimer(int timer_id) {
3563 touch_timers_.erase(timer_id); 3616 touch_timers_.erase(timer_id);
3564 } 3617 }
3565 3618
(...skipping 569 matching lines...) Expand 10 before | Expand all | Expand 10 after
4135 FPDF_DOCUMENT doc = 4188 FPDF_DOCUMENT doc =
4136 FPDF_LoadMemDocument(pdf_buffer, pdf_buffer_size, nullptr); 4189 FPDF_LoadMemDocument(pdf_buffer, pdf_buffer_size, nullptr);
4137 if (!doc) 4190 if (!doc)
4138 return false; 4191 return false;
4139 bool success = FPDF_GetPageSizeByIndex(doc, page_number, width, height) != 0; 4192 bool success = FPDF_GetPageSizeByIndex(doc, page_number, width, height) != 0;
4140 FPDF_CloseDocument(doc); 4193 FPDF_CloseDocument(doc);
4141 return success; 4194 return success;
4142 } 4195 }
4143 4196
4144 } // namespace chrome_pdf 4197 } // namespace chrome_pdf
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698