Bug 1852740: add tests for the `fetchpriority` attribute in Link headers. r=necko...
[gecko.git] / dom / base / TimeoutHandler.h
blobce659d762377b26576e06f8c2bada17e36abafe4
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/dom/FunctionBinding.h"
18 namespace mozilla::dom {
20 /**
21 * Utility class for implementing nsITimeoutHandlers, designed to be subclassed.
23 class TimeoutHandler : public nsISupports {
24 public:
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,
30 uint32_t* aColumn);
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() {}
39 protected:
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()
47 nsCString mFileName;
48 uint32_t mLineNo;
49 uint32_t mColumn;
51 private:
52 TimeoutHandler(const TimeoutHandler&) = delete;
53 TimeoutHandler& operator=(const TimeoutHandler&) = delete;
54 TimeoutHandler& operator=(const TimeoutHandler&&) = delete;
57 class ScriptTimeoutHandler : public TimeoutHandler {
58 public:
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 {
66 return false;
68 virtual void GetDescription(nsACString& aOutString) override;
70 protected:
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.
76 nsString mExpr;
79 class CallbackTimeoutHandler final : public TimeoutHandler {
80 public:
81 NS_DECL_CYCLE_COLLECTING_ISUPPORTS
82 NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(CallbackTimeoutHandler)
84 CallbackTimeoutHandler(JSContext* aCx, nsIGlobalObject* aGlobal,
85 Function* aFunction,
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();
94 private:
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