| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "ppapi/nacl_irt/plugin_startup.h" | 5 #include "ppapi/nacl_irt/plugin_startup.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/file_descriptor_posix.h" | 8 #include "base/file_descriptor_posix.h" |
| 9 #include "base/location.h" | 9 #include "base/location.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 #include "base/single_thread_task_runner.h" | 11 #include "base/single_thread_task_runner.h" |
| 12 #include "base/synchronization/waitable_event.h" | 12 #include "base/synchronization/waitable_event.h" |
| 13 #include "base/threading/thread.h" | 13 #include "base/threading/thread.h" |
| 14 #include "ipc/ipc_channel_handle.h" | 14 #include "ipc/ipc_channel_handle.h" |
| 15 #include "mojo/edk/embedder/embedder.h" | 15 #include "mojo/edk/embedder/embedder.h" |
| 16 #include "ppapi/nacl_irt/manifest_service.h" | 16 #include "ppapi/nacl_irt/manifest_service.h" |
| 17 #include "ppapi/shared_impl/ppb_audio_shared.h" | 17 #include "ppapi/shared_impl/ppb_audio_shared.h" |
| 18 | 18 |
| 19 namespace ppapi { | 19 namespace ppapi { |
| 20 namespace { | 20 namespace { |
| 21 | 21 |
| 22 int g_nacl_browser_ipc_fd = -1; | 22 IPC::ChannelHandle* g_nacl_browser_ipc_handle = nullptr; |
| 23 int g_nacl_renderer_ipc_fd = -1; | 23 IPC::ChannelHandle* g_nacl_renderer_ipc_handle = nullptr; |
| 24 int g_manifest_service_fd = -1; | 24 IPC::ChannelHandle* g_manifest_service_handle = nullptr; |
| 25 | 25 |
| 26 base::WaitableEvent* g_shutdown_event = NULL; | 26 base::WaitableEvent* g_shutdown_event = NULL; |
| 27 base::Thread* g_io_thread = NULL; | 27 base::Thread* g_io_thread = NULL; |
| 28 ManifestService* g_manifest_service = NULL; | 28 ManifestService* g_manifest_service = NULL; |
| 29 | 29 |
| 30 bool IsValidChannelHandle(IPC::ChannelHandle* handle) { |
| 31 // ChannelMojo not yet supported. |
| 32 return handle && handle->socket.fd != -1 && !handle->mojo_handle.is_valid(); |
| 33 } |
| 34 |
| 30 // Creates the manifest service on IO thread so that its Listener's thread and | 35 // Creates the manifest service on IO thread so that its Listener's thread and |
| 31 // IO thread are shared. Upon completion of the manifest service creation, | 36 // IO thread are shared. Upon completion of the manifest service creation, |
| 32 // event is signaled. | 37 // event is signaled. |
| 33 void StartUpManifestServiceOnIOThread(base::WaitableEvent* event) { | 38 void StartUpManifestServiceOnIOThread(base::WaitableEvent* event) { |
| 34 // The start up must be called only once. | 39 // The start up must be called only once. |
| 35 DCHECK(!g_manifest_service); | 40 DCHECK(!g_manifest_service); |
| 36 // manifest_service_fd must be set. | 41 // manifest_service_handle must be set. |
| 37 DCHECK_NE(g_manifest_service_fd, -1); | 42 DCHECK(IsValidChannelHandle(g_manifest_service_handle)); |
| 38 // IOThread and shutdown event must be initialized in advance. | 43 // IOThread and shutdown event must be initialized in advance. |
| 39 DCHECK(g_io_thread); | 44 DCHECK(g_io_thread); |
| 40 DCHECK(g_shutdown_event); | 45 DCHECK(g_shutdown_event); |
| 41 | 46 |
| 42 g_manifest_service = new ManifestService( | 47 g_manifest_service = new ManifestService( |
| 43 IPC::ChannelHandle("NaCl IPC", | 48 *g_manifest_service_handle, g_io_thread->task_runner(), |
| 44 base::FileDescriptor(g_manifest_service_fd, false)), | 49 g_shutdown_event); |
| 45 g_io_thread->task_runner(), g_shutdown_event); | |
| 46 event->Signal(); | 50 event->Signal(); |
| 47 } | 51 } |
| 48 | 52 |
| 49 } // namespace | 53 } // namespace |
| 50 | 54 |
| 51 void SetIPCFileDescriptors( | 55 void SetIPCChannelHandles( |
| 52 int browser_ipc_fd, int renderer_ipc_fd, int manifest_service_fd) { | 56 IPC::ChannelHandle browser_ipc_handle, |
| 57 IPC::ChannelHandle renderer_ipc_handle, |
| 58 IPC::ChannelHandle manifest_service_handle) { |
| 53 // The initialization must be only once. | 59 // The initialization must be only once. |
| 54 DCHECK_EQ(g_nacl_browser_ipc_fd, -1); | 60 DCHECK(!g_nacl_browser_ipc_handle); |
| 55 DCHECK_EQ(g_nacl_renderer_ipc_fd, -1); | 61 DCHECK(!g_nacl_renderer_ipc_handle); |
| 56 DCHECK_EQ(g_manifest_service_fd, -1); | 62 DCHECK(!g_nacl_renderer_ipc_handle); |
| 57 g_nacl_browser_ipc_fd = browser_ipc_fd; | 63 g_nacl_browser_ipc_handle = new IPC::ChannelHandle(browser_ipc_handle); |
| 58 g_nacl_renderer_ipc_fd = renderer_ipc_fd; | 64 g_nacl_renderer_ipc_handle = new IPC::ChannelHandle(renderer_ipc_handle); |
| 59 g_manifest_service_fd = manifest_service_fd; | 65 g_manifest_service_handle = new IPC::ChannelHandle(manifest_service_handle); |
| 60 } | 66 } |
| 61 | 67 |
| 62 void StartUpPlugin() { | 68 void StartUpPlugin() { |
| 63 // The start up must be called only once. | 69 // The start up must be called only once. |
| 64 DCHECK(!g_shutdown_event); | 70 DCHECK(!g_shutdown_event); |
| 65 DCHECK(!g_io_thread); | 71 DCHECK(!g_io_thread); |
| 66 | 72 |
| 67 // The Mojo EDK must be initialized before using IPC. | 73 // The Mojo EDK must be initialized before using IPC. |
| 68 mojo::edk::Init(); | 74 mojo::edk::Init(); |
| 69 | 75 |
| 70 g_shutdown_event = | 76 g_shutdown_event = |
| 71 new base::WaitableEvent(base::WaitableEvent::ResetPolicy::MANUAL, | 77 new base::WaitableEvent(base::WaitableEvent::ResetPolicy::MANUAL, |
| 72 base::WaitableEvent::InitialState::NOT_SIGNALED); | 78 base::WaitableEvent::InitialState::NOT_SIGNALED); |
| 73 g_io_thread = new base::Thread("Chrome_NaClIOThread"); | 79 g_io_thread = new base::Thread("Chrome_NaClIOThread"); |
| 74 g_io_thread->StartWithOptions( | 80 g_io_thread->StartWithOptions( |
| 75 base::Thread::Options(base::MessageLoop::TYPE_IO, 0)); | 81 base::Thread::Options(base::MessageLoop::TYPE_IO, 0)); |
| 76 | 82 |
| 77 if (g_manifest_service_fd != -1) { | 83 if (IsValidChannelHandle(g_manifest_service_handle)) { |
| 78 // Manifest service must be created on IOThread so that the main message | 84 // Manifest service must be created on IOThread so that the main message |
| 79 // handling will be done on the thread, which has a message loop | 85 // handling will be done on the thread, which has a message loop |
| 80 // even before irt_ppapi_start invocation. | 86 // even before irt_ppapi_start invocation. |
| 81 // TODO(hidehiko,dmichael): This works, but is probably not well designed | 87 // TODO(hidehiko,dmichael): This works, but is probably not well designed |
| 82 // usage. Once a better approach is made, replace this by that way. | 88 // usage. Once a better approach is made, replace this by that way. |
| 83 // (crbug.com/364241). | 89 // (crbug.com/364241). |
| 84 base::WaitableEvent event(base::WaitableEvent::ResetPolicy::MANUAL, | 90 base::WaitableEvent event(base::WaitableEvent::ResetPolicy::MANUAL, |
| 85 base::WaitableEvent::InitialState::NOT_SIGNALED); | 91 base::WaitableEvent::InitialState::NOT_SIGNALED); |
| 86 g_io_thread->task_runner()->PostTask( | 92 g_io_thread->task_runner()->PostTask( |
| 87 FROM_HERE, base::Bind(StartUpManifestServiceOnIOThread, &event)); | 93 FROM_HERE, base::Bind(StartUpManifestServiceOnIOThread, &event)); |
| 88 event.Wait(); | 94 event.Wait(); |
| 89 } | 95 } |
| 90 | 96 |
| 91 PPB_Audio_Shared::SetNaClMode(); | 97 PPB_Audio_Shared::SetNaClMode(); |
| 92 } | 98 } |
| 93 | 99 |
| 94 int GetBrowserIPCFileDescriptor() { | 100 IPC::ChannelHandle GetBrowserIPCChannelHandle() { |
| 95 // The descriptor must be initialized in advance. | 101 // The handle must be initialized in advance. |
| 96 DCHECK_NE(g_nacl_browser_ipc_fd, -1); | 102 DCHECK(IsValidChannelHandle(g_nacl_browser_ipc_handle)); |
| 97 return g_nacl_browser_ipc_fd; | 103 return *g_nacl_browser_ipc_handle; |
| 98 } | 104 } |
| 99 | 105 |
| 100 int GetRendererIPCFileDescriptor() { | 106 IPC::ChannelHandle GetRendererIPCChannelHandle() { |
| 101 // The descriptor must be initialized in advance. | 107 // The handle must be initialized in advance. |
| 102 DCHECK_NE(g_nacl_renderer_ipc_fd, -1); | 108 DCHECK(IsValidChannelHandle(g_nacl_renderer_ipc_handle)); |
| 103 return g_nacl_renderer_ipc_fd; | 109 return *g_nacl_renderer_ipc_handle; |
| 104 } | 110 } |
| 105 | 111 |
| 106 base::WaitableEvent* GetShutdownEvent() { | 112 base::WaitableEvent* GetShutdownEvent() { |
| 107 // The shutdown event must be initialized in advance. | 113 // The shutdown event must be initialized in advance. |
| 108 DCHECK(g_shutdown_event); | 114 DCHECK(g_shutdown_event); |
| 109 return g_shutdown_event; | 115 return g_shutdown_event; |
| 110 } | 116 } |
| 111 | 117 |
| 112 base::Thread* GetIOThread() { | 118 base::Thread* GetIOThread() { |
| 113 // The IOThread must be initialized in advance. | 119 // The IOThread must be initialized in advance. |
| 114 DCHECK(g_io_thread); | 120 DCHECK(g_io_thread); |
| 115 return g_io_thread; | 121 return g_io_thread; |
| 116 } | 122 } |
| 117 | 123 |
| 118 ManifestService* GetManifestService() { | 124 ManifestService* GetManifestService() { |
| 119 return g_manifest_service; | 125 return g_manifest_service; |
| 120 } | 126 } |
| 121 | 127 |
| 122 } // namespace ppapi | 128 } // namespace ppapi |
| OLD | NEW |