| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 #include "components/nacl/loader/nacl_listener.h" | 5 #include "components/nacl/loader/nacl_listener.h" |
| 6 | 6 |
| 7 #include <errno.h> | 7 #include <errno.h> |
| 8 #include <fcntl.h> | 8 #include <fcntl.h> |
| 9 #include <stdlib.h> | 9 #include <stdlib.h> |
| 10 #include <string.h> | 10 #include <string.h> |
| 11 | 11 |
| 12 #include <memory> | 12 #include <memory> |
| 13 #include <utility> | 13 #include <utility> |
| 14 | 14 |
| 15 #if defined(OS_POSIX) | 15 #if defined(OS_POSIX) |
| 16 #include <unistd.h> | 16 #include <unistd.h> |
| 17 #endif | 17 #endif |
| 18 | 18 |
| 19 #include "base/command_line.h" | 19 #include "base/command_line.h" |
| 20 #include "base/logging.h" | 20 #include "base/logging.h" |
| 21 #include "base/memory/ptr_util.h" | 21 #include "base/memory/ptr_util.h" |
| 22 #include "base/rand_util.h" | 22 #include "base/rand_util.h" |
| 23 #include "base/run_loop.h" | 23 #include "base/run_loop.h" |
| 24 #include "base/single_thread_task_runner.h" | 24 #include "base/single_thread_task_runner.h" |
| 25 #include "base/threading/thread_task_runner_handle.h" | 25 #include "base/threading/thread_task_runner_handle.h" |
| 26 #include "build/build_config.h" | 26 #include "build/build_config.h" |
| 27 #include "components/nacl/common/nacl_messages.h" | 27 #include "components/nacl/common/nacl_messages.h" |
| 28 #include "components/nacl/common/nacl_renderer_messages.h" | 28 #include "components/nacl/common/nacl_renderer_messages.h" |
| 29 #include "components/nacl/common/nacl_service.h" |
| 29 #include "components/nacl/common/nacl_switches.h" | 30 #include "components/nacl/common/nacl_switches.h" |
| 30 #include "components/nacl/loader/nacl_ipc_adapter.h" | 31 #include "components/nacl/loader/nacl_ipc_adapter.h" |
| 31 #include "components/nacl/loader/nacl_validation_db.h" | 32 #include "components/nacl/loader/nacl_validation_db.h" |
| 32 #include "components/nacl/loader/nacl_validation_query.h" | 33 #include "components/nacl/loader/nacl_validation_query.h" |
| 33 #include "content/public/common/mojo_channel_switches.h" | 34 #include "content/public/common/mojo_channel_switches.h" |
| 34 #include "ipc/ipc_channel_handle.h" | 35 #include "ipc/ipc_channel_handle.h" |
| 35 #include "ipc/ipc_sync_channel.h" | 36 #include "ipc/ipc_sync_channel.h" |
| 36 #include "ipc/ipc_sync_message_filter.h" | 37 #include "ipc/ipc_sync_message_filter.h" |
| 37 #include "mojo/edk/embedder/embedder.h" | |
| 38 #include "mojo/edk/embedder/scoped_ipc_support.h" | |
| 39 #include "native_client/src/public/chrome_main.h" | 38 #include "native_client/src/public/chrome_main.h" |
| 40 #include "native_client/src/public/nacl_app.h" | 39 #include "native_client/src/public/nacl_app.h" |
| 41 #include "native_client/src/public/nacl_desc.h" | 40 #include "native_client/src/public/nacl_desc.h" |
| 42 | 41 #include "services/service_manager/public/cpp/service_context.h" |
| 43 #if defined(OS_POSIX) | |
| 44 #include "base/posix/global_descriptors.h" | |
| 45 #include "content/public/common/content_descriptors.h" | |
| 46 #endif | |
| 47 | 42 |
| 48 #if defined(OS_LINUX) | 43 #if defined(OS_LINUX) |
| 49 #include "content/public/common/child_process_sandbox_support_linux.h" | 44 #include "content/public/common/child_process_sandbox_support_linux.h" |
| 50 #endif | 45 #endif |
| 51 | 46 |
| 52 #if defined(OS_WIN) | 47 #if defined(OS_WIN) |
| 53 #include <io.h> | 48 #include <io.h> |
| 54 | 49 |
| 55 #include "content/public/common/sandbox_init.h" | 50 #include "content/public/common/sandbox_init.h" |
| 56 #include "mojo/edk/embedder/platform_channel_pair.h" | |
| 57 #endif | 51 #endif |
| 58 | 52 |
| 59 namespace { | 53 namespace { |
| 60 | 54 |
| 61 NaClListener* g_listener; | 55 NaClListener* g_listener; |
| 62 | 56 |
| 63 void FatalLogHandler(const char* data, size_t bytes) { | 57 void FatalLogHandler(const char* data, size_t bytes) { |
| 64 // We use uint32_t rather than size_t for the case when the browser and NaCl | 58 // We use uint32_t rather than size_t for the case when the browser and NaCl |
| 65 // processes are a mix of 32-bit and 64-bit processes. | 59 // processes are a mix of 32-bit and 64-bit processes. |
| 66 uint32_t copy_bytes = std::min<uint32_t>(static_cast<uint32_t>(bytes), | 60 uint32_t copy_bytes = std::min<uint32_t>(static_cast<uint32_t>(bytes), |
| (...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 168 prereserved_sandbox_size_(0), | 162 prereserved_sandbox_size_(0), |
| 169 #endif | 163 #endif |
| 170 #if defined(OS_POSIX) | 164 #if defined(OS_POSIX) |
| 171 number_of_cores_(-1), // unknown/error | 165 number_of_cores_(-1), // unknown/error |
| 172 #endif | 166 #endif |
| 173 is_started_(false) { | 167 is_started_(false) { |
| 174 io_thread_.StartWithOptions( | 168 io_thread_.StartWithOptions( |
| 175 base::Thread::Options(base::MessageLoop::TYPE_IO, 0)); | 169 base::Thread::Options(base::MessageLoop::TYPE_IO, 0)); |
| 176 DCHECK(g_listener == NULL); | 170 DCHECK(g_listener == NULL); |
| 177 g_listener = this; | 171 g_listener = this; |
| 178 | |
| 179 mojo_ipc_support_ = | |
| 180 base::MakeUnique<mojo::edk::ScopedIPCSupport>(io_thread_.task_runner()); | |
| 181 #if defined(OS_WIN) | |
| 182 mojo::edk::ScopedPlatformHandle platform_channel( | |
| 183 mojo::edk::PlatformChannelPair::PassClientHandleFromParentProcess( | |
| 184 *base::CommandLine::ForCurrentProcess())); | |
| 185 #else | |
| 186 mojo::edk::ScopedPlatformHandle platform_channel( | |
| 187 mojo::edk::PlatformHandle( | |
| 188 base::GlobalDescriptors::GetInstance()->Get(kMojoIPCChannel))); | |
| 189 #endif | |
| 190 DCHECK(platform_channel.is_valid()); | |
| 191 mojo::edk::SetParentPipeHandle(std::move(platform_channel)); | |
| 192 } | 172 } |
| 193 | 173 |
| 194 NaClListener::~NaClListener() { | 174 NaClListener::~NaClListener() { |
| 195 NOTREACHED(); | 175 NOTREACHED(); |
| 196 shutdown_event_.Signal(); | 176 shutdown_event_.Signal(); |
| 197 g_listener = NULL; | 177 g_listener = NULL; |
| 198 } | 178 } |
| 199 | 179 |
| 200 bool NaClListener::Send(IPC::Message* msg) { | 180 bool NaClListener::Send(IPC::Message* msg) { |
| 201 DCHECK(!!main_task_runner_); | 181 DCHECK(!!main_task_runner_); |
| (...skipping 28 matching lines...) Expand all Loading... |
| 230 IPC::PlatformFileForTransit ipc_fd, | 210 IPC::PlatformFileForTransit ipc_fd, |
| 231 base::FilePath file_path) { | 211 base::FilePath file_path) { |
| 232 CHECK(g_listener); | 212 CHECK(g_listener); |
| 233 g_listener->OnFileTokenResolved(token_lo, token_hi, ipc_fd, file_path); | 213 g_listener->OnFileTokenResolved(token_lo, token_hi, ipc_fd, file_path); |
| 234 } | 214 } |
| 235 private: | 215 private: |
| 236 ~FileTokenMessageFilter() override {} | 216 ~FileTokenMessageFilter() override {} |
| 237 }; | 217 }; |
| 238 | 218 |
| 239 void NaClListener::Listen() { | 219 void NaClListener::Listen() { |
| 240 mojo::ScopedMessagePipeHandle handle( | |
| 241 mojo::edk::CreateChildMessagePipe( | |
| 242 base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII( | |
| 243 switches::kMojoChannelToken))); | |
| 244 DCHECK(handle.is_valid()); | |
| 245 | |
| 246 channel_ = IPC::SyncChannel::Create(this, io_thread_.task_runner().get(), | 220 channel_ = IPC::SyncChannel::Create(this, io_thread_.task_runner().get(), |
| 247 &shutdown_event_); | 221 &shutdown_event_); |
| 248 filter_ = channel_->CreateSyncMessageFilter(); | 222 filter_ = channel_->CreateSyncMessageFilter(); |
| 249 channel_->AddFilter(new FileTokenMessageFilter()); | 223 channel_->AddFilter(new FileTokenMessageFilter()); |
| 250 channel_->Init(handle.release(), IPC::Channel::MODE_CLIENT, true); | 224 mojo::ScopedMessagePipeHandle channel_handle; |
| 225 std::unique_ptr<service_manager::ServiceContext> service_context = |
| 226 CreateNaClServiceContext(io_thread_.task_runner(), &channel_handle); |
| 227 channel_->Init(channel_handle.release(), IPC::Channel::MODE_CLIENT, true); |
| 251 main_task_runner_ = base::ThreadTaskRunnerHandle::Get(); | 228 main_task_runner_ = base::ThreadTaskRunnerHandle::Get(); |
| 252 base::RunLoop().Run(); | 229 base::RunLoop().Run(); |
| 253 } | 230 } |
| 254 | 231 |
| 255 bool NaClListener::OnMessageReceived(const IPC::Message& msg) { | 232 bool NaClListener::OnMessageReceived(const IPC::Message& msg) { |
| 256 bool handled = true; | 233 bool handled = true; |
| 257 IPC_BEGIN_MESSAGE_MAP(NaClListener, msg) | 234 IPC_BEGIN_MESSAGE_MAP(NaClListener, msg) |
| 258 IPC_MESSAGE_HANDLER(NaClProcessMsg_AddPrefetchedResource, | 235 IPC_MESSAGE_HANDLER(NaClProcessMsg_AddPrefetchedResource, |
| 259 OnAddPrefetchedResource) | 236 OnAddPrefetchedResource) |
| 260 IPC_MESSAGE_HANDLER(NaClProcessMsg_Start, OnStart) | 237 IPC_MESSAGE_HANDLER(NaClProcessMsg_Start, OnStart) |
| (...skipping 201 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 462 } | 439 } |
| 463 | 440 |
| 464 void NaClListener::OnFileTokenResolved( | 441 void NaClListener::OnFileTokenResolved( |
| 465 uint64_t token_lo, | 442 uint64_t token_lo, |
| 466 uint64_t token_hi, | 443 uint64_t token_hi, |
| 467 IPC::PlatformFileForTransit ipc_fd, | 444 IPC::PlatformFileForTransit ipc_fd, |
| 468 base::FilePath file_path) { | 445 base::FilePath file_path) { |
| 469 resolved_cb_.Run(ipc_fd, file_path); | 446 resolved_cb_.Run(ipc_fd, file_path); |
| 470 resolved_cb_.Reset(); | 447 resolved_cb_.Reset(); |
| 471 } | 448 } |
| OLD | NEW |