• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
4 
5 #ifndef MOJO_EDK_EMBEDDER_PLATFORM_CHANNEL_PAIR_H_
6 #define MOJO_EDK_EMBEDDER_PLATFORM_CHANNEL_PAIR_H_
7 
8 #include <memory>
9 
10 #include "base/macros.h"
11 #include "base/process/launch.h"
12 #include "build/build_config.h"
13 #include "mojo/edk/embedder/scoped_platform_handle.h"
14 #include "mojo/edk/system/system_impl_export.h"
15 
16 namespace base {
17 class CommandLine;
18 }
19 
20 namespace mojo {
21 namespace edk {
22 
23 // It would be nice to refactor base/process/launch.h to have a more platform-
24 // independent way of representing handles that are passed to child processes.
25 #if defined(OS_WIN)
26 using HandlePassingInformation = base::HandlesToInheritVector;
27 #elif defined(OS_POSIX)
28 using HandlePassingInformation = base::FileHandleMappingVector;
29 #else
30 #error "Unsupported."
31 #endif
32 
33 // This is used to create a pair of |PlatformHandle|s that are connected by a
34 // suitable (platform-specific) bidirectional "pipe" (e.g., socket on POSIX,
35 // named pipe on Windows). The resulting handles can then be used in the same
36 // process (e.g., in tests) or between processes. (The "server" handle is the
37 // one that will be used in the process that created the pair, whereas the
38 // "client" handle is the one that will be used in a different process.)
39 //
40 // This class provides facilities for passing the client handle to a child
41 // process. The parent should call |PrepareToPassClientHandlelToChildProcess()|
42 // to get the data needed to do this, spawn the child using that data, and then
43 // call |ChildProcessLaunched()|. Note that on Windows this facility (will) only
44 // work on Vista and later (TODO(vtl)).
45 //
46 // Note: |PlatformChannelPair()|, |PassClientHandleFromParentProcess()| and
47 // |PrepareToPassClientHandleToChildProcess()| have platform-specific
48 // implementations.
49 //
50 // Note: On POSIX platforms, to write to the "pipe", use
51 // |PlatformChannel{Write,Writev}()| (from platform_channel_utils_posix.h)
52 // instead of |write()|, |writev()|, etc. Otherwise, you have to worry about
53 // platform differences in suppressing |SIGPIPE|.
54 class MOJO_SYSTEM_IMPL_EXPORT PlatformChannelPair {
55  public:
56   static const char kMojoPlatformChannelHandleSwitch[];
57 
58   // If |client_is_blocking| is true, then the client handle only supports
59   // blocking reads and writes. The default is nonblocking.
60   PlatformChannelPair(bool client_is_blocking = false);
61   ~PlatformChannelPair();
62 
63   ScopedPlatformHandle PassServerHandle();
64 
65   // For in-process use (e.g., in tests or to pass over another channel).
66   ScopedPlatformHandle PassClientHandle();
67 
68   // To be called in the child process, after the parent process called
69   // |PrepareToPassClientHandleToChildProcess()| and launched the child (using
70   // the provided data), to create a client handle connected to the server
71   // handle (in the parent process).
72   // TODO(jcivelli): remove the command_line param. http://crbug.com/670106
73   static ScopedPlatformHandle PassClientHandleFromParentProcess(
74       const base::CommandLine& command_line);
75 
76   // Like above, but gets the handle from the passed in string.
77   static ScopedPlatformHandle PassClientHandleFromParentProcessFromString(
78       const std::string& value);
79 
80   // Prepares to pass the client channel to a new child process, to be launched
81   // using |LaunchProcess()| (from base/launch.h). Modifies |*command_line| and
82   // |*handle_passing_info| as needed.
83   // Note: For Windows, this method only works on Vista and later.
84   void PrepareToPassClientHandleToChildProcess(
85       base::CommandLine* command_line,
86       HandlePassingInformation* handle_passing_info) const;
87 
88   // Like above, but returns a string instead of changing the command line.
89   std::string PrepareToPassClientHandleToChildProcessAsString(
90       HandlePassingInformation* handle_passing_info) const;
91 
92   // To be called once the child process has been successfully launched, to do
93   // any cleanup necessary.
94   void ChildProcessLaunched();
95 
96  private:
97   ScopedPlatformHandle server_handle_;
98   ScopedPlatformHandle client_handle_;
99 
100   DISALLOW_COPY_AND_ASSIGN(PlatformChannelPair);
101 };
102 
103 }  // namespace edk
104 }  // namespace mojo
105 
106 #endif  // MOJO_EDK_EMBEDDER_PLATFORM_CHANNEL_PAIR_H_
107