| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef CONTENT_COMMON_CHILD_PROCESS_HOST_H_ | 5 #ifndef CONTENT_COMMON_CHILD_PROCESS_HOST_IMPL_H_ |
| 6 #define CONTENT_COMMON_CHILD_PROCESS_HOST_H_ | 6 #define CONTENT_COMMON_CHILD_PROCESS_HOST_IMPL_H_ |
| 7 #pragma once | 7 #pragma once |
| 8 | 8 |
| 9 #include <string> | 9 #include <string> |
| 10 #include <vector> | 10 #include <vector> |
| 11 | 11 |
| 12 #include "build/build_config.h" | 12 #include "build/build_config.h" |
| 13 | 13 |
| 14 #include "base/basictypes.h" | 14 #include "base/basictypes.h" |
| 15 #include "base/memory/scoped_ptr.h" | 15 #include "base/memory/scoped_ptr.h" |
| 16 #include "base/memory/singleton.h" | 16 #include "base/memory/singleton.h" |
| 17 #include "base/shared_memory.h" | 17 #include "base/shared_memory.h" |
| 18 #include "base/string16.h" | 18 #include "base/string16.h" |
| 19 #include "content/common/content_export.h" | 19 #include "content/public/common/child_process_host.h" |
| 20 #include "ipc/ipc_channel_proxy.h" | |
| 21 | 20 |
| 22 class FilePath; | 21 class FilePath; |
| 23 | 22 |
| 24 namespace content { | 23 namespace content { |
| 25 class ChildProcessHostDelegate; | 24 class ChildProcessHostDelegate; |
| 26 } | |
| 27 | 25 |
| 28 // Provides common functionality for hosting a child process and processing IPC | 26 // Provides common functionality for hosting a child process and processing IPC |
| 29 // messages between the host and the child process. Users are responsible | 27 // messages between the host and the child process. Users are responsible |
| 30 // for the actual launching and terminating of the child processes. | 28 // for the actual launching and terminating of the child processes. |
| 31 class CONTENT_EXPORT ChildProcessHost : public IPC::Channel::Listener, | 29 class CONTENT_EXPORT ChildProcessHostImpl : public ChildProcessHost, |
| 32 public IPC::Message::Sender { | 30 public IPC::Channel::Listener { |
| 33 public: | 31 public: |
| 34 // These flags may be passed to GetChildPath in order to alter its behavior, | 32 virtual ~ChildProcessHostImpl(); |
| 35 // causing it to return a child path more suited to a specific task. | |
| 36 enum { | |
| 37 // No special behavior requested. | |
| 38 CHILD_NORMAL = 0, | |
| 39 | |
| 40 #if defined(OS_LINUX) | |
| 41 // Indicates that the child execed after forking may be execced from | |
| 42 // /proc/self/exe rather than using the "real" app path. This prevents | |
| 43 // autoupdate from confusing us if it changes the file out from under us. | |
| 44 // You will generally want to set this on Linux, except when there is an | |
| 45 // override to the command line (for example, we're forking a renderer in | |
| 46 // gdb). In this case, you'd use GetChildPath to get the real executable | |
| 47 // file name, and then prepend the GDB command to the command line. | |
| 48 CHILD_ALLOW_SELF = 1 << 0, | |
| 49 #elif defined(OS_MACOSX) | |
| 50 | |
| 51 // Requests that the child run in a process that does not have the | |
| 52 // PIE (position-independent executable) bit set, effectively disabling | |
| 53 // ASLR. For process types that need to allocate a large contiguous | |
| 54 // region, ASLR may not leave a large enough "hole" for the purpose. This | |
| 55 // option should be used sparingly, and only when absolutely necessary. | |
| 56 // This option is currently incompatible with CHILD_ALLOW_HEAP_EXECUTION. | |
| 57 CHILD_NO_PIE = 1 << 1, | |
| 58 | |
| 59 // Requests that the child run in a process that does not protect the | |
| 60 // heap against execution. Normally, heap pages may be made executable | |
| 61 // with mprotect, so this mode should be used sparingly. It is intended | |
| 62 // for processes that may host plug-ins that expect an executable heap | |
| 63 // without having to call mprotect. This option is currently incompatible | |
| 64 // with CHILD_NO_PIE. | |
| 65 CHILD_ALLOW_HEAP_EXECUTION = 1 << 2, | |
| 66 #endif | |
| 67 }; | |
| 68 | |
| 69 virtual ~ChildProcessHost(); | |
| 70 | |
| 71 // Returns the pathname to be used for a child process. If a subprocess | |
| 72 // pathname was specified on the command line, that will be used. Otherwise, | |
| 73 // the default child process pathname will be returned. On most platforms, | |
| 74 // this will be the same as the currently-executing process. | |
| 75 // | |
| 76 // The |flags| argument accepts one or more flags such as CHILD_ALLOW_SELF | |
| 77 // and CHILD_ALLOW_HEAP_EXECUTION as defined above. Pass only CHILD_NORMAL | |
| 78 // if none of these special behaviors are required. | |
| 79 // | |
| 80 // On failure, returns an empty FilePath. | |
| 81 static FilePath GetChildPath(int flags); | |
| 82 | |
| 83 explicit ChildProcessHost(content::ChildProcessHostDelegate* delegate); | |
| 84 | |
| 85 // IPC::Message::Sender implementation. | |
| 86 virtual bool Send(IPC::Message* message) OVERRIDE; | |
| 87 | |
| 88 // Adds an IPC message filter. A reference will be kept to the filter. | |
| 89 void AddFilter(IPC::ChannelProxy::MessageFilter* filter); | |
| 90 | 33 |
| 91 // Public and static for reuse by RenderMessageFilter. | 34 // Public and static for reuse by RenderMessageFilter. |
| 92 static void AllocateSharedMemory( | 35 static void AllocateSharedMemory( |
| 93 uint32 buffer_size, base::ProcessHandle child_process, | 36 uint32 buffer_size, base::ProcessHandle child_process, |
| 94 base::SharedMemoryHandle* handle); | 37 base::SharedMemoryHandle* handle); |
| 95 | 38 |
| 96 // Generates a unique channel name for a child process. | 39 // Generates a unique channel name for a child process. |
| 97 // The "instance" pointer value is baked into the channel id. | 40 // The "instance" pointer value is baked into the channel id. |
| 98 static std::string GenerateRandomChannelID(void* instance); | 41 static std::string GenerateRandomChannelID(void* instance); |
| 99 | 42 |
| 100 // Returns a unique ID to identify a child process. On construction, this | 43 // Returns a unique ID to identify a child process. On construction, this |
| 101 // function will be used to generate the id_, but it is also used to generate | 44 // function will be used to generate the id_, but it is also used to generate |
| 102 // IDs for the RenderProcessHost, which doesn't inherit from us, and whose IDs | 45 // IDs for the RenderProcessHost, which doesn't inherit from us, and whose IDs |
| 103 // must be unique for all child processes. | 46 // must be unique for all child processes. |
| 104 // | 47 // |
| 105 // This function is threadsafe since RenderProcessHost is on the UI thread, | 48 // This function is threadsafe since RenderProcessHost is on the UI thread, |
| 106 // but normally this will be used on the IO thread. | 49 // but normally this will be used on the IO thread. |
| 107 static int GenerateChildProcessUniqueId(); | 50 static int GenerateChildProcessUniqueId(); |
| 108 | 51 |
| 109 // Send the shutdown message to the child process. | 52 // ChildProcessHost implementation |
| 110 // Does not check if CanShutdown is true. | 53 virtual bool Send(IPC::Message* message) OVERRIDE; |
| 111 void ForceShutdown(); | 54 virtual void ForceShutdown() OVERRIDE; |
| 112 | 55 virtual std::string CreateChannel() OVERRIDE; |
| 113 // Creates the IPC channel. Returns true iff it succeeded. | 56 virtual bool IsChannelOpening() OVERRIDE; |
| 114 bool CreateChannel(); | 57 virtual void AddFilter(IPC::ChannelProxy::MessageFilter* filter) OVERRIDE; |
| 115 | 58 #if defined(OS_POSIX) |
| 116 bool opening_channel() { return opening_channel_; } | 59 virtual int TakeClientFileDescriptor() OVERRIDE; |
| 117 const std::string& channel_id() { return channel_id_; } | 60 #endif |
| 118 IPC::Channel* channel() { return channel_.get(); } | |
| 119 | 61 |
| 120 private: | 62 private: |
| 63 friend class ChildProcessHost; |
| 64 |
| 65 explicit ChildProcessHostImpl(ChildProcessHostDelegate* delegate); |
| 66 |
| 121 // IPC::Channel::Listener methods: | 67 // IPC::Channel::Listener methods: |
| 122 virtual bool OnMessageReceived(const IPC::Message& msg) OVERRIDE; | 68 virtual bool OnMessageReceived(const IPC::Message& msg) OVERRIDE; |
| 123 virtual void OnChannelConnected(int32 peer_pid) OVERRIDE; | 69 virtual void OnChannelConnected(int32 peer_pid) OVERRIDE; |
| 124 virtual void OnChannelError() OVERRIDE; | 70 virtual void OnChannelError() OVERRIDE; |
| 125 | 71 |
| 126 // Message handlers: | 72 // Message handlers: |
| 127 void OnShutdownRequest(); | 73 void OnShutdownRequest(); |
| 128 void OnAllocateSharedMemory(uint32 buffer_size, | 74 void OnAllocateSharedMemory(uint32 buffer_size, |
| 129 base::SharedMemoryHandle* handle); | 75 base::SharedMemoryHandle* handle); |
| 130 | 76 |
| 131 content::ChildProcessHostDelegate* delegate_; | 77 ChildProcessHostDelegate* delegate_; |
| 132 base::ProcessHandle peer_handle_; | 78 base::ProcessHandle peer_handle_; |
| 133 bool opening_channel_; // True while we're waiting the channel to be opened. | 79 bool opening_channel_; // True while we're waiting the channel to be opened. |
| 134 scoped_ptr<IPC::Channel> channel_; | 80 scoped_ptr<IPC::Channel> channel_; |
| 135 std::string channel_id_; | 81 std::string channel_id_; |
| 136 | 82 |
| 137 // Holds all the IPC message filters. Since this object lives on the IO | 83 // Holds all the IPC message filters. Since this object lives on the IO |
| 138 // thread, we don't have a IPC::ChannelProxy and so we manage filters | 84 // thread, we don't have a IPC::ChannelProxy and so we manage filters |
| 139 // manually. | 85 // manually. |
| 140 std::vector<scoped_refptr<IPC::ChannelProxy::MessageFilter> > filters_; | 86 std::vector<scoped_refptr<IPC::ChannelProxy::MessageFilter> > filters_; |
| 141 | 87 |
| 142 DISALLOW_COPY_AND_ASSIGN(ChildProcessHost); | 88 DISALLOW_COPY_AND_ASSIGN(ChildProcessHostImpl); |
| 143 }; | 89 }; |
| 144 | 90 |
| 145 #endif // CONTENT_COMMON_CHILD_PROCESS_HOST_H_ | 91 } // namespace content |
| 92 |
| 93 #endif // CONTENT_COMMON_CHILD_PROCESS_HOST_IMPL_H_ |
| OLD | NEW |