blob: a07a472da0e792e3e536e1864e9f2393e4f3f4ac [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
jochen2f43f2c92014-09-10 23:47:3123 GIN_EXPORT static ArrayBufferAllocator* SharedInstance();
[email protected]a22998a2013-11-10 05:00:5024};
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 ArrayBuffer(v8::Isolate* isolate, v8::Handle<v8::ArrayBuffer> buffer);
30 ~ArrayBuffer();
[email protected]48c21632013-12-12 21:32:3431 ArrayBuffer& operator=(const ArrayBuffer& other);
[email protected]a22998a2013-11-10 05:00:5032
33 void* bytes() const { return bytes_; }
34 size_t num_bytes() const { return num_bytes_; }
35
36 private:
37 class Private;
38
[email protected]a22998a2013-11-10 05:00:5039 scoped_refptr<Private> private_;
40 void* bytes_;
41 size_t num_bytes_;
[email protected]7618ebbb2013-11-27 03:38:2642
43 DISALLOW_COPY(ArrayBuffer);
[email protected]a22998a2013-11-10 05:00:5044};
45
[email protected]e87f3122013-11-12 00:41:2746template<>
[email protected]48c21632013-12-12 21:32:3447struct GIN_EXPORT Converter<ArrayBuffer> {
[email protected]7618ebbb2013-11-27 03:38:2648 static bool FromV8(v8::Isolate* isolate, v8::Handle<v8::Value> val,
[email protected]e87f3122013-11-12 00:41:2749 ArrayBuffer* out);
50};
51
[email protected]48c21632013-12-12 21:32:3452class GIN_EXPORT ArrayBufferView {
[email protected]e87f3122013-11-12 00:41:2753 public:
[email protected]7618ebbb2013-11-27 03:38:2654 ArrayBufferView();
[email protected]e87f3122013-11-12 00:41:2755 ArrayBufferView(v8::Isolate* isolate, v8::Handle<v8::ArrayBufferView> view);
56 ~ArrayBufferView();
[email protected]dfc613d2014-05-16 13:16:5257 ArrayBufferView& operator=(const ArrayBufferView& other);
[email protected]e87f3122013-11-12 00:41:2758
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_