Stop retring GCM unregistration requests after reaching maximum number
[chromium-blink-merge.git] / ppapi / shared_impl / ppb_input_event_shared.cc
blobef4e719ce35885ec5996ffebf728d76237537c3f
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
3 // found in the LICENSE file.
5 #include "ppapi/shared_impl/ppb_input_event_shared.h"
7 #include "ppapi/shared_impl/ppapi_globals.h"
8 #include "ppapi/shared_impl/var.h"
10 using ppapi::thunk::PPB_InputEvent_API;
12 namespace ppapi {
14 InputEventData::InputEventData()
15 : is_filtered(false),
16 event_type(PP_INPUTEVENT_TYPE_UNDEFINED),
17 event_time_stamp(0.0),
18 event_modifiers(0),
19 mouse_button(PP_INPUTEVENT_MOUSEBUTTON_NONE),
20 mouse_position(PP_MakePoint(0, 0)),
21 mouse_click_count(0),
22 mouse_movement(PP_MakePoint(0, 0)),
23 wheel_delta(PP_MakeFloatPoint(0.0f, 0.0f)),
24 wheel_ticks(PP_MakeFloatPoint(0.0f, 0.0f)),
25 wheel_scroll_by_page(false),
26 key_code(0),
27 code(),
28 character_text(),
29 composition_target_segment(-1),
30 composition_selection_start(0),
31 composition_selection_end(0),
32 touches(),
33 changed_touches(),
34 target_touches() {}
36 InputEventData::~InputEventData() {}
38 PPB_InputEvent_Shared::PPB_InputEvent_Shared(ResourceObjectType type,
39 PP_Instance instance,
40 const InputEventData& data)
41 : Resource(type, instance), data_(data) {}
43 PPB_InputEvent_API* PPB_InputEvent_Shared::AsPPB_InputEvent_API() {
44 return this;
47 const InputEventData& PPB_InputEvent_Shared::GetInputEventData() const {
48 return data_;
51 PP_InputEvent_Type PPB_InputEvent_Shared::GetType() { return data_.event_type; }
53 PP_TimeTicks PPB_InputEvent_Shared::GetTimeStamp() {
54 return data_.event_time_stamp;
57 uint32_t PPB_InputEvent_Shared::GetModifiers() { return data_.event_modifiers; }
59 PP_InputEvent_MouseButton PPB_InputEvent_Shared::GetMouseButton() {
60 return data_.mouse_button;
63 PP_Point PPB_InputEvent_Shared::GetMousePosition() {
64 return data_.mouse_position;
67 int32_t PPB_InputEvent_Shared::GetMouseClickCount() {
68 return data_.mouse_click_count;
71 PP_Point PPB_InputEvent_Shared::GetMouseMovement() {
72 return data_.mouse_movement;
75 PP_FloatPoint PPB_InputEvent_Shared::GetWheelDelta() {
76 return data_.wheel_delta;
79 PP_FloatPoint PPB_InputEvent_Shared::GetWheelTicks() {
80 return data_.wheel_ticks;
83 PP_Bool PPB_InputEvent_Shared::GetWheelScrollByPage() {
84 return PP_FromBool(data_.wheel_scroll_by_page);
87 uint32_t PPB_InputEvent_Shared::GetKeyCode() { return data_.key_code; }
89 PP_Var PPB_InputEvent_Shared::GetCharacterText() {
90 return StringVar::StringToPPVar(data_.character_text);
93 PP_Var PPB_InputEvent_Shared::GetCode() {
94 return StringVar::StringToPPVar(data_.code);
97 uint32_t PPB_InputEvent_Shared::GetIMESegmentNumber() {
98 if (data_.composition_segment_offsets.empty())
99 return 0;
100 return static_cast<uint32_t>(data_.composition_segment_offsets.size() - 1);
103 uint32_t PPB_InputEvent_Shared::GetIMESegmentOffset(uint32_t index) {
104 if (index >= data_.composition_segment_offsets.size())
105 return 0;
106 return data_.composition_segment_offsets[index];
109 int32_t PPB_InputEvent_Shared::GetIMETargetSegment() {
110 return data_.composition_target_segment;
113 void PPB_InputEvent_Shared::GetIMESelection(uint32_t* start, uint32_t* end) {
114 if (start)
115 *start = data_.composition_selection_start;
116 if (end)
117 *end = data_.composition_selection_end;
120 void PPB_InputEvent_Shared::AddTouchPoint(PP_TouchListType list,
121 const PP_TouchPoint& point) {
122 switch (list) {
123 case PP_TOUCHLIST_TYPE_TOUCHES:
124 data_.touches.push_back(point);
125 break;
126 case PP_TOUCHLIST_TYPE_CHANGEDTOUCHES:
127 data_.changed_touches.push_back(point);
128 break;
129 case PP_TOUCHLIST_TYPE_TARGETTOUCHES:
130 data_.target_touches.push_back(point);
131 break;
132 default:
133 break;
137 uint32_t PPB_InputEvent_Shared::GetTouchCount(PP_TouchListType list) {
138 switch (list) {
139 case PP_TOUCHLIST_TYPE_TOUCHES:
140 return static_cast<uint32_t>(data_.touches.size());
141 case PP_TOUCHLIST_TYPE_CHANGEDTOUCHES:
142 return static_cast<uint32_t>(data_.changed_touches.size());
143 case PP_TOUCHLIST_TYPE_TARGETTOUCHES:
144 return static_cast<uint32_t>(data_.target_touches.size());
147 return 0;
150 PP_TouchPoint PPB_InputEvent_Shared::GetTouchByIndex(PP_TouchListType list,
151 uint32_t index) {
152 std::vector<PP_TouchPoint>* points;
153 switch (list) {
154 case PP_TOUCHLIST_TYPE_TOUCHES:
155 points = &data_.touches;
156 break;
157 case PP_TOUCHLIST_TYPE_CHANGEDTOUCHES:
158 points = &data_.changed_touches;
159 break;
160 case PP_TOUCHLIST_TYPE_TARGETTOUCHES:
161 points = &data_.target_touches;
162 break;
163 default:
164 return PP_MakeTouchPoint();
166 if (index >= points->size()) {
167 return PP_MakeTouchPoint();
169 return points->at(index);
172 PP_TouchPoint PPB_InputEvent_Shared::GetTouchById(PP_TouchListType list,
173 uint32_t id) {
174 const std::vector<PP_TouchPoint>* points;
175 switch (list) {
176 case PP_TOUCHLIST_TYPE_TOUCHES:
177 points = &data_.touches;
178 break;
179 case PP_TOUCHLIST_TYPE_CHANGEDTOUCHES:
180 points = &data_.changed_touches;
181 break;
182 case PP_TOUCHLIST_TYPE_TARGETTOUCHES:
183 points = &data_.target_touches;
184 break;
185 default:
186 return PP_MakeTouchPoint();
188 for (size_t i = 0; i < points->size(); i++) {
189 if (points->at(i).id == id)
190 return points->at(i);
192 return PP_MakeTouchPoint();
195 PP_Bool PPB_InputEvent_Shared::TraceInputLatency(PP_Bool has_damage) {
196 ui::LatencyInfo latency = data_.latency_info;
197 if (!latency.FindLatency(ui::INPUT_EVENT_LATENCY_BEGIN_PLUGIN_COMPONENT,
198 0, NULL))
199 return PP_FALSE;
200 if (has_damage) {
201 PpapiGlobals::Get()->AddLatencyInfo(latency, pp_instance());
202 } else {
203 latency.AddLatencyNumber(
204 ui::INPUT_EVENT_LATENCY_TERMINATED_PLUGIN_COMPONENT, 0, 0);
206 return PP_TRUE;
209 // static
210 PP_Resource PPB_InputEvent_Shared::CreateIMEInputEvent(
211 ResourceObjectType type,
212 PP_Instance instance,
213 PP_InputEvent_Type event_type,
214 PP_TimeTicks time_stamp,
215 struct PP_Var text,
216 uint32_t segment_number,
217 const uint32_t* segment_offsets,
218 int32_t target_segment,
219 uint32_t selection_start,
220 uint32_t selection_end) {
221 if (event_type != PP_INPUTEVENT_TYPE_IME_COMPOSITION_START &&
222 event_type != PP_INPUTEVENT_TYPE_IME_COMPOSITION_UPDATE &&
223 event_type != PP_INPUTEVENT_TYPE_IME_COMPOSITION_END &&
224 event_type != PP_INPUTEVENT_TYPE_IME_TEXT)
225 return 0;
227 InputEventData data;
228 data.event_type = event_type;
229 data.event_time_stamp = time_stamp;
230 if (text.type == PP_VARTYPE_STRING) {
231 StringVar* text_str = StringVar::FromPPVar(text);
232 if (!text_str)
233 return 0;
234 data.character_text = text_str->value();
236 data.composition_target_segment = target_segment;
237 if (segment_number != 0) {
238 data.composition_segment_offsets.assign(
239 &segment_offsets[0], &segment_offsets[segment_number + 1]);
241 data.composition_selection_start = selection_start;
242 data.composition_selection_end = selection_end;
244 return (new PPB_InputEvent_Shared(type, instance, data))->GetReference();
247 // static
248 PP_Resource PPB_InputEvent_Shared::CreateKeyboardInputEvent(
249 ResourceObjectType type,
250 PP_Instance instance,
251 PP_InputEvent_Type event_type,
252 PP_TimeTicks time_stamp,
253 uint32_t modifiers,
254 uint32_t key_code,
255 struct PP_Var character_text,
256 struct PP_Var code) {
257 if (event_type != PP_INPUTEVENT_TYPE_RAWKEYDOWN &&
258 event_type != PP_INPUTEVENT_TYPE_KEYDOWN &&
259 event_type != PP_INPUTEVENT_TYPE_KEYUP &&
260 event_type != PP_INPUTEVENT_TYPE_CHAR)
261 return 0;
263 InputEventData data;
264 data.event_type = event_type;
265 data.event_time_stamp = time_stamp;
266 data.event_modifiers = modifiers;
267 data.key_code = key_code;
268 if (character_text.type == PP_VARTYPE_STRING) {
269 StringVar* text_str = StringVar::FromPPVar(character_text);
270 if (!text_str)
271 return 0;
272 data.character_text = text_str->value();
274 if (code.type == PP_VARTYPE_STRING) {
275 StringVar* code_str = StringVar::FromPPVar(code);
276 if (!code_str)
277 return 0;
278 data.code = code_str->value();
281 return (new PPB_InputEvent_Shared(type, instance, data))->GetReference();
284 // static
285 PP_Resource PPB_InputEvent_Shared::CreateMouseInputEvent(
286 ResourceObjectType type,
287 PP_Instance instance,
288 PP_InputEvent_Type event_type,
289 PP_TimeTicks time_stamp,
290 uint32_t modifiers,
291 PP_InputEvent_MouseButton mouse_button,
292 const PP_Point* mouse_position,
293 int32_t click_count,
294 const PP_Point* mouse_movement) {
295 if (event_type != PP_INPUTEVENT_TYPE_MOUSEDOWN &&
296 event_type != PP_INPUTEVENT_TYPE_MOUSEUP &&
297 event_type != PP_INPUTEVENT_TYPE_MOUSEMOVE &&
298 event_type != PP_INPUTEVENT_TYPE_MOUSEENTER &&
299 event_type != PP_INPUTEVENT_TYPE_MOUSELEAVE)
300 return 0;
302 InputEventData data;
303 data.event_type = event_type;
304 data.event_time_stamp = time_stamp;
305 data.event_modifiers = modifiers;
306 data.mouse_button = mouse_button;
307 data.mouse_position = *mouse_position;
308 data.mouse_click_count = click_count;
309 data.mouse_movement = *mouse_movement;
311 return (new PPB_InputEvent_Shared(type, instance, data))->GetReference();
314 // static
315 PP_Resource PPB_InputEvent_Shared::CreateWheelInputEvent(
316 ResourceObjectType type,
317 PP_Instance instance,
318 PP_TimeTicks time_stamp,
319 uint32_t modifiers,
320 const PP_FloatPoint* wheel_delta,
321 const PP_FloatPoint* wheel_ticks,
322 PP_Bool scroll_by_page) {
323 InputEventData data;
324 data.event_type = PP_INPUTEVENT_TYPE_WHEEL;
325 data.event_time_stamp = time_stamp;
326 data.event_modifiers = modifiers;
327 data.wheel_delta = *wheel_delta;
328 data.wheel_ticks = *wheel_ticks;
329 data.wheel_scroll_by_page = PP_ToBool(scroll_by_page);
331 return (new PPB_InputEvent_Shared(type, instance, data))->GetReference();
334 // static
335 PP_Resource PPB_InputEvent_Shared::CreateTouchInputEvent(
336 ResourceObjectType type,
337 PP_Instance instance,
338 PP_InputEvent_Type event_type,
339 PP_TimeTicks time_stamp,
340 uint32_t modifiers) {
341 if (event_type != PP_INPUTEVENT_TYPE_TOUCHSTART &&
342 event_type != PP_INPUTEVENT_TYPE_TOUCHMOVE &&
343 event_type != PP_INPUTEVENT_TYPE_TOUCHEND &&
344 event_type != PP_INPUTEVENT_TYPE_TOUCHCANCEL)
345 return 0;
347 InputEventData data;
348 data.event_type = event_type;
349 data.event_time_stamp = time_stamp;
350 data.event_modifiers = modifiers;
351 return (new PPB_InputEvent_Shared(type, instance, data))->GetReference();
354 } // namespace ppapi