[email protected] | 5c41e6e1 | 2012-03-17 02:20:46 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | d27893f6 | 2010-07-03 05:47:42 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | 4734d0b | 2011-12-03 07:10:44 | [diff] [blame] | 5 | #include "content/common/child_process_host_impl.h" |
[email protected] | d27893f6 | 2010-07-03 05:47:42 | [diff] [blame] | 6 | |
[email protected] | 4306c379 | 2011-12-02 01:57:53 | [diff] [blame] | 7 | #include <limits> |
| 8 | |
[email protected] | 3cb7cc953 | 2014-01-08 22:55:51 | [diff] [blame] | 9 | #include "base/atomic_sequence_num.h" |
[email protected] | d27893f6 | 2010-07-03 05:47:42 | [diff] [blame] | 10 | #include "base/command_line.h" |
[email protected] | 5799981 | 2013-02-24 05:40:52 | [diff] [blame] | 11 | #include "base/files/file_path.h" |
ssid | 1050d80 | 2015-07-28 20:28:14 | [diff] [blame] | 12 | #include "base/hash.h" |
[email protected] | 8c40f32 | 2011-08-24 03:33:36 | [diff] [blame] | 13 | #include "base/logging.h" |
erikchen | a91d0513 | 2016-03-21 23:19:40 | [diff] [blame] | 14 | #include "base/message_loop/message_loop.h" |
[email protected] | 835d7c8 | 2010-10-14 04:38:38 | [diff] [blame] | 15 | #include "base/metrics/histogram.h" |
reveman | 22dd929 | 2014-10-13 20:52:05 | [diff] [blame] | 16 | #include "base/numerics/safe_math.h" |
[email protected] | d27893f6 | 2010-07-03 05:47:42 | [diff] [blame] | 17 | #include "base/path_service.h" |
[email protected] | 71ed8e02 | 2013-07-25 14:39:57 | [diff] [blame] | 18 | #include "base/process/process_metrics.h" |
[email protected] | 4306c379 | 2011-12-02 01:57:53 | [diff] [blame] | 19 | #include "base/rand_util.h" |
[email protected] | 30fe1f9 | 2013-06-12 16:34:34 | [diff] [blame] | 20 | #include "base/strings/stringprintf.h" |
erikchen | 5708aae | 2015-09-14 17:45:12 | [diff] [blame] | 21 | #include "base/synchronization/lock.h" |
[email protected] | a5f28b4 | 2011-01-17 10:30:13 | [diff] [blame] | 22 | #include "base/third_party/dynamic_annotations/dynamic_annotations.h" |
avi | a9aa7a8 | 2015-12-25 03:06:31 | [diff] [blame] | 23 | #include "build/build_config.h" |
[email protected] | ff47b296 | 2011-03-07 23:51:49 | [diff] [blame] | 24 | #include "content/common/child_process_messages.h" |
[email protected] | 4cb4310 | 2011-12-02 20:24:49 | [diff] [blame] | 25 | #include "content/public/common/child_process_host_delegate.h" |
[email protected] | 5d921d4c | 2011-10-21 22:26:14 | [diff] [blame] | 26 | #include "content/public/common/content_paths.h" |
[email protected] | c08950d2 | 2011-10-13 22:20:29 | [diff] [blame] | 27 | #include "content/public/common/content_switches.h" |
penghuang | 346a46f9 | 2016-03-31 21:37:52 | [diff] [blame] | 28 | #include "gpu/ipc/client/gpu_memory_buffer_impl_shared_memory.h" |
erikchen | 87397e0 | 2015-10-10 01:10:24 | [diff] [blame] | 29 | #include "ipc/attachment_broker.h" |
erikchen | 9a19977 | 2015-11-03 19:47:28 | [diff] [blame] | 30 | #include "ipc/attachment_broker_privileged.h" |
[email protected] | 5c41e6e1 | 2012-03-17 02:20:46 | [diff] [blame] | 31 | #include "ipc/ipc_channel.h" |
amistry | d4aa70d | 2016-06-23 07:52:37 | [diff] [blame] | 32 | #include "ipc/ipc_channel_mojo.h" |
[email protected] | a83d4229 | 2010-08-17 22:51:10 | [diff] [blame] | 33 | #include "ipc/ipc_logging.h" |
[email protected] | 7412204 | 2014-04-25 00:07:30 | [diff] [blame] | 34 | #include "ipc/message_filter.h" |
amistry | ab52b0c | 2016-06-07 04:22:57 | [diff] [blame] | 35 | #include "mojo/edk/embedder/embedder.h" |
[email protected] | d27893f6 | 2010-07-03 05:47:42 | [diff] [blame] | 36 | |
| 37 | #if defined(OS_LINUX) |
| 38 | #include "base/linux_util.h" |
[email protected] | e35c9a8 | 2011-12-01 18:48:41 | [diff] [blame] | 39 | #elif defined(OS_WIN) |
| 40 | #include "content/common/font_cache_dispatcher_win.h" |
[email protected] | d27893f6 | 2010-07-03 05:47:42 | [diff] [blame] | 41 | #endif // OS_LINUX |
| 42 | |
[email protected] | 8c40f32 | 2011-08-24 03:33:36 | [diff] [blame] | 43 | namespace { |
| 44 | |
[email protected] | 3cb7cc953 | 2014-01-08 22:55:51 | [diff] [blame] | 45 | // Global atomic to generate child process unique IDs. |
| 46 | base::StaticAtomicSequenceNumber g_unique_id; |
[email protected] | 8c40f32 | 2011-08-24 03:33:36 | [diff] [blame] | 47 | |
| 48 | } // namespace |
[email protected] | 8c40f32 | 2011-08-24 03:33:36 | [diff] [blame] | 49 | |
[email protected] | 4734d0b | 2011-12-03 07:10:44 | [diff] [blame] | 50 | namespace content { |
[email protected] | d27893f6 | 2010-07-03 05:47:42 | [diff] [blame] | 51 | |
[email protected] | 3cb7cc953 | 2014-01-08 22:55:51 | [diff] [blame] | 52 | int ChildProcessHost::kInvalidUniqueID = -1; |
[email protected] | d7c7c98a | 2012-07-12 21:27:44 | [diff] [blame] | 53 | |
avi | a9aa7a8 | 2015-12-25 03:06:31 | [diff] [blame] | 54 | uint64_t ChildProcessHost::kBrowserTracingProcessId = |
| 55 | std::numeric_limits<uint64_t>::max(); |
ssid | 1050d80 | 2015-07-28 20:28:14 | [diff] [blame] | 56 | |
[email protected] | 4734d0b | 2011-12-03 07:10:44 | [diff] [blame] | 57 | // static |
| 58 | ChildProcessHost* ChildProcessHost::Create(ChildProcessHostDelegate* delegate) { |
| 59 | return new ChildProcessHostImpl(delegate); |
[email protected] | d27893f6 | 2010-07-03 05:47:42 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | // static |
[email protected] | a732916 | 2013-02-07 19:21:48 | [diff] [blame] | 63 | base::FilePath ChildProcessHost::GetChildPath(int flags) { |
| 64 | base::FilePath child_path; |
[email protected] | d27893f6 | 2010-07-03 05:47:42 | [diff] [blame] | 65 | |
[email protected] | 47927870 | 2014-08-11 20:32:09 | [diff] [blame] | 66 | child_path = base::CommandLine::ForCurrentProcess()->GetSwitchValuePath( |
[email protected] | d27893f6 | 2010-07-03 05:47:42 | [diff] [blame] | 67 | switches::kBrowserSubprocessPath); |
[email protected] | d27893f6 | 2010-07-03 05:47:42 | [diff] [blame] | 68 | |
[email protected] | d27893f6 | 2010-07-03 05:47:42 | [diff] [blame] | 69 | #if defined(OS_LINUX) |
| 70 | // Use /proc/self/exe rather than our known binary path so updates |
| 71 | // can't swap out the binary from underneath us. |
[email protected] | a5f28b4 | 2011-01-17 10:30:13 | [diff] [blame] | 72 | // When running under Valgrind, forking /proc/self/exe ends up forking the |
| 73 | // Valgrind executable, which then crashes. However, it's almost safe to |
| 74 | // assume that the updates won't happen while testing with Valgrind tools. |
[email protected] | bfd4748 | 2011-08-24 13:57:26 | [diff] [blame] | 75 | if (child_path.empty() && flags & CHILD_ALLOW_SELF && !RunningOnValgrind()) |
[email protected] | a732916 | 2013-02-07 19:21:48 | [diff] [blame] | 76 | child_path = base::FilePath(base::kProcSelfExe); |
[email protected] | d27893f6 | 2010-07-03 05:47:42 | [diff] [blame] | 77 | #endif |
| 78 | |
| 79 | // On most platforms, the child executable is the same as the current |
| 80 | // executable. |
[email protected] | bfd4748 | 2011-08-24 13:57:26 | [diff] [blame] | 81 | if (child_path.empty()) |
[email protected] | dcc72db | 2013-01-02 00:44:18 | [diff] [blame] | 82 | PathService::Get(CHILD_PROCESS_EXE, &child_path); |
[email protected] | d27893f6 | 2010-07-03 05:47:42 | [diff] [blame] | 83 | return child_path; |
| 84 | } |
| 85 | |
[email protected] | 4734d0b | 2011-12-03 07:10:44 | [diff] [blame] | 86 | ChildProcessHostImpl::ChildProcessHostImpl(ChildProcessHostDelegate* delegate) |
| 87 | : delegate_(delegate), |
[email protected] | 4734d0b | 2011-12-03 07:10:44 | [diff] [blame] | 88 | opening_channel_(false) { |
| 89 | #if defined(OS_WIN) |
| 90 | AddFilter(new FontCacheDispatcher()); |
| 91 | #endif |
erikchen | 9a19977 | 2015-11-03 19:47:28 | [diff] [blame] | 92 | |
| 93 | #if USE_ATTACHMENT_BROKER |
jam | e1f453c | 2016-03-21 15:51:34 | [diff] [blame] | 94 | #if defined(OS_MACOSX) |
erikchen | 9a19977 | 2015-11-03 19:47:28 | [diff] [blame] | 95 | // On Mac, the privileged AttachmentBroker needs a reference to the Mach port |
| 96 | // Provider, which is only available in the chrome/ module. The attachment |
| 97 | // broker must already be created. |
| 98 | DCHECK(IPC::AttachmentBroker::GetGlobal()); |
| 99 | #else |
| 100 | // Construct the privileged attachment broker early in the life cycle of a |
| 101 | // child process. |
| 102 | IPC::AttachmentBrokerPrivileged::CreateBrokerIfNeeded(); |
jam | e1f453c | 2016-03-21 15:51:34 | [diff] [blame] | 103 | #endif // defined(OS_MACOSX) |
erikchen | 9a19977 | 2015-11-03 19:47:28 | [diff] [blame] | 104 | #endif // USE_ATTACHMENT_BROKER |
[email protected] | 4734d0b | 2011-12-03 07:10:44 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | ChildProcessHostImpl::~ChildProcessHostImpl() { |
chrisha | e995038 | 2015-12-07 20:51:56 | [diff] [blame] | 108 | // If a channel was never created than it wasn't registered and the filters |
| 109 | // weren't notified. For the sake of symmetry don't call the matching teardown |
| 110 | // functions. This is analogous to how RenderProcessHostImpl handles things. |
| 111 | if (!channel_) |
| 112 | return; |
| 113 | |
erikchen | 1bfccba | 2015-07-31 01:09:27 | [diff] [blame] | 114 | #if USE_ATTACHMENT_BROKER |
erikchen | d8a3d902 | 2015-10-22 20:02:02 | [diff] [blame] | 115 | IPC::AttachmentBroker::GetGlobal()->DeregisterCommunicationChannel( |
| 116 | channel_.get()); |
erikchen | 1bfccba | 2015-07-31 01:09:27 | [diff] [blame] | 117 | #endif |
[email protected] | 4734d0b | 2011-12-03 07:10:44 | [diff] [blame] | 118 | for (size_t i = 0; i < filters_.size(); ++i) { |
| 119 | filters_[i]->OnChannelClosing(); |
| 120 | filters_[i]->OnFilterRemoved(); |
| 121 | } |
[email protected] | 4734d0b | 2011-12-03 07:10:44 | [diff] [blame] | 122 | } |
| 123 | |
[email protected] | 7412204 | 2014-04-25 00:07:30 | [diff] [blame] | 124 | void ChildProcessHostImpl::AddFilter(IPC::MessageFilter* filter) { |
[email protected] | 4734d0b | 2011-12-03 07:10:44 | [diff] [blame] | 125 | filters_.push_back(filter); |
| 126 | |
[email protected] | 59383c78 | 2013-04-17 16:43:27 | [diff] [blame] | 127 | if (channel_) |
[email protected] | 4734d0b | 2011-12-03 07:10:44 | [diff] [blame] | 128 | filter->OnFilterAdded(channel_.get()); |
| 129 | } |
| 130 | |
| 131 | void ChildProcessHostImpl::ForceShutdown() { |
[email protected] | bdae981 | 2011-10-15 00:33:03 | [diff] [blame] | 132 | Send(new ChildProcessMsg_Shutdown()); |
| 133 | } |
[email protected] | 91443fc | 2010-07-14 05:08:46 | [diff] [blame] | 134 | |
amistry | ab52b0c | 2016-06-07 04:22:57 | [diff] [blame] | 135 | std::string ChildProcessHostImpl::CreateChannelMojo( |
| 136 | const std::string& child_token) { |
| 137 | DCHECK(channel_id_.empty()); |
| 138 | channel_id_ = mojo::edk::GenerateRandomToken(); |
| 139 | mojo::ScopedMessagePipeHandle host_handle = |
| 140 | mojo::edk::CreateParentMessagePipe(channel_id_, child_token); |
| 141 | channel_ = IPC::ChannelMojo::Create(std::move(host_handle), |
| 142 | IPC::Channel::MODE_SERVER, this); |
| 143 | if (!channel_ || !InitChannel()) |
| 144 | return std::string(); |
| 145 | |
| 146 | return channel_id_; |
| 147 | } |
| 148 | |
[email protected] | 4734d0b | 2011-12-03 07:10:44 | [diff] [blame] | 149 | std::string ChildProcessHostImpl::CreateChannel() { |
amistry | ab52b0c | 2016-06-07 04:22:57 | [diff] [blame] | 150 | DCHECK(channel_id_.empty()); |
[email protected] | 5c41e6e1 | 2012-03-17 02:20:46 | [diff] [blame] | 151 | channel_id_ = IPC::Channel::GenerateVerifiedChannelID(std::string()); |
erikchen | 5708aae | 2015-09-14 17:45:12 | [diff] [blame] | 152 | channel_ = IPC::Channel::CreateServer(channel_id_, this); |
amistry | ab52b0c | 2016-06-07 04:22:57 | [diff] [blame] | 153 | if (!channel_ || !InitChannel()) |
| 154 | return std::string(); |
| 155 | |
| 156 | return channel_id_; |
| 157 | } |
| 158 | |
| 159 | bool ChildProcessHostImpl::InitChannel() { |
erikchen | 1bfccba | 2015-07-31 01:09:27 | [diff] [blame] | 160 | #if USE_ATTACHMENT_BROKER |
erikchen | d8a3d902 | 2015-10-22 20:02:02 | [diff] [blame] | 161 | IPC::AttachmentBroker::GetGlobal()->RegisterCommunicationChannel( |
erikchen | a91d0513 | 2016-03-21 23:19:40 | [diff] [blame] | 162 | channel_.get(), base::MessageLoopForIO::current()->task_runner()); |
erikchen | 1bfccba | 2015-07-31 01:09:27 | [diff] [blame] | 163 | #endif |
erikchen | 9097190 | 2016-04-25 23:45:31 | [diff] [blame] | 164 | if (!channel_->Connect()) { |
| 165 | #if USE_ATTACHMENT_BROKER |
| 166 | IPC::AttachmentBroker::GetGlobal()->DeregisterCommunicationChannel( |
| 167 | channel_.get()); |
| 168 | #endif |
amistry | ab52b0c | 2016-06-07 04:22:57 | [diff] [blame] | 169 | return false; |
erikchen | 9097190 | 2016-04-25 23:45:31 | [diff] [blame] | 170 | } |
[email protected] | d27893f6 | 2010-07-03 05:47:42 | [diff] [blame] | 171 | |
[email protected] | 8e2b647 | 2010-12-15 22:19:48 | [diff] [blame] | 172 | for (size_t i = 0; i < filters_.size(); ++i) |
| 173 | filters_[i]->OnFilterAdded(channel_.get()); |
| 174 | |
[email protected] | 91443fc | 2010-07-14 05:08:46 | [diff] [blame] | 175 | // Make sure these messages get sent first. |
| 176 | #if defined(IPC_MESSAGE_LOG_ENABLED) |
[email protected] | 8e8bb6d | 2010-12-13 08:18:55 | [diff] [blame] | 177 | bool enabled = IPC::Logging::GetInstance()->Enabled(); |
[email protected] | ff47b296 | 2011-03-07 23:51:49 | [diff] [blame] | 178 | Send(new ChildProcessMsg_SetIPCLoggingEnabled(enabled)); |
[email protected] | 91443fc | 2010-07-14 05:08:46 | [diff] [blame] | 179 | #endif |
| 180 | |
[email protected] | d27893f6 | 2010-07-03 05:47:42 | [diff] [blame] | 181 | opening_channel_ = true; |
| 182 | |
amistry | ab52b0c | 2016-06-07 04:22:57 | [diff] [blame] | 183 | return true; |
[email protected] | d27893f6 | 2010-07-03 05:47:42 | [diff] [blame] | 184 | } |
| 185 | |
[email protected] | 4734d0b | 2011-12-03 07:10:44 | [diff] [blame] | 186 | bool ChildProcessHostImpl::IsChannelOpening() { |
| 187 | return opening_channel_; |
| 188 | } |
| 189 | |
| 190 | #if defined(OS_POSIX) |
morrita | a409ccc | 2014-10-20 23:53:25 | [diff] [blame] | 191 | base::ScopedFD ChildProcessHostImpl::TakeClientFileDescriptor() { |
[email protected] | 4734d0b | 2011-12-03 07:10:44 | [diff] [blame] | 192 | return channel_->TakeClientFileDescriptor(); |
| 193 | } |
| 194 | #endif |
| 195 | |
| 196 | bool ChildProcessHostImpl::Send(IPC::Message* message) { |
[email protected] | 59383c78 | 2013-04-17 16:43:27 | [diff] [blame] | 197 | if (!channel_) { |
[email protected] | 8e2b647 | 2010-12-15 22:19:48 | [diff] [blame] | 198 | delete message; |
[email protected] | d27893f6 | 2010-07-03 05:47:42 | [diff] [blame] | 199 | return false; |
| 200 | } |
[email protected] | 8e2b647 | 2010-12-15 22:19:48 | [diff] [blame] | 201 | return channel_->Send(message); |
[email protected] | d27893f6 | 2010-07-03 05:47:42 | [diff] [blame] | 202 | } |
| 203 | |
[email protected] | 4734d0b | 2011-12-03 07:10:44 | [diff] [blame] | 204 | void ChildProcessHostImpl::AllocateSharedMemory( |
[email protected] | 3f89283 | 2013-01-11 03:23:59 | [diff] [blame] | 205 | size_t buffer_size, base::ProcessHandle child_process_handle, |
[email protected] | bdae981 | 2011-10-15 00:33:03 | [diff] [blame] | 206 | base::SharedMemoryHandle* shared_memory_handle) { |
| 207 | base::SharedMemory shared_buf; |
[email protected] | e607e2c1 | 2013-09-13 09:46:58 | [diff] [blame] | 208 | if (!shared_buf.CreateAnonymous(buffer_size)) { |
[email protected] | bdae981 | 2011-10-15 00:33:03 | [diff] [blame] | 209 | *shared_memory_handle = base::SharedMemory::NULLHandle(); |
[email protected] | e607e2c1 | 2013-09-13 09:46:58 | [diff] [blame] | 210 | NOTREACHED() << "Cannot create shared memory buffer"; |
[email protected] | bdae981 | 2011-10-15 00:33:03 | [diff] [blame] | 211 | return; |
| 212 | } |
| 213 | shared_buf.GiveToProcess(child_process_handle, shared_memory_handle); |
| 214 | } |
| 215 | |
[email protected] | 4734d0b | 2011-12-03 07:10:44 | [diff] [blame] | 216 | int ChildProcessHostImpl::GenerateChildProcessUniqueId() { |
[email protected] | 4306c379 | 2011-12-02 01:57:53 | [diff] [blame] | 217 | // This function must be threadsafe. |
[email protected] | d7c7c98a | 2012-07-12 21:27:44 | [diff] [blame] | 218 | // |
[email protected] | 3cb7cc953 | 2014-01-08 22:55:51 | [diff] [blame] | 219 | // Historically, this function returned ids started with 1, so in several |
| 220 | // places in the code a value of 0 (rather than kInvalidUniqueID) was used as |
| 221 | // an invalid value. So we retain those semantics. |
| 222 | int id = g_unique_id.GetNext() + 1; |
[email protected] | d7c7c98a | 2012-07-12 21:27:44 | [diff] [blame] | 223 | |
[email protected] | 3cb7cc953 | 2014-01-08 22:55:51 | [diff] [blame] | 224 | CHECK_NE(0, id); |
| 225 | CHECK_NE(kInvalidUniqueID, id); |
[email protected] | d7c7c98a | 2012-07-12 21:27:44 | [diff] [blame] | 226 | |
| 227 | return id; |
[email protected] | 4306c379 | 2011-12-02 01:57:53 | [diff] [blame] | 228 | } |
| 229 | |
avi | a9aa7a8 | 2015-12-25 03:06:31 | [diff] [blame] | 230 | uint64_t ChildProcessHostImpl::ChildProcessUniqueIdToTracingProcessId( |
ssid | 1050d80 | 2015-07-28 20:28:14 | [diff] [blame] | 231 | int child_process_id) { |
| 232 | // In single process mode, all the children are hosted in the same process, |
| 233 | // therefore the generated memory dump guids should not be conditioned by the |
| 234 | // child process id. The clients need not be aware of SPM and the conversion |
| 235 | // takes care of the SPM special case while translating child process ids to |
| 236 | // tracing process ids. |
| 237 | if (base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 238 | switches::kSingleProcess)) |
| 239 | return ChildProcessHost::kBrowserTracingProcessId; |
| 240 | |
| 241 | // The hash value is incremented so that the tracing id is never equal to |
| 242 | // MemoryDumpManager::kInvalidTracingProcessId. |
avi | a9aa7a8 | 2015-12-25 03:06:31 | [diff] [blame] | 243 | return static_cast<uint64_t>( |
ssid | 1050d80 | 2015-07-28 20:28:14 | [diff] [blame] | 244 | base::Hash(reinterpret_cast<const char*>(&child_process_id), |
| 245 | sizeof(child_process_id))) + |
| 246 | 1; |
| 247 | } |
| 248 | |
[email protected] | 4734d0b | 2011-12-03 07:10:44 | [diff] [blame] | 249 | bool ChildProcessHostImpl::OnMessageReceived(const IPC::Message& msg) { |
[email protected] | d27893f6 | 2010-07-03 05:47:42 | [diff] [blame] | 250 | #ifdef IPC_MESSAGE_LOG_ENABLED |
[email protected] | 8e8bb6d | 2010-12-13 08:18:55 | [diff] [blame] | 251 | IPC::Logging* logger = IPC::Logging::GetInstance(); |
[email protected] | d27893f6 | 2010-07-03 05:47:42 | [diff] [blame] | 252 | if (msg.type() == IPC_LOGGING_ID) { |
| 253 | logger->OnReceivedLoggingMessage(msg); |
[email protected] | a95986a8 | 2010-12-24 06:19:28 | [diff] [blame] | 254 | return true; |
[email protected] | d27893f6 | 2010-07-03 05:47:42 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | if (logger->Enabled()) |
| 258 | logger->OnPreDispatchMessage(msg); |
| 259 | #endif |
| 260 | |
[email protected] | 8e2b647 | 2010-12-15 22:19:48 | [diff] [blame] | 261 | bool handled = false; |
[email protected] | 4cb4310 | 2011-12-02 20:24:49 | [diff] [blame] | 262 | for (size_t i = 0; i < filters_.size(); ++i) { |
| 263 | if (filters_[i]->OnMessageReceived(msg)) { |
[email protected] | 8e2b647 | 2010-12-15 22:19:48 | [diff] [blame] | 264 | handled = true; |
| 265 | break; |
[email protected] | d27893f6 | 2010-07-03 05:47:42 | [diff] [blame] | 266 | } |
| 267 | } |
| 268 | |
[email protected] | bdae981 | 2011-10-15 00:33:03 | [diff] [blame] | 269 | if (!handled) { |
[email protected] | e525743 | 2010-12-21 01:18:37 | [diff] [blame] | 270 | handled = true; |
[email protected] | 4734d0b | 2011-12-03 07:10:44 | [diff] [blame] | 271 | IPC_BEGIN_MESSAGE_MAP(ChildProcessHostImpl, msg) |
[email protected] | bdae981 | 2011-10-15 00:33:03 | [diff] [blame] | 272 | IPC_MESSAGE_HANDLER(ChildProcessHostMsg_ShutdownRequest, |
| 273 | OnShutdownRequest) |
avi | ef875b52 | 2015-09-11 20:58:51 | [diff] [blame] | 274 | // NB: The SyncAllocateSharedMemory, SyncAllocateGpuMemoryBuffer, and |
| 275 | // DeletedGpuMemoryBuffer IPCs are handled here for non-renderer child |
| 276 | // processes. For renderer processes, they are handled in |
| 277 | // RenderMessageFilter. |
[email protected] | bdae981 | 2011-10-15 00:33:03 | [diff] [blame] | 278 | IPC_MESSAGE_HANDLER(ChildProcessHostMsg_SyncAllocateSharedMemory, |
| 279 | OnAllocateSharedMemory) |
jam | 81366316 | 2015-03-26 16:46:29 | [diff] [blame] | 280 | IPC_MESSAGE_HANDLER(ChildProcessHostMsg_SyncAllocateGpuMemoryBuffer, |
| 281 | OnAllocateGpuMemoryBuffer) |
reveman | 22dd929 | 2014-10-13 20:52:05 | [diff] [blame] | 282 | IPC_MESSAGE_HANDLER(ChildProcessHostMsg_DeletedGpuMemoryBuffer, |
| 283 | OnDeletedGpuMemoryBuffer) |
[email protected] | bdae981 | 2011-10-15 00:33:03 | [diff] [blame] | 284 | IPC_MESSAGE_UNHANDLED(handled = false) |
[email protected] | 4cb4310 | 2011-12-02 20:24:49 | [diff] [blame] | 285 | IPC_END_MESSAGE_MAP() |
[email protected] | e525743 | 2010-12-21 01:18:37 | [diff] [blame] | 286 | |
[email protected] | bdae981 | 2011-10-15 00:33:03 | [diff] [blame] | 287 | if (!handled) |
[email protected] | 4cb4310 | 2011-12-02 20:24:49 | [diff] [blame] | 288 | handled = delegate_->OnMessageReceived(msg); |
[email protected] | bdae981 | 2011-10-15 00:33:03 | [diff] [blame] | 289 | } |
[email protected] | 8e2b647 | 2010-12-15 22:19:48 | [diff] [blame] | 290 | |
[email protected] | d27893f6 | 2010-07-03 05:47:42 | [diff] [blame] | 291 | #ifdef IPC_MESSAGE_LOG_ENABLED |
| 292 | if (logger->Enabled()) |
[email protected] | 4cb4310 | 2011-12-02 20:24:49 | [diff] [blame] | 293 | logger->OnPostDispatchMessage(msg, channel_id_); |
[email protected] | d27893f6 | 2010-07-03 05:47:42 | [diff] [blame] | 294 | #endif |
[email protected] | a95986a8 | 2010-12-24 06:19:28 | [diff] [blame] | 295 | return handled; |
[email protected] | d27893f6 | 2010-07-03 05:47:42 | [diff] [blame] | 296 | } |
| 297 | |
avi | a9aa7a8 | 2015-12-25 03:06:31 | [diff] [blame] | 298 | void ChildProcessHostImpl::OnChannelConnected(int32_t peer_pid) { |
rvargas | 5779b38 | 2014-11-18 20:44:11 | [diff] [blame] | 299 | if (!peer_process_.IsValid()) { |
rvargas | 1c376a8 | 2015-03-16 23:03:52 | [diff] [blame] | 300 | peer_process_ = base::Process::OpenWithExtraPrivileges(peer_pid); |
rvargas | 747ff24 | 2015-01-17 02:46:47 | [diff] [blame] | 301 | if (!peer_process_.IsValid()) |
| 302 | peer_process_ = delegate_->GetProcess().Duplicate(); |
rvargas | 5779b38 | 2014-11-18 20:44:11 | [diff] [blame] | 303 | DCHECK(peer_process_.IsValid()); |
[email protected] | bdae981 | 2011-10-15 00:33:03 | [diff] [blame] | 304 | } |
[email protected] | 4cb4310 | 2011-12-02 20:24:49 | [diff] [blame] | 305 | opening_channel_ = false; |
| 306 | delegate_->OnChannelConnected(peer_pid); |
| 307 | for (size_t i = 0; i < filters_.size(); ++i) |
| 308 | filters_[i]->OnChannelConnected(peer_pid); |
[email protected] | d27893f6 | 2010-07-03 05:47:42 | [diff] [blame] | 309 | } |
| 310 | |
[email protected] | 4734d0b | 2011-12-03 07:10:44 | [diff] [blame] | 311 | void ChildProcessHostImpl::OnChannelError() { |
[email protected] | 4cb4310 | 2011-12-02 20:24:49 | [diff] [blame] | 312 | opening_channel_ = false; |
| 313 | delegate_->OnChannelError(); |
[email protected] | d27893f6 | 2010-07-03 05:47:42 | [diff] [blame] | 314 | |
[email protected] | 4cb4310 | 2011-12-02 20:24:49 | [diff] [blame] | 315 | for (size_t i = 0; i < filters_.size(); ++i) |
| 316 | filters_[i]->OnChannelError(); |
[email protected] | 8e2b647 | 2010-12-15 22:19:48 | [diff] [blame] | 317 | |
[email protected] | d27893f6 | 2010-07-03 05:47:42 | [diff] [blame] | 318 | // This will delete host_, which will also destroy this! |
[email protected] | 4cb4310 | 2011-12-02 20:24:49 | [diff] [blame] | 319 | delegate_->OnChildDisconnected(); |
[email protected] | d27893f6 | 2010-07-03 05:47:42 | [diff] [blame] | 320 | } |
| 321 | |
[email protected] | ef2f6ba | 2014-05-15 23:06:07 | [diff] [blame] | 322 | void ChildProcessHostImpl::OnBadMessageReceived(const IPC::Message& message) { |
| 323 | delegate_->OnBadMessageReceived(message); |
| 324 | } |
| 325 | |
[email protected] | 4734d0b | 2011-12-03 07:10:44 | [diff] [blame] | 326 | void ChildProcessHostImpl::OnAllocateSharedMemory( |
avi | a9aa7a8 | 2015-12-25 03:06:31 | [diff] [blame] | 327 | uint32_t buffer_size, |
[email protected] | bdae981 | 2011-10-15 00:33:03 | [diff] [blame] | 328 | base::SharedMemoryHandle* handle) { |
rvargas | 5779b38 | 2014-11-18 20:44:11 | [diff] [blame] | 329 | AllocateSharedMemory(buffer_size, peer_process_.Handle(), handle); |
[email protected] | bdae981 | 2011-10-15 00:33:03 | [diff] [blame] | 330 | } |
| 331 | |
[email protected] | 4734d0b | 2011-12-03 07:10:44 | [diff] [blame] | 332 | void ChildProcessHostImpl::OnShutdownRequest() { |
[email protected] | 4cb4310 | 2011-12-02 20:24:49 | [diff] [blame] | 333 | if (delegate_->CanShutdown()) |
| 334 | Send(new ChildProcessMsg_Shutdown()); |
[email protected] | d27893f6 | 2010-07-03 05:47:42 | [diff] [blame] | 335 | } |
[email protected] | 4734d0b | 2011-12-03 07:10:44 | [diff] [blame] | 336 | |
[email protected] | bc10f46 | 2013-10-22 23:13:09 | [diff] [blame] | 337 | void ChildProcessHostImpl::OnAllocateGpuMemoryBuffer( |
ericrk | c9984ebe | 2015-08-17 14:22:37 | [diff] [blame] | 338 | gfx::GpuMemoryBufferId id, |
avi | a9aa7a8 | 2015-12-25 03:06:31 | [diff] [blame] | 339 | uint32_t width, |
| 340 | uint32_t height, |
spang | fcf5fab4 | 2015-08-04 19:25:35 | [diff] [blame] | 341 | gfx::BufferFormat format, |
| 342 | gfx::BufferUsage usage, |
jam | 81366316 | 2015-03-26 16:46:29 | [diff] [blame] | 343 | gfx::GpuMemoryBufferHandle* handle) { |
reveman | 22dd929 | 2014-10-13 20:52:05 | [diff] [blame] | 344 | // TODO(reveman): Add support for other types of GpuMemoryBuffers. |
reveman | bc2de2b | 2014-11-17 03:21:32 | [diff] [blame] | 345 | |
| 346 | // AllocateForChildProcess() will check if |width| and |height| are valid |
| 347 | // and handle failure in a controlled way when not. We just need to make |
reveman | 485728ba | 2015-10-23 18:43:22 | [diff] [blame] | 348 | // sure |usage| is supported here. |
penghuang | 346a46f9 | 2016-03-31 21:37:52 | [diff] [blame] | 349 | if (gpu::GpuMemoryBufferImplSharedMemory::IsUsageSupported(usage)) { |
| 350 | *handle = gpu::GpuMemoryBufferImplSharedMemory::AllocateForChildProcess( |
ericrk | c9984ebe | 2015-08-17 14:22:37 | [diff] [blame] | 351 | id, gfx::Size(width, height), format, peer_process_.Handle()); |
reveman | 22dd929 | 2014-10-13 20:52:05 | [diff] [blame] | 352 | } |
reveman | 22dd929 | 2014-10-13 20:52:05 | [diff] [blame] | 353 | } |
| 354 | |
| 355 | void ChildProcessHostImpl::OnDeletedGpuMemoryBuffer( |
reveman | 510a6e0 | 2014-11-05 00:59:18 | [diff] [blame] | 356 | gfx::GpuMemoryBufferId id, |
dyen | ac45ecf | 2015-11-06 17:40:56 | [diff] [blame] | 357 | const gpu::SyncToken& sync_token) { |
reveman | 22dd929 | 2014-10-13 20:52:05 | [diff] [blame] | 358 | // Note: Nothing to do here as ownership of shared memory backed |
| 359 | // GpuMemoryBuffers is passed with IPC. |
| 360 | } |
| 361 | |
[email protected] | 4734d0b | 2011-12-03 07:10:44 | [diff] [blame] | 362 | } // namespace content |