Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / bindings / core / 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/core/v8/custom/V8ArrayBufferCustom.h"
33
34 #include "bindings/core/v8/V8Binding.h"
35 #include "wtf/ArrayBuffer.h"
36 #include "wtf/StdLibExtras.h"
37
38 namespace blink {
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,
51     V8ArrayBuffer::refObject,
52     V8ArrayBuffer::derefObject,
53     V8ArrayBuffer::createPersistentHandle,
54     0, 0, 0, 0, 0, 0,
55     WrapperTypeInfo::WrapperTypeObjectPrototype,
56     WrapperTypeInfo::ObjectClassId,
57     WrapperTypeInfo::Independent,
58     WrapperTypeInfo::RefCountedObject
59 };
60
61 bool V8ArrayBuffer::hasInstance(v8::Handle<v8::Value> value, v8::Isolate*)
62 {
63     return value->IsArrayBuffer();
64 }
65
66 void V8ArrayBuffer::refObject(ScriptWrappableBase* internalPointer)
67 {
68     toImpl(internalPointer)->ref();
69 }
70
71 void V8ArrayBuffer::derefObject(ScriptWrappableBase* internalPointer)
72 {
73     toImpl(internalPointer)->deref();
74 }
75
76 WrapperPersistentNode* V8ArrayBuffer::createPersistentHandle(ScriptWrappableBase* internalPointer)
77 {
78     ASSERT_NOT_REACHED();
79     return 0;
80 }
81
82 v8::Handle<v8::Object> V8ArrayBuffer::createWrapper(PassRefPtr<ArrayBuffer> impl, v8::Handle<v8::Object> creationContext, v8::Isolate* isolate)
83 {
84     ASSERT(impl.get());
85     ASSERT(!DOMDataStore::containsWrapper<V8ArrayBuffer>(impl.get(), isolate));
86
87     v8::Handle<v8::Object> wrapper = v8::ArrayBuffer::New(isolate, impl->data(), impl->byteLength());
88     impl->setDeallocationObserver(V8ArrayBufferDeallocationObserver::instanceTemplate());
89
90     V8DOMWrapper::associateObjectWithWrapper<V8ArrayBuffer>(impl, &wrapperTypeInfo, wrapper, isolate);
91     return wrapper;
92 }
93
94 ArrayBuffer* V8ArrayBuffer::toImpl(v8::Handle<v8::Object> object)
95 {
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));
101     }
102
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());
108
109     return reinterpret_cast<ArrayBuffer*>(blink::toScriptWrappableBase(object));
110 }
111
112 ArrayBuffer* V8ArrayBuffer::toImplWithTypeCheck(v8::Isolate* isolate, v8::Handle<v8::Value> value)
113 {
114     return V8ArrayBuffer::hasInstance(value, isolate) ? V8ArrayBuffer::toImpl(v8::Handle<v8::Object>::Cast(value)) : 0;
115 }
116
117 template<>
118 v8::Handle<v8::Value> toV8NoInline(ArrayBuffer* impl, v8::Handle<v8::Object> creationContext, v8::Isolate* isolate)
119 {
120     return toV8(impl, creationContext, isolate);
121 }
122
123 } // namespace blink