WebKit2/WebProcess/Plugins/NPJSObjectMap.cpp
changeset 0 4f2f89ce4247
equal deleted inserted replaced
-1:000000000000 0:4f2f89ce4247
       
     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 "NPJSObjectMap.h"
       
    27 
       
    28 #include "NPRuntimeUtilities.h"
       
    29 #include "PluginView.h"
       
    30 #include <WebCore/Frame.h>
       
    31 #include <WebCore/IdentifierRep.h>
       
    32 #include <JavaScriptCore/JSObject.h>
       
    33 #include <JavaScriptCore/Protect.h>
       
    34 #include <WebKit/npruntime.h>
       
    35 #include <wtf/Noncopyable.h>
       
    36 
       
    37 using namespace JSC;
       
    38 using namespace WebCore;
       
    39 
       
    40 namespace WebKit {
       
    41 
       
    42 class NPJSObject : public NPObject, Noncopyable {
       
    43 public:
       
    44     static NPJSObject* create(NPJSObjectMap* objectMap, JSObject* jsObject);
       
    45 
       
    46 private:
       
    47     NPJSObject()
       
    48         : m_objectMap(0)
       
    49     {
       
    50     }
       
    51 
       
    52     ~NPJSObject()
       
    53     {
       
    54         // Remove ourselves from the map.
       
    55         ASSERT(m_objectMap->m_objects.contains(m_jsObject.get()));
       
    56         m_objectMap->m_objects.remove(m_jsObject.get());
       
    57     }
       
    58 
       
    59     static bool isNPJSObject(NPObject*);
       
    60 
       
    61     static NPJSObject* toNPJSObject(NPObject* npObject)
       
    62     {
       
    63         ASSERT(isNPJSObject(npObject));
       
    64         return static_cast<NPJSObject*>(npObject);
       
    65     }
       
    66 
       
    67     void initialize(NPJSObjectMap*, JSObject* jsObject);
       
    68 
       
    69     bool hasProperty(NPIdentifier);
       
    70     bool getProperty(NPIdentifier, NPVariant* result);
       
    71 
       
    72     static NPClass* npClass();
       
    73     static NPObject* NP_Allocate(NPP, NPClass*);
       
    74     static void NP_Deallocate(NPObject*);
       
    75     static bool NP_HasProperty(NPObject* npobj, NPIdentifier name);
       
    76     static bool NP_GetProperty(NPObject* npobj, NPIdentifier name, NPVariant* result);
       
    77     
       
    78     NPJSObjectMap* m_objectMap;
       
    79     ProtectedPtr<JSObject> m_jsObject;
       
    80 };
       
    81 
       
    82 NPJSObject* NPJSObject::create(NPJSObjectMap* objectMap, JSObject* jsObject)
       
    83 {
       
    84     NPJSObject* npJSObject = toNPJSObject(createNPObject(0, npClass()));
       
    85     npJSObject->initialize(objectMap, jsObject);
       
    86 
       
    87     return npJSObject;
       
    88 }
       
    89 
       
    90 bool NPJSObject::isNPJSObject(NPObject* npObject)
       
    91 {
       
    92     return npObject->_class == npClass();
       
    93 }
       
    94 
       
    95 void NPJSObject::initialize(NPJSObjectMap* objectMap, JSObject* jsObject)
       
    96 {
       
    97     ASSERT(!m_objectMap);
       
    98     ASSERT(!m_jsObject);
       
    99 
       
   100     m_objectMap = objectMap;
       
   101     m_jsObject = jsObject;
       
   102 }
       
   103 
       
   104 static Identifier identifierFromIdentifierRep(ExecState* exec, IdentifierRep* identifierRep)
       
   105 {
       
   106     ASSERT(identifierRep->isString());
       
   107 
       
   108     const char* string = identifierRep->string();
       
   109     int length = strlen(string);
       
   110 
       
   111     return Identifier(exec, String::fromUTF8WithLatin1Fallback(string, length).impl());
       
   112 }
       
   113 
       
   114 bool NPJSObject::hasProperty(NPIdentifier identifier)
       
   115 {
       
   116     IdentifierRep* identifierRep = static_cast<IdentifierRep*>(identifier);
       
   117     
       
   118     Frame* frame = m_objectMap->m_pluginView->frame();
       
   119     if (!frame)
       
   120         return false;
       
   121 
       
   122     bool result;
       
   123     ExecState* exec = frame->script()->globalObject(pluginWorld())->globalExec();
       
   124     if (identifierRep->isString())
       
   125         result = m_jsObject->hasProperty(exec, identifierFromIdentifierRep(exec, identifierRep));
       
   126     else
       
   127         result = m_jsObject->hasProperty(exec, identifierRep->number());
       
   128 
       
   129     exec->clearException();
       
   130 
       
   131     return result;
       
   132 }
       
   133 
       
   134 bool NPJSObject::getProperty(NPIdentifier identifier, NPVariant* result)
       
   135 {
       
   136     // FIXME: Implement.
       
   137     return false;
       
   138 }
       
   139 
       
   140 NPClass* NPJSObject::npClass()
       
   141 {
       
   142     static NPClass npClass = {
       
   143         NP_CLASS_STRUCT_VERSION,
       
   144         NP_Allocate,
       
   145         NP_Deallocate,
       
   146         0, 
       
   147         0,
       
   148         0,
       
   149         0,
       
   150         NP_HasProperty,
       
   151         NP_GetProperty,
       
   152         0,
       
   153         0,
       
   154         0,
       
   155         0
       
   156     };
       
   157 
       
   158     return &npClass;
       
   159 }
       
   160     
       
   161 NPObject* NPJSObject::NP_Allocate(NPP npp, NPClass* npClass)
       
   162 {
       
   163     ASSERT_UNUSED(npp, !npp);
       
   164 
       
   165     return new NPJSObject;
       
   166 }
       
   167 
       
   168 void NPJSObject::NP_Deallocate(NPObject* npObject)
       
   169 {
       
   170     NPJSObject* npJSObject = toNPJSObject(npObject);
       
   171     delete npJSObject;
       
   172 }
       
   173 
       
   174 bool NPJSObject::NP_HasProperty(NPObject* npObject, NPIdentifier propertyName)
       
   175 {
       
   176     return toNPJSObject(npObject)->hasProperty(propertyName);
       
   177 }
       
   178     
       
   179 bool NPJSObject::NP_GetProperty(NPObject* npObject, NPIdentifier propertyName, NPVariant* result)
       
   180 {
       
   181     return toNPJSObject(npObject)->getProperty(propertyName, result);
       
   182 }
       
   183 
       
   184 NPJSObjectMap::NPJSObjectMap(PluginView* pluginView)
       
   185     : m_pluginView(pluginView)
       
   186 {
       
   187 }
       
   188 
       
   189 NPObject* NPJSObjectMap::getOrCreateObject(JSObject* jsObject)
       
   190 {
       
   191     // First, check if we already know about this object.
       
   192     if (NPJSObject* npJSObject = m_objects.get(jsObject)) {
       
   193         retainNPObject(npJSObject);
       
   194         return npJSObject;
       
   195     }
       
   196 
       
   197     NPJSObject* npJSObject = NPJSObject::create(this, jsObject);
       
   198     m_objects.set(jsObject, npJSObject);
       
   199 
       
   200     return npJSObject;
       
   201 }
       
   202 
       
   203 void NPJSObjectMap::invalidate()
       
   204 {
       
   205     Vector<NPJSObject*> npJSObjects;
       
   206     copyValuesToVector(m_objects, npJSObjects);
       
   207 
       
   208     // Deallocate all the object wrappers so we won't leak any JavaScript objects.
       
   209     for (size_t i = 0; i < npJSObjects.size(); ++i)
       
   210         deallocateNPObject(npJSObjects[i]);
       
   211     
       
   212     // We shouldn't have any objects left now.
       
   213     ASSERT(m_objects.isEmpty());
       
   214 }
       
   215 
       
   216 } // namespace WebKit