OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2013 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 #include "content/browser/renderer_host/synthetic_touch_event_android.h" | |
6 | |
7 #include "content/browser/android/content_view_core_impl.h" | |
8 #include "jni/SyntheticTouchEvent_jni.h" | |
9 #include "third_party/WebKit/public/web/WebInputEvent.h" | |
10 | |
11 namespace { | |
12 bool g_jni_initialized = false; | |
13 | |
14 void RegisterNativesIfNeeded(JNIEnv* env) { | |
15 if (!g_jni_initialized) { | |
16 content::RegisterNativesImpl(env); | |
17 g_jni_initialized = true; | |
18 } | |
19 } | |
20 } // namespace | |
21 | |
22 namespace content { | |
23 | |
24 SyntheticTouchEventAndroid::SyntheticTouchEventAndroid( | |
25 ContentViewCoreImpl* content_view_core) | |
26 : env_(base::android::AttachCurrentThread()), | |
27 java_touch_event_(content_view_core->CreateSyntheticTouchEvent()), | |
28 savedDownTime_(0) { | |
29 CHECK(! java_touch_event_.is_null()); | |
30 RegisterNativesIfNeeded(env_); | |
31 } | |
32 | |
33 SyntheticTouchEventAndroid::~SyntheticTouchEventAndroid() { | |
34 } | |
35 | |
36 void SyntheticTouchEventAndroid::reset() { | |
37 Java_SyntheticTouchEvent_reset(env_, java_touch_event_.obj()); | |
38 } | |
39 | |
40 long SyntheticTouchEventAndroid::GetCurrentTime() { | |
41 return Java_SyntheticTouchEvent_getCurrentTime(env_); | |
42 } | |
43 | |
44 void SyntheticTouchEventAndroid::SetDownTime(long downTime) { | |
45 Java_SyntheticTouchEvent_setDownTime(env_, java_touch_event_.obj(), | |
46 downTime); | |
47 } | |
48 | |
49 void SyntheticTouchEventAndroid::AddPointer(int x, int y, int id) { | |
50 Java_SyntheticTouchEvent_addPointer(env_, java_touch_event_.obj(), x, y, id); | |
51 } | |
52 | |
53 void SyntheticTouchEventAndroid::Inject(Action action) { | |
54 Java_SyntheticTouchEvent_inject(env_, java_touch_event_.obj(), | |
55 static_cast<int>(action)); | |
56 } | |
57 | |
58 void SyntheticTouchEventAndroid::InjectWebTouchEvent( | |
59 const WebKit::WebTouchEvent* web_touch) { | |
60 reset(); | |
Dominik Grewe
2013/10/10 14:01:45
If I understand correctly you're trying to make th
kouhei (in TOK)
2013/10/15 02:10:02
I think the cost of reset() here is negligible, as
Dominik Grewe
2013/10/15 09:10:55
You would also save the call to SetDownTime() on e
jdduke (slow)
2013/10/16 15:05:25
While JNI calls are "slow", that is only in the re
kouhei (in TOK)
2013/10/17 05:44:13
Thanks for your comments! I think I can go with th
| |
61 | |
62 SyntheticTouchEventAndroid::Action action = | |
63 SyntheticTouchEventAndroid::ActionInvalid; | |
64 switch (web_touch->type) { | |
65 case WebKit::WebInputEvent::TouchStart: | |
66 action = SyntheticTouchEventAndroid::ActionStart; | |
67 savedDownTime_ = GetCurrentTime(); | |
68 break; | |
69 case WebKit::WebInputEvent::TouchMove: | |
70 action = SyntheticTouchEventAndroid::ActionMove; | |
71 break; | |
72 case WebKit::WebInputEvent::TouchCancel: | |
73 action = SyntheticTouchEventAndroid::ActionCancel; | |
74 break; | |
75 case WebKit::WebInputEvent::TouchEnd: | |
76 action = SyntheticTouchEventAndroid::ActionEnd; | |
77 break; | |
78 default: | |
79 NOTREACHED(); | |
80 } | |
81 DCHECK(savedDownTime_ > 0); | |
82 SetDownTime(savedDownTime_); | |
83 | |
84 const unsigned num_touches = web_touch->touchesLength; | |
85 for (unsigned i = 0; i < num_touches; ++ i) { | |
86 const WebKit::WebTouchPoint* point = &web_touch->touches[i]; | |
87 AddPointer(point->position.x, point->position.y, point->id); | |
88 } | |
89 | |
90 Inject(action); | |
91 | |
92 if (web_touch->type == WebKit::WebInputEvent::TouchCancel || | |
93 web_touch->type == WebKit::WebInputEvent::TouchEnd) | |
94 savedDownTime_ = 0; | |
95 } | |
96 | |
97 } // namespace content | |
OLD | NEW |