| 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 "base/basictypes.h" | 8 #include "base/macros.h" |
| 9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
| 10 #include "base/process/launch.h" | |
| 11 #include "mojo/system/platform_channel_handle.h" | 10 #include "mojo/system/platform_channel_handle.h" |
| 12 #include "mojo/system/system_impl_export.h" | 11 #include "mojo/system/system_impl_export.h" |
| 13 | 12 |
| 14 class CommandLine; | |
| 15 | |
| 16 namespace mojo { | 13 namespace mojo { |
| 17 namespace system { | 14 namespace system { |
| 18 | 15 |
| 19 class MOJO_SYSTEM_IMPL_EXPORT PlatformChannel { | 16 class MOJO_SYSTEM_IMPL_EXPORT PlatformChannel { |
| 20 public: | 17 public: |
| 21 virtual ~PlatformChannel(); | 18 virtual ~PlatformChannel(); |
| 22 | 19 |
| 23 // Creates a channel if you already have the underlying handle for it, taking | 20 // Creates a channel if you already have the underlying handle for it, taking |
| 24 // ownership of |handle|. | 21 // ownership of |handle|. |
| 25 static scoped_ptr<PlatformChannel> CreateFromHandle( | 22 static scoped_ptr<PlatformChannel> CreateFromHandle( |
| 26 const PlatformChannelHandle& handle); | 23 const PlatformChannelHandle& handle); |
| 27 | 24 |
| 28 // Returns the channel's handle, passing ownership. | 25 // Returns the channel's handle, passing ownership. |
| 29 PlatformChannelHandle PassHandle(); | 26 PlatformChannelHandle PassHandle(); |
| 30 | 27 |
| 31 bool is_valid() const { return handle_.is_valid(); } | 28 bool is_valid() const { return handle_.is_valid(); } |
| 32 | 29 |
| 33 protected: | 30 protected: |
| 34 PlatformChannel(); | 31 PlatformChannel(); |
| 35 | 32 |
| 36 PlatformChannelHandle* mutable_handle() { return &handle_; } | 33 PlatformChannelHandle* mutable_handle() { return &handle_; } |
| 37 | 34 |
| 38 private: | 35 private: |
| 39 PlatformChannelHandle handle_; | 36 PlatformChannelHandle handle_; |
| 40 | 37 |
| 41 DISALLOW_COPY_AND_ASSIGN(PlatformChannel); | 38 DISALLOW_COPY_AND_ASSIGN(PlatformChannel); |
| 42 }; | 39 }; |
| 43 | 40 |
| 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(); | |
| 63 | |
| 64 // This transfers ownership of the server channel to the caller. Returns null | |
| 65 // on failure. | |
| 66 scoped_ptr<PlatformChannel> CreateServerChannel(); | |
| 67 | |
| 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(); | |
| 71 | |
| 72 // To be called in the child process, after the parent process called | |
| 73 // |PrepareToPassClientChannelToChildProcess()| and launched the child (using | |
| 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); | |
| 78 | |
| 79 // Prepares to pass the client channel to a new child process, to be launched | |
| 80 // using |LaunchProcess()| (from base/launch.h). Modifies |*command_line| and | |
| 81 // |*file_handle_mapping| as needed. (|file_handle_mapping| may be null on | |
| 82 // platforms that don't need it, like Windows.) | |
| 83 void PrepareToPassClientChannelToChildProcess( | |
| 84 CommandLine* command_line, | |
| 85 base::FileHandleMappingVector* file_handle_mapping) const; | |
| 86 // To be called once the child process has been successfully launched, to do | |
| 87 // any cleanup necessary. | |
| 88 void ChildProcessLaunched(); | |
| 89 | |
| 90 private: | |
| 91 PlatformChannelHandle server_handle_; | |
| 92 PlatformChannelHandle client_handle_; | |
| 93 | |
| 94 DISALLOW_COPY_AND_ASSIGN(PlatformChannelPair); | |
| 95 }; | |
| 96 | |
| 97 } // namespace system | 41 } // namespace system |
| 98 } // namespace mojo | 42 } // namespace mojo |
| 99 | 43 |
| 100 #endif // MOJO_SYSTEM_PLATFORM_CHANNEL_H_ | 44 #endif // MOJO_SYSTEM_PLATFORM_CHANNEL_H_ |
| OLD | NEW |