OLD | NEW |
| (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 "webkit/plugins/npapi/plugin_list.h" | |
6 | |
7 #include <set> | |
8 | |
9 #include "base/basictypes.h" | |
10 #include "base/file_util.h" | |
11 #include "base/file_version_info.h" | |
12 #include "base/file_version_info_win.h" | |
13 #include "base/files/memory_mapped_file.h" | |
14 #include "base/memory/scoped_ptr.h" | |
15 #include "base/path_service.h" | |
16 #include "base/strings/string_number_conversions.h" | |
17 #include "base/strings/string_split.h" | |
18 #include "base/strings/string_util.h" | |
19 #include "base/win/pe_image.h" | |
20 #include "base/win/registry.h" | |
21 #include "base/win/scoped_handle.h" | |
22 #include "base/win/windows_version.h" | |
23 #include "webkit/plugins/npapi/plugin_constants_win.h" | |
24 | |
25 namespace { | |
26 | |
27 const char16 kRegistryApps[] = | |
28 L"Software\\Microsoft\\Windows\\CurrentVersion\\App Paths"; | |
29 const char16 kRegistryFirefox[] = L"firefox.exe"; | |
30 const char16 kRegistryAcrobat[] = L"Acrobat.exe"; | |
31 const char16 kRegistryAcrobatReader[] = L"AcroRd32.exe"; | |
32 const char16 kRegistryWindowsMedia[] = L"wmplayer.exe"; | |
33 const char16 kRegistryQuickTime[] = L"QuickTimePlayer.exe"; | |
34 const char16 kRegistryPath[] = L"Path"; | |
35 const char16 kRegistryFirefoxInstalled[] = | |
36 L"SOFTWARE\\Mozilla\\Mozilla Firefox"; | |
37 const char16 kRegistryJava[] = | |
38 L"Software\\JavaSoft\\Java Runtime Environment"; | |
39 const char16 kRegistryBrowserJavaVersion[] = L"BrowserJavaVersion"; | |
40 const char16 kRegistryCurrentJavaVersion[] = L"CurrentVersion"; | |
41 const char16 kRegistryJavaHome[] = L"JavaHome"; | |
42 const char16 kJavaDeploy1[] = L"npdeploytk.dll"; | |
43 const char16 kJavaDeploy2[] = L"npdeployjava1.dll"; | |
44 | |
45 base::FilePath AppendPluginsDir(const base::FilePath& path) { | |
46 return path.AppendASCII("plugins"); | |
47 } | |
48 | |
49 // Gets the directory where the application data and libraries exist. This | |
50 // may be a versioned subdirectory, or it may be the same directory as the | |
51 // GetExeDirectory(), depending on the embedder's implementation. | |
52 // Path is an output parameter to receive the path. | |
53 void GetAppDirectory(std::set<base::FilePath>* plugin_dirs) { | |
54 base::FilePath app_path; | |
55 if (!PathService::Get(base::DIR_MODULE, &app_path)) | |
56 return; | |
57 plugin_dirs->insert(AppendPluginsDir(app_path)); | |
58 } | |
59 | |
60 // Gets the directory where the launching executable resides on disk. | |
61 // Path is an output parameter to receive the path. | |
62 void GetExeDirectory(std::set<base::FilePath>* plugin_dirs) { | |
63 base::FilePath exe_path; | |
64 if (!PathService::Get(base::DIR_EXE, &exe_path)) | |
65 return; | |
66 plugin_dirs->insert(AppendPluginsDir(exe_path)); | |
67 } | |
68 | |
69 // Gets the installed path for a registered app. | |
70 bool GetInstalledPath(const char16* app, base::FilePath* out) { | |
71 base::string16 reg_path(kRegistryApps); | |
72 reg_path.append(L"\\"); | |
73 reg_path.append(app); | |
74 | |
75 base::win::RegKey hkcu_key(HKEY_CURRENT_USER, reg_path.c_str(), KEY_READ); | |
76 base::string16 path; | |
77 // As of Win7 AppPaths can also be registered in HKCU: http://goo.gl/UgFOf. | |
78 if (base::win::GetVersion() >= base::win::VERSION_WIN7 && | |
79 hkcu_key.ReadValue(kRegistryPath, &path) == ERROR_SUCCESS) { | |
80 *out = base::FilePath(path); | |
81 return true; | |
82 } else { | |
83 base::win::RegKey hklm_key(HKEY_LOCAL_MACHINE, reg_path.c_str(), KEY_READ); | |
84 if (hklm_key.ReadValue(kRegistryPath, &path) == ERROR_SUCCESS) { | |
85 *out = base::FilePath(path); | |
86 return true; | |
87 } | |
88 } | |
89 | |
90 return false; | |
91 } | |
92 | |
93 // Search the registry at the given path and detect plugin directories. | |
94 void GetPluginsInRegistryDirectory( | |
95 HKEY root_key, | |
96 const base::string16& registry_folder, | |
97 std::set<base::FilePath>* plugin_dirs) { | |
98 for (base::win::RegistryKeyIterator iter(root_key, registry_folder.c_str()); | |
99 iter.Valid(); ++iter) { | |
100 // Use the registry to gather plugin across the file system. | |
101 base::string16 reg_path = registry_folder; | |
102 reg_path.append(L"\\"); | |
103 reg_path.append(iter.Name()); | |
104 base::win::RegKey key(root_key, reg_path.c_str(), KEY_READ); | |
105 | |
106 base::string16 path; | |
107 if (key.ReadValue(kRegistryPath, &path) == ERROR_SUCCESS) | |
108 plugin_dirs->insert(base::FilePath(path)); | |
109 } | |
110 } | |
111 | |
112 // Enumerate through the registry key to find all installed FireFox paths. | |
113 // FireFox 3 beta and version 2 can coexist. See bug: 1025003 | |
114 void GetFirefoxInstalledPaths(std::vector<base::FilePath>* out) { | |
115 base::win::RegistryKeyIterator it(HKEY_LOCAL_MACHINE, | |
116 kRegistryFirefoxInstalled); | |
117 for (; it.Valid(); ++it) { | |
118 base::string16 full_path = base::string16(kRegistryFirefoxInstalled) + | |
119 L"\\" + it.Name() + L"\\Main"; | |
120 base::win::RegKey key(HKEY_LOCAL_MACHINE, full_path.c_str(), KEY_READ); | |
121 base::string16 install_dir; | |
122 if (key.ReadValue(L"Install Directory", &install_dir) != ERROR_SUCCESS) | |
123 continue; | |
124 out->push_back(base::FilePath(install_dir)); | |
125 } | |
126 } | |
127 | |
128 // Get plugin directory locations from the Firefox install path. This is kind | |
129 // of a kludge, but it helps us locate the flash player for users that | |
130 // already have it for firefox. Not having to download yet-another-plugin | |
131 // is a good thing. | |
132 void GetFirefoxDirectory(std::set<base::FilePath>* plugin_dirs) { | |
133 std::vector<base::FilePath> paths; | |
134 GetFirefoxInstalledPaths(&paths); | |
135 for (unsigned int i = 0; i < paths.size(); ++i) { | |
136 plugin_dirs->insert(AppendPluginsDir(paths[i])); | |
137 } | |
138 | |
139 base::FilePath firefox_app_data_plugin_path; | |
140 if (PathService::Get(base::DIR_APP_DATA, &firefox_app_data_plugin_path)) { | |
141 firefox_app_data_plugin_path = | |
142 firefox_app_data_plugin_path.AppendASCII("Mozilla"); | |
143 plugin_dirs->insert(AppendPluginsDir(firefox_app_data_plugin_path)); | |
144 } | |
145 } | |
146 | |
147 // Hardcoded logic to detect Acrobat plugins locations. | |
148 void GetAcrobatDirectory(std::set<base::FilePath>* plugin_dirs) { | |
149 base::FilePath path; | |
150 if (!GetInstalledPath(kRegistryAcrobatReader, &path) && | |
151 !GetInstalledPath(kRegistryAcrobat, &path)) { | |
152 return; | |
153 } | |
154 | |
155 plugin_dirs->insert(path.Append(L"Browser")); | |
156 } | |
157 | |
158 // Hardcoded logic to detect QuickTime plugin location. | |
159 void GetQuicktimeDirectory(std::set<base::FilePath>* plugin_dirs) { | |
160 base::FilePath path; | |
161 if (GetInstalledPath(kRegistryQuickTime, &path)) | |
162 plugin_dirs->insert(AppendPluginsDir(path)); | |
163 } | |
164 | |
165 // Hardcoded logic to detect Windows Media Player plugin location. | |
166 void GetWindowsMediaDirectory(std::set<base::FilePath>* plugin_dirs) { | |
167 base::FilePath path; | |
168 if (GetInstalledPath(kRegistryWindowsMedia, &path)) | |
169 plugin_dirs->insert(path); | |
170 } | |
171 | |
172 // Hardcoded logic to detect Java plugin location. | |
173 void GetJavaDirectory(std::set<base::FilePath>* plugin_dirs) { | |
174 // Load the new NPAPI Java plugin | |
175 // 1. Open the main JRE key under HKLM | |
176 base::win::RegKey java_key(HKEY_LOCAL_MACHINE, kRegistryJava, | |
177 KEY_QUERY_VALUE); | |
178 | |
179 // 2. Read the current Java version | |
180 base::string16 java_version; | |
181 if (java_key.ReadValue(kRegistryBrowserJavaVersion, &java_version) != | |
182 ERROR_SUCCESS) { | |
183 java_key.ReadValue(kRegistryCurrentJavaVersion, &java_version); | |
184 } | |
185 | |
186 if (!java_version.empty()) { | |
187 java_key.OpenKey(java_version.c_str(), KEY_QUERY_VALUE); | |
188 | |
189 // 3. Install path of the JRE binaries is specified in "JavaHome" | |
190 // value under the Java version key. | |
191 base::string16 java_plugin_directory; | |
192 if (java_key.ReadValue(kRegistryJavaHome, &java_plugin_directory) == | |
193 ERROR_SUCCESS) { | |
194 // 4. The new plugin resides under the 'bin/new_plugin' | |
195 // subdirectory. | |
196 DCHECK(!java_plugin_directory.empty()); | |
197 java_plugin_directory.append(L"\\bin\\new_plugin"); | |
198 | |
199 // 5. We don't know the exact name of the DLL but it's in the form | |
200 // NP*.dll so just invoke LoadPlugins on this path. | |
201 plugin_dirs->insert(base::FilePath(java_plugin_directory)); | |
202 } | |
203 } | |
204 } | |
205 | |
206 bool IsValid32BitImage(const base::FilePath& path) { | |
207 base::MemoryMappedFile plugin_image; | |
208 | |
209 if (!plugin_image.InitializeAsImageSection(path)) | |
210 return false; | |
211 | |
212 base::win::PEImage image(plugin_image.data()); | |
213 | |
214 PIMAGE_NT_HEADERS nt_headers = image.GetNTHeaders(); | |
215 return (nt_headers->FileHeader.Machine == IMAGE_FILE_MACHINE_I386); | |
216 } | |
217 | |
218 // Returns true if the given plugins share at least one mime type. This is used | |
219 // to differentiate newer versions of a plugin vs two plugins which happen to | |
220 // have the same filename. | |
221 bool HaveSharedMimeType(const webkit::WebPluginInfo& plugin1, | |
222 const webkit::WebPluginInfo& plugin2) { | |
223 for (size_t i = 0; i < plugin1.mime_types.size(); ++i) { | |
224 for (size_t j = 0; j < plugin2.mime_types.size(); ++j) { | |
225 if (plugin1.mime_types[i].mime_type == plugin2.mime_types[j].mime_type) | |
226 return true; | |
227 } | |
228 } | |
229 | |
230 return false; | |
231 } | |
232 | |
233 // Compares Windows style version strings (i.e. 1,2,3,4). Returns true if b's | |
234 // version is newer than a's, or false if it's equal or older. | |
235 bool IsNewerVersion(const base::string16& a, const base::string16& b) { | |
236 std::vector<base::string16> a_ver, b_ver; | |
237 base::SplitString(a, ',', &a_ver); | |
238 base::SplitString(b, ',', &b_ver); | |
239 if (a_ver.size() == 1 && b_ver.size() == 1) { | |
240 base::SplitString(a, '.', &a_ver); | |
241 base::SplitString(b, '.', &b_ver); | |
242 } | |
243 if (a_ver.size() != b_ver.size()) | |
244 return false; | |
245 for (size_t i = 0; i < a_ver.size(); i++) { | |
246 int cur_a, cur_b; | |
247 base::StringToInt(a_ver[i], &cur_a); | |
248 base::StringToInt(b_ver[i], &cur_b); | |
249 | |
250 if (cur_a > cur_b) | |
251 return false; | |
252 if (cur_a < cur_b) | |
253 return true; | |
254 } | |
255 return false; | |
256 } | |
257 | |
258 } // namespace | |
259 | |
260 namespace webkit { | |
261 namespace npapi { | |
262 | |
263 bool PluginList::ReadWebPluginInfo(const base::FilePath& filename, | |
264 webkit::WebPluginInfo* info) { | |
265 // On windows, the way we get the mime types for the library is | |
266 // to check the version information in the DLL itself. This | |
267 // will be a string of the format: <type1>|<type2>|<type3>|... | |
268 // For example: | |
269 // video/quicktime|audio/aiff|image/jpeg | |
270 scoped_ptr<FileVersionInfo> version_info( | |
271 FileVersionInfo::CreateFileVersionInfo(filename)); | |
272 if (!version_info) { | |
273 LOG_IF(ERROR, PluginList::DebugPluginLoading()) | |
274 << "Could not get version info for plugin " | |
275 << filename.value(); | |
276 return false; | |
277 } | |
278 | |
279 FileVersionInfoWin* version_info_win = | |
280 static_cast<FileVersionInfoWin*>(version_info.get()); | |
281 | |
282 info->name = version_info->product_name(); | |
283 info->desc = version_info->file_description(); | |
284 info->version = version_info->file_version(); | |
285 info->path = filename; | |
286 | |
287 // TODO(evan): Move the ParseMimeTypes code inline once Pepper is updated. | |
288 if (!PluginList::ParseMimeTypes( | |
289 UTF16ToASCII(version_info_win->GetStringValue(L"MIMEType")), | |
290 UTF16ToASCII(version_info_win->GetStringValue(L"FileExtents")), | |
291 version_info_win->GetStringValue(L"FileOpenName"), | |
292 &info->mime_types)) { | |
293 LOG_IF(ERROR, PluginList::DebugPluginLoading()) | |
294 << "Plugin " << info->name << " has bad MIME types, skipping"; | |
295 return false; | |
296 } | |
297 | |
298 return true; | |
299 } | |
300 | |
301 void PluginList::GetPluginDirectories( | |
302 std::vector<base::FilePath>* plugin_dirs) { | |
303 if (PluginList::plugins_discovery_disabled_) | |
304 return; | |
305 | |
306 // We use a set for uniqueness, which we require, over order, which we do not. | |
307 std::set<base::FilePath> dirs; | |
308 | |
309 // Load from the application-specific area | |
310 GetAppDirectory(&dirs); | |
311 | |
312 // Load from the executable area | |
313 GetExeDirectory(&dirs); | |
314 | |
315 // Load Java | |
316 GetJavaDirectory(&dirs); | |
317 | |
318 // Load firefox plugins too. This is mainly to try to locate | |
319 // a pre-installed Flash player. | |
320 GetFirefoxDirectory(&dirs); | |
321 | |
322 // Firefox hard-codes the paths of some popular plugins to ensure that | |
323 // the plugins are found. We are going to copy this as well. | |
324 GetAcrobatDirectory(&dirs); | |
325 GetQuicktimeDirectory(&dirs); | |
326 GetWindowsMediaDirectory(&dirs); | |
327 | |
328 for (std::set<base::FilePath>::iterator i = dirs.begin(); i != dirs.end(); ++i
) | |
329 plugin_dirs->push_back(*i); | |
330 } | |
331 | |
332 void PluginList::GetPluginsInDir( | |
333 const base::FilePath& path, std::vector<base::FilePath>* plugins) { | |
334 WIN32_FIND_DATA find_file_data; | |
335 HANDLE find_handle; | |
336 | |
337 base::string16 dir = path.value(); | |
338 // FindFirstFile requires that you specify a wildcard for directories. | |
339 dir.append(L"\\NP*.DLL"); | |
340 | |
341 find_handle = FindFirstFile(dir.c_str(), &find_file_data); | |
342 if (find_handle == INVALID_HANDLE_VALUE) | |
343 return; | |
344 | |
345 do { | |
346 if (!(find_file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) { | |
347 base::FilePath filename = path.Append(find_file_data.cFileName); | |
348 plugins->push_back(filename); | |
349 } | |
350 } while (FindNextFile(find_handle, &find_file_data) != 0); | |
351 | |
352 DCHECK(GetLastError() == ERROR_NO_MORE_FILES); | |
353 FindClose(find_handle); | |
354 } | |
355 | |
356 void PluginList::GetPluginPathsFromRegistry( | |
357 std::vector<base::FilePath>* plugins) { | |
358 if (PluginList::plugins_discovery_disabled_) | |
359 return; | |
360 | |
361 std::set<base::FilePath> plugin_dirs; | |
362 | |
363 GetPluginsInRegistryDirectory( | |
364 HKEY_CURRENT_USER, kRegistryMozillaPlugins, &plugin_dirs); | |
365 GetPluginsInRegistryDirectory( | |
366 HKEY_LOCAL_MACHINE, kRegistryMozillaPlugins, &plugin_dirs); | |
367 | |
368 for (std::set<base::FilePath>::iterator i = plugin_dirs.begin(); | |
369 i != plugin_dirs.end(); ++i) { | |
370 plugins->push_back(*i); | |
371 } | |
372 } | |
373 | |
374 bool PluginList::ShouldLoadPluginUsingPluginList( | |
375 const webkit::WebPluginInfo& info, | |
376 std::vector<webkit::WebPluginInfo>* plugins) { | |
377 // Version check | |
378 for (size_t j = 0; j < plugins->size(); ++j) { | |
379 base::FilePath::StringType plugin1 = | |
380 StringToLowerASCII((*plugins)[j].path.BaseName().value()); | |
381 base::FilePath::StringType plugin2 = | |
382 StringToLowerASCII(info.path.BaseName().value()); | |
383 if ((plugin1 == plugin2 && HaveSharedMimeType((*plugins)[j], info)) || | |
384 (plugin1 == kJavaDeploy1 && plugin2 == kJavaDeploy2) || | |
385 (plugin1 == kJavaDeploy2 && plugin2 == kJavaDeploy1)) { | |
386 if (IsNewerVersion(info.version, (*plugins)[j].version)) | |
387 return false; // We have loaded a plugin whose version is newer. | |
388 plugins->erase(plugins->begin() + j); | |
389 break; | |
390 } | |
391 } | |
392 | |
393 // The checks below only apply to NPAPI plugins. | |
394 if (info.type != WebPluginInfo::PLUGIN_TYPE_NPAPI) | |
395 return true; | |
396 | |
397 // If the plugin is in our internal list we should load it. | |
398 for (size_t i = 0; i < internal_plugins_.size(); ++i) { | |
399 if (info.path == internal_plugins_[i].path) | |
400 return true; | |
401 } | |
402 | |
403 // Troublemakers. | |
404 base::FilePath::StringType filename = | |
405 StringToLowerASCII(info.path.BaseName().value()); | |
406 // Depends on XPCOM. | |
407 if (filename == kMozillaActiveXPlugin) | |
408 return false; | |
409 | |
410 // Disable the Yahoo Application State plugin as it crashes the plugin | |
411 // process on return from NPObjectStub::OnInvoke. Please refer to | |
412 // http://b/issue?id=1372124 for more information. | |
413 if (filename == kYahooApplicationStatePlugin) | |
414 return false; | |
415 | |
416 // Disable the WangWang protocol handler plugin (npww.dll) as it crashes | |
417 // chrome during shutdown. Firefox also disables this plugin. | |
418 // Please refer to http://code.google.com/p/chromium/issues/detail?id=3953 | |
419 // for more information. | |
420 if (filename == kWanWangProtocolHandlerPlugin) | |
421 return false; | |
422 | |
423 // We only work with newer versions of the Java plugin which use NPAPI only | |
424 // and don't depend on XPCOM. | |
425 if (filename == kJavaPlugin1 || filename == kJavaPlugin2) { | |
426 std::vector<base::FilePath::StringType> ver; | |
427 base::SplitString(info.version, '.', &ver); | |
428 int major, minor, update; | |
429 if (ver.size() == 4 && | |
430 base::StringToInt(ver[0], &major) && | |
431 base::StringToInt(ver[1], &minor) && | |
432 base::StringToInt(ver[2], &update)) { | |
433 if (major == 6 && minor == 0 && update < 120) | |
434 return false; // Java SE6 Update 11 or older. | |
435 } | |
436 } | |
437 | |
438 // Special WMP handling: | |
439 // If both the new and old WMP plugins exist, only load the new one. | |
440 if (filename == kNewWMPPlugin) { | |
441 for (size_t j = 0; j < plugins->size(); ++j) { | |
442 if ((*plugins)[j].path.BaseName().value() == kOldWMPPlugin) { | |
443 plugins->erase(plugins->begin() + j); | |
444 break; | |
445 } | |
446 } | |
447 | |
448 } else if (filename == kOldWMPPlugin) { | |
449 for (size_t j = 0; j < plugins->size(); ++j) { | |
450 if ((*plugins)[j].path.BaseName().value() == kNewWMPPlugin) | |
451 return false; | |
452 } | |
453 } | |
454 | |
455 #if !defined(ARCH_CPU_X86_64) | |
456 // The plugin in question could be a 64 bit plugin which we cannot load. | |
457 base::FilePath plugin_path(info.path); | |
458 if (!IsValid32BitImage(base::MakeAbsoluteFilePath(plugin_path))) | |
459 return false; | |
460 #endif | |
461 return true; | |
462 } | |
463 | |
464 } // namespace npapi | |
465 } // namespace webkit | |
OLD | NEW |