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.
4
5#ifndef CONTENT_COMMON_CHILD_PROCESS_HOST_IMPL_H_
6#define CONTENT_COMMON_CHILD_PROCESS_HOST_IMPL_H_
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"
15#include "base/memory/shared_memory.h"
16#include "base/memory/singleton.h"
17#include "base/strings/string16.h"
18#include "content/public/common/child_process_host.h"
19#include "ipc/ipc_listener.h"
20
21namespace base {
22class FilePath;
23}
24
25namespace content {
26class ChildProcessHostDelegate;
27
28// Provides common functionality for hosting a child process and processing IPC
29// messages between the host and the child process. Users are responsible
30// for the actual launching and terminating of the child processes.
31class CONTENT_EXPORT ChildProcessHostImpl : public ChildProcessHost,
32                                            public IPC::Listener {
33 public:
34  virtual ~ChildProcessHostImpl();
35
36  // This value is guaranteed to never be returned by
37  // GenerateChildProcessUniqueId() below.
38  static int kInvalidChildProcessId;
39
40  // Public and static for reuse by RenderMessageFilter.
41  static void AllocateSharedMemory(
42      size_t buffer_size, base::ProcessHandle child_process,
43      base::SharedMemoryHandle* handle);
44
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.
52  static int GenerateChildProcessUniqueId();
53
54  // ChildProcessHost implementation
55  virtual bool Send(IPC::Message* message) OVERRIDE;
56  virtual void ForceShutdown() OVERRIDE;
57  virtual std::string CreateChannel() OVERRIDE;
58  virtual bool IsChannelOpening() OVERRIDE;
59  virtual void AddFilter(IPC::ChannelProxy::MessageFilter* filter) OVERRIDE;
60#if defined(OS_POSIX)
61  virtual int TakeClientFileDescriptor() OVERRIDE;
62#endif
63
64 private:
65  friend class ChildProcessHost;
66
67  explicit ChildProcessHostImpl(ChildProcessHostDelegate* delegate);
68
69  // IPC::Listener methods:
70  virtual bool OnMessageReceived(const IPC::Message& msg) OVERRIDE;
71  virtual void OnChannelConnected(int32 peer_pid) OVERRIDE;
72  virtual void OnChannelError() OVERRIDE;
73
74  // Message handlers:
75  void OnShutdownRequest();
76  void OnAllocateSharedMemory(uint32 buffer_size,
77                              base::SharedMemoryHandle* handle);
78
79  ChildProcessHostDelegate* delegate_;
80  base::ProcessHandle peer_handle_;
81  bool opening_channel_;  // True while we're waiting the channel to be opened.
82  scoped_ptr<IPC::Channel> channel_;
83  std::string channel_id_;
84
85  // Holds all the IPC message filters.  Since this object lives on the IO
86  // thread, we don't have a IPC::ChannelProxy and so we manage filters
87  // manually.
88  std::vector<scoped_refptr<IPC::ChannelProxy::MessageFilter> > filters_;
89
90  DISALLOW_COPY_AND_ASSIGN(ChildProcessHostImpl);
91};
92
93}  // namespace content
94
95#endif  // CONTENT_COMMON_CHILD_PROCESS_HOST_IMPL_H_
96