1 /* 2 * Copyright (C) 2012 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 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'' AND ANY 14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 15 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 16 * DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY 17 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 18 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 19 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 20 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 22 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 23 * 24 */ 25 26 #ifndef V8ArrayBufferCustom_h 27 #define V8ArrayBufferCustom_h 28 29 #include "bindings/v8/V8Binding.h" 30 #include "bindings/v8/V8DOMWrapper.h" 31 #include "bindings/v8/WrapperTypeInfo.h" 32 33 #include "wtf/ArrayBuffer.h" 34 35 #include <v8.h> 36 37 namespace WebCore { 38 39 class V8ArrayBufferDeallocationObserver: public WTF::ArrayBufferDeallocationObserver { 40 public: 41 virtual void ArrayBufferDeallocated(unsigned sizeInBytes) 42 { 43 v8::V8::AdjustAmountOfExternalAllocatedMemory(-static_cast<int>(sizeInBytes)); 44 } 45 static V8ArrayBufferDeallocationObserver* instance(); 46 }; 47 48 class V8ArrayBuffer { 49 public: 50 static bool HasInstance(v8::Handle<v8::Value>, v8::Isolate*, WrapperWorldType); 51 static bool HasInstanceInAnyWorld(v8::Handle<v8::Value>, v8::Isolate*); 52 static ArrayBuffer* toNative(v8::Handle<v8::Object>); 53 static void derefObject(void*); 54 static WrapperTypeInfo info; 55 static const int internalFieldCount = v8DefaultWrapperInternalFieldCount; 56 static void installPerContextProperties(v8::Handle<v8::Object>, ArrayBuffer*, v8::Isolate*) { } 57 static void installPerContextPrototypeProperties(v8::Handle<v8::Object>, v8::Isolate*) { } 58 59 static inline void* toInternalPointer(ArrayBuffer* impl) 60 { 61 return impl; 62 } 63 64 static inline ArrayBuffer* fromInternalPointer(void* impl) 65 { 66 return static_cast<ArrayBuffer*>(impl); 67 } 68 69 private: 70 friend v8::Handle<v8::Object> wrap(ArrayBuffer*, v8::Handle<v8::Object> creationContext, v8::Isolate*); 71 static v8::Handle<v8::Object> createWrapper(PassRefPtr<ArrayBuffer>, v8::Handle<v8::Object> creationContext, v8::Isolate*); 72 }; 73 74 template<> 75 class WrapperTypeTraits<ArrayBuffer > { 76 public: 77 static WrapperTypeInfo* info() { return &V8ArrayBuffer::info; } 78 }; 79 80 81 inline v8::Handle<v8::Object> wrap(ArrayBuffer* impl, v8::Handle<v8::Object> creationContext, v8::Isolate* isolate) 82 { 83 ASSERT(impl); 84 ASSERT(DOMDataStore::getWrapper<V8ArrayBuffer>(impl, isolate).IsEmpty()); 85 return V8ArrayBuffer::createWrapper(impl, creationContext, isolate); 86 } 87 88 inline v8::Handle<v8::Value> toV8(ArrayBuffer* impl, v8::Handle<v8::Object> creationContext, v8::Isolate* isolate) 89 { 90 if (UNLIKELY(!impl)) 91 return v8NullWithCheck(isolate); 92 v8::Handle<v8::Value> wrapper = DOMDataStore::getWrapper<V8ArrayBuffer>(impl, isolate); 93 if (!wrapper.IsEmpty()) 94 return wrapper; 95 return wrap(impl, creationContext, isolate); 96 } 97 98 inline v8::Handle<v8::Value> toV8ForMainWorld(ArrayBuffer* impl, v8::Handle<v8::Object> creationContext, v8::Isolate* isolate) 99 { 100 ASSERT(worldType(isolate) == MainWorld); 101 if (UNLIKELY(!impl)) 102 return v8NullWithCheck(isolate); 103 v8::Handle<v8::Value> wrapper = DOMDataStore::getWrapperForMainWorld<V8ArrayBuffer>(impl); 104 if (!wrapper.IsEmpty()) 105 return wrapper; 106 return wrap(impl, creationContext, isolate); 107 } 108 109 template<class CallbackInfo, class Wrappable> 110 inline v8::Handle<v8::Value> toV8Fast(ArrayBuffer* impl, const CallbackInfo& callbackInfo, Wrappable* wrappable) 111 { 112 if (UNLIKELY(!impl)) 113 return v8::Null(callbackInfo.GetIsolate()); 114 v8::Handle<v8::Object> wrapper = DOMDataStore::getWrapperFast<V8ArrayBuffer>(impl, callbackInfo, wrappable); 115 if (!wrapper.IsEmpty()) 116 return wrapper; 117 return wrap(impl, callbackInfo.Holder(), callbackInfo.GetIsolate()); 118 } 119 120 template<class CallbackInfo, class Wrappable> 121 inline v8::Handle<v8::Value> toV8Fast(PassRefPtr< ArrayBuffer > impl, const CallbackInfo& callbackInfo, Wrappable* wrappable) 122 { 123 return toV8Fast(impl.get(), callbackInfo, wrappable); 124 } 125 126 inline v8::Handle<v8::Value> toV8(PassRefPtr< ArrayBuffer > impl, v8::Handle<v8::Object> creationContext, v8::Isolate* isolate) 127 { 128 return toV8(impl.get(), creationContext, isolate); 129 } 130 131 template<class CallbackInfo, class Wrappable> 132 inline v8::Handle<v8::Value> toV8ForMainWorld(PassRefPtr< ArrayBuffer > impl, v8::Handle<v8::Object> creationContext, v8::Isolate* isolate) 133 { 134 return toV8ForMainWorld(impl.get(), creationContext, isolate); 135 } 136 137 } // namespace WebCore 138 139 #endif // V8ArrayBufferCustom_h 140