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