Chromium Code Reviews| 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::WebView; | |
| 23 | 41 |
| 24 namespace { | 42 namespace { |
| 25 | 43 |
| 26 base::LazyInstance<scoped_refptr<IPC::SyncMessageFilter> > | 44 base::LazyInstance<scoped_refptr<IPC::SyncMessageFilter> > |
| 27 g_background_thread_sender = LAZY_INSTANCE_INITIALIZER; | 45 g_background_thread_sender = LAZY_INSTANCE_INITIALIZER; |
| 28 | 46 |
| 47 base::LazyInstance<IPC::ChannelHandle> g_ipc_channel_handle = | |
|
Mark Seaborn
2012/06/20 19:24:40
Why is this a global variable? A global is very l
bbudge
2012/06/21 00:00:46
Changed to a global map that associates ChannelHan
Mark Seaborn
2012/06/21 15:30:03
OK, but using a global still isn't very good. If
bbudge
2012/06/21 18:16:53
An IPC::ChannelHandle is a small struct that conta
| |
| 48 LAZY_INSTANCE_INITIALIZER; | |
| 49 base::ProcessId g_plugin_process_id; | |
|
Mark Seaborn
2012/06/20 19:24:40
This variable is assigned to but never read.
bbudge
2012/06/21 00:00:46
Done.
| |
| 50 | |
| 29 // Launch NaCl's sel_ldr process. | 51 // Launch NaCl's sel_ldr process. |
| 30 PP_Bool LaunchSelLdr(PP_Instance instance, | 52 PP_Bool LaunchSelLdr(PP_Instance instance, |
| 31 const char* alleged_url, int socket_count, | 53 const char* alleged_url, int socket_count, |
| 32 void* imc_handles) { | 54 void* imc_handles) { |
| 33 std::vector<nacl::FileDescriptor> sockets; | 55 std::vector<nacl::FileDescriptor> sockets; |
| 34 IPC::Sender* sender = content::RenderThread::Get(); | 56 IPC::Sender* sender = content::RenderThread::Get(); |
| 35 if (sender == NULL) | 57 if (sender == NULL) |
| 36 sender = g_background_thread_sender.Pointer()->get(); | 58 sender = g_background_thread_sender.Pointer()->get(); |
| 37 | 59 |
| 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 g_ipc_channel_handle.Pointer(), | |
| 63 &g_plugin_process_id))) | |
| 40 return PP_FALSE; | 64 return PP_FALSE; |
| 41 | 65 |
| 42 CHECK(static_cast<int>(sockets.size()) == socket_count); | 66 CHECK(static_cast<int>(sockets.size()) == socket_count); |
| 43 for (int i = 0; i < socket_count; i++) { | 67 for (int i = 0; i < socket_count; i++) { |
| 44 static_cast<nacl::Handle*>(imc_handles)[i] = | 68 static_cast<nacl::Handle*>(imc_handles)[i] = |
| 45 nacl::ToNativeHandle(sockets[i]); | 69 nacl::ToNativeHandle(sockets[i]); |
| 46 } | 70 } |
| 47 | 71 |
| 48 return PP_TRUE; | 72 return PP_TRUE; |
| 49 } | 73 } |
| 50 | 74 |
| 75 class ProxyChannelDelegate | |
| 76 : public ppapi::proxy::ProxyChannel::Delegate { | |
| 77 public: | |
| 78 ProxyChannelDelegate(); | |
| 79 virtual ~ProxyChannelDelegate(); | |
| 80 | |
| 81 // ProxyChannel::Delegate implementation. | |
| 82 virtual base::MessageLoopProxy* GetIPCMessageLoop() OVERRIDE; | |
| 83 virtual base::WaitableEvent* GetShutdownEvent() OVERRIDE; | |
| 84 virtual IPC::PlatformFileForTransit ShareHandleWithRemote( | |
| 85 base::PlatformFile handle, | |
| 86 const IPC::SyncChannel& channel, | |
| 87 bool should_close_source) OVERRIDE; | |
| 88 private: | |
| 89 base::WaitableEvent shutdown_event_; | |
| 90 }; | |
| 91 | |
| 92 ProxyChannelDelegate::ProxyChannelDelegate() | |
| 93 : shutdown_event_(true, false) {} | |
|
dmichael (off chromium)
2012/06/20 22:34:32
style nit: I'd put } on the next line to be consis
bbudge
2012/06/21 00:00:46
Done.
| |
| 94 | |
| 95 ProxyChannelDelegate::~ProxyChannelDelegate() { | |
| 96 } | |
| 97 | |
| 98 base::MessageLoopProxy* ProxyChannelDelegate::GetIPCMessageLoop() { | |
| 99 return RenderThread::Get()->GetIOMessageLoopProxy().get(); | |
| 100 } | |
| 101 | |
| 102 base::WaitableEvent* ProxyChannelDelegate::GetShutdownEvent() { | |
| 103 return &shutdown_event_; | |
| 104 } | |
| 105 | |
| 106 IPC::PlatformFileForTransit ProxyChannelDelegate::ShareHandleWithRemote( | |
| 107 base::PlatformFile handle, | |
| 108 const IPC::SyncChannel& channel, | |
| 109 bool should_close_source) { | |
| 110 return content::BrokerGetFileHandleForProcess(handle, channel.peer_pid(), | |
| 111 should_close_source); | |
| 112 } | |
| 113 | |
| 114 // Stubbed out SyncMessageStatusReceiver, required by HostDispatcher. | |
|
dmichael (off chromium)
2012/06/20 22:34:32
Should we add a TODO?
bbudge
2012/06/21 00:00:46
Done.
| |
| 115 class SyncMessageStatusReceiver | |
| 116 : public ppapi::proxy::HostDispatcher::SyncMessageStatusReceiver { | |
| 117 public: | |
| 118 SyncMessageStatusReceiver() {} | |
| 119 | |
| 120 // SyncMessageStatusReceiver implementation. | |
| 121 virtual void BeginBlockOnSyncMessage() OVERRIDE {} | |
| 122 virtual void EndBlockOnSyncMessage() OVERRIDE {} | |
| 123 | |
| 124 private: | |
| 125 virtual ~SyncMessageStatusReceiver() {} | |
| 126 }; | |
| 127 | |
| 128 class OutOfProcessProxy | |
| 129 : public webkit::ppapi::PluginDelegate::OutOfProcessProxy { | |
| 130 public: | |
| 131 OutOfProcessProxy() {} | |
| 132 virtual ~OutOfProcessProxy() {} | |
| 133 | |
| 134 bool Init(const IPC::ChannelHandle& channel_handle, | |
| 135 PP_Module pp_module, | |
| 136 PP_GetInterface_Func local_get_interface, | |
| 137 const ppapi::Preferences& preferences, | |
| 138 SyncMessageStatusReceiver* status_receiver) { | |
| 139 if (channel_handle.name.empty()) | |
| 140 return false; | |
| 141 | |
| 142 #if defined(OS_POSIX) | |
| 143 DCHECK_NE(-1, channel_handle.socket.fd); | |
| 144 if (channel_handle.socket.fd == -1) | |
| 145 return false; | |
| 146 #endif | |
| 147 | |
| 148 dispatcher_delegate_.reset(new ProxyChannelDelegate); | |
| 149 dispatcher_.reset(new ppapi::proxy::HostDispatcher( | |
| 150 pp_module, local_get_interface, status_receiver)); | |
| 151 | |
| 152 if (!dispatcher_->InitHostWithChannel(dispatcher_delegate_.get(), | |
| 153 channel_handle, | |
| 154 true, // Client. | |
| 155 preferences)) { | |
| 156 dispatcher_.reset(); | |
| 157 dispatcher_delegate_.reset(); | |
| 158 return false; | |
| 159 } | |
| 160 | |
| 161 return true; | |
| 162 } | |
| 163 | |
| 164 // OutOfProcessProxy implementation. | |
| 165 virtual const void* GetProxiedInterface(const char* name) OVERRIDE { | |
| 166 return dispatcher_->GetProxiedInterface(name); | |
| 167 } | |
| 168 virtual void AddInstance(PP_Instance instance) OVERRIDE { | |
| 169 ppapi::proxy::HostDispatcher::SetForInstance(instance, dispatcher_.get()); | |
| 170 } | |
| 171 virtual void RemoveInstance(PP_Instance instance) OVERRIDE { | |
| 172 ppapi::proxy::HostDispatcher::RemoveForInstance(instance); | |
| 173 } | |
| 174 | |
| 175 private: | |
| 176 scoped_ptr<ppapi::proxy::HostDispatcher> dispatcher_; | |
| 177 scoped_ptr<ppapi::proxy::ProxyChannel::Delegate> dispatcher_delegate_; | |
| 178 }; | |
| 179 | |
| 51 PP_Bool StartPpapiProxy(PP_Instance instance) { | 180 PP_Bool StartPpapiProxy(PP_Instance instance) { |
| 181 if (CommandLine::ForCurrentProcess()->HasSwitch( | |
| 182 switches::kEnableNaClIPCProxy)) { | |
| 183 IPC::ChannelHandle& channel_handle = g_ipc_channel_handle.Get(); | |
| 184 if (channel_handle.name.empty()) | |
| 185 return PP_FALSE; | |
| 186 | |
| 187 webkit::ppapi::PluginInstance* plugin_instance = | |
| 188 content::GetHostGlobals()->GetInstance(instance); | |
| 189 if (!plugin_instance) | |
| 190 return PP_FALSE; | |
| 191 | |
| 192 WebView* web_view = | |
| 193 plugin_instance->container()->element().document().frame()->view(); | |
| 194 RenderView* render_view = content::RenderView::FromWebView(web_view); | |
| 195 | |
| 196 webkit::ppapi::PluginModule* plugin_module = plugin_instance->module(); | |
| 197 | |
| 198 scoped_refptr<SyncMessageStatusReceiver> | |
| 199 status_receiver(new SyncMessageStatusReceiver()); | |
| 200 scoped_ptr<OutOfProcessProxy> out_of_process_proxy(new OutOfProcessProxy); | |
| 201 if (out_of_process_proxy->Init( | |
| 202 channel_handle, | |
| 203 plugin_module->pp_module(), | |
| 204 webkit::ppapi::PluginModule::GetLocalGetInterfaceFunc(), | |
| 205 ppapi::Preferences(render_view->GetWebkitPreferences()), | |
| 206 status_receiver.get())) { | |
| 207 out_of_process_proxy->AddInstance(instance); | |
| 208 plugin_module->InitAsProxiedNaCl(out_of_process_proxy.release(), | |
|
dmichael (off chromium)
2012/06/20 22:34:32
nit: We should probably use Pass() here and take s
bbudge
2012/06/21 00:00:46
Done.
| |
| 209 instance); | |
| 210 return PP_TRUE; | |
| 211 } | |
| 212 } | |
| 213 | |
| 52 return PP_FALSE; | 214 return PP_FALSE; |
| 53 } | 215 } |
| 54 | 216 |
| 55 int UrandomFD(void) { | 217 int UrandomFD(void) { |
| 56 #if defined(OS_POSIX) | 218 #if defined(OS_POSIX) |
| 57 return base::GetUrandomFD(); | 219 return base::GetUrandomFD(); |
| 58 #else | 220 #else |
| 59 return -1; | 221 return -1; |
| 60 #endif | 222 #endif |
| 61 } | 223 } |
| (...skipping 30 matching lines...) Expand all Loading... | |
| 92 &BrokerDuplicateHandle, | 254 &BrokerDuplicateHandle, |
| 93 }; | 255 }; |
| 94 | 256 |
| 95 } // namespace | 257 } // namespace |
| 96 | 258 |
| 97 const PPB_NaCl_Private* PPB_NaCl_Private_Impl::GetInterface() { | 259 const PPB_NaCl_Private* PPB_NaCl_Private_Impl::GetInterface() { |
| 98 return &nacl_interface; | 260 return &nacl_interface; |
| 99 } | 261 } |
| 100 | 262 |
| 101 #endif // DISABLE_NACL | 263 #endif // DISABLE_NACL |
| OLD | NEW |