[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] | 48c2163 | 2013-12-12 21:32:34 | [diff] [blame] | 12 | #include "gin/gin_export.h" |
[email protected] | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 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 | |||||
jochen | 2f43f2c9 | 2014-09-10 23:47:31 | [diff] [blame^] | 23 | GIN_EXPORT static ArrayBufferAllocator* SharedInstance(); |
[email protected] | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 24 | }; |
25 | |||||
[email protected] | 48c2163 | 2013-12-12 21:32:34 | [diff] [blame] | 26 | class GIN_EXPORT ArrayBuffer { |
[email protected] | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 27 | public: |
[email protected] | 7618ebbb | 2013-11-27 03:38:26 | [diff] [blame] | 28 | ArrayBuffer(); |
[email protected] | e87f312 | 2013-11-12 00:41:27 | [diff] [blame] | 29 | ArrayBuffer(v8::Isolate* isolate, v8::Handle<v8::ArrayBuffer> buffer); |
30 | ~ArrayBuffer(); | ||||
[email protected] | 48c2163 | 2013-12-12 21:32:34 | [diff] [blame] | 31 | ArrayBuffer& operator=(const ArrayBuffer& other); |
[email protected] | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 32 | |
33 | void* bytes() const { return bytes_; } | ||||
34 | size_t num_bytes() const { return num_bytes_; } | ||||
35 | |||||
36 | private: | ||||
37 | class Private; | ||||
38 | |||||
[email protected] | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 39 | scoped_refptr<Private> private_; |
40 | void* bytes_; | ||||
41 | size_t num_bytes_; | ||||
[email protected] | 7618ebbb | 2013-11-27 03:38:26 | [diff] [blame] | 42 | |
43 | DISALLOW_COPY(ArrayBuffer); | ||||
[email protected] | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 44 | }; |
45 | |||||
[email protected] | e87f312 | 2013-11-12 00:41:27 | [diff] [blame] | 46 | template<> |
[email protected] | 48c2163 | 2013-12-12 21:32:34 | [diff] [blame] | 47 | struct GIN_EXPORT Converter<ArrayBuffer> { |
[email protected] | 7618ebbb | 2013-11-27 03:38:26 | [diff] [blame] | 48 | static bool FromV8(v8::Isolate* isolate, v8::Handle<v8::Value> val, |
[email protected] | e87f312 | 2013-11-12 00:41:27 | [diff] [blame] | 49 | ArrayBuffer* out); |
50 | }; | ||||
51 | |||||
[email protected] | 48c2163 | 2013-12-12 21:32:34 | [diff] [blame] | 52 | class GIN_EXPORT ArrayBufferView { |
[email protected] | e87f312 | 2013-11-12 00:41:27 | [diff] [blame] | 53 | public: |
[email protected] | 7618ebbb | 2013-11-27 03:38:26 | [diff] [blame] | 54 | ArrayBufferView(); |
[email protected] | e87f312 | 2013-11-12 00:41:27 | [diff] [blame] | 55 | ArrayBufferView(v8::Isolate* isolate, v8::Handle<v8::ArrayBufferView> view); |
56 | ~ArrayBufferView(); | ||||
[email protected] | dfc613d | 2014-05-16 13:16:52 | [diff] [blame] | 57 | ArrayBufferView& operator=(const ArrayBufferView& other); |
[email protected] | e87f312 | 2013-11-12 00:41:27 | [diff] [blame] | 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] | e87f312 | 2013-11-12 00:41:27 | [diff] [blame] | 64 | private: |
65 | ArrayBuffer array_buffer_; | ||||
66 | size_t offset_; | ||||
67 | size_t num_bytes_; | ||||
[email protected] | 7618ebbb | 2013-11-27 03:38:26 | [diff] [blame] | 68 | |
69 | DISALLOW_COPY(ArrayBufferView); | ||||
[email protected] | e87f312 | 2013-11-12 00:41:27 | [diff] [blame] | 70 | }; |
71 | |||||
72 | template<> | ||||
[email protected] | 48c2163 | 2013-12-12 21:32:34 | [diff] [blame] | 73 | struct GIN_EXPORT Converter<ArrayBufferView> { |
[email protected] | 7618ebbb | 2013-11-27 03:38:26 | [diff] [blame] | 74 | static bool FromV8(v8::Isolate* isolate, v8::Handle<v8::Value> val, |
[email protected] | e87f312 | 2013-11-12 00:41:27 | [diff] [blame] | 75 | ArrayBufferView* out); |
76 | }; | ||||
77 | |||||
[email protected] | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 78 | } // namespace gin |
79 | |||||
80 | #endif // GIN_ARRAY_BUFFER_H_ |