1 // Copyright 2013 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef GIN_ARRAY_BUFFER_H_ 6 #define GIN_ARRAY_BUFFER_H_ 7 8 #include "base/basictypes.h" 9 #include "base/compiler_specific.h" 10 #include "base/memory/ref_counted.h" 11 #include "gin/converter.h" 12 #include "gin/gin_export.h" 13 #include "v8/include/v8.h" 14 15 namespace gin { 16 17 class ArrayBufferAllocator : public v8::ArrayBuffer::Allocator { 18 public: 19 virtual void* Allocate(size_t length) OVERRIDE; 20 virtual void* AllocateUninitialized(size_t length) OVERRIDE; 21 virtual void Free(void* data, size_t length) OVERRIDE; 22 23 static ArrayBufferAllocator* SharedInstance(); 24 }; 25 26 class GIN_EXPORT ArrayBuffer { 27 public: 28 ArrayBuffer(); 29 explicit ArrayBuffer(v8::Isolate* isolate); 30 ArrayBuffer(v8::Isolate* isolate, v8::Handle<v8::ArrayBuffer> buffer); 31 ~ArrayBuffer(); 32 ArrayBuffer& operator=(const ArrayBuffer& other); 33 bytes()34 void* bytes() const { return bytes_; } num_bytes()35 size_t num_bytes() const { return num_bytes_; } 36 37 private: 38 class Private; 39 40 scoped_refptr<Private> private_; 41 void* bytes_; 42 size_t num_bytes_; 43 44 DISALLOW_COPY(ArrayBuffer); 45 }; 46 47 template<> 48 struct GIN_EXPORT Converter<ArrayBuffer> { 49 static bool FromV8(v8::Isolate* isolate, v8::Handle<v8::Value> val, 50 ArrayBuffer* out); 51 }; 52 53 class GIN_EXPORT ArrayBufferView { 54 public: 55 ArrayBufferView(); 56 ArrayBufferView(v8::Isolate* isolate, v8::Handle<v8::ArrayBufferView> view); 57 ~ArrayBufferView(); 58 59 void* bytes() const { 60 return static_cast<uint8_t*>(array_buffer_.bytes()) + offset_; 61 } 62 size_t num_bytes() const { return num_bytes_; } 63 64 private: 65 ArrayBuffer array_buffer_; 66 size_t offset_; 67 size_t num_bytes_; 68 69 DISALLOW_COPY(ArrayBufferView); 70 }; 71 72 template<> 73 struct GIN_EXPORT Converter<ArrayBufferView> { 74 static bool FromV8(v8::Isolate* isolate, v8::Handle<v8::Value> val, 75 ArrayBufferView* out); 76 }; 77 78 } // namespace gin 79 80 #endif // GIN_ARRAY_BUFFER_H_ 81