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/. */
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"
21 #include "nsXULAppAPI.h"
23 #ifdef MOZ_WIDGET_ANDROID
27 #if defined(XP_WIN) && defined(MOZ_SANDBOX)
35 struct StaticXREAppData
;
37 struct BootstrapConfig
{
38 #if defined(XP_WIN) && defined(MOZ_SANDBOX)
39 /* Chromium sandbox BrokerServices. */
40 sandbox::BrokerServices
* sandboxBrokerServices
;
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
;
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.
60 // Because of allocator mismatches, code outside libxul shouldn't delete a
61 // Bootstrap instance. Use Dispose().
62 virtual ~Bootstrap() {}
65 * Destroy and deallocate this Bootstrap instance.
67 virtual void Dispose() = 0;
70 * Helper class to use with UniquePtr.
72 class BootstrapDelete
{
74 constexpr BootstrapDelete() {}
75 void operator()(Bootstrap
* aPtr
) const { aPtr
->Dispose(); }
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;
113 virtual void XRE_LibFuzzerSetDriver(LibFuzzerDriver aDriver
) = 0;
116 #ifdef MOZ_ENABLE_FORKSERVER
117 virtual int XRE_ForkServer(int* argc
, char*** argv
) = 0;
121 enum class LibLoadingStrategy
{
127 using DLErrorType
= unsigned long; // (DWORD)
129 using DLErrorType
= UniqueFreePtr
<char>;
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.
144 typedef void (*GetBootstrapType
)(Bootstrap::UniquePtr
&);
145 BootstrapResult
GetBootstrap(
146 const char* aXPCOMFile
= nullptr,
147 LibLoadingStrategy aLibLoadingStrategy
= LibLoadingStrategy::NoReadAhead
);
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
);
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