Coverage Report

Created: 2018-09-25 14:53

/work/obj-fuzz/dist/include/mozilla/dom/DOMIntersectionObserver.h
Line
Count
Source (jump to first uncovered line)
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 file,
5
 * You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7
#ifndef DOMIntersectionObserver_h
8
#define DOMIntersectionObserver_h
9
10
#include "mozilla/dom/IntersectionObserverBinding.h"
11
#include "nsStyleCoord.h"
12
#include "nsTArray.h"
13
14
using mozilla::dom::DOMRect;
15
using mozilla::dom::Element;
16
17
namespace mozilla {
18
namespace dom {
19
20
class DOMIntersectionObserver;
21
22
class DOMIntersectionObserverEntry final : public nsISupports,
23
                                           public nsWrapperCache
24
{
25
0
  ~DOMIntersectionObserverEntry() {}
26
27
public:
28
  DOMIntersectionObserverEntry(nsISupports* aOwner,
29
                               DOMHighResTimeStamp aTime,
30
                               RefPtr<DOMRect> aRootBounds,
31
                               RefPtr<DOMRect> aBoundingClientRect,
32
                               RefPtr<DOMRect> aIntersectionRect,
33
                               bool aIsIntersecting,
34
                               Element* aTarget,
35
                               double aIntersectionRatio)
36
  : mOwner(aOwner),
37
    mTime(aTime),
38
    mRootBounds(aRootBounds),
39
    mBoundingClientRect(aBoundingClientRect),
40
    mIntersectionRect(aIntersectionRect),
41
    mIsIntersecting(aIsIntersecting),
42
    mTarget(aTarget),
43
    mIntersectionRatio(aIntersectionRatio)
44
0
  {
45
0
  }
46
  NS_DECL_CYCLE_COLLECTING_ISUPPORTS
47
  NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(DOMIntersectionObserverEntry)
48
49
  nsISupports* GetParentObject() const
50
0
  {
51
0
    return mOwner;
52
0
  }
53
54
  virtual JSObject* WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto) override
55
0
  {
56
0
    return mozilla::dom::IntersectionObserverEntry_Binding::Wrap(aCx, this, aGivenProto);
57
0
  }
58
59
  DOMHighResTimeStamp Time()
60
0
  {
61
0
    return mTime;
62
0
  }
63
64
  DOMRect* GetRootBounds()
65
0
  {
66
0
    return mRootBounds;
67
0
  }
68
69
  DOMRect* BoundingClientRect()
70
0
  {
71
0
    return mBoundingClientRect;
72
0
  }
73
74
  DOMRect* IntersectionRect()
75
0
  {
76
0
    return mIntersectionRect;
77
0
  }
78
79
  bool IsIntersecting()
80
0
  {
81
0
    return mIsIntersecting;
82
0
  }
83
84
  double IntersectionRatio()
85
0
  {
86
0
    return mIntersectionRatio;
87
0
  }
88
89
  Element* Target()
90
0
  {
91
0
    return mTarget;
92
0
  }
93
94
protected:
95
  nsCOMPtr<nsISupports> mOwner;
96
  DOMHighResTimeStamp   mTime;
97
  RefPtr<DOMRect>       mRootBounds;
98
  RefPtr<DOMRect>       mBoundingClientRect;
99
  RefPtr<DOMRect>       mIntersectionRect;
100
  bool                  mIsIntersecting;
101
  RefPtr<Element>       mTarget;
102
  double                mIntersectionRatio;
103
};
104
105
#define NS_DOM_INTERSECTION_OBSERVER_IID \
106
{ 0x8570a575, 0xe303, 0x4d18, \
107
  { 0xb6, 0xb1, 0x4d, 0x2b, 0x49, 0xd8, 0xef, 0x94 } }
108
109
class DOMIntersectionObserver final : public nsISupports,
110
                                      public nsWrapperCache
111
{
112
0
  virtual ~DOMIntersectionObserver() {
113
0
    Disconnect();
114
0
  }
115
116
public:
117
  DOMIntersectionObserver(already_AddRefed<nsPIDOMWindowInner>&& aOwner,
118
                          mozilla::dom::IntersectionCallback& aCb)
119
  : mOwner(aOwner), mDocument(mOwner->GetExtantDoc()), mCallback(&aCb), mConnected(false)
120
0
  {
121
0
  }
122
  NS_DECL_CYCLE_COLLECTING_ISUPPORTS
123
  NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(DOMIntersectionObserver)
124
  NS_DECLARE_STATIC_IID_ACCESSOR(NS_DOM_INTERSECTION_OBSERVER_IID)
125
126
  static already_AddRefed<DOMIntersectionObserver>
127
  Constructor(const mozilla::dom::GlobalObject& aGlobal,
128
              mozilla::dom::IntersectionCallback& aCb,
129
              mozilla::ErrorResult& aRv);
130
  static already_AddRefed<DOMIntersectionObserver>
131
  Constructor(const mozilla::dom::GlobalObject& aGlobal,
132
              mozilla::dom::IntersectionCallback& aCb,
133
              const mozilla::dom::IntersectionObserverInit& aOptions,
134
              mozilla::ErrorResult& aRv);
135
136
  virtual JSObject* WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto) override
137
0
  {
138
0
    return mozilla::dom::IntersectionObserver_Binding::Wrap(aCx, this, aGivenProto);
139
0
  }
140
141
  nsISupports* GetParentObject() const
142
0
  {
143
0
    return mOwner;
144
0
  }
145
146
0
  Element* GetRoot() const {
147
0
    return mRoot;
148
0
  }
149
150
  void GetRootMargin(mozilla::dom::DOMString& aRetVal);
151
  void GetThresholds(nsTArray<double>& aRetVal);
152
  void Observe(Element& aTarget);
153
  void Unobserve(Element& aTarget);
154
155
  void UnlinkTarget(Element& aTarget);
156
  void Disconnect();
157
158
  void TakeRecords(nsTArray<RefPtr<DOMIntersectionObserverEntry>>& aRetVal);
159
160
0
  mozilla::dom::IntersectionCallback* IntersectionCallback() { return mCallback; }
161
162
  bool SetRootMargin(const nsAString& aString);
163
164
  void Update(nsIDocument* aDocument, DOMHighResTimeStamp time);
165
  void Notify();
166
167
protected:
168
  void Connect();
169
  void QueueIntersectionObserverEntry(Element* aTarget,
170
                                      DOMHighResTimeStamp time,
171
                                      const Maybe<nsRect>& aRootRect,
172
                                      const nsRect& aTargetRect,
173
                                      const Maybe<nsRect>& aIntersectionRect,
174
                                      double aIntersectionRatio);
175
176
  nsCOMPtr<nsPIDOMWindowInner>                    mOwner;
177
  RefPtr<nsIDocument>                             mDocument;
178
  RefPtr<mozilla::dom::IntersectionCallback>      mCallback;
179
  RefPtr<Element>                                 mRoot;
180
  nsStyleSides                                    mRootMargin;
181
  nsTArray<double>                                mThresholds;
182
183
  // Holds raw pointers which are explicitly cleared by UnlinkTarget().
184
  nsTArray<Element*>                              mObservationTargets;
185
186
  nsTArray<RefPtr<DOMIntersectionObserverEntry>>  mQueuedEntries;
187
  bool                                            mConnected;
188
};
189
190
NS_DEFINE_STATIC_IID_ACCESSOR(DOMIntersectionObserver, NS_DOM_INTERSECTION_OBSERVER_IID)
191
192
} // namespace dom
193
} // namespace mozilla
194
195
#endif