summaryrefslogtreecommitdiffstats
path: root/layout/base/nsRefreshObservers.h
blob: f1516cc4c1a094f1c248ac885fe23a1a1090680a (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
/* -*- 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/. */

/*
 * Code to notify things that animate before a refresh, at an appropriate
 * refresh rate.  (Perhaps temporary, until replaced by compositor.)
 */

#ifndef LAYOUT_BASE_NSREFRESHOBSERVERS_H_
#define LAYOUT_BASE_NSREFRESHOBSERVERS_H_

#include <functional>

#include "mozilla/Attributes.h"
#include "mozilla/TimeStamp.h"
#include "nsISupports.h"

class nsPresContext;

namespace mozilla {
class AnimationEventDispatcher;
class PendingFullscreenEvent;
class PresShell;
class RefreshDriverTimer;
}  // namespace mozilla

/**
 * An abstract base class to be implemented by callers wanting to be
 * notified at refresh times.  When nothing needs to be painted, callers
 * may not be notified.
 */
class nsARefreshObserver {
 public:
  // AddRef and Release signatures that match nsISupports.  Implementors
  // must implement reference counting, and those that do implement
  // nsISupports will already have methods with the correct signature.
  //
  // The refresh driver does NOT hold references to refresh observers
  // except while it is notifying them.
  NS_INLINE_DECL_PURE_VIRTUAL_REFCOUNTING

  MOZ_CAN_RUN_SCRIPT virtual void WillRefresh(mozilla::TimeStamp aTime) = 0;

#ifdef DEBUG
  // Count of the active registrations we currently have with nsRefreshDrivers.
  // This is incremented in nsRefreshDriver::AddRefreshObserver, decremented in
  // nsRefreshDriver::RemoveRefreshObserver. No other code should modify this.
  // We expect this to be 0 when we're destructed.
  int32_t mRegistrationCount = 0;

  // Virtual destructor to enforce the mRegistrationCount invariant.
  virtual ~nsARefreshObserver() {
    MOZ_ASSERT(mRegistrationCount == 0,
               "Refresh observer AddRefreshObserver/RemoveRefreshObserver "
               "calls should have balanced out to zero");
  }
#endif  // DEBUG
};

/**
 * An abstract base class to be implemented by callers wanting to be notified
 * when the observing refresh driver updated mMostRecentRefresh due to active
 * timer changes. Callers must ensure an observer is removed before it is
 * destroyed.
 */
class nsATimerAdjustmentObserver {
 public:
  virtual void NotifyTimerAdjusted(mozilla::TimeStamp aTime) = 0;
};

/**
 * An abstract base class to be implemented by callers wanting to be notified
 * that a refresh has occurred. Callers must ensure an observer is removed
 * before it is destroyed.
 */
class nsAPostRefreshObserver {
 public:
  virtual void DidRefresh() = 0;
};

namespace mozilla {

/**
 * A wrapper for nsAPostRefreshObserver that's refcounted and might unregister
 * itself after firing.
 *
 * Note, while the observer unregisters itself, the registering still needs to
 * be done by the caller.
 */
class ManagedPostRefreshObserver : public nsAPostRefreshObserver {
 public:
  // Whether the post-refresh observer should be unregistered after it has
  // fired.
  enum class Unregister : bool { No, Yes };
  using Action = std::function<Unregister(bool aWasCanceled)>;
  NS_INLINE_DECL_REFCOUNTING(ManagedPostRefreshObserver)
  ManagedPostRefreshObserver(nsPresContext*, Action&&);
  explicit ManagedPostRefreshObserver(nsPresContext*);
  void DidRefresh() override;
  void Cancel();

 protected:
  virtual ~ManagedPostRefreshObserver();
  RefPtr<nsPresContext> mPresContext;
  Action mAction;
};

}  // namespace mozilla

#endif