summaryrefslogtreecommitdiffstats
path: root/webkit/glue/plugins/plugin_lib_mac.mm
blob: bd2bcbb879cbbedfbe0333eadaf7243a0de050a4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#import <Carbon/Carbon.h>

#include "webkit/glue/plugins/plugin_lib.h"

#include "base/native_library.h"
#include "base/scoped_cftyperef.h"
#include "base/scoped_ptr.h"
#include "base/string_split.h"
#include "base/string_util.h"
#include "base/sys_string_conversions.h"
#include "base/utf_string_conversions.h"
#include "webkit/glue/plugins/plugin_list.h"

static const short kSTRTypeDefinitionResourceID = 128;
static const short kSTRTypeDescriptionResourceID = 127;
static const short kSTRPluginDescriptionResourceID = 126;

namespace NPAPI
{

namespace {

NSDictionary* GetMIMETypes(CFBundleRef bundle) {
  NSString* mime_filename =
      (NSString*)CFBundleGetValueForInfoDictionaryKey(bundle,
                     CFSTR("WebPluginMIMETypesFilename"));

  if (mime_filename) {

    // get the file

    NSString* mime_path =
        [NSString stringWithFormat:@"%@/Library/Preferences/%@",
         NSHomeDirectory(), mime_filename];
    NSDictionary* mime_file_dict =
        [NSDictionary dictionaryWithContentsOfFile:mime_path];

    // is it valid?

    bool valid_file = false;
    if (mime_file_dict) {
      NSString* l10n_name =
          [mime_file_dict objectForKey:@"WebPluginLocalizationName"];
      NSString* preferred_l10n = [[NSLocale currentLocale] localeIdentifier];
      if ([l10n_name isEqualToString:preferred_l10n])
        valid_file = true;
    }

    if (valid_file)
      return [mime_file_dict objectForKey:@"WebPluginMIMETypes"];

    // dammit, I didn't want to have to do this

    typedef void (*CreateMIMETypesPrefsPtr)(void);
    CreateMIMETypesPrefsPtr create_prefs_file =
        (CreateMIMETypesPrefsPtr)CFBundleGetFunctionPointerForName(
        bundle, CFSTR("BP_CreatePluginMIMETypesPreferences"));
    if (!create_prefs_file)
      return nil;
    create_prefs_file();

    // one more time

    mime_file_dict = [NSDictionary dictionaryWithContentsOfFile:mime_path];
    if (mime_file_dict)
      return [mime_file_dict objectForKey:@"WebPluginMIMETypes"];
    else
      return nil;

  } else {
    return (NSDictionary*)CFBundleGetValueForInfoDictionaryKey(bundle,
                              CFSTR("WebPluginMIMETypes"));
  }
}

bool ReadPlistPluginInfo(const FilePath& filename, CFBundleRef bundle,
                         WebPluginInfo* info) {
  NSDictionary* mime_types = GetMIMETypes(bundle);
  if (!mime_types)
    return false;  // no type info here; try elsewhere

  for (NSString* mime_type in [mime_types allKeys]) {
    NSDictionary* mime_dict = [mime_types objectForKey:mime_type];
    NSString* mime_desc = [mime_dict objectForKey:@"WebPluginTypeDescription"];
    NSArray* mime_exts = [mime_dict objectForKey:@"WebPluginExtensions"];

    WebPluginMimeType mime;
    mime.mime_type = base::SysNSStringToUTF8([mime_type lowercaseString]);
    // Remove PDF from the list of types handled by QuickTime, since it provides
    // a worse experience than just downloading the PDF.
    if (mime.mime_type == "application/pdf" &&
        StartsWithASCII(filename.BaseName().value(), "QuickTime", false)) {
      continue;
    }

    if (mime_desc)
      mime.description = base::SysNSStringToUTF16(mime_desc);
    for (NSString* ext in mime_exts)
      mime.file_extensions.push_back(
          base::SysNSStringToUTF8([ext lowercaseString]));

    info->mime_types.push_back(mime);
  }

  NSString* plugin_name =
      (NSString*)CFBundleGetValueForInfoDictionaryKey(bundle,
      CFSTR("WebPluginName"));
  NSString* plugin_vers =
      (NSString*)CFBundleGetValueForInfoDictionaryKey(bundle,
      CFSTR("CFBundleShortVersionString"));
  NSString* plugin_desc =
      (NSString*)CFBundleGetValueForInfoDictionaryKey(bundle,
      CFSTR("WebPluginDescription"));

  if (plugin_name)
    info->name = base::SysNSStringToUTF16(plugin_name);
  else
    info->name = UTF8ToUTF16(filename.BaseName().value());
  info->path = filename;
  if (plugin_vers)
    info->version = base::SysNSStringToUTF16(plugin_vers);
  if (plugin_desc)
    info->desc = base::SysNSStringToUTF16(plugin_desc);
  else
    info->desc = UTF8ToUTF16(filename.BaseName().value());
  info->enabled = true;

  return true;
}

class ScopedBundleResourceFile {
 public:
  ScopedBundleResourceFile(CFBundleRef bundle) : bundle_(bundle) {
    old_ref_num_ = CurResFile();
    bundle_ref_num_ = CFBundleOpenBundleResourceMap(bundle);
    UseResFile(bundle_ref_num_);
  }
  ~ScopedBundleResourceFile() {
    UseResFile(old_ref_num_);
    CFBundleCloseBundleResourceMap(bundle_, bundle_ref_num_);
  }

