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 // Derives a tracing process id from a child process id. Child process ids
59 // cannot be used directly in child process for tracing due to security
60 // reasons (see: discussion in crrev.com/1173263004). This method is meant to
61 // be used when tracing for identifying cross-process shared memory from a
62 // process which knows the child process id of its endpoints. The value
63 // returned by this method is guaranteed to be equal to the value returned by
64 // MemoryDumpManager::GetTracingProcessId() in the corresponding child
67 // Never returns MemoryDumpManager::kInvalidTracingProcessId.
68 // Returns only ChildProcessHost::kBrowserTracingProcessId in single-process
70 static uint64
ChildProcessUniqueIdToTracingProcessId(int child_process_id
);
72 // ChildProcessHost implementation
73 bool Send(IPC::Message
* message
) override
;
74 void ForceShutdown() override
;
75 std::string
CreateChannel() override
;
76 bool IsChannelOpening() override
;
77 void AddFilter(IPC::MessageFilter
* filter
) override
;
79 base::ScopedFD
TakeClientFileDescriptor() override
;
83 friend class ChildProcessHost
;
85 explicit ChildProcessHostImpl(ChildProcessHostDelegate
* delegate
);
87 // IPC::Listener methods:
88 bool OnMessageReceived(const IPC::Message
& msg
) override
;
89 void OnChannelConnected(int32 peer_pid
) override
;
90 void OnChannelError() override
;
91 void OnBadMessageReceived(const IPC::Message
& message
) override
;
94 void OnShutdownRequest();
95 void OnAllocateSharedMemory(uint32 buffer_size
,
96 base::SharedMemoryHandle
* handle
);
97 void OnAllocateGpuMemoryBuffer(uint32 width
,
99 gfx::BufferFormat format
,
100 gfx::BufferUsage usage
,
101 gfx::GpuMemoryBufferHandle
* handle
);
102 void OnDeletedGpuMemoryBuffer(gfx::GpuMemoryBufferId id
,
105 ChildProcessHostDelegate
* delegate_
;
106 base::Process peer_process_
;
107 bool opening_channel_
; // True while we're waiting the channel to be opened.
108 scoped_ptr
<IPC::Channel
> channel_
;
109 std::string channel_id_
;
111 // Holds all the IPC message filters. Since this object lives on the IO
112 // thread, we don't have a IPC::ChannelProxy and so we manage filters
114 std::vector
<scoped_refptr
<IPC::MessageFilter
> > filters_
;
116 DISALLOW_COPY_AND_ASSIGN(ChildProcessHostImpl
);
119 } // namespace content
121 #endif // CONTENT_COMMON_CHILD_PROCESS_HOST_IMPL_H_