| 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 "config.h" |
| 27 | #include "NetscapePluginModule.h" |
| 28 | |
| 29 | #if ENABLE(NETSCAPE_PLUGIN_API) |
| 30 | |
| 31 | #include "Module.h" |
| 32 | #include "NPRuntimeUtilities.h" |
| 33 | #include "NetscapeBrowserFuncs.h" |
| 34 | #include <wtf/NeverDestroyed.h> |
| 35 | #include <wtf/text/CString.h> |
| 36 | |
| 37 | namespace WebKit { |
| 38 | |
| 39 | static Vector<NetscapePluginModule*>& initializedNetscapePluginModules() |
| 40 | { |
| 41 | static NeverDestroyed<Vector<NetscapePluginModule*>> initializedNetscapePluginModules; |
| 42 | return initializedNetscapePluginModules; |
| 43 | } |
| 44 | |
| 45 | NetscapePluginModule::NetscapePluginModule(const String& pluginPath) |
| 46 | : m_pluginPath(pluginPath) |
| 47 | , m_isInitialized(false) |
| 48 | , m_loadCount(0) |
| 49 | , m_shutdownProcPtr(0) |
| 50 | , m_pluginFuncs() |
| 51 | { |
| 52 | } |
| 53 | |
| 54 | NetscapePluginModule::~NetscapePluginModule() |
| 55 | { |
| 56 | ASSERT(initializedNetscapePluginModules().find(this) == notFound); |
| 57 | } |
| 58 | |
| 59 | Vector<String> NetscapePluginModule::sitesWithData() |
| 60 | { |
| 61 | Vector<String> sites; |
| 62 | |
| 63 | incrementLoadCount(); |
| 64 | tryGetSitesWithData(sites); |
| 65 | decrementLoadCount(); |
| 66 | |
| 67 | return sites; |
| 68 | } |
| 69 | |
| 70 | bool NetscapePluginModule::clearSiteData(const String& site, uint64_t flags, uint64_t maxAge) |
| 71 | { |
| 72 | incrementLoadCount(); |
| 73 | bool result = tryClearSiteData(site, flags, maxAge); |
| 74 | decrementLoadCount(); |
| 75 | |
| 76 | return result; |
| 77 | } |
| 78 | |
| 79 | bool NetscapePluginModule::tryGetSitesWithData(Vector<String>& sites) |
| 80 | { |
| 81 | if (!m_isInitialized) |
| 82 | return false; |
| 83 | |
| 84 | // Check if the plug-in supports NPP_GetSitesWithData. |
| 85 | if (!m_pluginFuncs.getsiteswithdata) |
| 86 | return false; |
| 87 | |
| 88 | char** siteArray = m_pluginFuncs.getsiteswithdata(); |
| 89 | |
| 90 | // There were no sites with data. |
| 91 | if (!siteArray) |
| 92 | return true; |
| 93 | |
| 94 | for (int i = 0; siteArray[i]; ++i) { |
| 95 | char* site = siteArray[i]; |
| 96 | |
| 97 | String siteString = String::fromUTF8(site); |
| 98 | if (!siteString.isNull()) |
| 99 | sites.append(siteString); |
| 100 | |
| 101 | npnMemFree(site); |
| 102 | } |
| 103 | |
| 104 | npnMemFree(siteArray); |
| 105 | return true; |
| 106 | } |
| 107 | |
| 108 | bool NetscapePluginModule::tryClearSiteData(const String& site, uint64_t flags, uint64_t maxAge) |
| 109 | { |
| 110 | if (!m_isInitialized) |
| 111 | return false; |
| 112 | |
| 113 | // Check if the plug-in supports NPP_ClearSiteData. |
| 114 | if (!m_pluginFuncs.clearsitedata) |
| 115 | return false; |
| 116 | |
| 117 | CString siteString; |
| 118 | if (!site.isNull()) |
| 119 | siteString = site.utf8(); |
| 120 | |
| 121 | return m_pluginFuncs.clearsitedata(siteString.data(), flags, maxAge) == NPERR_NO_ERROR; |
| 122 | } |
| 123 | |
| 124 | void NetscapePluginModule::shutdown() |
| 125 | { |
| 126 | ASSERT(m_isInitialized); |
| 127 | |
| 128 | m_shutdownProcPtr(); |
| 129 | |
| 130 | m_isInitialized = false; |
| 131 | |
| 132 | size_t pluginModuleIndex = initializedNetscapePluginModules().find(this); |
| 133 | ASSERT(pluginModuleIndex != notFound); |
| 134 | |
| 135 | initializedNetscapePluginModules().remove(pluginModuleIndex); |
| 136 | } |
| 137 | |
| 138 | RefPtr<NetscapePluginModule> NetscapePluginModule::getOrCreate(const String& pluginPath) |
| 139 | { |
| 140 | // First, see if we already have a module with this plug-in path. |
| 141 | for (size_t i = 0; i < initializedNetscapePluginModules().size(); ++i) { |
| 142 | NetscapePluginModule* pluginModule = initializedNetscapePluginModules()[i]; |
| 143 | |
| 144 | if (pluginModule->m_pluginPath == pluginPath) |
| 145 | return pluginModule; |
| 146 | } |
| 147 | |
| 148 | auto pluginModule(adoptRef(*new NetscapePluginModule(pluginPath))); |
| 149 | |
| 150 | // Try to load and initialize the plug-in module. |
| 151 | if (!pluginModule->load()) |
| 152 | return nullptr; |
| 153 | |
| 154 | return pluginModule; |
| 155 | } |
| 156 | |
| 157 | void NetscapePluginModule::incrementLoadCount() |
| 158 | { |
| 159 | if (!m_loadCount) { |
| 160 | // Load the plug-in module if necessary. |
| 161 | load(); |
| 162 | } |
| 163 | |
| 164 | m_loadCount++; |
| 165 | } |
| 166 | |
| 167 | void NetscapePluginModule::decrementLoadCount() |
| 168 | { |
| 169 | ASSERT(m_loadCount > 0); |
| 170 | m_loadCount--; |
| 171 | |
| 172 | if (!m_loadCount && m_isInitialized) { |
| 173 | shutdown(); |
| 174 | unload(); |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | bool NetscapePluginModule::load() |
| 179 | { |
| 180 | if (m_isInitialized) { |
| 181 | ASSERT(initializedNetscapePluginModules().find(this) != notFound); |
| 182 | return true; |
| 183 | } |
| 184 | |
| 185 | if (!tryLoad()) { |
| 186 | unload(); |
| 187 | return false; |
| 188 | } |
| 189 | |
| 190 | m_isInitialized = true; |
| 191 | |
| 192 | ASSERT(initializedNetscapePluginModules().find(this) == notFound); |
| 193 | initializedNetscapePluginModules().append(this); |
| 194 | |
| 195 | determineQuirks(); |
| 196 | |
| 197 | return true; |
| 198 | } |
| 199 | |
| 200 | #if PLATFORM(GTK) |
| 201 | static bool moduleMixesGtkSymbols(Module* module) |
| 202 | { |
| 203 | #ifdef GTK_API_VERSION_2 |
| 204 | return module->functionPointer<gpointer>("gtk_application_get_type" ); |
| 205 | #else |
| 206 | return module->functionPointer<gpointer>("gtk_object_get_type" ); |
| 207 | #endif |
| 208 | } |
| 209 | #endif |
| 210 | |
| 211 | bool NetscapePluginModule::tryLoad() |
| 212 | { |
| 213 | m_module = std::make_unique<Module>(m_pluginPath); |
| 214 | if (!m_module->load()) |
| 215 | return false; |
| 216 | |
| 217 | #if PLATFORM(GTK) |
| 218 | if (moduleMixesGtkSymbols(m_module.get())) |
| 219 | return false; |
| 220 | #endif |
| 221 | |
| 222 | NP_InitializeFuncPtr initializeFuncPtr = m_module->functionPointer<NP_InitializeFuncPtr>("NP_Initialize" ); |
| 223 | if (!initializeFuncPtr) |
| 224 | return false; |
| 225 | |
| 226 | #if !PLUGIN_ARCHITECTURE(UNIX) |
| 227 | NP_GetEntryPointsFuncPtr getEntryPointsFuncPtr = m_module->functionPointer<NP_GetEntryPointsFuncPtr>("NP_GetEntryPoints" ); |
| 228 | if (!getEntryPointsFuncPtr) |
| 229 | return false; |
| 230 | #endif |
| 231 | |
| 232 | m_shutdownProcPtr = m_module->functionPointer<NPP_ShutdownProcPtr>("NP_Shutdown" ); |
| 233 | if (!m_shutdownProcPtr) |
| 234 | return false; |
| 235 | |
| 236 | m_pluginFuncs.size = sizeof(NPPluginFuncs); |
| 237 | m_pluginFuncs.version = (NP_VERSION_MAJOR << 8) | NP_VERSION_MINOR; |
| 238 | |
| 239 | // On Mac, NP_Initialize must be called first, then NP_GetEntryPoints. On Windows, the order is |
| 240 | // reversed. Failing to follow this order results in crashes (e.g., in Silverlight on Mac and |
| 241 | // in Flash and QuickTime on Windows). |
| 242 | #if PLUGIN_ARCHITECTURE(MAC) |
| 243 | return initializeFuncPtr(netscapeBrowserFuncs()) == NPERR_NO_ERROR && getEntryPointsFuncPtr(&m_pluginFuncs) == NPERR_NO_ERROR; |
| 244 | #elif PLUGIN_ARCHITECTURE(UNIX) |
| 245 | if (initializeFuncPtr(netscapeBrowserFuncs(), &m_pluginFuncs) != NPERR_NO_ERROR) |
| 246 | return false; |
| 247 | #endif |
| 248 | |
| 249 | return true; |
| 250 | } |
| 251 | |
| 252 | void NetscapePluginModule::unload() |
| 253 | { |
| 254 | ASSERT(!m_isInitialized); |
| 255 | |
| 256 | m_module = nullptr; |
| 257 | } |
| 258 | |
| 259 | } // namespace WebKit |
| 260 | |
| 261 | #endif // ENABLE(NETSCAPE_PLUGIN_API) |
| 262 | |