1 // Copyright (c) 2011 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_
12 #include "build/build_config.h"
14 #include "base/basictypes.h"
15 #include "base/memory/scoped_ptr.h"
16 #include "base/memory/singleton.h"
17 #include "base/shared_memory.h"
18 #include "base/string16.h"
19 #include "content/public/common/child_process_host.h"
24 class ChildProcessHostDelegate
;
26 // Provides common functionality for hosting a child process and processing IPC
27 // messages between the host and the child process. Users are responsible
28 // for the actual launching and terminating of the child processes.
29 class CONTENT_EXPORT ChildProcessHostImpl
: public ChildProcessHost
,
30 public IPC::Channel::Listener
{
32 virtual ~ChildProcessHostImpl();
34 // Public and static for reuse by RenderMessageFilter.
35 static void AllocateSharedMemory(
36 uint32 buffer_size
, base::ProcessHandle child_process
,
37 base::SharedMemoryHandle
* handle
);
39 // Generates a unique channel name for a child process.
40 // The "instance" pointer value is baked into the channel id.
41 static std::string
GenerateRandomChannelID(void* instance
);
43 // Returns a unique ID to identify a child process. On construction, this
44 // function will be used to generate the id_, but it is also used to generate
45 // IDs for the RenderProcessHost, which doesn't inherit from us, and whose IDs
46 // must be unique for all child processes.
48 // This function is threadsafe since RenderProcessHost is on the UI thread,
49 // but normally this will be used on the IO thread.
50 static int GenerateChildProcessUniqueId();
52 // ChildProcessHost implementation
53 virtual bool Send(IPC::Message
* message
) OVERRIDE
;
54 virtual void ForceShutdown() OVERRIDE
;
55 virtual std::string
CreateChannel() OVERRIDE
;
56 virtual bool IsChannelOpening() OVERRIDE
;
57 virtual void AddFilter(IPC::ChannelProxy::MessageFilter
* filter
) OVERRIDE
;
59 virtual int TakeClientFileDescriptor() OVERRIDE
;
63 friend class ChildProcessHost
;
65 explicit ChildProcessHostImpl(ChildProcessHostDelegate
* delegate
);
67 // IPC::Channel::Listener methods:
68 virtual bool OnMessageReceived(const IPC::Message
& msg
) OVERRIDE
;
69 virtual void OnChannelConnected(int32 peer_pid
) OVERRIDE
;
70 virtual void OnChannelError() OVERRIDE
;
73 void OnShutdownRequest();
74 void OnAllocateSharedMemory(uint32 buffer_size
,
75 base::SharedMemoryHandle
* handle
);
77 ChildProcessHostDelegate
* delegate_
;
78 base::ProcessHandle peer_handle_
;
79 bool opening_channel_
; // True while we're waiting the channel to be opened.
80 scoped_ptr
<IPC::Channel
> channel_
;
81 std::string channel_id_
;
83 // Holds all the IPC message filters. Since this object lives on the IO
84 // thread, we don't have a IPC::ChannelProxy and so we manage filters
86 std::vector
<scoped_refptr
<IPC::ChannelProxy::MessageFilter
> > filters_
;
88 DISALLOW_COPY_AND_ASSIGN(ChildProcessHostImpl
);
91 } // namespace content
93 #endif // CONTENT_COMMON_CHILD_PROCESS_HOST_IMPL_H_