ExtensionKitUtils.h (2739B)
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 file, 5 * You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 7 #ifndef mozilla_ipc_ExtensionKitUtils_h 8 #define mozilla_ipc_ExtensionKitUtils_h 9 10 #include <functional> 11 #include <xpc/xpc.h> 12 #include "mozilla/DarwinObjectPtr.h" 13 #include "mozilla/Result.h" 14 #include "mozilla/ResultVariant.h" 15 #include "mozilla/UniquePtr.h" 16 #include "mozilla/ipc/LaunchError.h" 17 18 namespace mozilla::ipc { 19 20 class BEProcessCapabilityGrantDeleter { 21 public: 22 void operator()(void* aGrant) const; 23 }; 24 25 using UniqueBEProcessCapabilityGrant = 26 mozilla::UniquePtr<void, BEProcessCapabilityGrantDeleter>; 27 28 class ExtensionKitProcess { 29 public: 30 enum class Kind { 31 WebContent, 32 Networking, 33 Rendering, 34 }; 35 36 // Called to start the process. The `aCompletion` function may be executed on 37 // a background libdispatch thread. 38 static void StartProcess( 39 Kind aKind, 40 const std::function<void(Result<ExtensionKitProcess, LaunchError>&&)>& 41 aCompletion); 42 43 // Get the kind of process being started. 44 Kind GetKind() const { return mKind; } 45 46 // Make an xpc_connection_t to this process. If an error is encountered, 47 // `aError` will be populated with the error. 48 // 49 // Ownership over the newly created connection is returned to the caller. 50 // The connection is returned in a suspended state, and must be resumed. 51 DarwinObjectPtr<xpc_connection_t> MakeLibXPCConnection(); 52 53 UniqueBEProcessCapabilityGrant GrantForegroundCapability(); 54 55 // Invalidate the process, indicating that it should be cleaned up & 56 // destroyed. 57 void Invalidate(); 58 59 // Explicit copy constructors 60 ExtensionKitProcess(const ExtensionKitProcess&); 61 ExtensionKitProcess& operator=(const ExtensionKitProcess&); 62 63 // Release the object when completed. 64 ~ExtensionKitProcess(); 65 66 private: 67 ExtensionKitProcess(Kind aKind, void* aProcessObject) 68 : mKind(aKind), mProcessObject(aProcessObject) {} 69 70 // Type tag for `mProcessObject`. 71 Kind mKind; 72 73 // This is one of `BEWebContentProcess`, `BENetworkingProcess` or 74 // `BERenderingProcess`. It has been type erased to be usable from C++ code. 75 void* mProcessObject; 76 }; 77 78 enum class ExtensionKitSandboxRevision { 79 // RestrictedSandboxRevision.revision1 80 Revision1, 81 }; 82 83 // Call `applyRestrictedSandbox` on the current ExtensionKit process, if it 84 // supports the given sandbox revision. 85 void LockdownExtensionKitProcess(ExtensionKitSandboxRevision aRevision); 86 87 } // namespace mozilla::ipc 88 89 #endif // mozilla_ipc_ExtensionKitUtils_h