Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(89)

Side by Side Diff: chrome/renderer/pepper/ppb_nacl_private_impl.cc

Issue 10214007: Add an IPC channel between the NaCl loader process and the renderer. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Created 8 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 // 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
52 return PP_FALSE; 222 return PP_FALSE;
53 } 223 }
54 224
55 int UrandomFD(void) { 225 int UrandomFD(void) {
56 #if defined(OS_POSIX) 226 #if defined(OS_POSIX)
57 return base::GetUrandomFD(); 227 return base::GetUrandomFD();
58 #else 228 #else
59 return -1; 229 return -1;
60 #endif 230 #endif
61 } 231 }
(...skipping 30 matching lines...) Expand all
92 &BrokerDuplicateHandle, 262 &BrokerDuplicateHandle,
93 }; 263 };
94 264
95 } // namespace 265 } // namespace
96 266
97 const PPB_NaCl_Private* PPB_NaCl_Private_Impl::GetInterface() { 267 const PPB_NaCl_Private* PPB_NaCl_Private_Impl::GetInterface() {
98 return &nacl_interface; 268 return &nacl_interface;
99 } 269 }
100 270
101 #endif // DISABLE_NACL 271 #endif // DISABLE_NACL
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698