• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2008, 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 
33 #include "V8NPUtils.h"
34 
35 #include "DOMWindow.h"
36 #include "Frame.h"
37 #include "PlatformString.h"
38 #undef LOG
39 
40 #include "NPV8Object.h"
41 #include "V8NPObject.h"
42 #include "V8Proxy.h"
43 #include "npruntime_impl.h"
44 #include "npruntime_priv.h"
45 
convertV8ObjectToNPVariant(v8::Local<v8::Value> object,NPObject * owner,NPVariant * result)46 void convertV8ObjectToNPVariant(v8::Local<v8::Value> object, NPObject* owner, NPVariant* result)
47 {
48     VOID_TO_NPVARIANT(*result);
49 
50     // It is really the caller's responsibility to deal with the empty handle case because there could be different actions to
51     // take in different contexts.
52     ASSERT(!object.IsEmpty());
53 
54     if (object.IsEmpty())
55         return;
56 
57     if (object->IsInt32())
58         INT32_TO_NPVARIANT(object->NumberValue(), *result);
59     else if (object->IsNumber())
60         DOUBLE_TO_NPVARIANT(object->NumberValue(), *result);
61     else if (object->IsBoolean())
62         BOOLEAN_TO_NPVARIANT(object->BooleanValue(), *result);
63     else if (object->IsNull())
64         NULL_TO_NPVARIANT(*result);
65     else if (object->IsUndefined())
66         VOID_TO_NPVARIANT(*result);
67     else if (object->IsString()) {
68         v8::String::Utf8Value utf8(object);
69         char* utf8_chars = strdup(*utf8);
70         STRINGN_TO_NPVARIANT(utf8_chars, utf8.length(), *result);
71     } else if (object->IsObject()) {
72         WebCore::DOMWindow* window = WebCore::V8Proxy::retrieveWindow(WebCore::V8Proxy::currentContext());
73         NPObject* npobject = npCreateV8ScriptObject(0, v8::Handle<v8::Object>::Cast(object), window);
74         if (npobject)
75             _NPN_RegisterObject(npobject, owner);
76         OBJECT_TO_NPVARIANT(npobject, *result);
77     }
78 }
79 
80 
convertNPVariantToV8Object(const NPVariant * variant,NPObject * npobject)81 v8::Handle<v8::Value> convertNPVariantToV8Object(const NPVariant* variant, NPObject* npobject)
82 {
83     NPVariantType type = variant->type;
84 
85     switch (type) {
86     case NPVariantType_Int32:
87         return v8::Integer::New(NPVARIANT_TO_INT32(*variant));
88     case NPVariantType_Double:
89         return v8::Number::New(NPVARIANT_TO_DOUBLE(*variant));
90     case NPVariantType_Bool:
91         return NPVARIANT_TO_BOOLEAN(*variant) ? v8::True() : v8::False();
92     case NPVariantType_Null:
93         return v8::Null();
94     case NPVariantType_Void:
95         return v8::Undefined();
96     case NPVariantType_String: {
97         NPString src = NPVARIANT_TO_STRING(*variant);
98         return v8::String::New(src.UTF8Characters, src.UTF8Length);
99     }
100     case NPVariantType_Object: {
101         NPObject* obj = NPVARIANT_TO_OBJECT(*variant);
102         if (obj->_class == npScriptObjectClass)
103             return reinterpret_cast<V8NPObject*>(obj)->v8Object;
104         return createV8ObjectForNPObject(obj, npobject);
105     }
106     default:
107         return v8::Undefined();
108     }
109 }
110 
111 // Helper function to create an NPN String Identifier from a v8 string.
getStringIdentifier(v8::Handle<v8::String> str)112 NPIdentifier getStringIdentifier(v8::Handle<v8::String> str)
113 {
114     const int kStackBufferSize = 100;
115 
116     int bufferLength = str->Utf8Length() + 1;
117     if (bufferLength <= kStackBufferSize) {
118         // Use local stack buffer to avoid heap allocations for small strings. Here we should only use the stack space for
119         // stackBuffer when it's used, not when we use the heap.
120         //
121         // WriteUtf8 is guaranteed to generate a null-terminated string because bufferLength is constructed to be one greater
122         // than the string length.
123         char stackBuffer[kStackBufferSize];
124         str->WriteUtf8(stackBuffer, bufferLength);
125         return _NPN_GetStringIdentifier(stackBuffer);
126     }
127 
128     v8::String::Utf8Value utf8(str);
129     return _NPN_GetStringIdentifier(*utf8);
130 }
131