blob: 7886fa9be05067c77c55d49e3c01eef4b5f201b0 [file] [log] [blame]
[email protected]a22998a2013-11-10 05:00:501// 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"
[email protected]855ab432013-11-18 17:09:3610#include "base/memory/ref_counted.h"
[email protected]e87f3122013-11-12 00:41:2711#include "gin/converter.h"
[email protected]48c21632013-12-12 21:32:3412#include "gin/gin_export.h"
[email protected]a22998a2013-11-10 05:00:5013#include "v8/include/v8.h"
14
15namespace gin {
16
17class 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
[email protected]48c21632013-12-12 21:32:3426class GIN_EXPORT ArrayBuffer {
[email protected]a22998a2013-11-10 05:00:5027 public:
[email protected]7618ebbb2013-11-27 03:38:2628 ArrayBuffer();
[email protected]e87f3122013-11-12 00:41:2729 explicit ArrayBuffer(v8::Isolate* isolate);
30 ArrayBuffer(v8::Isolate* isolate, v8::Handle<v8::ArrayBuffer> buffer);
31 ~ArrayBuffer();
[email protected]48c21632013-12-12 21:32:3432 ArrayBuffer& operator=(const ArrayBuffer& other);
[email protected]a22998a2013-11-10 05:00:5033
34 void* bytes() const { return bytes_; }
35 size_t num_bytes() const { return num_bytes_; }
36
37 private:
38 class Private;
39
[email protected]a22998a2013-11-10 05:00:5040 scoped_refptr<Private> private_;
41 void* bytes_;
42 size_t num_bytes_;
[email protected]7618ebbb2013-11-27 03:38:2643
44 DISALLOW_COPY(ArrayBuffer);
[email protected]a22998a2013-11-10 05:00:5045};
46
[email protected]e87f3122013-11-12 00:41:2747template<>
[email protected]48c21632013-12-12 21:32:3448struct GIN_EXPORT Converter<ArrayBuffer> {
[email protected]7618ebbb2013-11-27 03:38:2649 static bool FromV8(v8::Isolate* isolate, v8::Handle<v8::Value> val,
[email protected]e87f3122013-11-12 00:41:2750 ArrayBuffer* out);
51};
52
[email protected]48c21632013-12-12 21:32:3453class GIN_EXPORT ArrayBufferView {
[email protected]e87f3122013-11-12 00:41:2754 public:
[email protected]7618ebbb2013-11-27 03:38:2655 ArrayBufferView();
[email protected]e87f3122013-11-12 00:41:2756 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
[email protected]e87f3122013-11-12 00:41:2764 private:
65 ArrayBuffer array_buffer_;
66 size_t offset_;
67 size_t num_bytes_;
[email protected]7618ebbb2013-11-27 03:38:2668
69 DISALLOW_COPY(ArrayBufferView);
[email protected]e87f3122013-11-12 00:41:2770};
71
72template<>
[email protected]48c21632013-12-12 21:32:3473struct GIN_EXPORT Converter<ArrayBufferView> {
[email protected]7618ebbb2013-11-27 03:38:2674 static bool FromV8(v8::Isolate* isolate, v8::Handle<v8::Value> val,
[email protected]e87f3122013-11-12 00:41:2775 ArrayBufferView* out);
76};
77
[email protected]a22998a2013-11-10 05:00:5078} // namespace gin
79
80#endif // GIN_ARRAY_BUFFER_H_