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_EMBEDDER_PLATFORM_CHANNEL_PAIR_H_ 6#define MOJO_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/embedder/scoped_platform_handle.h" 13#include "mojo/system/system_impl_export.h" 14 15namespace base { 16class CommandLine; 17} 18 19namespace mojo { 20namespace embedder { 21 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. 24#if defined(OS_WIN) 25typedef base::HandlesToInheritVector HandlePassingInformation; 26#elif defined(OS_POSIX) 27typedef base::FileHandleMappingVector HandlePassingInformation; 28#else 29#error "Unsupported." 30#endif 31 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.) 38// 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)). 44// 45// Note: |PlatformChannelPair()|, |PassClientHandleFromParentProcess()| and 46// |PrepareToPassClientHandleToChildProcess()| have platform-specific 47// implementations. 48// 49// Note: On POSIX platforms, to write to the "pipe", use 50// |PlatformChannel{Write,Writev}()| (from platform_channel_utils_posix.h) 51// instead of |write()|, |writev()|, etc. Otherwise, you have to worry about 52// platform differences in suppressing |SIGPIPE|. 53class MOJO_SYSTEM_IMPL_EXPORT PlatformChannelPair { 54 public: 55 PlatformChannelPair(); 56 ~PlatformChannelPair(); 57 58 ScopedPlatformHandle PassServerHandle(); 59 60 // For in-process use (e.g., in tests or to pass over another channel). 61 ScopedPlatformHandle PassClientHandle(); 62 63 // To be called in the child process, after the parent process called 64 // |PrepareToPassClientHandleToChildProcess()| and launched the child (using 65 // the provided data), to create a client handle connected to the server 66 // handle (in the parent process). 67 static ScopedPlatformHandle PassClientHandleFromParentProcess( 68 const base::CommandLine& command_line); 69 70 // Prepares to pass the client channel to a new child process, to be launched 71 // using |LaunchProcess()| (from base/launch.h). Modifies |*command_line| and 72 // |*handle_passing_info| as needed. 73 // Note: For Windows, this method only works on Vista and later. 74 void PrepareToPassClientHandleToChildProcess( 75 base::CommandLine* command_line, 76 HandlePassingInformation* handle_passing_info) const; 77 78 // To be called once the child process has been successfully launched, to do 79 // any cleanup necessary. 80 void ChildProcessLaunched(); 81 82 private: 83 static const char kMojoPlatformChannelHandleSwitch[]; 84 85 ScopedPlatformHandle server_handle_; 86 ScopedPlatformHandle client_handle_; 87 88 DISALLOW_COPY_AND_ASSIGN(PlatformChannelPair); 89}; 90 91} // namespace embedder 92} // namespace mojo 93 94#endif // MOJO_EMBEDDER_PLATFORM_CHANNEL_PAIR_H_ 95