| 1 | /* |
| 2 | * Copyright (C) 2016 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 "config.h" |
| 27 | #include "WebPluginInfoProvider.h" |
| 28 | |
| 29 | #include "HangDetectionDisabler.h" |
| 30 | #include "WebCoreArgumentCoders.h" |
| 31 | #include "WebProcess.h" |
| 32 | #include "WebProcessProxyMessages.h" |
| 33 | #include <WebCore/Document.h> |
| 34 | #include <WebCore/DocumentLoader.h> |
| 35 | #include <WebCore/Frame.h> |
| 36 | #include <WebCore/FrameLoader.h> |
| 37 | #include <WebCore/Page.h> |
| 38 | #include <WebCore/SchemeRegistry.h> |
| 39 | #include <WebCore/Settings.h> |
| 40 | #include <WebCore/SubframeLoader.h> |
| 41 | #include <wtf/text/StringHash.h> |
| 42 | |
| 43 | #if PLATFORM(MAC) |
| 44 | #include <WebCore/StringUtilities.h> |
| 45 | #endif |
| 46 | |
| 47 | namespace WebKit { |
| 48 | using namespace WebCore; |
| 49 | |
| 50 | WebPluginInfoProvider& WebPluginInfoProvider::singleton() |
| 51 | { |
| 52 | static WebPluginInfoProvider& pluginInfoProvider = adoptRef(*new WebPluginInfoProvider).leakRef(); |
| 53 | |
| 54 | return pluginInfoProvider; |
| 55 | } |
| 56 | |
| 57 | WebPluginInfoProvider::WebPluginInfoProvider() |
| 58 | { |
| 59 | } |
| 60 | |
| 61 | WebPluginInfoProvider::~WebPluginInfoProvider() |
| 62 | { |
| 63 | } |
| 64 | |
| 65 | #if PLATFORM(MAC) |
| 66 | void WebPluginInfoProvider::setPluginLoadClientPolicy(WebCore::PluginLoadClientPolicy clientPolicy, const String& host, const String& bundleIdentifier, const String& versionString) |
| 67 | { |
| 68 | String hostToSet = host.isNull() || !host.length() ? "*" : host; |
| 69 | String bundleIdentifierToSet = bundleIdentifier.isNull() || !bundleIdentifier.length() ? "*" : bundleIdentifier; |
| 70 | String versionStringToSet = versionString.isNull() || !versionString.length() ? "*" : versionString; |
| 71 | |
| 72 | PluginPolicyMapsByIdentifier policiesByIdentifier; |
| 73 | if (m_hostsToPluginIdentifierData.contains(hostToSet)) |
| 74 | policiesByIdentifier = m_hostsToPluginIdentifierData.get(hostToSet); |
| 75 | |
| 76 | PluginLoadClientPoliciesByBundleVersion versionsToPolicies; |
| 77 | if (policiesByIdentifier.contains(bundleIdentifierToSet)) |
| 78 | versionsToPolicies = policiesByIdentifier.get(bundleIdentifierToSet); |
| 79 | |
| 80 | versionsToPolicies.set(versionStringToSet, clientPolicy); |
| 81 | policiesByIdentifier.set(bundleIdentifierToSet, versionsToPolicies); |
| 82 | m_hostsToPluginIdentifierData.set(hostToSet, policiesByIdentifier); |
| 83 | |
| 84 | clearPagesPluginData(); |
| 85 | } |
| 86 | |
| 87 | void WebPluginInfoProvider::clearPluginClientPolicies() |
| 88 | { |
| 89 | m_hostsToPluginIdentifierData.clear(); |
| 90 | clearPagesPluginData(); |
| 91 | } |
| 92 | #endif |
| 93 | |
| 94 | void WebPluginInfoProvider::refreshPlugins() |
| 95 | { |
| 96 | #if ENABLE(NETSCAPE_PLUGIN_API) |
| 97 | m_cachedPlugins.clear(); |
| 98 | m_pluginCacheIsPopulated = false; |
| 99 | m_shouldRefreshPlugins = true; |
| 100 | #endif |
| 101 | } |
| 102 | |
| 103 | Vector<PluginInfo> WebPluginInfoProvider::pluginInfo(Page& page, Optional<Vector<SupportedPluginIdentifier>>& supportedPluginIdentifiers) |
| 104 | { |
| 105 | #if ENABLE(NETSCAPE_PLUGIN_API) |
| 106 | populatePluginCache(page); |
| 107 | |
| 108 | if (m_cachedSupportedPluginIdentifiers) |
| 109 | supportedPluginIdentifiers = *m_cachedSupportedPluginIdentifiers; |
| 110 | |
| 111 | return page.mainFrame().loader().subframeLoader().allowPlugins() ? m_cachedPlugins : m_cachedApplicationPlugins; |
| 112 | #else |
| 113 | UNUSED_PARAM(page); |
| 114 | UNUSED_PARAM(supportedPluginIdentifiers); |
| 115 | return { }; |
| 116 | #endif // ENABLE(NETSCAPE_PLUGIN_API) |
| 117 | } |
| 118 | |
| 119 | Vector<WebCore::PluginInfo> WebPluginInfoProvider::webVisiblePluginInfo(Page& page, const URL& url) |
| 120 | { |
| 121 | Optional<Vector<WebCore::SupportedPluginIdentifier>> supportedPluginIdentifiers; |
| 122 | auto plugins = pluginInfo(page, supportedPluginIdentifiers); |
| 123 | |
| 124 | plugins.removeAllMatching([&] (auto& plugin) { |
| 125 | return supportedPluginIdentifiers && !isSupportedPlugin(*supportedPluginIdentifiers, url, plugin.bundleIdentifier); |
| 126 | }); |
| 127 | |
| 128 | #if PLATFORM(MAC) |
| 129 | if (SchemeRegistry::shouldTreatURLSchemeAsLocal(url.protocol().toString())) |
| 130 | return plugins; |
| 131 | |
| 132 | for (int32_t i = plugins.size() - 1; i >= 0; --i) { |
| 133 | auto& info = plugins.at(i); |
| 134 | |
| 135 | // Allow built-in plugins. Also tentatively allow plugins that the client might later selectively permit. |
| 136 | if (info.isApplicationPlugin || info.clientLoadPolicy == WebCore::PluginLoadClientPolicyAsk) |
| 137 | continue; |
| 138 | |
| 139 | if (info.clientLoadPolicy == WebCore::PluginLoadClientPolicyBlock) |
| 140 | plugins.remove(i); |
| 141 | } |
| 142 | #endif |
| 143 | return plugins; |
| 144 | } |
| 145 | |
| 146 | #if ENABLE(NETSCAPE_PLUGIN_API) |
| 147 | void WebPluginInfoProvider::populatePluginCache(const WebCore::Page& page) |
| 148 | { |
| 149 | if (!m_pluginCacheIsPopulated) { |
| 150 | #if PLATFORM(COCOA) |
| 151 | // Application plugins are not affected by enablePlugins setting, so we always need to scan plugins to get them. |
| 152 | bool shouldScanPlugins = true; |
| 153 | #else |
| 154 | bool shouldScanPlugins = page.mainFrame().loader().subframeLoader().allowPlugins(); |
| 155 | #endif |
| 156 | if (shouldScanPlugins) { |
| 157 | HangDetectionDisabler hangDetectionDisabler; |
| 158 | if (!WebProcess::singleton().parentProcessConnection()->sendSync(Messages::WebProcessProxy::GetPlugins(m_shouldRefreshPlugins), |
| 159 | Messages::WebProcessProxy::GetPlugins::Reply(m_cachedPlugins, m_cachedApplicationPlugins, m_cachedSupportedPluginIdentifiers), 0)) |
| 160 | return; |
| 161 | } |
| 162 | |
| 163 | m_shouldRefreshPlugins = false; |
| 164 | m_pluginCacheIsPopulated = true; |
| 165 | } |
| 166 | |
| 167 | #if PLATFORM(MAC) |
| 168 | String pageHost = page.mainFrame().loader().documentLoader()->responseURL().host().toString(); |
| 169 | if (pageHost.isNull()) |
| 170 | return; |
| 171 | for (auto& info : m_cachedPlugins) { |
| 172 | if (auto clientPolicy = pluginLoadClientPolicyForHost(pageHost, info)) |
| 173 | info.clientLoadPolicy = *clientPolicy; |
| 174 | } |
| 175 | #else |
| 176 | UNUSED_PARAM(page); |
| 177 | #endif // not PLATFORM(MAC) |
| 178 | } |
| 179 | #endif |
| 180 | |
| 181 | #if PLATFORM(MAC) |
| 182 | Optional<WebCore::PluginLoadClientPolicy> WebPluginInfoProvider::pluginLoadClientPolicyForHost(const String& host, const WebCore::PluginInfo& info) const |
| 183 | { |
| 184 | String hostToLookUp = host; |
| 185 | String identifier = info.bundleIdentifier; |
| 186 | |
| 187 | auto policiesByIdentifierIterator = m_hostsToPluginIdentifierData.find(hostToLookUp); |
| 188 | |
| 189 | if (!identifier.isNull() && policiesByIdentifierIterator == m_hostsToPluginIdentifierData.end()) { |
| 190 | if (!replaceHostWithMatchedWildcardHost(hostToLookUp, identifier)) |
| 191 | hostToLookUp = "*" ; |
| 192 | policiesByIdentifierIterator = m_hostsToPluginIdentifierData.find(hostToLookUp); |
| 193 | if (hostToLookUp != "*" && policiesByIdentifierIterator == m_hostsToPluginIdentifierData.end()) { |
| 194 | hostToLookUp = "*" ; |
| 195 | policiesByIdentifierIterator = m_hostsToPluginIdentifierData.find(hostToLookUp); |
| 196 | } |
| 197 | } |
| 198 | if (policiesByIdentifierIterator == m_hostsToPluginIdentifierData.end()) |
| 199 | return WTF::nullopt; |
| 200 | |
| 201 | auto& policiesByIdentifier = policiesByIdentifierIterator->value; |
| 202 | |
| 203 | if (!identifier) |
| 204 | identifier = "*" ; |
| 205 | |
| 206 | auto identifierPolicyIterator = policiesByIdentifier.find(identifier); |
| 207 | if (identifier != "*" && identifierPolicyIterator == policiesByIdentifier.end()) { |
| 208 | identifier = "*" ; |
| 209 | identifierPolicyIterator = policiesByIdentifier.find(identifier); |
| 210 | } |
| 211 | |
| 212 | if (identifierPolicyIterator == policiesByIdentifier.end()) |
| 213 | return WTF::nullopt; |
| 214 | |
| 215 | auto& versionsToPolicies = identifierPolicyIterator->value; |
| 216 | |
| 217 | String version = info.versionString; |
| 218 | if (!version) |
| 219 | version = "*" ; |
| 220 | auto policyIterator = versionsToPolicies.find(version); |
| 221 | if (version != "*" && policyIterator == versionsToPolicies.end()) { |
| 222 | version = "*" ; |
| 223 | policyIterator = versionsToPolicies.find(version); |
| 224 | } |
| 225 | |
| 226 | if (policyIterator == versionsToPolicies.end()) |
| 227 | return WTF::nullopt; |
| 228 | |
| 229 | return policyIterator->value; |
| 230 | } |
| 231 | |
| 232 | String WebPluginInfoProvider::longestMatchedWildcardHostForHost(const String& host) const |
| 233 | { |
| 234 | String longestMatchedHost; |
| 235 | |
| 236 | for (auto& key : m_hostsToPluginIdentifierData.keys()) { |
| 237 | if (key.contains('*') && key != "*" && stringMatchesWildcardString(host, key)) { |
| 238 | if (key.length() > longestMatchedHost.length()) |
| 239 | longestMatchedHost = key; |
| 240 | else if (key.length() == longestMatchedHost.length() && codePointCompareLessThan(key, longestMatchedHost)) |
| 241 | longestMatchedHost = key; |
| 242 | } |
| 243 | } |
| 244 | |
| 245 | return longestMatchedHost; |
| 246 | } |
| 247 | |
| 248 | bool WebPluginInfoProvider::replaceHostWithMatchedWildcardHost(String& host, const String& identifier) const |
| 249 | { |
| 250 | String matchedWildcardHost = longestMatchedWildcardHostForHost(host); |
| 251 | |
| 252 | if (matchedWildcardHost.isNull()) |
| 253 | return false; |
| 254 | |
| 255 | auto plugInIdentifierData = m_hostsToPluginIdentifierData.find(matchedWildcardHost); |
| 256 | if (plugInIdentifierData == m_hostsToPluginIdentifierData.end() || !plugInIdentifierData->value.contains(identifier)) |
| 257 | return false; |
| 258 | |
| 259 | host = matchedWildcardHost; |
| 260 | return true; |
| 261 | } |
| 262 | #endif |
| 263 | |
| 264 | } |
| 265 | |