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