1 // Copyright (c) 2012 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.
5 #ifndef CONTENT_COMMON_CHILD_PROCESS_HOST_IMPL_H_
6 #define CONTENT_COMMON_CHILD_PROCESS_HOST_IMPL_H_
11 #include "build/build_config.h"
13 #include "base/basictypes.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "base/memory/shared_memory.h"
16 #include "base/memory/singleton.h"
17 #include "base/process/process.h"
18 #include "base/strings/string16.h"
19 #include "content/public/common/child_process_host.h"
20 #include "ipc/ipc_listener.h"
21 #include "ui/gfx/gpu_memory_buffer.h"
32 class ChildProcessHostDelegate
;
34 // Provides common functionality for hosting a child process and processing IPC
35 // messages between the host and the child process. Users are responsible
36 // for the actual launching and terminating of the child processes.
37 class CONTENT_EXPORT ChildProcessHostImpl
: public ChildProcessHost
,
38 public IPC::Listener
{
40 ~ChildProcessHostImpl() override
;
42 // Public and static for reuse by RenderMessageFilter.
43 static void AllocateSharedMemory(
44 size_t buffer_size
, base::ProcessHandle child_process
,
45 base::SharedMemoryHandle
* handle
);
47 // Returns a unique ID to identify a child process. On construction, this
48 // function will be used to generate the id_, but it is also used to generate
49 // IDs for the RenderProcessHost, which doesn't inherit from us, and whose IDs
50 // must be unique for all child processes.
52 // This function is threadsafe since RenderProcessHost is on the UI thread,
53 // but normally this will be used on the IO thread.
55 // This will never return ChildProcessHost::kInvalidUniqueID.
56 static int GenerateChildProcessUniqueId();
58 // ChildProcessHost implementation
59 bool Send(IPC::Message
* message
) override
;
60 void ForceShutdown() override
;
61 std::string
CreateChannel() override
;
62 bool IsChannelOpening() override
;
63 void AddFilter(IPC::MessageFilter
* filter
) override
;
65 base::ScopedFD
TakeClientFileDescriptor() override
;
69 friend class ChildProcessHost
;
71 explicit ChildProcessHostImpl(ChildProcessHostDelegate
* delegate
);
73 // IPC::Listener methods:
74 bool OnMessageReceived(const IPC::Message
& msg
) override
;
75 void OnChannelConnected(int32 peer_pid
) override
;
76 void OnChannelError() override
;
77 void OnBadMessageReceived(const IPC::Message
& message
) override
;
80 void OnShutdownRequest();
81 void OnAllocateSharedMemory(uint32 buffer_size
,
82 base::SharedMemoryHandle
* handle
);
83 void OnAllocateGpuMemoryBuffer(uint32 width
,
85 gfx::GpuMemoryBuffer::Format format
,
86 gfx::GpuMemoryBuffer::Usage usage
,
88 void OnDeletedGpuMemoryBuffer(gfx::GpuMemoryBufferId id
,
91 void GpuMemoryBufferAllocated(IPC::Message
* reply
,
92 const gfx::GpuMemoryBufferHandle
& handle
);
94 ChildProcessHostDelegate
* delegate_
;
95 base::Process peer_process_
;
96 bool opening_channel_
; // True while we're waiting the channel to be opened.
97 scoped_ptr
<IPC::Channel
> channel_
;
98 std::string channel_id_
;
100 // Holds all the IPC message filters. Since this object lives on the IO
101 // thread, we don't have a IPC::ChannelProxy and so we manage filters
103 std::vector
<scoped_refptr
<IPC::MessageFilter
> > filters_
;
105 DISALLOW_COPY_AND_ASSIGN(ChildProcessHostImpl
);
108 } // namespace content
110 #endif // CONTENT_COMMON_CHILD_PROCESS_HOST_IMPL_H_