1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
|
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "content/browser/renderer_host/input/synthetic_tap_gesture.h"
#include "base/logging.h"
#include "third_party/WebKit/public/web/WebInputEvent.h"
#include "ui/events/latency_info.h"
namespace content {
SyntheticTapGesture::SyntheticTapGesture(
const SyntheticTapGestureParams& params)
: params_(params),
gesture_source_type_(SyntheticGestureParams::DEFAULT_INPUT),
state_(SETUP) {
DCHECK_GE(params_.duration_ms, 0);
}
SyntheticTapGesture::~SyntheticTapGesture() {}
SyntheticGesture::Result SyntheticTapGesture::ForwardInputEvents(
const base::TimeTicks& timestamp, SyntheticGestureTarget* target) {
if (state_ == SETUP) {
gesture_source_type_ = params_.gesture_source_type;
if (gesture_source_type_ == SyntheticGestureParams::DEFAULT_INPUT)
gesture_source_type_ = target->GetDefaultSyntheticGestureSourceType();
state_ = PRESS;
}
DCHECK_NE(gesture_source_type_, SyntheticGestureParams::DEFAULT_INPUT);
if (gesture_source_type_ == SyntheticGestureParams::TOUCH_INPUT ||
gesture_source_type_ == SyntheticGestureParams::MOUSE_INPUT)
ForwardTouchOrMouseInputEvents(timestamp, target);
else
return SyntheticGesture::GESTURE_SOURCE_TYPE_NOT_IMPLEMENTED;
return (state_ == DONE) ? SyntheticGesture::GESTURE_FINISHED
: SyntheticGesture::GESTURE_RUNNING;
}
void SyntheticTapGesture::ForwardTouchOrMouseInputEvents(
const base::TimeTicks& timestamp, SyntheticGestureTarget* target) {
switch (state_) {
case PRESS:
Press(target, timestamp);
// Release immediately if duration is 0.
if (params_.duration_ms == 0) {
Release(target, timestamp);
state_ = DONE;
} else {
start_time_ = timestamp;
state_ = WAITING_TO_RELEASE;
}
break;
case WAITING_TO_RELEASE:
if (timestamp - start_time_ >= GetDuration()) {
Release(target, start_time_ + GetDuration());
state_ = DONE;
}
break;
case SETUP:
NOTREACHED() << "State SETUP invalid for synthetic tap gesture.";
case DONE:
NOTREACHED() << "State DONE invalid for synthetic tap gesture.";
}
}
void SyntheticTapGesture::Press(SyntheticGestureTarget* target,
const base::TimeTicks& timestamp) {
if (gesture_source_type_ == SyntheticGestureParams::TOUCH_INPUT) {
touch_event_.PressPoint(params_.position.x(), params_.position.y());
touch_event_.timeStampSeconds = ConvertTimestampToSeconds(timestamp);
target->DispatchInputEventToPlatform(touch_event_);
} else if (gesture_source_type_ == SyntheticGestureParams::MOUSE_INPUT) {
blink::WebMouseEvent mouse_event =
SyntheticWebMouseEventBuilder::Build(blink::WebInputEvent::MouseDown,
params_.position.x(),
params_.position.y(),
0);
mouse_event.clickCount = 1;
mouse_event.timeStampSeconds = ConvertTimestampToSeconds(timestamp);
target->DispatchInputEventToPlatform(mouse_event);
} else {
NOTREACHED() << "Invalid gesture source type for synthetic tap gesture.";
}
}
void SyntheticTapGesture::Release(SyntheticGestureTarget* target,
const base::TimeTicks& timestamp) {
if (gesture_source_type_ == SyntheticGestureParams::TOUCH_INPUT) {
touch_event_.ReleasePoint(0);
touch_event_.timeStampSeconds = ConvertTimestampToSeconds(timestamp);
target->DispatchInputEventToPlatform(touch_event_);
} else if (gesture_source_type_ == SyntheticGestureParams::MOUSE_INPUT) {
blink::WebMouseEvent mouse_event =
SyntheticWebMouseEventBuilder::Build(blink::WebInputEvent::MouseUp,
params_.position.x(),
params_.position.y(),
0);
mouse_event.clickCount = 1;
mouse_event.timeStampSeconds = ConvertTimestampToSeconds(timestamp);
target->DispatchInputEventToPlatform(mouse_event);
} else {
NOTREACHED() << "Invalid gesture source type for synthetic tap gesture.";
}
}
base::TimeDelta SyntheticTapGesture::GetDuration() const {
return base::TimeDelta::FromMilliseconds(params_.duration_ms);
}
} // namespace content
|