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