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 #import <Carbon/Carbon.h>
8 #import <Foundation/Foundation.h>
10 #include "base/files/file_enumerator.h"
11 #include "base/files/file_util.h"
12 #include "base/mac/mac_util.h"
13 #include "base/mac/scoped_cftyperef.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "base/native_library.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/sys_string_conversions.h"
20 #include "base/strings/utf_string_conversions.h"
22 using base::ScopedCFTypeRef;
28 void GetPluginCommonDirectory(std::vector<base::FilePath>* plugin_dirs,
30 // Note that there are no NSSearchPathDirectory constants for these
31 // directories so we can't use Cocoa's NSSearchPathForDirectoriesInDomains().
32 // Interestingly, Safari hard-codes the location (see
33 // WebKit/WebKit/mac/Plugins/WebPluginDatabase.mm's +_defaultPlugInPaths).
35 OSErr err = FSFindFolder(user ? kUserDomain : kLocalDomain,
36 kInternetPlugInFolderType, false, &ref);
41 plugin_dirs->push_back(base::FilePath(base::mac::PathFromFSRef(ref)));
44 // Returns true if the plugin should be prevented from loading.
45 bool IsBlacklistedPlugin(const WebPluginInfo& info) {
46 // We blacklist Gears by included MIME type, since that is more stable than
47 // its name. Be careful about adding any more plugins to this list though,
48 // since it's easy to accidentally blacklist plugins that support lots of
50 for (const WebPluginMimeType& mime : info.mime_types) {
51 // The Gears plugin is Safari-specific, so don't load it.
52 if (mime.mime_type == "application/x-googlegears")
56 // Versions of Flip4Mac 2.3 before 2.3.6 often hang the renderer, so don't
58 if (base::StartsWith(info.name,
59 base::ASCIIToUTF16("Flip4Mac Windows Media"),
60 base::CompareCase::INSENSITIVE_ASCII) &&
61 base::StartsWith(info.version, base::ASCIIToUTF16("2.3"),
62 base::CompareCase::SENSITIVE)) {
63 std::vector<base::StringPiece16> components = base::SplitStringPiece(
64 info.version, base::ASCIIToUTF16("."), base::TRIM_WHITESPACE,
65 base::SPLIT_WANT_ALL);
66 int bugfix_version = 0;
67 return (components.size() >= 3 &&
68 base::StringToInt(components[2], &bugfix_version) &&
75 NSDictionary* GetMIMETypes(CFBundleRef bundle) {
76 NSString* mime_filename =
77 (NSString*)CFBundleGetValueForInfoDictionaryKey(bundle,
78 CFSTR("WebPluginMIMETypesFilename"));
85 [NSString stringWithFormat:@"%@/Library/Preferences/%@",
86 NSHomeDirectory(), mime_filename];
87 NSDictionary* mime_file_dict =
88 [NSDictionary dictionaryWithContentsOfFile:mime_path];
92 bool valid_file = false;
95 [mime_file_dict objectForKey:@"WebPluginLocalizationName"];
96 NSString* preferred_l10n = [[NSLocale currentLocale] localeIdentifier];
97 if ([l10n_name isEqualToString:preferred_l10n])
102 return [mime_file_dict objectForKey:@"WebPluginMIMETypes"];
104 // dammit, I didn't want to have to do this
106 typedef void (*CreateMIMETypesPrefsPtr)(void);
107 CreateMIMETypesPrefsPtr create_prefs_file =
108 (CreateMIMETypesPrefsPtr)CFBundleGetFunctionPointerForName(
109 bundle, CFSTR("BP_CreatePluginMIMETypesPreferences"));
110 if (!create_prefs_file)
116 mime_file_dict = [NSDictionary dictionaryWithContentsOfFile:mime_path];
118 return [mime_file_dict objectForKey:@"WebPluginMIMETypes"];
123 return (NSDictionary*)CFBundleGetValueForInfoDictionaryKey(bundle,
124 CFSTR("WebPluginMIMETypes"));
128 bool ReadPlistPluginInfo(const base::FilePath& filename, CFBundleRef bundle,
129 WebPluginInfo* info) {
130 NSDictionary* mime_types = GetMIMETypes(bundle);
132 return false; // no type info here; try elsewhere
134 for (NSString* mime_type in [mime_types allKeys]) {
135 NSDictionary* mime_dict = [mime_types objectForKey:mime_type];
136 NSNumber* type_enabled = [mime_dict objectForKey:@"WebPluginTypeEnabled"];
137 NSString* mime_desc = [mime_dict objectForKey:@"WebPluginTypeDescription"];
138 NSArray* mime_exts = [mime_dict objectForKey:@"WebPluginExtensions"];
140 // Skip any disabled types.
141 if (type_enabled && ![type_enabled boolValue])
144 WebPluginMimeType mime;
145 mime.mime_type = base::SysNSStringToUTF8([mime_type lowercaseString]);
146 // Remove PDF from the list of types handled by QuickTime, since it provides
147 // a worse experience than just downloading the PDF.
148 if (mime.mime_type == "application/pdf" &&
149 base::StartsWith(filename.BaseName().value(), "QuickTime",
150 base::CompareCase::INSENSITIVE_ASCII)) {
155 mime.description = base::SysNSStringToUTF16(mime_desc);
156 for (NSString* ext in mime_exts)
157 mime.file_extensions.push_back(
158 base::SysNSStringToUTF8([ext lowercaseString]));
160 info->mime_types.push_back(mime);
163 NSString* plugin_name =
164 (NSString*)CFBundleGetValueForInfoDictionaryKey(bundle,
165 CFSTR("WebPluginName"));
166 NSString* plugin_vers =
167 (NSString*)CFBundleGetValueForInfoDictionaryKey(bundle,
168 CFSTR("CFBundleShortVersionString"));
169 NSString* plugin_desc =
170 (NSString*)CFBundleGetValueForInfoDictionaryKey(bundle,
171 CFSTR("WebPluginDescription"));
174 info->name = base::SysNSStringToUTF16(plugin_name);
176 info->name = base::UTF8ToUTF16(filename.BaseName().value());
177 info->path = filename;
179 info->version = base::SysNSStringToUTF16(plugin_vers);
181 info->desc = base::SysNSStringToUTF16(plugin_desc);
183 info->desc = base::UTF8ToUTF16(filename.BaseName().value());
190 bool PluginList::ReadWebPluginInfo(const base::FilePath &filename,
191 WebPluginInfo* info) {
192 // There are three ways to get information about plugin capabilities:
193 // 1) a set of Info.plist keys, documented at
194 // http://developer.apple.com/documentation/InternetWeb/Conceptual/WebKit_PluginProgTopic/Concepts/AboutPlugins.html .
195 // 2) a set of STR# resources, documented at
196 // https://developer.mozilla.org/En/Gecko_Plugin_API_Reference/Plug-in_Development_Overview .
197 // 3) a NP_GetMIMEDescription() entry point, documented at
198 // https://developer.mozilla.org/en/NP_GetMIMEDescription
200 // Mozilla supported (3), but WebKit never has, so no plugins rely on it. Most
201 // browsers supported (2) and then added support for (1); Chromium originally
202 // supported (2) and (1), but now supports only (1) as (2) is deprecated.
204 // For the Info.plist version, the data is formatted as follows (in text plist
207 // ... the usual plist keys ...
208 // WebPluginDescription = <<plugindescription>>;
209 // WebPluginMIMETypes = {
210 // <<type0mimetype>> = {
211 // WebPluginExtensions = (
212 // <<type0fileextension0>>,
214 // <<type0fileextensionk>>,
216 // WebPluginTypeDescription = <<type0description>>;
218 // <<type1mimetype>> = { ... };
220 // <<typenmimetype>> = { ... };
222 // WebPluginName = <<pluginname>>;
225 // Alternatively (and this is undocumented), rather than a WebPluginMIMETypes
226 // key, there may be a WebPluginMIMETypesFilename key. If it is present, then
227 // it is the name of a file in the user's preferences folder in which to find
228 // the WebPluginMIMETypes key. If the key is present but the file doesn't
229 // exist, we must load the plugin and call a specific function to have the
230 // plugin create the file.
232 ScopedCFTypeRef<CFURLRef> bundle_url(CFURLCreateFromFileSystemRepresentation(
233 kCFAllocatorDefault, (const UInt8*)filename.value().c_str(),
234 filename.value().length(), true));
236 LOG_IF(ERROR, PluginList::DebugPluginLoading())
237 << "PluginLib::ReadWebPluginInfo could not create bundle URL";
240 ScopedCFTypeRef<CFBundleRef> bundle(CFBundleCreate(kCFAllocatorDefault,
243 LOG_IF(ERROR, PluginList::DebugPluginLoading())
244 << "PluginLib::ReadWebPluginInfo could not create CFBundleRef";
251 CFBundleGetPackageInfo(bundle.get(), &type, NULL);
252 if (type != FOUR_CHAR_CODE('BRPL')) {
253 LOG_IF(ERROR, PluginList::DebugPluginLoading())
254 << "PluginLib::ReadWebPluginInfo bundle is not BRPL, is " << type;
259 Boolean would_load = CFBundlePreflightExecutable(bundle.get(), &error);
261 ScopedCFTypeRef<CFStringRef> error_string(CFErrorCopyDescription(error));
262 LOG_IF(ERROR, PluginList::DebugPluginLoading())
263 << "PluginLib::ReadWebPluginInfo bundle failed preflight: "
264 << base::SysCFStringRefToUTF8(error_string);
270 if (ReadPlistPluginInfo(filename, bundle.get(), info))
278 void PluginList::GetPluginDirectories(
279 std::vector<base::FilePath>* plugin_dirs) {
280 if (PluginList::plugins_discovery_disabled_)
283 // Load from the user's area
284 GetPluginCommonDirectory(plugin_dirs, true);
286 // Load from the machine-wide area
287 GetPluginCommonDirectory(plugin_dirs, false);
290 void PluginList::GetPluginsInDir(
291 const base::FilePath& path, std::vector<base::FilePath>* plugins) {
292 base::FileEnumerator enumerator(path,
293 false, // not recursive
294 base::FileEnumerator::DIRECTORIES);
295 for (base::FilePath path = enumerator.Next(); !path.value().empty();
296 path = enumerator.Next()) {
297 plugins->push_back(path);
301 bool PluginList::ShouldLoadPluginUsingPluginList(
302 const WebPluginInfo& info,
303 std::vector<WebPluginInfo>* plugins) {
304 return !IsBlacklistedPlugin(info);
307 } // namespace content