Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(175)

Side by Side Diff: third_party/WebKit/Source/bindings/core/v8/ScriptValueSerializer.cpp

Issue 2277403002: Support structured clone atop v8::ValueSerializer behind a RuntimeEnabledFeature. (Closed)
Patch Set: switch to v8::Maybe<bool>::To Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "bindings/core/v8/ScriptValueSerializer.h" 5 #include "bindings/core/v8/ScriptValueSerializer.h"
6 6
7 #include "bindings/core/v8/Transferables.h" 7 #include "bindings/core/v8/Transferables.h"
8 #include "bindings/core/v8/V8ArrayBuffer.h" 8 #include "bindings/core/v8/V8ArrayBuffer.h"
9 #include "bindings/core/v8/V8ArrayBufferView.h" 9 #include "bindings/core/v8/V8ArrayBufferView.h"
10 #include "bindings/core/v8/V8Blob.h" 10 #include "bindings/core/v8/V8Blob.h"
(...skipping 717 matching lines...) Expand 10 before | Expand all | Expand 10 after
728 CustomCountHistogram domWrapperCount{"Blink.ScriptValueSerializer.DOMWra pperCount", 0, 100000, 50}; 728 CustomCountHistogram domWrapperCount{"Blink.ScriptValueSerializer.DOMWra pperCount", 0, 100000, 50};
729 }; 729 };
730 DEFINE_THREAD_SAFE_STATIC_LOCAL(ObjectCountHistograms, histograms, new Objec tCountHistograms); 730 DEFINE_THREAD_SAFE_STATIC_LOCAL(ObjectCountHistograms, histograms, new Objec tCountHistograms);
731 histograms.primitiveCount.count(primitiveCount); 731 histograms.primitiveCount.count(primitiveCount);
732 histograms.jsObjectCount.count(jsObjectCount); 732 histograms.jsObjectCount.count(jsObjectCount);
733 histograms.domWrapperCount.count(domWrapperCount); 733 histograms.domWrapperCount.count(domWrapperCount);
734 } 734 }
735 735
736 PassRefPtr<SerializedScriptValue> ScriptValueSerializer::serialize(v8::Local<v8: :Value> value, Transferables* transferables, ExceptionState& exceptionState) 736 PassRefPtr<SerializedScriptValue> ScriptValueSerializer::serialize(v8::Local<v8: :Value> value, Transferables* transferables, ExceptionState& exceptionState)
737 { 737 {
738 if (RuntimeEnabledFeatures::v8BasedStructuredCloneEnabled()) {
739 v8::HandleScope scope(isolate());
740 v8::ValueSerializer serializer(isolate());
741 serializer.WriteHeader();
742 bool wroteValue;
743 if (!serializer.WriteValue(context(), value).To(&wroteValue)) {
744 if (m_tryCatch.HasCaught()) {
haraken 2016/08/29 17:09:05 Add a TODO about how to handle the exception.
jbroman 2016/08/29 17:16:14 Done.
745 exceptionState.rethrowV8Exception(m_tryCatch.Exception());
746 } else {
747 exceptionState.throwDOMException(DataCloneError, "An object coul d not be cloned.");
748 }
749 return nullptr;
750 }
751 DCHECK(wroteValue);
752 std::vector<uint8_t> buffer = serializer.ReleaseBuffer();
753 // TODO(jbroman): Remove this old conversion to WTF::String.
754 if (buffer.size() % 2)
755 buffer.push_back(0);
756 return SerializedScriptValue::create(String(reinterpret_cast<const UChar *>(&buffer[0]), buffer.size() / 2));
757 }
758
738 m_primitiveCount = m_jsObjectCount = m_domWrapperCount = 0; 759 m_primitiveCount = m_jsObjectCount = m_domWrapperCount = 0;
739 760
740 DCHECK(!m_blobDataHandles); 761 DCHECK(!m_blobDataHandles);
741 762
742 RefPtr<SerializedScriptValue> serializedValue = SerializedScriptValue::creat e(); 763 RefPtr<SerializedScriptValue> serializedValue = SerializedScriptValue::creat e();
743 764
744 m_blobDataHandles = &serializedValue->blobDataHandles(); 765 m_blobDataHandles = &serializedValue->blobDataHandles();
745 if (transferables) 766 if (transferables)
746 copyTransferables(*transferables); 767 copyTransferables(*transferables);
747 768
(...skipping 1372 matching lines...) Expand 10 before | Expand all | Expand 10 after
2120 if (it != m_blobDataHandles.end()) { 2141 if (it != m_blobDataHandles.end()) {
2121 // make assertions about type and size? 2142 // make assertions about type and size?
2122 return it->value; 2143 return it->value;
2123 } 2144 }
2124 return BlobDataHandle::create(uuid, type, size); 2145 return BlobDataHandle::create(uuid, type, size);
2125 } 2146 }
2126 2147
2127 v8::Local<v8::Value> ScriptValueDeserializer::deserialize() 2148 v8::Local<v8::Value> ScriptValueDeserializer::deserialize()
2128 { 2149 {
2129 v8::Isolate* isolate = m_reader.getScriptState()->isolate(); 2150 v8::Isolate* isolate = m_reader.getScriptState()->isolate();
2151
2152 if (RuntimeEnabledFeatures::v8BasedStructuredCloneEnabled()) {
2153 v8::EscapableHandleScope scope(isolate);
2154 v8::TryCatch tryCatch(isolate);
2155 v8::ValueDeserializer deserializer(isolate, m_reader.buffer(), m_reader. length());
2156 deserializer.SetSupportsLegacyWireFormat(true);
2157 bool readHeader;
2158 if (!deserializer.ReadHeader().To(&readHeader))
2159 return v8::Null(isolate);
2160 DCHECK(readHeader);
2161 v8::Local<v8::Context> context = m_reader.getScriptState()->context();
2162 v8::Local<v8::Value> value;
2163 if (!deserializer.ReadValue(context).ToLocal(&value))
2164 return v8::Null(isolate);
2165 return scope.Escape(value);
2166 }
2167
2130 if (!m_reader.readVersion(m_version) || m_version > SerializedScriptValue::w ireFormatVersion) 2168 if (!m_reader.readVersion(m_version) || m_version > SerializedScriptValue::w ireFormatVersion)
2131 return v8::Null(isolate); 2169 return v8::Null(isolate);
2132 m_reader.setVersion(m_version); 2170 m_reader.setVersion(m_version);
2133 v8::EscapableHandleScope scope(isolate); 2171 v8::EscapableHandleScope scope(isolate);
2134 while (!m_reader.isEof()) { 2172 while (!m_reader.isEof()) {
2135 if (!doDeserialize()) 2173 if (!doDeserialize())
2136 return v8::Null(isolate); 2174 return v8::Null(isolate);
2137 } 2175 }
2138 if (stackDepth() != 1 || m_openCompositeReferenceStack.size()) 2176 if (stackDepth() != 1 || m_openCompositeReferenceStack.size())
2139 return v8::Null(isolate); 2177 return v8::Null(isolate);
(...skipping 307 matching lines...) Expand 10 before | Expand all | Expand 10 after
2447 return false; 2485 return false;
2448 uint32_t objectReference = m_openCompositeReferenceStack[m_openCompositeRefe renceStack.size() - 1]; 2486 uint32_t objectReference = m_openCompositeReferenceStack[m_openCompositeRefe renceStack.size() - 1];
2449 m_openCompositeReferenceStack.shrink(m_openCompositeReferenceStack.size() - 1); 2487 m_openCompositeReferenceStack.shrink(m_openCompositeReferenceStack.size() - 1);
2450 if (objectReference >= m_objectPool.size()) 2488 if (objectReference >= m_objectPool.size())
2451 return false; 2489 return false;
2452 *object = m_objectPool[objectReference]; 2490 *object = m_objectPool[objectReference];
2453 return true; 2491 return true;
2454 } 2492 }
2455 2493
2456 } // namespace blink 2494 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698