Add ICU message format support
[chromium-blink-merge.git] / content / common / plugin_list_win.cc
blob6c95a0db006b7dfc9a7edc92e76feb946c0c6db7
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, b_ver;
241 base::SplitString(a, ',', &a_ver);
242 base::SplitString(b, ',', &b_ver);
243 if (a_ver.size() == 1 && b_ver.size() == 1) {
244 base::SplitString(a, '.', &a_ver);
245 base::SplitString(b, '.', &b_ver);
247 if (a_ver.size() != b_ver.size())
248 return false;
249 for (size_t i = 0; i < a_ver.size(); i++) {
250 int cur_a, cur_b;
251 base::StringToInt(a_ver[i], &cur_a);
252 base::StringToInt(b_ver[i], &cur_b);
254 if (cur_a > cur_b)
255 return false;
256 if (cur_a < cur_b)
257 return true;
259 return false;
262 } // namespace
264 bool PluginList::ReadWebPluginInfo(const base::FilePath& filename,
265 WebPluginInfo* info) {
266 // On windows, the way we get the mime types for the library is
267 // to check the version information in the DLL itself. This
268 // will be a string of the format: <type1>|<type2>|<type3>|...
269 // For example:
270 // video/quicktime|audio/aiff|image/jpeg
271 scoped_ptr<FileVersionInfo> version_info(
272 FileVersionInfo::CreateFileVersionInfo(filename));
273 if (!version_info) {
274 LOG_IF(ERROR, PluginList::DebugPluginLoading())
275 << "Could not get version info for plugin "
276 << filename.value();
277 return false;
280 FileVersionInfoWin* version_info_win =
281 static_cast<FileVersionInfoWin*>(version_info.get());
283 info->name = version_info->product_name();
284 info->desc = version_info->file_description();
285 info->version = version_info->file_version();
286 info->path = filename;
288 // TODO(evan): Move the ParseMimeTypes code inline once Pepper is updated.
289 if (!PluginList::ParseMimeTypes(
290 base::UTF16ToASCII(version_info_win->GetStringValue(L"MIMEType")),
291 base::UTF16ToASCII(version_info_win->GetStringValue(L"FileExtents")),
292 version_info_win->GetStringValue(L"FileOpenName"),
293 &info->mime_types)) {
294 LOG_IF(ERROR, PluginList::DebugPluginLoading())
295 << "Plugin " << info->name << " has bad MIME types, skipping";
296 return false;
299 return true;
302 void PluginList::GetPluginDirectories(
303 std::vector<base::FilePath>* plugin_dirs) {
304 if (PluginList::plugins_discovery_disabled_)
305 return;
307 // We use a set for uniqueness, which we require, over order, which we do not.
308 std::set<base::FilePath> dirs;
310 // Load from the application-specific area
311 GetAppDirectory(&dirs);
313 // Load from the executable area
314 GetExeDirectory(&dirs);
316 // Load Java
317 GetJavaDirectory(&dirs);
319 // Load firefox plugins too. This is mainly to try to locate
320 // a pre-installed Flash player.
321 GetFirefoxDirectory(&dirs);
323 // Firefox hard-codes the paths of some popular plugins to ensure that
324 // the plugins are found. We are going to copy this as well.
325 GetAcrobatDirectory(&dirs);
326 GetQuicktimeDirectory(&dirs);
327 GetWindowsMediaDirectory(&dirs);
329 for (std::set<base::FilePath>::iterator i = dirs.begin(); i != dirs.end(); ++i)
330 plugin_dirs->push_back(*i);
333 void PluginList::GetPluginsInDir(
334 const base::FilePath& path, std::vector<base::FilePath>* plugins) {
335 WIN32_FIND_DATA find_file_data;
336 HANDLE find_handle;
338 base::string16 dir = path.value();
339 // FindFirstFile requires that you specify a wildcard for directories.
340 dir.append(L"\\NP*.DLL");
342 find_handle = FindFirstFile(dir.c_str(), &find_file_data);
343 if (find_handle == INVALID_HANDLE_VALUE)
344 return;
346 do {
347 if (!(find_file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) {
348 base::FilePath filename = path.Append(find_file_data.cFileName);
349 plugins->push_back(filename);
351 } while (FindNextFile(find_handle, &find_file_data) != 0);
353 DCHECK(GetLastError() == ERROR_NO_MORE_FILES);
354 FindClose(find_handle);
357 void PluginList::GetPluginPathsFromRegistry(
358 std::vector<base::FilePath>* plugins) {
359 if (PluginList::plugins_discovery_disabled_)
360 return;
362 std::set<base::FilePath> plugin_dirs;
364 // Search for plugins from HKCU and HKLM. THis will only find plugins that
365 // are correctly registered in the correct WOW64 registry hive.
366 GetPluginsInRegistryDirectory(HKEY_CURRENT_USER,
367 kRegistryMozillaPlugins,
369 &plugin_dirs);
370 GetPluginsInRegistryDirectory(HKEY_LOCAL_MACHINE,
371 kRegistryMozillaPlugins,
373 &plugin_dirs);
375 for (std::set<base::FilePath>::iterator i = plugin_dirs.begin();
376 i != plugin_dirs.end(); ++i) {
377 plugins->push_back(*i);
381 bool PluginList::ShouldLoadPluginUsingPluginList(
382 const WebPluginInfo& info,
383 std::vector<WebPluginInfo>* plugins) {
384 bool should_check_version = true;
386 base::AutoLock lock(lock_);
387 should_check_version =
388 std::find(extra_plugin_paths_.begin(), extra_plugin_paths_.end(),
389 info.path) == extra_plugin_paths_.end();
391 // Version check for plugins that are not coming from |extra_plugin_paths_|.
392 if (should_check_version) {
393 for (size_t j = 0; j < plugins->size(); ++j) {
394 base::FilePath::StringType plugin1 =
395 base::StringToLowerASCII((*plugins)[j].path.BaseName().value());
396 base::FilePath::StringType plugin2 =
397 base::StringToLowerASCII(info.path.BaseName().value());
398 if ((plugin1 == plugin2 && HaveSharedMimeType((*plugins)[j], info)) ||
399 (plugin1 == kJavaDeploy1 && plugin2 == kJavaDeploy2) ||
400 (plugin1 == kJavaDeploy2 && plugin2 == kJavaDeploy1)) {
401 if (IsNewerVersion(info.version, (*plugins)[j].version))
402 return false; // We have loaded a plugin whose version is newer.
403 plugins->erase(plugins->begin() + j);
404 break;
409 // The checks below only apply to NPAPI plugins.
410 if (info.type != WebPluginInfo::PLUGIN_TYPE_NPAPI)
411 return true;
414 base::AutoLock lock(lock_);
415 // If the plugin is in our internal list we should load it.
416 for (size_t i = 0; i < internal_plugins_.size(); ++i) {
417 if (info.path == internal_plugins_[i].path)
418 return true;
422 // Troublemakers.
423 base::FilePath::StringType filename =
424 base::StringToLowerASCII(info.path.BaseName().value());
425 // Depends on XPCOM.
426 if (filename == kMozillaActiveXPlugin)
427 return false;
429 // Disable the Yahoo Application State plugin as it crashes the plugin
430 // process on return from NPObjectStub::OnInvoke. Please refer to
431 // http://b/issue?id=1372124 for more information.
432 if (filename == kYahooApplicationStatePlugin)
433 return false;
435 // Disable the WangWang protocol handler plugin (npww.dll) as it crashes
436 // chrome during shutdown. Firefox also disables this plugin.
437 // Please refer to http://code.google.com/p/chromium/issues/detail?id=3953
438 // for more information.
439 if (filename == kWanWangProtocolHandlerPlugin)
440 return false;
442 // We only work with newer versions of the Java plugin which use NPAPI only
443 // and don't depend on XPCOM.
444 if (filename == kJavaPlugin1 || filename == kJavaPlugin2) {
445 std::vector<base::FilePath::StringType> ver;
446 base::SplitString(info.version, '.', &ver);
447 int major, minor, update;
448 if (ver.size() == 4 &&
449 base::StringToInt(ver[0], &major) &&
450 base::StringToInt(ver[1], &minor) &&
451 base::StringToInt(ver[2], &update)) {
452 if (major == 6 && minor == 0 && update < 120)
453 return false; // Java SE6 Update 11 or older.
457 // Special WMP handling:
458 // If both the new and old WMP plugins exist, only load the new one.
459 if (filename == kNewWMPPlugin) {
460 for (size_t j = 0; j < plugins->size(); ++j) {
461 if ((*plugins)[j].path.BaseName().value() == kOldWMPPlugin) {
462 plugins->erase(plugins->begin() + j);
463 break;
467 } else if (filename == kOldWMPPlugin) {
468 for (size_t j = 0; j < plugins->size(); ++j) {
469 if ((*plugins)[j].path.BaseName().value() == kNewWMPPlugin)
470 return false;
474 base::FilePath plugin_path(info.path);
475 #if defined(ARCH_CPU_X86_64)
476 // The plugin in question could be a 32 bit plugin which we cannot load.
477 if (IsValid32BitImage(base::MakeAbsoluteFilePath(plugin_path)))
478 return false;
479 #else
480 // The plugin in question could be a 64 bit plugin which we cannot load.
481 if (!IsValid32BitImage(base::MakeAbsoluteFilePath(plugin_path)))
482 return false;
483 #endif
484 return true;
487 } // namespace content