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
11 #include "nsIGlobalObject.h"
12 #include "nsISupports.h"
13 #include "nsCycleCollectionParticipant.h"
15 #include "mozilla/Attributes.h"
16 #include "mozilla/SourceLocation.h"
17 #include "mozilla/dom/FunctionBinding.h"
19 namespace mozilla::dom
{
22 * Utility class for implementing nsITimeoutHandlers, designed to be subclassed.
24 class TimeoutHandler
: public nsISupports
{
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() {}
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
= {};
47 TimeoutHandler(const TimeoutHandler
&) = delete;
48 TimeoutHandler
& operator=(const TimeoutHandler
&) = delete;
49 TimeoutHandler
& operator=(const TimeoutHandler
&&) = delete;
52 class ScriptTimeoutHandler
: public TimeoutHandler
{
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
{
63 virtual void GetDescription(nsACString
& aOutString
) override
;
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.
74 class CallbackTimeoutHandler final
: public TimeoutHandler
{
76 NS_DECL_CYCLE_COLLECTING_ISUPPORTS
77 NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(CallbackTimeoutHandler
)
79 CallbackTimeoutHandler(JSContext
* aCx
, nsIGlobalObject
* aGlobal
,
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();
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