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
3 // found in the LICENSE file.
5 #ifndef MOJO_EMBEDDER_PLATFORM_CHANNEL_PAIR_H_
6 #define MOJO_EMBEDDER_PLATFORM_CHANNEL_PAIR_H_
8 #include "base/macros.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/process/launch.h"
11 #include "build/build_config.h"
12 #include "mojo/embedder/scoped_platform_handle.h"
13 #include "mojo/system/system_impl_export.h"
22 // It would be nice to refactor base/process/launch.h to have a more platform-
23 // independent way of representing handles that are passed to child processes.
25 typedef base::HandlesToInheritVector HandlePassingInformation
;
26 #elif defined(OS_POSIX)
27 typedef base::FileHandleMappingVector HandlePassingInformation
;
32 // This is used to create a pair of |PlatformHandle|s that are connected by a
33 // suitable (platform-specific) bidirectional "pipe" (e.g., socket on POSIX,
34 // named pipe on Windows). The resulting handles can then be used in the same
35 // process (e.g., in tests) or between processes. (The "server" handle is the
36 // one that will be used in the process that created the pair, whereas the
37 // "client" handle is the one that will be used in a different process.)
39 // This class provides facilities for passing the client handle to a child
40 // process. The parent should call |PrepareToPassClientHandlelToChildProcess()|
41 // to get the data needed to do this, spawn the child using that data, and then
42 // call |ChildProcessLaunched()|. Note that on Windows this facility (will) only
43 // work on Vista and later (TODO(vtl)).
45 // Note: |PlatformChannelPair()|, |PassClientHandleFromParentProcess()| and
46 // |PrepareToPassClientHandleToChildProcess()| have platform-specific
48 class MOJO_SYSTEM_IMPL_EXPORT PlatformChannelPair
{
50 PlatformChannelPair();
51 ~PlatformChannelPair();
53 ScopedPlatformHandle
PassServerHandle();
55 // For in-process use (e.g., in tests or to pass over another channel).
56 ScopedPlatformHandle
PassClientHandle();
58 // To be called in the child process, after the parent process called
59 // |PrepareToPassClientHandleToChildProcess()| and launched the child (using
60 // the provided data), to create a client handle connected to the server
61 // handle (in the parent process).
62 static ScopedPlatformHandle
PassClientHandleFromParentProcess(
63 const base::CommandLine
& command_line
);
65 // Prepares to pass the client channel to a new child process, to be launched
66 // using |LaunchProcess()| (from base/launch.h). Modifies |*command_line| and
67 // |*handle_passing_info| as needed.
68 // Note: For Windows, this method only works on Vista and later.
69 void PrepareToPassClientHandleToChildProcess(
70 base::CommandLine
* command_line
,
71 HandlePassingInformation
* handle_passing_info
) const;
73 // To be called once the child process has been successfully launched, to do
74 // any cleanup necessary.
75 void ChildProcessLaunched();
78 static const char kMojoPlatformChannelHandleSwitch
[];
80 ScopedPlatformHandle server_handle_
;
81 ScopedPlatformHandle client_handle_
;
83 DISALLOW_COPY_AND_ASSIGN(PlatformChannelPair
);
86 } // namespace embedder
89 #endif // MOJO_EMBEDDER_PLATFORM_CHANNEL_PAIR_H_