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 #include "wtf/ArrayBuffer.h" 33 #include <v8.h> 34 35 namespace WebCore { 36 37 class V8ArrayBufferDeallocationObserver FINAL: public WTF::ArrayBufferDeallocationObserver { 38 public: 39 virtual void arrayBufferDeallocated(unsigned sizeInBytes) OVERRIDE 40 { 41 v8::Isolate::GetCurrent()->AdjustAmountOfExternalAllocatedMemory(-static_cast<int>(sizeInBytes)); 42 } 43 static V8ArrayBufferDeallocationObserver* instanceTemplate(); 44 45 protected: 46 virtual void blinkAllocatedMemory(unsigned sizeInBytes) OVERRIDE 47 { 48 v8::Isolate::GetCurrent()->AdjustAmountOfExternalAllocatedMemory(static_cast<int>(sizeInBytes)); 49 } 50 }; 51 52 class V8ArrayBuffer { 53 public: 54 static bool hasInstance(v8::Handle<v8::Value>, v8::Isolate*); 55 static ArrayBuffer* toNative(v8::Handle<v8::Object>); 56 static ArrayBuffer* toNativeWithTypeCheck(v8::Isolate*, v8::Handle<v8::Value>); 57 static void derefObject(void*); 58 static const WrapperTypeInfo wrapperTypeInfo; 59 static const int internalFieldCount = v8DefaultWrapperInternalFieldCount; 60 static void installPerContextEnabledProperties(v8::Handle<v8::Object>, ArrayBuffer*, v8::Isolate*) { } 61 static void installPerContextEnabledMethods(v8::Handle<v8::Object>, v8::Isolate*) { } 62 63 static inline void* toInternalPointer(ArrayBuffer* impl) 64 { 65 return impl; 66 } 67 68 static inline ArrayBuffer* fromInternalPointer(void* impl) 69 { 70 return static_cast<ArrayBuffer*>(impl); 71 } 72 73 private: 74 friend v8::Handle<v8::Object> wrap(ArrayBuffer*, v8::Handle<v8::Object> creationContext, v8::Isolate*); 75 static v8::Handle<v8::Object> createWrapper(PassRefPtr<ArrayBuffer>, v8::Handle<v8::Object> creationContext, v8::Isolate*); 76 }; 77 78 inline v8::Handle<v8::Object> wrap(ArrayBuffer* impl, v8::Handle<v8::Object> creationContext, v8::Isolate* isolate) 79 { 80 ASSERT(impl); 81 ASSERT(!DOMDataStore::containsWrapper<V8ArrayBuffer>(impl, isolate)); 82 return V8ArrayBuffer::createWrapper(impl, creationContext, isolate); 83 } 84 85 inline v8::Handle<v8::Value> toV8(ArrayBuffer* impl, v8::Handle<v8::Object> creationContext, v8::Isolate* isolate) 86 { 87 if (UNLIKELY(!impl)) 88 return v8::Null(isolate); 89 v8::Handle<v8::Value> wrapper = DOMDataStore::getWrapper<V8ArrayBuffer>(impl, isolate); 90 if (!wrapper.IsEmpty()) 91 return wrapper; 92 return wrap(impl, creationContext, isolate); 93 } 94 95 template<class CallbackInfo> 96 inline void v8SetReturnValue(const CallbackInfo& info, ArrayBuffer* impl) 97 { 98 if (UNLIKELY(!impl)) { 99 v8SetReturnValueNull(info); 100 return; 101 } 102 if (DOMDataStore::setReturnValueFromWrapper<V8ArrayBuffer>(info.GetReturnValue(), impl)) 103 return; 104 v8::Handle<v8::Object> wrapper = wrap(impl, info.Holder(), info.GetIsolate()); 105 v8SetReturnValue(info, wrapper); 106 } 107 108 template<class CallbackInfo> 109 inline void v8SetReturnValueForMainWorld(const CallbackInfo& info, ArrayBuffer* impl) 110 { 111 ASSERT(DOMWrapperWorld::current(info.GetIsolate()).isMainWorld()); 112 if (UNLIKELY(!impl)) { 113 v8SetReturnValueNull(info); 114 return; 115 } 116 if (DOMDataStore::setReturnValueFromWrapperForMainWorld<V8ArrayBuffer>(info.GetReturnValue(), impl)) 117 return; 118 v8::Handle<v8::Value> wrapper = wrap(impl, info.Holder(), info.GetIsolate()); 119 v8SetReturnValue(info, wrapper); 120 } 121 122 template<class CallbackInfo, class Wrappable> 123 inline void v8SetReturnValueFast(const CallbackInfo& info, ArrayBuffer* impl, Wrappable* wrappable) 124 { 125 if (UNLIKELY(!impl)) { 126 v8SetReturnValueNull(info); 127 return; 128 } 129 if (DOMDataStore::setReturnValueFromWrapperFast<V8ArrayBuffer>(info.GetReturnValue(), impl, info.Holder(), wrappable)) 130 return; 131 v8::Handle<v8::Object> wrapper = wrap(impl, info.Holder(), info.GetIsolate()); 132 v8SetReturnValue(info, wrapper); 133 } 134 135 inline v8::Handle<v8::Value> toV8(PassRefPtr< ArrayBuffer > impl, v8::Handle<v8::Object> creationContext, v8::Isolate* isolate) 136 { 137 return toV8(impl.get(), creationContext, isolate); 138 } 139 140 template<class CallbackInfo> 141 inline void v8SetReturnValue(const CallbackInfo& info, PassRefPtr< ArrayBuffer > impl) 142 { 143 v8SetReturnValue(info, impl.get()); 144 } 145 146 template<class CallbackInfo> 147 inline void v8SetReturnValueForMainWorld(const CallbackInfo& info, PassRefPtr< ArrayBuffer > impl) 148 { 149 v8SetReturnValueForMainWorld(info, impl.get()); 150 } 151 152 template<class CallbackInfo, class Wrappable> 153 inline void v8SetReturnValueFast(const CallbackInfo& info, PassRefPtr< ArrayBuffer > impl, Wrappable* wrappable) 154 { 155 v8SetReturnValueFast(info, impl.get(), wrappable); 156 } 157 158 } // namespace WebCore 159 160 #endif // V8ArrayBufferCustom_h 161