Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 /* | 1 /* |
| 2 * Copyright (C) 2009, 2010 Google Inc. All rights reserved. | 2 * Copyright (C) 2009, 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 18 matching lines...) Expand all Loading... | |
| 29 */ | 29 */ |
| 30 | 30 |
| 31 #ifndef SerializedScriptValue_h | 31 #ifndef SerializedScriptValue_h |
| 32 #define SerializedScriptValue_h | 32 #define SerializedScriptValue_h |
| 33 | 33 |
| 34 #include "bindings/core/v8/ScriptValue.h" | 34 #include "bindings/core/v8/ScriptValue.h" |
| 35 #include "bindings/core/v8/Transferables.h" | 35 #include "bindings/core/v8/Transferables.h" |
| 36 #include "core/CoreExport.h" | 36 #include "core/CoreExport.h" |
| 37 #include "wtf/HashMap.h" | 37 #include "wtf/HashMap.h" |
| 38 #include "wtf/ThreadSafeRefCounted.h" | 38 #include "wtf/ThreadSafeRefCounted.h" |
| 39 #include "wtf/allocator/Partitions.h" | |
| 39 #include "wtf/typed_arrays/ArrayBufferContents.h" | 40 #include "wtf/typed_arrays/ArrayBufferContents.h" |
| 40 #include <memory> | 41 #include <memory> |
| 41 #include <v8.h> | 42 #include <v8.h> |
| 42 | 43 |
| 43 namespace blink { | 44 namespace blink { |
| 44 | 45 |
| 45 class BlobDataHandle; | 46 class BlobDataHandle; |
| 46 class Transferables; | 47 class Transferables; |
| 47 class ExceptionState; | 48 class ExceptionState; |
| 48 class StaticBitmapImage; | 49 class StaticBitmapImage; |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 85 | 86 |
| 86 static PassRefPtr<SerializedScriptValue> create(); | 87 static PassRefPtr<SerializedScriptValue> create(); |
| 87 static PassRefPtr<SerializedScriptValue> create(const String&); | 88 static PassRefPtr<SerializedScriptValue> create(const String&); |
| 88 static PassRefPtr<SerializedScriptValue> create(const char* data, | 89 static PassRefPtr<SerializedScriptValue> create(const char* data, |
| 89 size_t length); | 90 size_t length); |
| 90 | 91 |
| 91 virtual ~SerializedScriptValue(); | 92 virtual ~SerializedScriptValue(); |
| 92 | 93 |
| 93 static PassRefPtr<SerializedScriptValue> nullValue(); | 94 static PassRefPtr<SerializedScriptValue> nullValue(); |
| 94 | 95 |
| 95 String toWireString() const { return m_data; } | 96 String toWireString() const; |
| 96 void toWireBytes(Vector<char>&) const; | 97 void toWireBytes(Vector<char>&) const; |
| 97 | 98 |
| 98 // Deserializes the value (in the current context). Returns a null value in | 99 // Deserializes the value (in the current context). Returns a null value in |
| 99 // case of failure. | 100 // case of failure. |
| 100 v8::Local<v8::Value> deserialize(MessagePortArray* = 0); | 101 v8::Local<v8::Value> deserialize(MessagePortArray* = 0); |
| 101 v8::Local<v8::Value> deserialize(v8::Isolate*, | 102 v8::Local<v8::Value> deserialize(v8::Isolate*, |
| 102 MessagePortArray* = 0, | 103 MessagePortArray* = 0, |
| 103 const WebBlobInfoArray* = 0); | 104 const WebBlobInfoArray* = 0); |
| 104 | 105 |
| 105 // Helper function which pulls the values out of a JS sequence and into a | 106 // Helper function which pulls the values out of a JS sequence and into a |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 122 const ArrayBufferArray&, | 123 const ArrayBufferArray&, |
| 123 ExceptionState&); | 124 ExceptionState&); |
| 124 | 125 |
| 125 // Informs the V8 about external memory allocated and owned by this object. | 126 // Informs the V8 about external memory allocated and owned by this object. |
| 126 // Large values should contribute to GC counters to eventually trigger a GC, | 127 // Large values should contribute to GC counters to eventually trigger a GC, |
| 127 // otherwise flood of postMessage() can cause OOM. | 128 // otherwise flood of postMessage() can cause OOM. |
| 128 // Ok to invoke multiple times (only adds memory once). | 129 // Ok to invoke multiple times (only adds memory once). |
| 129 // The memory registration is revoked automatically in destructor. | 130 // The memory registration is revoked automatically in destructor. |
| 130 void registerMemoryAllocatedWithCurrentScriptContext(); | 131 void registerMemoryAllocatedWithCurrentScriptContext(); |
| 131 | 132 |
| 132 String& data() { return m_data; } | 133 // Provides access to the data and its attributes, regardless of whether the |
| 134 // data was created as a string or as a vector. | |
| 135 // TODO(jbroman): Remove the 16-bit string representation, and simplify. | |
| 136 const uint8_t* data() { | |
| 137 if (!m_data.isNull()) { | |
| 138 m_data.ensure16Bit(); | |
|
haraken
2016/11/11 03:50:01
Shall we rename m_data to m_dataString?
jbroman
2016/11/11 16:14:58
Done.
| |
| 139 return reinterpret_cast<const uint8_t*>(m_data.characters16()); | |
| 140 } | |
| 141 return m_dataBuffer.get(); | |
| 142 } | |
| 143 size_t dataLengthInBytes() const { | |
| 144 if (!m_data.isNull()) | |
| 145 return m_data.length() * 2; | |
| 146 return m_dataBufferSize; | |
| 147 } | |
| 148 bool dataHasOneRef() const { | |
| 149 if (!m_data.isNull()) | |
| 150 return m_data.impl()->hasOneRef(); | |
| 151 return true; | |
| 152 } | |
| 153 | |
| 133 BlobDataHandleMap& blobDataHandles() { return m_blobDataHandles; } | 154 BlobDataHandleMap& blobDataHandles() { return m_blobDataHandles; } |
| 134 ArrayBufferContentsArray* getArrayBufferContentsArray() { | 155 ArrayBufferContentsArray* getArrayBufferContentsArray() { |
| 135 return m_arrayBufferContentsArray.get(); | 156 return m_arrayBufferContentsArray.get(); |
| 136 } | 157 } |
| 137 ImageBitmapContentsArray* getImageBitmapContentsArray() { | 158 ImageBitmapContentsArray* getImageBitmapContentsArray() { |
| 138 return m_imageBitmapContentsArray.get(); | 159 return m_imageBitmapContentsArray.get(); |
| 139 } | 160 } |
| 140 | 161 |
| 141 private: | 162 private: |
| 142 friend class ScriptValueSerializer; | 163 friend class ScriptValueSerializer; |
| 143 friend class V8ScriptValueSerializer; | 164 friend class V8ScriptValueSerializer; |
| 144 | 165 |
| 145 enum StringDataMode { StringValue, WireData }; | 166 struct BufferDeleter { |
| 167 void operator()(uint8_t* buffer) { WTF::Partitions::bufferFree(buffer); } | |
| 168 }; | |
| 169 using DataBufferPtr = std::unique_ptr<uint8_t[], BufferDeleter>; | |
| 146 | 170 |
| 147 SerializedScriptValue(); | 171 SerializedScriptValue(); |
| 148 explicit SerializedScriptValue(const String& wireData); | 172 explicit SerializedScriptValue(const String& wireData); |
| 149 | 173 |
| 150 void setData(const String& data) { m_data = data; } | 174 void setData(const String& data) { |
| 175 m_data = data; | |
| 176 m_dataBuffer.reset(); | |
| 177 m_dataBufferSize = 0; | |
| 178 } | |
| 179 void setData(DataBufferPtr data, size_t size) { | |
| 180 m_data = String(); | |
| 181 m_dataBuffer = std::move(data); | |
| 182 m_dataBufferSize = size; | |
| 183 } | |
| 184 | |
| 151 void transferArrayBuffers(v8::Isolate*, | 185 void transferArrayBuffers(v8::Isolate*, |
| 152 const ArrayBufferArray&, | 186 const ArrayBufferArray&, |
| 153 ExceptionState&); | 187 ExceptionState&); |
| 154 void transferImageBitmaps(v8::Isolate*, | 188 void transferImageBitmaps(v8::Isolate*, |
| 155 const ImageBitmapArray&, | 189 const ImageBitmapArray&, |
| 156 ExceptionState&); | 190 ExceptionState&); |
| 157 void transferOffscreenCanvas(v8::Isolate*, | 191 void transferOffscreenCanvas(v8::Isolate*, |
| 158 const OffscreenCanvasArray&, | 192 const OffscreenCanvasArray&, |
| 159 ExceptionState&); | 193 ExceptionState&); |
| 160 | 194 |
| 195 // Either |m_data| is set (if the serialized data is stored in a WTF::String), | |
| 196 // or |m_dataBuffer| is (if it is stored in a raw buffer allocation). In the | |
|
haraken
2016/11/11 03:50:01
Can we add a DCHECK to verify that |m_data| and |m
jbroman
2016/11/11 16:14:58
I've added one to the data() getter; I'm not sure
| |
| 197 // latter case, the size is stored separately, and will not necessarily be | |
| 198 // even in length. | |
|
haraken
2016/11/11 03:50:01
What do you mean by "will not necessarily be even
jbroman
2016/11/11 16:14:58
I mean that while the string version will always b
| |
| 161 String m_data; | 199 String m_data; |
| 200 DataBufferPtr m_dataBuffer; | |
| 201 size_t m_dataBufferSize; | |
| 202 | |
| 162 std::unique_ptr<ArrayBufferContentsArray> m_arrayBufferContentsArray; | 203 std::unique_ptr<ArrayBufferContentsArray> m_arrayBufferContentsArray; |
| 163 std::unique_ptr<ImageBitmapContentsArray> m_imageBitmapContentsArray; | 204 std::unique_ptr<ImageBitmapContentsArray> m_imageBitmapContentsArray; |
| 164 BlobDataHandleMap m_blobDataHandles; | 205 BlobDataHandleMap m_blobDataHandles; |
| 165 intptr_t m_externallyAllocatedMemory; | 206 intptr_t m_externallyAllocatedMemory; |
| 166 }; | 207 }; |
| 167 | 208 |
| 168 } // namespace blink | 209 } // namespace blink |
| 169 | 210 |
| 170 #endif // SerializedScriptValue_h | 211 #endif // SerializedScriptValue_h |
| OLD | NEW |