1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4  * License, v. 2.0. If a copy of the MPL was not distributed with this
5  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
6  */
7 
8 /* Description of TouchManager class.
9  * Incapsulate code related with work of touch events.
10  */
11 
12 #ifndef TouchManager_h_
13 #define TouchManager_h_
14 
15 #include "mozilla/BasicEvents.h"
16 #include "mozilla/dom/Touch.h"
17 #include "mozilla/TouchEvents.h"
18 #include "nsRefPtrHashtable.h"
19 
20 namespace mozilla {
21 class PresShell;
22 
23 class TouchManager {
24  public:
25   // Initialize and release static variables
26   static void InitializeStatics();
27   static void ReleaseStatics();
28 
29   void Init(PresShell* aPresShell, dom::Document* aDocument);
30   void Destroy();
31 
32   // Perform hit test and setup the event targets for touchstart. Other touch
33   // events are dispatched to the same target as touchstart.
34   static nsIFrame* SetupTarget(WidgetTouchEvent* aEvent, nsIFrame* aFrame);
35 
36   /**
37    * This function checks whether all touch points hit elements in the same
38    * document. If not, we try to find its cross document parent which is in the
39    * same document of the existing target as the event target. We mark the
40    * touch point as suppressed if can't find it. The suppressed touch points are
41    * removed in TouchManager::PreHandleEvent so that we don't dispatch them to
42    * content.
43    *
44    * @param aEvent    A touch event to be checked.
45    *
46    * @return          The targeted frame of aEvent.
47    */
48   static nsIFrame* SuppressInvalidPointsAndGetTargetedFrame(
49       WidgetTouchEvent* aEvent);
50 
51   bool PreHandleEvent(mozilla::WidgetEvent* aEvent, nsEventStatus* aStatus,
52                       bool& aTouchIsNew,
53                       nsCOMPtr<nsIContent>& aCurrentEventContent);
54 
55   static already_AddRefed<nsIContent> GetAnyCapturedTouchTarget();
56   static bool HasCapturedTouch(int32_t aId);
57   static already_AddRefed<dom::Touch> GetCapturedTouch(int32_t aId);
58   static bool ShouldConvertTouchToPointer(const dom::Touch* aTouch,
59                                           const WidgetTouchEvent* aEvent);
60 
61  private:
62   void EvictTouches();
63   static void EvictTouchPoint(RefPtr<dom::Touch>& aTouch,
64                               dom::Document* aLimitToDocument = nullptr);
65   static void AppendToTouchList(WidgetTouchEvent::TouchArrayBase* aTouchList);
66 
67   RefPtr<PresShell> mPresShell;
68   RefPtr<dom::Document> mDocument;
69 
70   struct TouchInfo {
71     RefPtr<mozilla::dom::Touch> mTouch;
72     nsCOMPtr<nsIContent> mNonAnonymousTarget;
73     bool mConvertToPointer;
74   };
75   static nsDataHashtable<nsUint32HashKey, TouchInfo>* sCaptureTouchList;
76 };
77 
78 }  // namespace mozilla
79 
80 #endif /* !defined(TouchManager_h_) */
81