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_SYSTEM_PLATFORM_CHANNEL_PAIR_H_ | 5 #ifndef MOJO_SYSTEM_PLATFORM_CHANNEL_PAIR_H_ |
6 #define MOJO_SYSTEM_PLATFORM_CHANNEL_PAIR_H_ | 6 #define MOJO_SYSTEM_PLATFORM_CHANNEL_PAIR_H_ |
7 | 7 |
8 #include "base/macros.h" | 8 #include "base/macros.h" |
9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
10 #include "base/process/launch.h" | 10 #include "base/process/launch.h" |
11 #include "mojo/system/platform_channel_handle.h" | 11 #include "mojo/system/scoped_platform_handle.h" |
12 #include "mojo/system/system_impl_export.h" | 12 #include "mojo/system/system_impl_export.h" |
13 | 13 |
14 class CommandLine; | 14 class CommandLine; |
15 | 15 |
16 namespace mojo { | 16 namespace mojo { |
17 namespace system { | 17 namespace system { |
18 | 18 |
19 class PlatformChannel; | 19 // This is used to create a pair of |PlatformHandle|s that are connected by a |
20 | 20 // suitable (platform-specific) bidirectional "pipe" (e.g., socket on POSIX, |
21 // This is used to create a pair of connected |PlatformChannel|s. The resulting | 21 // named pipe on Windows). The resulting handles can then be used in the same |
22 // channels can then be used in the same process (e.g., in tests) or between | 22 // process (e.g., in tests) or between processes. (The "server" handle is the |
23 // processes. (The "server" channel is the one that will be used in the process | 23 // one that will be used in the process that created the pair, whereas the |
24 // that created the pair, whereas the "client" channel is the one that will be | 24 // "client" handle is the one that will be used in a different process.) |
25 // used in a different process.) | |
26 // | 25 // |
27 // This class provides facilities for passing the client channel to a child | 26 // This class provides facilities for passing the client handle to a child |
28 // process. The parent should call |PrepareToPassClientChannelToChildProcess()| | 27 // process. The parent should call |PrepareToPassClientHandlelToChildProcess()| |
29 // to get the data needed to do this, spawn the child using that data, and then | 28 // to get the data needed to do this, spawn the child using that data, and then |
30 // call |ChildProcessLaunched()|. Note that on Windows this facility (will) only | 29 // call |ChildProcessLaunched()|. Note that on Windows this facility (will) only |
31 // work on Vista and later (TODO(vtl)). | 30 // work on Vista and later (TODO(vtl)). |
32 // | 31 // |
33 // Note: |PlatformChannelPair()|, |CreateClientChannelFromParentProcess()|, | 32 // Note: |PlatformChannelPair()|, |PassClientHandleFromParentProcess()|, |
34 // |PrepareToPassClientChannelToChildProcess()|, and |ChildProcessLaunched()| | 33 // |PrepareToPassClientHandleToChildProcess()|, and |ChildProcessLaunched()| |
35 // have platform-specific implementations. | 34 // have platform-specific implementations. |
36 class MOJO_SYSTEM_IMPL_EXPORT PlatformChannelPair { | 35 class MOJO_SYSTEM_IMPL_EXPORT PlatformChannelPair { |
37 public: | 36 public: |
38 PlatformChannelPair(); | 37 PlatformChannelPair(); |
39 ~PlatformChannelPair(); | 38 ~PlatformChannelPair(); |
40 | 39 |
41 // This transfers ownership of the server channel to the caller. Returns null | 40 ScopedPlatformHandle PassServerHandle(); |
42 // on failure. | |
43 scoped_ptr<PlatformChannel> CreateServerChannel(); | |
44 | 41 |
45 // For in-process use (e.g., in tests). This transfers ownership of the client | 42 // For in-process use (e.g., in tests). |
46 // channel to the caller. Returns null on failure. | 43 ScopedPlatformHandle PassClientHandle(); |
47 scoped_ptr<PlatformChannel> CreateClientChannel(); | |
48 | 44 |
49 // To be called in the child process, after the parent process called | 45 // To be called in the child process, after the parent process called |
50 // |PrepareToPassClientChannelToChildProcess()| and launched the child (using | 46 // |PrepareToPassClientHandleToChildProcess()| and launched the child (using |
51 // the provided data), to create a client channel connected to the server | 47 // the provided data), to create a client handle connected to the server |
52 // channel (in the parent process). Returns null on failure. | 48 // handle (in the parent process). |
53 static scoped_ptr<PlatformChannel> CreateClientChannelFromParentProcess( | 49 static ScopedPlatformHandle PassClientHandleFromParentProcess( |
54 const CommandLine& command_line); | 50 const CommandLine& command_line); |
55 | 51 |
56 // Prepares to pass the client channel to a new child process, to be launched | 52 // Prepares to pass the client channel to a new child process, to be launched |
57 // using |LaunchProcess()| (from base/launch.h). Modifies |*command_line| and | 53 // using |LaunchProcess()| (from base/launch.h). Modifies |*command_line| and |
58 // |*file_handle_mapping| as needed. (|file_handle_mapping| may be null on | 54 // |*file_handle_mapping| as needed. (|file_handle_mapping| may be null on |
59 // platforms that don't need it, like Windows.) | 55 // platforms that don't need it, like Windows.) |
60 void PrepareToPassClientChannelToChildProcess( | 56 void PrepareToPassClientHandleToChildProcess( |
61 CommandLine* command_line, | 57 CommandLine* command_line, |
62 base::FileHandleMappingVector* file_handle_mapping) const; | 58 base::FileHandleMappingVector* file_handle_mapping) const; |
63 // To be called once the child process has been successfully launched, to do | 59 // To be called once the child process has been successfully launched, to do |
64 // any cleanup necessary. | 60 // any cleanup necessary. |
65 void ChildProcessLaunched(); | 61 void ChildProcessLaunched(); |
66 | 62 |
67 private: | 63 private: |
68 PlatformChannelHandle server_handle_; | 64 ScopedPlatformHandle server_handle_; |
69 PlatformChannelHandle client_handle_; | 65 ScopedPlatformHandle client_handle_; |
70 | 66 |
71 DISALLOW_COPY_AND_ASSIGN(PlatformChannelPair); | 67 DISALLOW_COPY_AND_ASSIGN(PlatformChannelPair); |
72 }; | 68 }; |
73 | 69 |
74 } // namespace system | 70 } // namespace system |
75 } // namespace mojo | 71 } // namespace mojo |
76 | 72 |
77 #endif // MOJO_SYSTEM_PLATFORM_CHANNEL_PAIR_H_ | 73 #endif // MOJO_SYSTEM_PLATFORM_CHANNEL_PAIR_H_ |
OLD | NEW |