[email protected] | 5c41e6e1 | 2012-03-17 02:20:46 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 4734d0b | 2011-12-03 07:10:44 | [diff] [blame] | 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 CONTENT_COMMON_CHILD_PROCESS_HOST_IMPL_H_ |
| 6 | #define CONTENT_COMMON_CHILD_PROCESS_HOST_IMPL_H_ |
[email protected] | 4734d0b | 2011-12-03 07:10:44 | [diff] [blame] | 7 | |
| 8 | #include <string> |
| 9 | #include <vector> |
| 10 | |
| 11 | #include "build/build_config.h" |
| 12 | |
| 13 | #include "base/basictypes.h" |
| 14 | #include "base/memory/scoped_ptr.h" |
[email protected] | 9cc992b | 2013-07-17 06:30:36 | [diff] [blame] | 15 | #include "base/memory/shared_memory.h" |
[email protected] | 4734d0b | 2011-12-03 07:10:44 | [diff] [blame] | 16 | #include "base/memory/singleton.h" |
[email protected] | 30fe1f9 | 2013-06-12 16:34:34 | [diff] [blame] | 17 | #include "base/strings/string16.h" |
[email protected] | 4734d0b | 2011-12-03 07:10:44 | [diff] [blame] | 18 | #include "content/public/common/child_process_host.h" |
[email protected] | 30fe1f9 | 2013-06-12 16:34:34 | [diff] [blame] | 19 | #include "ipc/ipc_listener.h" |
[email protected] | 4734d0b | 2011-12-03 07:10:44 | [diff] [blame] | 20 | |
[email protected] | a3ef483 | 2013-02-02 05:12:33 | [diff] [blame] | 21 | namespace base { |
[email protected] | 4734d0b | 2011-12-03 07:10:44 | [diff] [blame] | 22 | class FilePath; |
[email protected] | a3ef483 | 2013-02-02 05:12:33 | [diff] [blame] | 23 | } |
[email protected] | 4734d0b | 2011-12-03 07:10:44 | [diff] [blame] | 24 | |
[email protected] | bc10f46 | 2013-10-22 23:13:09 | [diff] [blame] | 25 | namespace gfx { |
| 26 | struct GpuMemoryBufferHandle; |
| 27 | } |
| 28 | |
[email protected] | 4734d0b | 2011-12-03 07:10:44 | [diff] [blame] | 29 | namespace content { |
| 30 | class ChildProcessHostDelegate; |
| 31 | |
| 32 | // Provides common functionality for hosting a child process and processing IPC |
| 33 | // messages between the host and the child process. Users are responsible |
| 34 | // for the actual launching and terminating of the child processes. |
| 35 | class CONTENT_EXPORT ChildProcessHostImpl : public ChildProcessHost, |
[email protected] | d84effeb | 2012-06-25 17:03:10 | [diff] [blame] | 36 | public IPC::Listener { |
[email protected] | 4734d0b | 2011-12-03 07:10:44 | [diff] [blame] | 37 | public: |
| 38 | virtual ~ChildProcessHostImpl(); |
| 39 | |
[email protected] | 4734d0b | 2011-12-03 07:10:44 | [diff] [blame] | 40 | // Public and static for reuse by RenderMessageFilter. |
| 41 | static void AllocateSharedMemory( |
[email protected] | 3f89283 | 2013-01-11 03:23:59 | [diff] [blame] | 42 | size_t buffer_size, base::ProcessHandle child_process, |
[email protected] | 4734d0b | 2011-12-03 07:10:44 | [diff] [blame] | 43 | base::SharedMemoryHandle* handle); |
| 44 | |
[email protected] | 4734d0b | 2011-12-03 07:10:44 | [diff] [blame] | 45 | // Returns a unique ID to identify a child process. On construction, this |
| 46 | // function will be used to generate the id_, but it is also used to generate |
| 47 | // IDs for the RenderProcessHost, which doesn't inherit from us, and whose IDs |
| 48 | // must be unique for all child processes. |
| 49 | // |
| 50 | // This function is threadsafe since RenderProcessHost is on the UI thread, |
| 51 | // but normally this will be used on the IO thread. |
[email protected] | 3cb7cc953 | 2014-01-08 22:55:51 | [diff] [blame] | 52 | // |
| 53 | // This will never return ChildProcessHost::kInvalidUniqueID. |
[email protected] | 4734d0b | 2011-12-03 07:10:44 | [diff] [blame] | 54 | static int GenerateChildProcessUniqueId(); |
| 55 | |
| 56 | // ChildProcessHost implementation |
| 57 | virtual bool Send(IPC::Message* message) OVERRIDE; |
| 58 | virtual void ForceShutdown() OVERRIDE; |
| 59 | virtual std::string CreateChannel() OVERRIDE; |
| 60 | virtual bool IsChannelOpening() OVERRIDE; |
| 61 | virtual void AddFilter(IPC::ChannelProxy::MessageFilter* filter) OVERRIDE; |
| 62 | #if defined(OS_POSIX) |
| 63 | virtual int TakeClientFileDescriptor() OVERRIDE; |
| 64 | #endif |
| 65 | |
| 66 | private: |
| 67 | friend class ChildProcessHost; |
| 68 | |
| 69 | explicit ChildProcessHostImpl(ChildProcessHostDelegate* delegate); |
| 70 | |
[email protected] | d84effeb | 2012-06-25 17:03:10 | [diff] [blame] | 71 | // IPC::Listener methods: |
[email protected] | 4734d0b | 2011-12-03 07:10:44 | [diff] [blame] | 72 | virtual bool OnMessageReceived(const IPC::Message& msg) OVERRIDE; |
| 73 | virtual void OnChannelConnected(int32 peer_pid) OVERRIDE; |
| 74 | virtual void OnChannelError() OVERRIDE; |
| 75 | |
| 76 | // Message handlers: |
| 77 | void OnShutdownRequest(); |
| 78 | void OnAllocateSharedMemory(uint32 buffer_size, |
| 79 | base::SharedMemoryHandle* handle); |
[email protected] | 8810516 | 2013-12-05 01:02:12 | [diff] [blame] | 80 | void OnAllocateGpuMemoryBuffer(uint32 width, |
| 81 | uint32 height, |
| 82 | uint32 internalformat, |
[email protected] | bc10f46 | 2013-10-22 23:13:09 | [diff] [blame] | 83 | gfx::GpuMemoryBufferHandle* handle); |
[email protected] | 4734d0b | 2011-12-03 07:10:44 | [diff] [blame] | 84 | |
| 85 | ChildProcessHostDelegate* delegate_; |
| 86 | base::ProcessHandle peer_handle_; |
| 87 | bool opening_channel_; // True while we're waiting the channel to be opened. |
| 88 | scoped_ptr<IPC::Channel> channel_; |
| 89 | std::string channel_id_; |
| 90 | |
| 91 | // Holds all the IPC message filters. Since this object lives on the IO |
| 92 | // thread, we don't have a IPC::ChannelProxy and so we manage filters |
| 93 | // manually. |
| 94 | std::vector<scoped_refptr<IPC::ChannelProxy::MessageFilter> > filters_; |
| 95 | |
| 96 | DISALLOW_COPY_AND_ASSIGN(ChildProcessHostImpl); |
| 97 | }; |
| 98 | |
| 99 | } // namespace content |
| 100 | |
| 101 | #endif // CONTENT_COMMON_CHILD_PROCESS_HOST_IMPL_H_ |