OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 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 NotShared_h |
| 6 #define NotShared_h |
| 7 |
| 8 // A wrapper template type that is used to ensure that a TypedArray is not |
| 9 // backed by a SharedArrayBuffer. |
| 10 // |
| 11 // Typically this is used as an annotation on C++ functions that are called by |
| 12 // the bindings layer, e.g.: |
| 13 // |
| 14 // void Foo(NotShared<DOMUint32Array> param) { |
| 15 // DOMUint32Array* array = param.View(); |
| 16 // ... |
| 17 // } |
| 18 |
| 19 #include "platform/heap/Handle.h" |
| 20 |
| 21 namespace blink { |
| 22 |
| 23 template <typename T> |
| 24 class NotShared { |
| 25 STACK_ALLOCATED(); |
| 26 |
| 27 public: |
| 28 using TypedArrayType = T; |
| 29 |
| 30 NotShared() {} |
| 31 |
| 32 explicit NotShared(T* typedArray) : typed_array_(typedArray) { |
| 33 DCHECK(!(typedArray && typedArray->View()->IsShared())); |
| 34 } |
| 35 NotShared(const NotShared& other) = default; |
| 36 template <typename U> |
| 37 NotShared(const NotShared<U>& other) : typed_array_(other.View()) {} |
| 38 template <typename U> |
| 39 NotShared(const Member<U>& other) { |
| 40 DCHECK(!other->View()->IsShared()); |
| 41 typed_array_ = other.Get(); |
| 42 } |
| 43 |
| 44 NotShared& operator=(const NotShared& other) = default; |
| 45 template <typename U> |
| 46 NotShared& operator=(const NotShared<U>& other) { |
| 47 typed_array_ = other.View(); |
| 48 return *this; |
| 49 } |
| 50 |
| 51 T* View() const { return typed_array_.Get(); } |
| 52 |
| 53 bool operator!() const { return !typed_array_; } |
| 54 explicit operator bool() const { return !!typed_array_; } |
| 55 |
| 56 private: |
| 57 // Must use an untraced member here since this object may be constructed on a |
| 58 // thread without a ThreadState (e.g. an Audio worklet). It is safe in that |
| 59 // case because the pointed-to ArrayBuffer is being kept alive another way |
| 60 // (e.g. CrossThreadPersistent). |
| 61 // |
| 62 // TODO(binji): update to using Member, see crbug.com/710295. |
| 63 UntracedMember<T> typed_array_; |
| 64 }; |
| 65 |
| 66 } // namespace blink |
| 67 |
| 68 #endif // NotShared_h |
OLD | NEW |