| 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 |
| 48 typedef std::map<PP_Instance, IPC::ChannelHandle> ChannelHandleMap; |
| 49 |
| 50 base::LazyInstance<ChannelHandleMap> g_channel_handle_map = |
| 51 LAZY_INSTANCE_INITIALIZER; |
| 52 |
| 29 // Launch NaCl's sel_ldr process. | 53 // Launch NaCl's sel_ldr process. |
| 30 PP_Bool LaunchSelLdr(PP_Instance instance, | 54 PP_Bool LaunchSelLdr(PP_Instance instance, |
| 31 const char* alleged_url, int socket_count, | 55 const char* alleged_url, |
| 56 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::Sender* sender = content::RenderThread::Get(); | 59 IPC::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 |
| 63 IPC::ChannelHandle channel_handle; |
| 38 if (!sender->Send(new ChromeViewHostMsg_LaunchNaCl( | 64 if (!sender->Send(new ChromeViewHostMsg_LaunchNaCl( |
| 39 GURL(alleged_url), socket_count, &sockets))) | 65 GURL(alleged_url), socket_count, &sockets, |
| 66 &channel_handle))) { |
| 40 return PP_FALSE; | 67 return PP_FALSE; |
| 68 } |
| 69 |
| 70 // Don't save invalid channel handles. |
| 71 bool invalid_handle = channel_handle.name.empty(); |
| 72 |
| 73 #if defined(OS_POSIX) |
| 74 if (!invalid_handle) |
| 75 invalid_handle = (channel_handle.socket.fd == -1); |
| 76 #endif |
| 77 |
| 78 if (!invalid_handle) |
| 79 g_channel_handle_map.Get()[instance] = channel_handle; |
| 41 | 80 |
| 42 CHECK(static_cast<int>(sockets.size()) == socket_count); | 81 CHECK(static_cast<int>(sockets.size()) == socket_count); |
| 43 for (int i = 0; i < socket_count; i++) { | 82 for (int i = 0; i < socket_count; i++) { |
| 44 static_cast<nacl::Handle*>(imc_handles)[i] = | 83 static_cast<nacl::Handle*>(imc_handles)[i] = |
| 45 nacl::ToNativeHandle(sockets[i]); | 84 nacl::ToNativeHandle(sockets[i]); |
| 46 } | 85 } |
| 47 | 86 |
| 48 return PP_TRUE; | 87 return PP_TRUE; |
| 49 } | 88 } |
| 50 | 89 |
| 90 class ProxyChannelDelegate |
| 91 : public ppapi::proxy::ProxyChannel::Delegate { |
| 92 public: |
| 93 ProxyChannelDelegate(); |
| 94 virtual ~ProxyChannelDelegate(); |
| 95 |
| 96 // ProxyChannel::Delegate implementation. |
| 97 virtual base::MessageLoopProxy* GetIPCMessageLoop() OVERRIDE; |
| 98 virtual base::WaitableEvent* GetShutdownEvent() OVERRIDE; |
| 99 virtual IPC::PlatformFileForTransit ShareHandleWithRemote( |
| 100 base::PlatformFile handle, |
| 101 const IPC::SyncChannel& channel, |
| 102 bool should_close_source) OVERRIDE; |
| 103 private: |
| 104 // TODO(bbudge) Modify the content public API so we can get |
| 105 // the renderer process's shutdown event. |
| 106 base::WaitableEvent shutdown_event_; |
| 107 }; |
| 108 |
| 109 ProxyChannelDelegate::ProxyChannelDelegate() |
| 110 : shutdown_event_(true, false) { |
| 111 } |
| 112 |
| 113 ProxyChannelDelegate::~ProxyChannelDelegate() { |
| 114 } |
| 115 |
| 116 base::MessageLoopProxy* ProxyChannelDelegate::GetIPCMessageLoop() { |
| 117 return RenderThread::Get()->GetIOMessageLoopProxy().get(); |
| 118 } |
| 119 |
| 120 base::WaitableEvent* ProxyChannelDelegate::GetShutdownEvent() { |
| 121 return &shutdown_event_; |
| 122 } |
| 123 |
| 124 IPC::PlatformFileForTransit ProxyChannelDelegate::ShareHandleWithRemote( |
| 125 base::PlatformFile handle, |
| 126 const IPC::SyncChannel& channel, |
| 127 bool should_close_source) { |
| 128 return content::BrokerGetFileHandleForProcess(handle, channel.peer_pid(), |
| 129 should_close_source); |
| 130 } |
| 131 |
| 132 // Stubbed out SyncMessageStatusReceiver, required by HostDispatcher. |
| 133 // TODO(bbudge) Use a content::PepperHungPluginFilter instead. |
| 134 class SyncMessageStatusReceiver |
| 135 : public ppapi::proxy::HostDispatcher::SyncMessageStatusReceiver { |
| 136 public: |
| 137 SyncMessageStatusReceiver() {} |
| 138 |
| 139 // SyncMessageStatusReceiver implementation. |
| 140 virtual void BeginBlockOnSyncMessage() OVERRIDE {} |
| 141 virtual void EndBlockOnSyncMessage() OVERRIDE {} |
| 142 |
| 143 private: |
| 144 virtual ~SyncMessageStatusReceiver() {} |
| 145 }; |
| 146 |
| 147 class OutOfProcessProxy : public PluginDelegate::OutOfProcessProxy { |
| 148 public: |
| 149 OutOfProcessProxy() {} |
| 150 virtual ~OutOfProcessProxy() {} |
| 151 |
| 152 bool Init(const IPC::ChannelHandle& channel_handle, |
| 153 PP_Module pp_module, |
| 154 PP_GetInterface_Func local_get_interface, |
| 155 const ppapi::Preferences& preferences, |
| 156 SyncMessageStatusReceiver* status_receiver) { |
| 157 dispatcher_delegate_.reset(new ProxyChannelDelegate); |
| 158 dispatcher_.reset(new ppapi::proxy::HostDispatcher( |
| 159 pp_module, local_get_interface, status_receiver)); |
| 160 |
| 161 if (!dispatcher_->InitHostWithChannel(dispatcher_delegate_.get(), |
| 162 channel_handle, |
| 163 true, // Client. |
| 164 preferences)) { |
| 165 dispatcher_.reset(); |
| 166 dispatcher_delegate_.reset(); |
| 167 return false; |
| 168 } |
| 169 |
| 170 return true; |
| 171 } |
| 172 |
| 173 // OutOfProcessProxy implementation. |
| 174 virtual const void* GetProxiedInterface(const char* name) OVERRIDE { |
| 175 return dispatcher_->GetProxiedInterface(name); |
| 176 } |
| 177 virtual void AddInstance(PP_Instance instance) OVERRIDE { |
| 178 ppapi::proxy::HostDispatcher::SetForInstance(instance, dispatcher_.get()); |
| 179 } |
| 180 virtual void RemoveInstance(PP_Instance instance) OVERRIDE { |
| 181 ppapi::proxy::HostDispatcher::RemoveForInstance(instance); |
| 182 } |
| 183 |
| 184 private: |
| 185 scoped_ptr<ppapi::proxy::HostDispatcher> dispatcher_; |
| 186 scoped_ptr<ppapi::proxy::ProxyChannel::Delegate> dispatcher_delegate_; |
| 187 }; |
| 188 |
| 51 PP_Bool StartPpapiProxy(PP_Instance instance) { | 189 PP_Bool StartPpapiProxy(PP_Instance instance) { |
| 190 if (CommandLine::ForCurrentProcess()->HasSwitch( |
| 191 switches::kEnableNaClIPCProxy)) { |
| 192 ChannelHandleMap& map = g_channel_handle_map.Get(); |
| 193 ChannelHandleMap::iterator it = map.find(instance); |
| 194 if (it == map.end()) |
| 195 return PP_FALSE; |
| 196 IPC::ChannelHandle channel_handle = it->second; |
| 197 map.erase(it); |
| 198 |
| 199 webkit::ppapi::PluginInstance* plugin_instance = |
| 200 content::GetHostGlobals()->GetInstance(instance); |
| 201 if (!plugin_instance) |
| 202 return PP_FALSE; |
| 203 |
| 204 WebView* web_view = |
| 205 plugin_instance->container()->element().document().frame()->view(); |
| 206 RenderView* render_view = content::RenderView::FromWebView(web_view); |
| 207 |
| 208 webkit::ppapi::PluginModule* plugin_module = plugin_instance->module(); |
| 209 |
| 210 scoped_refptr<SyncMessageStatusReceiver> |
| 211 status_receiver(new SyncMessageStatusReceiver()); |
| 212 scoped_ptr<OutOfProcessProxy> out_of_process_proxy(new OutOfProcessProxy); |
| 213 if (out_of_process_proxy->Init( |
| 214 channel_handle, |
| 215 plugin_module->pp_module(), |
| 216 webkit::ppapi::PluginModule::GetLocalGetInterfaceFunc(), |
| 217 ppapi::Preferences(render_view->GetWebkitPreferences()), |
| 218 status_receiver.get())) { |
| 219 plugin_module->InitAsProxiedNaCl( |
| 220 out_of_process_proxy.PassAs<PluginDelegate::OutOfProcessProxy>(), |
| 221 instance); |
| 222 return PP_TRUE; |
| 223 } |
| 224 } |
| 225 |
| 52 return PP_FALSE; | 226 return PP_FALSE; |
| 53 } | 227 } |
| 54 | 228 |
| 55 int UrandomFD(void) { | 229 int UrandomFD(void) { |
| 56 #if defined(OS_POSIX) | 230 #if defined(OS_POSIX) |
| 57 return base::GetUrandomFD(); | 231 return base::GetUrandomFD(); |
| 58 #else | 232 #else |
| 59 return -1; | 233 return -1; |
| 60 #endif | 234 #endif |
| 61 } | 235 } |
| (...skipping 30 matching lines...) Expand all Loading... |
| 92 &BrokerDuplicateHandle, | 266 &BrokerDuplicateHandle, |
| 93 }; | 267 }; |
| 94 | 268 |
| 95 } // namespace | 269 } // namespace |
| 96 | 270 |
| 97 const PPB_NaCl_Private* PPB_NaCl_Private_Impl::GetInterface() { | 271 const PPB_NaCl_Private* PPB_NaCl_Private_Impl::GetInterface() { |
| 98 return &nacl_interface; | 272 return &nacl_interface; |
| 99 } | 273 } |
| 100 | 274 |
| 101 #endif // DISABLE_NACL | 275 #endif // DISABLE_NACL |
| OLD | NEW |