WebCore/plugins/qt/PluginPackageQt.cpp
changeset 0 4f2f89ce4247
child 2 303757a437d3
equal deleted inserted replaced
-1:000000000000 0:4f2f89ce4247
       
     1 /*
       
     2  * Copyright (C) 2006, 2007 Apple Inc.  All rights reserved.
       
     3  * Copyright (C) 2008 Collabora Ltd. All rights reserved.
       
     4  *
       
     5  * Redistribution and use in source and binary forms, with or without
       
     6  * modification, are permitted provided that the following conditions
       
     7  * are met:
       
     8  * 1. Redistributions of source code must retain the above copyright
       
     9  *    notice, this list of conditions and the following disclaimer.
       
    10  * 2. Redistributions in binary form must reproduce the above copyright
       
    11  *    notice, this list of conditions and the following disclaimer in the
       
    12  *    documentation and/or other materials provided with the distribution.
       
    13  *
       
    14  * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
       
    15  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
       
    16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
       
    17  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
       
    18  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
       
    19  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
       
    20  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
       
    21  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
       
    22  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
       
    23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
       
    24  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
       
    25  */
       
    26 
       
    27 #include "config.h"
       
    28 #include "PluginPackage.h"
       
    29 
       
    30 #include "MIMETypeRegistry.h"
       
    31 #include "npruntime_impl.h"
       
    32 #include "PluginDatabase.h"
       
    33 #include "PluginDebug.h"
       
    34 #include <wtf/text/CString.h>
       
    35 
       
    36 namespace WebCore {
       
    37 
       
    38 typedef void gtkInitFunc(int *argc, char ***argv);
       
    39 
       
    40 bool PluginPackage::fetchInfo()
       
    41 {
       
    42     if (!load())
       
    43         return false;
       
    44 
       
    45     NPP_GetValueProcPtr gv = (NPP_GetValueProcPtr)m_module->resolve("NP_GetValue");
       
    46     typedef char *(*NPP_GetMIMEDescriptionProcPtr)();
       
    47     NPP_GetMIMEDescriptionProcPtr gm =
       
    48         (NPP_GetMIMEDescriptionProcPtr)m_module->resolve("NP_GetMIMEDescription");
       
    49     if (!gm || !gv)
       
    50         return false;
       
    51 
       
    52     char *buf = 0;
       
    53     NPError err = gv(0, NPPVpluginNameString, (void*) &buf);
       
    54     if (err != NPERR_NO_ERROR)
       
    55         return false;
       
    56 
       
    57     m_name = buf;
       
    58     err = gv(0, NPPVpluginDescriptionString, (void*) &buf);
       
    59     if (err != NPERR_NO_ERROR)
       
    60         return false;
       
    61 
       
    62     m_description = buf;
       
    63     determineModuleVersionFromDescription();
       
    64 
       
    65     String s = gm();
       
    66     Vector<String> types;
       
    67     s.split(UChar(';'), false, types);
       
    68     for (unsigned i = 0; i < types.size(); ++i) {
       
    69         Vector<String> mime;
       
    70         types[i].split(UChar(':'), true, mime);
       
    71         if (mime.size() > 0) {
       
    72             Vector<String> exts;
       
    73             if (mime.size() > 1)
       
    74                 mime[1].split(UChar(','), false, exts);
       
    75             determineQuirks(mime[0]);
       
    76             m_mimeToExtensions.add(mime[0], exts);
       
    77             if (mime.size() > 2)
       
    78                 m_mimeToDescriptions.add(mime[0], mime[2]);
       
    79         }
       
    80     }
       
    81 
       
    82     return true;
       
    83 }
       
    84 
       
    85 static NPError staticPluginQuirkRequiresGtkToolKit_NPN_GetValue(NPP instance, NPNVariable variable, void* value)
       
    86 {
       
    87     if (variable == NPNVToolkit) {
       
    88         *static_cast<uint32_t*>(value) = 2;
       
    89         return NPERR_NO_ERROR;
       
    90     }
       
    91 
       
    92     return NPN_GetValue(instance, variable, value);
       
    93 }
       
    94 
       
    95 bool PluginPackage::load()
       
    96 {
       
    97     if (m_isLoaded) {
       
    98         m_loadCount++;
       
    99         return true;
       
   100     }
       
   101 
       
   102     m_module = new QLibrary((QString)m_path);
       
   103     m_module->setLoadHints(QLibrary::ResolveAllSymbolsHint);
       
   104     if (!m_module->load()) {
       
   105         LOG(Plugins, "%s not loaded (%s)", m_path.utf8().data(),
       
   106                 m_module->errorString().toLatin1().constData());
       
   107         return false;
       
   108     }
       
   109 
       
   110     m_isLoaded = true;
       
   111 
       
   112     NP_InitializeFuncPtr NP_Initialize;
       
   113     NPError npErr;
       
   114     gtkInitFunc* gtkInit;
       
   115 
       
   116     NP_Initialize = (NP_InitializeFuncPtr)m_module->resolve("NP_Initialize");
       
   117     m_NPP_Shutdown = (NPP_ShutdownProcPtr)m_module->resolve("NP_Shutdown");
       
   118 
       
   119     if (!NP_Initialize || !m_NPP_Shutdown)
       
   120         goto abort;
       
   121 
       
   122     memset(&m_pluginFuncs, 0, sizeof(m_pluginFuncs));
       
   123     m_pluginFuncs.size = sizeof(m_pluginFuncs);
       
   124 
       
   125     initializeBrowserFuncs();
       
   126 
       
   127     if (m_path.contains("npwrapper.")) {
       
   128         // nspluginwrapper relies on the toolkit value to know if glib is available
       
   129         // It does so in NP_Initialize with a null instance, therefore it is done this way:
       
   130         m_browserFuncs.getvalue = staticPluginQuirkRequiresGtkToolKit_NPN_GetValue;
       
   131     }
       
   132 
       
   133     // WORKAROUND: Prevent gtk based plugin crashes such as BR# 40567 by
       
   134     // explicitly forcing the initializing of Gtk, i.e. calling gtk_init,
       
   135     // whenver the symbol is present in the plugin library loaded above.
       
   136     // Note that this workaround is based on code from the NSPluginClass ctor
       
   137     // in KDE's kdebase/apps/nsplugins/viewer/nsplugin.cpp file.
       
   138     gtkInit = (gtkInitFunc*)m_module->resolve("gtk_init");
       
   139     if (gtkInit) {
       
   140         // Prevent gtk_init() from replacing the X error handlers, since the Gtk
       
   141         // handlers abort when they receive an X error, thus killing the viewer.
       
   142 #ifdef Q_WS_X11
       
   143         int (*old_error_handler)(Display*, XErrorEvent*) = XSetErrorHandler(0);
       
   144         int (*old_io_error_handler)(Display*) = XSetIOErrorHandler(0);
       
   145 #endif
       
   146         gtkInit(0, 0);
       
   147 #ifdef Q_WS_X11
       
   148         XSetErrorHandler(old_error_handler);
       
   149         XSetIOErrorHandler(old_io_error_handler);
       
   150 #endif
       
   151     }
       
   152 
       
   153 #if defined(XP_UNIX)
       
   154     npErr = NP_Initialize(&m_browserFuncs, &m_pluginFuncs);
       
   155 #else
       
   156     npErr = NP_Initialize(&m_browserFuncs);
       
   157 #endif
       
   158     if (npErr != NPERR_NO_ERROR)
       
   159         goto abort;
       
   160 
       
   161     m_loadCount++;
       
   162     return true;
       
   163 
       
   164 abort:
       
   165     unloadWithoutShutdown();
       
   166     return false;
       
   167 }
       
   168 
       
   169 uint16_t PluginPackage::NPVersion() const
       
   170 {
       
   171     return NP_VERSION_MINOR;
       
   172 }
       
   173 }