|
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 #include "PluginInfoStore.h" |
|
27 |
|
28 #include <algorithm> |
|
29 #include <WebCore/KURL.h> |
|
30 #include <wtf/StdLibExtras.h> |
|
31 |
|
32 using namespace std; |
|
33 using namespace WebCore; |
|
34 |
|
35 namespace WebKit { |
|
36 |
|
37 PluginInfoStore::PluginInfoStore() |
|
38 : m_pluginListIsUpToDate(false) |
|
39 { |
|
40 } |
|
41 |
|
42 void PluginInfoStore::setAdditionalPluginPaths(const Vector<WebCore::String>& paths) |
|
43 { |
|
44 m_additionalPluginPaths = paths; |
|
45 refresh(); |
|
46 } |
|
47 |
|
48 void PluginInfoStore::refresh() |
|
49 { |
|
50 m_pluginListIsUpToDate = false; |
|
51 } |
|
52 |
|
53 void PluginInfoStore::loadPluginsIfNecessary() |
|
54 { |
|
55 if (m_pluginListIsUpToDate) |
|
56 return; |
|
57 |
|
58 m_plugins.clear(); |
|
59 |
|
60 // First, load plug-ins from the additional plug-in paths specified. |
|
61 for (size_t i = 0; i < m_additionalPluginPaths.size(); ++i) |
|
62 loadPluginsInDirectory(m_additionalPluginPaths[i]); |
|
63 |
|
64 Vector<String> directories = pluginDirectories(); |
|
65 for (size_t i = 0; i < directories.size(); ++i) |
|
66 loadPluginsInDirectory(directories[i]); |
|
67 |
|
68 m_pluginListIsUpToDate = true; |
|
69 } |
|
70 |
|
71 void PluginInfoStore::loadPluginsInDirectory(const String& directory) |
|
72 { |
|
73 Vector<String> pluginPaths = pluginPathsInDirectory(directory); |
|
74 for (size_t i = 0; i < pluginPaths.size(); ++i) |
|
75 loadPlugin(pluginPaths[i]); |
|
76 } |
|
77 |
|
78 void PluginInfoStore::loadPlugin(const String& pluginPath) |
|
79 { |
|
80 Plugin plugin; |
|
81 |
|
82 if (!getPluginInfo(pluginPath, plugin)) |
|
83 return; |
|
84 |
|
85 if (!shouldUsePlugin(plugin, m_plugins)) |
|
86 return; |
|
87 |
|
88 // Add the plug-in. |
|
89 m_plugins.append(plugin); |
|
90 } |
|
91 |
|
92 void PluginInfoStore::getPlugins(Vector<PluginInfo>& plugins) |
|
93 { |
|
94 loadPluginsIfNecessary(); |
|
95 |
|
96 for (size_t i = 0; i < m_plugins.size(); ++i) |
|
97 plugins.append(m_plugins[i].info); |
|
98 } |
|
99 |
|
100 PluginInfoStore::Plugin PluginInfoStore::findPluginForMIMEType(const String& mimeType) |
|
101 { |
|
102 ASSERT(!mimeType.isNull()); |
|
103 |
|
104 for (size_t i = 0; i < m_plugins.size(); ++i) { |
|
105 const Plugin& plugin = m_plugins[i]; |
|
106 |
|
107 for (size_t j = 0; j < plugin.info.mimes.size(); ++j) { |
|
108 const MimeClassInfo& mimeClassInfo = plugin.info.mimes[j]; |
|
109 if (mimeClassInfo.type == mimeType) |
|
110 return plugin; |
|
111 } |
|
112 } |
|
113 |
|
114 return Plugin(); |
|
115 } |
|
116 |
|
117 PluginInfoStore::Plugin PluginInfoStore::findPluginForExtension(const String& extension, String& mimeType) |
|
118 { |
|
119 ASSERT(!extension.isNull()); |
|
120 |
|
121 for (size_t i = 0; i < m_plugins.size(); ++i) { |
|
122 const Plugin& plugin = m_plugins[i]; |
|
123 |
|
124 for (size_t j = 0; j < plugin.info.mimes.size(); ++j) { |
|
125 const MimeClassInfo& mimeClassInfo = plugin.info.mimes[j]; |
|
126 |
|
127 const Vector<String>& extensions = mimeClassInfo.extensions; |
|
128 |
|
129 if (find(extensions.begin(), extensions.end(), extension) != extensions.end()) { |
|
130 // We found a supported extension, set the correct MIME type. |
|
131 mimeType = mimeClassInfo.type; |
|
132 return plugin; |
|
133 } |
|
134 } |
|
135 } |
|
136 |
|
137 return Plugin(); |
|
138 } |
|
139 |
|
140 static inline String pathExtension(const KURL& url) |
|
141 { |
|
142 String extension; |
|
143 String filename = url.lastPathComponent(); |
|
144 if (!filename.endsWith("/")) { |
|
145 int extensionPos = filename.reverseFind('.'); |
|
146 if (extensionPos != -1) |
|
147 extension = filename.substring(extensionPos + 1); |
|
148 } |
|
149 |
|
150 return extension; |
|
151 } |
|
152 |
|
153 PluginInfoStore::Plugin PluginInfoStore::findPlugin(String& mimeType, const KURL& url) |
|
154 { |
|
155 loadPluginsIfNecessary(); |
|
156 |
|
157 // First, check if we can get the plug-in based on its MIME type. |
|
158 if (!mimeType.isNull()) { |
|
159 Plugin plugin = findPluginForMIMEType(mimeType); |
|
160 if (!plugin.path.isNull()) |
|
161 return plugin; |
|
162 } |
|
163 |
|
164 // Next, check if any plug-ins claim to support the URL extension. |
|
165 String extension = pathExtension(url).lower(); |
|
166 if (!extension.isNull()) { |
|
167 Plugin plugin = findPluginForExtension(extension, mimeType); |
|
168 if (!plugin.path.isNull()) |
|
169 return plugin; |
|
170 |
|
171 // Finally, try to get the MIME type from the extension in a platform specific manner and use that. |
|
172 String extensionMimeType = mimeTypeFromExtension(extension); |
|
173 if (!extensionMimeType.isNull()) { |
|
174 Plugin plugin = findPluginForMIMEType(extensionMimeType); |
|
175 if (!plugin.path.isNull()) { |
|
176 mimeType = extensionMimeType; |
|
177 return plugin; |
|
178 } |
|
179 } |
|
180 } |
|
181 |
|
182 return Plugin(); |
|
183 } |
|
184 |
|
185 } // namespace WebKit |