Home | History | Annotate | Download | only in web
      1 /*
      2  * Copyright (C) 2009 Google 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 are
      6  * met:
      7  *
      8  *     * Redistributions of source code must retain the above copyright
      9  * notice, this list of conditions and the following disclaimer.
     10  *     * Redistributions in binary form must reproduce the above
     11  * copyright notice, this list of conditions and the following disclaimer
     12  * in the documentation and/or other materials provided with the
     13  * distribution.
     14  *     * Neither the name of Google Inc. nor the names of its
     15  * contributors may be used to endorse or promote products derived from
     16  * this software without specific prior written permission.
     17  *
     18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
     21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     22  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     23  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
     24  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     29  */
     30 
     31 #include "config.h"
     32 #include "WebBindings.h"
     33 
     34 #include "V8Element.h"
     35 #include "V8Range.h"
     36 #include "WebArrayBufferView.h"
     37 #include "WebElement.h"
     38 #include "WebRange.h"
     39 #include "bindings/v8/NPV8Object.h"  // for PrivateIdentifier
     40 #include "bindings/v8/ScriptController.h"
     41 #include "bindings/v8/V8DOMWrapper.h"
     42 #include "bindings/v8/V8NPUtils.h"
     43 #include "bindings/v8/custom/V8ArrayBufferCustom.h"
     44 #include "bindings/v8/custom/V8ArrayBufferViewCustom.h"
     45 #include "bindings/v8/npruntime_impl.h"
     46 #include "bindings/v8/npruntime_priv.h"
     47 #include "core/dom/Range.h"
     48 #include "core/page/DOMWindow.h"
     49 #include "core/page/Frame.h"
     50 #include "public/platform/WebArrayBuffer.h"
     51 #include "wtf/ArrayBufferView.h"
     52 
     53 using namespace WebCore;
     54 
     55 namespace WebKit {
     56 
     57 bool WebBindings::construct(NPP npp, NPObject* object, const NPVariant* args, uint32_t argCount, NPVariant* result)
     58 {
     59     return _NPN_Construct(npp, object, args, argCount, result);
     60 }
     61 
     62 NPObject* WebBindings::createObject(NPP npp, NPClass* npClass)
     63 {
     64     return _NPN_CreateObject(npp, npClass);
     65 }
     66 
     67 bool WebBindings::enumerate(NPP npp, NPObject* object, NPIdentifier** identifier, uint32_t* identifierCount)
     68 {
     69     return _NPN_Enumerate(npp, object, identifier, identifierCount);
     70 }
     71 
     72 bool WebBindings::evaluate(NPP npp, NPObject* object, NPString* script, NPVariant* result)
     73 {
     74     return _NPN_Evaluate(npp, object, script, result);
     75 }
     76 
     77 bool WebBindings::evaluateHelper(NPP npp, bool popupsAllowed, NPObject* object, NPString* script, NPVariant* result)
     78 {
     79     return _NPN_EvaluateHelper(npp, popupsAllowed, object, script, result);
     80 }
     81 
     82 NPIdentifier WebBindings::getIntIdentifier(int32_t number)
     83 {
     84     return _NPN_GetIntIdentifier(number);
     85 }
     86 
     87 bool WebBindings::getProperty(NPP npp, NPObject* object, NPIdentifier property, NPVariant* result)
     88 {
     89     return _NPN_GetProperty(npp, object, property, result);
     90 }
     91 
     92 NPIdentifier WebBindings::getStringIdentifier(const NPUTF8* string)
     93 {
     94     return _NPN_GetStringIdentifier(string);
     95 }
     96 
     97 void WebBindings::getStringIdentifiers(const NPUTF8** names, int32_t nameCount, NPIdentifier* identifiers)
     98 {
     99     _NPN_GetStringIdentifiers(names, nameCount, identifiers);
    100 }
    101 
    102 bool WebBindings::hasMethod(NPP npp, NPObject* object, NPIdentifier method)
    103 {
    104     return _NPN_HasMethod(npp, object, method);
    105 }
    106 
    107 bool WebBindings::hasProperty(NPP npp, NPObject* object, NPIdentifier property)
    108 {
    109     return _NPN_HasProperty(npp, object, property);
    110 }
    111 
    112 bool WebBindings::identifierIsString(NPIdentifier identifier)
    113 {
    114     return _NPN_IdentifierIsString(identifier);
    115 }
    116 
    117 int32_t WebBindings::intFromIdentifier(NPIdentifier identifier)
    118 {
    119     return _NPN_IntFromIdentifier(identifier);
    120 }
    121 
    122 void WebBindings::initializeVariantWithStringCopy(NPVariant* variant, const NPString* value)
    123 {
    124     _NPN_InitializeVariantWithStringCopy(variant, value);
    125 }
    126 
    127 bool WebBindings::invoke(NPP npp, NPObject* object, NPIdentifier method, const NPVariant* args, uint32_t argCount, NPVariant* result)
    128 {
    129     return _NPN_Invoke(npp, object, method, args, argCount, result);
    130 }
    131 
    132 bool WebBindings::invokeDefault(NPP npp, NPObject* object, const NPVariant* args, uint32_t argCount, NPVariant* result)
    133 {
    134     return _NPN_InvokeDefault(npp, object, args, argCount, result);
    135 }
    136 
    137 void WebBindings::releaseObject(NPObject* object)
    138 {
    139     return _NPN_ReleaseObject(object);
    140 }
    141 
    142 void WebBindings::releaseVariantValue(NPVariant* variant)
    143 {
    144     _NPN_ReleaseVariantValue(variant);
    145 }
    146 
    147 bool WebBindings::removeProperty(NPP npp, NPObject* object, NPIdentifier identifier)
    148 {
    149     return _NPN_RemoveProperty(npp, object, identifier);
    150 }
    151 
    152 NPObject* WebBindings::retainObject(NPObject* object)
    153 {
    154     return _NPN_RetainObject(object);
    155 }
    156 
    157 void WebBindings::setException(NPObject* object, const NPUTF8* message)
    158 {
    159     _NPN_SetException(object, message);
    160 }
    161 
    162 bool WebBindings::setProperty(NPP npp, NPObject* object, NPIdentifier identifier, const NPVariant* value)
    163 {
    164     return _NPN_SetProperty(npp, object, identifier, value);
    165 }
    166 
    167 void WebBindings::registerObjectOwner(NPP)
    168 {
    169 }
    170 
    171 void WebBindings::unregisterObjectOwner(NPP)
    172 {
    173 }
    174 
    175 NPP WebBindings::getObjectOwner(NPObject*)
    176 {
    177     return 0;
    178 }
    179 
    180 void WebBindings::unregisterObject(NPObject* object)
    181 {
    182     _NPN_UnregisterObject(object);
    183 }
    184 
    185 NPUTF8* WebBindings::utf8FromIdentifier(NPIdentifier identifier)
    186 {
    187     return _NPN_UTF8FromIdentifier(identifier);
    188 }
    189 
    190 void WebBindings::extractIdentifierData(const NPIdentifier& identifier, const NPUTF8*& string, int32_t& number, bool& isString)
    191 {
    192     PrivateIdentifier* data = static_cast<PrivateIdentifier*>(identifier);
    193     if (!data) {
    194         isString = false;
    195         number = 0;
    196         return;
    197     }
    198 
    199     isString = data->isString;
    200     if (isString)
    201         string = data->value.string;
    202     else
    203         number = data->value.number;
    204 }
    205 
    206 static bool getRangeImpl(NPObject* object, WebRange* webRange, v8::Isolate* isolate)
    207 {
    208     if (!object)
    209         return false;
    210 
    211     V8NPObject* v8NPObject = npObjectToV8NPObject(object);
    212     if (!v8NPObject)
    213         return false;
    214 
    215     v8::HandleScope handleScope(isolate);
    216     v8::Handle<v8::Object> v8Object = v8::Local<v8::Object>::New(isolate, v8NPObject->v8Object);
    217     if (v8Object.IsEmpty())
    218         return false;
    219     if (!V8Range::info.equals(toWrapperTypeInfo(v8Object)))
    220         return false;
    221 
    222     Range* native = V8Range::HasInstanceInAnyWorld(v8Object, isolate) ? V8Range::toNative(v8Object) : 0;
    223     if (!native)
    224         return false;
    225 
    226     *webRange = WebRange(native);
    227     return true;
    228 }
    229 
    230 static bool getNodeImpl(NPObject* object, WebNode* webNode, v8::Isolate* isolate)
    231 {
    232     if (!object)
    233         return false;
    234 
    235     V8NPObject* v8NPObject = npObjectToV8NPObject(object);
    236     if (!v8NPObject)
    237         return false;
    238 
    239     v8::HandleScope handleScope(isolate);
    240     v8::Handle<v8::Object> v8Object = v8::Local<v8::Object>::New(isolate, v8NPObject->v8Object);
    241     if (v8Object.IsEmpty())
    242         return false;
    243     Node* native = V8Node::HasInstanceInAnyWorld(v8Object, isolate) ? V8Node::toNative(v8Object) : 0;
    244     if (!native)
    245         return false;
    246 
    247     *webNode = WebNode(native);
    248     return true;
    249 }
    250 
    251 static bool getElementImpl(NPObject* object, WebElement* webElement, v8::Isolate* isolate)
    252 {
    253     if (!object)
    254         return false;
    255 
    256     V8NPObject* v8NPObject = npObjectToV8NPObject(object);
    257     if (!v8NPObject)
    258         return false;
    259 
    260     v8::HandleScope handleScope(isolate);
    261     v8::Handle<v8::Object> v8Object = v8::Local<v8::Object>::New(isolate, v8NPObject->v8Object);
    262     if (v8Object.IsEmpty())
    263         return false;
    264     Element* native = V8Element::HasInstanceInAnyWorld(v8Object, isolate) ? V8Element::toNative(v8Object) : 0;
    265     if (!native)
    266         return false;
    267 
    268     *webElement = WebElement(native);
    269     return true;
    270 }
    271 
    272 static bool getArrayBufferImpl(NPObject* object, WebArrayBuffer* arrayBuffer, v8::Isolate* isolate)
    273 {
    274     if (!object)
    275         return false;
    276 
    277     V8NPObject* v8NPObject = npObjectToV8NPObject(object);
    278     if (!v8NPObject)
    279         return false;
    280 
    281     v8::HandleScope handleScope(isolate);
    282     v8::Handle<v8::Object> v8Object = v8::Local<v8::Object>::New(isolate, v8NPObject->v8Object);
    283     if (v8Object.IsEmpty())
    284         return false;
    285     ArrayBuffer* native = V8ArrayBuffer::HasInstanceInAnyWorld(v8Object, isolate) ? V8ArrayBuffer::toNative(v8Object) : 0;
    286     if (!native)
    287         return false;
    288 
    289     *arrayBuffer = WebArrayBuffer(native);
    290     return true;
    291 }
    292 
    293 static bool getArrayBufferViewImpl(NPObject* object, WebArrayBufferView* arrayBufferView, v8::Isolate* isolate)
    294 {
    295     if (!object)
    296         return false;
    297 
    298     V8NPObject* v8NPObject = npObjectToV8NPObject(object);
    299     if (!v8NPObject)
    300         return false;
    301 
    302     v8::HandleScope handleScope(isolate);
    303     v8::Handle<v8::Object> v8Object = v8::Local<v8::Object>::New(isolate, v8NPObject->v8Object);
    304     if (v8Object.IsEmpty())
    305         return false;
    306     ArrayBufferView* native = V8ArrayBufferView::HasInstanceInAnyWorld(v8Object, isolate) ? V8ArrayBufferView::toNative(v8Object) : 0;
    307     if (!native)
    308         return false;
    309 
    310     *arrayBufferView = WebArrayBufferView(native);
    311     return true;
    312 }
    313 
    314 static NPObject* makeIntArrayImpl(const WebVector<int>& data)
    315 {
    316     v8::HandleScope handleScope;
    317     v8::Handle<v8::Array> result = v8::Array::New(data.size());
    318     for (size_t i = 0; i < data.size(); ++i)
    319         result->Set(i, v8::Number::New(data[i]));
    320 
    321     DOMWindow* window = toDOMWindow(v8::Context::GetCurrent());
    322     return npCreateV8ScriptObject(0, result, window);
    323 }
    324 
    325 static NPObject* makeStringArrayImpl(const WebVector<WebString>& data)
    326 {
    327     v8::Isolate* isolate = v8::Isolate::GetCurrent();
    328     v8::HandleScope handleScope;
    329     v8::Handle<v8::Array> result = v8::Array::New(data.size());
    330     for (size_t i = 0; i < data.size(); ++i)
    331         result->Set(i, v8String(data[i], isolate));
    332 
    333     DOMWindow* window = toDOMWindow(v8::Context::GetCurrent());
    334     return npCreateV8ScriptObject(0, result, window);
    335 }
    336 
    337 bool WebBindings::getRange(NPObject* range, WebRange* webRange)
    338 {
    339     return getRangeImpl(range, webRange, v8::Isolate::GetCurrent());
    340 }
    341 
    342 bool WebBindings::getArrayBuffer(NPObject* arrayBuffer, WebArrayBuffer* webArrayBuffer)
    343 {
    344     return getArrayBufferImpl(arrayBuffer, webArrayBuffer, v8::Isolate::GetCurrent());
    345 }
    346 
    347 bool WebBindings::getArrayBufferView(NPObject* arrayBufferView, WebArrayBufferView* webArrayBufferView)
    348 {
    349     return getArrayBufferViewImpl(arrayBufferView, webArrayBufferView, v8::Isolate::GetCurrent());
    350 }
    351 
    352 bool WebBindings::getNode(NPObject* node, WebNode* webNode)
    353 {
    354     return getNodeImpl(node, webNode, v8::Isolate::GetCurrent());
    355 }
    356 
    357 bool WebBindings::getElement(NPObject* element, WebElement* webElement)
    358 {
    359     return getElementImpl(element, webElement, v8::Isolate::GetCurrent());
    360 }
    361 
    362 NPObject* WebBindings::makeIntArray(const WebVector<int>& data)
    363 {
    364     return makeIntArrayImpl(data);
    365 }
    366 
    367 NPObject* WebBindings::makeStringArray(const WebVector<WebString>& data)
    368 {
    369     return makeStringArrayImpl(data);
    370 }
    371 
    372 void WebBindings::pushExceptionHandler(ExceptionHandler handler, void* data)
    373 {
    374     WebCore::pushExceptionHandler(handler, data);
    375 }
    376 
    377 void WebBindings::popExceptionHandler()
    378 {
    379     WebCore::popExceptionHandler();
    380 }
    381 
    382 void WebBindings::toNPVariant(v8::Local<v8::Value> object, NPObject* root, NPVariant* result)
    383 {
    384     WebCore::convertV8ObjectToNPVariant(object, root, result);
    385 }
    386 
    387 v8::Handle<v8::Value> WebBindings::toV8Value(const NPVariant* variant)
    388 {
    389     v8::Isolate* isolate = v8::Isolate::GetCurrent();
    390     if (variant->type == NPVariantType_Object) {
    391         NPObject* object = NPVARIANT_TO_OBJECT(*variant);
    392         V8NPObject* v8Object = npObjectToV8NPObject(object);
    393         if (!v8Object)
    394             return v8::Undefined();
    395         return convertNPVariantToV8Object(variant, v8Object->rootObject->frame()->script()->windowScriptNPObject(), isolate);
    396     }
    397     // Safe to pass 0 since we have checked the script object class to make sure the
    398     // argument is a primitive v8 type.
    399     return convertNPVariantToV8Object(variant, 0, isolate);
    400 }
    401 
    402 } // namespace WebKit
    403