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

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 <map>
6
5 #include "chrome/renderer/pepper/ppb_nacl_private_impl.h" 7 #include "chrome/renderer/pepper/ppb_nacl_private_impl.h"
6 8
7 #ifndef DISABLE_NACL 9 #ifndef DISABLE_NACL
8 10
9 #include "base/command_line.h" 11 #include "base/command_line.h"
10 #include "base/lazy_instance.h" 12 #include "base/lazy_instance.h"
11 #include "base/logging.h" 13 #include "base/logging.h"
14 #include "base/message_loop.h"
12 #include "base/rand_util.h" 15 #include "base/rand_util.h"
16 #include "chrome/common/chrome_switches.h"
13 #include "chrome/common/render_messages.h" 17 #include "chrome/common/render_messages.h"
18 #include "content/public/common/content_client.h"
14 #include "content/public/common/content_switches.h" 19 #include "content/public/common/content_switches.h"
20 #include "content/public/common/sandbox_init.h"
15 #include "content/public/renderer/render_thread.h" 21 #include "content/public/renderer/render_thread.h"
22 #include "content/public/renderer/render_view.h"
16 #include "ipc/ipc_sync_message_filter.h" 23 #include "ipc/ipc_sync_message_filter.h"
17 #include "ppapi/c/private/ppb_nacl_private.h" 24 #include "ppapi/c/private/ppb_nacl_private.h"
18 #include "ppapi/native_client/src/trusted/plugin/nacl_entry_points.h" 25 #include "ppapi/native_client/src/trusted/plugin/nacl_entry_points.h"
26 #include "ppapi/proxy/host_dispatcher.h"
27 #include "ppapi/proxy/proxy_channel.h"
28 #include "ppapi/shared_impl/ppapi_preferences.h"
29 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h"
30 #include "third_party/WebKit/Source/WebKit/chromium/public/WebElement.h"
31 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h"
32 #include "third_party/WebKit/Source/WebKit/chromium/public/WebPluginContainer.h"
33 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h"
34 #include "webkit/plugins/ppapi/host_globals.h"
35 #include "webkit/plugins/ppapi/plugin_module.h"
36 #include "webkit/plugins/ppapi/ppapi_plugin_instance.h"
19 37
20 #if defined(OS_WIN) 38 using content::RenderThread;
21 #include "content/public/common/sandbox_init.h" 39 using content::RenderView;
22 #endif 40 using webkit::ppapi::HostGlobals;
41 using webkit::ppapi::PluginInstance;
42 using webkit::ppapi::PluginDelegate;
43 using WebKit::WebView;
23 44
24 namespace { 45 namespace {
25 46
26 base::LazyInstance<scoped_refptr<IPC::SyncMessageFilter> > 47 base::LazyInstance<scoped_refptr<IPC::SyncMessageFilter> >
27 g_background_thread_sender = LAZY_INSTANCE_INITIALIZER; 48 g_background_thread_sender = LAZY_INSTANCE_INITIALIZER;
28 49
50 typedef std::map<PP_Instance, IPC::ChannelHandle> ChannelHandleMap;
51
52 base::LazyInstance<ChannelHandleMap> g_channel_handle_map =
53 LAZY_INSTANCE_INITIALIZER;
54
29 // Launch NaCl's sel_ldr process. 55 // Launch NaCl's sel_ldr process.
30 PP_Bool LaunchSelLdr(PP_Instance instance, 56 PP_Bool LaunchSelLdr(PP_Instance instance,
31 const char* alleged_url, int socket_count, 57 const char* alleged_url, int socket_count,
32 void* imc_handles) { 58 void* imc_handles) {
33 std::vector<nacl::FileDescriptor> sockets; 59 std::vector<nacl::FileDescriptor> sockets;
34 IPC::Sender* sender = content::RenderThread::Get(); 60 IPC::Sender* sender = content::RenderThread::Get();
35 if (sender == NULL) 61 if (sender == NULL)
36 sender = g_background_thread_sender.Pointer()->get(); 62 sender = g_background_thread_sender.Pointer()->get();
37 63
64 IPC::ChannelHandle channel_handle;
38 if (!sender->Send(new ChromeViewHostMsg_LaunchNaCl( 65 if (!sender->Send(new ChromeViewHostMsg_LaunchNaCl(
39 GURL(alleged_url), socket_count, &sockets))) 66 GURL(alleged_url), socket_count, &sockets,
67 &channel_handle)))
40 return PP_FALSE; 68 return PP_FALSE;
41 69
70 g_channel_handle_map.Get()[instance] = channel_handle;
dmichael (off chromium) 2012/06/21 18:18:16 optional: This will over-write the instance if it'
bbudge 2012/06/21 19:28:18 Next patch removes this global and passes the poin
71
42 CHECK(static_cast<int>(sockets.size()) == socket_count); 72 CHECK(static_cast<int>(sockets.size()) == socket_count);
43 for (int i = 0; i < socket_count; i++) { 73 for (int i = 0; i < socket_count; i++) {
44 static_cast<nacl::Handle*>(imc_handles)[i] = 74 static_cast<nacl::Handle*>(imc_handles)[i] =
45 nacl::ToNativeHandle(sockets[i]); 75 nacl::ToNativeHandle(sockets[i]);
46 } 76 }
47 77
48 return PP_TRUE; 78 return PP_TRUE;
49 } 79 }
50 80
81 class ProxyChannelDelegate
82 : public ppapi::proxy::ProxyChannel::Delegate {
83 public:
84 ProxyChannelDelegate();
85 virtual ~ProxyChannelDelegate();
86
87 // ProxyChannel::Delegate implementation.
88 virtual base::MessageLoopProxy* GetIPCMessageLoop() OVERRIDE;
89 virtual base::WaitableEvent* GetShutdownEvent() OVERRIDE;
90 virtual IPC::PlatformFileForTransit ShareHandleWithRemote(
91 base::PlatformFile handle,
92 const IPC::SyncChannel& channel,
93 bool should_close_source) OVERRIDE;
94 private:
95 base::WaitableEvent shutdown_event_;
96 };
97
98 ProxyChannelDelegate::ProxyChannelDelegate()
99 : shutdown_event_(true, false) {
100 }
101
102 ProxyChannelDelegate::~ProxyChannelDelegate() {
103 }
104
105 base::MessageLoopProxy* ProxyChannelDelegate::GetIPCMessageLoop() {
106 return RenderThread::Get()->GetIOMessageLoopProxy().get();
107 }
108
109 base::WaitableEvent* ProxyChannelDelegate::GetShutdownEvent() {
110 return &shutdown_event_;
111 }
112
113 IPC::PlatformFileForTransit ProxyChannelDelegate::ShareHandleWithRemote(
114 base::PlatformFile handle,
115 const IPC::SyncChannel& channel,
116 bool should_close_source) {
117 return content::BrokerGetFileHandleForProcess(handle, channel.peer_pid(),
118 should_close_source);
119 }
120
121 // Stubbed out SyncMessageStatusReceiver, required by HostDispatcher.
122 // TODO(bbudge) Implement something like PepperHungPluginFilter.
123 class SyncMessageStatusReceiver
124 : public ppapi::proxy::HostDispatcher::SyncMessageStatusReceiver {
125 public:
126 SyncMessageStatusReceiver() {}
127
128 // SyncMessageStatusReceiver implementation.
129 virtual void BeginBlockOnSyncMessage() OVERRIDE {}
130 virtual void EndBlockOnSyncMessage() OVERRIDE {}
131
132 private:
133 virtual ~SyncMessageStatusReceiver() {}
134 };
135
136 class OutOfProcessProxy : public PluginDelegate::OutOfProcessProxy {
137 public:
138 OutOfProcessProxy() {}
139 virtual ~OutOfProcessProxy() {}
140
141 bool Init(const IPC::ChannelHandle& channel_handle,
142 PP_Module pp_module,
143 PP_GetInterface_Func local_get_interface,
144 const ppapi::Preferences& preferences,
145 SyncMessageStatusReceiver* status_receiver) {
146 if (channel_handle.name.empty())
147 return false;
148
149 #if defined(OS_POSIX)
150 DCHECK_NE(-1, channel_handle.socket.fd);
151 if (channel_handle.socket.fd == -1)
152 return false;
153 #endif
154
155 dispatcher_delegate_.reset(new ProxyChannelDelegate);
156 dispatcher_.reset(new ppapi::proxy::HostDispatcher(
157 pp_module, local_get_interface, status_receiver));
158
159 if (!dispatcher_->InitHostWithChannel(dispatcher_delegate_.get(),
160 channel_handle,
161 true, // Client.
162 preferences)) {
163 dispatcher_.reset();
164 dispatcher_delegate_.reset();
165 return false;
166 }
167
168 return true;
169 }
170
171 // OutOfProcessProxy implementation.
172 virtual const void* GetProxiedInterface(const char* name) OVERRIDE {
173 return dispatcher_->GetProxiedInterface(name);
174 }
175 virtual void AddInstance(PP_Instance instance) OVERRIDE {
176 ppapi::proxy::HostDispatcher::SetForInstance(instance, dispatcher_.get());
177 }
178 virtual void RemoveInstance(PP_Instance instance) OVERRIDE {
179 ppapi::proxy::HostDispatcher::RemoveForInstance(instance);
180 }
181
182 private:
183 scoped_ptr<ppapi::proxy::HostDispatcher> dispatcher_;
184 scoped_ptr<ppapi::proxy::ProxyChannel::Delegate> dispatcher_delegate_;
185 };
186
51 PP_Bool StartPpapiProxy(PP_Instance instance) { 187 PP_Bool StartPpapiProxy(PP_Instance instance) {
188 if (CommandLine::ForCurrentProcess()->HasSwitch(
189 switches::kEnableNaClIPCProxy)) {
190 ChannelHandleMap& map = g_channel_handle_map.Get();
191 ChannelHandleMap::iterator it = map.find(instance);
192 if (it == map.end())
193 return PP_FALSE;
194 IPC::ChannelHandle channel_handle = it->second;
195 map.erase(it);
196 if (channel_handle.name.empty())
197 return PP_FALSE;
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
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
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698