Bug 984271 part.1-4 Clean up WheelHandlingHelper.cpp r=smaug
authorMasayuki Nakano <masayuki@d-toybox.com>
Tue, 01 Apr 2014 13:09:22 +0900
changeset 176462 df1a290b1c22a6bae9bdb636322d0c235de9308a
parent 176461 77372b1a1ba6365f76fdeb8cc48c272dd4f62522
child 176463 77b2c81993b5b00b90ce4359b50bf3cd418552e1
push id26524
push userryanvm@gmail.com
push dateTue, 01 Apr 2014 20:44:18 +0000
treeherdermozilla-central@0ff6afce0133 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerssmaug
bugs984271
milestone31.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 984271 part.1-4 Clean up WheelHandlingHelper.cpp r=smaug
dom/events/WheelHandlingHelper.cpp
dom/events/WheelHandlingHelper.h
--- a/dom/events/WheelHandlingHelper.cpp
+++ b/dom/events/WheelHandlingHelper.cpp
@@ -71,102 +71,105 @@ WheelHandlingUtils::CanScrollOn(nsIScrol
 
 nsWeakFrame WheelTransaction::sTargetFrame(nullptr);
 uint32_t WheelTransaction::sTime = 0;
 uint32_t WheelTransaction::sMouseMoved = 0;
 nsITimer* WheelTransaction::sTimer = nullptr;
 int32_t WheelTransaction::sScrollSeriesCounter = 0;
 bool WheelTransaction::sOwnScrollbars = false;
 
-bool
+/* static */ bool
 WheelTransaction::OutOfTime(uint32_t aBaseTime, uint32_t aThreshold)
 {
   uint32_t now = PR_IntervalToMilliseconds(PR_IntervalNow());
   return (now - aBaseTime > aThreshold);
 }
 
-void
+/* static */ void
 WheelTransaction::OwnScrollbars(bool aOwn)
 {
   sOwnScrollbars = aOwn;
 }
 
-void
+/* static */ void
 WheelTransaction::BeginTransaction(nsIFrame* aTargetFrame,
                                    WidgetWheelEvent* aEvent)
 {
   NS_ASSERTION(!sTargetFrame, "previous transaction is not finished!");
   MOZ_ASSERT(aEvent->message == NS_WHEEL_WHEEL,
              "Transaction must be started with a wheel event");
   ScrollbarsForWheel::OwnWheelTransaction(false);
   sTargetFrame = aTargetFrame;
   sScrollSeriesCounter = 0;
   if (!UpdateTransaction(aEvent)) {
     NS_ERROR("BeginTransaction is called even cannot scroll the frame");
     EndTransaction();
   }
 }
 
-bool
+/* static */ bool
 WheelTransaction::UpdateTransaction(WidgetWheelEvent* aEvent)
 {
   nsIScrollableFrame* sf = GetTargetFrame()->GetScrollTargetFrame();
   NS_ENSURE_TRUE(sf, false);
 
   if (!WheelHandlingUtils::CanScrollOn(sf, aEvent->deltaX, aEvent->deltaY)) {
     OnFailToScrollTarget();
     // We should not modify the transaction state when the view will not be
     // scrolled actually.
     return false;
   }
 
   SetTimeout();
 
-  if (sScrollSeriesCounter != 0 && OutOfTime(sTime, kScrollSeriesTimeout))
+  if (sScrollSeriesCounter != 0 && OutOfTime(sTime, kScrollSeriesTimeout)) {
     sScrollSeriesCounter = 0;
+  }
   sScrollSeriesCounter++;
 
   // We should use current time instead of WidgetEvent.time.
   // 1. Some events doesn't have the correct creation time.
   // 2. If the computer runs slowly by other processes eating the CPU resource,
   //    the event creation time doesn't keep real time.
   sTime = PR_IntervalToMilliseconds(PR_IntervalNow());
   sMouseMoved = 0;
   return true;
 }
 
-void
+/* static */ void
 WheelTransaction::MayEndTransaction()
 {
   if (!sOwnScrollbars && ScrollbarsForWheel::IsActive()) {
     ScrollbarsForWheel::OwnWheelTransaction(true);
   } else {
     EndTransaction();
   }
 }
 
-void
+/* static */ void
 WheelTransaction::EndTransaction()
 {
-  if (sTimer)
+  if (sTimer) {
     sTimer->Cancel();
+  }
   sTargetFrame = nullptr;
   sScrollSeriesCounter = 0;
   if (sOwnScrollbars) {
     sOwnScrollbars = false;
     ScrollbarsForWheel::OwnWheelTransaction(false);
     ScrollbarsForWheel::Inactivate();
   }
 }
 
-void
+/* static */ void
 WheelTransaction::OnEvent(WidgetEvent* aEvent)
 {
-  if (!sTargetFrame)
+  if (!sTargetFrame) {
     return;
+  }
 
   if (OutOfTime(sTime, GetTimeoutTime())) {
     // Even if the scroll event which is handled after timeout, but onTimeout
     // was not fired by timer, then the scroll event will scroll old frame,
     // therefore, we should call OnTimeout here and ensure to finish the old
     // transaction.
     OnTimeout(nullptr, nullptr);
     return;
@@ -193,19 +196,18 @@ WheelTransaction::OnEvent(WidgetEvent* a
           EndTransaction();
           return;
         }
 
         // If the cursor is moving inside the frame, and it is less than
         // ignoremovedelay milliseconds since the last scroll operation, ignore
         // the mouse move; otherwise, record the current mouse move time to be
         // checked later
-        if (OutOfTime(sTime, GetIgnoreMoveDelayTime())) {
-          if (sMouseMoved == 0)
-            sMouseMoved = PR_IntervalToMilliseconds(PR_IntervalNow());
+        if (!sMouseMoved && OutOfTime(sTime, GetIgnoreMoveDelayTime())) {
+          sMouseMoved = PR_IntervalToMilliseconds(PR_IntervalNow());
         }
       }
       return;
     }
     case NS_KEY_PRESS:
     case NS_KEY_UP:
     case NS_KEY_DOWN:
     case NS_MOUSE_BUTTON_UP:
@@ -214,23 +216,23 @@ WheelTransaction::OnEvent(WidgetEvent* a
     case NS_MOUSE_CLICK:
     case NS_CONTEXTMENU:
     case NS_DRAGDROP_DROP:
       EndTransaction();
       return;
   }
 }
 
-void
+/* static */ void
 WheelTransaction::Shutdown()
 {
   NS_IF_RELEASE(sTimer);
 }
 
-void
+/* static */ void
 WheelTransaction::OnFailToScrollTarget()
 {
   NS_PRECONDITION(sTargetFrame, "We don't have mouse scrolling transaction");
 
   if (Preferences::GetBool("test.mousescroll", false)) {
     // This event is used for automated tests, see bug 442774.
     nsContentUtils::DispatchTrustedEvent(
                       sTargetFrame->GetContent()->OwnerDoc(),
@@ -240,17 +242,17 @@ WheelTransaction::OnFailToScrollTarget()
   }
   // The target frame might be destroyed in the event handler, at that time,
   // we need to finish the current transaction
   if (!sTargetFrame) {
     EndTransaction();
   }
 }
 
-void
+/* static */ void
 WheelTransaction::OnTimeout(nsITimer* aTimer, void* aClosure)
 {
   if (!sTargetFrame) {
     // The transaction target was destroyed already
     EndTransaction();
     return;
   }
   // Store the sTargetFrame, the variable becomes null in EndTransaction.
@@ -264,56 +266,55 @@ WheelTransaction::OnTimeout(nsITimer* aT
     nsContentUtils::DispatchTrustedEvent(
                       frame->GetContent()->OwnerDoc(),
                       frame->GetContent(),
                       NS_LITERAL_STRING("MozMouseScrollTransactionTimeout"),
                       true, true);
   }
 }
 
-void
+/* static */ void
 WheelTransaction::SetTimeout()
 {
   if (!sTimer) {
     nsCOMPtr<nsITimer> timer = do_CreateInstance(NS_TIMER_CONTRACTID);
-    if (!timer)
+    if (!timer) {
       return;
+    }
     timer.swap(sTimer);
   }
   sTimer->Cancel();
-#ifdef DEBUG
-  nsresult rv =
-#endif
-  sTimer->InitWithFuncCallback(OnTimeout, nullptr, GetTimeoutTime(),
-                               nsITimer::TYPE_ONE_SHOT);
+  DebugOnly<nsresult> rv =
+    sTimer->InitWithFuncCallback(OnTimeout, nullptr, GetTimeoutTime(),
+                                 nsITimer::TYPE_ONE_SHOT);
   NS_WARN_IF_FALSE(NS_SUCCEEDED(rv), "nsITimer::InitWithFuncCallback failed");
 }
 
-nsIntPoint
+/* static */ nsIntPoint
 WheelTransaction::GetScreenPoint(WidgetGUIEvent* aEvent)
 {
   NS_ASSERTION(aEvent, "aEvent is null");
   NS_ASSERTION(aEvent->widget, "aEvent-widget is null");
   return LayoutDeviceIntPoint::ToUntyped(aEvent->refPoint) +
-         aEvent->widget->WidgetToScreenOffset();
+           aEvent->widget->WidgetToScreenOffset();
 }
 
-uint32_t
+/* static */ uint32_t
 WheelTransaction::GetTimeoutTime()
 {
   return Preferences::GetUint("mousewheel.transaction.timeout", 1500);
 }
 
-uint32_t
+/* static */ uint32_t
 WheelTransaction::GetIgnoreMoveDelayTime()
 {
   return Preferences::GetUint("mousewheel.transaction.ignoremovedelay", 100);
 }
 
-DeltaValues
+/* static */ DeltaValues
 WheelTransaction::AccelerateWheelDelta(WidgetWheelEvent* aEvent,
                                        bool aAllowScrollSpeedOverride)
 {
   DeltaValues result(aEvent);
 
   // Don't accelerate the delta values if the event isn't line scrolling.
   if (aEvent->deltaMode != nsIDOMWheelEvent::DOM_DELTA_LINE) {
     return result;
@@ -331,40 +332,40 @@ WheelTransaction::AccelerateWheelDelta(W
       result.deltaX = ComputeAcceleratedWheelDelta(result.deltaX, factor);
       result.deltaY = ComputeAcceleratedWheelDelta(result.deltaY, factor);
     }
   }
 
   return result;
 }
 
-double
+/* static */ double
 WheelTransaction::ComputeAcceleratedWheelDelta(double aDelta,
                                                int32_t aFactor)
 {
   if (aDelta == 0.0) {
     return 0;
   }
 
   return (aDelta * sScrollSeriesCounter * (double)aFactor / 10);
 }
 
-int32_t
+/* static */ int32_t
 WheelTransaction::GetAccelerationStart()
 {
   return Preferences::GetInt("mousewheel.acceleration.start", -1);
 }
 
-int32_t
+/* static */ int32_t
 WheelTransaction::GetAccelerationFactor()
 {
   return Preferences::GetInt("mousewheel.acceleration.factor", -1);
 }
 
-DeltaValues
+/* static */ DeltaValues
 WheelTransaction::OverrideSystemScrollSpeed(WidgetWheelEvent* aEvent)
 {
   MOZ_ASSERT(sTargetFrame, "We don't have mouse scrolling transaction");
   MOZ_ASSERT(aEvent->deltaMode == nsIDOMWheelEvent::DOM_DELTA_LINE);
 
   // If the event doesn't scroll to both X and Y, we don't need to do anything
   // here.
   if (!aEvent->deltaX && !aEvent->deltaY) {
@@ -402,116 +403,116 @@ const DeltaValues ScrollbarsForWheel::di
 nsWeakFrame ScrollbarsForWheel::sActiveOwner = nullptr;
 nsWeakFrame ScrollbarsForWheel::sActivatedScrollTargets[kNumberOfTargets] = {
   nullptr, nullptr, nullptr, nullptr
 };
 
 bool ScrollbarsForWheel::sHadWheelStart = false;
 bool ScrollbarsForWheel::sOwnWheelTransaction = false;
 
-void
+/* static */ void
 ScrollbarsForWheel::PrepareToScrollText(nsEventStateManager* aESM,
                                         nsIFrame* aTargetFrame,
                                         WidgetWheelEvent* aEvent)
 {
   if (aEvent->message == NS_WHEEL_START) {
     WheelTransaction::OwnScrollbars(false);
     if (!IsActive()) {
       TemporarilyActivateAllPossibleScrollTargets(aESM, aTargetFrame, aEvent);
       sHadWheelStart = true;
     }
   } else {
     DeactivateAllTemporarilyActivatedScrollTargets();
   }
 }
 
-void
+/* static */ void
 ScrollbarsForWheel::SetActiveScrollTarget(nsIScrollableFrame* aScrollTarget)
 {
   if (!sHadWheelStart) {
     return;
   }
   nsIScrollbarOwner* scrollbarOwner = do_QueryFrame(aScrollTarget);
   if (!scrollbarOwner) {
     return;
   }
   sHadWheelStart = false;
   sActiveOwner = do_QueryFrame(aScrollTarget);
   scrollbarOwner->ScrollbarActivityStarted();
 }
 
-void
+/* static */ void
 ScrollbarsForWheel::MayInactivate()
 {
   if (!sOwnWheelTransaction && WheelTransaction::GetTargetFrame()) {
     WheelTransaction::OwnScrollbars(true);
   } else {
     Inactivate();
   }
 }
 
-void
+/* static */ void
 ScrollbarsForWheel::Inactivate()
 {
   nsIScrollbarOwner* scrollbarOwner = do_QueryFrame(sActiveOwner);
   if (scrollbarOwner) {
     scrollbarOwner->ScrollbarActivityStopped();
   }
   sActiveOwner = nullptr;
   DeactivateAllTemporarilyActivatedScrollTargets();
   if (sOwnWheelTransaction) {
     sOwnWheelTransaction = false;
     WheelTransaction::OwnScrollbars(false);
     WheelTransaction::EndTransaction();
   }
 }
 
-bool
+/* static */ bool
 ScrollbarsForWheel::IsActive()
 {
   if (sActiveOwner) {
     return true;
   }
   for (size_t i = 0; i < kNumberOfTargets; ++i) {
     if (sActivatedScrollTargets[i]) {
       return true;
     }
   }
   return false;
 }
 
-void
+/* static */ void
 ScrollbarsForWheel::OwnWheelTransaction(bool aOwn)
 {
   sOwnWheelTransaction = aOwn;
 }
 
-void
+/* static */ void
 ScrollbarsForWheel::TemporarilyActivateAllPossibleScrollTargets(
                       nsEventStateManager* aESM,
                       nsIFrame* aTargetFrame,
                       WidgetWheelEvent* aEvent)
 {
   for (size_t i = 0; i < kNumberOfTargets; i++) {
     const DeltaValues *dir = &directions[i];
     nsWeakFrame* scrollTarget = &sActivatedScrollTargets[i];
     MOZ_ASSERT(!*scrollTarget, "scroll target still temporarily activated!");
     nsIScrollableFrame* target =
-      aESM->ComputeScrollTarget(aTargetFrame, dir->deltaX, dir->deltaY, aEvent, 
-                                nsEventStateManager::COMPUTE_DEFAULT_ACTION_TARGET);
+      aESM->ComputeScrollTarget(aTargetFrame, dir->deltaX, dir->deltaY, aEvent,
+              nsEventStateManager::COMPUTE_DEFAULT_ACTION_TARGET);
     nsIScrollbarOwner* scrollbarOwner = do_QueryFrame(target);
     if (scrollbarOwner) {
       nsIFrame* targetFrame = do_QueryFrame(target);
       *scrollTarget = targetFrame;
       scrollbarOwner->ScrollbarActivityStarted();
     }
   }
 }
 
-void
+/* static */ void
 ScrollbarsForWheel::DeactivateAllTemporarilyActivatedScrollTargets()
 {
   for (size_t i = 0; i < kNumberOfTargets; i++) {
     nsWeakFrame* scrollTarget = &sActivatedScrollTargets[i];
     if (*scrollTarget) {
       nsIScrollbarOwner* scrollbarOwner = do_QueryFrame(*scrollTarget);
       if (scrollbarOwner) {
         scrollbarOwner->ScrollbarActivityStopped();
--- a/dom/events/WheelHandlingHelper.h
+++ b/dom/events/WheelHandlingHelper.h
@@ -15,16 +15,21 @@
 class nsEventStateManager;
 class nsIScrollableFrame;
 class nsITimer;
 
 struct nsIntPoint;
 
 namespace mozilla {
 
+/**
+ * DeltaValues stores two delta values which are along X and Y axis.  This is
+ * useful for arguments and results of some methods.
+ */
+
 struct DeltaValues
 {
   DeltaValues()
     : deltaX(0.0)
     , deltaY(0.0)
   {
   }
 
@@ -35,31 +40,43 @@ struct DeltaValues
   }
 
   explicit DeltaValues(WidgetWheelEvent* aEvent);
 
   double deltaX;
   double deltaY;
 };
 
+/**
+ * WheelHandlingUtils provides some static methods which are useful at handling
+ * wheel events.
+ */
+
 class WheelHandlingUtils
 {
 public:
   /**
    * Returns true if the scrollable frame can be scrolled to either aDirectionX
    * or aDirectionY along each axis.  Otherwise, false.
    */
   static bool CanScrollOn(nsIScrollableFrame* aScrollFrame,
                           double aDirectionX, double aDirectionY);
 
 private:
   static bool CanScrollInRange(nscoord aMin, nscoord aValue, nscoord aMax,
                                double aDirection);
 };
 
+/**
+ * ScrollbarsForWheel manages scrollbars state during wheel operation.
+ * E.g., on some platforms, scrollbars should show only while user attempts to
+ * scroll.  At that time, scrollbars which may be possible to scroll by
+ * operation of wheel at the point should show temporarily.
+ */
+
 class ScrollbarsForWheel
 {
 public:
   static void PrepareToScrollText(nsEventStateManager* aESM,
                                   nsIFrame* aTargetFrame,
                                   WidgetWheelEvent* aEvent);
   static void SetActiveScrollTarget(nsIScrollableFrame* aScrollTarget);
   // Hide all scrollbars (both mActiveOwner's and mActivatedScrollTargets')
@@ -83,16 +100,24 @@ protected:
    */
   static void TemporarilyActivateAllPossibleScrollTargets(
                 nsEventStateManager* aESM,
                 nsIFrame* aTargetFrame,
                 WidgetWheelEvent* aEvent);
   static void DeactivateAllTemporarilyActivatedScrollTargets();
 };
 
+/**
+ * WheelTransaction manages a series of wheel events as a transaction.
+ * While in a transaction, every wheel event should scroll the same scrollable
+ * element even if a different scrollable element is under the mouse cursor.
+ *
+ * Additionally, this class also manages wheel scroll speed acceleration.
+ */
+
 class WheelTransaction
 {
 public:
   static nsIFrame* GetTargetFrame() { return sTargetFrame; }
   static void BeginTransaction(nsIFrame* aTargetFrame,
                                WidgetWheelEvent* aEvent);
   // Be careful, UpdateTransaction may fire a DOM event, therefore, the target
   // frame might be destroyed in the event handler.