[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 | |||||
avi | 90e658dd | 2015-12-21 07:16:19 | [diff] [blame] | 8 | #include <stddef.h> |
9 | #include <stdint.h> | ||||
10 | |||||
[email protected] | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 11 | #include "base/compiler_specific.h" |
avi | 90e658dd | 2015-12-21 07:16:19 | [diff] [blame] | 12 | #include "base/macros.h" |
[email protected] | 855ab43 | 2013-11-18 17:09:36 | [diff] [blame] | 13 | #include "base/memory/ref_counted.h" |
[email protected] | e87f312 | 2013-11-12 00:41:27 | [diff] [blame] | 14 | #include "gin/converter.h" |
[email protected] | 48c2163 | 2013-12-12 21:32:34 | [diff] [blame] | 15 | #include "gin/gin_export.h" |
[email protected] | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 16 | #include "v8/include/v8.h" |
17 | |||||
18 | namespace gin { | ||||
19 | |||||
20 | class ArrayBufferAllocator : public v8::ArrayBuffer::Allocator { | ||||
21 | public: | ||||
dcheng | 074c039 | 2014-10-23 19:08:25 | [diff] [blame] | 22 | void* Allocate(size_t length) override; |
23 | void* AllocateUninitialized(size_t length) override; | ||||
24 | void Free(void* data, size_t length) override; | ||||
[email protected] | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 25 | |
jochen | 2f43f2c9 | 2014-09-10 23:47:31 | [diff] [blame] | 26 | GIN_EXPORT static ArrayBufferAllocator* SharedInstance(); |
[email protected] | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 27 | }; |
28 | |||||
[email protected] | 48c2163 | 2013-12-12 21:32:34 | [diff] [blame] | 29 | class GIN_EXPORT ArrayBuffer { |
[email protected] | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 30 | public: |
[email protected] | 7618ebbb | 2013-11-27 03:38:26 | [diff] [blame] | 31 | ArrayBuffer(); |
deepak.s | faaa1b6 | 2015-04-30 07:30:48 | [diff] [blame] | 32 | ArrayBuffer(v8::Isolate* isolate, v8::Local<v8::ArrayBuffer> buffer); |
[email protected] | e87f312 | 2013-11-12 00:41:27 | [diff] [blame] | 33 | ~ArrayBuffer(); |
[email protected] | 48c2163 | 2013-12-12 21:32:34 | [diff] [blame] | 34 | ArrayBuffer& operator=(const ArrayBuffer& other); |
[email protected] | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 35 | |
36 | void* bytes() const { return bytes_; } | ||||
37 | size_t num_bytes() const { return num_bytes_; } | ||||
38 | |||||
39 | private: | ||||
40 | class Private; | ||||
41 | |||||
[email protected] | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 42 | scoped_refptr<Private> private_; |
43 | void* bytes_; | ||||
44 | size_t num_bytes_; | ||||
[email protected] | 7618ebbb | 2013-11-27 03:38:26 | [diff] [blame] | 45 | |
46 | DISALLOW_COPY(ArrayBuffer); | ||||
[email protected] | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 47 | }; |
48 | |||||
[email protected] | e87f312 | 2013-11-12 00:41:27 | [diff] [blame] | 49 | template<> |
[email protected] | 48c2163 | 2013-12-12 21:32:34 | [diff] [blame] | 50 | struct GIN_EXPORT Converter<ArrayBuffer> { |
deepak.s | faaa1b6 | 2015-04-30 07:30:48 | [diff] [blame] | 51 | static bool FromV8(v8::Isolate* isolate, v8::Local<v8::Value> val, |
[email protected] | e87f312 | 2013-11-12 00:41:27 | [diff] [blame] | 52 | ArrayBuffer* out); |
53 | }; | ||||
54 | |||||
[email protected] | 48c2163 | 2013-12-12 21:32:34 | [diff] [blame] | 55 | class GIN_EXPORT ArrayBufferView { |
[email protected] | e87f312 | 2013-11-12 00:41:27 | [diff] [blame] | 56 | public: |
[email protected] | 7618ebbb | 2013-11-27 03:38:26 | [diff] [blame] | 57 | ArrayBufferView(); |
deepak.s | faaa1b6 | 2015-04-30 07:30:48 | [diff] [blame] | 58 | ArrayBufferView(v8::Isolate* isolate, v8::Local<v8::ArrayBufferView> view); |
[email protected] | e87f312 | 2013-11-12 00:41:27 | [diff] [blame] | 59 | ~ArrayBufferView(); |
[email protected] | dfc613d | 2014-05-16 13:16:52 | [diff] [blame] | 60 | ArrayBufferView& operator=(const ArrayBufferView& other); |
[email protected] | e87f312 | 2013-11-12 00:41:27 | [diff] [blame] | 61 | |
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] | e87f312 | 2013-11-12 00:41:27 | [diff] [blame] | 67 | private: |
68 | ArrayBuffer array_buffer_; | ||||
69 | size_t offset_; | ||||
70 | size_t num_bytes_; | ||||
[email protected] | 7618ebbb | 2013-11-27 03:38:26 | [diff] [blame] | 71 | |
72 | DISALLOW_COPY(ArrayBufferView); | ||||
[email protected] | e87f312 | 2013-11-12 00:41:27 | [diff] [blame] | 73 | }; |
74 | |||||
75 | template<> | ||||
[email protected] | 48c2163 | 2013-12-12 21:32:34 | [diff] [blame] | 76 | struct GIN_EXPORT Converter<ArrayBufferView> { |
deepak.s | faaa1b6 | 2015-04-30 07:30:48 | [diff] [blame] | 77 | static bool FromV8(v8::Isolate* isolate, v8::Local<v8::Value> val, |
[email protected] | e87f312 | 2013-11-12 00:41:27 | [diff] [blame] | 78 | ArrayBufferView* out); |
79 | }; | ||||
80 | |||||
[email protected] | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 81 | } // namespace gin |
82 | |||||
83 | #endif // GIN_ARRAY_BUFFER_H_ |