OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "core/dom/DOMDataView.h" | 5 #include "core/dom/DOMDataView.h" |
6 | 6 |
7 #include "bindings/core/v8/DOMDataStore.h" | 7 #include "bindings/core/v8/DOMDataStore.h" |
8 #include "bindings/core/v8/V8ArrayBuffer.h" | 8 #include "bindings/core/v8/V8ArrayBuffer.h" |
9 #include "platform/CheckedInt.h" | 9 #include "platform/CheckedInt.h" |
10 #include "wtf/ArrayBufferView.h" | 10 #include "wtf/ArrayBufferView.h" |
11 | 11 |
12 namespace blink { | 12 namespace blink { |
13 | 13 |
14 namespace { | 14 namespace { |
15 | 15 |
16 class DataView final : public ArrayBufferView { | 16 class DataView final : public ArrayBufferView { |
17 public: | 17 public: |
18 static PassRefPtr<DataView> create(PassRefPtr<ArrayBuffer> buffer, unsigned
byteOffset, unsigned byteLength) | 18 static PassRefPtr<DataView> create(ArrayBuffer* buffer, unsigned byteOffset,
unsigned byteLength) |
19 { | 19 { |
20 RELEASE_ASSERT(byteOffset <= buffer->byteLength()); | 20 RELEASE_ASSERT(byteOffset <= buffer->byteLength()); |
21 CheckedInt<uint32_t> checkedOffset(byteOffset); | 21 CheckedInt<uint32_t> checkedOffset(byteOffset); |
22 CheckedInt<uint32_t> checkedLength(byteLength); | 22 CheckedInt<uint32_t> checkedLength(byteLength); |
23 CheckedInt<uint32_t> checkedMax = checkedOffset + checkedLength; | 23 CheckedInt<uint32_t> checkedMax = checkedOffset + checkedLength; |
24 RELEASE_ASSERT(checkedMax.isValid()); | 24 RELEASE_ASSERT(checkedMax.isValid()); |
25 RELEASE_ASSERT(checkedMax.value() <= buffer->byteLength()); | 25 RELEASE_ASSERT(checkedMax.value() <= buffer->byteLength()); |
26 return adoptRef(new DataView(buffer, byteOffset, byteLength)); | 26 return adoptRef(new DataView(buffer, byteOffset, byteLength)); |
27 } | 27 } |
28 | 28 |
29 unsigned byteLength() const override { return m_byteLength; } | 29 unsigned byteLength() const override { return m_byteLength; } |
30 ViewType type() const override { return TypeDataView; } | 30 ViewType type() const override { return TypeDataView; } |
31 | 31 |
32 protected: | 32 protected: |
33 void neuter() override | 33 void neuter() override |
34 { | 34 { |
35 ArrayBufferView::neuter(); | 35 ArrayBufferView::neuter(); |
36 m_byteLength = 0; | 36 m_byteLength = 0; |
37 } | 37 } |
38 | 38 |
39 private: | 39 private: |
40 DataView(PassRefPtr<ArrayBuffer> buffer, unsigned byteOffset, unsigned byteL
ength) | 40 DataView(ArrayBuffer* buffer, unsigned byteOffset, unsigned byteLength) |
41 : ArrayBufferView(buffer, byteOffset) | 41 : ArrayBufferView(buffer, byteOffset) |
42 , m_byteLength(byteLength) { } | 42 , m_byteLength(byteLength) { } |
43 | 43 |
44 unsigned m_byteLength; | 44 unsigned m_byteLength; |
45 }; | 45 }; |
46 | 46 |
47 } // anonymous namespace | 47 } // anonymous namespace |
48 | 48 |
49 PassRefPtr<DOMDataView> DOMDataView::create(PassRefPtr<DOMArrayBufferBase> prpBu
ffer, unsigned byteOffset, unsigned byteLength) | 49 DOMDataView* DOMDataView::create(DOMArrayBufferBase* buffer, unsigned byteOffset
, unsigned byteLength) |
50 { | 50 { |
51 RefPtr<DOMArrayBufferBase> buffer = prpBuffer; | |
52 RefPtr<DataView> dataView = DataView::create(buffer->buffer(), byteOffset, b
yteLength); | 51 RefPtr<DataView> dataView = DataView::create(buffer->buffer(), byteOffset, b
yteLength); |
53 return adoptRef(new DOMDataView(dataView.release(), buffer.release())); | 52 return new DOMDataView(dataView, buffer); |
54 } | 53 } |
55 | 54 |
56 v8::Local<v8::Object> DOMDataView::wrap(v8::Isolate* isolate, v8::Local<v8::Obje
ct> creationContext) | 55 v8::Local<v8::Object> DOMDataView::wrap(v8::Isolate* isolate, v8::Local<v8::Obje
ct> creationContext) |
57 { | 56 { |
58 // It's possible that no one except for the new wrapper owns this object at | |
59 // this moment, so we have to prevent GC to collect this object until the | |
60 // object gets associated with the wrapper. | |
61 RefPtr<DOMDataView> protect(this); | |
62 | |
63 DCHECK(!DOMDataStore::containsWrapper(this, isolate)); | 57 DCHECK(!DOMDataStore::containsWrapper(this, isolate)); |
64 | 58 |
65 const WrapperTypeInfo* wrapperTypeInfo = this->wrapperTypeInfo(); | 59 const WrapperTypeInfo* wrapperTypeInfo = this->wrapperTypeInfo(); |
66 v8::Local<v8::Value> v8Buffer = toV8(buffer(), creationContext, isolate); | 60 v8::Local<v8::Value> v8Buffer = toV8(buffer(), creationContext, isolate); |
67 if (v8Buffer.IsEmpty()) | 61 if (v8Buffer.IsEmpty()) |
68 return v8::Local<v8::Object>(); | 62 return v8::Local<v8::Object>(); |
69 DCHECK(v8Buffer->IsArrayBuffer()); | 63 DCHECK(v8Buffer->IsArrayBuffer()); |
70 | 64 |
71 v8::Local<v8::Object> wrapper = v8::DataView::New(v8Buffer.As<v8::ArrayBuffe
r>(), byteOffset(), byteLength()); | 65 v8::Local<v8::Object> wrapper = v8::DataView::New(v8Buffer.As<v8::ArrayBuffe
r>(), byteOffset(), byteLength()); |
72 | 66 |
73 return associateWithWrapper(isolate, wrapperTypeInfo, wrapper); | 67 return associateWithWrapper(isolate, wrapperTypeInfo, wrapper); |
74 } | 68 } |
75 | 69 |
76 } // namespace blink | 70 } // namespace blink |
OLD | NEW |