| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (C) 2010 Google Inc. All rights reserved. | 2 * Copyright (C) 2010 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 are | 5 * modification, are permitted provided that the following conditions are |
| 6 * met: | 6 * met: |
| 7 * | 7 * |
| 8 * * Redistributions of source code must retain the above copyright | 8 * * Redistributions of source code must retain the above copyright |
| 9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
| 10 * * Redistributions in binary form must reproduce the above | 10 * * Redistributions in binary form must reproduce the above |
| (...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 105 for (size_t i = 0; i < stringLength; i++) | 105 for (size_t i = 0; i < stringLength; i++) |
| 106 dst[i] = ntohs(src[i]); | 106 dst[i] = ntohs(src[i]); |
| 107 | 107 |
| 108 return adoptRef(new SerializedScriptValue(String::adopt(buffer))); | 108 return adoptRef(new SerializedScriptValue(String::adopt(buffer))); |
| 109 } | 109 } |
| 110 | 110 |
| 111 SerializedScriptValue::SerializedScriptValue() | 111 SerializedScriptValue::SerializedScriptValue() |
| 112 : m_externallyAllocatedMemory(0) {} | 112 : m_externallyAllocatedMemory(0) {} |
| 113 | 113 |
| 114 SerializedScriptValue::SerializedScriptValue(const String& wireData) | 114 SerializedScriptValue::SerializedScriptValue(const String& wireData) |
| 115 : m_dataString(wireData.isolatedCopy()), m_externallyAllocatedMemory(0) {} | 115 : m_externallyAllocatedMemory(0) { |
| 116 size_t byteLength = wireData.length() * 2; |
| 117 m_dataBuffer.reset(static_cast<uint8_t*>(WTF::Partitions::bufferMalloc( |
| 118 byteLength, "SerializedScriptValue buffer"))); |
| 119 m_dataBufferSize = byteLength; |
| 120 wireData.copyTo(reinterpret_cast<UChar*>(m_dataBuffer.get()), 0, |
| 121 wireData.length()); |
| 122 } |
| 116 | 123 |
| 117 SerializedScriptValue::~SerializedScriptValue() { | 124 SerializedScriptValue::~SerializedScriptValue() { |
| 118 // If the allocated memory was not registered before, then this class is | 125 // If the allocated memory was not registered before, then this class is |
| 119 // likely used in a context other than Worker's onmessage environment and the | 126 // likely used in a context other than Worker's onmessage environment and the |
| 120 // presence of current v8 context is not guaranteed. Avoid calling v8 then. | 127 // presence of current v8 context is not guaranteed. Avoid calling v8 then. |
| 121 if (m_externallyAllocatedMemory) { | 128 if (m_externallyAllocatedMemory) { |
| 122 ASSERT(v8::Isolate::GetCurrent()); | 129 ASSERT(v8::Isolate::GetCurrent()); |
| 123 v8::Isolate::GetCurrent()->AdjustAmountOfExternalAllocatedMemory( | 130 v8::Isolate::GetCurrent()->AdjustAmountOfExternalAllocatedMemory( |
| 124 -m_externallyAllocatedMemory); | 131 -m_externallyAllocatedMemory); |
| 125 } | 132 } |
| 126 } | 133 } |
| 127 | 134 |
| 128 PassRefPtr<SerializedScriptValue> SerializedScriptValue::nullValue() { | 135 PassRefPtr<SerializedScriptValue> SerializedScriptValue::nullValue() { |
| 129 // UChar rather than uint8_t here to get host endian behavior. | 136 // UChar rather than uint8_t here to get host endian behavior. |
| 130 static const UChar kNullData[] = {0xff09, 0x3000}; | 137 static const UChar kNullData[] = {0xff09, 0x3000}; |
| 131 return create(reinterpret_cast<const char*>(kNullData), sizeof(kNullData)); | 138 return create(reinterpret_cast<const char*>(kNullData), sizeof(kNullData)); |
| 132 } | 139 } |
| 133 | 140 |
| 134 String SerializedScriptValue::toWireString() const { | 141 String SerializedScriptValue::toWireString() const { |
| 135 if (!m_dataString.isNull()) | |
| 136 return m_dataString; | |
| 137 | |
| 138 // Add the padding '\0', but don't put it in |m_dataBuffer|. | 142 // Add the padding '\0', but don't put it in |m_dataBuffer|. |
| 139 // This requires direct use of uninitialized strings, though. | 143 // This requires direct use of uninitialized strings, though. |
| 140 UChar* destination; | 144 UChar* destination; |
| 141 size_t stringSizeBytes = (m_dataBufferSize + 1) & ~1; | 145 size_t stringSizeBytes = (m_dataBufferSize + 1) & ~1; |
| 142 String wireString = | 146 String wireString = |
| 143 String::createUninitialized(stringSizeBytes / 2, destination); | 147 String::createUninitialized(stringSizeBytes / 2, destination); |
| 144 memcpy(destination, m_dataBuffer.get(), m_dataBufferSize); | 148 memcpy(destination, m_dataBuffer.get(), m_dataBufferSize); |
| 145 if (stringSizeBytes > m_dataBufferSize) | 149 if (stringSizeBytes > m_dataBufferSize) |
| 146 reinterpret_cast<char*>(destination)[stringSizeBytes - 1] = '\0'; | 150 reinterpret_cast<char*>(destination)[stringSizeBytes - 1] = '\0'; |
| 147 return wireString; | 151 return wireString; |
| 148 } | 152 } |
| 149 | 153 |
| 150 // Convert serialized string to big endian wire data. | 154 // Convert serialized string to big endian wire data. |
| 151 void SerializedScriptValue::toWireBytes(Vector<char>& result) const { | 155 void SerializedScriptValue::toWireBytes(Vector<char>& result) const { |
| 152 DCHECK(result.isEmpty()); | 156 DCHECK(result.isEmpty()); |
| 153 | 157 |
| 154 if (m_dataString.isNull()) { | 158 size_t wireSizeBytes = (m_dataBufferSize + 1) & ~1; |
| 155 size_t wireSizeBytes = (m_dataBufferSize + 1) & ~1; | 159 result.resize(wireSizeBytes); |
| 156 result.resize(wireSizeBytes); | |
| 157 | 160 |
| 158 const UChar* src = reinterpret_cast<UChar*>(m_dataBuffer.get()); | 161 const UChar* src = reinterpret_cast<UChar*>(m_dataBuffer.get()); |
| 159 UChar* dst = reinterpret_cast<UChar*>(result.data()); | 162 UChar* dst = reinterpret_cast<UChar*>(result.data()); |
| 160 for (size_t i = 0; i < m_dataBufferSize / 2; i++) | 163 for (size_t i = 0; i < m_dataBufferSize / 2; i++) |
| 161 dst[i] = htons(src[i]); | 164 dst[i] = htons(src[i]); |
| 162 | 165 |
| 163 // This is equivalent to swapping the byte order of the two bytes (x, 0), | 166 // This is equivalent to swapping the byte order of the two bytes (x, 0), |
| 164 // depending on endianness. | 167 // depending on endianness. |
| 165 if (m_dataBufferSize % 2) | 168 if (m_dataBufferSize % 2) |
| 166 dst[wireSizeBytes / 2 - 1] = m_dataBuffer[m_dataBufferSize - 1] << 8; | 169 dst[wireSizeBytes / 2 - 1] = m_dataBuffer[m_dataBufferSize - 1] << 8; |
| 167 | |
| 168 return; | |
| 169 } | |
| 170 | |
| 171 size_t length = m_dataString.length(); | |
| 172 result.resize(length * sizeof(UChar)); | |
| 173 UChar* dst = reinterpret_cast<UChar*>(result.data()); | |
| 174 | |
| 175 if (m_dataString.is8Bit()) { | |
| 176 const LChar* src = m_dataString.characters8(); | |
| 177 for (size_t i = 0; i < length; i++) | |
| 178 dst[i] = htons(static_cast<UChar>(src[i])); | |
| 179 } else { | |
| 180 const UChar* src = m_dataString.characters16(); | |
| 181 for (size_t i = 0; i < length; i++) | |
| 182 dst[i] = htons(src[i]); | |
| 183 } | |
| 184 } | 170 } |
| 185 | 171 |
| 186 static void accumulateArrayBuffersForAllWorlds( | 172 static void accumulateArrayBuffersForAllWorlds( |
| 187 v8::Isolate* isolate, | 173 v8::Isolate* isolate, |
| 188 DOMArrayBuffer* object, | 174 DOMArrayBuffer* object, |
| 189 Vector<v8::Local<v8::ArrayBuffer>, 4>& buffers) { | 175 Vector<v8::Local<v8::ArrayBuffer>, 4>& buffers) { |
| 190 if (isMainThread()) { | 176 if (isMainThread()) { |
| 191 Vector<RefPtr<DOMWrapperWorld>> worlds; | 177 Vector<RefPtr<DOMWrapperWorld>> worlds; |
| 192 DOMWrapperWorld::allWorldsInMainThread(worlds); | 178 DOMWrapperWorld::allWorldsInMainThread(worlds); |
| 193 for (size_t i = 0; i < worlds.size(); i++) { | 179 for (size_t i = 0; i < worlds.size(); i++) { |
| (...skipping 267 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 461 void SerializedScriptValue::registerMemoryAllocatedWithCurrentScriptContext() { | 447 void SerializedScriptValue::registerMemoryAllocatedWithCurrentScriptContext() { |
| 462 if (m_externallyAllocatedMemory) | 448 if (m_externallyAllocatedMemory) |
| 463 return; | 449 return; |
| 464 | 450 |
| 465 m_externallyAllocatedMemory = static_cast<intptr_t>(dataLengthInBytes()); | 451 m_externallyAllocatedMemory = static_cast<intptr_t>(dataLengthInBytes()); |
| 466 v8::Isolate::GetCurrent()->AdjustAmountOfExternalAllocatedMemory( | 452 v8::Isolate::GetCurrent()->AdjustAmountOfExternalAllocatedMemory( |
| 467 m_externallyAllocatedMemory); | 453 m_externallyAllocatedMemory); |
| 468 } | 454 } |
| 469 | 455 |
| 470 } // namespace blink | 456 } // namespace blink |
| OLD | NEW |