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

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: Rename and relocate SetMouseHeldDown() to indicate left button 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
« no previous file with comments | « 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 1620 matching lines...) Expand 10 before | Expand all | Expand 10 after
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.
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 75 matching lines...) Expand 10 before | Expand all | Expand 10 after
1739 if (event.GetButton() == PP_INPUTEVENT_MOUSEBUTTON_MIDDLE) 1741 if (event.GetButton() == PP_INPUTEVENT_MOUSEBUTTON_MIDDLE)
1740 return false; 1742 return false;
1741 1743
1742 if (page_index != -1) { 1744 if (page_index != -1) {
1743 last_page_mouse_down_ = page_index; 1745 last_page_mouse_down_ = page_index;
1744 double page_x, page_y; 1746 double page_x, page_y;
1745 pp::Point point = event.GetPosition(); 1747 pp::Point point = event.GetPosition();
1746 DeviceToPage(page_index, point.x(), point.y(), &page_x, &page_y); 1748 DeviceToPage(page_index, point.x(), point.y(), &page_x, &page_y);
1747 1749
1748 FORM_OnLButtonDown(form_, pages_[page_index]->GetPage(), 0, page_x, page_y); 1750 FORM_OnLButtonDown(form_, pages_[page_index]->GetPage(), 0, page_x, page_y);
1751 SetMouseLeftButtonDown(true);
Lei Zhang 2017/07/01 00:30:46 So if we move the call here, then if the user left
drgage 2017/07/05 18:29:31 Done.
1749 if (form_type > FPDF_FORMFIELD_UNKNOWN) { // returns -1 sometimes... 1752 if (form_type > FPDF_FORMFIELD_UNKNOWN) { // returns -1 sometimes...
1750 mouse_down_state_.Set(PDFiumPage::FormTypeToArea(form_type), target); 1753 mouse_down_state_.Set(PDFiumPage::FormTypeToArea(form_type), target);
1751 1754
1752 bool is_valid_control = (form_type == FPDF_FORMFIELD_TEXTFIELD || 1755 bool is_valid_control = (form_type == FPDF_FORMFIELD_TEXTFIELD ||
1753 form_type == FPDF_FORMFIELD_COMBOBOX); 1756 form_type == FPDF_FORMFIELD_COMBOBOX);
1754 1757
1755 // TODO(bug_62400): figure out selection and copying 1758 // TODO(bug_62400): figure out selection and copying
1756 // for XFA fields 1759 // for XFA fields
1757 #if defined(PDF_ENABLE_XFA) 1760 #if defined(PDF_ENABLE_XFA)
1758 is_valid_control |= (form_type == FPDF_FORMFIELD_XFA); 1761 is_valid_control |= (form_type == FPDF_FORMFIELD_XFA);
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after
1848 1851
1849 // Prevent middle mouse button from selecting texts. 1852 // Prevent middle mouse button from selecting texts.
1850 if (event.GetButton() == PP_INPUTEVENT_MOUSEBUTTON_MIDDLE) 1853 if (event.GetButton() == PP_INPUTEVENT_MOUSEBUTTON_MIDDLE)
1851 return false; 1854 return false;
1852 1855
1853 if (page_index != -1) { 1856 if (page_index != -1) {
1854 double page_x, page_y; 1857 double page_x, page_y;
1855 pp::Point point = event.GetPosition(); 1858 pp::Point point = event.GetPosition();
1856 DeviceToPage(page_index, point.x(), point.y(), &page_x, &page_y); 1859 DeviceToPage(page_index, point.x(), point.y(), &page_x, &page_y);
1857 FORM_OnLButtonUp(form_, pages_[page_index]->GetPage(), 0, page_x, page_y); 1860 FORM_OnLButtonUp(form_, pages_[page_index]->GetPage(), 0, page_x, page_y);
1861 SetMouseLeftButtonDown(false);
1858 } 1862 }
1859 1863
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_) 1864 if (!selecting_)
1864 return false; 1865 return false;
1865 1866
1866 SetSelecting(false); 1867 SetSelecting(false);
1867 return true; 1868 return true;
1868 } 1869 }
1869 1870
1870 bool PDFiumEngine::OnMouseMove(const pp::MouseInputEvent& event) { 1871 bool PDFiumEngine::OnMouseMove(const pp::MouseInputEvent& event) {
1871 int page_index = -1; 1872 int page_index = -1;
1872 int char_index = -1; 1873 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); 1918 DeviceToPage(page_index, point.x(), point.y(), &page_x, &page_y);
1918 FORM_OnMouseMove(form_, pages_[page_index]->GetPage(), 0, page_x, page_y); 1919 FORM_OnMouseMove(form_, pages_[page_index]->GetPage(), 0, page_x, page_y);
1919 } 1920 }
1920 1921
1921 client_->UpdateCursor(cursor); 1922 client_->UpdateCursor(cursor);
1922 std::string url = GetLinkAtPosition(event.GetPosition()); 1923 std::string url = GetLinkAtPosition(event.GetPosition());
1923 if (url != link_under_cursor_) { 1924 if (url != link_under_cursor_) {
1924 link_under_cursor_ = url; 1925 link_under_cursor_ = url;
1925 pp::PDF::SetLinkUnderCursor(GetPluginInstance(), url.c_str()); 1926 pp::PDF::SetLinkUnderCursor(GetPluginInstance(), url.c_str());
1926 } 1927 }
1928
1929 // If in form text area while left mouse button is held down, check if form
1930 // text selection needs to be updated.
1931 if (mouse_left_button_down_ && area == PDFiumPage::FORM_TEXT_AREA &&
1932 last_page_mouse_down_ != -1) {
1933 SetFormSelectedText(form_, pages_[last_page_mouse_down_]->GetPage());
1934 }
1935
1927 // No need to swallow the event, since this might interfere with the 1936 // No need to swallow the event, since this might interfere with the
1928 // scrollbars if the user is dragging them. 1937 // scrollbars if the user is dragging them.
1929 return false; 1938 return false;
1930 } 1939 }
1931 1940
1932 // We're selecting but right now we're not over text, so don't change the 1941 // We're selecting but right now we're not over text, so don't change the
1933 // current selection. 1942 // current selection.
1934 if (area != PDFiumPage::TEXT_AREA && area != PDFiumPage::WEBLINK_AREA && 1943 if (area != PDFiumPage::TEXT_AREA && area != PDFiumPage::WEBLINK_AREA &&
1935 area != PDFiumPage::DOCLINK_AREA) { 1944 area != PDFiumPage::DOCLINK_AREA) {
1936 return false; 1945 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 2020 // 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. 2021 // for more information. So just fake one since PDFium uses it.
2013 std::string str; 2022 std::string str;
2014 str.push_back(event.GetKeyCode()); 2023 str.push_back(event.GetKeyCode());
2015 pp::KeyboardInputEvent synthesized(pp::KeyboardInputEvent( 2024 pp::KeyboardInputEvent synthesized(pp::KeyboardInputEvent(
2016 client_->GetPluginInstance(), PP_INPUTEVENT_TYPE_CHAR, 2025 client_->GetPluginInstance(), PP_INPUTEVENT_TYPE_CHAR,
2017 event.GetTimeStamp(), event.GetModifiers(), event.GetKeyCode(), str)); 2026 event.GetTimeStamp(), event.GetModifiers(), event.GetKeyCode(), str));
2018 OnChar(synthesized); 2027 OnChar(synthesized);
2019 } 2028 }
2020 2029
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; 2030 return rv;
2030 } 2031 }
2031 2032
2032 bool PDFiumEngine::OnKeyUp(const pp::KeyboardInputEvent& event) { 2033 bool PDFiumEngine::OnKeyUp(const pp::KeyboardInputEvent& event) {
2033 if (last_page_mouse_down_ == -1) 2034 if (last_page_mouse_down_ == -1)
2034 return false; 2035 return false;
2035 2036
2037 // Check if form text selection needs to be updated.
2036 if (in_form_text_area_) { 2038 if (in_form_text_area_) {
2037 if (event.GetKeyCode() == ui::VKEY_SHIFT) 2039 SetFormSelectedText(form_, pages_[last_page_mouse_down_]->GetPage());
2038 SetFormSelectedText(form_, pages_[last_page_mouse_down_]->GetPage());
2039 } 2040 }
2040 2041
2041 return !!FORM_OnKeyUp(form_, pages_[last_page_mouse_down_]->GetPage(), 2042 return !!FORM_OnKeyUp(form_, pages_[last_page_mouse_down_]->GetPage(),
2042 event.GetKeyCode(), event.GetModifiers()); 2043 event.GetKeyCode(), event.GetModifiers());
2043 } 2044 }
2044 2045
2045 bool PDFiumEngine::OnChar(const pp::KeyboardInputEvent& event) { 2046 bool PDFiumEngine::OnChar(const pp::KeyboardInputEvent& event) {
2046 if (last_page_mouse_down_ == -1) 2047 if (last_page_mouse_down_ == -1)
2047 return false; 2048 return false;
2048 2049
(...skipping 1560 matching lines...) Expand 10 before | Expand all | Expand 10 after
3609 selecting_ = selecting; 3610 selecting_ = selecting;
3610 if (selecting_ != was_selecting) 3611 if (selecting_ != was_selecting)
3611 client_->IsSelectingChanged(selecting); 3612 client_->IsSelectingChanged(selecting);
3612 } 3613 }
3613 3614
3614 void PDFiumEngine::SetInFormTextArea(bool in_form_text_area) { 3615 void PDFiumEngine::SetInFormTextArea(bool in_form_text_area) {
3615 client_->FormTextFieldFocusChange(in_form_text_area); 3616 client_->FormTextFieldFocusChange(in_form_text_area);
3616 in_form_text_area_ = in_form_text_area; 3617 in_form_text_area_ = in_form_text_area;
3617 } 3618 }
3618 3619
3620 void PDFiumEngine::SetMouseLeftButtonDown(bool is_mouse_left_button_down) {
3621 mouse_left_button_down_ = is_mouse_left_button_down;
3622 }
3623
3619 void PDFiumEngine::ScheduleTouchTimer(const pp::TouchInputEvent& evt) { 3624 void PDFiumEngine::ScheduleTouchTimer(const pp::TouchInputEvent& evt) {
3620 touch_timers_[++next_touch_timer_id_] = evt; 3625 touch_timers_[++next_touch_timer_id_] = evt;
3621 client_->ScheduleTouchTimerCallback(next_touch_timer_id_, 3626 client_->ScheduleTouchTimerCallback(next_touch_timer_id_,
3622 kTouchLongPressTimeoutMs); 3627 kTouchLongPressTimeoutMs);
3623 } 3628 }
3624 3629
3625 void PDFiumEngine::KillTouchTimer(int timer_id) { 3630 void PDFiumEngine::KillTouchTimer(int timer_id) {
3626 touch_timers_.erase(timer_id); 3631 touch_timers_.erase(timer_id);
3627 } 3632 }
3628 3633
(...skipping 569 matching lines...) Expand 10 before | Expand all | Expand 10 after
4198 FPDF_DOCUMENT doc = 4203 FPDF_DOCUMENT doc =
4199 FPDF_LoadMemDocument(pdf_buffer, pdf_buffer_size, nullptr); 4204 FPDF_LoadMemDocument(pdf_buffer, pdf_buffer_size, nullptr);
4200 if (!doc) 4205 if (!doc)
4201 return false; 4206 return false;
4202 bool success = FPDF_GetPageSizeByIndex(doc, page_number, width, height) != 0; 4207 bool success = FPDF_GetPageSizeByIndex(doc, page_number, width, height) != 0;
4203 FPDF_CloseDocument(doc); 4208 FPDF_CloseDocument(doc);
4204 return success; 4209 return success;
4205 } 4210 }
4206 4211
4207 } // namespace chrome_pdf 4212 } // namespace chrome_pdf
OLDNEW
« no previous file with comments | « pdf/pdfium/pdfium_engine.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698