Backed out changeset b462e7b742d8 (bug 1908261) for causing multiple reftest failures...
[gecko.git] / dom / base / TimeoutHandler.h
blob28227da2de1561165f703187efa3d254ebb89624
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
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #ifndef mozilla_dom_timeout_handler_h
8 #define mozilla_dom_timeout_handler_h
10 #include "nsCOMPtr.h"
11 #include "nsIGlobalObject.h"
12 #include "nsISupports.h"
13 #include "nsCycleCollectionParticipant.h"
14 #include "nsString.h"
15 #include "mozilla/Attributes.h"
16 #include "mozilla/SourceLocation.h"
17 #include "mozilla/dom/FunctionBinding.h"
19 namespace mozilla::dom {
21 /**
22 * Utility class for implementing nsITimeoutHandlers, designed to be subclassed.
24 class TimeoutHandler : public nsISupports {
25 public:
26 MOZ_CAN_RUN_SCRIPT virtual bool Call(const char* /* unused */);
27 // Append a UTF-8 string to aOutString that describes the callback function,
28 // for use in logging or profiler markers.
29 // The string contains the function name and its source location, if
30 // available, in the following format:
31 // "<functionName> (<sourceURL>:<lineNumber>:<columnNumber>)"
32 virtual void GetDescription(nsACString& aOutString);
33 virtual void MarkForCC() {}
35 protected:
36 TimeoutHandler() = default;
37 explicit TimeoutHandler(JSContext* aCx)
38 : mCaller(JSCallingLocation::Get(aCx)) {}
40 virtual ~TimeoutHandler() = default;
42 // filename, line number and JS language version string of the
43 // caller of setTimeout()
44 const JSCallingLocation mCaller = {};
46 private:
47 TimeoutHandler(const TimeoutHandler&) = delete;
48 TimeoutHandler& operator=(const TimeoutHandler&) = delete;
49 TimeoutHandler& operator=(const TimeoutHandler&&) = delete;
52 class ScriptTimeoutHandler : public TimeoutHandler {
53 public:
54 NS_DECL_CYCLE_COLLECTING_ISUPPORTS
55 NS_DECL_CYCLE_COLLECTION_CLASS(ScriptTimeoutHandler)
57 ScriptTimeoutHandler(JSContext* aCx, nsIGlobalObject* aGlobal,
58 const nsAString& aExpression);
60 MOZ_CAN_RUN_SCRIPT virtual bool Call(const char* /* unused */) override {
61 return false;
63 virtual void GetDescription(nsACString& aOutString) override;
65 protected:
66 virtual ~ScriptTimeoutHandler() = default;
68 nsCOMPtr<nsIGlobalObject> mGlobal;
69 // The expression to evaluate or function to call. If mFunction is non-null
70 // it should be used, else use mExpr.
71 nsString mExpr;
74 class CallbackTimeoutHandler final : public TimeoutHandler {
75 public:
76 NS_DECL_CYCLE_COLLECTING_ISUPPORTS
77 NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(CallbackTimeoutHandler)
79 CallbackTimeoutHandler(JSContext* aCx, nsIGlobalObject* aGlobal,
80 Function* aFunction,
81 nsTArray<JS::Heap<JS::Value>>&& aArguments);
83 MOZ_CAN_RUN_SCRIPT virtual bool Call(const char* aExecutionReason) override;
84 virtual void MarkForCC() override;
85 virtual void GetDescription(nsACString& aOutString) override;
87 void ReleaseJSObjects();
89 private:
90 virtual ~CallbackTimeoutHandler() { ReleaseJSObjects(); }
92 nsCOMPtr<nsIGlobalObject> mGlobal;
93 RefPtr<Function> mFunction;
94 nsTArray<JS::Heap<JS::Value>> mArgs;
97 } // namespace mozilla::dom
99 #endif // mozilla_dom_timeout_handler_h