Bug 1917491 - Part 3: Introduce call-like syntax for resource disposal in DisposableS...
[gecko.git] / js / xpconnect / src / XPCSelfHostedShmem.h
blob0fcb1ed05c016cf3bf414248117bfbb08bc64097
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 xpcselfhostedshmem_h___
8 #define xpcselfhostedshmem_h___
10 #include "base/shared_memory.h"
11 #include "mozilla/UniquePtr.h"
12 #include "mozilla/UniquePtrExtensions.h"
13 #include "mozilla/Span.h"
14 #include "mozilla/StaticPtr.h"
15 #include "nsIMemoryReporter.h"
16 #include "nsIObserver.h"
17 #include "nsIThread.h"
19 namespace xpc {
21 // This class is a singleton which holds a file-mapping of the Self Hosted
22 // Stencil XDR, to be shared by the parent process with all the child processes.
24 // It is either initialized by the parent process by monitoring when the Self
25 // hosted stencil is produced, or by the content process by reading a shared
26 // memory-mapped file.
27 class SelfHostedShmem final : public nsIMemoryReporter {
28 public:
29 NS_DECL_THREADSAFE_ISUPPORTS
30 NS_DECL_NSIMEMORYREPORTER
32 // NOTE: This type is identical to JS::SelfHostedCache, but we repeat it to
33 // avoid including JS engine API in ipc code.
34 using ContentType = mozilla::Span<const uint8_t>;
36 static SelfHostedShmem& GetSingleton();
38 // Initialize this singleton with the content of the Self-hosted Stencil XDR.
39 // This will be used to initialize the shared memory which would hold a copy.
41 // This function is not thread-safe and should be call at most once and from
42 // the main thread.
43 void InitFromParent(ContentType aXdr);
45 // Initialize this singleton with the content coming from the parent process,
46 // using a file handle which maps to the memory pages of the parent process.
48 // This function is not thread-safe and should be call at most once and from
49 // the main thread.
50 [[nodiscard]] bool InitFromChild(base::SharedMemoryHandle aHandle,
51 size_t aLen);
53 // Return a span over the read-only XDR content of the self-hosted stencil.
54 ContentType Content() const;
56 // Return the file handle which is under which the content is mapped.
57 const mozilla::UniqueFileHandle& Handle() const;
59 // Register this class to the memory reporter service.
60 void InitMemoryReporter();
62 // Unregister this class from the memory reporter service, and delete the
63 // memory associated with the shared memory. As the memory is borrowed by the
64 // JS engine, this function should be called after JS_Shutdown.
66 // NOTE: This is not using the Observer service which would call Shutdown
67 // while JS code might still be running during shutdown process.
68 static void Shutdown();
70 private:
71 SelfHostedShmem() = default;
72 ~SelfHostedShmem() = default;
74 static mozilla::StaticRefPtr<SelfHostedShmem> sSelfHostedXdr;
76 // read-only file Handle used to transfer from the parent process to content
77 // processes.
78 mozilla::UniqueFileHandle mHandle;
80 // Shared memory used by JS runtime initialization.
81 mozilla::UniquePtr<base::SharedMemory> mMem;
83 // Length of the content within the shared memory.
84 size_t mLen = 0;
87 } // namespace xpc
89 #endif // !xpcselfhostedshmem_h___