Chromium Code Reviews| Index: third_party/WebKit/Source/bindings/core/v8/SerializedScriptValue.cpp |
| diff --git a/third_party/WebKit/Source/bindings/core/v8/SerializedScriptValue.cpp b/third_party/WebKit/Source/bindings/core/v8/SerializedScriptValue.cpp |
| index 06c7575aa6c00319bba040d5281568a38eba5b7a..085484f9e78a9d386e69ff6e34ca28f59e3a9358 100644 |
| --- a/third_party/WebKit/Source/bindings/core/v8/SerializedScriptValue.cpp |
| +++ b/third_party/WebKit/Source/bindings/core/v8/SerializedScriptValue.cpp |
| @@ -131,9 +131,43 @@ PassRefPtr<SerializedScriptValue> SerializedScriptValue::nullValue() { |
| return create(ScriptValueSerializer::serializeNullValue()); |
| } |
| +String SerializedScriptValue::toWireString() const { |
| + if (!m_data.isNull()) |
| + return m_data; |
| + |
| + // Add the padding '\0', but don't put it in |m_dataBuffer|. |
| + // This requires direct use of uninitialized strings, though. |
| + UChar* destination; |
| + size_t stringSizeBytes = (m_dataBufferSize + 1) & ~1; |
|
haraken
2016/11/11 03:50:01
Shall we use CheckedNumeric<>?
jbroman
2016/11/11 16:14:58
I can if you'd like, but the addition could only o
|
| + String wireString = |
| + String::createUninitialized(stringSizeBytes / 2, destination); |
| + memcpy(destination, m_dataBuffer.get(), m_dataBufferSize); |
| + if (stringSizeBytes > m_dataBufferSize) |
| + reinterpret_cast<char*>(destination)[stringSizeBytes - 1] = '\0'; |
| + return wireString; |
| +} |
| + |
| // Convert serialized string to big endian wire data. |
| void SerializedScriptValue::toWireBytes(Vector<char>& result) const { |
| - ASSERT(result.isEmpty()); |
| + DCHECK(result.isEmpty()); |
| + |
| + if (m_data.isNull()) { |
| + size_t wireSizeBytes = (m_dataBufferSize + 1) & ~1; |
|
haraken
2016/11/11 03:50:01
Ditto.
|
| + result.resize(wireSizeBytes); |
| + |
| + const UChar* src = reinterpret_cast<UChar*>(m_dataBuffer.get()); |
| + UChar* dst = reinterpret_cast<UChar*>(result.data()); |
| + for (size_t i = 0; i < m_dataBufferSize / 2; i++) |
| + dst[i] = htons(src[i]); |
| + |
| + // This is equivalent to swapping the byte order of the two bytes (x, 0), |
| + // depending on endianness. |
| + if (m_dataBufferSize % 1) |
| + dst[wireSizeBytes / 2 - 1] = m_dataBuffer[m_dataBufferSize - 1] << 8; |
| + |
| + return; |
| + } |
| + |
| size_t length = m_data.length(); |
| result.resize(length * sizeof(UChar)); |
| UChar* dst = reinterpret_cast<UChar*>(result.data()); |
| @@ -417,7 +451,8 @@ SerializedScriptValue::transferArrayBufferContents( |
| void SerializedScriptValue::registerMemoryAllocatedWithCurrentScriptContext() { |
| if (m_externallyAllocatedMemory) |
| return; |
| - m_externallyAllocatedMemory = static_cast<intptr_t>(m_data.length()); |
| + |
| + m_externallyAllocatedMemory = static_cast<intptr_t>(dataLengthInBytes()); |
| v8::Isolate::GetCurrent()->AdjustAmountOfExternalAllocatedMemory( |
| m_externallyAllocatedMemory); |
| } |