1/*
2 * Copyright (C) 2010 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 *    notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 *    notice, this list of conditions and the following disclaimer in the
11 *    documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#import "config.h"
27#import "PluginInfoStore.h"
28
29#import "NetscapePluginModule.h"
30#import "WebKitSystemInterface.h"
31#import <WebCore/WebCoreNSStringExtras.h>
32#import <wtf/HashSet.h>
33#import <wtf/RetainPtr.h>
34
35using namespace WebCore;
36
37namespace WebKit {
38
39Vector<String> PluginInfoStore::pluginsDirectories()
40{
41    Vector<String> pluginsDirectories;
42
43    pluginsDirectories.append([NSHomeDirectory() stringByAppendingPathComponent:@"Library/Internet Plug-Ins"]);
44    pluginsDirectories.append("/Library/Internet Plug-Ins");
45
46    return pluginsDirectories;
47}
48
49// FIXME: Once the UI process knows the difference between the main thread and the web thread we can drop this and just use
50// String::createCFString.
51static CFStringRef safeCreateCFString(const String& string)
52{
53    return CFStringCreateWithCharacters(0, reinterpret_cast<const UniChar*>(string.characters()), string.length());
54}
55
56Vector<String> PluginInfoStore::pluginPathsInDirectory(const String& directory)
57{
58    Vector<String> pluginPaths;
59
60    RetainPtr<CFStringRef> directoryCFString(AdoptCF, safeCreateCFString(directory));
61
62    NSArray *filenames = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:(NSString *)directoryCFString.get() error:nil];
63    for (NSString *filename in filenames)
64        pluginPaths.append([(NSString *)directoryCFString.get() stringByAppendingPathComponent:filename]);
65
66    return pluginPaths;
67}
68
69Vector<String> PluginInfoStore::individualPluginPaths()
70{
71    return Vector<String>();
72}
73
74bool PluginInfoStore::getPluginInfo(const String& pluginPath, Plugin& plugin)
75{
76    return NetscapePluginModule::getPluginInfo(pluginPath, plugin);
77}
78
79bool PluginInfoStore::shouldUsePlugin(const Plugin& plugin)
80{
81    for (size_t i = 0; i < m_plugins.size(); ++i) {
82        const Plugin& loadedPlugin = m_plugins[i];
83
84        // If a plug-in with the same bundle identifier already exists, we don't want to load it.
85        if (loadedPlugin.bundleIdentifier == plugin.bundleIdentifier)
86            return false;
87    }
88
89    return true;
90}
91
92String PluginInfoStore::getMIMETypeForExtension(const String& extension)
93{
94    // FIXME: This should just call MIMETypeRegistry::getMIMETypeForExtension and be
95    // strength reduced into the callsite once we can safely convert String
96    // to CFStringRef off the main thread.
97
98    RetainPtr<CFStringRef> extensionCFString(AdoptCF, safeCreateCFString(extension));
99    return WKGetMIMETypeForExtension((NSString *)extensionCFString.get());
100}
101
102} // namespace WebKit
103