| 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> |
| (...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 124 return false; | 124 return false; |
| 125 return result; | 125 return result; |
| 126 } | 126 } |
| 127 | 127 |
| 128 void DebugStubPortSelectedHandler(uint16_t port) { | 128 void DebugStubPortSelectedHandler(uint16_t port) { |
| 129 g_listener->Send(new NaClProcessHostMsg_DebugStubPortSelected(port)); | 129 g_listener->Send(new NaClProcessHostMsg_DebugStubPortSelected(port)); |
| 130 } | 130 } |
| 131 | 131 |
| 132 #endif | 132 #endif |
| 133 | 133 |
| 134 // This function should be used be provided to the ManifestChannel |
| 135 // NaClIPCAdapter instead of the one inside NaClListener; this one will always |
| 136 // run the callback, even if the NaClListener has been destroyed. |
| 137 void ResolveFileToken( |
| 138 uint64_t token_lo, |
| 139 uint64_t token_hi, |
| 140 base::Callback<void(IPC::PlatformFileForTransit, |
| 141 base::FilePath)> resolved_cb) { |
| 142 if (!g_listener) |
| 143 resolved_cb.Run(IPC::PlatformFileForTransit(), base::FilePath()); |
| 144 g_listener->ResolveFileToken(token_lo, token_hi, resolved_cb); |
| 145 } |
| 146 |
| 134 // Creates the PPAPI IPC channel between the NaCl IRT and the host | 147 // Creates the PPAPI IPC channel between the NaCl IRT and the host |
| 135 // (browser/renderer) process, and starts to listen it on the thread where | 148 // (browser/renderer) process, and starts to listen it on the thread where |
| 136 // the given message_loop_proxy runs. | 149 // the given message_loop_proxy runs. |
| 137 // Also, creates and sets the corresponding NaClDesc to the given nap with | 150 // Also, creates and sets the corresponding NaClDesc to the given nap with |
| 138 // the FD #. | 151 // the FD #. |
| 139 void SetUpIPCAdapter(IPC::ChannelHandle* handle, | 152 scoped_refptr<NaClIPCAdapter> SetUpIPCAdapter( |
| 140 scoped_refptr<base::MessageLoopProxy> message_loop_proxy, | 153 IPC::ChannelHandle* handle, |
| 141 struct NaClApp* nap, | 154 scoped_refptr<base::MessageLoopProxy> message_loop_proxy, |
| 142 int nacl_fd) { | 155 struct NaClApp* nap, |
| 156 int nacl_fd) { |
| 143 scoped_refptr<NaClIPCAdapter> ipc_adapter( | 157 scoped_refptr<NaClIPCAdapter> ipc_adapter( |
| 144 new NaClIPCAdapter(*handle, message_loop_proxy.get())); | 158 new NaClIPCAdapter(*handle, message_loop_proxy.get())); |
| 145 ipc_adapter->ConnectChannel(); | 159 ipc_adapter->ConnectChannel(); |
| 146 #if defined(OS_POSIX) | 160 #if defined(OS_POSIX) |
| 147 handle->socket = | 161 handle->socket = |
| 148 base::FileDescriptor(ipc_adapter->TakeClientFileDescriptor(), true); | 162 base::FileDescriptor(ipc_adapter->TakeClientFileDescriptor(), true); |
| 149 #endif | 163 #endif |
| 150 | 164 |
| 151 // Pass a NaClDesc to the untrusted side. This will hold a ref to the | 165 // Pass a NaClDesc to the untrusted side. This will hold a ref to the |
| 152 // NaClIPCAdapter. | 166 // NaClIPCAdapter. |
| 153 NaClAppSetDesc(nap, nacl_fd, ipc_adapter->MakeNaClDesc()); | 167 NaClAppSetDesc(nap, nacl_fd, ipc_adapter->MakeNaClDesc()); |
| 168 return ipc_adapter; |
| 154 } | 169 } |
| 155 | 170 |
| 156 } // namespace | 171 } // namespace |
| 157 | 172 |
| 158 class BrowserValidationDBProxy : public NaClValidationDB { | 173 class BrowserValidationDBProxy : public NaClValidationDB { |
| 159 public: | 174 public: |
| 160 explicit BrowserValidationDBProxy(NaClListener* listener) | 175 explicit BrowserValidationDBProxy(NaClListener* listener) |
| 161 : listener_(listener) { | 176 : listener_(listener) { |
| 162 } | 177 } |
| 163 | 178 |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 244 DCHECK(main_loop_ != NULL); | 259 DCHECK(main_loop_ != NULL); |
| 245 if (base::MessageLoop::current() == main_loop_) { | 260 if (base::MessageLoop::current() == main_loop_) { |
| 246 // This thread owns the channel. | 261 // This thread owns the channel. |
| 247 return channel_->Send(msg); | 262 return channel_->Send(msg); |
| 248 } else { | 263 } else { |
| 249 // This thread does not own the channel. | 264 // This thread does not own the channel. |
| 250 return filter_->Send(msg); | 265 return filter_->Send(msg); |
| 251 } | 266 } |
| 252 } | 267 } |
| 253 | 268 |
| 269 class FileTokenMessageFilter : public IPC::MessageFilter { |
| 270 public: |
| 271 virtual bool OnMessageReceived(const IPC::Message& msg) OVERRIDE { |
| 272 bool handled = true; |
| 273 IPC_BEGIN_MESSAGE_MAP(FileTokenMessageFilter, msg) |
| 274 IPC_MESSAGE_HANDLER(NaClProcessMsg_ResolveFileTokenAsyncReply, |
| 275 OnResolveFileTokenAsyncReply) |
| 276 IPC_MESSAGE_UNHANDLED(handled = false) |
| 277 IPC_END_MESSAGE_MAP() |
| 278 return handled; |
| 279 } |
| 280 |
| 281 void OnResolveFileTokenAsyncReply( |
| 282 uint64_t token_lo, |
| 283 uint64_t token_hi, |
| 284 IPC::PlatformFileForTransit ipc_fd, |
| 285 base::FilePath file_path) { |
| 286 if (g_listener) |
| 287 g_listener->OnFileTokenResolved(token_lo, token_hi, ipc_fd, file_path); |
| 288 } |
| 289 private: |
| 290 virtual ~FileTokenMessageFilter() { } |
| 291 }; |
| 292 |
| 254 void NaClListener::Listen() { | 293 void NaClListener::Listen() { |
| 255 std::string channel_name = | 294 std::string channel_name = |
| 256 CommandLine::ForCurrentProcess()->GetSwitchValueASCII( | 295 CommandLine::ForCurrentProcess()->GetSwitchValueASCII( |
| 257 switches::kProcessChannelID); | 296 switches::kProcessChannelID); |
| 258 channel_ = IPC::SyncChannel::Create( | 297 channel_ = IPC::SyncChannel::Create( |
| 259 this, io_thread_.message_loop_proxy().get(), &shutdown_event_); | 298 this, io_thread_.message_loop_proxy().get(), &shutdown_event_); |
| 260 filter_ = new IPC::SyncMessageFilter(&shutdown_event_); | 299 filter_ = new IPC::SyncMessageFilter(&shutdown_event_); |
| 261 channel_->AddFilter(filter_.get()); | 300 channel_->AddFilter(filter_.get()); |
| 301 channel_->AddFilter(new FileTokenMessageFilter()); |
| 262 channel_->Init(channel_name, IPC::Channel::MODE_CLIENT, true); | 302 channel_->Init(channel_name, IPC::Channel::MODE_CLIENT, true); |
| 263 main_loop_ = base::MessageLoop::current(); | 303 main_loop_ = base::MessageLoop::current(); |
| 264 main_loop_->Run(); | 304 main_loop_->Run(); |
| 265 } | 305 } |
| 266 | 306 |
| 267 bool NaClListener::OnMessageReceived(const IPC::Message& msg) { | 307 bool NaClListener::OnMessageReceived(const IPC::Message& msg) { |
| 268 bool handled = true; | 308 bool handled = true; |
| 269 IPC_BEGIN_MESSAGE_MAP(NaClListener, msg) | 309 IPC_BEGIN_MESSAGE_MAP(NaClListener, msg) |
| 270 IPC_MESSAGE_HANDLER(NaClProcessMsg_Start, OnStart) | 310 IPC_MESSAGE_HANDLER(NaClProcessMsg_Start, OnStart) |
| 271 IPC_MESSAGE_UNHANDLED(handled = false) | 311 IPC_MESSAGE_UNHANDLED(handled = false) |
| (...skipping 19 matching lines...) Expand all Loading... |
| 291 NaClSetFatalErrorCallback(&FatalLogHandler); | 331 NaClSetFatalErrorCallback(&FatalLogHandler); |
| 292 | 332 |
| 293 nap = NaClAppCreate(); | 333 nap = NaClAppCreate(); |
| 294 if (nap == NULL) { | 334 if (nap == NULL) { |
| 295 LOG(ERROR) << "NaClAppCreate() failed"; | 335 LOG(ERROR) << "NaClAppCreate() failed"; |
| 296 return; | 336 return; |
| 297 } | 337 } |
| 298 | 338 |
| 299 IPC::ChannelHandle browser_handle; | 339 IPC::ChannelHandle browser_handle; |
| 300 IPC::ChannelHandle ppapi_renderer_handle; | 340 IPC::ChannelHandle ppapi_renderer_handle; |
| 341 IPC::ChannelHandle manifest_service_handle; |
| 301 | 342 |
| 302 if (params.enable_ipc_proxy) { | 343 if (params.enable_ipc_proxy) { |
| 303 browser_handle = IPC::Channel::GenerateVerifiedChannelID("nacl"); | 344 browser_handle = IPC::Channel::GenerateVerifiedChannelID("nacl"); |
| 304 ppapi_renderer_handle = IPC::Channel::GenerateVerifiedChannelID("nacl"); | 345 ppapi_renderer_handle = IPC::Channel::GenerateVerifiedChannelID("nacl"); |
| 346 manifest_service_handle = IPC::Channel::GenerateVerifiedChannelID("nacl"); |
| 305 | 347 |
| 306 // Create the PPAPI IPC channels between the NaCl IRT and the host | 348 // Create the PPAPI IPC channels between the NaCl IRT and the host |
| 307 // (browser/renderer) processes. The IRT uses these channels to | 349 // (browser/renderer) processes. The IRT uses these channels to |
| 308 // communicate with the host and to initialize the IPC dispatchers. | 350 // communicate with the host and to initialize the IPC dispatchers. |
| 309 SetUpIPCAdapter(&browser_handle, io_thread_.message_loop_proxy(), | 351 SetUpIPCAdapter(&browser_handle, io_thread_.message_loop_proxy(), |
| 310 nap, NACL_CHROME_DESC_BASE); | 352 nap, NACL_CHROME_DESC_BASE); |
| 311 SetUpIPCAdapter(&ppapi_renderer_handle, io_thread_.message_loop_proxy(), | 353 SetUpIPCAdapter(&ppapi_renderer_handle, io_thread_.message_loop_proxy(), |
| 312 nap, NACL_CHROME_DESC_BASE + 1); | 354 nap, NACL_CHROME_DESC_BASE + 1); |
| 355 |
| 356 scoped_refptr<NaClIPCAdapter> manifest_ipc_adapter = |
| 357 SetUpIPCAdapter(&manifest_service_handle, |
| 358 io_thread_.message_loop_proxy(), |
| 359 nap, |
| 360 NACL_CHROME_DESC_BASE + 2); |
| 361 manifest_ipc_adapter->set_resolve_file_token_callback( |
| 362 base::Bind(&::ResolveFileToken)); |
| 313 } | 363 } |
| 314 | 364 |
| 315 trusted_listener_ = new NaClTrustedListener( | 365 trusted_listener_ = new NaClTrustedListener( |
| 316 IPC::Channel::GenerateVerifiedChannelID("nacl"), | 366 IPC::Channel::GenerateVerifiedChannelID("nacl"), |
| 317 io_thread_.message_loop_proxy().get(), | 367 io_thread_.message_loop_proxy().get(), |
| 318 &shutdown_event_); | 368 &shutdown_event_); |
| 319 if (!Send(new NaClProcessHostMsg_PpapiChannelsCreated( | 369 if (!Send(new NaClProcessHostMsg_PpapiChannelsCreated( |
| 320 browser_handle, | 370 browser_handle, |
| 321 ppapi_renderer_handle, | 371 ppapi_renderer_handle, |
| 322 trusted_listener_->TakeClientChannelHandle(), | 372 trusted_listener_->TakeClientChannelHandle(), |
| 323 IPC::ChannelHandle()))) | 373 manifest_service_handle))) |
| 324 LOG(ERROR) << "Failed to send IPC channel handle to NaClProcessHost."; | 374 LOG(ERROR) << "Failed to send IPC channel handle to NaClProcessHost."; |
| 325 | 375 |
| 326 std::vector<nacl::FileDescriptor> handles = params.handles; | 376 std::vector<nacl::FileDescriptor> handles = params.handles; |
| 327 struct NaClChromeMainArgs* args = NaClChromeMainArgsCreate(); | 377 struct NaClChromeMainArgs* args = NaClChromeMainArgsCreate(); |
| 328 if (args == NULL) { | 378 if (args == NULL) { |
| 329 LOG(ERROR) << "NaClChromeMainArgsCreate() failed"; | 379 LOG(ERROR) << "NaClChromeMainArgsCreate() failed"; |
| 330 return; | 380 return; |
| 331 } | 381 } |
| 332 | 382 |
| 333 #if defined(OS_LINUX) || defined(OS_MACOSX) | 383 #if defined(OS_LINUX) || defined(OS_MACOSX) |
| (...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 420 args->nexe_desc = NaClDescIoFromFileInfo(nexe_file_info, NACL_ABI_O_RDONLY); | 470 args->nexe_desc = NaClDescIoFromFileInfo(nexe_file_info, NACL_ABI_O_RDONLY); |
| 421 | 471 |
| 422 int exit_status; | 472 int exit_status; |
| 423 if (!NaClChromeMainStart(nap, args, &exit_status)) | 473 if (!NaClChromeMainStart(nap, args, &exit_status)) |
| 424 NaClExit(1); | 474 NaClExit(1); |
| 425 | 475 |
| 426 // Report the plugin's exit status if the application started successfully. | 476 // Report the plugin's exit status if the application started successfully. |
| 427 trusted_listener_->Send(new NaClRendererMsg_ReportExitStatus(exit_status)); | 477 trusted_listener_->Send(new NaClRendererMsg_ReportExitStatus(exit_status)); |
| 428 NaClExit(exit_status); | 478 NaClExit(exit_status); |
| 429 } | 479 } |
| 480 |
| 481 void NaClListener::ResolveFileToken( |
| 482 uint64_t token_lo, |
| 483 uint64_t token_hi, |
| 484 base::Callback<void(IPC::PlatformFileForTransit, base::FilePath)> cb) { |
| 485 if (!Send(new NaClProcessMsg_ResolveFileTokenAsync(token_lo, token_hi))) { |
| 486 cb.Run(IPC::PlatformFileForTransit(), base::FilePath()); |
| 487 return; |
| 488 } |
| 489 resolved_cb_ = cb; |
| 490 } |
| 491 |
| 492 void NaClListener::OnFileTokenResolved( |
| 493 uint64_t token_lo, |
| 494 uint64_t token_hi, |
| 495 IPC::PlatformFileForTransit ipc_fd, |
| 496 base::FilePath file_path) { |
| 497 resolved_cb_.Run(ipc_fd, file_path); |
| 498 resolved_cb_.Reset(); |
| 499 } |
| OLD | NEW |