[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 | |||||
23 | static ArrayBufferAllocator* SharedInstance(); | ||||
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 | explicit ArrayBuffer(v8::Isolate* isolate); |
30 | ArrayBuffer(v8::Isolate* isolate, v8::Handle<v8::ArrayBuffer> buffer); | ||||
31 | ~ArrayBuffer(); | ||||
[email protected] | 48c2163 | 2013-12-12 21:32:34 | [diff] [blame^] | 32 | ArrayBuffer& operator=(const ArrayBuffer& other); |
[email protected] | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 33 | |
34 | void* bytes() const { return bytes_; } | ||||
35 | size_t num_bytes() const { return num_bytes_; } | ||||
36 | |||||
37 | private: | ||||
38 | class Private; | ||||
39 | |||||
[email protected] | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 40 | scoped_refptr<Private> private_; |
41 | void* bytes_; | ||||
42 | size_t num_bytes_; | ||||
[email protected] | 7618ebbb | 2013-11-27 03:38:26 | [diff] [blame] | 43 | |
44 | DISALLOW_COPY(ArrayBuffer); | ||||
[email protected] | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 45 | }; |
46 | |||||
[email protected] | e87f312 | 2013-11-12 00:41:27 | [diff] [blame] | 47 | template<> |
[email protected] | 48c2163 | 2013-12-12 21:32:34 | [diff] [blame^] | 48 | struct GIN_EXPORT Converter<ArrayBuffer> { |
[email protected] | 7618ebbb | 2013-11-27 03:38:26 | [diff] [blame] | 49 | static bool FromV8(v8::Isolate* isolate, v8::Handle<v8::Value> val, |
[email protected] | e87f312 | 2013-11-12 00:41:27 | [diff] [blame] | 50 | ArrayBuffer* out); |
51 | }; | ||||
52 | |||||
[email protected] | 48c2163 | 2013-12-12 21:32:34 | [diff] [blame^] | 53 | class GIN_EXPORT ArrayBufferView { |
[email protected] | e87f312 | 2013-11-12 00:41:27 | [diff] [blame] | 54 | public: |
[email protected] | 7618ebbb | 2013-11-27 03:38:26 | [diff] [blame] | 55 | ArrayBufferView(); |
[email protected] | e87f312 | 2013-11-12 00:41:27 | [diff] [blame] | 56 | 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] | 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_ |