blob: 2aef366ac8194aa261cbca6abc051f7da8a988d3 [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
avi90e658dd2015-12-21 07:16:198#include <stddef.h>
9#include <stdint.h>
10
[email protected]a22998a2013-11-10 05:00:5011#include "base/compiler_specific.h"
avi90e658dd2015-12-21 07:16:1912#include "base/macros.h"
[email protected]855ab432013-11-18 17:09:3613#include "base/memory/ref_counted.h"
[email protected]e87f3122013-11-12 00:41:2714#include "gin/converter.h"
[email protected]48c21632013-12-12 21:32:3415#include "gin/gin_export.h"
[email protected]a22998a2013-11-10 05:00:5016#include "v8/include/v8.h"
17
18namespace gin {
19
20class ArrayBufferAllocator : public v8::ArrayBuffer::Allocator {
21 public:
dcheng074c0392014-10-23 19:08:2522 void* Allocate(size_t length) override;
23 void* AllocateUninitialized(size_t length) override;
24 void Free(void* data, size_t length) override;
[email protected]a22998a2013-11-10 05:00:5025
jochen2f43f2c92014-09-10 23:47:3126 GIN_EXPORT static ArrayBufferAllocator* SharedInstance();
[email protected]a22998a2013-11-10 05:00:5027};
28
[email protected]48c21632013-12-12 21:32:3429class GIN_EXPORT ArrayBuffer {
[email protected]a22998a2013-11-10 05:00:5030 public:
[email protected]7618ebbb2013-11-27 03:38:2631 ArrayBuffer();
deepak.sfaaa1b62015-04-30 07:30:4832 ArrayBuffer(v8::Isolate* isolate, v8::Local<v8::ArrayBuffer> buffer);
[email protected]e87f3122013-11-12 00:41:2733 ~ArrayBuffer();
[email protected]48c21632013-12-12 21:32:3434 ArrayBuffer& operator=(const ArrayBuffer& other);
[email protected]a22998a2013-11-10 05:00:5035
36 void* bytes() const { return bytes_; }
37 size_t num_bytes() const { return num_bytes_; }
38
39 private:
40 class Private;
41
[email protected]a22998a2013-11-10 05:00:5042 scoped_refptr<Private> private_;
43 void* bytes_;
44 size_t num_bytes_;
[email protected]7618ebbb2013-11-27 03:38:2645
46 DISALLOW_COPY(ArrayBuffer);
[email protected]a22998a2013-11-10 05:00:5047};
48
[email protected]e87f3122013-11-12 00:41:2749template<>
[email protected]48c21632013-12-12 21:32:3450struct GIN_EXPORT Converter<ArrayBuffer> {
deepak.sfaaa1b62015-04-30 07:30:4851 static bool FromV8(v8::Isolate* isolate, v8::Local<v8::Value> val,
[email protected]e87f3122013-11-12 00:41:2752 ArrayBuffer* out);
53};
54
[email protected]48c21632013-12-12 21:32:3455class GIN_EXPORT ArrayBufferView {
[email protected]e87f3122013-11-12 00:41:2756 public:
[email protected]7618ebbb2013-11-27 03:38:2657 ArrayBufferView();
deepak.sfaaa1b62015-04-30 07:30:4858 ArrayBufferView(v8::Isolate* isolate, v8::Local<v8::ArrayBufferView> view);
[email protected]e87f3122013-11-12 00:41:2759 ~ArrayBufferView();
[email protected]dfc613d2014-05-16 13:16:5260 ArrayBufferView& operator=(const ArrayBufferView& other);
[email protected]e87f3122013-11-12 00:41:2761
62 void* bytes() const {
63 return static_cast<uint8_t*>(array_buffer_.bytes()) + offset_;
64 }
65 size_t num_bytes() const { return num_bytes_; }
66
[email protected]e87f3122013-11-12 00:41:2767 private:
68 ArrayBuffer array_buffer_;
69 size_t offset_;
70 size_t num_bytes_;
[email protected]7618ebbb2013-11-27 03:38:2671
72 DISALLOW_COPY(ArrayBufferView);
[email protected]e87f3122013-11-12 00:41:2773};
74
75template<>
[email protected]48c21632013-12-12 21:32:3476struct GIN_EXPORT Converter<ArrayBufferView> {
deepak.sfaaa1b62015-04-30 07:30:4877 static bool FromV8(v8::Isolate* isolate, v8::Local<v8::Value> val,
[email protected]e87f3122013-11-12 00:41:2778 ArrayBufferView* out);
79};
80
[email protected]a22998a2013-11-10 05:00:5081} // namespace gin
82
83#endif // GIN_ARRAY_BUFFER_H_