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"
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 // Get the location of the script.
28 // Note: The memory pointed to by aFileName is owned by the
29 // nsITimeoutHandler and should not be freed by the caller.
30 virtual void GetLocation(const char** aFileName
, uint32_t* aLineNo
,
32 // Append a UTF-8 string to aOutString that describes the callback function,
33 // for use in logging or profiler markers.
34 // The string contains the function name and its source location, if
35 // available, in the following format:
36 // "<functionName> (<sourceURL>:<lineNumber>:<columnNumber>)"
37 virtual void GetDescription(nsACString
& aOutString
);
38 virtual void MarkForCC() {}
41 TimeoutHandler() : mFileName(""), mLineNo(0), mColumn(0) {}
42 explicit TimeoutHandler(JSContext
* aCx
);
44 virtual ~TimeoutHandler() = default;
46 // filename, line number and JS language version string of the
47 // caller of setTimeout()
53 TimeoutHandler(const TimeoutHandler
&) = delete;
54 TimeoutHandler
& operator=(const TimeoutHandler
&) = delete;
55 TimeoutHandler
& operator=(const TimeoutHandler
&&) = delete;
58 class ScriptTimeoutHandler
: public TimeoutHandler
{
60 NS_DECL_CYCLE_COLLECTING_ISUPPORTS
61 NS_DECL_CYCLE_COLLECTION_CLASS(ScriptTimeoutHandler
)
63 ScriptTimeoutHandler(JSContext
* aCx
, nsIGlobalObject
* aGlobal
,
64 const nsAString
& aExpression
);
66 MOZ_CAN_RUN_SCRIPT
virtual bool Call(const char* /* unused */) override
{
69 virtual void GetDescription(nsACString
& aOutString
) override
;
72 virtual ~ScriptTimeoutHandler() = default;
74 nsCOMPtr
<nsIGlobalObject
> mGlobal
;
75 // The expression to evaluate or function to call. If mFunction is non-null
76 // it should be used, else use mExpr.
80 class CallbackTimeoutHandler final
: public TimeoutHandler
{
82 NS_DECL_CYCLE_COLLECTING_ISUPPORTS
83 NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(CallbackTimeoutHandler
)
85 CallbackTimeoutHandler(JSContext
* aCx
, nsIGlobalObject
* aGlobal
,
87 nsTArray
<JS::Heap
<JS::Value
>>&& aArguments
);
89 MOZ_CAN_RUN_SCRIPT
virtual bool Call(const char* aExecutionReason
) override
;
90 virtual void MarkForCC() override
;
91 virtual void GetDescription(nsACString
& aOutString
) override
;
93 void ReleaseJSObjects();
96 virtual ~CallbackTimeoutHandler() { ReleaseJSObjects(); }
98 nsCOMPtr
<nsIGlobalObject
> mGlobal
;
99 RefPtr
<Function
> mFunction
;
100 nsTArray
<JS::Heap
<JS::Value
>> mArgs
;
104 } // namespace mozilla
106 #endif // mozilla_dom_timeout_handler_h