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 (std::vector<WebPluginMimeType>::const_iterator i =
51 info.mime_types.begin(); i != info.mime_types.end(); ++i) {
52 // The Gears plugin is Safari-specific, so don't load it.
53 if (i->mime_type == "application/x-googlegears")
57 // Versions of Flip4Mac 2.3 before 2.3.6 often hang the renderer, so don't
59 if (base::StartsWith(info.name, base::ASCIIToUTF16("Flip4Mac Windows Media"),
61 base::StartsWith(info.version, base::ASCIIToUTF16("2.3"), false)) {
62 std::vector<base::string16> components;
63 base::SplitString(info.version, '.', &components);
64 int bugfix_version = 0;
65 return (components.size() >= 3 &&
66 base::StringToInt(components[2], &bugfix_version) &&
73 NSDictionary* GetMIMETypes(CFBundleRef bundle) {
74 NSString* mime_filename =
75 (NSString*)CFBundleGetValueForInfoDictionaryKey(bundle,
76 CFSTR("WebPluginMIMETypesFilename"));
83 [NSString stringWithFormat:@"%@/Library/Preferences/%@",
84 NSHomeDirectory(), mime_filename];
85 NSDictionary* mime_file_dict =
86 [NSDictionary dictionaryWithContentsOfFile:mime_path];
90 bool valid_file = false;
93 [mime_file_dict objectForKey:@"WebPluginLocalizationName"];
94 NSString* preferred_l10n = [[NSLocale currentLocale] localeIdentifier];
95 if ([l10n_name isEqualToString:preferred_l10n])
100 return [mime_file_dict objectForKey:@"WebPluginMIMETypes"];
102 // dammit, I didn't want to have to do this
104 typedef void (*CreateMIMETypesPrefsPtr)(void);
105 CreateMIMETypesPrefsPtr create_prefs_file =
106 (CreateMIMETypesPrefsPtr)CFBundleGetFunctionPointerForName(
107 bundle, CFSTR("BP_CreatePluginMIMETypesPreferences"));
108 if (!create_prefs_file)
114 mime_file_dict = [NSDictionary dictionaryWithContentsOfFile:mime_path];
116 return [mime_file_dict objectForKey:@"WebPluginMIMETypes"];
121 return (NSDictionary*)CFBundleGetValueForInfoDictionaryKey(bundle,
122 CFSTR("WebPluginMIMETypes"));
126 bool ReadPlistPluginInfo(const base::FilePath& filename, CFBundleRef bundle,
127 WebPluginInfo* info) {
128 NSDictionary* mime_types = GetMIMETypes(bundle);
130 return false; // no type info here; try elsewhere
132 for (NSString* mime_type in [mime_types allKeys]) {
133 NSDictionary* mime_dict = [mime_types objectForKey:mime_type];
134 NSNumber* type_enabled = [mime_dict objectForKey:@"WebPluginTypeEnabled"];
135 NSString* mime_desc = [mime_dict objectForKey:@"WebPluginTypeDescription"];
136 NSArray* mime_exts = [mime_dict objectForKey:@"WebPluginExtensions"];
138 // Skip any disabled types.
139 if (type_enabled && ![type_enabled boolValue])
142 WebPluginMimeType mime;
143 mime.mime_type = base::SysNSStringToUTF8([mime_type lowercaseString]);
144 // Remove PDF from the list of types handled by QuickTime, since it provides
145 // a worse experience than just downloading the PDF.
146 if (mime.mime_type == "application/pdf" &&
147 base::StartsWithASCII(filename.BaseName().value(), "QuickTime",
153 mime.description = base::SysNSStringToUTF16(mime_desc);
154 for (NSString* ext in mime_exts)
155 mime.file_extensions.push_back(
156 base::SysNSStringToUTF8([ext lowercaseString]));
158 info->mime_types.push_back(mime);
161 NSString* plugin_name =
162 (NSString*)CFBundleGetValueForInfoDictionaryKey(bundle,
163 CFSTR("WebPluginName"));
164 NSString* plugin_vers =
165 (NSString*)CFBundleGetValueForInfoDictionaryKey(bundle,
166 CFSTR("CFBundleShortVersionString"));
167 NSString* plugin_desc =
168 (NSString*)CFBundleGetValueForInfoDictionaryKey(bundle,
169 CFSTR("WebPluginDescription"));
172 info->name = base::SysNSStringToUTF16(plugin_name);
174 info->name = base::UTF8ToUTF16(filename.BaseName().value());
175 info->path = filename;
177 info->version = base::SysNSStringToUTF16(plugin_vers);
179 info->desc = base::SysNSStringToUTF16(plugin_desc);
181 info->desc = base::UTF8ToUTF16(filename.BaseName().value());
188 bool PluginList::ReadWebPluginInfo(const base::FilePath &filename,
189 WebPluginInfo* info) {
190 // There are three ways to get information about plugin capabilities:
191 // 1) a set of Info.plist keys, documented at
192 // http://developer.apple.com/documentation/InternetWeb/Conceptual/WebKit_PluginProgTopic/Concepts/AboutPlugins.html .
193 // 2) a set of STR# resources, documented at
194 // https://developer.mozilla.org/En/Gecko_Plugin_API_Reference/Plug-in_Development_Overview .
195 // 3) a NP_GetMIMEDescription() entry point, documented at
196 // https://developer.mozilla.org/en/NP_GetMIMEDescription
198 // Mozilla supported (3), but WebKit never has, so no plugins rely on it. Most
199 // browsers supported (2) and then added support for (1); Chromium originally
200 // supported (2) and (1), but now supports only (1) as (2) is deprecated.
202 // For the Info.plist version, the data is formatted as follows (in text plist
205 // ... the usual plist keys ...
206 // WebPluginDescription = <<plugindescription>>;
207 // WebPluginMIMETypes = {
208 // <<type0mimetype>> = {
209 // WebPluginExtensions = (
210 // <<type0fileextension0>>,
212 // <<type0fileextensionk>>,
214 // WebPluginTypeDescription = <<type0description>>;
216 // <<type1mimetype>> = { ... };
218 // <<typenmimetype>> = { ... };
220 // WebPluginName = <<pluginname>>;
223 // Alternatively (and this is undocumented), rather than a WebPluginMIMETypes
224 // key, there may be a WebPluginMIMETypesFilename key. If it is present, then
225 // it is the name of a file in the user's preferences folder in which to find
226 // the WebPluginMIMETypes key. If the key is present but the file doesn't
227 // exist, we must load the plugin and call a specific function to have the
228 // plugin create the file.
230 ScopedCFTypeRef<CFURLRef> bundle_url(CFURLCreateFromFileSystemRepresentation(
231 kCFAllocatorDefault, (const UInt8*)filename.value().c_str(),
232 filename.value().length(), true));
234 LOG_IF(ERROR, PluginList::DebugPluginLoading())
235 << "PluginLib::ReadWebPluginInfo could not create bundle URL";
238 ScopedCFTypeRef<CFBundleRef> bundle(CFBundleCreate(kCFAllocatorDefault,
241 LOG_IF(ERROR, PluginList::DebugPluginLoading())
242 << "PluginLib::ReadWebPluginInfo could not create CFBundleRef";
249 CFBundleGetPackageInfo(bundle.get(), &type, NULL);
250 if (type != FOUR_CHAR_CODE('BRPL')) {
251 LOG_IF(ERROR, PluginList::DebugPluginLoading())
252 << "PluginLib::ReadWebPluginInfo bundle is not BRPL, is " << type;
257 Boolean would_load = CFBundlePreflightExecutable(bundle.get(), &error);
259 ScopedCFTypeRef<CFStringRef> error_string(CFErrorCopyDescription(error));
260 LOG_IF(ERROR, PluginList::DebugPluginLoading())
261 << "PluginLib::ReadWebPluginInfo bundle failed preflight: "
262 << base::SysCFStringRefToUTF8(error_string);
268 if (ReadPlistPluginInfo(filename, bundle.get(), info))
276 void PluginList::GetPluginDirectories(
277 std::vector<base::FilePath>* plugin_dirs) {
278 if (PluginList::plugins_discovery_disabled_)
281 // Load from the user's area
282 GetPluginCommonDirectory(plugin_dirs, true);
284 // Load from the machine-wide area
285 GetPluginCommonDirectory(plugin_dirs, false);
288 void PluginList::GetPluginsInDir(
289 const base::FilePath& path, std::vector<base::FilePath>* plugins) {
290 base::FileEnumerator enumerator(path,
291 false, // not recursive
292 base::FileEnumerator::DIRECTORIES);
293 for (base::FilePath path = enumerator.Next(); !path.value().empty();
294 path = enumerator.Next()) {
295 plugins->push_back(path);
299 bool PluginList::ShouldLoadPluginUsingPluginList(
300 const WebPluginInfo& info,
301 std::vector<WebPluginInfo>* plugins) {
302 return !IsBlacklistedPlugin(info);
305 } // namespace content