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