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"
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 "content/common/plugin_constants_win.h"
28 const base::char16 kRegistryApps
[] =
29 L
"Software\\Microsoft\\Windows\\CurrentVersion\\App Paths";
30 const base::char16 kRegistryFirefox
[] = L
"firefox.exe";
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
))
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
))
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
"\\");
76 base::win::RegKey
hkcu_key(HKEY_CURRENT_USER
, reg_path
.c_str(), KEY_READ
);
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
);
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
);
94 // Search the registry at the given path and detect plugin directories.
95 void GetPluginsInRegistryDirectory(
97 const base::string16
& registry_folder
,
98 std::set
<base::FilePath
>* plugin_dirs
) {
99 for (base::win::RegistryKeyIterator
iter(root_key
, registry_folder
.c_str());
100 iter
.Valid(); ++iter
) {
101 // Use the registry to gather plugin across the file system.
102 base::string16 reg_path
= registry_folder
;
103 reg_path
.append(L
"\\");
104 reg_path
.append(iter
.Name());
105 base::win::RegKey
key(root_key
, reg_path
.c_str(), KEY_READ
);
108 if (key
.ReadValue(kRegistryPath
, &path
) == ERROR_SUCCESS
)
109 plugin_dirs
->insert(base::FilePath(path
));
113 // Enumerate through the registry key to find all installed FireFox paths.
114 // FireFox 3 beta and version 2 can coexist. See bug: 1025003
115 void GetFirefoxInstalledPaths(std::vector
<base::FilePath
>* out
) {
116 base::win::RegistryKeyIterator
it(HKEY_LOCAL_MACHINE
,
117 kRegistryFirefoxInstalled
);
118 for (; it
.Valid(); ++it
) {
119 base::string16 full_path
= base::string16(kRegistryFirefoxInstalled
) +
120 L
"\\" + it
.Name() + L
"\\Main";
121 base::win::RegKey
key(HKEY_LOCAL_MACHINE
, full_path
.c_str(), KEY_READ
);
122 base::string16 install_dir
;
123 if (key
.ReadValue(L
"Install Directory", &install_dir
) != ERROR_SUCCESS
)
125 out
->push_back(base::FilePath(install_dir
));
129 // Get plugin directory locations from the Firefox install path. This is kind
130 // of a kludge, but it helps us locate the flash player for users that
131 // already have it for firefox. Not having to download yet-another-plugin
133 void GetFirefoxDirectory(std::set
<base::FilePath
>* plugin_dirs
) {
134 std::vector
<base::FilePath
> paths
;
135 GetFirefoxInstalledPaths(&paths
);
136 for (unsigned int i
= 0; i
< paths
.size(); ++i
) {
137 plugin_dirs
->insert(AppendPluginsDir(paths
[i
]));
140 base::FilePath firefox_app_data_plugin_path
;
141 if (PathService::Get(base::DIR_APP_DATA
, &firefox_app_data_plugin_path
)) {
142 firefox_app_data_plugin_path
=
143 firefox_app_data_plugin_path
.AppendASCII("Mozilla");
144 plugin_dirs
->insert(AppendPluginsDir(firefox_app_data_plugin_path
));
148 // Hardcoded logic to detect Acrobat plugins locations.
149 void GetAcrobatDirectory(std::set
<base::FilePath
>* plugin_dirs
) {
151 if (!GetInstalledPath(kRegistryAcrobatReader
, &path
) &&
152 !GetInstalledPath(kRegistryAcrobat
, &path
)) {
156 plugin_dirs
->insert(path
.Append(L
"Browser"));
159 // Hardcoded logic to detect QuickTime plugin location.
160 void GetQuicktimeDirectory(std::set
<base::FilePath
>* plugin_dirs
) {
162 if (GetInstalledPath(kRegistryQuickTime
, &path
))
163 plugin_dirs
->insert(AppendPluginsDir(path
));
166 // Hardcoded logic to detect Windows Media Player plugin location.
167 void GetWindowsMediaDirectory(std::set
<base::FilePath
>* plugin_dirs
) {
169 if (GetInstalledPath(kRegistryWindowsMedia
, &path
))
170 plugin_dirs
->insert(path
);
173 // Hardcoded logic to detect Java plugin location.
174 void GetJavaDirectory(std::set
<base::FilePath
>* plugin_dirs
) {
175 // Load the new NPAPI Java plugin
176 // 1. Open the main JRE key under HKLM
177 base::win::RegKey
java_key(HKEY_LOCAL_MACHINE
, kRegistryJava
,
180 // 2. Read the current Java version
181 base::string16 java_version
;
182 if (java_key
.ReadValue(kRegistryBrowserJavaVersion
, &java_version
) !=
184 java_key
.ReadValue(kRegistryCurrentJavaVersion
, &java_version
);
187 if (!java_version
.empty()) {
188 java_key
.OpenKey(java_version
.c_str(), KEY_QUERY_VALUE
);
190 // 3. Install path of the JRE binaries is specified in "JavaHome"
191 // value under the Java version key.
192 base::string16 java_plugin_directory
;
193 if (java_key
.ReadValue(kRegistryJavaHome
, &java_plugin_directory
) ==
195 // 4. The new plugin resides under the 'bin/new_plugin'
197 DCHECK(!java_plugin_directory
.empty());
198 java_plugin_directory
.append(L
"\\bin\\new_plugin");
200 // 5. We don't know the exact name of the DLL but it's in the form
201 // NP*.dll so just invoke LoadPlugins on this path.
202 plugin_dirs
->insert(base::FilePath(java_plugin_directory
));
207 bool IsValid32BitImage(const base::FilePath
& path
) {
208 base::MemoryMappedFile plugin_image
;
210 if (!plugin_image
.InitializeAsImageSection(path
))
213 base::win::PEImage
image(plugin_image
.data());
215 PIMAGE_NT_HEADERS nt_headers
= image
.GetNTHeaders();
216 return (nt_headers
->FileHeader
.Machine
== IMAGE_FILE_MACHINE_I386
);
219 // Returns true if the given plugins share at least one mime type. This is used
220 // to differentiate newer versions of a plugin vs two plugins which happen to
221 // have the same filename.
222 bool HaveSharedMimeType(const WebPluginInfo
& plugin1
,
223 const WebPluginInfo
& plugin2
) {
224 for (size_t i
= 0; i
< plugin1
.mime_types
.size(); ++i
) {
225 for (size_t j
= 0; j
< plugin2
.mime_types
.size(); ++j
) {
226 if (plugin1
.mime_types
[i
].mime_type
== plugin2
.mime_types
[j
].mime_type
)
234 // Compares Windows style version strings (i.e. 1,2,3,4). Returns true if b's
235 // version is newer than a's, or false if it's equal or older.
236 bool IsNewerVersion(const base::string16
& a
, const base::string16
& b
) {
237 std::vector
<base::string16
> a_ver
, b_ver
;
238 base::SplitString(a
, ',', &a_ver
);
239 base::SplitString(b
, ',', &b_ver
);
240 if (a_ver
.size() == 1 && b_ver
.size() == 1) {
241 base::SplitString(a
, '.', &a_ver
);
242 base::SplitString(b
, '.', &b_ver
);
244 if (a_ver
.size() != b_ver
.size())
246 for (size_t i
= 0; i
< a_ver
.size(); i
++) {
248 base::StringToInt(a_ver
[i
], &cur_a
);
249 base::StringToInt(b_ver
[i
], &cur_b
);
261 bool PluginList::ReadWebPluginInfo(const base::FilePath
& filename
,
262 WebPluginInfo
* info
) {
263 // On windows, the way we get the mime types for the library is
264 // to check the version information in the DLL itself. This
265 // will be a string of the format: <type1>|<type2>|<type3>|...
267 // video/quicktime|audio/aiff|image/jpeg
268 scoped_ptr
<FileVersionInfo
> version_info(
269 FileVersionInfo::CreateFileVersionInfo(filename
));
271 LOG_IF(ERROR
, PluginList::DebugPluginLoading())
272 << "Could not get version info for plugin "
277 FileVersionInfoWin
* version_info_win
=
278 static_cast<FileVersionInfoWin
*>(version_info
.get());
280 info
->name
= version_info
->product_name();
281 info
->desc
= version_info
->file_description();
282 info
->version
= version_info
->file_version();
283 info
->path
= filename
;
285 // TODO(evan): Move the ParseMimeTypes code inline once Pepper is updated.
286 if (!PluginList::ParseMimeTypes(
287 UTF16ToASCII(version_info_win
->GetStringValue(L
"MIMEType")),
288 UTF16ToASCII(version_info_win
->GetStringValue(L
"FileExtents")),
289 version_info_win
->GetStringValue(L
"FileOpenName"),
290 &info
->mime_types
)) {
291 LOG_IF(ERROR
, PluginList::DebugPluginLoading())
292 << "Plugin " << info
->name
<< " has bad MIME types, skipping";
299 void PluginList::GetPluginDirectories(
300 std::vector
<base::FilePath
>* plugin_dirs
) {
301 if (PluginList::plugins_discovery_disabled_
)
304 // We use a set for uniqueness, which we require, over order, which we do not.
305 std::set
<base::FilePath
> dirs
;
307 // Load from the application-specific area
308 GetAppDirectory(&dirs
);
310 // Load from the executable area
311 GetExeDirectory(&dirs
);
314 GetJavaDirectory(&dirs
);
316 // Load firefox plugins too. This is mainly to try to locate
317 // a pre-installed Flash player.
318 GetFirefoxDirectory(&dirs
);
320 // Firefox hard-codes the paths of some popular plugins to ensure that
321 // the plugins are found. We are going to copy this as well.
322 GetAcrobatDirectory(&dirs
);
323 GetQuicktimeDirectory(&dirs
);
324 GetWindowsMediaDirectory(&dirs
);
326 for (std::set
<base::FilePath
>::iterator i
= dirs
.begin(); i
!= dirs
.end(); ++i
)
327 plugin_dirs
->push_back(*i
);
330 void PluginList::GetPluginsInDir(
331 const base::FilePath
& path
, std::vector
<base::FilePath
>* plugins
) {
332 WIN32_FIND_DATA find_file_data
;
335 base::string16 dir
= path
.value();
336 // FindFirstFile requires that you specify a wildcard for directories.
337 dir
.append(L
"\\NP*.DLL");
339 find_handle
= FindFirstFile(dir
.c_str(), &find_file_data
);
340 if (find_handle
== INVALID_HANDLE_VALUE
)
344 if (!(find_file_data
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
)) {
345 base::FilePath filename
= path
.Append(find_file_data
.cFileName
);
346 plugins
->push_back(filename
);
348 } while (FindNextFile(find_handle
, &find_file_data
) != 0);
350 DCHECK(GetLastError() == ERROR_NO_MORE_FILES
);
351 FindClose(find_handle
);
354 void PluginList::GetPluginPathsFromRegistry(
355 std::vector
<base::FilePath
>* plugins
) {
356 if (PluginList::plugins_discovery_disabled_
)
359 std::set
<base::FilePath
> plugin_dirs
;
361 GetPluginsInRegistryDirectory(
362 HKEY_CURRENT_USER
, kRegistryMozillaPlugins
, &plugin_dirs
);
363 GetPluginsInRegistryDirectory(
364 HKEY_LOCAL_MACHINE
, kRegistryMozillaPlugins
, &plugin_dirs
);
366 for (std::set
<base::FilePath
>::iterator i
= plugin_dirs
.begin();
367 i
!= plugin_dirs
.end(); ++i
) {
368 plugins
->push_back(*i
);
372 bool PluginList::ShouldLoadPluginUsingPluginList(
373 const WebPluginInfo
& info
,
374 std::vector
<WebPluginInfo
>* plugins
) {
375 bool should_check_version
= true;
377 base::AutoLock
lock(lock_
);
378 should_check_version
=
379 std::find(extra_plugin_paths_
.begin(), extra_plugin_paths_
.end(),
380 info
.path
) == extra_plugin_paths_
.end();
382 // Version check for plugins that are not coming from |extra_plugin_paths_|.
383 if (should_check_version
) {
384 for (size_t j
= 0; j
< plugins
->size(); ++j
) {
385 base::FilePath::StringType plugin1
=
386 StringToLowerASCII((*plugins
)[j
].path
.BaseName().value());
387 base::FilePath::StringType plugin2
=
388 StringToLowerASCII(info
.path
.BaseName().value());
389 if ((plugin1
== plugin2
&& HaveSharedMimeType((*plugins
)[j
], info
)) ||
390 (plugin1
== kJavaDeploy1
&& plugin2
== kJavaDeploy2
) ||
391 (plugin1
== kJavaDeploy2
&& plugin2
== kJavaDeploy1
)) {
392 if (IsNewerVersion(info
.version
, (*plugins
)[j
].version
))
393 return false; // We have loaded a plugin whose version is newer.
394 plugins
->erase(plugins
->begin() + j
);
400 // The checks below only apply to NPAPI plugins.
401 if (info
.type
!= WebPluginInfo::PLUGIN_TYPE_NPAPI
)
405 base::AutoLock
lock(lock_
);
406 // If the plugin is in our internal list we should load it.
407 for (size_t i
= 0; i
< internal_plugins_
.size(); ++i
) {
408 if (info
.path
== internal_plugins_
[i
].path
)
414 base::FilePath::StringType filename
=
415 StringToLowerASCII(info
.path
.BaseName().value());
417 if (filename
== kMozillaActiveXPlugin
)
420 // Disable the Yahoo Application State plugin as it crashes the plugin
421 // process on return from NPObjectStub::OnInvoke. Please refer to
422 // http://b/issue?id=1372124 for more information.
423 if (filename
== kYahooApplicationStatePlugin
)
426 // Disable the WangWang protocol handler plugin (npww.dll) as it crashes
427 // chrome during shutdown. Firefox also disables this plugin.
428 // Please refer to http://code.google.com/p/chromium/issues/detail?id=3953
429 // for more information.
430 if (filename
== kWanWangProtocolHandlerPlugin
)
433 // We only work with newer versions of the Java plugin which use NPAPI only
434 // and don't depend on XPCOM.
435 if (filename
== kJavaPlugin1
|| filename
== kJavaPlugin2
) {
436 std::vector
<base::FilePath::StringType
> ver
;
437 base::SplitString(info
.version
, '.', &ver
);
438 int major
, minor
, update
;
439 if (ver
.size() == 4 &&
440 base::StringToInt(ver
[0], &major
) &&
441 base::StringToInt(ver
[1], &minor
) &&
442 base::StringToInt(ver
[2], &update
)) {
443 if (major
== 6 && minor
== 0 && update
< 120)
444 return false; // Java SE6 Update 11 or older.
448 // Special WMP handling:
449 // If both the new and old WMP plugins exist, only load the new one.
450 if (filename
== kNewWMPPlugin
) {
451 for (size_t j
= 0; j
< plugins
->size(); ++j
) {
452 if ((*plugins
)[j
].path
.BaseName().value() == kOldWMPPlugin
) {
453 plugins
->erase(plugins
->begin() + j
);
458 } else if (filename
== kOldWMPPlugin
) {
459 for (size_t j
= 0; j
< plugins
->size(); ++j
) {
460 if ((*plugins
)[j
].path
.BaseName().value() == kNewWMPPlugin
)
465 #if !defined(ARCH_CPU_X86_64)
466 // The plugin in question could be a 64 bit plugin which we cannot load.
467 base::FilePath
plugin_path(info
.path
);
468 if (!IsValid32BitImage(base::MakeAbsoluteFilePath(plugin_path
)))
474 } // namespace content