| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef MOJO_SYSTEM_PLATFORM_CHANNEL_H_ | 5 #ifndef MOJO_SYSTEM_PLATFORM_CHANNEL_H_ |
| 6 #define MOJO_SYSTEM_PLATFORM_CHANNEL_H_ | 6 #define MOJO_SYSTEM_PLATFORM_CHANNEL_H_ |
| 7 | 7 |
| 8 #include <string> | |
| 9 #include <utility> | |
| 10 | |
| 11 #include "base/basictypes.h" | 8 #include "base/basictypes.h" |
| 12 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
| 13 #include "base/process/launch.h" | 10 #include "base/process/launch.h" |
| 14 #include "mojo/system/platform_channel_handle.h" | 11 #include "mojo/system/platform_channel_handle.h" |
| 15 #include "mojo/system/system_impl_export.h" | 12 #include "mojo/system/system_impl_export.h" |
| 16 | 13 |
| 17 class CommandLine; | 14 class CommandLine; |
| 18 | 15 |
| 19 namespace mojo { | 16 namespace mojo { |
| 20 namespace system { | 17 namespace system { |
| 21 | 18 |
| 22 class MOJO_SYSTEM_IMPL_EXPORT PlatformChannel { | 19 class MOJO_SYSTEM_IMPL_EXPORT PlatformChannel { |
| 23 public: | 20 public: |
| 24 virtual ~PlatformChannel(); | 21 virtual ~PlatformChannel(); |
| 25 | 22 |
| 23 // Creates a channel if you already have the underlying handle for it, taking |
| 24 // ownership of |handle|. |
| 25 static scoped_ptr<PlatformChannel> CreateFromHandle( |
| 26 const PlatformChannelHandle& handle); |
| 27 |
| 26 // Returns the channel's handle, passing ownership. | 28 // Returns the channel's handle, passing ownership. |
| 27 PlatformChannelHandle PassHandle(); | 29 PlatformChannelHandle PassHandle(); |
| 28 | 30 |
| 29 bool is_valid() const { return handle_.is_valid(); } | 31 bool is_valid() const { return handle_.is_valid(); } |
| 30 | 32 |
| 31 protected: | 33 protected: |
| 32 PlatformChannel(); | 34 PlatformChannel(); |
| 33 | 35 |
| 34 PlatformChannelHandle* mutable_handle() { return &handle_; } | 36 PlatformChannelHandle* mutable_handle() { return &handle_; } |
| 35 | 37 |
| 36 private: | 38 private: |
| 37 PlatformChannelHandle handle_; | 39 PlatformChannelHandle handle_; |
| 38 | 40 |
| 39 DISALLOW_COPY_AND_ASSIGN(PlatformChannel); | 41 DISALLOW_COPY_AND_ASSIGN(PlatformChannel); |
| 40 }; | 42 }; |
| 41 | 43 |
| 42 class PlatformClientChannel; | 44 // This is used to create a pair of connected |PlatformChannel|s. The resulting |
| 45 // channels can then be used in the same process (e.g., in tests) or between |
| 46 // processes. (The "server" channel is the one that will be used in the process |
| 47 // that created the pair, whereas the "client" channel is the one that will be |
| 48 // used in a different process.) |
| 49 // |
| 50 // This class provides facilities for passing the client channel to a child |
| 51 // process. The parent should call |PrepareToPassClientChannelToChildProcess()| |
| 52 // to get the data needed to do this, spawn the child using that data, and then |
| 53 // call |ChildProcessLaunched()|. Note that on Windows this facility (will) only |
| 54 // work on Vista and later (TODO(vtl)). |
| 55 // |
| 56 // Note: |PlatformChannelPair()|, |CreateClientChannelFromParentProcess()|, |
| 57 // |PrepareToPassClientChannelToChildProcess()|, and |ChildProcessLaunched()| |
| 58 // have platform-specific implementations. |
| 59 class MOJO_SYSTEM_IMPL_EXPORT PlatformChannelPair { |
| 60 public: |
| 61 PlatformChannelPair(); |
| 62 ~PlatformChannelPair(); |
| 43 | 63 |
| 44 // A server channel has an "implicit" client channel created with it. This may | 64 // This transfers ownership of the server channel to the caller. Returns null |
| 45 // be a real channel (in the case of POSIX, in which case there's an actual FD | 65 // on failure. |
| 46 // for it) or fake. | 66 scoped_ptr<PlatformChannel> CreateServerChannel(); |
| 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 | 67 |
| 60 static scoped_ptr<PlatformServerChannel> Create(const std::string& name); | 68 // For in-process use (e.g., in tests). This transfers ownership of the client |
| 69 // channel to the caller. Returns null on failure. |
| 70 scoped_ptr<PlatformChannel> CreateClientChannel(); |
| 61 | 71 |
| 62 // For in-process use, from a server channel you can make a corresponding | 72 // To be called in the child process, after the parent process called |
| 63 // client channel. | 73 // |PrepareToPassClientChannelToChildProcess()| and launched the child (using |
| 64 virtual scoped_ptr<PlatformClientChannel> CreateClientChannel() = 0; | 74 // the provided data), to create a client channel connected to the server |
| 75 // channel (in the parent process). Returns null on failure. |
| 76 static scoped_ptr<PlatformChannel> CreateClientChannelFromParentProcess( |
| 77 const CommandLine& command_line); |
| 65 | 78 |
| 66 // Prepares to pass the client channel to a new child process, to be launched | 79 // 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 | 80 // using |LaunchProcess()| (from base/launch.h). Modifies |*command_line| and |
| 68 // |*file_handle_mapping| as needed. (|file_handle_mapping| may be null on | 81 // |*file_handle_mapping| as needed. (|file_handle_mapping| may be null on |
| 69 // platforms that don't need it, like Windows.) | 82 // platforms that don't need it, like Windows.) |
| 70 virtual void GetDataNeededToPassClientChannelToChildProcess( | 83 void PrepareToPassClientChannelToChildProcess( |
| 71 CommandLine* command_line, | 84 CommandLine* command_line, |
| 72 base::FileHandleMappingVector* file_handle_mapping) const = 0; | 85 base::FileHandleMappingVector* file_handle_mapping) const; |
| 73 // To be called once the child process has been successfully launched, to do | 86 // To be called once the child process has been successfully launched, to do |
| 74 // any cleanup necessary. | 87 // any cleanup necessary. |
| 75 virtual void ChildProcessLaunched() = 0; | 88 void ChildProcessLaunched(); |
| 76 | |
| 77 const std::string& name() const { return name_; } | |
| 78 | |
| 79 protected: | |
| 80 explicit PlatformServerChannel(const std::string& name); | |
| 81 | 89 |
| 82 private: | 90 private: |
| 83 const std::string name_; | 91 PlatformChannelHandle server_handle_; |
| 92 PlatformChannelHandle client_handle_; |
| 84 | 93 |
| 85 DISALLOW_COPY_AND_ASSIGN(PlatformServerChannel); | 94 DISALLOW_COPY_AND_ASSIGN(PlatformChannelPair); |
| 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 }; | 95 }; |
| 108 | 96 |
| 109 } // namespace system | 97 } // namespace system |
| 110 } // namespace mojo | 98 } // namespace mojo |
| 111 | 99 |
| 112 #endif // MOJO_SYSTEM_PLATFORM_CHANNEL_H_ | 100 #endif // MOJO_SYSTEM_PLATFORM_CHANNEL_H_ |
| OLD | NEW |