Bug 1660051 [wpt PR 25111] - Origin isolation: expand getter test coverage, a=testonly
[gecko.git] / dom / base / TimeoutHandler.h
blobd86473f54546c2c806afa6034308b579df6cd13d
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 {
19 namespace 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 // 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,
31 uint32_t* aColumn);
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() {}
40 protected:
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()
48 nsCString mFileName;
49 uint32_t mLineNo;
50 uint32_t mColumn;
52 private:
53 TimeoutHandler(const TimeoutHandler&) = delete;
54 TimeoutHandler& operator=(const TimeoutHandler&) = delete;
55 TimeoutHandler& operator=(const TimeoutHandler&&) = delete;
58 class ScriptTimeoutHandler : public TimeoutHandler {
59 public:
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 {
67 return false;
69 virtual void GetDescription(nsACString& aOutString) override;
71 protected:
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.
77 nsString mExpr;
80 class CallbackTimeoutHandler final : public TimeoutHandler {
81 public:
82 NS_DECL_CYCLE_COLLECTING_ISUPPORTS
83 NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(CallbackTimeoutHandler)
85 CallbackTimeoutHandler(JSContext* aCx, nsIGlobalObject* aGlobal,
86 Function* aFunction,
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();
95 private:
96 virtual ~CallbackTimeoutHandler() { ReleaseJSObjects(); }
98 nsCOMPtr<nsIGlobalObject> mGlobal;
99 RefPtr<Function> mFunction;
100 nsTArray<JS::Heap<JS::Value>> mArgs;
103 } // namespace dom
104 } // namespace mozilla
106 #endif // mozilla_dom_timeout_handler_h