Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / bindings / v8 / custom / V8ArrayBufferCustom.cpp
1 /*
2  * Copyright (C) 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 #include "bindings/v8/custom/V8ArrayBufferCustom.h"
33
34 #include "bindings/v8/V8Binding.h"
35 #include "wtf/ArrayBuffer.h"
36 #include "wtf/StdLibExtras.h"
37
38 namespace WebCore {
39
40 using namespace WTF;
41
42 V8ArrayBufferDeallocationObserver* V8ArrayBufferDeallocationObserver::instanceTemplate()
43 {
44     DEFINE_STATIC_LOCAL(V8ArrayBufferDeallocationObserver, deallocationObserver, ());
45     return &deallocationObserver;
46 }
47
48 const WrapperTypeInfo V8ArrayBuffer::wrapperTypeInfo = {
49     gin::kEmbedderBlink,
50     0, V8ArrayBuffer::derefObject,
51     0, 0, 0, 0, 0, WrapperTypeObjectPrototype, RefCountedObject
52 };
53
54 bool V8ArrayBuffer::hasInstance(v8::Handle<v8::Value> value, v8::Isolate*)
55 {
56     return value->IsArrayBuffer();
57 }
58
59 void V8ArrayBuffer::derefObject(void* object)
60 {
61     static_cast<ArrayBuffer*>(object)->deref();
62 }
63
64 v8::Handle<v8::Object> V8ArrayBuffer::createWrapper(PassRefPtr<ArrayBuffer> impl, v8::Handle<v8::Object> creationContext, v8::Isolate* isolate)
65 {
66     ASSERT(impl.get());
67     ASSERT(!DOMDataStore::containsWrapper<V8ArrayBuffer>(impl.get(), isolate));
68
69     v8::Handle<v8::Object> wrapper = v8::ArrayBuffer::New(isolate, impl->data(), impl->byteLength());
70     impl->setDeallocationObserver(V8ArrayBufferDeallocationObserver::instanceTemplate());
71
72     V8DOMWrapper::associateObjectWithWrapper<V8ArrayBuffer>(impl, &wrapperTypeInfo, wrapper, isolate, WrapperConfiguration::Independent);
73     return wrapper;
74 }
75
76 ArrayBuffer* V8ArrayBuffer::toNative(v8::Handle<v8::Object> object)
77 {
78     ASSERT(object->IsArrayBuffer());
79     v8::Local<v8::ArrayBuffer> v8buffer = object.As<v8::ArrayBuffer>();
80     if (v8buffer->IsExternal()) {
81         RELEASE_ASSERT(toWrapperTypeInfo(object)->ginEmbedder == gin::kEmbedderBlink);
82         return reinterpret_cast<ArrayBuffer*>(WebCore::toNative(object));
83     }
84
85     v8::ArrayBuffer::Contents v8Contents = v8buffer->Externalize();
86     ArrayBufferContents contents(v8Contents.Data(), v8Contents.ByteLength(),
87         V8ArrayBufferDeallocationObserver::instanceTemplate());
88     RefPtr<ArrayBuffer> buffer = ArrayBuffer::create(contents);
89     V8DOMWrapper::associateObjectWithWrapper<V8ArrayBuffer>(buffer.release(), &wrapperTypeInfo, object, v8::Isolate::GetCurrent(), WrapperConfiguration::Dependent);
90
91     return reinterpret_cast<ArrayBuffer*>(WebCore::toNative(object));
92 }
93
94 ArrayBuffer* V8ArrayBuffer::toNativeWithTypeCheck(v8::Isolate* isolate, v8::Handle<v8::Value> value)
95 {
96     return V8ArrayBuffer::hasInstance(value, isolate) ? V8ArrayBuffer::toNative(v8::Handle<v8::Object>::Cast(value)) : 0;
97 }
98
99 template<>
100 v8::Handle<v8::Value> toV8NoInline(ArrayBuffer* impl, v8::Handle<v8::Object> creationContext, v8::Isolate* isolate)
101 {
102     return toV8(impl, creationContext, isolate);
103 }
104
105 } // namespace WebCore