 private:
  CFBundleRef bundle_;
  CFBundleRefNum bundle_ref_num_;
  ResFileRefNum old_ref_num_;
};

bool GetSTRResource(CFBundleRef bundle, short res_id,
                    std::vector<std::string>* contents) {
  Handle res_handle = Get1Resource('STR#', res_id);
  if (!res_handle || !*res_handle)
    return false;

  char* pointer = *res_handle;
  short num_strings = *(short*)pointer;
  pointer += sizeof(short);
  for (short i = 0; i < num_strings; ++i) {
    // Despite being 8-bits wide, these are legacy encoded. Make a round trip.
    scoped_cftyperef<CFStringRef> str(CFStringCreateWithPascalStringNoCopy(
        kCFAllocatorDefault,
        (unsigned char*)pointer,
        GetApplicationTextEncoding(),  // is this right?
        kCFAllocatorNull));            // perhaps CFStringGetSystemEncoding?
    if (!str.get())
      return false;
    contents->push_back(base::SysCFStringRefToUTF8(str.get()));
    pointer += 1+*reinterpret_cast<unsigned char*>(pointer);
  }

  return true;
}

bool ReadSTRPluginInfo(const FilePath& filename, CFBundleRef bundle,
                       WebPluginInfo* info) {
  ScopedBundleResourceFile res_file(bundle);

  std::vector<std::string> type_strings;
  if (!GetSTRResource(bundle, kSTRTypeDefinitionResourceID, &type_strings))
    return false;

  std::vector<std::string> type_descs;
  bool have_type_descs = GetSTRResource(bundle,
                                        kSTRTypeDescriptionResourceID,
                                        &type_descs);

  std::vector<std::string> plugin_descs;
  bool have_plugin_descs = GetSTRResource(bundle,
                                          kSTRPluginDescriptionResourceID,
                                          &plugin_descs);

  size_t num_types = type_strings.size()/2;

  for (size_t i = 0; i < num_types; ++i) {
    WebPluginMimeType mime;
    mime.mime_type = StringToLowerASCII(type_strings[2*i]);
    if (have_type_descs && i < type_descs.size())
      mime.description = UTF8ToUTF16(type_descs[i]);
    base::SplitString(
        StringToLowerASCII(type_strings[2*i+1]), ',', &mime.file_extensions);

    info->mime_types.push_back(mime);
  }

  NSString* plugin_vers =
      (NSString*)CFBundleGetValueForInfoDictionaryKey(bundle,
      CFSTR("CFBundleShortVersionString"));

  if (have_plugin_descs && plugin_descs.size() > 1)
    info->name = UTF8ToUTF16(plugin_descs[1]);
  else
    info->name = UTF8ToUTF16(filename.BaseName().value());
  info->path = filename;
  if (plugin_vers)
    info->version = base::SysNSStringToUTF16(plugin_vers);
  if (have_plugin_descs && plugin_descs.size() > 0)
    info->desc = UTF8ToUTF16(plugin_descs[0]);
  else
    info->desc = UTF8ToUTF16(filename.BaseName().value());
  info->enabled = true;

  return true;
}

}  // anonymous namespace

bool PluginLib::ReadWebPluginInfo(const FilePath &filename,
                                  WebPluginInfo* info) {
  // There are two ways to get information about plugin capabilities. One is an
  // Info.plist set of keys, documented at
  // http://developer.apple.com/documentation/InternetWeb/Conceptual/WebKit_PluginProgTopic/Concepts/AboutPlugins.html .
  // The other is a set of STR# resources, documented at
  // https://developer.mozilla.org/En/Gecko_Plugin_API_Reference/Plug-in_Development_Overview .
  //
  // Historically, the data was maintained in the STR# resources. Apple, with
  // the introduction of WebKit, noted the weaknesses of resources and moved the
  // information into the Info.plist. Mozilla had always supported a
  // NP_GetMIMEDescription() entry point for Unix plugins and also supports it
  // on the Mac to supplement the STR# format. WebKit does not support
  // NP_GetMIMEDescription() and neither do we. (That entry point is documented
  // at https://developer.mozilla.org/en/NP_GetMIMEDescription .) We prefer the
  // Info.plist format because it's more extensible and has a defined encoding,
  // but will fall back to the STR# format of the data if it is not present in
  // the Info.plist.
  //
  // The parsing of the data lives in the two functions ReadSTRPluginInfo() and
  // ReadPlistPluginInfo(), but a summary of the formats follows.
  //
  // Each data type handled by a plugin has several properties:
  // - <<type0mimetype>>
  // - <<type0fileextension0>>..<<type0fileextensionk>>
  // - <<type0description>>
  //
  // Each plugin may have any number of types defined. In addition, the plugin
  // itself has properties:
  // - <<plugindescription>>
  // - <<pluginname>>
  //
  // For the Info.plist version, the data is formatted as follows (in text plist
  // format):
  //  {
  //    ... the usual plist keys ...
  //    WebPluginDescription = <<plugindescription>>;
  //    WebPluginMIMETypes = {
  //      <<type0mimetype>> = {
  //        WebPluginExtensions = (
  //                               <<type0fileextension0>>,
  //                               ...
  //                               <<type0fileextensionk>>,
  //                               );
  //        WebPluginTypeDescription = <<type0description>>;
  //      };
  //      <<type1mimetype>> = { ... };
  //      ...
  //      <<typenmimetype>> = { ... };
  //    };
  //    WebPluginName = <<pluginname>>;
  //  }
  //
  // Alternatively (and this is undocumented), rather than a WebPluginMIMETypes
  // key, there may be a WebPluginMIMETypesFilename key. If it is present, then
  // it is the name of a file in the user's preferences folder in which to find
  // the WebPluginMIMETypes key. If the key is present but the file doesn't
  // exist, we must load the plugin and call a specific function to have the
  // plugin create the file.
  //
  // If we do not find those keys in the Info.plist, we fall back to the STR#
  // resources. In them, the data is formatted as follows:
  // STR# 128
  // (1) <<type0mimetype>>
  // (2) <<type0fileextension0>>,...,<<type0fileextensionk>>
  // (3) <<type1mimetype>>
  // (4) <<type1fileextension0>>,...,<<type1fileextensionk>>
  // (...)
  // (2n+1) <<typenmimetype>>
  // (2n+2) <<typenfileextension0>>,...,<<typenfileextensionk>>
  // STR# 127
  // (1) <<type0description>>
  // (2) <<type1description>>
  // (...)
  // (n+1) <<typendescription>>
  // STR# 126
  // (1) <<plugindescription>>
  // (2) <<pluginname>>
  //
  // Strictly speaking, only STR# 128 is required.

  scoped_cftyperef<CFURLRef> bundle_url(CFURLCreateFromFileSystemRepresentation(
      kCFAllocatorDefault, (const UInt8*)filename.value().c_str(),
      filename.value().length(), true));
  if (!bundle_url)
    return false;
  scoped_cftyperef<CFBundleRef> bundle(CFBundleCreate(kCFAllocatorDefault,
                                                      bundle_url.get()));
  if (!bundle)
    return false;

  // preflight

  OSType type = 0;
  CFBundleGetPackageInfo(bundle.get(), &type, NULL);
  if (type != FOUR_CHAR_CODE('BRPL'))
    return false;

  CFErrorRef error;
  Boolean would_load = CFBundlePreflightExecutable(bundle.get(), &error);
  if (!would_load)
    return false;

  // get the info

  if (ReadPlistPluginInfo(filename, bundle.get(), info))
    return true;

  if (ReadSTRPluginInfo(filename, bundle.get(), info))
    return true;

  // ... or not

  return false;
}

}  // namespace NPAPI