2 * Copyright (C) 2009 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
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
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.
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.
32 #include "bindings/core/v8/custom/V8ArrayBufferCustom.h"
34 #include "bindings/core/v8/V8Binding.h"
35 #include "wtf/ArrayBuffer.h"
36 #include "wtf/StdLibExtras.h"
42 V8ArrayBufferDeallocationObserver* V8ArrayBufferDeallocationObserver::instanceTemplate()
44 DEFINE_STATIC_LOCAL(V8ArrayBufferDeallocationObserver, deallocationObserver, ());
45 return &deallocationObserver;
48 const WrapperTypeInfo V8ArrayBuffer::wrapperTypeInfo = {
51 V8ArrayBuffer::refObject,
52 V8ArrayBuffer::derefObject,
53 V8ArrayBuffer::createPersistentHandle,
55 WrapperTypeInfo::WrapperTypeObjectPrototype,
56 WrapperTypeInfo::ObjectClassId,
57 WrapperTypeInfo::Independent,
58 WrapperTypeInfo::RefCountedObject
61 bool V8ArrayBuffer::hasInstance(v8::Handle<v8::Value> value, v8::Isolate*)
63 return value->IsArrayBuffer();
66 void V8ArrayBuffer::refObject(ScriptWrappableBase* internalPointer)
68 toImpl(internalPointer)->ref();
71 void V8ArrayBuffer::derefObject(ScriptWrappableBase* internalPointer)
73 toImpl(internalPointer)->deref();
76 WrapperPersistentNode* V8ArrayBuffer::createPersistentHandle(ScriptWrappableBase* internalPointer)
82 v8::Handle<v8::Object> V8ArrayBuffer::createWrapper(PassRefPtr<ArrayBuffer> impl, v8::Handle<v8::Object> creationContext, v8::Isolate* isolate)
85 ASSERT(!DOMDataStore::containsWrapper<V8ArrayBuffer>(impl.get(), isolate));
87 v8::Handle<v8::Object> wrapper = v8::ArrayBuffer::New(isolate, impl->data(), impl->byteLength());
88 impl->setDeallocationObserver(V8ArrayBufferDeallocationObserver::instanceTemplate());
90 V8DOMWrapper::associateObjectWithWrapper<V8ArrayBuffer>(impl, &wrapperTypeInfo, wrapper, isolate);
94 ArrayBuffer* V8ArrayBuffer::toImpl(v8::Handle<v8::Object> object)
96 ASSERT(object->IsArrayBuffer());
97 v8::Local<v8::ArrayBuffer> v8buffer = object.As<v8::ArrayBuffer>();
98 if (v8buffer->IsExternal()) {
99 RELEASE_ASSERT(toWrapperTypeInfo(object)->ginEmbedder == gin::kEmbedderBlink);
100 return reinterpret_cast<ArrayBuffer*>(blink::toScriptWrappableBase(object));
103 v8::ArrayBuffer::Contents v8Contents = v8buffer->Externalize();
104 ArrayBufferContents contents(v8Contents.Data(), v8Contents.ByteLength(),
105 V8ArrayBufferDeallocationObserver::instanceTemplate());
106 RefPtr<ArrayBuffer> buffer = ArrayBuffer::create(contents);
107 V8DOMWrapper::associateObjectWithWrapper<V8ArrayBuffer>(buffer.release(), &wrapperTypeInfo, object, v8::Isolate::GetCurrent());
109 return reinterpret_cast<ArrayBuffer*>(blink::toScriptWrappableBase(object));
112 ArrayBuffer* V8ArrayBuffer::toImplWithTypeCheck(v8::Isolate* isolate, v8::Handle<v8::Value> value)
114 return V8ArrayBuffer::hasInstance(value, isolate) ? V8ArrayBuffer::toImpl(v8::Handle<v8::Object>::Cast(value)) : 0;
118 v8::Handle<v8::Value> toV8NoInline(ArrayBuffer* impl, v8::Handle<v8::Object> creationContext, v8::Isolate* isolate)
120 return toV8(impl, creationContext, isolate);