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 js_loader_BaseLoadContext_h
8 #define js_loader_BaseLoadContext_h
10 #include "js/loader/ScriptLoadRequest.h"
11 #include "nsIStringBundle.h"
13 namespace mozilla::dom
{
14 class ScriptLoadContext
;
15 class WorkerLoadContext
;
16 class WorkletLoadContext
;
17 } // namespace mozilla::dom
19 namespace mozilla::loader
{
20 class SyncLoadContext
;
23 namespace JS::loader
{
25 class ScriptLoadRequest
;
30 * LoadContexts augment the loading of a ScriptLoadRequest. This class
31 * is used as a base for all LoadContexts, and provides shared functionality.
35 enum class ContextKind
{ Window
, Sync
, Worker
, Worklet
};
37 class LoadContextBase
: public nsISupports
{
42 virtual ~LoadContextBase() = default;
45 NS_DECL_CYCLE_COLLECTING_ISUPPORTS
46 NS_DECL_CYCLE_COLLECTION_CLASS(LoadContextBase
)
48 explicit LoadContextBase(ContextKind kind
);
50 void SetRequest(JS::loader::ScriptLoadRequest
* aRequest
);
52 // Used to output a string for the Gecko Profiler.
53 virtual void GetProfilerLabel(nsACString
& aOutString
);
55 // Whether this is a preload, for contexts that support them.
56 virtual bool IsPreload() const { return false; }
58 // Casting to the different contexts
59 bool IsWindowContext() const { return mKind
== ContextKind::Window
; }
60 mozilla::dom::ScriptLoadContext
* AsWindowContext();
62 bool IsSyncContext() const { return mKind
== ContextKind::Sync
; }
63 mozilla::loader::SyncLoadContext
* AsSyncContext();
65 bool IsWorkerContext() const { return mKind
== ContextKind::Worker
; }
66 mozilla::dom::WorkerLoadContext
* AsWorkerContext();
68 bool IsWorkletContext() const { return mKind
== ContextKind::Worklet
; }
69 mozilla::dom::WorkletLoadContext
* AsWorkletContext();
71 RefPtr
<JS::loader::ScriptLoadRequest
> mRequest
;
74 } // namespace JS::loader
76 #endif // js_loader_BaseLoadContext_h