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/dom/FunctionBinding.h"
18 namespace mozilla::dom
{
21 * Utility class for implementing nsITimeoutHandlers, designed to be subclassed.
23 class TimeoutHandler
: public nsISupports
{
25 MOZ_CAN_RUN_SCRIPT
virtual bool Call(const char* /* unused */);
26 // Get the location of the script.
27 // Note: The memory pointed to by aFileName is owned by the
28 // nsITimeoutHandler and should not be freed by the caller.
29 virtual void GetLocation(const char** aFileName
, uint32_t* aLineNo
,
31 // Append a UTF-8 string to aOutString that describes the callback function,
32 // for use in logging or profiler markers.
33 // The string contains the function name and its source location, if
34 // available, in the following format:
35 // "<functionName> (<sourceURL>:<lineNumber>:<columnNumber>)"
36 virtual void GetDescription(nsACString
& aOutString
);
37 virtual void MarkForCC() {}
40 TimeoutHandler() : mFileName(""), mLineNo(0), mColumn(0) {}
41 explicit TimeoutHandler(JSContext
* aCx
);
43 virtual ~TimeoutHandler() = default;
45 // filename, line number and JS language version string of the
46 // caller of setTimeout()
52 TimeoutHandler(const TimeoutHandler
&) = delete;
53 TimeoutHandler
& operator=(const TimeoutHandler
&) = delete;
54 TimeoutHandler
& operator=(const TimeoutHandler
&&) = delete;
57 class ScriptTimeoutHandler
: public TimeoutHandler
{
59 NS_DECL_CYCLE_COLLECTING_ISUPPORTS
60 NS_DECL_CYCLE_COLLECTION_CLASS(ScriptTimeoutHandler
)
62 ScriptTimeoutHandler(JSContext
* aCx
, nsIGlobalObject
* aGlobal
,
63 const nsAString
& aExpression
);
65 MOZ_CAN_RUN_SCRIPT
virtual bool Call(const char* /* unused */) override
{
68 virtual void GetDescription(nsACString
& aOutString
) override
;
71 virtual ~ScriptTimeoutHandler() = default;
73 nsCOMPtr
<nsIGlobalObject
> mGlobal
;
74 // The expression to evaluate or function to call. If mFunction is non-null
75 // it should be used, else use mExpr.
79 class CallbackTimeoutHandler final
: public TimeoutHandler
{
81 NS_DECL_CYCLE_COLLECTING_ISUPPORTS
82 NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(CallbackTimeoutHandler
)
84 CallbackTimeoutHandler(JSContext
* aCx
, nsIGlobalObject
* aGlobal
,
86 nsTArray
<JS::Heap
<JS::Value
>>&& aArguments
);
88 MOZ_CAN_RUN_SCRIPT
virtual bool Call(const char* aExecutionReason
) override
;
89 virtual void MarkForCC() override
;
90 virtual void GetDescription(nsACString
& aOutString
) override
;
92 void ReleaseJSObjects();
95 virtual ~CallbackTimeoutHandler() { ReleaseJSObjects(); }
97 nsCOMPtr
<nsIGlobalObject
> mGlobal
;
98 RefPtr
<Function
> mFunction
;
99 nsTArray
<JS::Heap
<JS::Value
>> mArgs
;
102 } // namespace mozilla::dom
104 #endif // mozilla_dom_timeout_handler_h