1 // Copyright 2013 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_PLATFORM_CHANNEL_H_ 6 #define MOJO_SYSTEM_PLATFORM_CHANNEL_H_ 7 8 #include <string> 9 #include <utility> 10 11 #include "base/basictypes.h" 12 #include "base/memory/scoped_ptr.h" 13 #include "base/process/launch.h" 14 #include "mojo/system/platform_channel_handle.h" 15 #include "mojo/system/system_impl_export.h" 16 17 class CommandLine; 18 19 namespace mojo { 20 namespace system { 21 22 class MOJO_SYSTEM_IMPL_EXPORT PlatformChannel { 23 public: 24 virtual ~PlatformChannel(); 25 26 // Returns the channel's handle, passing ownership. 27 PlatformChannelHandle PassHandle(); 28 29 bool is_valid() const { return handle_.is_valid(); } 30 31 protected: 32 PlatformChannel(); 33 34 PlatformChannelHandle* mutable_handle() { return &handle_; } 35 36 private: 37 PlatformChannelHandle handle_; 38 39 DISALLOW_COPY_AND_ASSIGN(PlatformChannel); 40 }; 41 42 class PlatformClientChannel; 43 44 // A server channel has an "implicit" client channel created with it. This may 45 // be a real channel (in the case of POSIX, in which case there's an actual FD 46 // for it) or fake. 47 // - That client channel may then be used in-process (e.g., for single process 48 // tests) by getting a |PlatformClientChannel| using |CreateClientChannel()|. 49 // - Or it may be "passed" to a new child process using 50 // |GetDataNeededToPassClientChannelToChildProcess()|, etc. (see below). The 51 // child process would then get a |PlatformClientChannel| by using 52 // |PlatformClientChannel::CreateFromParentProcess()|. 53 // - In both these cases, "ownership" of the client channel is transferred (to 54 // the |PlatformClientChannel| or the child process). 55 // TODO(vtl): Add ways of passing it to other existing processes. 56 class MOJO_SYSTEM_IMPL_EXPORT PlatformServerChannel : public PlatformChannel { 57 public: 58 virtual ~PlatformServerChannel() {} 59 60 static scoped_ptr<PlatformServerChannel> Create(const std::string& name); 61 62 // For in-process use, from a server channel you can make a corresponding 63 // client channel. 64 virtual scoped_ptr<PlatformClientChannel> CreateClientChannel() = 0; 65 66 // Prepares to pass the client channel to a new child process, to be launched 67 // using |LaunchProcess()| (from base/launch.h). Modifies |*command_line| and 68 // |*file_handle_mapping| as needed. (|file_handle_mapping| may be null on 69 // platforms that don't need it, like Windows.) 70 virtual void GetDataNeededToPassClientChannelToChildProcess( 71 CommandLine* command_line, 72 base::FileHandleMappingVector* file_handle_mapping) const = 0; 73 // To be called once the child process has been successfully launched, to do 74 // any cleanup necessary. 75 virtual void ChildProcessLaunched() = 0; 76 77 const std::string& name() const { return name_; } 78 79 protected: 80 explicit PlatformServerChannel(const std::string& name); 81 82 private: 83 const std::string name_; 84 85 DISALLOW_COPY_AND_ASSIGN(PlatformServerChannel); 86 }; 87 88 class MOJO_SYSTEM_IMPL_EXPORT PlatformClientChannel : public PlatformChannel { 89 public: 90 virtual ~PlatformClientChannel() {} 91 92 // Creates a client channel if you already have the underlying handle for it. 93 // Note: This takes ownership of |handle|. 94 static scoped_ptr<PlatformClientChannel> CreateFromHandle( 95 const PlatformChannelHandle& handle); 96 97 // To be called to get a client channel passed from the parent process, using 98 // |PlatformServerChannel::GetDataNeededToPassClientChannelToChildProcess()|, 99 // etc. Returns null on failure. 100 static scoped_ptr<PlatformClientChannel> CreateFromParentProcess( 101 const CommandLine& command_line); 102 103 private: 104 PlatformClientChannel() {} 105 106 DISALLOW_COPY_AND_ASSIGN(PlatformClientChannel); 107 }; 108 109 } // namespace system 110 } // namespace mojo 111 112 #endif // MOJO_SYSTEM_PLATFORM_CHANNEL_H_ 113