Home | History | Annotate | Download | only in Plugins
      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 "NPObjectMessageReceiver.h"
     28 
     29 #if ENABLE(PLUGIN_PROCESS)
     30 
     31 #include "NPIdentifierData.h"
     32 #include "NPRemoteObjectMap.h"
     33 #include "NPRuntimeUtilities.h"
     34 #include "NPVariantData.h"
     35 
     36 namespace WebKit {
     37 
     38 PassOwnPtr<NPObjectMessageReceiver> NPObjectMessageReceiver::create(NPRemoteObjectMap* npRemoteObjectMap, Plugin* plugin, uint64_t npObjectID, NPObject* npObject)
     39 {
     40     return adoptPtr(new NPObjectMessageReceiver(npRemoteObjectMap, plugin, npObjectID, npObject));
     41 }
     42 
     43 NPObjectMessageReceiver::NPObjectMessageReceiver(NPRemoteObjectMap* npRemoteObjectMap, Plugin* plugin, uint64_t npObjectID, NPObject* npObject)
     44     : m_npRemoteObjectMap(npRemoteObjectMap)
     45     , m_plugin(plugin)
     46     , m_npObjectID(npObjectID)
     47     , m_npObject(npObject)
     48 {
     49     retainNPObject(m_npObject);
     50 }
     51 
     52 NPObjectMessageReceiver::~NPObjectMessageReceiver()
     53 {
     54     m_npRemoteObjectMap->unregisterNPObject(m_npObjectID);
     55 
     56     releaseNPObject(m_npObject);
     57 }
     58 
     59 void NPObjectMessageReceiver::deallocate()
     60 {
     61     delete this;
     62 }
     63 
     64 void NPObjectMessageReceiver::hasMethod(const NPIdentifierData& methodNameData, bool& returnValue)
     65 {
     66     if (!m_npObject->_class->hasMethod) {
     67         returnValue = false;
     68         return;
     69     }
     70 
     71     returnValue = m_npObject->_class->hasMethod(m_npObject, methodNameData.createNPIdentifier());
     72 }
     73 
     74 void NPObjectMessageReceiver::invoke(const NPIdentifierData& methodNameData, const Vector<NPVariantData>& argumentsData, bool& returnValue, NPVariantData& resultData)
     75 {
     76     if (!m_npObject->_class->invoke) {
     77         returnValue = false;
     78         return;
     79     }
     80 
     81     Vector<NPVariant> arguments;
     82     for (size_t i = 0; i < argumentsData.size(); ++i)
     83         arguments.append(m_npRemoteObjectMap->npVariantDataToNPVariant(argumentsData[i], m_plugin));
     84 
     85     NPVariant result;
     86     VOID_TO_NPVARIANT(result);
     87 
     88     returnValue = m_npObject->_class->invoke(m_npObject, methodNameData.createNPIdentifier(), arguments.data(), arguments.size(), &result);
     89     if (returnValue) {
     90         // Convert the NPVariant to an NPVariantData.
     91         resultData = m_npRemoteObjectMap->npVariantToNPVariantData(result, m_plugin);
     92     }
     93 
     94     // Release all arguments.
     95     for (size_t i = 0; i < argumentsData.size(); ++i)
     96         releaseNPVariantValue(&arguments[i]);
     97 
     98     // And release the result.
     99     releaseNPVariantValue(&result);
    100 }
    101 
    102 void NPObjectMessageReceiver::invokeDefault(const Vector<NPVariantData>& argumentsData, bool& returnValue, NPVariantData& resultData)
    103 {
    104     if (!m_npObject->_class->invokeDefault) {
    105         returnValue = false;
    106         return;
    107     }
    108 
    109     Vector<NPVariant> arguments;
    110     for (size_t i = 0; i < argumentsData.size(); ++i)
    111         arguments.append(m_npRemoteObjectMap->npVariantDataToNPVariant(argumentsData[i], m_plugin));
    112 
    113     NPVariant result;
    114     VOID_TO_NPVARIANT(result);
    115 
    116     returnValue = m_npObject->_class->invokeDefault(m_npObject, arguments.data(), arguments.size(), &result);
    117     if (returnValue) {
    118         // Convert the NPVariant to an NPVariantData.
    119         resultData = m_npRemoteObjectMap->npVariantToNPVariantData(result, m_plugin);
    120     }
    121 
    122     // Release all arguments.
    123     for (size_t i = 0; i < argumentsData.size(); ++i)
    124         releaseNPVariantValue(&arguments[i]);
    125 
    126     // And release the result.
    127     releaseNPVariantValue(&result);
    128 }
    129 
    130 void NPObjectMessageReceiver::hasProperty(const NPIdentifierData& propertyNameData, bool& returnValue)
    131 {
    132     if (!m_npObject->_class->hasProperty) {
    133         returnValue = false;
    134         return;
    135     }
    136 
    137     returnValue = m_npObject->_class->hasProperty(m_npObject, propertyNameData.createNPIdentifier());
    138 }
    139 
    140 void NPObjectMessageReceiver::getProperty(const NPIdentifierData& propertyNameData, bool& returnValue, NPVariantData& resultData)
    141 {
    142     if (!m_npObject->_class->getProperty) {
    143         returnValue = false;
    144         return;
    145     }
    146 
    147     NPVariant result;
    148     returnValue = m_npObject->_class->getProperty(m_npObject, propertyNameData.createNPIdentifier(), &result);
    149     if (!returnValue)
    150         return;
    151 
    152     // Convert the NPVariant to an NPVariantData.
    153     resultData = m_npRemoteObjectMap->npVariantToNPVariantData(result, m_plugin);
    154 
    155     // And release the result.
    156     releaseNPVariantValue(&result);
    157 }
    158 
    159 void NPObjectMessageReceiver::setProperty(const NPIdentifierData& propertyNameData, const NPVariantData& propertyValueData, bool& returnValue)
    160 {
    161     if (!m_npObject->_class->setProperty) {
    162         returnValue = false;
    163         return;
    164     }
    165 
    166     NPVariant propertyValue = m_npRemoteObjectMap->npVariantDataToNPVariant(propertyValueData, m_plugin);
    167 
    168     // Set the property.
    169     returnValue = m_npObject->_class->setProperty(m_npObject, propertyNameData.createNPIdentifier(), &propertyValue);
    170 
    171     // And release the value.
    172     releaseNPVariantValue(&propertyValue);
    173 }
    174 
    175 void NPObjectMessageReceiver::removeProperty(const NPIdentifierData& propertyNameData, bool& returnValue)
    176 {
    177     if (!m_npObject->_class->removeProperty) {
    178         returnValue = false;
    179         return;
    180     }
    181 
    182     returnValue = m_npObject->_class->removeProperty(m_npObject, propertyNameData.createNPIdentifier());
    183 }
    184 
    185 void NPObjectMessageReceiver::enumerate(bool& returnValue, Vector<NPIdentifierData>& identifiersData)
    186 {
    187     if (!NP_CLASS_STRUCT_VERSION_HAS_ENUM(m_npObject->_class) || !m_npObject->_class->enumerate) {
    188         returnValue = false;
    189         return;
    190     }
    191 
    192     NPIdentifier* identifiers = 0;
    193     uint32_t identifierCount = 0;
    194 
    195     returnValue = m_npObject->_class->enumerate(m_npObject, &identifiers, &identifierCount);
    196     if (!returnValue)
    197         return;
    198 
    199     for (uint32_t i = 0; i < identifierCount; ++i)
    200         identifiersData.append(NPIdentifierData::fromNPIdentifier(identifiers[i]));
    201 
    202     npnMemFree(identifiers);
    203 }
    204 
    205 void NPObjectMessageReceiver::construct(const Vector<NPVariantData>& argumentsData, bool& returnValue, NPVariantData& resultData)
    206 {
    207     if (!NP_CLASS_STRUCT_VERSION_HAS_CTOR(m_npObject->_class) || !m_npObject->_class->construct) {
    208         returnValue = false;
    209         return;
    210     }
    211 
    212     Vector<NPVariant> arguments;
    213     for (size_t i = 0; i < argumentsData.size(); ++i)
    214         arguments.append(m_npRemoteObjectMap->npVariantDataToNPVariant(argumentsData[i], m_plugin));
    215 
    216     NPVariant result;
    217     VOID_TO_NPVARIANT(result);
    218 
    219     returnValue = m_npObject->_class->construct(m_npObject, arguments.data(), arguments.size(), &result);
    220     if (returnValue) {
    221         // Convert the NPVariant to an NPVariantData.
    222         resultData = m_npRemoteObjectMap->npVariantToNPVariantData(result, m_plugin);
    223     }
    224 
    225     // Release all arguments.
    226     for (size_t i = 0; i < argumentsData.size(); ++i)
    227         releaseNPVariantValue(&arguments[i]);
    228 
    229     // And release the result.
    230     releaseNPVariantValue(&result);
    231 }
    232 
    233 } // namespace WebKit
    234 
    235 #endif // ENABLE(PLUGIN_PROCESS)
    236 
    237