| 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 StringResource_h |
| 6 #define StringResource_h |
| 7 |
| 8 #include "core/CoreExport.h" |
| 9 #include "platform/wtf/Allocator.h" |
| 10 #include "platform/wtf/Threading.h" |
| 11 #include "platform/wtf/text/AtomicString.h" |
| 12 #include "v8/include/v8.h" |
| 13 |
| 14 namespace blink { |
| 15 |
| 16 // StringResource is a helper class for V8ExternalString. It is used |
| 17 // to manage the life-cycle of the underlying buffer of the external string. |
| 18 class StringResourceBase { |
| 19 USING_FAST_MALLOC(StringResourceBase); |
| 20 WTF_MAKE_NONCOPYABLE(StringResourceBase); |
| 21 |
| 22 public: |
| 23 explicit StringResourceBase(const String& string) : plain_string_(string) { |
| 24 #if DCHECK_IS_ON() |
| 25 thread_id_ = WTF::CurrentThread(); |
| 26 #endif |
| 27 DCHECK(!string.IsNull()); |
| 28 v8::Isolate::GetCurrent()->AdjustAmountOfExternalAllocatedMemory( |
| 29 string.CharactersSizeInBytes()); |
| 30 } |
| 31 |
| 32 explicit StringResourceBase(const AtomicString& string) |
| 33 : plain_string_(string.GetString()), atomic_string_(string) { |
| 34 #if DCHECK_IS_ON() |
| 35 thread_id_ = WTF::CurrentThread(); |
| 36 #endif |
| 37 DCHECK(!string.IsNull()); |
| 38 v8::Isolate::GetCurrent()->AdjustAmountOfExternalAllocatedMemory( |
| 39 string.CharactersSizeInBytes()); |
| 40 } |
| 41 |
| 42 virtual ~StringResourceBase() { |
| 43 #if DCHECK_IS_ON() |
| 44 DCHECK(thread_id_ == WTF::CurrentThread()); |
| 45 #endif |
| 46 int64_t reduced_external_memory = plain_string_.CharactersSizeInBytes(); |
| 47 if (plain_string_.Impl() != atomic_string_.Impl() && |
| 48 !atomic_string_.IsNull()) |
| 49 reduced_external_memory += atomic_string_.CharactersSizeInBytes(); |
| 50 v8::Isolate::GetCurrent()->AdjustAmountOfExternalAllocatedMemory( |
| 51 -reduced_external_memory); |
| 52 } |
| 53 |
| 54 const String& WebcoreString() { return plain_string_; } |
| 55 |
| 56 const AtomicString& GetAtomicString() { |
| 57 #if DCHECK_IS_ON() |
| 58 DCHECK(thread_id_ == WTF::CurrentThread()); |
| 59 #endif |
| 60 if (atomic_string_.IsNull()) { |
| 61 atomic_string_ = AtomicString(plain_string_); |
| 62 DCHECK(!atomic_string_.IsNull()); |
| 63 if (plain_string_.Impl() != atomic_string_.Impl()) { |
| 64 v8::Isolate::GetCurrent()->AdjustAmountOfExternalAllocatedMemory( |
| 65 atomic_string_.CharactersSizeInBytes()); |
| 66 } |
| 67 } |
| 68 return atomic_string_; |
| 69 } |
| 70 |
| 71 protected: |
| 72 // A shallow copy of the string. Keeps the string buffer alive until the V8 |
| 73 // engine garbage collects it. |
| 74 String plain_string_; |
| 75 // If this string is atomic or has been made atomic earlier the |
| 76 // atomic string is held here. In the case where the string starts |
| 77 // off non-atomic and becomes atomic later it is necessary to keep |
| 78 // the original string alive because v8 may keep derived pointers |
| 79 // into that string. |
| 80 AtomicString atomic_string_; |
| 81 |
| 82 private: |
| 83 #if DCHECK_IS_ON() |
| 84 WTF::ThreadIdentifier thread_id_; |
| 85 #endif |
| 86 }; |
| 87 |
| 88 class StringResource16 final : public StringResourceBase, |
| 89 public v8::String::ExternalStringResource { |
| 90 WTF_MAKE_NONCOPYABLE(StringResource16); |
| 91 |
| 92 public: |
| 93 explicit StringResource16(const String& string) : StringResourceBase(string) { |
| 94 DCHECK(!string.Is8Bit()); |
| 95 } |
| 96 |
| 97 explicit StringResource16(const AtomicString& string) |
| 98 : StringResourceBase(string) { |
| 99 DCHECK(!string.Is8Bit()); |
| 100 } |
| 101 |
| 102 size_t length() const override { return plain_string_.Impl()->length(); } |
| 103 const uint16_t* data() const override { |
| 104 return reinterpret_cast<const uint16_t*>( |
| 105 plain_string_.Impl()->Characters16()); |
| 106 } |
| 107 }; |
| 108 |
| 109 class StringResource8 final : public StringResourceBase, |
| 110 public v8::String::ExternalOneByteStringResource { |
| 111 WTF_MAKE_NONCOPYABLE(StringResource8); |
| 112 |
| 113 public: |
| 114 explicit StringResource8(const String& string) : StringResourceBase(string) { |
| 115 DCHECK(string.Is8Bit()); |
| 116 } |
| 117 |
| 118 explicit StringResource8(const AtomicString& string) |
| 119 : StringResourceBase(string) { |
| 120 DCHECK(string.Is8Bit()); |
| 121 } |
| 122 |
| 123 size_t length() const override { return plain_string_.Impl()->length(); } |
| 124 const char* data() const override { |
| 125 return reinterpret_cast<const char*>(plain_string_.Impl()->Characters8()); |
| 126 } |
| 127 }; |
| 128 |
| 129 enum ExternalMode { kExternalize, kDoNotExternalize }; |
| 130 |
| 131 template <typename StringType> |
| 132 CORE_EXPORT StringType V8StringToWebCoreString(v8::Local<v8::String>, |
| 133 ExternalMode); |
| 134 CORE_EXPORT String Int32ToWebCoreString(int value); |
| 135 |
| 136 } // namespace blink |
| 137 |
| 138 #endif // StringResource_h |
| OLD | NEW |