OLD | NEW |
| (Empty) |
1 // Copyright (c) 2011 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 "chrome/common/pepper_plugin_registry.h" | |
6 | |
7 #include "base/command_line.h" | |
8 #include "base/file_util.h" | |
9 #include "base/native_library.h" | |
10 #include "base/path_service.h" | |
11 #include "base/string_split.h" | |
12 #include "base/string_util.h" | |
13 #include "base/utf_string_conversions.h" | |
14 #include "chrome/common/chrome_paths.h" | |
15 #include "chrome/common/chrome_switches.h" | |
16 #include "content/common/child_process.h" | |
17 #include "content/common/content_switches.h" | |
18 #include "remoting/client/plugin/pepper_entrypoints.h" | |
19 #include "webkit/plugins/npapi/plugin_list.h" | |
20 | |
21 namespace { | |
22 | |
23 const char* kPDFPluginName = "Chrome PDF Viewer"; | |
24 const char* kPDFPluginMimeType = "application/pdf"; | |
25 const char* kPDFPluginExtension = "pdf"; | |
26 const char* kPDFPluginDescription = "Portable Document Format"; | |
27 | |
28 const char* kNaClPluginName = "Chrome NaCl"; | |
29 const char* kNaClPluginMimeType = "application/x-nacl"; | |
30 const char* kNaClPluginExtension = "nexe"; | |
31 const char* kNaClPluginDescription = "Native Client Executable"; | |
32 | |
33 #if defined(ENABLE_REMOTING) | |
34 const char* kRemotingPluginMimeType = "pepper-application/x-chromoting"; | |
35 #endif | |
36 | |
37 const char* kFlashPluginName = "Shockwave Flash"; | |
38 const char* kFlashPluginSwfMimeType = "application/x-shockwave-flash"; | |
39 const char* kFlashPluginSwfExtension = "swf"; | |
40 const char* kFlashPluginSwfDescription = "Shockwave Flash"; | |
41 const char* kFlashPluginSplMimeType = "application/futuresplash"; | |
42 const char* kFlashPluginSplExtension = "spl"; | |
43 const char* kFlashPluginSplDescription = "FutureSplash Player"; | |
44 | |
45 // Appends the known built-in plugins to the given vector. Some built-in | |
46 // plugins are "internal" which means they are compiled into the Chrome binary, | |
47 // and some are extra shared libraries distributed with the browser (these are | |
48 // not marked internal, aside from being automatically registered, they're just | |
49 // regular plugins). | |
50 void ComputeBuiltInPlugins(std::vector<PepperPluginInfo>* plugins) { | |
51 // PDF. | |
52 // | |
53 // Once we're sandboxed, we can't know if the PDF plugin is available or not; | |
54 // but (on Linux) this function is always called once before we're sandboxed. | |
55 // So the first time through test if the file is available and then skip the | |
56 // check on subsequent calls if yes. | |
57 static bool skip_pdf_file_check = false; | |
58 FilePath path; | |
59 if (PathService::Get(chrome::FILE_PDF_PLUGIN, &path)) { | |
60 if (skip_pdf_file_check || file_util::PathExists(path)) { | |
61 PepperPluginInfo pdf; | |
62 pdf.path = path; | |
63 pdf.name = kPDFPluginName; | |
64 webkit::npapi::WebPluginMimeType pdf_mime_type(kPDFPluginMimeType, | |
65 kPDFPluginExtension, | |
66 kPDFPluginDescription); | |
67 pdf.mime_types.push_back(pdf_mime_type); | |
68 plugins->push_back(pdf); | |
69 | |
70 skip_pdf_file_check = true; | |
71 } | |
72 } | |
73 | |
74 // Handle the Native Client plugin just like the PDF plugin. | |
75 static bool skip_nacl_file_check = false; | |
76 if (PathService::Get(chrome::FILE_NACL_PLUGIN, &path)) { | |
77 if (skip_nacl_file_check || file_util::PathExists(path)) { | |
78 PepperPluginInfo nacl; | |
79 nacl.path = path; | |
80 nacl.name = kNaClPluginName; | |
81 webkit::npapi::WebPluginMimeType nacl_mime_type(kNaClPluginMimeType, | |
82 kNaClPluginExtension, | |
83 kNaClPluginDescription); | |
84 nacl.mime_types.push_back(nacl_mime_type); | |
85 plugins->push_back(nacl); | |
86 | |
87 skip_nacl_file_check = true; | |
88 } | |
89 } | |
90 | |
91 // Remoting. | |
92 #if defined(ENABLE_REMOTING) | |
93 if (CommandLine::ForCurrentProcess()->HasSwitch( | |
94 switches::kEnableRemoting)) { | |
95 PepperPluginInfo info; | |
96 info.is_internal = true; | |
97 info.path = FilePath(FILE_PATH_LITERAL("internal-chromoting")); | |
98 webkit::npapi::WebPluginMimeType remoting_mime_type(kRemotingPluginMimeType, | |
99 std::string(), | |
100 std::string()); | |
101 info.mime_types.push_back(remoting_mime_type); | |
102 info.internal_entry_points.get_interface = remoting::PPP_GetInterface; | |
103 info.internal_entry_points.initialize_module = | |
104 remoting::PPP_InitializeModule; | |
105 info.internal_entry_points.shutdown_module = remoting::PPP_ShutdownModule; | |
106 | |
107 plugins->push_back(info); | |
108 } | |
109 #endif | |
110 } | |
111 | |
112 // Appends any plugins from the command line to the given vector. | |
113 void ComputePluginsFromCommandLine(std::vector<PepperPluginInfo>* plugins) { | |
114 // Flash being out of process is handled separately than general plugins | |
115 // for testing purposes. | |
116 bool out_of_process = | |
117 CommandLine::ForCurrentProcess()->HasSwitch(switches::kPpapiOutOfProcess); | |
118 bool flash_out_of_process = !CommandLine::ForCurrentProcess()->HasSwitch( | |
119 switches::kPpapiFlashInProcess); | |
120 | |
121 // Handle any Pepper Flash first. | |
122 const CommandLine::StringType flash_path = | |
123 CommandLine::ForCurrentProcess()->GetSwitchValueNative( | |
124 switches::kPpapiFlashPath); | |
125 if (!flash_path.empty()) { | |
126 PepperPluginInfo plugin; | |
127 plugin.is_out_of_process = flash_out_of_process; | |
128 plugin.path = FilePath(flash_path); | |
129 plugin.name = kFlashPluginName; | |
130 | |
131 const std::string flash_version = | |
132 CommandLine::ForCurrentProcess()->GetSwitchValueASCII( | |
133 switches::kPpapiFlashVersion); | |
134 std::vector<std::string> flash_version_numbers; | |
135 base::SplitString(flash_version, '.', &flash_version_numbers); | |
136 if (flash_version_numbers.size() < 1) | |
137 flash_version_numbers.push_back("10"); | |
138 // |SplitString()| puts in an empty string given an empty string. :( | |
139 else if (flash_version_numbers[0].empty()) | |
140 flash_version_numbers[0] = "10"; | |
141 if (flash_version_numbers.size() < 2) | |
142 flash_version_numbers.push_back("2"); | |
143 if (flash_version_numbers.size() < 3) | |
144 flash_version_numbers.push_back("999"); | |
145 if (flash_version_numbers.size() < 4) | |
146 flash_version_numbers.push_back("999"); | |
147 // E.g., "Shockwave Flash 10.2 r154": | |
148 plugin.description = plugin.name + " " + flash_version_numbers[0] + "." + | |
149 flash_version_numbers[1] + " r" + flash_version_numbers[2]; | |
150 plugin.version = JoinString(flash_version_numbers, '.'); | |
151 webkit::npapi::WebPluginMimeType swf_mime_type(kFlashPluginSwfMimeType, | |
152 kFlashPluginSwfExtension, | |
153 kFlashPluginSwfDescription); | |
154 plugin.mime_types.push_back(swf_mime_type); | |
155 webkit::npapi::WebPluginMimeType spl_mime_type(kFlashPluginSplMimeType, | |
156 kFlashPluginSplExtension, | |
157 kFlashPluginSplDescription); | |
158 plugin.mime_types.push_back(spl_mime_type); | |
159 plugins->push_back(plugin); | |
160 } | |
161 | |
162 // Handle other plugins. | |
163 const std::string value = | |
164 CommandLine::ForCurrentProcess()->GetSwitchValueASCII( | |
165 switches::kRegisterPepperPlugins); | |
166 if (value.empty()) | |
167 return; | |
168 | |
169 // FORMAT: | |
170 // command-line = <plugin-entry> + *( LWS + "," + LWS + <plugin-entry> ) | |
171 // plugin-entry = | |
172 // <file-path> + | |
173 // ["#" + <name> + ["#" + <description> + ["#" + <version>]]] + | |
174 // *1( LWS + ";" + LWS + <mime-type> ) | |
175 | |
176 std::vector<std::string> modules; | |
177 base::SplitString(value, ',', &modules); | |
178 for (size_t i = 0; i < modules.size(); ++i) { | |
179 std::vector<std::string> parts; | |
180 base::SplitString(modules[i], ';', &parts); | |
181 if (parts.size() < 2) { | |
182 DLOG(ERROR) << "Required mime-type not found"; | |
183 continue; | |
184 } | |
185 | |
186 std::vector<std::string> name_parts; | |
187 base::SplitString(parts[0], '#', &name_parts); | |
188 | |
189 PepperPluginInfo plugin; | |
190 plugin.is_out_of_process = out_of_process; | |
191 #if defined(OS_WIN) | |
192 // This means we can't provide plugins from non-ASCII paths, but | |
193 // since this switch is only for development I don't think that's | |
194 // too awful. | |
195 plugin.path = FilePath(ASCIIToUTF16(name_parts[0])); | |
196 #else | |
197 plugin.path = FilePath(name_parts[0]); | |
198 #endif | |
199 if (name_parts.size() > 1) | |
200 plugin.name = name_parts[1]; | |
201 if (name_parts.size() > 2) | |
202 plugin.description = name_parts[2]; | |
203 if (name_parts.size() > 3) | |
204 plugin.version = name_parts[3]; | |
205 for (size_t j = 1; j < parts.size(); ++j) { | |
206 webkit::npapi::WebPluginMimeType mime_type(parts[j], | |
207 std::string(), | |
208 plugin.description); | |
209 plugin.mime_types.push_back(mime_type); | |
210 } | |
211 | |
212 plugins->push_back(plugin); | |
213 } | |
214 } | |
215 | |
216 } // namespace | |
217 | |
218 const char* PepperPluginRegistry::kPDFPluginName = ::kPDFPluginName; | |
219 | |
220 webkit::npapi::WebPluginInfo PepperPluginInfo::ToWebPluginInfo() const { | |
221 webkit::npapi::WebPluginInfo info; | |
222 | |
223 info.name = name.empty() ? path.BaseName().LossyDisplayName() : | |
224 ASCIIToUTF16(name); | |
225 info.path = path; | |
226 info.version = ASCIIToUTF16(version); | |
227 info.desc = ASCIIToUTF16(description); | |
228 info.mime_types = mime_types; | |
229 | |
230 webkit::npapi::WebPluginInfo::EnabledStates enabled_state = | |
231 webkit::npapi::WebPluginInfo::USER_ENABLED_POLICY_UNMANAGED; | |
232 | |
233 // Enable the Native Client Plugin based on the command line. | |
234 // TODO(abarth): This is the wrong place to do this work! | |
235 if (name == kNaClPluginName) { | |
236 bool nacl_enabled = | |
237 CommandLine::ForCurrentProcess()->HasSwitch(switches::kEnableNaCl); | |
238 enabled_state = nacl_enabled ? | |
239 webkit::npapi::WebPluginInfo::USER_ENABLED_POLICY_UNMANAGED : | |
240 webkit::npapi::WebPluginInfo::USER_DISABLED_POLICY_UNMANAGED; | |
241 } | |
242 info.enabled = enabled_state; | |
243 return info; | |
244 } | |
245 | |
246 PepperPluginInfo::PepperPluginInfo() | |
247 : is_internal(false), | |
248 is_out_of_process(false) { | |
249 } | |
250 | |
251 PepperPluginInfo::~PepperPluginInfo() { | |
252 } | |
253 | |
254 NaClModuleInfo::NaClModuleInfo() { | |
255 } | |
256 | |
257 NaClModuleInfo::~NaClModuleInfo() { | |
258 } | |
259 | |
260 // static | |
261 PepperPluginRegistry* PepperPluginRegistry::GetInstance() { | |
262 static PepperPluginRegistry* registry = NULL; | |
263 // This object leaks. It is a temporary hack to work around a crash. | |
264 // http://code.google.com/p/chromium/issues/detail?id=63234 | |
265 if (!registry) | |
266 registry = new PepperPluginRegistry; | |
267 return registry; | |
268 } | |
269 | |
270 // static | |
271 void PepperPluginRegistry::ComputeList(std::vector<PepperPluginInfo>* plugins) { | |
272 ComputeBuiltInPlugins(plugins); | |
273 ComputePluginsFromCommandLine(plugins); | |
274 } | |
275 | |
276 // static | |
277 void PepperPluginRegistry::PreloadModules() { | |
278 std::vector<PepperPluginInfo> plugins; | |
279 ComputeList(&plugins); | |
280 for (size_t i = 0; i < plugins.size(); ++i) { | |
281 if (!plugins[i].is_internal) { | |
282 base::NativeLibrary library = base::LoadNativeLibrary(plugins[i].path); | |
283 LOG_IF(WARNING, !library) << "Unable to load plugin " | |
284 << plugins[i].path.value(); | |
285 } | |
286 } | |
287 } | |
288 | |
289 const PepperPluginInfo* PepperPluginRegistry::GetInfoForPlugin( | |
290 const FilePath& path) const { | |
291 for (size_t i = 0; i < plugin_list_.size(); ++i) { | |
292 if (path == plugin_list_[i].path) | |
293 return &plugin_list_[i]; | |
294 } | |
295 return NULL; | |
296 } | |
297 | |
298 webkit::ppapi::PluginModule* PepperPluginRegistry::GetLiveModule( | |
299 const FilePath& path) { | |
300 NonOwningModuleMap::iterator it = live_modules_.find(path); | |
301 if (it == live_modules_.end()) | |
302 return NULL; | |
303 return it->second; | |
304 } | |
305 | |
306 void PepperPluginRegistry::AddLiveModule(const FilePath& path, | |
307 webkit::ppapi::PluginModule* module) { | |
308 DCHECK(live_modules_.find(path) == live_modules_.end()); | |
309 live_modules_[path] = module; | |
310 } | |
311 | |
312 void PepperPluginRegistry::PluginModuleDead( | |
313 webkit::ppapi::PluginModule* dead_module) { | |
314 // DANGER: Don't dereference the dead_module pointer! It may be in the | |
315 // process of being deleted. | |
316 | |
317 // Modules aren't destroyed very often and there are normally at most a | |
318 // couple of them. So for now we just do a brute-force search. | |
319 for (NonOwningModuleMap::iterator i = live_modules_.begin(); | |
320 i != live_modules_.end(); ++i) { | |
321 if (i->second == dead_module) { | |
322 live_modules_.erase(i); | |
323 return; | |
324 } | |
325 } | |
326 NOTREACHED(); // Should have always found the module above. | |
327 } | |
328 | |
329 PepperPluginRegistry::~PepperPluginRegistry() { | |
330 // Explicitly clear all preloaded modules first. This will cause callbacks | |
331 // to erase these modules from the live_modules_ list, and we don't want | |
332 // that to happen implicitly out-of-order. | |
333 preloaded_modules_.clear(); | |
334 | |
335 DCHECK(live_modules_.empty()); | |
336 } | |
337 | |
338 PepperPluginRegistry::PepperPluginRegistry() { | |
339 ComputeList(&plugin_list_); | |
340 | |
341 // Note that in each case, AddLiveModule must be called before completing | |
342 // initialization. If we bail out (in the continue clauses) before saving | |
343 // the initialized module, it will still try to unregister itself in its | |
344 // destructor. | |
345 for (size_t i = 0; i < plugin_list_.size(); i++) { | |
346 const PepperPluginInfo& current = plugin_list_[i]; | |
347 if (current.is_out_of_process) | |
348 continue; // Out of process plugins need no special pre-initialization. | |
349 | |
350 scoped_refptr<webkit::ppapi::PluginModule> module = | |
351 new webkit::ppapi::PluginModule(current.name, current.path, this); | |
352 AddLiveModule(current.path, module); | |
353 if (current.is_internal) { | |
354 if (!module->InitAsInternalPlugin(current.internal_entry_points)) { | |
355 DLOG(ERROR) << "Failed to load pepper module: " << current.path.value(); | |
356 continue; | |
357 } | |
358 } else { | |
359 // Preload all external plugins we're not running out of process. | |
360 if (!module->InitAsLibrary(current.path)) { | |
361 DLOG(ERROR) << "Failed to load pepper module: " << current.path.value(); | |
362 continue; | |
363 } | |
364 } | |
365 preloaded_modules_[current.path] = module; | |
366 } | |
367 } | |
368 | |
369 MessageLoop* PepperPluginRegistry::GetIPCMessageLoop() { | |
370 // This is called only in the renderer so we know we have a child process. | |
371 DCHECK(ChildProcess::current()) << "Must be in the renderer."; | |
372 return ChildProcess::current()->io_message_loop(); | |
373 } | |
374 | |
375 base::WaitableEvent* PepperPluginRegistry::GetShutdownEvent() { | |
376 DCHECK(ChildProcess::current()) << "Must be in the renderer."; | |
377 return ChildProcess::current()->GetShutDownEvent(); | |
378 } | |
379 | |
380 std::set<PP_Instance>* PepperPluginRegistry::GetGloballySeenInstanceIDSet() { | |
381 // This function is not needed on the host side of the proxy. | |
382 return NULL; | |
383 } | |
384 | |
385 void PepperPluginRegistry::RegisterNaClModule(const GURL& url, | |
386 const std::string& mime_type) { | |
387 NaClModuleInfo info; | |
388 info.url = url; | |
389 info.mime_type = mime_type; | |
390 | |
391 DCHECK(FindNaClModule(url) == nacl_module_list_.end()); | |
392 nacl_module_list_.push_front(info); | |
393 } | |
394 | |
395 void PepperPluginRegistry::UnregisterNaClModule(const GURL& url) { | |
396 NaClModuleInfoList::iterator iter = FindNaClModule(url); | |
397 DCHECK(iter != nacl_module_list_.end()); | |
398 nacl_module_list_.erase(iter); | |
399 } | |
400 | |
401 void PepperPluginRegistry::UpdatePluginListWithNaClModules() { | |
402 FilePath path; | |
403 PathService::Get(chrome::FILE_NACL_PLUGIN, &path); | |
404 | |
405 webkit::npapi::PluginList::Singleton()->UnregisterInternalPlugin(path); | |
406 | |
407 const PepperPluginInfo* pepper_info = GetInfoForPlugin(path); | |
408 webkit::npapi::WebPluginInfo info = pepper_info->ToWebPluginInfo(); | |
409 | |
410 DCHECK(nacl_module_list_.size() <= 1); | |
411 for (NaClModuleInfoList::iterator iter = nacl_module_list_.begin(); | |
412 iter != nacl_module_list_.end(); ++iter) { | |
413 webkit::npapi::WebPluginMimeType mime_type_info; | |
414 mime_type_info.mime_type = iter->mime_type; | |
415 mime_type_info.additional_param_names.push_back(UTF8ToUTF16("nacl")); | |
416 mime_type_info.additional_param_values.push_back( | |
417 UTF8ToUTF16(iter->url.spec())); | |
418 info.mime_types.push_back(mime_type_info); | |
419 } | |
420 | |
421 webkit::npapi::PluginList::Singleton()->RefreshPlugins(); | |
422 webkit::npapi::PluginList::Singleton()->RegisterInternalPlugin(info); | |
423 } | |
424 | |
425 PepperPluginRegistry::NaClModuleInfoList::iterator | |
426 PepperPluginRegistry::FindNaClModule(const GURL& url) { | |
427 for (NaClModuleInfoList::iterator iter = nacl_module_list_.begin(); | |
428 iter != nacl_module_list_.end(); ++iter) { | |
429 if (iter->url == url) | |
430 return iter; | |
431 } | |
432 return nacl_module_list_.end(); | |
433 } | |
OLD | NEW |