| OLD | NEW |
| 1 // Copyright (c) 2008, Google Inc. | 1 /* |
| 2 // All rights reserved. | 2 * Copyright (C) 2008, 2009 Google Inc. All rights reserved. |
| 3 // | 3 * |
| 4 // Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
| 5 // modification, are permitted provided that the following conditions are | 5 * modification, are permitted provided that the following conditions are |
| 6 // met: | 6 * met: |
| 7 // | 7 * |
| 8 // * Redistributions of source code must retain the above copyright | 8 * * Redistributions of source code must retain the above copyright |
| 9 // notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
| 10 // * Redistributions in binary form must reproduce the above | 10 * * Redistributions in binary form must reproduce the above |
| 11 // copyright notice, this list of conditions and the following disclaimer | 11 * copyright notice, this list of conditions and the following disclaimer |
| 12 // in the documentation and/or other materials provided with the | 12 * in the documentation and/or other materials provided with the |
| 13 // distribution. | 13 * distribution. |
| 14 // * Neither the name of Google Inc. nor the names of its | 14 * * Neither the name of Google Inc. nor the names of its |
| 15 // contributors may be used to endorse or promote products derived from | 15 * contributors may be used to endorse or promote products derived from |
| 16 // this software without specific prior written permission. | 16 * this software without specific prior written permission. |
| 17 // | 17 * |
| 18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | 19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | 20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | 21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | 22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | 23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 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. | 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 */ |
| 29 | 30 |
| 30 #include "config.h" | 31 #include "config.h" |
| 31 | 32 |
| 32 #include "V8NPUtils.h" | 33 #include "V8NPUtils.h" |
| 33 | 34 |
| 34 #include "DOMWindow.h" | 35 #include "DOMWindow.h" |
| 35 #include "Frame.h" | 36 #include "Frame.h" |
| 36 #include "PlatformString.h" | 37 #include "PlatformString.h" |
| 37 #undef LOG | 38 #undef LOG |
| 38 | 39 |
| 39 #include "npruntime_priv.h" | |
| 40 #include "NPV8Object.h" | 40 #include "NPV8Object.h" |
| 41 #include "V8NPObject.h" | 41 #include "V8NPObject.h" |
| 42 #include "V8Proxy.h" | 42 #include "V8Proxy.h" |
| 43 #include "npruntime_priv.h" |
| 43 | 44 |
| 44 void convertV8ObjectToNPVariant(v8::Local<v8::Value> object, NPObject *owner, NP
Variant* result) | 45 void convertV8ObjectToNPVariant(v8::Local<v8::Value> object, NPObject* owner, NP
Variant* result) |
| 45 { | 46 { |
| 46 VOID_TO_NPVARIANT(*result); | 47 VOID_TO_NPVARIANT(*result); |
| 47 | 48 |
| 48 // It is really the caller's responsibility to deal with the empty handle | 49 // It is really the caller's responsibility to deal with the empty handle ca
se because there could be different actions to |
| 49 // case because there could be different actions to take in different | 50 // take in different contexts. |
| 50 // contexts. | |
| 51 ASSERT(!object.IsEmpty()); | 51 ASSERT(!object.IsEmpty()); |
| 52 | 52 |
| 53 if (object.IsEmpty()) | 53 if (object.IsEmpty()) |
| 54 return; | 54 return; |
| 55 | 55 |
| 56 if (object->IsInt32()) | 56 if (object->IsInt32()) |
| 57 INT32_TO_NPVARIANT(object->NumberValue(), *result); | 57 INT32_TO_NPVARIANT(object->NumberValue(), *result); |
| 58 else if (object->IsNumber()) | 58 else if (object->IsNumber()) |
| 59 DOUBLE_TO_NPVARIANT(object->NumberValue(), *result); | 59 DOUBLE_TO_NPVARIANT(object->NumberValue(), *result); |
| 60 else if (object->IsBoolean()) | 60 else if (object->IsBoolean()) |
| 61 BOOLEAN_TO_NPVARIANT(object->BooleanValue(), *result); | 61 BOOLEAN_TO_NPVARIANT(object->BooleanValue(), *result); |
| 62 else if (object->IsNull()) | 62 else if (object->IsNull()) |
| 63 NULL_TO_NPVARIANT(*result); | 63 NULL_TO_NPVARIANT(*result); |
| 64 else if (object->IsUndefined()) | 64 else if (object->IsUndefined()) |
| 65 VOID_TO_NPVARIANT(*result); | 65 VOID_TO_NPVARIANT(*result); |
| 66 else if (object->IsString()) { | 66 else if (object->IsString()) { |
| 67 v8::String::Utf8Value utf8(object); | 67 v8::String::Utf8Value utf8(object); |
| 68 char* utf8_chars = strdup(*utf8); | 68 char* utf8_chars = strdup(*utf8); |
| 69 STRINGN_TO_NPVARIANT(utf8_chars, utf8.length(), *result); | 69 STRINGN_TO_NPVARIANT(utf8_chars, utf8.length(), *result); |
| 70 } else if (object->IsObject()) { | 70 } else if (object->IsObject()) { |
| 71 WebCore::DOMWindow* window = WebCore::V8Proxy::retrieveWindow(); | 71 WebCore::DOMWindow* window = WebCore::V8Proxy::retrieveWindow(); |
| 72 NPObject* npobject = npCreateV8ScriptObject(0, v8::Handle<v8::Object>::C
ast(object), window); | 72 NPObject* npobject = npCreateV8ScriptObject(0, v8::Handle<v8::Object>::C
ast(object), window); |
| 73 if (npobject) | 73 if (npobject) |
| 74 _NPN_RegisterObject(npobject, owner); | 74 _NPN_RegisterObject(npobject, owner); |
| 75 OBJECT_TO_NPVARIANT(npobject, *result); | 75 OBJECT_TO_NPVARIANT(npobject, *result); |
| 76 } | 76 } |
| 77 } | 77 } |
| 78 | 78 |
| 79 | 79 |
| 80 v8::Handle<v8::Value> convertNPVariantToV8Object(const NPVariant* variant, NPObj
ect* npobject) | 80 v8::Handle<v8::Value> convertNPVariantToV8Object(const NPVariant* variant, NPObj
ect* npobject) |
| 81 { | 81 { |
| 82 NPVariantType type = variant->type; | 82 NPVariantType type = variant->type; |
| 83 | 83 |
| 84 if (type == NPVariantType_Int32) | 84 switch (type) { |
| 85 case NPVariantType_Int32: |
| 85 return v8::Integer::New(NPVARIANT_TO_INT32(*variant)); | 86 return v8::Integer::New(NPVARIANT_TO_INT32(*variant)); |
| 86 if (type == NPVariantType_Double) | 87 case NPVariantType_Double: |
| 87 return v8::Number::New(NPVARIANT_TO_DOUBLE(*variant)); | 88 return v8::Number::New(NPVARIANT_TO_DOUBLE(*variant)); |
| 88 if (type == NPVariantType_Bool) | 89 case NPVariantType_Bool: |
| 89 return NPVARIANT_TO_BOOLEAN(*variant) ? v8::True() : v8::False(); | 90 return NPVARIANT_TO_BOOLEAN(*variant) ? v8::True() : v8::False(); |
| 90 if (type == NPVariantType_Null) | 91 case NPVariantType_Null: |
| 91 return v8::Null(); | 92 return v8::Null(); |
| 92 if (type == NPVariantType_Void) | 93 case NPVariantType_Void: |
| 93 return v8::Undefined(); | 94 return v8::Undefined(); |
| 94 if (type == NPVariantType_String) { | 95 case NPVariantType_String: { |
| 95 NPString src = NPVARIANT_TO_STRING(*variant); | 96 NPString src = NPVARIANT_TO_STRING(*variant); |
| 96 return v8::String::New(src.UTF8Characters, src.UTF8Length); | 97 return v8::String::New(src.UTF8Characters, src.UTF8Length); |
| 97 } | 98 } |
| 98 if (type == NPVariantType_Object) { | 99 case NPVariantType_Object: { |
| 99 NPObject* obj = NPVARIANT_TO_OBJECT(*variant); | 100 NPObject* obj = NPVARIANT_TO_OBJECT(*variant); |
| 100 if (obj->_class == npScriptObjectClass) | 101 if (obj->_class == npScriptObjectClass) |
| 101 return reinterpret_cast<V8NPObject*>(obj)->v8Object; | 102 return reinterpret_cast<V8NPObject*>(obj)->v8Object; |
| 102 return createV8ObjectForNPObject(obj, npobject); | 103 return createV8ObjectForNPObject(obj, npobject); |
| 103 } | 104 } |
| 104 return v8::Undefined(); | 105 default: |
| 106 return v8::Undefined(); |
| 107 } |
| 105 } | 108 } |
| 106 | 109 |
| 107 // Helper function to create an NPN String Identifier from a v8 string. | 110 // Helper function to create an NPN String Identifier from a v8 string. |
| 108 NPIdentifier getStringIdentifier(v8::Handle<v8::String> str) | 111 NPIdentifier getStringIdentifier(v8::Handle<v8::String> str) |
| 109 { | 112 { |
| 110 const int kStackBufSize = 100; | 113 const int kStackBufferSize = 100; |
| 111 | 114 |
| 112 int bufLen = str->Length() + 1; | 115 int bufferLength = str->Length() + 1; |
| 113 if (bufLen <= kStackBufSize) { | 116 if (bufferLength <= kStackBufferSize) { |
| 114 // Use local stack buffer to avoid heap allocations for small strings. | 117 // Use local stack buffer to avoid heap allocations for small strings. H
ere we should only use the stack space for |
| 115 // Here we should only use the stack space for stack_buf when it's used, | 118 // stackBuffer when it's used, not when we use the heap. |
| 116 // not when we use the heap. | 119 // |
| 117 char stackBuf[kStackBufSize]; | 120 // WriteAscii is guaranteed to generate a null-terminated string because
bufferLength is constructed to be one greater |
| 118 str->WriteAscii(stackBuf); | 121 // than the string length. |
| 119 return NPN_GetStringIdentifier(stackBuf); | 122 char stackBuffer[kStackBufferSize]; |
| 123 str->WriteAscii(stackBuffer, 0, bufferLength); |
| 124 return NPN_GetStringIdentifier(stackBuffer); |
| 120 } | 125 } |
| 121 | 126 |
| 122 v8::String::AsciiValue ascii(str); | 127 v8::String::AsciiValue ascii(str); |
| 123 return NPN_GetStringIdentifier(*ascii); | 128 return NPN_GetStringIdentifier(*ascii); |
| 124 } | 129 } |
| OLD | NEW |