OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "chrome/renderer/pepper/ppb_nacl_private_impl.h" | 5 #include "chrome/renderer/pepper/ppb_nacl_private_impl.h" |
6 | 6 |
7 #ifndef DISABLE_NACL | 7 #ifndef DISABLE_NACL |
8 | 8 |
9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
10 #include "base/compiler_specific.h" | |
10 #include "base/lazy_instance.h" | 11 #include "base/lazy_instance.h" |
11 #include "base/logging.h" | 12 #include "base/logging.h" |
13 #include "base/message_loop.h" | |
14 #include "base/path_service.h" | |
12 #include "base/rand_util_c.h" | 15 #include "base/rand_util_c.h" |
16 #include "chrome/common/chrome_paths.h" | |
17 #include "chrome/common/chrome_switches.h" | |
13 #include "chrome/common/render_messages.h" | 18 #include "chrome/common/render_messages.h" |
19 #include "content/public/common/content_client.h" | |
14 #include "content/public/common/content_switches.h" | 20 #include "content/public/common/content_switches.h" |
21 #include "content/public/common/sandbox_init.h" | |
15 #include "content/public/renderer/render_thread.h" | 22 #include "content/public/renderer/render_thread.h" |
23 #include "content/public/renderer/render_view.h" | |
16 #include "ipc/ipc_sync_message_filter.h" | 24 #include "ipc/ipc_sync_message_filter.h" |
17 #include "ppapi/c/private/ppb_nacl_private.h" | 25 #include "ppapi/c/private/ppb_nacl_private.h" |
18 #include "ppapi/native_client/src/trusted/plugin/nacl_entry_points.h" | 26 #include "ppapi/native_client/src/trusted/plugin/nacl_entry_points.h" |
27 #include "ppapi/proxy/host_dispatcher.h" | |
28 #include "ppapi/proxy/proxy_channel.h" | |
29 #include "ppapi/shared_impl/ppapi_preferences.h" | |
30 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h" | |
31 #include "third_party/WebKit/Source/WebKit/chromium/public/WebElement.h" | |
32 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" | |
33 #include "third_party/WebKit/Source/WebKit/chromium/public/WebPluginContainer.h" | |
34 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" | |
35 #include "webkit/plugins/ppapi/host_globals.h" | |
36 #include "webkit/plugins/ppapi/plugin_module.h" | |
37 #include "webkit/plugins/ppapi/ppapi_plugin_instance.h" | |
19 | 38 |
20 #if defined(OS_WIN) | 39 using content::RenderThread; |
21 #include "content/public/common/sandbox_init.h" | 40 using content::RenderView; |
22 #endif | 41 using webkit::ppapi::HostGlobals; |
42 using webkit::ppapi::PluginInstance; | |
43 using WebKit::WebView; | |
23 | 44 |
24 namespace { | 45 namespace { |
25 | 46 |
26 base::LazyInstance<scoped_refptr<IPC::SyncMessageFilter> > | 47 base::LazyInstance<scoped_refptr<IPC::SyncMessageFilter> > |
27 g_background_thread_sender = LAZY_INSTANCE_INITIALIZER; | 48 g_background_thread_sender = LAZY_INSTANCE_INITIALIZER; |
28 | 49 |
50 base::LazyInstance<IPC::ChannelHandle> g_ipc_channel_handle = | |
51 LAZY_INSTANCE_INITIALIZER; | |
52 base::ProcessId g_plugin_process_id; | |
53 | |
29 // Launch NaCl's sel_ldr process. | 54 // Launch NaCl's sel_ldr process. |
30 PP_Bool LaunchSelLdr(PP_Instance instance, | 55 PP_Bool LaunchSelLdr(PP_Instance instance, |
31 const char* alleged_url, int socket_count, | 56 const char* alleged_url, int socket_count, |
32 void* imc_handles) { | 57 void* imc_handles) { |
33 std::vector<nacl::FileDescriptor> sockets; | 58 std::vector<nacl::FileDescriptor> sockets; |
34 IPC::Message::Sender* sender = content::RenderThread::Get(); | 59 IPC::Message::Sender* sender = content::RenderThread::Get(); |
35 if (sender == NULL) | 60 if (sender == NULL) |
36 sender = g_background_thread_sender.Pointer()->get(); | 61 sender = g_background_thread_sender.Pointer()->get(); |
37 | 62 |
38 if (!sender->Send(new ChromeViewHostMsg_LaunchNaCl( | 63 if (!sender->Send(new ChromeViewHostMsg_LaunchNaCl( |
39 GURL(alleged_url), socket_count, &sockets))) | 64 GURL(alleged_url), socket_count, &sockets, |
65 g_ipc_channel_handle.Pointer(), | |
66 &g_plugin_process_id))) | |
40 return PP_FALSE; | 67 return PP_FALSE; |
41 | 68 |
42 CHECK(static_cast<int>(sockets.size()) == socket_count); | 69 CHECK(static_cast<int>(sockets.size()) == socket_count); |
43 for (int i = 0; i < socket_count; i++) { | 70 for (int i = 0; i < socket_count; i++) { |
44 static_cast<nacl::Handle*>(imc_handles)[i] = | 71 static_cast<nacl::Handle*>(imc_handles)[i] = |
45 nacl::ToNativeHandle(sockets[i]); | 72 nacl::ToNativeHandle(sockets[i]); |
46 } | 73 } |
47 | 74 |
48 return PP_TRUE; | 75 return PP_TRUE; |
49 } | 76 } |
50 | 77 |
78 class ProxyChannelDelegate | |
79 : public ppapi::proxy::ProxyChannel::Delegate { | |
80 public: | |
81 ProxyChannelDelegate(); | |
82 virtual ~ProxyChannelDelegate(); | |
83 | |
84 // ProxyChannel::Delegate implementation. | |
85 virtual base::MessageLoopProxy* GetIPCMessageLoop() OVERRIDE; | |
86 virtual base::WaitableEvent* GetShutdownEvent() OVERRIDE; | |
87 virtual IPC::PlatformFileForTransit ShareHandleWithRemote( | |
88 base::PlatformFile handle, | |
89 const IPC::SyncChannel& channel, | |
90 bool should_close_source) OVERRIDE; | |
91 private: | |
92 base::WaitableEvent shutdown_event_; | |
93 }; | |
94 | |
95 ProxyChannelDelegate::ProxyChannelDelegate() | |
96 : shutdown_event_(true, false) {} | |
97 | |
98 ProxyChannelDelegate::~ProxyChannelDelegate() { | |
99 } | |
100 | |
101 base::MessageLoopProxy* ProxyChannelDelegate::GetIPCMessageLoop() { | |
102 return RenderThread::Get()->GetIOMessageLoopProxy().get(); | |
103 } | |
104 | |
105 base::WaitableEvent* ProxyChannelDelegate::GetShutdownEvent() { | |
106 return &shutdown_event_; | |
107 } | |
108 | |
109 IPC::PlatformFileForTransit ProxyChannelDelegate::ShareHandleWithRemote( | |
110 base::PlatformFile handle, | |
111 const IPC::SyncChannel& channel, | |
112 bool should_close_source) { | |
113 return content::BrokerGetFileHandleForProcess(handle, channel.peer_pid(), | |
114 should_close_source); | |
115 } | |
116 | |
117 // Stubbed out SyncMessageStatusReceiver, required by HostDispatcher. | |
118 class SyncMessageStatusReceiver | |
119 : public ppapi::proxy::HostDispatcher::SyncMessageStatusReceiver { | |
120 public: | |
121 SyncMessageStatusReceiver() {} | |
122 virtual ~SyncMessageStatusReceiver() {} | |
123 | |
124 // SyncMessageStatusReceiver implementation. | |
125 virtual void BeginBlockOnSyncMessage() {} | |
126 virtual void EndBlockOnSyncMessage() {} | |
127 }; | |
128 | |
129 class OutOfProcessProxy | |
130 : public webkit::ppapi::PluginDelegate::OutOfProcessProxy { | |
131 public: | |
132 OutOfProcessProxy() {} | |
133 virtual ~OutOfProcessProxy() {} | |
134 | |
135 bool Init(const IPC::ChannelHandle& channel_handle, | |
136 PP_Module pp_module, | |
137 PP_GetInterface_Func local_get_interface, | |
138 const ppapi::Preferences& preferences, | |
139 SyncMessageStatusReceiver* status_receiver) { | |
140 if (channel_handle.name.empty()) | |
141 return false; | |
142 | |
143 #if defined(OS_POSIX) | |
144 DCHECK_NE(-1, channel_handle.socket.fd); | |
145 if (channel_handle.socket.fd == -1) | |
146 return false; | |
147 #endif | |
148 | |
149 dispatcher_delegate_.reset(new ProxyChannelDelegate); | |
150 dispatcher_.reset(new ppapi::proxy::HostDispatcher( | |
151 pp_module, local_get_interface, status_receiver)); | |
152 | |
153 if (!dispatcher_->InitHostWithChannel(dispatcher_delegate_.get(), | |
154 channel_handle, | |
155 true, // Client. | |
156 preferences)) { | |
157 dispatcher_.reset(); | |
158 dispatcher_delegate_.reset(); | |
159 return false; | |
160 } | |
161 | |
162 return true; | |
163 } | |
164 | |
165 // OutOfProcessProxy implementation. | |
166 virtual const void* GetProxiedInterface(const char* name) { | |
dmichael (off chromium)
2012/05/23 00:41:04
OVERRIDE for these?
bbudge
2012/05/23 21:42:20
Done.
| |
167 return dispatcher_->GetProxiedInterface(name); | |
168 } | |
169 virtual void AddInstance(PP_Instance instance) { | |
170 ppapi::proxy::HostDispatcher::SetForInstance(instance, dispatcher_.get()); | |
171 } | |
172 virtual void RemoveInstance(PP_Instance instance) { | |
173 ppapi::proxy::HostDispatcher::RemoveForInstance(instance); | |
174 } | |
175 | |
176 private: | |
177 scoped_ptr<ppapi::proxy::HostDispatcher> dispatcher_; | |
178 scoped_ptr<ppapi::proxy::ProxyChannel::Delegate> dispatcher_delegate_; | |
179 }; | |
180 | |
51 PP_Bool StartPpapiProxy(PP_Instance instance) { | 181 PP_Bool StartPpapiProxy(PP_Instance instance) { |
182 if (CommandLine::ForCurrentProcess()->HasSwitch( | |
183 switches::kEnableNaClIPCProxy)) { | |
184 IPC::ChannelHandle& channel_handle = g_ipc_channel_handle.Get(); | |
185 if (channel_handle.name.empty()) | |
186 return PP_FALSE; | |
187 | |
188 webkit::ppapi::PluginInstance* plugin_instance = | |
189 content::GetHostGlobals()->GetInstance(instance); | |
190 if (!plugin_instance) | |
191 return PP_FALSE; | |
192 | |
193 WebView* web_view = | |
194 plugin_instance->container()->element().document().frame()->view(); | |
195 RenderView* render_view = content::RenderView::FromWebView(web_view); | |
196 | |
197 webkit::ppapi::PluginModule* plugin_module = plugin_instance->module(); | |
198 | |
199 scoped_refptr<SyncMessageStatusReceiver> | |
200 status_receiver(new SyncMessageStatusReceiver()); | |
201 scoped_ptr<OutOfProcessProxy> out_of_process_proxy(new OutOfProcessProxy); | |
202 if (out_of_process_proxy->Init( | |
203 channel_handle, | |
204 plugin_module->pp_module(), | |
205 webkit::ppapi::PluginModule::GetLocalGetInterfaceFunc(), | |
206 ppapi::Preferences(render_view->GetWebkitPreferences()), | |
207 status_receiver.get())) { | |
208 plugin_module->InitAsProxied(out_of_process_proxy.release()); | |
dmichael (off chromium)
2012/05/23 00:41:04
I think this might cause trouble... this module h
| |
209 return PP_TRUE; | |
210 } | |
211 } | |
212 | |
52 return PP_FALSE; | 213 return PP_FALSE; |
53 } | 214 } |
54 | 215 |
55 int UrandomFD(void) { | 216 int UrandomFD(void) { |
56 #if defined(OS_POSIX) | 217 #if defined(OS_POSIX) |
57 return GetUrandomFD(); | 218 return GetUrandomFD(); |
58 #else | 219 #else |
59 return 0; | 220 return 0; |
60 #endif | 221 #endif |
61 } | 222 } |
(...skipping 30 matching lines...) Expand all Loading... | |
92 &BrokerDuplicateHandle, | 253 &BrokerDuplicateHandle, |
93 }; | 254 }; |
94 | 255 |
95 } // namespace | 256 } // namespace |
96 | 257 |
97 const PPB_NaCl_Private* PPB_NaCl_Private_Impl::GetInterface() { | 258 const PPB_NaCl_Private* PPB_NaCl_Private_Impl::GetInterface() { |
98 return &nacl_interface; | 259 return &nacl_interface; |
99 } | 260 } |
100 | 261 |
101 #endif // DISABLE_NACL | 262 #endif // DISABLE_NACL |
OLD | NEW |