Use nsIScrollPositionListener draft
authorBotond Ballo <botond@mozilla.com>
Fri, 29 Jul 2016 19:12:43 -0400
changeset 394556 f522de6357147eac332638d940dbdc409bc53739
parent 394555 88878d26a871901f2164edf15b8798fe84085673
child 526840 86ab5a60d7701db13acd168990e335f9ad479a55
push id24603
push userbballo@mozilla.com
push dateFri, 29 Jul 2016 23:28:33 +0000
milestone50.0a1
Use nsIScrollPositionListener MozReview-Commit-ID: BEfHe8gBiva
dom/animation/ScrollTimeline.cpp
dom/animation/ScrollTimeline.h
dom/base/Element.h
layout/generic/nsGfxScrollFrame.cpp
layout/generic/nsIScrollableFrame.h
--- a/dom/animation/ScrollTimeline.cpp
+++ b/dom/animation/ScrollTimeline.cpp
@@ -7,16 +7,17 @@
 #include "ScrollTimeline.h"
 #include "mozilla/dom/ScrollTimelineBinding.h"
 #include "AnimationUtils.h"
 #include "nsContentUtils.h"
 #include "nsCSSPseudoElements.h" // For CSSPseudoElementType
 #include "nsDOMMutationObserver.h"
 #include "nsDOMNavigationTiming.h"
 #include "nsIPresShell.h"
+#include "nsLayoutUtils.h"
 #include "nsPresContext.h"
 #include "nsRefreshDriver.h"
 
 namespace mozilla {
 namespace dom {
 
 NS_IMPL_CYCLE_COLLECTION_INHERITED(ScrollTimeline, AnimationTimeline,
                                    mDocument)
@@ -176,16 +177,24 @@ ScrollTimeline::Tick() {
     }
   }
 
   for (Animation* animation : animationsToRemove) {
     RemoveAnimation(animation);
   }
 }
 
+nsIScrollableFrame*
+ScrollTimeline::GetScrollFrame() const
+{
+  return nsLayoutUtils::FindScrollableFrameFor(
+      nsLayoutUtils::FindOrCreateIDFor(mElement.get()));
+}
+
+
 void
-ScrollObserverImpl::notify()
+ScrollPositionListener::ScrollPositionDidChange(nscoord, nscoord)
 {
   mTimeline->Tick();
 }
 
 } // namespace dom
 } // namespace mozilla
--- a/dom/animation/ScrollTimeline.h
+++ b/dom/animation/ScrollTimeline.h
@@ -7,34 +7,35 @@
 #ifndef mozilla_dom_ScrollTimeline_h
 #define mozilla_dom_ScrollTimeline_h
 
 #include "mozilla/TimeStamp.h"
 #include "mozilla/AnimationTarget.h"
 #include "mozilla/dom/ScrollTimelineBinding.h"
 #include "AnimationTimeline.h"
 #include "nsIDocument.h"
