xref: /aosp_15_r20/external/cronet/base/memory/shared_memory_switch.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2024 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef BASE_MEMORY_SHARED_MEMORY_SWITCH_H_
6 #define BASE_MEMORY_SHARED_MEMORY_SWITCH_H_
7 
8 #include <string_view>
9 
10 #include "base/base_export.h"
11 #include "base/memory/read_only_shared_memory_region.h"
12 #include "base/memory/unsafe_shared_memory_region.h"
13 #include "base/types/expected.h"
14 #include "build/blink_buildflags.h"
15 #include "build/build_config.h"
16 
17 #if !BUILDFLAG(USE_BLINK)
18 #error "This is only intended for platforms that use blink."
19 #endif
20 
21 #if BUILDFLAG(IS_APPLE)
22 #include "base/mac/mach_port_rendezvous.h"
23 #endif
24 
25 #if BUILDFLAG(IS_POSIX) && !BUILDFLAG(IS_APPLE)
26 #include "base/files/platform_file.h"
27 #include "base/posix/global_descriptors.h"
28 #endif
29 
30 namespace base {
31 
32 class CommandLine;
33 struct LaunchOptions;
34 
35 namespace shared_memory {
36 
37 // Indicates failure modes of deserializing a shared memory switch value.
38 enum class SharedMemoryError {
39   kNoError,
40   kUnexpectedTokensCount,
41   kParseInt0Failed,
42   kParseInt4Failed,
43   kUnexpectedHandleType,
44   kInvalidHandle,
45   kGetFDFailed,
46   kDeserializeGUIDFailed,
47   kDeserializeFailed,
48   kCreateTrialsFailed,
49   kUnexpectedSize,
50 };
51 
52 // Updates `command_line` and `launch_options` to use `switch_name` to pass
53 // `read_only_memory_region` to child process that is about to be launched.
54 // This should be called in the parent process as a part of setting up the
55 // launch conditions of the child. This call will update the `command_line`
56 // and `launch_options`. On posix, where we prefer to use a zygote instead of
57 // using the launch_options to launch a new process, the platform
58 // `descriptor_to_share` is returned. The caller is expected to transmit the
59 // descriptor to the launch flow for the zygote.
60 BASE_EXPORT void AddToLaunchParameters(
61     std::string_view switch_name,
62     ReadOnlySharedMemoryRegion read_only_memory_region,
63 #if BUILDFLAG(IS_APPLE)
64     MachPortsForRendezvous::key_type rendezvous_key,
65 #elif BUILDFLAG(IS_POSIX)
66     GlobalDescriptors::Key descriptor_key,
67     ScopedFD& out_descriptor_to_share,
68 #endif
69     CommandLine* command_line,
70     LaunchOptions* launch_options);
71 
72 // Updates `command_line` and `launch_options` to use `switch_name` to pass
73 // `unsafe_memory_region` to a child process that is about to be launched.
74 // This should be called in the parent process as a part of setting up the
75 // launch conditions of the child. This call will update the `command_line`
76 // and `launch_options`. On posix, where we prefer to use a zygote instead of
77 // using the launch_options to launch a new process, the platform
78 // `descriptor_to_share` is returned. The caller is expected to transmit the
79 // descriptor to the launch flow for the zygote.
80 BASE_EXPORT void AddToLaunchParameters(
81     std::string_view switch_name,
82     UnsafeSharedMemoryRegion unsafe_memory_region,
83 #if BUILDFLAG(IS_APPLE)
84     MachPortsForRendezvous::key_type rendezvous_key,
85 #elif BUILDFLAG(IS_POSIX)
86     GlobalDescriptors::Key descriptor_key,
87     ScopedFD& out_descriptor_to_share,
88 #endif
89     CommandLine* command_line,
90     LaunchOptions* launch_options);
91 
92 // Returns an UnsafeSharedMemoryRegion deserialized from `switch_value`.
93 BASE_EXPORT expected<UnsafeSharedMemoryRegion, SharedMemoryError>
94 UnsafeSharedMemoryRegionFrom(std::string_view switch_value);
95 
96 // Returns an ReadOnlySharedMemoryRegion deserialized from `switch_value`.
97 BASE_EXPORT expected<ReadOnlySharedMemoryRegion, SharedMemoryError>
98 ReadOnlySharedMemoryRegionFrom(std::string_view switch_value);
99 
100 }  // namespace shared_memory
101 }  // namespace base
102 
103 #endif  // BASE_MEMORY_SHARED_MEMORY_SWITCH_H_
104