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

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

Issue 2963753003: Fix discrepancies in form text selection between PDF and HTML forms. (Closed)
Patch Set: Adjust calls to SetMouseLeftButtonDown() to avoid early returns Created 3 years, 5 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
« pdf/pdfium/pdfium_engine.h ('K') | « pdf/pdfium/pdfium_engine.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 1619 matching lines...) Expand 10 before | Expand all | Expand 10 after
1630 SetInFormTextArea(false); 1630 SetInFormTextArea(false);
1631 } 1631 }
1632 1632
1633 void PDFiumEngine::SetFormSelectedText(FPDF_FORMHANDLE form_handle, 1633 void PDFiumEngine::SetFormSelectedText(FPDF_FORMHANDLE form_handle,
1634 FPDF_PAGE page) { 1634 FPDF_PAGE page) {
1635 unsigned long form_sel_text_len = 1635 unsigned long form_sel_text_len =
1636 FORM_GetSelectedText(form_handle, page, nullptr, 0); 1636 FORM_GetSelectedText(form_handle, page, nullptr, 0);
1637 1637
1638 // Check to see if there is selected text in the form. When 1638 // Check to see if there is selected text in the form. When
1639 // |form_sel_text_len| is 2, that represents a wide string with just a 1639 // |form_sel_text_len| is 2, that represents a wide string with just a
1640 // NUL-terminator. 1640 // NUL-terminator.
Lei Zhang 2017/07/05 19:31:12 Update the comment, now that the if-statement has
drgage 2017/07/06 19:13:55 Done.
1641 if (form_sel_text_len <= 2) 1641 if (form_sel_text_len <= 2 && selected_form_text_.empty())
1642 return; 1642 return;
1643 1643
1644 base::string16 selected_form_text16; 1644 base::string16 selected_form_text16;
1645 PDFiumAPIStringBufferSizeInBytesAdapter<base::string16> string_adapter( 1645 PDFiumAPIStringBufferSizeInBytesAdapter<base::string16> string_adapter(
1646 &selected_form_text16, form_sel_text_len, false); 1646 &selected_form_text16, form_sel_text_len, false);
1647 string_adapter.Close(FORM_GetSelectedText( 1647 string_adapter.Close(FORM_GetSelectedText(
1648 form_handle, page, string_adapter.GetData(), form_sel_text_len)); 1648 form_handle, page, string_adapter.GetData(), form_sel_text_len));
1649 1649
1650 std::string selected_form_text = base::UTF16ToUTF8(selected_form_text16); 1650 // Update previous and current selections, then compare them to check if
1651 if (!selected_form_text.empty()) { 1651 // selection has changed. If so, set plugin text selection.
1652 pp::PDF::SetSelectedText(GetPluginInstance(), selected_form_text.c_str()); 1652 std::string selected_form_text = selected_form_text_;
1653 } 1653 selected_form_text_ = base::UTF16ToUTF8(selected_form_text16);
1654 if (selected_form_text != selected_form_text_)
1655 pp::PDF::SetSelectedText(GetPluginInstance(), selected_form_text_.c_str());
1654 } 1656 }
1655 1657
1656 void PDFiumEngine::PrintEnd() { 1658 void PDFiumEngine::PrintEnd() {
1657 FORM_DoDocumentAAction(form_, FPDFDOC_AACTION_DP); 1659 FORM_DoDocumentAAction(form_, FPDFDOC_AACTION_DP);
1658 } 1660 }
1659 1661
1660 PDFiumPage::Area PDFiumEngine::GetCharIndex(const pp::MouseInputEvent& event, 1662 PDFiumPage::Area PDFiumEngine::GetCharIndex(const pp::MouseInputEvent& event,
1661 int* page_index, 1663 int* page_index,
1662 int* char_index, 1664 int* char_index,
1663 int* form_type, 1665 int* form_type,
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
1712 SelectionChangeInvalidator selection_invalidator(this); 1714 SelectionChangeInvalidator selection_invalidator(this);
1713 selection_.clear(); 1715 selection_.clear();
1714 return true; 1716 return true;
1715 } 1717 }
1716 1718
1717 if (event.GetButton() != PP_INPUTEVENT_MOUSEBUTTON_LEFT && 1719 if (event.GetButton() != PP_INPUTEVENT_MOUSEBUTTON_LEFT &&
1718 event.GetButton() != PP_INPUTEVENT_MOUSEBUTTON_MIDDLE) { 1720 event.GetButton() != PP_INPUTEVENT_MOUSEBUTTON_MIDDLE) {
1719 return false; 1721 return false;
1720 } 1722 }
1721 1723
1724 SetMouseLeftButtonDown(event.GetButton() == PP_INPUTEVENT_MOUSEBUTTON_LEFT);
1725
1722 SelectionChangeInvalidator selection_invalidator(this); 1726 SelectionChangeInvalidator selection_invalidator(this);
1723 selection_.clear(); 1727 selection_.clear();
1724 1728
1725 int page_index = -1; 1729 int page_index = -1;
1726 int char_index = -1; 1730 int char_index = -1;
1727 int form_type = FPDF_FORMFIELD_UNKNOWN; 1731 int form_type = FPDF_FORMFIELD_UNKNOWN;
1728 PDFiumPage::LinkTarget target; 1732 PDFiumPage::LinkTarget target;
1729 PDFiumPage::Area area = 1733 PDFiumPage::Area area =
1730 GetCharIndex(event, &page_index, &char_index, &form_type, &target); 1734 GetCharIndex(event, &page_index, &char_index, &form_type, &target);
1731 mouse_down_state_.Set(area, target); 1735 mouse_down_state_.Set(area, target);
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after
1807 selection_.push_back(PDFiumRange(pages_[page_index].get(), start_index, 1811 selection_.push_back(PDFiumRange(pages_[page_index].get(), start_index,
1808 end_index - start_index)); 1812 end_index - start_index));
1809 } 1813 }
1810 1814
1811 bool PDFiumEngine::OnMouseUp(const pp::MouseInputEvent& event) { 1815 bool PDFiumEngine::OnMouseUp(const pp::MouseInputEvent& event) {
1812 if (event.GetButton() != PP_INPUTEVENT_MOUSEBUTTON_LEFT && 1816 if (event.GetButton() != PP_INPUTEVENT_MOUSEBUTTON_LEFT &&
1813 event.GetButton() != PP_INPUTEVENT_MOUSEBUTTON_MIDDLE) { 1817 event.GetButton() != PP_INPUTEVENT_MOUSEBUTTON_MIDDLE) {
1814 return false; 1818 return false;
1815 } 1819 }
1816 1820
1821 if (event.GetButton() == PP_INPUTEVENT_MOUSEBUTTON_LEFT)
1822 SetMouseLeftButtonDown(false);
1823
1817 int page_index = -1; 1824 int page_index = -1;
1818 int char_index = -1; 1825 int char_index = -1;
1819 int form_type = FPDF_FORMFIELD_UNKNOWN; 1826 int form_type = FPDF_FORMFIELD_UNKNOWN;
1820 PDFiumPage::LinkTarget target; 1827 PDFiumPage::LinkTarget target;
1821 PDFiumPage::Area area = 1828 PDFiumPage::Area area =
1822 GetCharIndex(event, &page_index, &char_index, &form_type, &target); 1829 GetCharIndex(event, &page_index, &char_index, &form_type, &target);
1823 1830
1824 // Open link on mouse up for same link for which mouse down happened earlier. 1831 // Open link on mouse up for same link for which mouse down happened earlier.
1825 if (mouse_down_state_.Matches(area, target)) { 1832 if (mouse_down_state_.Matches(area, target)) {
1826 if (area == PDFiumPage::WEBLINK_AREA) { 1833 if (area == PDFiumPage::WEBLINK_AREA) {
(...skipping 23 matching lines...) Expand all
1850 if (event.GetButton() == PP_INPUTEVENT_MOUSEBUTTON_MIDDLE) 1857 if (event.GetButton() == PP_INPUTEVENT_MOUSEBUTTON_MIDDLE)
1851 return false; 1858 return false;
1852 1859
1853 if (page_index != -1) { 1860 if (page_index != -1) {
1854 double page_x, page_y; 1861 double page_x, page_y;
1855 pp::Point point = event.GetPosition(); 1862 pp::Point point = event.GetPosition();
1856 DeviceToPage(page_index, point.x(), point.y(), &page_x, &page_y); 1863 DeviceToPage(page_index, point.x(), point.y(), &page_x, &page_y);
1857 FORM_OnLButtonUp(form_, pages_[page_index]->GetPage(), 0, page_x, page_y); 1864 FORM_OnLButtonUp(form_, pages_[page_index]->GetPage(), 0, page_x, page_y);
1858 } 1865 }
1859 1866
1860 if (area == PDFiumPage::FORM_TEXT_AREA && last_page_mouse_down_ != -1)
1861 SetFormSelectedText(form_, pages_[last_page_mouse_down_]->GetPage());
1862
1863 if (!selecting_) 1867 if (!selecting_)
1864 return false; 1868 return false;
1865 1869
1866 SetSelecting(false); 1870 SetSelecting(false);
1867 return true; 1871 return true;
1868 } 1872 }
1869 1873
1870 bool PDFiumEngine::OnMouseMove(const pp::MouseInputEvent& event) { 1874 bool PDFiumEngine::OnMouseMove(const pp::MouseInputEvent& event) {
1871 int page_index = -1; 1875 int page_index = -1;
1872 int char_index = -1; 1876 int char_index = -1;
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
1917 DeviceToPage(page_index, point.x(), point.y(), &page_x, &page_y); 1921 DeviceToPage(page_index, point.x(), point.y(), &page_x, &page_y);
1918 FORM_OnMouseMove(form_, pages_[page_index]->GetPage(), 0, page_x, page_y); 1922 FORM_OnMouseMove(form_, pages_[page_index]->GetPage(), 0, page_x, page_y);
1919 } 1923 }
1920 1924
1921 client_->UpdateCursor(cursor); 1925 client_->UpdateCursor(cursor);
1922 std::string url = GetLinkAtPosition(event.GetPosition()); 1926 std::string url = GetLinkAtPosition(event.GetPosition());
1923 if (url != link_under_cursor_) { 1927 if (url != link_under_cursor_) {
1924 link_under_cursor_ = url; 1928 link_under_cursor_ = url;
1925 pp::PDF::SetLinkUnderCursor(GetPluginInstance(), url.c_str()); 1929 pp::PDF::SetLinkUnderCursor(GetPluginInstance(), url.c_str());
1926 } 1930 }
1931
1932 // If in form text area while left mouse button is held down, check if form
1933 // text selection needs to be updated.
1934 if (mouse_left_button_down_ && area == PDFiumPage::FORM_TEXT_AREA &&
1935 last_page_mouse_down_ != -1) {
1936 SetFormSelectedText(form_, pages_[last_page_mouse_down_]->GetPage());
1937 }
1938
1927 // No need to swallow the event, since this might interfere with the 1939 // No need to swallow the event, since this might interfere with the
1928 // scrollbars if the user is dragging them. 1940 // scrollbars if the user is dragging them.
1929 return false; 1941 return false;
1930 } 1942 }
1931 1943
1932 // We're selecting but right now we're not over text, so don't change the 1944 // We're selecting but right now we're not over text, so don't change the
1933 // current selection. 1945 // current selection.
1934 if (area != PDFiumPage::TEXT_AREA && area != PDFiumPage::WEBLINK_AREA && 1946 if (area != PDFiumPage::TEXT_AREA && area != PDFiumPage::WEBLINK_AREA &&
1935 area != PDFiumPage::DOCLINK_AREA) { 1947 area != PDFiumPage::DOCLINK_AREA) {
1936 return false; 1948 return false;
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
2011 // http://chrome-corpsvn.mtv.corp.google.com/viewvc?view=rev&root=chrome&rev ision=31805 2023 // http://chrome-corpsvn.mtv.corp.google.com/viewvc?view=rev&root=chrome&rev ision=31805
2012 // for more information. So just fake one since PDFium uses it. 2024 // for more information. So just fake one since PDFium uses it.
2013 std::string str; 2025 std::string str;
2014 str.push_back(event.GetKeyCode()); 2026 str.push_back(event.GetKeyCode());
2015 pp::KeyboardInputEvent synthesized(pp::KeyboardInputEvent( 2027 pp::KeyboardInputEvent synthesized(pp::KeyboardInputEvent(
2016 client_->GetPluginInstance(), PP_INPUTEVENT_TYPE_CHAR, 2028 client_->GetPluginInstance(), PP_INPUTEVENT_TYPE_CHAR,
2017 event.GetTimeStamp(), event.GetModifiers(), event.GetKeyCode(), str)); 2029 event.GetTimeStamp(), event.GetModifiers(), event.GetKeyCode(), str));
2018 OnChar(synthesized); 2030 OnChar(synthesized);
2019 } 2031 }
2020 2032
2021 // If form selected text is empty and key pressed within form text area,
2022 // plugin text selection should be cleared.
2023 if (in_form_text_area_ &&
2024 FORM_GetSelectedText(form_, pages_[last_page_mouse_down_]->GetPage(),
2025 nullptr, 0) <= 2) {
2026 pp::PDF::SetSelectedText(GetPluginInstance(), "");
2027 }
2028
2029 return rv; 2033 return rv;
2030 } 2034 }
2031 2035
2032 bool PDFiumEngine::OnKeyUp(const pp::KeyboardInputEvent& event) { 2036 bool PDFiumEngine::OnKeyUp(const pp::KeyboardInputEvent& event) {
2033 if (last_page_mouse_down_ == -1) 2037 if (last_page_mouse_down_ == -1)
2034 return false; 2038 return false;
2035 2039
2040 // Check if form text selection needs to be updated.
2036 if (in_form_text_area_) { 2041 if (in_form_text_area_) {
2037 if (event.GetKeyCode() == ui::VKEY_SHIFT) 2042 SetFormSelectedText(form_, pages_[last_page_mouse_down_]->GetPage());
2038 SetFormSelectedText(form_, pages_[last_page_mouse_down_]->GetPage());
2039 } 2043 }
2040 2044
2041 return !!FORM_OnKeyUp(form_, pages_[last_page_mouse_down_]->GetPage(), 2045 return !!FORM_OnKeyUp(form_, pages_[last_page_mouse_down_]->GetPage(),
2042 event.GetKeyCode(), event.GetModifiers()); 2046 event.GetKeyCode(), event.GetModifiers());
2043 } 2047 }
2044 2048
2045 bool PDFiumEngine::OnChar(const pp::KeyboardInputEvent& event) { 2049 bool PDFiumEngine::OnChar(const pp::KeyboardInputEvent& event) {
2046 if (last_page_mouse_down_ == -1) 2050 if (last_page_mouse_down_ == -1)
2047 return false; 2051 return false;
2048 2052
(...skipping 1560 matching lines...) Expand 10 before | Expand all | Expand 10 after
3609 selecting_ = selecting; 3613 selecting_ = selecting;
3610 if (selecting_ != was_selecting) 3614 if (selecting_ != was_selecting)
3611 client_->IsSelectingChanged(selecting); 3615 client_->IsSelectingChanged(selecting);
3612 } 3616 }
3613 3617
3614 void PDFiumEngine::SetInFormTextArea(bool in_form_text_area) { 3618 void PDFiumEngine::SetInFormTextArea(bool in_form_text_area) {
3615 client_->FormTextFieldFocusChange(in_form_text_area); 3619 client_->FormTextFieldFocusChange(in_form_text_area);
3616 in_form_text_area_ = in_form_text_area; 3620 in_form_text_area_ = in_form_text_area;
3617 } 3621 }
3618 3622
3623 void PDFiumEngine::SetMouseLeftButtonDown(bool is_mouse_left_button_down) {
3624 mouse_left_button_down_ = is_mouse_left_button_down;
3625 }
3626
3619 void PDFiumEngine::ScheduleTouchTimer(const pp::TouchInputEvent& evt) { 3627 void PDFiumEngine::ScheduleTouchTimer(const pp::TouchInputEvent& evt) {
3620 touch_timers_[++next_touch_timer_id_] = evt; 3628 touch_timers_[++next_touch_timer_id_] = evt;
3621 client_->ScheduleTouchTimerCallback(next_touch_timer_id_, 3629 client_->ScheduleTouchTimerCallback(next_touch_timer_id_,
3622 kTouchLongPressTimeoutMs); 3630 kTouchLongPressTimeoutMs);
3623 } 3631 }
3624 3632
3625 void PDFiumEngine::KillTouchTimer(int timer_id) { 3633 void PDFiumEngine::KillTouchTimer(int timer_id) {
3626 touch_timers_.erase(timer_id); 3634 touch_timers_.erase(timer_id);
3627 } 3635 }
3628 3636
(...skipping 569 matching lines...) Expand 10 before | Expand all | Expand 10 after
4198 FPDF_DOCUMENT doc = 4206 FPDF_DOCUMENT doc =
4199 FPDF_LoadMemDocument(pdf_buffer, pdf_buffer_size, nullptr); 4207 FPDF_LoadMemDocument(pdf_buffer, pdf_buffer_size, nullptr);
4200 if (!doc) 4208 if (!doc)
4201 return false; 4209 return false;
4202 bool success = FPDF_GetPageSizeByIndex(doc, page_number, width, height) != 0; 4210 bool success = FPDF_GetPageSizeByIndex(doc, page_number, width, height) != 0;
4203 FPDF_CloseDocument(doc); 4211 FPDF_CloseDocument(doc);
4204 return success; 4212 return success;
4205 } 4213 }
4206 4214
4207 } // namespace chrome_pdf 4215 } // namespace chrome_pdf
OLDNEW
« pdf/pdfium/pdfium_engine.h ('K') | « pdf/pdfium/pdfium_engine.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698