| OLD | NEW |
| (Empty) |
| 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. | |
| 4 | |
| 5 #ifndef CONTENT_COMMON_CHILD_PROCESS_HOST_H_ | |
| 6 #define CONTENT_COMMON_CHILD_PROCESS_HOST_H_ | |
| 7 #pragma once | |
| 8 | |
| 9 #include <string> | |
| 10 #include <vector> | |
| 11 | |
| 12 #include "build/build_config.h" | |
| 13 | |
| 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/common/content_export.h" | |
| 20 #include "ipc/ipc_channel_proxy.h" | |
| 21 | |
| 22 class FilePath; | |
| 23 | |
| 24 namespace content { | |
| 25 class ChildProcessHostDelegate; | |
| 26 } | |
| 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. | |
| 31 class CONTENT_EXPORT ChildProcessHost : public IPC::Channel::Listener, | |
| 32 public IPC::Message::Sender { | |
| 33 public: | |
| 34 // These flags may be passed to GetChildPath in order to alter its behavior, | |
| 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 | |
| 91 // Public and static for reuse by RenderMessageFilter. | |
| 92 static void AllocateSharedMemory( | |
| 93 uint32 buffer_size, base::ProcessHandle child_process, | |
| 94 base::SharedMemoryHandle* handle); | |
| 95 | |
| 96 // Generates a unique channel name for a child process. | |
| 97 // The "instance" pointer value is baked into the channel id. | |
| 98 static std::string GenerateRandomChannelID(void* instance); | |
| 99 | |
| 100 // 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 | |
| 102 // IDs for the RenderProcessHost, which doesn't inherit from us, and whose IDs | |
| 103 // must be unique for all child processes. | |
| 104 // | |
| 105 // This function is threadsafe since RenderProcessHost is on the UI thread, | |
| 106 // but normally this will be used on the IO thread. | |
| 107 static int GenerateChildProcessUniqueId(); | |
| 108 | |
| 109 // Send the shutdown message to the child process. | |
| 110 // Does not check if CanShutdown is true. | |
| 111 void ForceShutdown(); | |
| 112 | |
| 113 // Creates the IPC channel. Returns true iff it succeeded. | |
| 114 bool CreateChannel(); | |
| 115 | |
| 116 bool opening_channel() { return opening_channel_; } | |
| 117 const std::string& channel_id() { return channel_id_; } | |
| 118 IPC::Channel* channel() { return channel_.get(); } | |
| 119 | |
| 120 private: | |
| 121 // IPC::Channel::Listener methods: | |
| 122 virtual bool OnMessageReceived(const IPC::Message& msg) OVERRIDE; | |
| 123 virtual void OnChannelConnected(int32 peer_pid) OVERRIDE; | |
| 124 virtual void OnChannelError() OVERRIDE; | |
| 125 | |
| 126 // Message handlers: | |
| 127 void OnShutdownRequest(); | |
| 128 void OnAllocateSharedMemory(uint32 buffer_size, | |
| 129 base::SharedMemoryHandle* handle); | |
| 130 | |
| 131 content::ChildProcessHostDelegate* delegate_; | |
| 132 base::ProcessHandle peer_handle_; | |
| 133 bool opening_channel_; // True while we're waiting the channel to be opened. | |
| 134 scoped_ptr<IPC::Channel> channel_; | |
| 135 std::string channel_id_; | |
| 136 | |
| 137 // 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 | |
| 139 // manually. | |
| 140 std::vector<scoped_refptr<IPC::ChannelProxy::MessageFilter> > filters_; | |
| 141 | |
| 142 DISALLOW_COPY_AND_ASSIGN(ChildProcessHost); | |
| 143 }; | |
| 144 | |
| 145 #endif // CONTENT_COMMON_CHILD_PROCESS_HOST_H_ | |
| OLD | NEW |