Advertisement
Guest User

Patch: QtWebKit npapi plugin treats int32 parameter as double

a guest
Jul 4th, 2010
255
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.91 KB | None | 0 0
  1. /*
  2.  * Copyright (C) 2004, 2006 Apple Computer, Inc.  All rights reserved.
  3.  * Copyright (C) 2006 Alexey Proskuryakov (ap@nypop.com)
  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.  
  29. #if ENABLE(NETSCAPE_PLUGIN_API)
  30.  
  31. #include "c_utility.h"
  32.  
  33. #include "JSDOMWindow.h"
  34. #include "NP_jsobject.h"
  35. #include "c_instance.h"
  36. #include <runtime/JSGlobalObject.h>
  37. #include <runtime/JSLock.h>
  38. #include "PlatformString.h"
  39. #include "npruntime_impl.h"
  40. #include "npruntime_priv.h"
  41. #include "runtime_object.h"
  42. #include "runtime_root.h"
  43. #include <wtf/Assertions.h>
  44.  
  45. using WebCore::String;
  46.  
  47. namespace JSC { namespace Bindings {
  48.  
  49. static String convertUTF8ToUTF16WithLatin1Fallback(const NPUTF8* UTF8Chars, int UTF8Length)
  50. {
  51.     ASSERT(UTF8Chars || UTF8Length == 0);
  52.    
  53.     if (UTF8Length == -1)
  54.         UTF8Length = static_cast<int>(strlen(UTF8Chars));
  55.  
  56.     String result = String::fromUTF8(UTF8Chars, UTF8Length);
  57.    
  58.     // If we got back a null string indicating an unsuccessful conversion, fall back to latin 1.
  59.     // Some plugins return invalid UTF-8 in NPVariantType_String, see <http://bugs.webkit.org/show_bug.cgi?id=5163>
  60.     // There is no "bad data" for latin1. It is unlikely that the plugin was really sending text in this encoding,
  61.     // but it should have used UTF-8, and now we are simply avoiding a crash.
  62.     if (result.isNull())
  63.         result = String(UTF8Chars, UTF8Length);
  64.    
  65.     return result;
  66. }
  67.  
  68. // Variant value must be released with NPReleaseVariantValue()
  69. void convertValueToNPVariant(ExecState* exec, JSValue value, NPVariant* result)
  70. {
  71.     JSLock lock(SilenceAssertionsOnly);
  72.  
  73.     VOID_TO_NPVARIANT(*result);
  74.  
  75.     if (value.isString()) {
  76.         UString ustring = value.toString(exec);
  77.         CString cstring = ustring.UTF8String();
  78.         NPString string = { (const NPUTF8*)cstring.c_str(), static_cast<uint32_t>(cstring.size()) };
  79.         NPN_InitializeVariantWithStringCopy(result, &string);
  80.     } else if (value.isInt32()) {
  81.         INT32_TO_NPVARIANT(value.toNumber(exec), *result);
  82.     } else if (value.isNumber()) {
  83.         DOUBLE_TO_NPVARIANT(value.toNumber(exec), *result);
  84.     } else if (value.isBoolean()) {
  85.         BOOLEAN_TO_NPVARIANT(value.toBoolean(exec), *result);
  86.     } else if (value.isNull()) {
  87.         NULL_TO_NPVARIANT(*result);
  88.     } else if (value.isObject()) {
  89.         JSObject* object = asObject(value);
  90.         if (object->classInfo() == &RuntimeObjectImp::s_info) {
  91.             RuntimeObjectImp* imp = static_cast<RuntimeObjectImp*>(object);
  92.             CInstance* instance = static_cast<CInstance*>(imp->getInternalInstance());
  93.             if (instance) {
  94.                 NPObject* obj = instance->getObject();
  95.                 _NPN_RetainObject(obj);
  96.                 OBJECT_TO_NPVARIANT(obj, *result);
  97.             }
  98.         } else {
  99.             JSGlobalObject* globalObject = exec->dynamicGlobalObject();
  100.  
  101.             RootObject* rootObject = findRootObject(globalObject);
  102.             if (rootObject) {
  103.                 NPObject* npObject = _NPN_CreateScriptObject(0, object, rootObject);
  104.                 OBJECT_TO_NPVARIANT(npObject, *result);
  105.             }
  106.         }
  107.     }
  108. }
  109.  
  110. JSValue convertNPVariantToValue(ExecState* exec, const NPVariant* variant, RootObject* rootObject)
  111. {
  112.     JSLock lock(SilenceAssertionsOnly);
  113.    
  114.     NPVariantType type = variant->type;
  115.  
  116.     if (type == NPVariantType_Bool)
  117.         return jsBoolean(NPVARIANT_TO_BOOLEAN(*variant));
  118.     if (type == NPVariantType_Null)
  119.         return jsNull();
  120.     if (type == NPVariantType_Void)
  121.         return jsUndefined();
  122.     if (type == NPVariantType_Int32)
  123.         return jsNumber(exec, NPVARIANT_TO_INT32(*variant));
  124.     if (type == NPVariantType_Double)
  125.         return jsNumber(exec, NPVARIANT_TO_DOUBLE(*variant));
  126.     if (type == NPVariantType_String)
  127.         return jsString(exec, convertNPStringToUTF16(&variant->value.stringValue));
  128.     if (type == NPVariantType_Object) {
  129.         NPObject* obj = variant->value.objectValue;
  130.        
  131.         if (obj->_class == NPScriptObjectClass)
  132.             // Get JSObject from NP_JavaScriptObject.
  133.             return ((JavaScriptObject*)obj)->imp;
  134.  
  135.         // Wrap NPObject in a CInstance.
  136.         return CInstance::create(obj, rootObject)->createRuntimeObject(exec);
  137.     }
  138.    
  139.     return jsUndefined();
  140. }
  141.  
  142. String convertNPStringToUTF16(const NPString* string)
  143. {
  144.     return String::fromUTF8WithLatin1Fallback(string->UTF8Characters, string->UTF8Length);
  145. }
  146.  
  147. Identifier identifierFromNPIdentifier(const NPUTF8* name)
  148. {
  149.     return Identifier(WebCore::JSDOMWindow::commonJSGlobalData(), convertUTF8ToUTF16WithLatin1Fallback(name, -1));
  150. }
  151.  
  152. } }
  153.  
  154. #endif // ENABLE(NETSCAPE_PLUGIN_API)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement