1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef CONTENT_COMMON_INPUT_SYNTHETIC_POINTER_ACTION_PARAMS_H_
6 #define CONTENT_COMMON_INPUT_SYNTHETIC_POINTER_ACTION_PARAMS_H_
7 
8 #include "base/check_op.h"
9 #include "content/common/content_export.h"
10 #include "content/common/content_param_traits_macros.h"
11 #include "content/common/input/synthetic_gesture_params.h"
12 #include "third_party/blink/public/common/input/web_mouse_event.h"
13 #include "third_party/blink/public/common/input/web_touch_event.h"
14 #include "ui/gfx/geometry/point_f.h"
15 
16 namespace ipc_fuzzer {
17 template <class T>
18 struct FuzzTraits;
19 }  // namespace ipc_fuzzer
20 
21 namespace content {
22 
23 // It contains all the parameters to create the synthetic events of touch,
24 // mouse and pen inputs in SyntheticPointerAction::ForwardInputEvents function.
25 struct CONTENT_EXPORT SyntheticPointerActionParams {
26  public:
27   // All the pointer actions that will be dispatched together will be grouped
28   // in an array.
29   enum class PointerActionType {
30     NOT_INITIALIZED,
31     PRESS,
32     MOVE,
33     RELEASE,
34     CANCEL,
35     LEAVE,
36     IDLE,
37     POINTER_ACTION_TYPE_MAX = IDLE
38   };
39 
40   enum class Button {
41     NO_BUTTON,
42     LEFT,
43     MIDDLE,
44     RIGHT,
45     BACK,
46     FORWARD,
47     BUTTON_MAX = FORWARD
48   };
49 
50   SyntheticPointerActionParams();
51   explicit SyntheticPointerActionParams(PointerActionType action_type);
52   SyntheticPointerActionParams(const SyntheticPointerActionParams& other);
53   ~SyntheticPointerActionParams();
54 
set_pointer_action_typeSyntheticPointerActionParams55   void set_pointer_action_type(PointerActionType pointer_action_type) {
56     pointer_action_type_ = pointer_action_type;
57   }
58 
set_pointer_idSyntheticPointerActionParams59   void set_pointer_id(uint32_t pointer_id) { pointer_id_ = pointer_id; }
60 
set_positionSyntheticPointerActionParams61   void set_position(const gfx::PointF& position) {
62     DCHECK(pointer_action_type_ == PointerActionType::PRESS ||
63            pointer_action_type_ == PointerActionType::MOVE);
64     position_ = position;
65   }
66 
set_buttonSyntheticPointerActionParams67   void set_button(Button button) {
68     DCHECK(pointer_action_type_ == PointerActionType::PRESS ||
69            pointer_action_type_ == PointerActionType::RELEASE ||
70            pointer_action_type_ == PointerActionType::CANCEL);
71     button_ = button;
72   }
73 
set_key_modifiersSyntheticPointerActionParams74   void set_key_modifiers(int key_modifiers) {
75     DCHECK_NE(PointerActionType::IDLE, pointer_action_type_);
76     key_modifiers_ = key_modifiers;
77   }
78 
set_widthSyntheticPointerActionParams79   void set_width(float width) {
80     DCHECK(pointer_action_type_ == PointerActionType::PRESS ||
81            pointer_action_type_ == PointerActionType::MOVE);
82     width_ = width;
83   }
84 
set_heightSyntheticPointerActionParams85   void set_height(float height) {
86     DCHECK(pointer_action_type_ == PointerActionType::PRESS ||
87            pointer_action_type_ == PointerActionType::MOVE);
88     height_ = height;
89   }
90 
set_rotation_angleSyntheticPointerActionParams91   void set_rotation_angle(float rotation_angle) {
92     DCHECK(pointer_action_type_ == PointerActionType::PRESS ||
93            pointer_action_type_ == PointerActionType::MOVE);
94     rotation_angle_ = rotation_angle;
95   }
96 
set_forceSyntheticPointerActionParams97   void set_force(float force) {
98     DCHECK(pointer_action_type_ == PointerActionType::PRESS ||
99            pointer_action_type_ == PointerActionType::MOVE);
100     force_ = force;
101   }
102 
set_timestampSyntheticPointerActionParams103   void set_timestamp(base::TimeTicks timestamp) { timestamp_ = timestamp; }
104 
set_durationSyntheticPointerActionParams105   void set_duration(base::TimeDelta duration) {
106     DCHECK_EQ(PointerActionType::IDLE, pointer_action_type_);
107     duration_ = duration;
108   }
109 
pointer_action_typeSyntheticPointerActionParams110   PointerActionType pointer_action_type() const { return pointer_action_type_; }
111 
pointer_idSyntheticPointerActionParams112   uint32_t pointer_id() const { return pointer_id_; }
113 
positionSyntheticPointerActionParams114   gfx::PointF position() const {
115     DCHECK(pointer_action_type_ == PointerActionType::PRESS ||
116            pointer_action_type_ == PointerActionType::MOVE);
117     return position_;
118   }
119 
buttonSyntheticPointerActionParams120   Button button() const {
121     DCHECK(pointer_action_type_ == PointerActionType::PRESS ||
122            pointer_action_type_ == PointerActionType::RELEASE ||
123            pointer_action_type_ == PointerActionType::CANCEL);
124     return button_;
125   }
126 
key_modifiersSyntheticPointerActionParams127   int key_modifiers() const {
128     DCHECK_NE(PointerActionType::IDLE, pointer_action_type_);
129     return key_modifiers_;
130   }
131 
widthSyntheticPointerActionParams132   float width() const {
133     DCHECK(pointer_action_type_ == PointerActionType::PRESS ||
134            pointer_action_type_ == PointerActionType::MOVE);
135     return width_;
136   }
137 
heightSyntheticPointerActionParams138   float height() const {
139     DCHECK(pointer_action_type_ == PointerActionType::PRESS ||
140            pointer_action_type_ == PointerActionType::MOVE);
141     return height_;
142   }
143 
rotation_angleSyntheticPointerActionParams144   float rotation_angle() const {
145     DCHECK(pointer_action_type_ == PointerActionType::PRESS ||
146            pointer_action_type_ == PointerActionType::MOVE);
147     return rotation_angle_;
148   }
149 
forceSyntheticPointerActionParams150   float force() const {
151     DCHECK(pointer_action_type_ == PointerActionType::PRESS ||
152            pointer_action_type_ == PointerActionType::MOVE);
153     return force_;
154   }
155 
timestampSyntheticPointerActionParams156   base::TimeTicks timestamp() const { return timestamp_; }
157 
durationSyntheticPointerActionParams158   base::TimeDelta duration() const {
159     DCHECK_EQ(PointerActionType::IDLE, pointer_action_type_);
160     return duration_;
161   }
162 
163   static unsigned GetWebMouseEventModifier(
164       SyntheticPointerActionParams::Button button);
165   static blink::WebMouseEvent::Button GetWebMouseEventButton(
166       SyntheticPointerActionParams::Button button);
167   static blink::WebMouseEvent::Button GetWebMouseEventButtonFromModifier(
168       unsigned modifiers);
169 
170  private:
171   friend struct IPC::ParamTraits<content::SyntheticPointerActionParams>;
172   friend struct ipc_fuzzer::FuzzTraits<content::SyntheticPointerActionParams>;
173 
174   PointerActionType pointer_action_type_ = PointerActionType::NOT_INITIALIZED;
175   // The position of the pointer, where it presses or moves to.
176   gfx::PointF position_;
177   // The id of the pointer given by the users.
178   uint32_t pointer_id_ = 0;
179   Button button_ = Button::LEFT;
180   // “Alt“, ”Control“, ”Meta“, ”Shift“, ”CapsLock“, ”NumLock“, ”AltGraph”
181   // buttons are supported right now. It stores a matching modifiers defined
182   // in WebInputEvent class.
183   int key_modifiers_ = 0;
184   float width_ = 40.f;
185   float height_ = 40.f;
186   float rotation_angle_ = 0.f;
187   float force_ = 1.f;
188   base::TimeTicks timestamp_;
189   // The duration of the pause action is in milliseconds.
190   base::TimeDelta duration_;
191 };
192 
193 }  // namespace content
194 
195 #endif  // CONTENT_COMMON_INPUT_SYNTHETIC_POINTER_ACTION_PARAMS_H_
196