| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (C) 2009 Google Inc. All rights reserved. | 2 * Copyright (C) 2009 Google Inc. All rights reserved. |
| 3 * | 3 * |
| 4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
| 5 * modification, are permitted provided that the following conditions | 5 * modification, are permitted provided that the following conditions |
| 6 * are met: | 6 * are met: |
| 7 * 1. Redistributions of source code must retain the above copyright | 7 * 1. Redistributions of source code must retain the above copyright |
| 8 * notice, this list of conditions and the following disclaimer. | 8 * notice, this list of conditions and the following disclaimer. |
| 9 * 2. Redistributions in binary form must reproduce the above copyright | 9 * 2. Redistributions in binary form must reproduce the above copyright |
| 10 * notice, this list of conditions and the following disclaimer in the | 10 * notice, this list of conditions and the following disclaimer in the |
| 11 * documentation and/or other materials provided with the distribution. | 11 * documentation and/or other materials provided with the distribution. |
| 12 * | 12 * |
| 13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' | 13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' |
| 14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, | 14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
| 15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | 15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS | 16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS |
| 17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | 17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | 18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | 19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | 20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | 21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF | 22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
| 23 * THE POSSIBILITY OF SUCH DAMAGE. | 23 * THE POSSIBILITY OF SUCH DAMAGE. |
| 24 */ | 24 */ |
| 25 | 25 |
| 26 #ifndef V8StringResource_h | 26 #ifndef V8StringResource_h |
| 27 #define V8StringResource_h | 27 #define V8StringResource_h |
| 28 | 28 |
| 29 #include "bindings/core/v8/ExceptionState.h" | 29 #include "bindings/core/v8/ExceptionState.h" |
| 30 #include "bindings/core/v8/StringResource.h" |
| 30 #include "core/CoreExport.h" | 31 #include "core/CoreExport.h" |
| 31 #include "platform/wtf/Allocator.h" | 32 #include "platform/wtf/Allocator.h" |
| 32 #include "platform/wtf/Threading.h" | 33 #include "platform/wtf/Threading.h" |
| 33 #include "platform/wtf/text/AtomicString.h" | 34 #include "platform/wtf/text/AtomicString.h" |
| 34 #include "v8/include/v8.h" | 35 #include "v8/include/v8.h" |
| 35 | 36 |
| 36 namespace blink { | 37 namespace blink { |
| 37 | 38 |
| 38 // WebCoreStringResource is a helper class for v8ExternalString. It is used | |
| 39 // to manage the life-cycle of the underlying buffer of the external string. | |
| 40 class WebCoreStringResourceBase { | |
| 41 USING_FAST_MALLOC(WebCoreStringResourceBase); | |
| 42 WTF_MAKE_NONCOPYABLE(WebCoreStringResourceBase); | |
| 43 | |
| 44 public: | |
| 45 explicit WebCoreStringResourceBase(const String& string) | |
| 46 : plain_string_(string) { | |
| 47 #if DCHECK_IS_ON() | |
| 48 thread_id_ = WTF::CurrentThread(); | |
| 49 #endif | |
| 50 DCHECK(!string.IsNull()); | |
| 51 v8::Isolate::GetCurrent()->AdjustAmountOfExternalAllocatedMemory( | |
| 52 string.CharactersSizeInBytes()); | |
| 53 } | |
| 54 | |
| 55 explicit WebCoreStringResourceBase(const AtomicString& string) | |
| 56 : plain_string_(string.GetString()), atomic_string_(string) { | |
| 57 #if DCHECK_IS_ON() | |
| 58 thread_id_ = WTF::CurrentThread(); | |
| 59 #endif | |
| 60 DCHECK(!string.IsNull()); | |
| 61 v8::Isolate::GetCurrent()->AdjustAmountOfExternalAllocatedMemory( | |
| 62 string.CharactersSizeInBytes()); | |
| 63 } | |
| 64 | |
| 65 virtual ~WebCoreStringResourceBase() { | |
| 66 #if DCHECK_IS_ON() | |
| 67 DCHECK_EQ(thread_id_, WTF::CurrentThread()); | |
| 68 #endif | |
| 69 int64_t reduced_external_memory = plain_string_.CharactersSizeInBytes(); | |
| 70 if (plain_string_.Impl() != atomic_string_.Impl() && | |
| 71 !atomic_string_.IsNull()) | |
| 72 reduced_external_memory += atomic_string_.CharactersSizeInBytes(); | |
| 73 v8::Isolate::GetCurrent()->AdjustAmountOfExternalAllocatedMemory( | |
| 74 -reduced_external_memory); | |
| 75 } | |
| 76 | |
| 77 const String& WebcoreString() { return plain_string_; } | |
| 78 | |
| 79 const AtomicString& GetAtomicString() { | |
| 80 #if DCHECK_IS_ON() | |
| 81 DCHECK_EQ(thread_id_, WTF::CurrentThread()); | |
| 82 #endif | |
| 83 if (atomic_string_.IsNull()) { | |
| 84 atomic_string_ = AtomicString(plain_string_); | |
| 85 DCHECK(!atomic_string_.IsNull()); | |
| 86 if (plain_string_.Impl() != atomic_string_.Impl()) | |
| 87 v8::Isolate::GetCurrent()->AdjustAmountOfExternalAllocatedMemory( | |
| 88 atomic_string_.CharactersSizeInBytes()); | |
| 89 } | |
| 90 return atomic_string_; | |
| 91 } | |
| 92 | |
| 93 protected: | |
| 94 // A shallow copy of the string. Keeps the string buffer alive until the V8 | |
| 95 // engine garbage collects it. | |
| 96 String plain_string_; | |
| 97 // If this string is atomic or has been made atomic earlier the | |
| 98 // atomic string is held here. In the case where the string starts | |
| 99 // off non-atomic and becomes atomic later it is necessary to keep | |
| 100 // the original string alive because v8 may keep derived pointers | |
| 101 // into that string. | |
| 102 AtomicString atomic_string_; | |
| 103 | |
| 104 private: | |
| 105 #if DCHECK_IS_ON() | |
| 106 WTF::ThreadIdentifier thread_id_; | |
| 107 #endif | |
| 108 }; | |
| 109 | |
| 110 class WebCoreStringResource16 final | |
| 111 : public WebCoreStringResourceBase, | |
| 112 public v8::String::ExternalStringResource { | |
| 113 WTF_MAKE_NONCOPYABLE(WebCoreStringResource16); | |
| 114 | |
| 115 public: | |
| 116 explicit WebCoreStringResource16(const String& string) | |
| 117 : WebCoreStringResourceBase(string) { | |
| 118 DCHECK(!string.Is8Bit()); | |
| 119 } | |
| 120 | |
| 121 explicit WebCoreStringResource16(const AtomicString& string) | |
| 122 : WebCoreStringResourceBase(string) { | |
| 123 DCHECK(!string.Is8Bit()); | |
| 124 } | |
| 125 | |
| 126 size_t length() const override { return plain_string_.Impl()->length(); } | |
| 127 const uint16_t* data() const override { | |
| 128 return reinterpret_cast<const uint16_t*>( | |
| 129 plain_string_.Impl()->Characters16()); | |
| 130 } | |
| 131 }; | |
| 132 | |
| 133 class WebCoreStringResource8 final | |
| 134 : public WebCoreStringResourceBase, | |
| 135 public v8::String::ExternalOneByteStringResource { | |
| 136 WTF_MAKE_NONCOPYABLE(WebCoreStringResource8); | |
| 137 | |
| 138 public: | |
| 139 explicit WebCoreStringResource8(const String& string) | |
| 140 : WebCoreStringResourceBase(string) { | |
| 141 DCHECK(string.Is8Bit()); | |
| 142 } | |
| 143 | |
| 144 explicit WebCoreStringResource8(const AtomicString& string) | |
| 145 : WebCoreStringResourceBase(string) { | |
| 146 DCHECK(string.Is8Bit()); | |
| 147 } | |
| 148 | |
| 149 size_t length() const override { return plain_string_.Impl()->length(); } | |
| 150 const char* data() const override { | |
| 151 return reinterpret_cast<const char*>(plain_string_.Impl()->Characters8()); | |
| 152 } | |
| 153 }; | |
| 154 | |
| 155 enum ExternalMode { kExternalize, kDoNotExternalize }; | |
| 156 | |
| 157 template <typename StringType> | |
| 158 CORE_EXPORT StringType V8StringToWebCoreString(v8::Local<v8::String>, | |
| 159 ExternalMode); | |
| 160 CORE_EXPORT String Int32ToWebCoreString(int value); | |
| 161 | |
| 162 // V8StringResource is an adapter class that converts V8 values to Strings | 39 // V8StringResource is an adapter class that converts V8 values to Strings |
| 163 // or AtomicStrings as appropriate, using multiple typecast operators. | 40 // or AtomicStrings as appropriate, using multiple typecast operators. |
| 164 enum V8StringResourceMode { | 41 enum V8StringResourceMode { |
| 165 kDefaultMode, | 42 kDefaultMode, |
| 166 kTreatNullAsEmptyString, | 43 kTreatNullAsEmptyString, |
| 167 kTreatNullAsNullString, | 44 kTreatNullAsNullString, |
| 168 kTreatNullAndUndefinedAsNullString | 45 kTreatNullAndUndefinedAsNullString |
| 169 }; | 46 }; |
| 170 | 47 |
| 171 template <V8StringResourceMode Mode = kDefaultMode> | 48 template <V8StringResourceMode Mode = kDefaultMode> |
| (...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 302 | 179 |
| 303 template <> | 180 template <> |
| 304 inline String | 181 inline String |
| 305 V8StringResource<kTreatNullAndUndefinedAsNullString>::FallbackString() const { | 182 V8StringResource<kTreatNullAndUndefinedAsNullString>::FallbackString() const { |
| 306 return String(); | 183 return String(); |
| 307 } | 184 } |
| 308 | 185 |
| 309 } // namespace blink | 186 } // namespace blink |
| 310 | 187 |
| 311 #endif // V8StringResource_h | 188 #endif // V8StringResource_h |
| OLD | NEW |