[email protected] | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 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" | ||||
[email protected] | 855ab43 | 2013-11-18 17:09:36 | [diff] [blame^] | 10 | #include "base/memory/ref_counted.h" |
[email protected] | e87f312 | 2013-11-12 00:41:27 | [diff] [blame] | 11 | #include "gin/converter.h" |
[email protected] | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 12 | #include "v8/include/v8.h" |
13 | |||||
14 | namespace gin { | ||||
15 | |||||
16 | class ArrayBufferAllocator : public v8::ArrayBuffer::Allocator { | ||||
17 | public: | ||||
18 | virtual void* Allocate(size_t length) OVERRIDE; | ||||
19 | virtual void* AllocateUninitialized(size_t length) OVERRIDE; | ||||
20 | virtual void Free(void* data, size_t length) OVERRIDE; | ||||
21 | |||||
22 | static ArrayBufferAllocator* SharedInstance(); | ||||
23 | }; | ||||
24 | |||||
[email protected] | e87f312 | 2013-11-12 00:41:27 | [diff] [blame] | 25 | class ArrayBuffer { |
[email protected] | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 26 | public: |
[email protected] | e87f312 | 2013-11-12 00:41:27 | [diff] [blame] | 27 | explicit ArrayBuffer(v8::Isolate* isolate); |
28 | ArrayBuffer(v8::Isolate* isolate, v8::Handle<v8::ArrayBuffer> buffer); | ||||
29 | ~ArrayBuffer(); | ||||
[email protected] | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 30 | |
31 | void* bytes() const { return bytes_; } | ||||
32 | size_t num_bytes() const { return num_bytes_; } | ||||
33 | |||||
[email protected] | e87f312 | 2013-11-12 00:41:27 | [diff] [blame] | 34 | v8::Isolate* isolate() const { return isolate_; } |
35 | |||||
[email protected] | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 36 | private: |
37 | class Private; | ||||
38 | |||||
[email protected] | e87f312 | 2013-11-12 00:41:27 | [diff] [blame] | 39 | v8::Isolate* isolate_; |
[email protected] | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 40 | scoped_refptr<Private> private_; |
41 | void* bytes_; | ||||
42 | size_t num_bytes_; | ||||
43 | }; | ||||
44 | |||||
[email protected] | e87f312 | 2013-11-12 00:41:27 | [diff] [blame] | 45 | template<> |
46 | struct Converter<ArrayBuffer> { | ||||
47 | static bool FromV8(v8::Handle<v8::Value> val, | ||||
48 | ArrayBuffer* out); | ||||
49 | }; | ||||
50 | |||||
51 | class ArrayBufferView { | ||||
52 | public: | ||||
53 | explicit ArrayBufferView(v8::Isolate* isolate); | ||||
54 | ArrayBufferView(v8::Isolate* isolate, v8::Handle<v8::ArrayBufferView> view); | ||||
55 | ~ArrayBufferView(); | ||||
56 | |||||
57 | void* bytes() const { | ||||
58 | return static_cast<uint8_t*>(array_buffer_.bytes()) + offset_; | ||||
59 | } | ||||
60 | size_t num_bytes() const { return num_bytes_; } | ||||
61 | |||||
62 | v8::Isolate* isolate() const { return array_buffer_.isolate(); } | ||||
63 | |||||
64 | private: | ||||
65 | ArrayBuffer array_buffer_; | ||||
66 | size_t offset_; | ||||
67 | size_t num_bytes_; | ||||
68 | }; | ||||
69 | |||||
70 | template<> | ||||
71 | struct Converter<ArrayBufferView> { | ||||
72 | static bool FromV8(v8::Handle<v8::Value> val, | ||||
73 | ArrayBufferView* out); | ||||
74 | }; | ||||
75 | |||||
[email protected] | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 76 | } // namespace gin |
77 | |||||
78 | #endif // GIN_ARRAY_BUFFER_H_ |