0

Remove PPAPI dependency of mouse events from PDFiumEngine

This change removes the pp::MouseInputEvent dependency from
PDFiumEngine. There are two reasons to remove this dependency
- Ability to unit test OnMouseDown, OnMouseUp, OnMouseMove
  and OnMouseEnter event handlers
- Facilitate the PPAPI deprecation by moving some PPAPI dependencies
  out of PDFiumEngine.

Bug: 1091834
Change-Id: I1ba6f9f9197567b9bf795c1eaf9b963290f43956
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2282726
Reviewed-by: Lei Zhang <thestig@chromium.org>
Commit-Queue: Lei Zhang <thestig@chromium.org>
Auto-Submit: Virender Singh <virens@microsoft.com>
Cr-Commit-Position: refs/heads/master@{#788114}
This commit is contained in:
Virender Singh
2020-07-14 12:58:23 +00:00
committed by Commit Bot
parent 52f5af9731
commit e10a8cbf0b
6 changed files with 175 additions and 63 deletions

@ -42,6 +42,7 @@
#include "pdf/pdfium/pdfium_permissions.h"
#include "pdf/pdfium/pdfium_unsupported_features.h"
#include "pdf/ppapi_migration/bitmap.h"
#include "pdf/ppapi_migration/geometry_conversions.h"
#include "pdf/ppapi_migration/input_event_conversions.h"
#include "pdf/url_loader_wrapper_impl.h"
#include "ppapi/c/ppb_input_event.h"
@ -272,19 +273,18 @@ bool IsLinkArea(PDFiumPage::Area area) {
// Normalize a MouseInputEvent. For Mac, this means transforming ctrl + left
// button down events into a right button down events.
pp::MouseInputEvent NormalizeMouseEvent(pp::Instance* instance,
const pp::MouseInputEvent& event) {
pp::MouseInputEvent normalized_event = event;
MouseInputEvent NormalizeMouseEvent(const MouseInputEvent& event) {
MouseInputEvent normalized_event = event;
#if defined(OS_MACOSX)
uint32_t modifiers = event.GetModifiers();
if ((modifiers & PP_INPUTEVENT_MODIFIER_CONTROLKEY) &&
event.GetButton() == PP_INPUTEVENT_MOUSEBUTTON_LEFT &&
event.GetType() == PP_INPUTEVENT_TYPE_MOUSEDOWN) {
uint32_t new_modifiers = modifiers & ~PP_INPUTEVENT_MODIFIER_CONTROLKEY;
normalized_event = pp::MouseInputEvent(
instance, PP_INPUTEVENT_TYPE_MOUSEDOWN, event.GetTimeStamp(),
new_modifiers, PP_INPUTEVENT_MOUSEBUTTON_RIGHT, event.GetPosition(), 1,
event.GetMovement());
if ((event.GetModifiers() & kInputEventModifierControlKey) &&
event.GetButton() == InputEventMouseButtonType::kLeft &&
event.GetEventType() == InputEventType::kMouseDown) {
uint32_t new_modifiers = modifiers & ~kInputEventModifierControlKey;
normalized_event =
MouseInputEvent(InputEventType::kMouseDown, event.GetTimeStamp(),
new_modifiers, InputEventMouseButtonType::kRight,
event.GetPosition(), 1, event.GetMovement());
}
#endif
return normalized_event;
@ -826,16 +826,16 @@ bool PDFiumEngine::HandleEvent(const pp::InputEvent& event) {
bool rv = false;
switch (event.GetType()) {
case PP_INPUTEVENT_TYPE_MOUSEDOWN:
rv = OnMouseDown(pp::MouseInputEvent(event));
rv = OnMouseDown(GetMouseInputEvent(pp::MouseInputEvent(event)));
break;
case PP_INPUTEVENT_TYPE_MOUSEUP:
rv = OnMouseUp(pp::MouseInputEvent(event));
rv = OnMouseUp(GetMouseInputEvent(pp::MouseInputEvent(event)));
break;
case PP_INPUTEVENT_TYPE_MOUSEMOVE:
rv = OnMouseMove(pp::MouseInputEvent(event));
rv = OnMouseMove(GetMouseInputEvent(pp::MouseInputEvent(event)));
break;
case PP_INPUTEVENT_TYPE_MOUSEENTER:
OnMouseEnter(pp::MouseInputEvent(event));
OnMouseEnter(GetMouseInputEvent(pp::MouseInputEvent(event)));
break;
case PP_INPUTEVENT_TYPE_KEYDOWN:
rv = OnKeyDown(GetKeyboardInputEvent(pp::KeyboardInputEvent(event)));
@ -1077,14 +1077,13 @@ PDFiumPage::Area PDFiumEngine::GetCharIndex(const pp::Point& point,
: result;
}
bool PDFiumEngine::OnMouseDown(const pp::MouseInputEvent& event) {
pp::MouseInputEvent normalized_event =
NormalizeMouseEvent(client_->GetPluginInstance(), event);
if (normalized_event.GetButton() == PP_INPUTEVENT_MOUSEBUTTON_LEFT)
bool PDFiumEngine::OnMouseDown(const MouseInputEvent& event) {
MouseInputEvent normalized_event = NormalizeMouseEvent(event);
if (normalized_event.GetButton() == InputEventMouseButtonType::kLeft)
return OnLeftMouseDown(normalized_event);
if (normalized_event.GetButton() == PP_INPUTEVENT_MOUSEBUTTON_MIDDLE)
if (normalized_event.GetButton() == InputEventMouseButtonType::kMiddle)
return OnMiddleMouseDown(normalized_event);
if (normalized_event.GetButton() == PP_INPUTEVENT_MOUSEBUTTON_RIGHT)
if (normalized_event.GetButton() == InputEventMouseButtonType::kRight)
return OnRightMouseDown(normalized_event);
return false;
}
@ -1126,7 +1125,7 @@ void PDFiumEngine::OnMultipleClick(int click_count,
client_->NotifyTouchSelectionOccurred();
}
bool PDFiumEngine::OnLeftMouseDown(const pp::MouseInputEvent& event) {
bool PDFiumEngine::OnLeftMouseDown(const MouseInputEvent& event) {
SetMouseLeftButtonDown(true);
auto selection_invalidator =
@ -1137,9 +1136,9 @@ bool PDFiumEngine::OnLeftMouseDown(const pp::MouseInputEvent& event) {
int char_index = -1;
int form_type = FPDF_FORMFIELD_UNKNOWN;
PDFiumPage::LinkTarget target;
pp::Point point = event.GetPosition();
PDFiumPage::Area area =
GetCharIndex(point, &page_index, &char_index, &form_type, &target);
gfx::Point point = event.GetPosition();
PDFiumPage::Area area = GetCharIndex(PPPointFromPoint(point), &page_index,
&char_index, &form_type, &target);
DCHECK_GE(form_type, FPDF_FORMFIELD_UNKNOWN);
mouse_down_state_.Set(area, target);
@ -1153,7 +1152,7 @@ bool PDFiumEngine::OnLeftMouseDown(const pp::MouseInputEvent& event) {
last_focused_page_ = page_index;
double page_x;
double page_y;
DeviceToPage(page_index, point, &page_x, &page_y);
DeviceToPage(page_index, PPPointFromPoint(point), &page_x, &page_y);
if (form_type != FPDF_FORMFIELD_UNKNOWN) {
// FORM_OnLButton*() will trigger a callback to
@ -1191,10 +1190,10 @@ bool PDFiumEngine::OnLeftMouseDown(const pp::MouseInputEvent& event) {
return true;
}
bool PDFiumEngine::OnMiddleMouseDown(const pp::MouseInputEvent& event) {
bool PDFiumEngine::OnMiddleMouseDown(const MouseInputEvent& event) {
SetMouseLeftButtonDown(false);
mouse_middle_button_down_ = true;
mouse_middle_button_last_position_ = event.GetPosition();
mouse_middle_button_last_position_ = PPPointFromPoint(event.GetPosition());
SelectionChangeInvalidator selection_invalidator(this);
selection_.clear();
@ -1204,8 +1203,8 @@ bool PDFiumEngine::OnMiddleMouseDown(const pp::MouseInputEvent& event) {
int unused_form_type = FPDF_FORMFIELD_UNKNOWN;
PDFiumPage::LinkTarget target;
PDFiumPage::Area area =
GetCharIndex(event.GetPosition(), &unused_page_index, &unused_char_index,
&unused_form_type, &target);
GetCharIndex(PPPointFromPoint(event.GetPosition()), &unused_page_index,
&unused_char_index, &unused_form_type, &target);
mouse_down_state_.Set(area, target);
// Decide whether to open link or not based on user action in mouse up and
@ -1222,10 +1221,10 @@ bool PDFiumEngine::OnMiddleMouseDown(const pp::MouseInputEvent& event) {
return false;
}
bool PDFiumEngine::OnRightMouseDown(const pp::MouseInputEvent& event) {
DCHECK_EQ(PP_INPUTEVENT_MOUSEBUTTON_RIGHT, event.GetButton());
bool PDFiumEngine::OnRightMouseDown(const MouseInputEvent& event) {
DCHECK_EQ(InputEventMouseButtonType::kRight, event.GetButton());
pp::Point point = event.GetPosition();
pp::Point point = PPPointFromPoint(event.GetPosition());
int page_index = -1;
int char_index = -1;
int form_type = FPDF_FORMFIELD_UNKNOWN;
@ -1314,22 +1313,22 @@ bool PDFiumEngine::NavigateToLinkDestination(
return false;
}
bool PDFiumEngine::OnMouseUp(const pp::MouseInputEvent& event) {
if (event.GetButton() != PP_INPUTEVENT_MOUSEBUTTON_LEFT &&
event.GetButton() != PP_INPUTEVENT_MOUSEBUTTON_MIDDLE) {
bool PDFiumEngine::OnMouseUp(const MouseInputEvent& event) {
if (event.GetButton() != InputEventMouseButtonType::kLeft &&
event.GetButton() != InputEventMouseButtonType::kMiddle) {
return false;
}
if (event.GetButton() == PP_INPUTEVENT_MOUSEBUTTON_LEFT)
if (event.GetButton() == InputEventMouseButtonType::kLeft)
SetMouseLeftButtonDown(false);
else if (event.GetButton() == PP_INPUTEVENT_MOUSEBUTTON_MIDDLE)
else if (event.GetButton() == InputEventMouseButtonType::kMiddle)
mouse_middle_button_down_ = false;
int page_index = -1;
int char_index = -1;
int form_type = FPDF_FORMFIELD_UNKNOWN;
PDFiumPage::LinkTarget target;
pp::Point point = event.GetPosition();
pp::Point point = PPPointFromPoint(event.GetPosition());
PDFiumPage::Area area =
GetCharIndex(point, &page_index, &char_index, &form_type, &target);
@ -1350,7 +1349,7 @@ bool PDFiumEngine::OnMouseUp(const pp::MouseInputEvent& event) {
return true;
}
if (event.GetButton() == PP_INPUTEVENT_MOUSEBUTTON_MIDDLE) {
if (event.GetButton() == InputEventMouseButtonType::kMiddle) {
if (kViewerImplementedPanning) {
// Update the cursor when panning stops.
client_->UpdateCursor(DetermineCursorType(area, form_type));
@ -1375,12 +1374,12 @@ bool PDFiumEngine::OnMouseUp(const pp::MouseInputEvent& event) {
return true;
}
bool PDFiumEngine::OnMouseMove(const pp::MouseInputEvent& event) {
bool PDFiumEngine::OnMouseMove(const MouseInputEvent& event) {
int page_index = -1;
int char_index = -1;
int form_type = FPDF_FORMFIELD_UNKNOWN;
PDFiumPage::LinkTarget target;
pp::Point point = event.GetPosition();
pp::Point point = PPPointFromPoint(event.GetPosition());
PDFiumPage::Area area =
GetCharIndex(point, &page_index, &char_index, &form_type, &target);
@ -1400,7 +1399,8 @@ bool PDFiumEngine::OnMouseMove(const pp::MouseInputEvent& event) {
page_y);
}
UpdateLinkUnderCursor(GetLinkAtPosition(event.GetPosition()));
UpdateLinkUnderCursor(
GetLinkAtPosition(PPPointFromPoint(event.GetPosition())));
// If in form text area while left mouse button is held down, check if form
// text selection needs to be updated.
@ -1414,11 +1414,12 @@ bool PDFiumEngine::OnMouseMove(const pp::MouseInputEvent& event) {
// moving the page, rather than the delta the mouse moved.
// GetMovement() does not work here, as small mouse movements are
// considered zero.
pp::Point page_position_delta =
mouse_middle_button_last_position_ - event.GetPosition();
pp::Point page_position_delta = mouse_middle_button_last_position_ -
PPPointFromPoint(event.GetPosition());
if (page_position_delta.x() != 0 || page_position_delta.y() != 0) {
client_->ScrollBy(page_position_delta);
mouse_middle_button_last_position_ = event.GetPosition();
mouse_middle_button_last_position_ =
PPPointFromPoint(event.GetPosition());
}
}
@ -1477,11 +1478,12 @@ PP_CursorType_Dev PDFiumEngine::DetermineCursorType(PDFiumPage::Area area,
}
}
void PDFiumEngine::OnMouseEnter(const pp::MouseInputEvent& event) {
void PDFiumEngine::OnMouseEnter(const MouseInputEvent& event) {
if (event.GetModifiers() & PP_INPUTEVENT_MODIFIER_MIDDLEBUTTONDOWN) {
if (!mouse_middle_button_down_) {
mouse_middle_button_down_ = true;
mouse_middle_button_last_position_ = event.GetPosition();
mouse_middle_button_last_position_ =
PPPointFromPoint(event.GetPosition());
}
} else {
if (mouse_middle_button_down_) {
@ -2382,15 +2384,12 @@ void PDFiumEngine::HandleLongPress(const pp::TouchInputEvent& event) {
base::AutoReset<bool> handling_long_press_guard(&handling_long_press_, true);
pp::FloatPoint fp =
event.GetTouchByIndex(PP_TOUCHLIST_TYPE_TARGETTOUCHES, 0).position();
pp::Point point;
point.set_x(fp.x());
point.set_y(fp.y());
gfx::Point point(fp.x(), fp.y());
// Send a fake mouse down to trigger the multi-click selection code.
pp::MouseInputEvent mouse_event(
client_->GetPluginInstance(), PP_INPUTEVENT_TYPE_MOUSEDOWN,
event.GetTimeStamp(), event.GetModifiers(),
PP_INPUTEVENT_MOUSEBUTTON_LEFT, point, 2, point);
MouseInputEvent mouse_event(
InputEventType::kMouseDown, event.GetTimeStamp(), event.GetModifiers(),
InputEventMouseButtonType::kLeft, point, 2, point);
OnMouseDown(mouse_event);
}

@ -44,6 +44,7 @@
namespace chrome_pdf {
class KeyboardInputEvent;
class MouseInputEvent;
class PDFiumDocument;
class PDFiumPermissions;
@ -380,10 +381,10 @@ class PDFiumEngine : public PDFEngine,
int current_page);
// Input event handlers.
bool OnMouseDown(const pp::MouseInputEvent& event);
bool OnMouseUp(const pp::MouseInputEvent& event);
bool OnMouseMove(const pp::MouseInputEvent& event);
void OnMouseEnter(const pp::MouseInputEvent& event);
bool OnMouseDown(const MouseInputEvent& event);
bool OnMouseUp(const MouseInputEvent& event);
bool OnMouseMove(const MouseInputEvent& event);
void OnMouseEnter(const MouseInputEvent& event);
bool OnKeyDown(const KeyboardInputEvent& event);
bool OnKeyUp(const KeyboardInputEvent& event);
bool OnChar(const KeyboardInputEvent& event);
@ -422,9 +423,9 @@ class PDFiumEngine : public PDFEngine,
void OnSingleClick(int page_index, int char_index);
void OnMultipleClick(int click_count, int page_index, int char_index);
bool OnLeftMouseDown(const pp::MouseInputEvent& event);
bool OnMiddleMouseDown(const pp::MouseInputEvent& event);
bool OnRightMouseDown(const pp::MouseInputEvent& event);
bool OnLeftMouseDown(const MouseInputEvent& event);
bool OnMiddleMouseDown(const MouseInputEvent& event);
bool OnRightMouseDown(const MouseInputEvent& event);
// Starts a progressive paint operation given a rectangle in screen
// coordinates. Returns the index in progressive_rects_.

@ -4,8 +4,10 @@
#include "pdf/ppapi_migration/geometry_conversions.h"
#include "ppapi/c/pp_point.h"
#include "ppapi/c/pp_rect.h"
#include "ppapi/c/pp_size.h"
#include "ui/gfx/geometry/point.h"
#include "ui/gfx/geometry/rect.h"
#include "ui/gfx/geometry/size.h"
@ -20,4 +22,12 @@ gfx::Size SizeFromPPSize(const PP_Size& pp_size) {
return gfx::Size(pp_size.width, pp_size.height);
}
gfx::Point PointFromPPPoint(const PP_Point& pp_point) {
return gfx::Point(pp_point.x, pp_point.y);
}
PP_Point PPPointFromPoint(const gfx::Point& point) {
return {point.x(), point.y()};
}
} // namespace chrome_pdf

@ -5,10 +5,12 @@
#ifndef PDF_PPAPI_MIGRATION_GEOMETRY_CONVERSIONS_H_
#define PDF_PPAPI_MIGRATION_GEOMETRY_CONVERSIONS_H_
struct PP_Point;
struct PP_Rect;
struct PP_Size;
namespace gfx {
class Point;
class Rect;
class Size;
} // namespace gfx
@ -17,6 +19,8 @@ namespace chrome_pdf {
gfx::Rect RectFromPPRect(const PP_Rect& pp_rect);
gfx::Size SizeFromPPSize(const PP_Size& pp_size);
gfx::Point PointFromPPPoint(const PP_Point& pp_point);
PP_Point PPPointFromPoint(const gfx::Point& point);
} // namespace chrome_pdf

@ -5,6 +5,7 @@
#include "pdf/ppapi_migration/input_event_conversions.h"
#include "base/notreached.h"
#include "pdf/ppapi_migration/geometry_conversions.h"
#include "ppapi/cpp/input_event.h"
#include "ppapi/cpp/var.h"
@ -53,7 +54,21 @@ chrome_pdf::InputEventType GetEventType(const PP_InputEvent_Type& input_type) {
default:
NOTREACHED();
return chrome_pdf::InputEventType::kNone;
};
}
}
chrome_pdf::InputEventMouseButtonType GetInputEventMouseButtonType(
const PP_InputEvent_MouseButton& mouse_button_type) {
switch (mouse_button_type) {
case PP_INPUTEVENT_MOUSEBUTTON_LEFT:
return chrome_pdf::InputEventMouseButtonType::kLeft;
case PP_INPUTEVENT_MOUSEBUTTON_MIDDLE:
return chrome_pdf::InputEventMouseButtonType::kMiddle;
case PP_INPUTEVENT_MOUSEBUTTON_RIGHT:
return chrome_pdf::InputEventMouseButtonType::kRight;
default:
return chrome_pdf::InputEventMouseButtonType::kNone;
}
}
} // namespace
@ -79,10 +94,40 @@ KeyboardInputEvent& KeyboardInputEvent::operator=(
KeyboardInputEvent::~KeyboardInputEvent() = default;
MouseInputEvent::MouseInputEvent(InputEventType event_type,
double time_stamp,
uint32_t modifiers,
InputEventMouseButtonType mouse_button_type,
const gfx::Point& point,
int32_t click_count,
const gfx::Point& movement)
: event_type_(event_type),
time_stamp_(time_stamp),
modifiers_(modifiers),
mouse_button_type_(mouse_button_type),
point_(point),
click_count_(click_count),
movement_(movement) {}
MouseInputEvent::MouseInputEvent(const MouseInputEvent& other) = default;
MouseInputEvent& MouseInputEvent::operator=(const MouseInputEvent& other) =
default;
MouseInputEvent::~MouseInputEvent() = default;
KeyboardInputEvent GetKeyboardInputEvent(const pp::KeyboardInputEvent& event) {
return KeyboardInputEvent(GetEventType(event.GetType()), event.GetTimeStamp(),
event.GetModifiers(), event.GetKeyCode(),
event.GetCharacterText().AsString());
}
MouseInputEvent GetMouseInputEvent(const pp::MouseInputEvent& event) {
return MouseInputEvent(
GetEventType(event.GetType()), event.GetTimeStamp(), event.GetModifiers(),
GetInputEventMouseButtonType(event.GetButton()),
PointFromPPPoint(event.GetPosition()), event.GetClickCount(),
PointFromPPPoint(event.GetMovement()));
}
} // namespace chrome_pdf

@ -8,8 +8,11 @@
#include <stdint.h>
#include <string>
#include "ui/gfx/geometry/point.h"
namespace pp {
class KeyboardInputEvent;
class MouseInputEvent;
} // namespace pp
namespace chrome_pdf {
@ -99,6 +102,13 @@ enum class InputEventType {
kTouchCancel
};
enum class InputEventMouseButtonType {
kNone = 0,
kLeft,
kMiddle,
kRight,
};
class KeyboardInputEvent {
public:
KeyboardInputEvent(InputEventType event_type,
@ -130,8 +140,51 @@ class KeyboardInputEvent {
std::string key_char_;
};
class MouseInputEvent {
public:
MouseInputEvent(InputEventType event_type,
double time_stamp,
uint32_t modifier,
InputEventMouseButtonType mouse_button_type,
const gfx::Point& point,
int32_t click_count,
const gfx::Point& movement);
MouseInputEvent(const MouseInputEvent& other);
MouseInputEvent& operator=(const MouseInputEvent& other);
~MouseInputEvent();
const InputEventType& GetEventType() const { return event_type_; }
double GetTimeStamp() const { return time_stamp_; }
uint32_t GetModifiers() const { return modifiers_; }
const InputEventMouseButtonType& GetButton() const {
return mouse_button_type_;
}
const gfx::Point& GetPosition() const { return point_; }
int32_t GetClickCount() const { return click_count_; }
const gfx::Point& GetMovement() const { return movement_; }
private:
InputEventType event_type_ = InputEventType::kNone;
// The units are in seconds, but are not measured relative to any particular
// epoch, so the most you can do is compare two values.
double time_stamp_ = 0;
uint32_t modifiers_ = kInputEventModifierNone;
InputEventMouseButtonType mouse_button_type_;
gfx::Point point_;
int32_t click_count_ = 0;
gfx::Point movement_;
};
KeyboardInputEvent GetKeyboardInputEvent(const pp::KeyboardInputEvent& event);
MouseInputEvent GetMouseInputEvent(const pp::MouseInputEvent& event);
} // namespace chrome_pdf
#endif // PDF_PPAPI_MIGRATION_INPUT_EVENT_CONVERSIONS_H_