+#include "nsIScrollPositionListener.h"
 #include "nsRefreshDriver.h"
 #include "mozilla/dom/Element.h"
 #include "mozilla/dom/Animation.h"
 #include "mozilla/LinkedList.h"
 
 struct JSContext;
 
 namespace mozilla {
 namespace dom {
-class ScrollObserver;
 
-class ScrollObserverImpl : public ScrollObserver {
+class ScrollPositionListener : public nsIScrollPositionListener {
 public:
-  ScrollObserverImpl(ScrollTimeline* aTimeline) {
+  ScrollPositionListener(ScrollTimeline* aTimeline) {
     mTimeline = aTimeline;
   }
-  
-  void notify() override;
+
+  void ScrollPositionWillChange(nscoord, nscoord) override {}
+  void ScrollPositionDidChange(nscoord, nscoord) override;
 private:
   ScrollTimeline* mTimeline;
 };
 
 class ScrollTimeline final
   : public AnimationTimeline
 {
 public:
@@ -45,27 +46,29 @@ public:
     : AnimationTimeline(aDocument->GetParentObject())
     , mDocument(aDocument)
     , mOrientation(aOrientation)
     , mElement(aTarget)
     , mCurrentScroll(0)
     , mMinScroll(0)
     , mMaxScroll(0)
   {
-    mScrollObserver = new ScrollObserverImpl(this);
-    aTarget->RegistScrollTimelineObserver(mScrollObserver);
+    mScrollPositionListener = new ScrollPositionListener(this);
+    if (nsIScrollableFrame* scrollFrame = GetScrollFrame()) {
+      scrollFrame->AddScrollPositionListener(mScrollPositionListener);
+    }
     CalculateTimeRange();
     QueryScrollValues();
   }
 
 protected:
   virtual ~ScrollTimeline()
   {
-    if (mScrollObserver) {
-      mElement.get()->UnregistScrollTimelineObserver();
+    if (nsIScrollableFrame* scrollFrame = GetScrollFrame()) {
+      scrollFrame->RemoveScrollPositionListener(mScrollPositionListener);
     }
   }
 
 public:
   NS_DECL_ISUPPORTS_INHERITED
   NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS_INHERITED(ScrollTimeline,
                                                          AnimationTimeline)
 
@@ -93,24 +96,26 @@ public:
 
   void RemoveAnimation(Animation* aAnimation) override;
 
   void Tick();
   void QueryScrollValues();
 
   bool SupportsOMTA() const override { return false; }
 
+  nsIScrollableFrame* GetScrollFrame() const;
+
 protected:
   void CalculateTimeRange();
 
   nsCOMPtr<nsIDocument> mDocument;
 
   Orientation mOrientation;
   double mMaxTime;
-  ScrollObserverImpl *mScrollObserver;
+  ScrollPositionListener* mScrollPositionListener;
   RefPtr<Element> mElement;
   StickyTimeDuration mTimeRange;
   double mCurrentScroll, mMinScroll, mMaxScroll;
 };
 
 } // namespace dom
 } // namespace mozilla
 
--- a/dom/base/Element.h
+++ b/dom/base/Element.h
@@ -136,73 +136,27 @@ namespace dom {
 
 class Animation;
 class Link;
 class UndoManager;
 class DOMRect;
 class DOMRectList;
 class DestinationInsertionPointList;
 class Grid;
-class ScrollHandler;
 
 // IID for the dom::Element interface
 #define NS_ELEMENT_IID \
 { 0xc67ed254, 0xfd3b, 0x4b10, \
   { 0x96, 0xa2, 0xc5, 0x8b, 0x7b, 0x64, 0x97, 0xd1 } }
 
-class ScrollObserver {
-public:
-  ScrollObserver() {};
-  virtual void notify() { };
-};
-
-class ScrollHandlerImpl : public ScrollHandler{
-public:
-  ScrollHandlerImpl(ScrollObserver* observer)
-    : ScrollHandler()
-    , scrollObserver(observer) {
-  };
-
-  // Implemented after Element definitions.
-  void notify() override {
-    scrollObserver->notify();
-  }
-
-private:
-  ScrollObserver* scrollObserver;
-};
 
 class Element : public FragmentOrElement
 {
 public:
 
-  ScrollObserver* scrollObserver;
-  ScrollHandlerImpl* mScrollHandler;
-  void RegistScrollTimelineObserver(ScrollObserver* obs) {
-    scrollObserver = obs;
-    nsIScrollableFrame* sf = GetScrollFrame(nullptr, false);
-    if (!sf) {
-      printf("Element:%s, ScrollableFrame is not found.\n", __func__);
-      return;
-    }
-    mScrollHandler = new ScrollHandlerImpl(scrollObserver);
-    sf->AddScrollHandler(mScrollHandler);
-  }
-
-  void UnregistScrollTimelineObserver() {
-    scrollObserver = nullptr;
-
-    nsIScrollableFrame* sf = GetScrollFrame(nullptr, false);
-    if (!sf) {
-      printf("Element:%s, ScrollableFrame is not found.\n", __func__);
-      return;
-    }
-    sf->RemoveScrollHandler();
-  }
-
 #ifdef MOZILLA_INTERNAL_API
   explicit Element(already_AddRefed<mozilla::dom::NodeInfo>& aNodeInfo) :
     FragmentOrElement(aNodeInfo),
     mState(NS_EVENT_STATE_MOZ_READONLY)
   {
     MOZ_ASSERT(mNodeInfo->NodeType() == nsIDOMNode::ELEMENT_NODE,
                "Bad NodeType in aNodeInfo");
     SetIsElement();
--- a/layout/generic/nsGfxScrollFrame.cpp
+++ b/layout/generic/nsGfxScrollFrame.cpp
@@ -2815,20 +2815,16 @@ ScrollFrameHelper::ScrollToImpl(nsPoint 
     UpdateScrollbarPosition();
     if (!weakFrame.IsAlive()) {
       return;
     }
   }
 
   PostScrollEvent();
 
-  // Notify the scroll volume to ScrollTimeline.
-  nsIScrollableFrame *scrollable = do_QueryFrame(mOuter);
-  scrollable->NotifyScroll();
-
   // notify the listeners.
   for (uint32_t i = 0; i < mListeners.Length(); i++) {
     mListeners[i]->ScrollPositionDidChange(pt.x, pt.y);
   }
 
   nsCOMPtr<nsIDocShell> docShell = presContext->GetDocShell();
   if (docShell) {
     docShell->NotifyScrollObservers();
--- a/layout/generic/nsIScrollableFrame.h
+++ b/layout/generic/nsIScrollableFrame.h
@@ -31,46 +31,25 @@ class nsRenderingContext;
 class nsIAtom;
 class nsDisplayListBuilder;
 
 namespace mozilla {
 struct ContainerLayerParameters;
 namespace layers {
 class Layer;
 } // namespace layers
-namespace dom {
-class ScrollHandler
-{
-public:
-  ScrollHandler() {};
-  virtual void notify() { };
-};
-} // namespace dom
 } // namespace mozilla
 
 /**
  * Interface for frames that are scrollable. This interface exposes
  * APIs for examining scroll state, observing changes to scroll state,
  * and triggering scrolling.
  */
 class nsIScrollableFrame : public nsIScrollbarMediator {
 public:
-  mozilla::dom::ScrollHandler* scrollHandler = nullptr;
-  void AddScrollHandler(mozilla::dom::ScrollHandler* handler) {
-    scrollHandler = handler;
-  }
-  void RemoveScrollHandler() {
-    scrollHandler = nullptr;
-  }
-  void NotifyScroll() {
-    if (scrollHandler) {
-      scrollHandler->notify();
-    }
-  }
-  
   typedef mozilla::CSSIntPoint CSSIntPoint;
   typedef mozilla::ContainerLayerParameters ContainerLayerParameters;
   typedef mozilla::layers::FrameMetrics FrameMetrics;
   typedef mozilla::layers::ScrollSnapInfo ScrollSnapInfo;
 
   NS_DECL_QUERYFRAME_TARGET(nsIScrollableFrame)
 
   /**