XPCSelfHostedShmem.h (3427B)
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/. */ 6 7 #ifndef xpcselfhostedshmem_h___ 8 #define xpcselfhostedshmem_h___ 9 10 #include "mozilla/Span.h" 11 #include "mozilla/StaticPtr.h" 12 #include "mozilla/ipc/SharedMemoryHandle.h" 13 #include "mozilla/ipc/SharedMemoryMapping.h" 14 #include "nsIMemoryReporter.h" 15 #include "nsIObserver.h" 16 #include "nsIThread.h" 17 18 namespace xpc { 19 20 // This class is a singleton which holds a file-mapping of the Self Hosted 21 // Stencil XDR, to be shared by the parent process with all the child processes. 22 // 23 // It is either initialized by the parent process by monitoring when the Self 24 // hosted stencil is produced, or by the content process by reading a shared 25 // memory-mapped file. 26 class SelfHostedShmem final : public nsIMemoryReporter { 27 public: 28 NS_DECL_THREADSAFE_ISUPPORTS 29 NS_DECL_NSIMEMORYREPORTER 30 31 // NOTE: This type is identical to JS::SelfHostedCache, but we repeat it to 32 // avoid including JS engine API in ipc code. 33 using ContentType = mozilla::Span<const uint8_t>; 34 35 static SelfHostedShmem& GetSingleton(); 36 37 static void SetSelfHostedUseSharedMemory(bool aSelfHostedUseSharedMemory) { 38 sSelfHostedUseSharedMemory = aSelfHostedUseSharedMemory; 39 } 40 41 static bool SelfHostedUseSharedMemory() { return sSelfHostedUseSharedMemory; } 42 43 // Initialize this singleton with the content of the Self-hosted Stencil XDR. 44 // This will be used to initialize the shared memory which would hold a copy. 45 // 46 // This function is not thread-safe and should be call at most once and from 47 // the main thread. 48 void InitFromParent(ContentType aXdr); 49 50 // Initialize this singleton with the content coming from the parent process, 51 // using a file handle which maps to the memory pages of the parent process. 52 // 53 // This function is not thread-safe and should be call at most once and from 54 // the main thread. 55 [[nodiscard]] bool InitFromChild( 56 mozilla::ipc::ReadOnlySharedMemoryHandle aHandle); 57 58 // Return a span over the read-only XDR content of the self-hosted stencil. 59 ContentType Content() const; 60 61 // Return the file handle which is under which the content is mapped. 62 const mozilla::ipc::ReadOnlySharedMemoryHandle& Handle() const; 63 64 // Register this class to the memory reporter service. 65 void InitMemoryReporter(); 66 67 // Unregister this class from the memory reporter service, and delete the 68 // memory associated with the shared memory. As the memory is borrowed by the 69 // JS engine, this function should be called after JS_Shutdown. 70 // 71 // NOTE: This is not using the Observer service which would call Shutdown 72 // while JS code might still be running during shutdown process. 73 static void Shutdown(); 74 75 private: 76 SelfHostedShmem() = default; 77 ~SelfHostedShmem() = default; 78 79 // Mirrored value of javascript.options.self_hosted.use_shared_memory. 80 static bool sSelfHostedUseSharedMemory; 81 static mozilla::StaticRefPtr<SelfHostedShmem> sSelfHostedXdr; 82 83 // Read-only shared memory used by JS runtime initialization. 84 mozilla::ipc::ReadOnlySharedMemoryHandle mHandle; 85 mozilla::ipc::ReadOnlySharedMemoryMapping mMem; 86 }; 87 88 } // namespace xpc 89 90 #endif // !xpcselfhostedshmem_h___