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_SYSTEM_EMBEDDER_PLATFORM_CHANNEL_PAIR_H_
6 #define MOJO_SYSTEM_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/system/embedder/scoped_platform_handle.h"
13 #include "mojo/system/system_impl_export.h"
20 // It would be nice to refactor base/process/launch.h to have a more platform-
21 // independent way of representing handles that are passed to child processes.
23 typedef base::HandlesToInheritVector HandlePassingInformation;
24 #elif defined(OS_POSIX)
25 typedef base::FileHandleMappingVector HandlePassingInformation;
30 // This is used to create a pair of |PlatformHandle|s that are connected by a
31 // suitable (platform-specific) bidirectional "pipe" (e.g., socket on POSIX,
32 // named pipe on Windows). The resulting handles can then be used in the same
33 // process (e.g., in tests) or between processes. (The "server" handle is the
34 // one that will be used in the process that created the pair, whereas the
35 // "client" handle is the one that will be used in a different process.)
37 // This class provides facilities for passing the client handle to a child
38 // process. The parent should call |PrepareToPassClientHandlelToChildProcess()|
39 // to get the data needed to do this, spawn the child using that data, and then
40 // call |ChildProcessLaunched()|. Note that on Windows this facility (will) only
41 // work on Vista and later (TODO(vtl)).
43 // Note: |PlatformChannelPair()|, |PassClientHandleFromParentProcess()| and
44 // |PrepareToPassClientHandleToChildProcess()| have platform-specific
46 class MOJO_SYSTEM_IMPL_EXPORT PlatformChannelPair {
48 PlatformChannelPair();
49 ~PlatformChannelPair();
51 ScopedPlatformHandle PassServerHandle();
53 // For in-process use (e.g., in tests).
54 ScopedPlatformHandle PassClientHandle();
56 // To be called in the child process, after the parent process called
57 // |PrepareToPassClientHandleToChildProcess()| and launched the child (using
58 // the provided data), to create a client handle connected to the server
59 // handle (in the parent process).
60 static ScopedPlatformHandle PassClientHandleFromParentProcess(
61 const CommandLine& command_line);
63 // Prepares to pass the client channel to a new child process, to be launched
64 // using |LaunchProcess()| (from base/launch.h). Modifies |*command_line| and
65 // |*handle_passing_info| as needed.
66 // Note: For Windows, this method only works on Vista and later.
67 void PrepareToPassClientHandleToChildProcess(
68 CommandLine* command_line,
69 HandlePassingInformation* handle_passing_info) const;
71 // To be called once the child process has been successfully launched, to do
72 // any cleanup necessary.
73 void ChildProcessLaunched();
76 static const char kMojoPlatformChannelHandleSwitch[];
78 ScopedPlatformHandle server_handle_;
79 ScopedPlatformHandle client_handle_;
81 DISALLOW_COPY_AND_ASSIGN(PlatformChannelPair);
84 } // namespace embedder
87 #endif // MOJO_SYSTEM_EMBEDDER_PLATFORM_CHANNEL_PAIR_H_