Update touch handler in preparation for upstreaming of m_originatingTouchTargets map. WebKit bug tracking is https://bugs.webkit.org/show_bug.cgi?id=34585

Landed to webkit as http://trac.webkit.org/changeset/55230

Change-Id: I68fa02615aec35c8c2d3e7c69e8479c33d66293c
diff --git a/WebCore/page/EventHandler.cpp b/WebCore/page/EventHandler.cpp
index 9a3b1ec..226e3e5 100644
--- a/WebCore/page/EventHandler.cpp
+++ b/WebCore/page/EventHandler.cpp
@@ -2596,20 +2596,23 @@
         int adjustedPageX = lroundf(pagePoint.x() / m_frame->pageZoomFactor());
         int adjustedPageY = lroundf(pagePoint.y() / m_frame->pageZoomFactor());
 
-        // ANDROID
-        // The touch event should act on the originating touch target, not the current target
-        // TODO: Upstream this fix to webkit.org (see webkit bug 34585)
-        int touchPointId = point.id();
+        // Increment the platform touch id by 1 to avoid storing a key of 0 in the hashmap.
+        unsigned touchPointTargetKey = point.id() + 1;
         EventTarget* touchTarget = 0;
         if (point.state() == PlatformTouchPoint::TouchPressed) {
-            m_originatingTouchPointTargets.set(touchPointId, target);
+            m_originatingTouchPointTargets.set(touchPointTargetKey, target);
             touchTarget = target;
+        } else if (point.state() == PlatformTouchPoint::TouchReleased || point.state() == PlatformTouchPoint::TouchCancelled) {
+            // The target should be the original target for this touch, so get it from the hashmap. As it's a release or cancel
+            // we also remove it from the map.
+            touchTarget = m_originatingTouchPointTargets.take(touchPointTargetKey).get();
         } else
-            touchTarget = m_originatingTouchPointTargets.get(touchPointId).get();
+            touchTarget = m_originatingTouchPointTargets.get(touchPointTargetKey).get();
 
-        ASSERT(touchTarget);
+        if (!touchTarget)
+            continue;
 
-        RefPtr<Touch> touch = Touch::create(doc->frame(), touchTarget, touchPointId,
+        RefPtr<Touch> touch = Touch::create(doc->frame(), touchTarget, point.id(),
                                             point.screenPos().x(), point.screenPos().y(),
                                             adjustedPageX, adjustedPageY);
 
diff --git a/WebCore/page/EventHandler.h b/WebCore/page/EventHandler.h
index 759be2b..1d374e6 100644
--- a/WebCore/page/EventHandler.h
+++ b/WebCore/page/EventHandler.h
@@ -37,7 +37,7 @@
 class NSView;
 #endif
 
-#if ENABLE(TOUCH_EVENTS) // ANDROID addition, needs to be upstreamed. (see webkit bug 34585)
+#if ENABLE(TOUCH_EVENTS)
 #include <wtf/HashMap.h>
 #endif
 
@@ -428,7 +428,6 @@
     int m_activationEventNumber;
 #endif
 #if ENABLE(TOUCH_EVENTS)
-    // ANDROID fix to be upstreamed, see webkit bug 34585.
     typedef HashMap<int, RefPtr<EventTarget> > TouchTargetMap;
     TouchTargetMap m_originatingTouchPointTargets;
     RefPtr<Node> m_touchEventTarget;
diff --git a/WebCore/platform/PlatformTouchPoint.h b/WebCore/platform/PlatformTouchPoint.h
index 2df9e31..d4f855e 100644
--- a/WebCore/platform/PlatformTouchPoint.h
+++ b/WebCore/platform/PlatformTouchPoint.h
@@ -50,13 +50,13 @@
     PlatformTouchPoint(const IntPoint& windowPos, State);
 #endif
 
-    int id() const { return m_id; }
+    unsigned id() const { return m_id; }
     State state() const { return m_state; }
     IntPoint screenPos() const { return m_screenPos; }
     IntPoint pos() const { return m_pos; }
     
 private:
-    int m_id;
+    unsigned m_id;
     State m_state;
     IntPoint m_screenPos;
     IntPoint m_pos;
diff --git a/WebCore/platform/qt/PlatformTouchPointQt.cpp b/WebCore/platform/qt/PlatformTouchPointQt.cpp
index 1788cef..c293212 100644
--- a/WebCore/platform/qt/PlatformTouchPointQt.cpp
+++ b/WebCore/platform/qt/PlatformTouchPointQt.cpp
@@ -29,7 +29,8 @@
 
 PlatformTouchPoint::PlatformTouchPoint(const QTouchEvent::TouchPoint& point)
 {
-    m_id = point.id();
+    // The QTouchEvent::TouchPoint API states that ids will be >= 0.
+    m_id = static_cast<unsigned>(point.id());
     switch (point.state()) {
     case Qt::TouchPointReleased: m_state = TouchReleased; break;
     case Qt::TouchPointMoved: m_state = TouchMoved; break;