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/core/v8/V8Binding.h"
30#include "bindings/core/v8/V8DOMWrapper.h"
31#include "bindings/core/v8/WrapperTypeInfo.h"
32#include "wtf/ArrayBuffer.h"
33#include <v8.h>
34
35namespace blink {
36
37class V8ArrayBufferDeallocationObserver FINAL: public WTF::ArrayBufferDeallocationObserver {
38public:
39    virtual void arrayBufferDeallocated(unsigned sizeInBytes) OVERRIDE
40    {
41        v8::Isolate::GetCurrent()->AdjustAmountOfExternalAllocatedMemory(-static_cast<int>(sizeInBytes));
42    }
43    static V8ArrayBufferDeallocationObserver* instanceTemplate();
44
45protected:
46    virtual void blinkAllocatedMemory(unsigned sizeInBytes) OVERRIDE
47    {
48        v8::Isolate::GetCurrent()->AdjustAmountOfExternalAllocatedMemory(static_cast<int>(sizeInBytes));
49    }
50};
51
52class V8ArrayBuffer {
53public:
54    static bool hasInstance(v8::Handle<v8::Value>, v8::Isolate*);
55    static ArrayBuffer* toImpl(v8::Handle<v8::Object>);
56    static ArrayBuffer* toImplWithTypeCheck(v8::Isolate*, v8::Handle<v8::Value>);
57    static void refObject(ScriptWrappableBase* internalPointer);
58    static void derefObject(ScriptWrappableBase* internalPointer);
59    static WrapperPersistentNode* createPersistentHandle(ScriptWrappableBase* internalPointer);
60    static const WrapperTypeInfo wrapperTypeInfo;
61    static const int internalFieldCount = v8DefaultWrapperInternalFieldCount;
62
63    static inline ScriptWrappableBase* toScriptWrappableBase(ArrayBuffer* impl)
64    {
65        return reinterpret_cast<ScriptWrappableBase*>(impl);
66    }
67
68    static inline ArrayBuffer* toImpl(ScriptWrappableBase* internalPointer)
69    {
70        return reinterpret_cast<ArrayBuffer*>(internalPointer);
71    }
72
73private:
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
78inline 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
85inline 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
95template<class CallbackInfo>
96inline 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
108template<class CallbackInfo>
109inline 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
122template<class CallbackInfo, class Wrappable>
123inline 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
135inline 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
140template<class CallbackInfo>
141inline void v8SetReturnValue(const CallbackInfo& info, PassRefPtr< ArrayBuffer > impl)
142{
143    v8SetReturnValue(info, impl.get());
144}
145
146template<class CallbackInfo>
147inline void v8SetReturnValueForMainWorld(const CallbackInfo& info, PassRefPtr< ArrayBuffer > impl)
148{
149    v8SetReturnValueForMainWorld(info, impl.get());
150}
151
152template<class CallbackInfo, class Wrappable>
153inline void v8SetReturnValueFast(const CallbackInfo& info, PassRefPtr< ArrayBuffer > impl, Wrappable* wrappable)
154{
155    v8SetReturnValueFast(info, impl.get(), wrappable);
156}
157
158} // namespace blink
159
160#endif // V8ArrayBufferCustom_h
161