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

Side by Side Diff: ppapi/proxy/plugin_main_nacl.cc

Issue 140573003: Connect PPAPI IPC channels for non-SFI mode. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix build error Created 6 years, 10 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
« no previous file with comments | « ppapi/proxy/plugin_main_irt.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #include <unistd.h>
6
7 #include <map>
8 #include <set>
9
10 #include "build/build_config.h"
11 // Need to include this before most other files because it defines
12 // IPC_MESSAGE_LOG_ENABLED. We need to use it to define
13 // IPC_MESSAGE_MACROS_LOG_ENABLED so ppapi_messages.h will generate the
14 // ViewMsgLog et al. functions.
15
16 #include "base/command_line.h"
17 #include "base/memory/scoped_ptr.h"
18 #include "base/message_loop/message_loop.h"
19 #include "base/strings/string_number_conversions.h"
20 #include "base/synchronization/waitable_event.h"
21 #include "base/threading/thread.h"
22 #include "components/tracing/child_trace_message_filter.h"
23 #include "ipc/ipc_channel_handle.h"
24 #include "ipc/ipc_logging.h"
25 #include "ipc/ipc_message.h"
26 #include "native_client/src/public/chrome_main.h"
27 #include "native_client/src/shared/srpc/nacl_srpc.h"
28 #include "ppapi/c/ppp.h"
29 #include "ppapi/c/ppp_instance.h"
30 #include "ppapi/native_client/src/shared/ppapi_proxy/ppruntime.h"
31 #include "ppapi/proxy/plugin_dispatcher.h"
32 #include "ppapi/proxy/plugin_globals.h"
33 #include "ppapi/proxy/plugin_message_filter.h"
34 #include "ppapi/proxy/plugin_proxy_delegate.h"
35 #include "ppapi/proxy/resource_reply_thread_registrar.h"
36 #include "ppapi/shared_impl/ppapi_switches.h"
37 #include "ppapi/shared_impl/ppb_audio_shared.h"
38
39 #if defined(IPC_MESSAGE_LOG_ENABLED)
40 #include "base/containers/hash_tables.h"
41
42 LogFunctionMap g_log_function_mapping;
43
44 #define IPC_MESSAGE_MACROS_LOG_ENABLED
45 #define IPC_LOG_TABLE_ADD_ENTRY(msg_id, logger) \
46 g_log_function_mapping[msg_id] = logger
47
48 #endif
49 #include "ppapi/proxy/ppapi_messages.h"
50
51 using ppapi::proxy::PluginDispatcher;
52 using ppapi::proxy::PluginGlobals;
53 using ppapi::proxy::PluginProxyDelegate;
54 using ppapi::proxy::ProxyChannel;
55 using ppapi::proxy::SerializedHandle;
56
57 namespace {
58
59 // This class manages communication between the plugin and the browser, and
60 // manages the PluginDispatcher instances for communication between the plugin
61 // and the renderer.
62 class PpapiDispatcher : public PluginDispatcher::PluginDelegate,
63 public PluginProxyDelegate,
64 public IPC::Listener,
65 public IPC::Sender {
66 public:
67 explicit PpapiDispatcher(scoped_refptr<base::MessageLoopProxy> io_loop);
68
69 // PluginDispatcher::PluginDelegate implementation.
70 virtual base::MessageLoopProxy* GetIPCMessageLoop() OVERRIDE;
71 virtual base::WaitableEvent* GetShutdownEvent() OVERRIDE;
72 virtual IPC::PlatformFileForTransit ShareHandleWithRemote(
73 base::PlatformFile handle,
74 base::ProcessId peer_pid,
75 bool should_close_source) OVERRIDE;
76 virtual std::set<PP_Instance>* GetGloballySeenInstanceIDSet() OVERRIDE;
77 virtual uint32 Register(PluginDispatcher* plugin_dispatcher) OVERRIDE;
78 virtual void Unregister(uint32 plugin_dispatcher_id) OVERRIDE;
79
80 // PluginProxyDelegate implementation.
81 virtual IPC::Sender* GetBrowserSender() OVERRIDE;
82 virtual std::string GetUILanguage() OVERRIDE;
83 virtual void PreCacheFont(const void* logfontw) OVERRIDE;
84 virtual void SetActiveURL(const std::string& url) OVERRIDE;
85 virtual PP_Resource CreateBrowserFont(
86 ppapi::proxy::Connection connection,
87 PP_Instance instance,
88 const PP_BrowserFont_Trusted_Description& desc,
89 const ppapi::Preferences& prefs) OVERRIDE;
90
91 // IPC::Listener implementation.
92 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE;
93 virtual void OnChannelError() OVERRIDE;
94
95 // IPC::Sender implementation
96 virtual bool Send(IPC::Message* message) OVERRIDE;
97
98 private:
99 void OnMsgInitializeNaClDispatcher(const ppapi::PpapiNaClPluginArgs& args);
100 void OnPluginDispatcherMessageReceived(const IPC::Message& msg);
101
102 void SetPpapiKeepAliveThrottleFromCommandLine();
103
104 std::set<PP_Instance> instances_;
105 std::map<uint32, PluginDispatcher*> plugin_dispatchers_;
106 uint32 next_plugin_dispatcher_id_;
107 scoped_refptr<base::MessageLoopProxy> message_loop_;
108 base::WaitableEvent shutdown_event_;
109 scoped_ptr<IPC::SyncChannel> channel_;
110 };
111
112 PpapiDispatcher::PpapiDispatcher(scoped_refptr<base::MessageLoopProxy> io_loop)
113 : next_plugin_dispatcher_id_(0),
114 message_loop_(io_loop),
115 shutdown_event_(true, false) {
116 // The first FD (based on NACL_CHROME_DESC_BASE) is the IPC channel to the
117 // browser.
118 IPC::ChannelHandle channel_handle(
119 "NaCl IPC", base::FileDescriptor(NACL_CHROME_DESC_BASE, false));
120
121 // Delay initializing the SyncChannel until after we add filters. This
122 // ensures that the filters won't miss any messages received by
123 // the channel.
124 channel_.reset(new IPC::SyncChannel(
125 this, GetIPCMessageLoop(), GetShutdownEvent()));
126 channel_->AddFilter(new ppapi::proxy::PluginMessageFilter(
127 NULL, PluginGlobals::Get()->resource_reply_thread_registrar()));
128 channel_->AddFilter(
129 new tracing::ChildTraceMessageFilter(message_loop_.get()));
130 channel_->Init(channel_handle, IPC::Channel::MODE_SERVER, true);
131 }
132
133 base::MessageLoopProxy* PpapiDispatcher::GetIPCMessageLoop() {
134 return message_loop_.get();
135 }
136
137 base::WaitableEvent* PpapiDispatcher::GetShutdownEvent() {
138 return &shutdown_event_;
139 }
140
141 IPC::PlatformFileForTransit PpapiDispatcher::ShareHandleWithRemote(
142 base::PlatformFile handle,
143 base::ProcessId peer_pid,
144 bool should_close_source) {
145 return IPC::InvalidPlatformFileForTransit();
146 }
147
148 std::set<PP_Instance>* PpapiDispatcher::GetGloballySeenInstanceIDSet() {
149 return &instances_;
150 }
151
152 uint32 PpapiDispatcher::Register(PluginDispatcher* plugin_dispatcher) {
153 if (!plugin_dispatcher ||
154 plugin_dispatchers_.size() >= std::numeric_limits<uint32>::max()) {
155 return 0;
156 }
157
158 uint32 id = 0;
159 do {
160 // Although it is unlikely, make sure that we won't cause any trouble
161 // when the counter overflows.
162 id = next_plugin_dispatcher_id_++;
163 } while (id == 0 ||
164 plugin_dispatchers_.find(id) != plugin_dispatchers_.end());
165 plugin_dispatchers_[id] = plugin_dispatcher;
166 return id;
167 }
168
169 void PpapiDispatcher::Unregister(uint32 plugin_dispatcher_id) {
170 plugin_dispatchers_.erase(plugin_dispatcher_id);
171 }
172
173 IPC::Sender* PpapiDispatcher::GetBrowserSender() {
174 return this;
175 }
176
177 std::string PpapiDispatcher::GetUILanguage() {
178 NOTIMPLEMENTED();
179 return std::string();
180 }
181
182 void PpapiDispatcher::PreCacheFont(const void* logfontw) {
183 NOTIMPLEMENTED();
184 }
185
186 void PpapiDispatcher::SetActiveURL(const std::string& url) {
187 NOTIMPLEMENTED();
188 }
189
190 PP_Resource PpapiDispatcher::CreateBrowserFont(
191 ppapi::proxy::Connection connection,
192 PP_Instance instance,
193 const PP_BrowserFont_Trusted_Description& desc,
194 const ppapi::Preferences& prefs) {
195 NOTIMPLEMENTED();
196 return 0;
197 }
198
199 bool PpapiDispatcher::OnMessageReceived(const IPC::Message& msg) {
200 IPC_BEGIN_MESSAGE_MAP(PpapiDispatcher, msg)
201 IPC_MESSAGE_HANDLER(PpapiMsg_InitializeNaClDispatcher,
202 OnMsgInitializeNaClDispatcher)
203 // All other messages are simply forwarded to a PluginDispatcher.
204 IPC_MESSAGE_UNHANDLED(OnPluginDispatcherMessageReceived(msg))
205 IPC_END_MESSAGE_MAP()
206 return true;
207 }
208
209 void PpapiDispatcher::OnChannelError() {
210 exit(1);
211 }
212
213 bool PpapiDispatcher::Send(IPC::Message* msg) {
214 return channel_->Send(msg);
215 }
216
217 void PpapiDispatcher::OnMsgInitializeNaClDispatcher(
218 const ppapi::PpapiNaClPluginArgs& args) {
219 static bool command_line_and_logging_initialized = false;
220 if (command_line_and_logging_initialized) {
221 LOG(FATAL) << "InitializeNaClDispatcher must be called once per plugin.";
222 return;
223 }
224
225 command_line_and_logging_initialized = true;
226 CommandLine::Init(0, NULL);
227 for (size_t i = 0; i < args.switch_names.size(); ++i) {
228 DCHECK(i < args.switch_values.size());
229 CommandLine::ForCurrentProcess()->AppendSwitchASCII(
230 args.switch_names[i], args.switch_values[i]);
231 }
232 logging::LoggingSettings settings;
233 settings.logging_dest = logging::LOG_TO_SYSTEM_DEBUG_LOG;
234 logging::InitLogging(settings);
235 SetPpapiKeepAliveThrottleFromCommandLine();
236
237 // Tell the process-global GetInterface which interfaces it can return to the
238 // plugin.
239 ppapi::proxy::InterfaceList::SetProcessGlobalPermissions(
240 args.permissions);
241
242 int32_t error = ::PPP_InitializeModule(
243 0 /* module */,
244 &ppapi::proxy::PluginDispatcher::GetBrowserInterface);
245 if (error)
246 ::exit(error);
247
248 PluginDispatcher* dispatcher =
249 new PluginDispatcher(::PPP_GetInterface, args.permissions,
250 args.off_the_record);
251 // The channel handle's true name is not revealed here.
252 // The second FD (based on NACL_CHROME_DESC_BASE) is the IPC channel to the
253 // renderer.
254 IPC::ChannelHandle channel_handle(
255 "nacl", base::FileDescriptor(NACL_CHROME_DESC_BASE + 1, false));
256 if (!dispatcher->InitPluginWithChannel(this, base::kNullProcessId,
257 channel_handle, false)) {
258 delete dispatcher;
259 return;
260 }
261 // From here, the dispatcher will manage its own lifetime according to the
262 // lifetime of the attached channel.
263 }
264
265 void PpapiDispatcher::OnPluginDispatcherMessageReceived(
266 const IPC::Message& msg) {
267 // The first parameter should be a plugin dispatcher ID.
268 PickleIterator iter(msg);
269 uint32 id = 0;
270 if (!msg.ReadUInt32(&iter, &id)) {
271 NOTREACHED();
272 return;
273 }
274 std::map<uint32, ppapi::proxy::PluginDispatcher*>::iterator dispatcher =
275 plugin_dispatchers_.find(id);
276 if (dispatcher != plugin_dispatchers_.end())
277 dispatcher->second->OnMessageReceived(msg);
278 }
279
280 void PpapiDispatcher::SetPpapiKeepAliveThrottleFromCommandLine() {
281 unsigned keepalive_throttle_interval_milliseconds = 0;
282 if (base::StringToUint(
283 CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
284 switches::kPpapiKeepAliveThrottle),
285 &keepalive_throttle_interval_milliseconds)) {
286 ppapi::proxy::PluginGlobals::Get()->
287 set_keepalive_throttle_interval_milliseconds(
288 keepalive_throttle_interval_milliseconds);
289 }
290 }
291
292 } // namespace
293
294 void PpapiPluginRegisterThreadCreator(
295 const struct PP_ThreadFunctions* thread_functions) {
296 // Initialize all classes that need to create threads that call back into
297 // user code.
298 ppapi::PPB_Audio_Shared::SetThreadFunctions(thread_functions);
299 }
300
301 int PpapiPluginMain() {
302 // Though it isn't referenced here, we must instantiate an AtExitManager.
303 base::AtExitManager exit_manager;
304 base::MessageLoop loop;
305 IPC::Logging::set_log_function_map(&g_log_function_mapping);
306 ppapi::proxy::PluginGlobals plugin_globals;
307 base::Thread io_thread("Chrome_NaClIOThread");
308 base::Thread::Options options;
309 options.message_loop_type = base::MessageLoop::TYPE_IO;
310 io_thread.StartWithOptions(options);
311
312 // Start up the SRPC server on another thread. Otherwise, when it blocks
313 // on an RPC, the PPAPI proxy will hang. Do this before we initialize the
314 // module and start the PPAPI proxy so that the NaCl plugin can continue
315 // loading the app.
316 static struct NaClSrpcHandlerDesc srpc_methods[] = { { NULL, NULL } };
317 if (!NaClSrpcAcceptClientOnThread(srpc_methods)) {
318 return 1;
319 }
320
321 PpapiDispatcher ppapi_dispatcher(io_thread.message_loop_proxy());
322 plugin_globals.set_plugin_proxy_delegate(&ppapi_dispatcher);
323
324 loop.Run();
325
326 return 0;
327 }
OLDNEW
« no previous file with comments | « ppapi/proxy/plugin_main_irt.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698