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/lazy_instance.h" | 10 #include "base/lazy_instance.h" |
11 #include "base/logging.h" | 11 #include "base/logging.h" |
12 #include "base/message_loop.h" | |
12 #include "base/rand_util.h" | 13 #include "base/rand_util.h" |
14 #include "chrome/common/chrome_switches.h" | |
13 #include "chrome/common/render_messages.h" | 15 #include "chrome/common/render_messages.h" |
16 #include "content/public/common/content_client.h" | |
14 #include "content/public/common/content_switches.h" | 17 #include "content/public/common/content_switches.h" |
18 #include "content/public/common/sandbox_init.h" | |
15 #include "content/public/renderer/render_thread.h" | 19 #include "content/public/renderer/render_thread.h" |
20 #include "content/public/renderer/render_view.h" | |
16 #include "ipc/ipc_sync_message_filter.h" | 21 #include "ipc/ipc_sync_message_filter.h" |
17 #include "ppapi/c/private/ppb_nacl_private.h" | 22 #include "ppapi/c/private/ppb_nacl_private.h" |
18 #include "ppapi/native_client/src/trusted/plugin/nacl_entry_points.h" | 23 #include "ppapi/native_client/src/trusted/plugin/nacl_entry_points.h" |
24 #include "ppapi/proxy/host_dispatcher.h" | |
25 #include "ppapi/proxy/proxy_channel.h" | |
26 #include "ppapi/shared_impl/ppapi_preferences.h" | |
27 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h" | |
28 #include "third_party/WebKit/Source/WebKit/chromium/public/WebElement.h" | |
29 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" | |
30 #include "third_party/WebKit/Source/WebKit/chromium/public/WebPluginContainer.h" | |
31 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" | |
32 #include "webkit/plugins/ppapi/host_globals.h" | |
33 #include "webkit/plugins/ppapi/plugin_module.h" | |
34 #include "webkit/plugins/ppapi/ppapi_plugin_instance.h" | |
19 | 35 |
20 #if defined(OS_WIN) | 36 using content::RenderThread; |
21 #include "content/public/common/sandbox_init.h" | 37 using content::RenderView; |
22 #endif | 38 using webkit::ppapi::HostGlobals; |
39 using webkit::ppapi::PluginInstance; | |
40 using webkit::ppapi::PluginDelegate; | |
41 using WebKit::WebView; | |
23 | 42 |
24 namespace { | 43 namespace { |
25 | 44 |
26 base::LazyInstance<scoped_refptr<IPC::SyncMessageFilter> > | 45 base::LazyInstance<scoped_refptr<IPC::SyncMessageFilter> > |
27 g_background_thread_sender = LAZY_INSTANCE_INITIALIZER; | 46 g_background_thread_sender = LAZY_INSTANCE_INITIALIZER; |
28 | 47 |
29 // Launch NaCl's sel_ldr process. | 48 // Launch NaCl's sel_ldr process. |
30 PP_Bool LaunchSelLdr(PP_Instance instance, | 49 PP_Bool LaunchSelLdr(PP_Instance instance, |
31 const char* alleged_url, int socket_count, | 50 const char* alleged_url, |
32 void* imc_handles) { | 51 int socket_count, |
52 void* imc_handles, | |
53 void** ipc_channel_handle) { | |
33 std::vector<nacl::FileDescriptor> sockets; | 54 std::vector<nacl::FileDescriptor> sockets; |
34 IPC::Sender* sender = content::RenderThread::Get(); | 55 IPC::Sender* sender = content::RenderThread::Get(); |
35 if (sender == NULL) | 56 if (sender == NULL) |
36 sender = g_background_thread_sender.Pointer()->get(); | 57 sender = g_background_thread_sender.Pointer()->get(); |
37 | 58 |
59 scoped_ptr<IPC::ChannelHandle> channel_handle(new IPC::ChannelHandle); | |
38 if (!sender->Send(new ChromeViewHostMsg_LaunchNaCl( | 60 if (!sender->Send(new ChromeViewHostMsg_LaunchNaCl( |
39 GURL(alleged_url), socket_count, &sockets))) | 61 GURL(alleged_url), socket_count, &sockets, |
62 channel_handle.get()))) { | |
63 *ipc_channel_handle = NULL; | |
40 return PP_FALSE; | 64 return PP_FALSE; |
65 } | |
66 | |
67 *ipc_channel_handle = channel_handle.release(); | |
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 |
51 PP_Bool StartPpapiProxy(PP_Instance instance) { | 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 | |
99 ProxyChannelDelegate::~ProxyChannelDelegate() { | |
100 } | |
101 | |
102 base::MessageLoopProxy* ProxyChannelDelegate::GetIPCMessageLoop() { | |
103 return RenderThread::Get()->GetIOMessageLoopProxy().get(); | |
104 } | |
105 | |
106 base::WaitableEvent* ProxyChannelDelegate::GetShutdownEvent() { | |
107 return &shutdown_event_; | |
brettw
2012/06/21 20:59:44
I think this is supposed to be
return ChildProce
bbudge
2012/06/21 21:20:01
Done.
| |
108 } | |
109 | |
110 IPC::PlatformFileForTransit ProxyChannelDelegate::ShareHandleWithRemote( | |
111 base::PlatformFile handle, | |
112 const IPC::SyncChannel& channel, | |
113 bool should_close_source) { | |
114 return content::BrokerGetFileHandleForProcess(handle, channel.peer_pid(), | |
115 should_close_source); | |
116 } | |
117 | |
118 // Stubbed out SyncMessageStatusReceiver, required by HostDispatcher. | |
119 // TODO(bbudge) Implement something like PepperHungPluginFilter. | |
brettw
2012/06/21 20:59:44
This should say "hook up to PepperHungPluginFilter
bbudge
2012/06/21 21:20:01
Done.
| |
120 class SyncMessageStatusReceiver | |
121 : public ppapi::proxy::HostDispatcher::SyncMessageStatusReceiver { | |
122 public: | |
123 SyncMessageStatusReceiver() {} | |
124 | |
125 // SyncMessageStatusReceiver implementation. | |
126 virtual void BeginBlockOnSyncMessage() OVERRIDE {} | |
127 virtual void EndBlockOnSyncMessage() OVERRIDE {} | |
128 | |
129 private: | |
130 virtual ~SyncMessageStatusReceiver() {} | |
131 }; | |
132 | |
133 class OutOfProcessProxy : public PluginDelegate::OutOfProcessProxy { | |
134 public: | |
135 OutOfProcessProxy() {} | |
136 virtual ~OutOfProcessProxy() {} | |
137 | |
138 bool Init(const IPC::ChannelHandle& channel_handle, | |
139 PP_Module pp_module, | |
140 PP_GetInterface_Func local_get_interface, | |
141 const ppapi::Preferences& preferences, | |
142 SyncMessageStatusReceiver* status_receiver) { | |
143 if (channel_handle.name.empty()) | |
144 return false; | |
145 | |
146 #if defined(OS_POSIX) | |
147 DCHECK_NE(-1, channel_handle.socket.fd); | |
148 if (channel_handle.socket.fd == -1) | |
149 return false; | |
150 #endif | |
151 | |
152 dispatcher_delegate_.reset(new ProxyChannelDelegate); | |
153 dispatcher_.reset(new ppapi::proxy::HostDispatcher( | |
154 pp_module, local_get_interface, status_receiver)); | |
155 | |
156 if (!dispatcher_->InitHostWithChannel(dispatcher_delegate_.get(), | |
157 channel_handle, | |
158 true, // Client. | |
159 preferences)) { | |
160 dispatcher_.reset(); | |
161 dispatcher_delegate_.reset(); | |
162 return false; | |
163 } | |
164 | |
165 return true; | |
166 } | |
167 | |
168 // OutOfProcessProxy implementation. | |
169 virtual const void* GetProxiedInterface(const char* name) OVERRIDE { | |
170 return dispatcher_->GetProxiedInterface(name); | |
171 } | |
172 virtual void AddInstance(PP_Instance instance) OVERRIDE { | |
173 ppapi::proxy::HostDispatcher::SetForInstance(instance, dispatcher_.get()); | |
174 } | |
175 virtual void RemoveInstance(PP_Instance instance) OVERRIDE { | |
176 ppapi::proxy::HostDispatcher::RemoveForInstance(instance); | |
177 } | |
178 | |
179 private: | |
180 scoped_ptr<ppapi::proxy::HostDispatcher> dispatcher_; | |
181 scoped_ptr<ppapi::proxy::ProxyChannel::Delegate> dispatcher_delegate_; | |
182 }; | |
183 | |
184 PP_Bool StartPpapiProxy(PP_Instance instance, | |
185 void* ipc_channel_handle) { | |
186 if (CommandLine::ForCurrentProcess()->HasSwitch( | |
187 switches::kEnableNaClIPCProxy)) { | |
188 scoped_ptr<IPC::ChannelHandle> channel_handle( | |
189 static_cast<IPC::ChannelHandle*>(ipc_channel_handle)); | |
190 if (channel_handle->name.empty()) | |
191 return PP_FALSE; | |
192 | |
193 webkit::ppapi::PluginInstance* plugin_instance = | |
194 content::GetHostGlobals()->GetInstance(instance); | |
195 if (!plugin_instance) | |
196 return PP_FALSE; | |
197 | |
198 WebView* web_view = | |
199 plugin_instance->container()->element().document().frame()->view(); | |
200 RenderView* render_view = content::RenderView::FromWebView(web_view); | |
201 | |
202 webkit::ppapi::PluginModule* plugin_module = plugin_instance->module(); | |
203 | |
204 scoped_refptr<SyncMessageStatusReceiver> | |
205 status_receiver(new SyncMessageStatusReceiver()); | |
206 scoped_ptr<OutOfProcessProxy> out_of_process_proxy(new OutOfProcessProxy); | |
207 if (out_of_process_proxy->Init( | |
208 *channel_handle, | |
209 plugin_module->pp_module(), | |
210 webkit::ppapi::PluginModule::GetLocalGetInterfaceFunc(), | |
211 ppapi::Preferences(render_view->GetWebkitPreferences()), | |
212 status_receiver.get())) { | |
213 plugin_module->InitAsProxiedNaCl( | |
214 out_of_process_proxy.PassAs<PluginDelegate::OutOfProcessProxy>(), | |
215 instance); | |
216 return PP_TRUE; | |
217 } | |
218 } | |
219 | |
52 return PP_FALSE; | 220 return PP_FALSE; |
53 } | 221 } |
54 | 222 |
55 int UrandomFD(void) { | 223 int UrandomFD(void) { |
56 #if defined(OS_POSIX) | 224 #if defined(OS_POSIX) |
57 return base::GetUrandomFD(); | 225 return base::GetUrandomFD(); |
58 #else | 226 #else |
59 return -1; | 227 return -1; |
60 #endif | 228 #endif |
61 } | 229 } |
(...skipping 30 matching lines...) Expand all Loading... | |
92 &BrokerDuplicateHandle, | 260 &BrokerDuplicateHandle, |
93 }; | 261 }; |
94 | 262 |
95 } // namespace | 263 } // namespace |
96 | 264 |
97 const PPB_NaCl_Private* PPB_NaCl_Private_Impl::GetInterface() { | 265 const PPB_NaCl_Private* PPB_NaCl_Private_Impl::GetInterface() { |
98 return &nacl_interface; | 266 return &nacl_interface; |
99 } | 267 } |
100 | 268 |
101 #endif // DISABLE_NACL | 269 #endif // DISABLE_NACL |
OLD | NEW |