summaryrefslogtreecommitdiff
path: root/dom/base/ResizeObserver.h
blob: ca87a080b48256de1aaec09e10dd153226cc9822 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* 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 mozilla_dom_ResizeObserver_h
#define mozilla_dom_ResizeObserver_h

#include "mozilla/AppUnits.h"
#include "mozilla/WritingModes.h"
#include "mozilla/dom/ResizeObserverBinding.h"
#include "nsCoord.h"

namespace mozilla {
namespace dom {

/**
 * ResizeObserver interfaces and algorithms are based on
 * https://wicg.github.io/ResizeObserver/#api
 */
class ResizeObserver final
  : public nsISupports
  , public nsWrapperCache
{
public:
  NS_DECL_CYCLE_COLLECTING_ISUPPORTS
  NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(ResizeObserver)

  ResizeObserver(already_AddRefed<nsPIDOMWindowInner>&& aOwner,
                 ResizeObserverCallback& aCb)
    : mOwner(aOwner)
    , mCallback(&aCb)
  {
    MOZ_ASSERT(mOwner, "Need a non-null owner window");
  }

  static already_AddRefed<ResizeObserver>
  Constructor(const GlobalObject& aGlobal,
              ResizeObserverCallback& aCb,
              ErrorResult& aRv);

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

  nsISupports* GetParentObject() const
  {
    return mOwner;
  }

  void Observe(Element* aTarget, const ResizeObserverOptions& aOptions, ErrorResult& aRv);

  void Unobserve(Element* aTarget, ErrorResult& aRv);

  void Disconnect();

  /*
   * Gather all observations which have an observed target with size changed
   * since last BroadcastActiveObservations() in this ResizeObserver.
   * An observation will be skipped if the depth of its observed target is less
   * or equal than aDepth. All gathered observations will be added to
   * mActiveTargets.
  */
  void GatherActiveObservations(uint32_t aDepth);

  /*
   * Returns whether this ResizeObserver has any active observations
   * since last GatherActiveObservations().
  */
  bool HasActiveObservations() const;

  /*
   * Returns whether this ResizeObserver has any skipped observations
   * since last GatherActiveObservations().
  */
  bool HasSkippedObservations() const;

  /*
   * Deliver the callback function in JavaScript for all active observations
   * and pass the sequence of ResizeObserverEntry so JavaScript can access them.
   * The active observations' mLastReportedSize fields will be updated, and
   * mActiveTargets will be cleared. It also returns the shallowest depth of
   * elements from active observations or numeric_limits<uint32_t>::max() if
   * there are not any active observations.
  */
  uint32_t BroadcastActiveObservations();

protected:
  ~ResizeObserver()
  {
    mObservationList.clear();
  }

  nsCOMPtr<nsPIDOMWindowInner> mOwner;
  RefPtr<ResizeObserverCallback> mCallback;
  nsTArray<RefPtr<ResizeObservation>> mActiveTargets;
  // The spec uses a list to store the skipped targets. However, it seems what
  // we want is to check if there are any skipped targets (i.e. existence).
  // Therefore, we use a boolean value to represent the existence of skipped
  // targets.
  bool mHasSkippedTargets;

  // Combination of HashTable and LinkedList so we can iterate through
  // the elements of HashTable in order of insertion time.
  // Will be nice if we have our own data structure for this in the future.
  nsRefPtrHashtable<nsPtrHashKey<Element>, ResizeObservation> mObservationMap;
  LinkedList<ResizeObservation> mObservationList;
};

/**
 * ResizeObserverEntry is the entry that contains the information for observed
 * elements. This object is the one that visible to JavaScript in callback
 * function that is fired by ResizeObserver.
 */
class ResizeObserverEntry final
  : public nsISupports
  , public nsWrapperCache
{
public:
  NS_DECL_CYCLE_COLLECTING_ISUPPORTS
  NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(ResizeObserverEntry)

  ResizeObserverEntry(nsISupports* aOwner, Element* aTarget)
    : mOwner(aOwner)
    , mTarget(aTarget)
  {
    MOZ_ASSERT(mOwner, "Need a non-null owner");
    MOZ_ASSERT(mTarget, "Need a non-null target element");
  }

  static already_AddRefed<ResizeObserverEntry>
  Constructor(const GlobalObject& aGlobal,
              Element* aTarget,
              ErrorResult& aRv);

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

  nsISupports* GetParentObject() const
  {
    return mOwner;
  }

  Element* Target() const
  {
    return mTarget;
  }

  /*
   * Returns the DOMRectReadOnly of target's content rect so it can be
   * accessed from JavaScript in callback function of ResizeObserver.
  */
  DOMRectReadOnly* GetContentRect() const
  {
    return mContentRect;
  }

  /**
   * Returns target's logical border-box size and content-box size as
   * a ResizeObserverSize array.
   */
  void GetBorderBoxSize(nsTArray<RefPtr<ResizeObserverSize>>& aRetVal) const;
  void GetContentBoxSize(nsTArray<RefPtr<ResizeObserverSize>>& aRetVal) const;

  // Set borderBoxSize.
  void SetBorderBoxSize(const nsSize& aSize);
  // Set contentRect and contentBoxSize.
  void SetContentRectAndSize(const nsSize& aSize);

protected:
  ~ResizeObserverEntry();

  nsCOMPtr<nsISupports> mOwner;
  nsCOMPtr<Element> mTarget;

  RefPtr<DOMRectReadOnly> mContentRect;
  RefPtr<ResizeObserverSize> mBorderBoxSize;
  RefPtr<ResizeObserverSize> mContentBoxSize;
};

class ResizeObserverSize final : public nsISupports, public nsWrapperCache {
public:
  NS_DECL_CYCLE_COLLECTING_ISUPPORTS
  NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(ResizeObserverSize)

