OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef MOJO_EDK_EMBEDDER_PLATFORM_CHANNEL_PAIR_H_ | 5 #ifndef MOJO_EDK_EMBEDDER_PLATFORM_CHANNEL_PAIR_H_ |
6 #define MOJO_EDK_EMBEDDER_PLATFORM_CHANNEL_PAIR_H_ | 6 #define MOJO_EDK_EMBEDDER_PLATFORM_CHANNEL_PAIR_H_ |
7 | 7 |
8 #include <string> | |
9 | |
10 #include "base/process/launch.h" | |
11 #include "mojo/edk/platform/scoped_platform_handle.h" | 8 #include "mojo/edk/platform/scoped_platform_handle.h" |
12 #include "mojo/public/cpp/system/macros.h" | |
13 | 9 |
14 namespace mojo { | 10 namespace mojo { |
15 namespace embedder { | 11 namespace embedder { |
16 | 12 |
17 // It would be nice to refactor base/process/launch.h to have a more platform- | 13 // A helper class for creating a pair of |PlatformHandle|s that are connected by |
18 // independent way of representing handles that are passed to child processes. | 14 // a suitable (platform-specific) bidirectional "pipe" (e.g., Unix domain |
19 using HandlePassingInformation = base::FileHandleMappingVector; | 15 // socket). The resulting handles can then be used in the same process (e.g., in |
20 | 16 // tests) or between processes. |
21 // This is used to create a pair of |PlatformHandle|s that are connected by a | |
22 // suitable (platform-specific) bidirectional "pipe" (e.g., Unix domain socket). | |
23 // The resulting handles can then be used in the same process (e.g., in tests) | |
24 // or between processes. (The "server" handle is the one that will be used in | |
25 // the process that created the pair, whereas the "client" handle is the one | |
26 // that will be used in a different process.) | |
27 // | |
28 // This class provides facilities for passing the client handle to a child | |
29 // process. The parent should call |PrepareToPassClientHandlelToChildProcess()| | |
30 // to get the data needed to do this, spawn the child using that data, and then | |
31 // call |ChildProcessLaunched()|. | |
32 // | |
33 // Note: |PlatformChannelPair()|, |PassClientHandleFromParentProcess()| and | |
34 // |PrepareToPassClientHandleToChildProcess()| have platform-specific | |
35 // implementations. | |
36 // | 17 // |
37 // Note: On POSIX platforms, to write to the "pipe", use | 18 // Note: On POSIX platforms, to write to the "pipe", use |
38 // |PlatformChannel{Write,Writev}()| (from platform_channel_utils.h) instead of | 19 // |PlatformChannel{Write,Writev}()| (from platform_channel_utils.h) instead of |
39 // |write()|, |writev()|, etc. Otherwise, you have to worry about platform | 20 // |write()|, |writev()|, etc. Otherwise, you have to worry about platform |
40 // differences in suppressing |SIGPIPE|. | 21 // differences in suppressing |SIGPIPE|. |
41 class PlatformChannelPair { | 22 class PlatformChannelPair { |
42 public: | 23 public: |
43 PlatformChannelPair(); | 24 PlatformChannelPair(); |
44 ~PlatformChannelPair(); | 25 ~PlatformChannelPair(); |
45 | 26 |
46 platform::ScopedPlatformHandle PassServerHandle(); | 27 platform::ScopedPlatformHandle handle0; |
47 | 28 platform::ScopedPlatformHandle handle1; |
48 // For in-process use (e.g., in tests or to pass over another channel). | |
49 platform::ScopedPlatformHandle PassClientHandle(); | |
50 | |
51 // To be called in the child process, after the parent process called | |
52 // |PrepareToPassClientHandleToChildProcess()| and launched the child (using | |
53 // the provided data), to create a client handle connected to the server | |
54 // handle (in the parent process). |string_from_parent| should be the string | |
55 // that was produced (in the parent process) by | |
56 // |PrepareToPassClientHandleToChildProcess()|. | |
57 static platform::ScopedPlatformHandle PassClientHandleFromParentProcess( | |
58 const std::string& string_from_parent); | |
59 | |
60 // Prepares to pass the client channel to a new child process, to be launched | |
61 // using |LaunchProcess()| (from base/launch.h). |*string_for_child| will be | |
62 // set to a string that should be passed to the child process and which should | |
63 // be given (in the child ) to |PassClientHandleFromParentProcess()|. Also | |
64 // modifies |*handle_passing_info| as needed. | |
65 void PrepareToPassClientHandleToChildProcess( | |
66 std::string* string_for_child, | |
67 HandlePassingInformation* handle_passing_info) const; | |
68 | |
69 // To be called once the child process has been successfully launched, to do | |
70 // any cleanup necessary. | |
71 void ChildProcessLaunched(); | |
72 | |
73 private: | |
74 static const char kMojoPlatformChannelHandleSwitch[]; | |
75 | |
76 platform::ScopedPlatformHandle server_handle_; | |
77 platform::ScopedPlatformHandle client_handle_; | |
78 | |
79 MOJO_DISALLOW_COPY_AND_ASSIGN(PlatformChannelPair); | |
80 }; | 29 }; |
81 | 30 |
82 } // namespace embedder | 31 } // namespace embedder |
83 } // namespace mojo | 32 } // namespace mojo |
84 | 33 |
85 #endif // MOJO_EDK_EMBEDDER_PLATFORM_CHANNEL_PAIR_H_ | 34 #endif // MOJO_EDK_EMBEDDER_PLATFORM_CHANNEL_PAIR_H_ |
OLD | NEW |