Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / mojo / system / embedder / platform_channel_pair.h
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_SYSTEM_EMBEDDER_PLATFORM_CHANNEL_PAIR_H_
6 #define MOJO_SYSTEM_EMBEDDER_PLATFORM_CHANNEL_PAIR_H_
7
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"
14
15 class CommandLine;
16
17 namespace mojo {
18 namespace embedder {
19
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.
22 #if defined(OS_WIN)
23 typedef base::HandlesToInheritVector HandlePassingInformation;
24 #elif defined(OS_POSIX)
25 typedef base::FileHandleMappingVector HandlePassingInformation;
26 #else
27 #error "Unsupported."
28 #endif
29
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.)
36 //
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)).
42 //
43 // Note: |PlatformChannelPair()|, |PassClientHandleFromParentProcess()| and
44 // |PrepareToPassClientHandleToChildProcess()| have platform-specific
45 // implementations.
46 class MOJO_SYSTEM_IMPL_EXPORT PlatformChannelPair {
47  public:
48   PlatformChannelPair();
49   ~PlatformChannelPair();
50
51   ScopedPlatformHandle PassServerHandle();
52
53   // For in-process use (e.g., in tests).
54   ScopedPlatformHandle PassClientHandle();
55
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);
62
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;
70
71   // To be called once the child process has been successfully launched, to do
72   // any cleanup necessary.
73   void ChildProcessLaunched();
74
75  private:
76   static const char kMojoPlatformChannelHandleSwitch[];
77
78   ScopedPlatformHandle server_handle_;
79   ScopedPlatformHandle client_handle_;
80
81   DISALLOW_COPY_AND_ASSIGN(PlatformChannelPair);
82 };
83
84 }  // namespace embedder
85 }  // namespace mojo
86
87 #endif  // MOJO_SYSTEM_EMBEDDER_PLATFORM_CHANNEL_PAIR_H_