  // Note: the unit of |aSize| is app unit, and we convert it into css pixel in
  // the public JS APIs.
  ResizeObserverSize(nsISupports* aOwner, const nsSize& aSize,
                     const WritingMode aWM)
      : mOwner(aOwner), mSize(aWM, aSize), mWM(aWM) {
    MOZ_ASSERT(mOwner, "Need a non-null owner");
  }

  nsISupports* GetParentObject() const { return mOwner; }

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

  double InlineSize() const {
    return NSAppUnitsToDoublePixels(mSize.ISize(mWM), AppUnitsPerCSSPixel());
  }

  double BlockSize() const {
    return NSAppUnitsToDoublePixels(mSize.BSize(mWM), AppUnitsPerCSSPixel());
  }

protected:
  ~ResizeObserverSize() = default;

  nsCOMPtr<nsISupports> mOwner;
  const LogicalSize mSize;
  const WritingMode mWM;
};

/**
 * We use ResizeObservation to store and sync the size information of one
 * observed element so we can decide whether an observation should be fired
 * or not.
 */
class ResizeObservation final
  : public nsISupports
  , public nsWrapperCache
  , public LinkedListElement<ResizeObservation>
{
public:
  NS_DECL_CYCLE_COLLECTING_ISUPPORTS
  NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(ResizeObservation)

  ResizeObservation(nsISupports* aOwner,
                    Element* aTarget,
                    ResizeObserverBoxOptions aBox,
                    const WritingMode aWM)
    : mOwner(aOwner)
    , mTarget(aTarget)
    , mObservedBox(aBox)
    , mLastReportedSize(aWM)
    , mLastReportedWM(aWM)
  {
    MOZ_ASSERT(mOwner, "Need a non-null owner");
    MOZ_ASSERT(mTarget, "Need a non-null target element");
  }

  static already_AddRefed<ResizeObservation>
  Constructor(const GlobalObject& aGlobal,
              Element* aTarget,
              ErrorResult& aRv);

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

  nsISupports* GetParentObject() const
  {
    return mOwner;
  }

  Element* Target() const
  {
    return mTarget;
  }

  ResizeObserverBoxOptions BoxOptions() const
  { 
    return mObservedBox;
  }

  /*
   * Returns whether the observed target element size differs from the saved
   * mLastReportedSize.
  */
  bool IsActive() const;

  /*
   * Update current mLastReportedSize with size from aSize.
  */
  void UpdateLastReportedSize(const nsSize& aSize);

protected:
  ~ResizeObservation();

  nsCOMPtr<nsISupports> mOwner;
  nsCOMPtr<Element> mTarget;

  const ResizeObserverBoxOptions mObservedBox;

  // The latest recorded size of observed target.
  // Per the spec, observation.lastReportedSize should be entry.borderBoxSize
  // or entry.contentBoxSize (i.e. logical size), instead of entry.contentRect
  // (i.e. physical rect), so we store this as LogicalSize.
  LogicalSize mLastReportedSize;
  WritingMode mLastReportedWM;
};

} // namespace dom
} // namespace mozilla

#endif // mozilla_dom_ResizeObserver_h