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" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/file_descriptor_posix.h" |
5 #include "base/logging.h" | 9 #include "base/logging.h" |
6 #include "base/synchronization/waitable_event.h" | 10 #include "base/synchronization/waitable_event.h" |
7 #include "base/threading/thread.h" | 11 #include "base/threading/thread.h" |
8 #include "ppapi/nacl_irt/plugin_startup.h" | 12 #include "ipc/ipc_channel_handle.h" |
| 13 #include "ppapi/nacl_irt/manifest_service.h" |
9 | 14 |
10 namespace ppapi { | 15 namespace ppapi { |
11 namespace { | 16 namespace { |
12 | 17 |
13 int g_nacl_browser_ipc_fd = -1; | 18 int g_nacl_browser_ipc_fd = -1; |
14 int g_nacl_renderer_ipc_fd = -1; | 19 int g_nacl_renderer_ipc_fd = -1; |
| 20 int g_manifest_service_fd = -1; |
15 | 21 |
16 base::WaitableEvent* g_shutdown_event = NULL; | 22 base::WaitableEvent* g_shutdown_event = NULL; |
17 base::Thread* g_io_thread = NULL; | 23 base::Thread* g_io_thread = NULL; |
| 24 ManifestService* g_manifest_service = NULL; |
| 25 |
| 26 // Creates the manifest service on IO thread so that its Listener's thread and |
| 27 // IO thread are shared. Upon completion of the manifest service creation, |
| 28 // event is signaled. |
| 29 void StartUpManifestServiceOnIOThread(base::WaitableEvent* event) { |
| 30 // The start up must be called only once. |
| 31 DCHECK(!g_manifest_service); |
| 32 // manifest_service_fd must be set. |
| 33 DCHECK_NE(g_manifest_service_fd, -1); |
| 34 // IOThread and shutdown event must be initialized in advance. |
| 35 DCHECK(g_io_thread); |
| 36 DCHECK(g_shutdown_event); |
| 37 |
| 38 g_manifest_service = new ManifestService( |
| 39 IPC::ChannelHandle( |
| 40 "NaCl IPC", base::FileDescriptor(g_manifest_service_fd, false)), |
| 41 g_io_thread->message_loop_proxy(), |
| 42 g_shutdown_event); |
| 43 event->Signal(); |
| 44 } |
18 | 45 |
19 } // namespace | 46 } // namespace |
20 | 47 |
21 void SetIPCFileDescriptors(int browser_ipc_fd, int renderer_ipc_fd) { | 48 void SetIPCFileDescriptors( |
| 49 int browser_ipc_fd, int renderer_ipc_fd, int manifest_service_fd) { |
22 // The initialization must be only once. | 50 // The initialization must be only once. |
23 DCHECK_EQ(g_nacl_browser_ipc_fd, -1); | 51 DCHECK_EQ(g_nacl_browser_ipc_fd, -1); |
24 DCHECK_EQ(g_nacl_renderer_ipc_fd, -1); | 52 DCHECK_EQ(g_nacl_renderer_ipc_fd, -1); |
| 53 DCHECK_EQ(g_manifest_service_fd, -1); |
25 g_nacl_browser_ipc_fd = browser_ipc_fd; | 54 g_nacl_browser_ipc_fd = browser_ipc_fd; |
26 g_nacl_renderer_ipc_fd = renderer_ipc_fd; | 55 g_nacl_renderer_ipc_fd = renderer_ipc_fd; |
| 56 g_manifest_service_fd = manifest_service_fd; |
27 } | 57 } |
28 | 58 |
29 void StartUpPlugin() { | 59 void StartUpPlugin() { |
30 // The start up must be called only once. | 60 // The start up must be called only once. |
31 DCHECK(!g_shutdown_event); | 61 DCHECK(!g_shutdown_event); |
32 DCHECK(!g_io_thread); | 62 DCHECK(!g_io_thread); |
33 | 63 |
34 g_shutdown_event = new base::WaitableEvent(true, false); | 64 g_shutdown_event = new base::WaitableEvent(true, false); |
35 g_io_thread = new base::Thread("Chrome_NaClIOThread"); | 65 g_io_thread = new base::Thread("Chrome_NaClIOThread"); |
36 g_io_thread->StartWithOptions( | 66 g_io_thread->StartWithOptions( |
37 base::Thread::Options(base::MessageLoop::TYPE_IO, 0)); | 67 base::Thread::Options(base::MessageLoop::TYPE_IO, 0)); |
| 68 |
| 69 if (g_manifest_service_fd != -1) { |
| 70 // Manifest service must be created on IOThread so that the main message |
| 71 // handling will be done on the thread, which has a message loop |
| 72 // even before irt_ppapi_start invocation. |
| 73 // TODO(hidehiko,dmichael): This works, but is probably not well designed |
| 74 // usage. Once a better approach is made, replace this by that way. |
| 75 // (crbug.com/364241). |
| 76 base::WaitableEvent event(true, false); |
| 77 g_io_thread->message_loop_proxy()->PostTask( |
| 78 FROM_HERE, |
| 79 base::Bind(StartUpManifestServiceOnIOThread, &event)); |
| 80 event.Wait(); |
| 81 } |
38 } | 82 } |
39 | 83 |
40 int GetBrowserIPCFileDescriptor() { | 84 int GetBrowserIPCFileDescriptor() { |
41 // The descriptor must be initialized in advance. | 85 // The descriptor must be initialized in advance. |
42 DCHECK_NE(g_nacl_browser_ipc_fd, -1); | 86 DCHECK_NE(g_nacl_browser_ipc_fd, -1); |
43 return g_nacl_browser_ipc_fd; | 87 return g_nacl_browser_ipc_fd; |
44 } | 88 } |
45 | 89 |
46 int GetRendererIPCFileDescriptor() { | 90 int GetRendererIPCFileDescriptor() { |
47 // The descriptor must be initialized in advance. | 91 // The descriptor must be initialized in advance. |
48 DCHECK_NE(g_nacl_renderer_ipc_fd, -1); | 92 DCHECK_NE(g_nacl_renderer_ipc_fd, -1); |
49 return g_nacl_renderer_ipc_fd; | 93 return g_nacl_renderer_ipc_fd; |
50 } | 94 } |
51 | 95 |
52 base::WaitableEvent* GetShutdownEvent() { | 96 base::WaitableEvent* GetShutdownEvent() { |
53 // The shutdown event must be initialized in advance. | 97 // The shutdown event must be initialized in advance. |
54 DCHECK(g_shutdown_event); | 98 DCHECK(g_shutdown_event); |
55 return g_shutdown_event; | 99 return g_shutdown_event; |
56 } | 100 } |
57 | 101 |
58 base::Thread* GetIOThread() { | 102 base::Thread* GetIOThread() { |
59 // The IOThread must be initialized in advance. | 103 // The IOThread must be initialized in advance. |
60 DCHECK(g_io_thread); | 104 DCHECK(g_io_thread); |
61 return g_io_thread; | 105 return g_io_thread; |
62 } | 106 } |
63 | 107 |
64 } // namespace ppapi | 108 } // namespace ppapi |
OLD | NEW |