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(ArrayBuffer* buffer, unsigned byteOffset,
unsigned byteLength) | 18 static PassRefPtr<DataView> create(PassRefPtr<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(ArrayBuffer* buffer, unsigned byteOffset, unsigned byteLength) | 40 DataView(PassRefPtr<ArrayBuffer> buffer, unsigned byteOffset, unsigned byteL
ength) |
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 DOMDataView* DOMDataView::create(DOMArrayBufferBase* buffer, unsigned byteOffset
, unsigned byteLength) | 49 PassRefPtr<DOMDataView> DOMDataView::create(PassRefPtr<DOMArrayBufferBase> prpBu
ffer, unsigned byteOffset, unsigned byteLength) |
50 { | 50 { |
| 51 RefPtr<DOMArrayBufferBase> buffer = prpBuffer; |
51 RefPtr<DataView> dataView = DataView::create(buffer->buffer(), byteOffset, b
yteLength); | 52 RefPtr<DataView> dataView = DataView::create(buffer->buffer(), byteOffset, b
yteLength); |
52 return new DOMDataView(dataView, buffer); | 53 return adoptRef(new DOMDataView(dataView.release(), buffer.release())); |
53 } | 54 } |
54 | 55 |
55 v8::Local<v8::Object> DOMDataView::wrap(v8::Isolate* isolate, v8::Local<v8::Obje
ct> creationContext) | 56 v8::Local<v8::Object> DOMDataView::wrap(v8::Isolate* isolate, v8::Local<v8::Obje
ct> creationContext) |
56 { | 57 { |
| 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 |
57 DCHECK(!DOMDataStore::containsWrapper(this, isolate)); | 63 DCHECK(!DOMDataStore::containsWrapper(this, isolate)); |
58 | 64 |
59 const WrapperTypeInfo* wrapperTypeInfo = this->wrapperTypeInfo(); | 65 const WrapperTypeInfo* wrapperTypeInfo = this->wrapperTypeInfo(); |
60 v8::Local<v8::Value> v8Buffer = toV8(buffer(), creationContext, isolate); | 66 v8::Local<v8::Value> v8Buffer = toV8(buffer(), creationContext, isolate); |
61 if (v8Buffer.IsEmpty()) | 67 if (v8Buffer.IsEmpty()) |
62 return v8::Local<v8::Object>(); | 68 return v8::Local<v8::Object>(); |
63 DCHECK(v8Buffer->IsArrayBuffer()); | 69 DCHECK(v8Buffer->IsArrayBuffer()); |
64 | 70 |
65 v8::Local<v8::Object> wrapper = v8::DataView::New(v8Buffer.As<v8::ArrayBuffe
r>(), byteOffset(), byteLength()); | 71 v8::Local<v8::Object> wrapper = v8::DataView::New(v8Buffer.As<v8::ArrayBuffe
r>(), byteOffset(), byteLength()); |
66 | 72 |
67 return associateWithWrapper(isolate, wrapperTypeInfo, wrapper); | 73 return associateWithWrapper(isolate, wrapperTypeInfo, wrapper); |
68 } | 74 } |
69 | 75 |
70 } // namespace blink | 76 } // namespace blink |
OLD | NEW |