dom/base/DOMIntersectionObserver.h
author Simon Whitehead <chemnova@gmail.com>
Sat, 13 May 2017 15:12:15 -0500
changeset 358329 633151f5d481d49bd39900d43e6a75d77459a7bb
parent 347912 8a47d616af8f56f7cb0c9b210901b621e645465a
child 366079 c285e406afb2651d39e1a6d1fb9400b65f23e4d3
permissions -rw-r--r--
servo: Merge #16829 - Disallow negative duration for animation and transition (from hiikezoe:disallow-negative-duration); r=emilio <!-- Please describe your changes on the following line: --> --- <!-- Thank you for contributing to Servo! Please replace each `[ ]` by `[X]` when the step is complete, and replace `__` with appropriate data: --> - [X] `./mach build -d` does not report any errors - [X] `./mach test-tidy` does not report any errors - [X] These changes fix #15343 <!-- Either: --> - [X] There are tests for these changes written by @simon-whitehead . Thank you! <!-- Also, please make sure that "Allow edits from maintainers" checkbox is checked, so that we can help you if you get stuck somewhere along the way.--> <!-- Pull requests that do not address these steps are welcome, but they will require additional verification as part of the review process. --> Source-Repo: https://github.com/servo/servo Source-Revision: 4613c0382ab76263fb8202ccf52f4c0520e44809

/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/. */

#ifndef DOMIntersectionObserver_h
#define DOMIntersectionObserver_h

#include "mozilla/dom/IntersectionObserverBinding.h"
#include "nsCSSValue.h"
#include "nsTArray.h"

using mozilla::dom::DOMRect;
using mozilla::dom::Element;

namespace mozilla {
namespace dom {

class DOMIntersectionObserver;

class DOMIntersectionObserverEntry final : public nsISupports,
                                           public nsWrapperCache
{
  ~DOMIntersectionObserverEntry() {}

public:
  DOMIntersectionObserverEntry(nsISupports* aOwner,
                               DOMHighResTimeStamp aTime,
                               RefPtr<DOMRect> aRootBounds,
                               RefPtr<DOMRect> aBoundingClientRect,
                               RefPtr<DOMRect> aIntersectionRect,
                               bool aIsIntersecting,
                               Element* aTarget,
                               double aIntersectionRatio)
  : mOwner(aOwner),
    mTime(aTime),
    mRootBounds(aRootBounds),
    mBoundingClientRect(aBoundingClientRect),
    mIntersectionRect(aIntersectionRect),
    mIsIntersecting(aIsIntersecting),
    mTarget(aTarget),
    mIntersectionRatio(aIntersectionRatio)
  {
  }
  NS_DECL_CYCLE_COLLECTING_ISUPPORTS
  NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(DOMIntersectionObserverEntry)

  nsISupports* GetParentObject() const
  {
    return mOwner;
  }

  virtual JSObject* WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto) override
  {
    return mozilla::dom::IntersectionObserverEntryBinding::Wrap(aCx, this, aGivenProto);
  }

  DOMHighResTimeStamp Time()
  {
    return mTime;
  }

  DOMRect* GetRootBounds()
  {
    return mRootBounds;
  }

  DOMRect* BoundingClientRect()
  {
    return mBoundingClientRect;
  }

  DOMRect* IntersectionRect()
  {
    return mIntersectionRect;
  }

  bool IsIntersecting()
  {
    return mIsIntersecting;
  }

  double IntersectionRatio()
  {
    return mIntersectionRatio;
  }

  Element* Target()
  {
    return mTarget;
  }

protected:
  nsCOMPtr<nsISupports> mOwner;
  DOMHighResTimeStamp   mTime;
  RefPtr<DOMRect>       mRootBounds;
  RefPtr<DOMRect>       mBoundingClientRect;
  RefPtr<DOMRect>       mIntersectionRect;
  bool                  mIsIntersecting;
  RefPtr<Element>       mTarget;
  double                mIntersectionRatio;
};

#define NS_DOM_INTERSECTION_OBSERVER_IID \
{ 0x8570a575, 0xe303, 0x4d18, \
  { 0xb6, 0xb1, 0x4d, 0x2b, 0x49, 0xd8, 0xef, 0x94 } }

class DOMIntersectionObserver final : public nsISupports,
                                      public nsWrapperCache
{
  virtual ~DOMIntersectionObserver() {
    Disconnect();
  }

public:
  DOMIntersectionObserver(already_AddRefed<nsPIDOMWindowInner>&& aOwner,
                          mozilla::dom::IntersectionCallback& aCb)
  : mOwner(aOwner), mDocument(mOwner->GetExtantDoc()), mCallback(&aCb), mConnected(false)
  {
  }
  NS_DECL_CYCLE_COLLECTING_ISUPPORTS
  NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(DOMIntersectionObserver)
  NS_DECLARE_STATIC_IID_ACCESSOR(NS_DOM_INTERSECTION_OBSERVER_IID)

  static already_AddRefed<DOMIntersectionObserver>
  Constructor(const mozilla::dom::GlobalObject& aGlobal,
              mozilla::dom::IntersectionCallback& aCb,
              mozilla::ErrorResult& aRv);
  static already_AddRefed<DOMIntersectionObserver>
  Constructor(const mozilla::dom::GlobalObject& aGlobal,
              mozilla::dom::IntersectionCallback& aCb,
              const mozilla::dom::IntersectionObserverInit& aOptions,
              mozilla::ErrorResult& aRv);

  virtual JSObject* WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto) override
  {
    return mozilla::dom::IntersectionObserverBinding::Wrap(aCx, this, aGivenProto);
  }

  nsISupports* GetParentObject() const
  {
    return mOwner;
  }

  Element* GetRoot() const {
    return mRoot;
  }

  void GetRootMargin(mozilla::dom::DOMString& aRetVal);
  void GetThresholds(nsTArray<double>& aRetVal);
  void Observe(Element& aTarget);
  void Unobserve(Element& aTarget);

  void UnlinkTarget(Element& aTarget);
  void Disconnect();

  void TakeRecords(nsTArray<RefPtr<DOMIntersectionObserverEntry>>& aRetVal);

  mozilla::dom::IntersectionCallback* IntersectionCallback() { return mCallback; }

  bool SetRootMargin(const nsAString& aString);

  void Update(nsIDocument* aDocument, DOMHighResTimeStamp time);
  void Notify();

protected:
  void Connect();
  void QueueIntersectionObserverEntry(Element* aTarget,
                                      DOMHighResTimeStamp time,
                                      const Maybe<nsRect>& aRootRect,
                                      const nsRect& aTargetRect,
                                      const Maybe<nsRect>& aIntersectionRect,
                                      double aIntersectionRatio);

  nsCOMPtr<nsPIDOMWindowInner>                    mOwner;
  RefPtr<nsIDocument>                             mDocument;
  RefPtr<mozilla::dom::IntersectionCallback>      mCallback;
  RefPtr<Element>                                 mRoot;
  nsCSSRect                                       mRootMargin;
  nsTArray<double>                                mThresholds;
  nsTHashtable<nsPtrHashKey<Element>>             mObservationTargets;
  nsTArray<RefPtr<DOMIntersectionObserverEntry>>  mQueuedEntries;
  bool                                            mConnected;
};

NS_DEFINE_STATIC_IID_ACCESSOR(DOMIntersectionObserver, NS_DOM_INTERSECTION_OBSERVER_IID)

} // namespace dom
} // namespace mozilla

#endif