Bug 1942239 - Add option to explicitly enable incremental origin initialization in...
[gecko.git] / toolkit / xre / Bootstrap.h
blob657d8bccb309a384b47d78f9e06afce48b022f75
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 /**
7 * This file represents the only external interface exposed from libxul. It
8 * is used by the various stub binaries (nsBrowserApp, xpcshell,
9 * plugin-container) to initialize XPCOM and start their main loop.
12 #ifndef mozilla_Bootstrap_h
13 #define mozilla_Bootstrap_h
15 #include "mozilla/Maybe.h"
16 #include "mozilla/ResultVariant.h"
17 #include "mozilla/UniquePtr.h"
18 #include "mozilla/UniquePtrExtensions.h"
19 #include "mozilla/Variant.h"
20 #include "nscore.h"
21 #include "nsXULAppAPI.h"
23 #ifdef MOZ_WIDGET_ANDROID
24 # include "jni.h"
25 #endif
27 #if defined(XP_WIN) && defined(MOZ_SANDBOX)
28 namespace sandbox {
29 class BrokerServices;
31 #endif
33 namespace mozilla {
35 struct StaticXREAppData;
37 struct BootstrapConfig {
38 #if defined(XP_WIN) && defined(MOZ_SANDBOX)
39 /* Chromium sandbox BrokerServices. */
40 sandbox::BrokerServices* sandboxBrokerServices;
41 #endif
42 /* Pointer to static XRE AppData from application.ini.h */
43 const StaticXREAppData* appData;
44 /* When the pointer above is null, points to the (string) path of an
45 * application.ini file to open and parse.
46 * When the pointer above is non-null, may indicate the directory where
47 * application files are, relative to the XRE. */
48 const char* appDataPath;
51 /**
52 * This class is virtual abstract so that using it does not require linking
53 * any symbols. The singleton instance of this class is obtained from the
54 * exported method XRE_GetBootstrap.
56 class Bootstrap {
57 protected:
58 Bootstrap() {}
60 // Because of allocator mismatches, code outside libxul shouldn't delete a
61 // Bootstrap instance. Use Dispose().
62 virtual ~Bootstrap() {}
64 /**
65 * Destroy and deallocate this Bootstrap instance.
67 virtual void Dispose() = 0;
69 /**
70 * Helper class to use with UniquePtr.
72 class BootstrapDelete {
73 public:
74 constexpr BootstrapDelete() {}
75 void operator()(Bootstrap* aPtr) const { aPtr->Dispose(); }
78 public:
79 typedef mozilla::UniquePtr<Bootstrap, BootstrapDelete> UniquePtr;
81 virtual void NS_LogInit() = 0;
83 virtual void NS_LogTerm() = 0;
85 virtual void XRE_TelemetryAccumulate(int aID, uint32_t aSample) = 0;
87 virtual void XRE_StartupTimelineRecord(int aEvent,
88 mozilla::TimeStamp aWhen) = 0;
90 virtual int XRE_main(int argc, char* argv[],
91 const BootstrapConfig& aConfig) = 0;
93 virtual void XRE_StopLateWriteChecks() = 0;
95 virtual int XRE_XPCShellMain(int argc, char** argv, char** envp,
96 const XREShellData* aShellData) = 0;
98 virtual nsresult XRE_InitChildProcess(int argc, char* argv[],
99 const XREChildData* aChildData) = 0;
101 virtual void XRE_EnableSameExecutableForContentProc() = 0;
103 #ifdef MOZ_WIDGET_ANDROID
104 virtual void XRE_SetGeckoThreadEnv(JNIEnv* aEnv) = 0;
106 virtual void XRE_SetAndroidChildFds(JNIEnv* aEnv, jintArray aFds) = 0;
107 # ifdef MOZ_PROFILE_GENERATE
108 virtual void XRE_WriteLLVMProfData() = 0;
109 # endif
110 #endif
112 #ifdef LIBFUZZER
113 virtual void XRE_LibFuzzerSetDriver(LibFuzzerDriver aDriver) = 0;
114 #endif
116 #ifdef MOZ_ENABLE_FORKSERVER
117 virtual int XRE_ForkServer(int* argc, char*** argv) = 0;
118 #endif
121 enum class LibLoadingStrategy {
122 NoReadAhead,
123 ReadAhead,
126 #if defined(XP_WIN)
127 using DLErrorType = unsigned long; // (DWORD)
128 #else
129 using DLErrorType = UniqueFreePtr<char>;
130 #endif
132 using BootstrapError = Variant<nsresult, DLErrorType>;
134 using BootstrapResult = ::mozilla::Result<Bootstrap::UniquePtr, BootstrapError>;
137 * Creates and returns the singleton instance of the bootstrap object.
138 * @param `b` is an outparam. We use a parameter and not a return value
139 * because MSVC doesn't let us return a c++ class from a function with
140 * "C" linkage. On failure this will be null.
141 * @note This function may only be called once and will crash if called again.
143 #ifdef XPCOM_GLUE
144 typedef void (*GetBootstrapType)(Bootstrap::UniquePtr&);
145 BootstrapResult GetBootstrap(
146 const char* aXPCOMFile = nullptr,
147 LibLoadingStrategy aLibLoadingStrategy = LibLoadingStrategy::NoReadAhead);
148 #else
149 extern "C" NS_EXPORT void NS_FROZENCALL
150 XRE_GetBootstrap(Bootstrap::UniquePtr& b);
152 inline BootstrapResult GetBootstrap(
153 const char* aXPCOMFile = nullptr,
154 LibLoadingStrategy aLibLoadingStrategy = LibLoadingStrategy::NoReadAhead) {
155 Bootstrap::UniquePtr bootstrap;
156 XRE_GetBootstrap(bootstrap);
157 return bootstrap;
159 #endif
161 #if defined(XP_WIN) && defined(_M_X64) && defined(MOZ_DIAGNOSTIC_ASSERT_ENABLED)
162 extern "C" NS_EXPORT bool XRE_CheckBlockScopeStaticVarInit(uint32_t* aTlsIndex);
163 #endif // XP_WIN && _M_X64 && MOZ_DIAGNOSTIC_ASSERT_ENABLED
165 } // namespace mozilla
167 #endif // mozilla_Bootstrap_h