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_dataString.isNull()) { |
| 138 DCHECK(!m_dataBuffer); |
| 139 m_dataString.ensure16Bit(); |
| 140 return reinterpret_cast<const uint8_t*>(m_dataString.characters16()); |
| 141 } |
| 142 return m_dataBuffer.get(); |
| 143 } |
| 144 size_t dataLengthInBytes() const { |
| 145 if (!m_dataString.isNull()) |
| 146 return m_dataString.length() * 2; |
| 147 return m_dataBufferSize; |
| 148 } |
| 149 bool dataHasOneRef() const { |
| 150 if (!m_dataString.isNull()) |
| 151 return m_dataString.impl()->hasOneRef(); |
| 152 return true; |
| 153 } |
| 154 |
133 BlobDataHandleMap& blobDataHandles() { return m_blobDataHandles; } | 155 BlobDataHandleMap& blobDataHandles() { return m_blobDataHandles; } |
134 ArrayBufferContentsArray* getArrayBufferContentsArray() { | 156 ArrayBufferContentsArray* getArrayBufferContentsArray() { |
135 return m_arrayBufferContentsArray.get(); | 157 return m_arrayBufferContentsArray.get(); |
136 } | 158 } |
137 ImageBitmapContentsArray* getImageBitmapContentsArray() { | 159 ImageBitmapContentsArray* getImageBitmapContentsArray() { |
138 return m_imageBitmapContentsArray.get(); | 160 return m_imageBitmapContentsArray.get(); |
139 } | 161 } |
140 | 162 |
141 private: | 163 private: |
142 friend class ScriptValueSerializer; | 164 friend class ScriptValueSerializer; |
143 friend class V8ScriptValueSerializer; | 165 friend class V8ScriptValueSerializer; |
144 | 166 |
145 enum StringDataMode { StringValue, WireData }; | 167 struct BufferDeleter { |
| 168 void operator()(uint8_t* buffer) { WTF::Partitions::bufferFree(buffer); } |
| 169 }; |
| 170 using DataBufferPtr = std::unique_ptr<uint8_t[], BufferDeleter>; |
146 | 171 |
147 SerializedScriptValue(); | 172 SerializedScriptValue(); |
148 explicit SerializedScriptValue(const String& wireData); | 173 explicit SerializedScriptValue(const String& wireData); |
149 | 174 |
150 void setData(const String& data) { m_data = data; } | 175 void setData(const String& data) { |
| 176 m_dataString = data; |
| 177 m_dataBuffer.reset(); |
| 178 m_dataBufferSize = 0; |
| 179 } |
| 180 void setData(DataBufferPtr data, size_t size) { |
| 181 m_dataString = String(); |
| 182 m_dataBuffer = std::move(data); |
| 183 m_dataBufferSize = size; |
| 184 } |
| 185 |
151 void transferArrayBuffers(v8::Isolate*, | 186 void transferArrayBuffers(v8::Isolate*, |
152 const ArrayBufferArray&, | 187 const ArrayBufferArray&, |
153 ExceptionState&); | 188 ExceptionState&); |
154 void transferImageBitmaps(v8::Isolate*, | 189 void transferImageBitmaps(v8::Isolate*, |
155 const ImageBitmapArray&, | 190 const ImageBitmapArray&, |
156 ExceptionState&); | 191 ExceptionState&); |
157 void transferOffscreenCanvas(v8::Isolate*, | 192 void transferOffscreenCanvas(v8::Isolate*, |
158 const OffscreenCanvasArray&, | 193 const OffscreenCanvasArray&, |
159 ExceptionState&); | 194 ExceptionState&); |
160 | 195 |
161 String m_data; | 196 // Either: |
| 197 // - |m_dataString| is non-null, and contains the data as a WTF::String which, |
| 198 // when made 16-bit, is the serialized data (padded to a two-byte boundary), |
| 199 // or |
| 200 // - |m_dataBuffer| is non-null, and |m_dataBufferSize| contains its size; |
| 201 // unlike |m_dataString|, that size is not guaranteed to be padded to a |
| 202 // two-byte boundary |
| 203 String m_dataString; |
| 204 DataBufferPtr m_dataBuffer; |
| 205 size_t m_dataBufferSize = 0; |
| 206 |
162 std::unique_ptr<ArrayBufferContentsArray> m_arrayBufferContentsArray; | 207 std::unique_ptr<ArrayBufferContentsArray> m_arrayBufferContentsArray; |
163 std::unique_ptr<ImageBitmapContentsArray> m_imageBitmapContentsArray; | 208 std::unique_ptr<ImageBitmapContentsArray> m_imageBitmapContentsArray; |
164 BlobDataHandleMap m_blobDataHandles; | 209 BlobDataHandleMap m_blobDataHandles; |
165 intptr_t m_externallyAllocatedMemory; | 210 intptr_t m_externallyAllocatedMemory; |
166 }; | 211 }; |
167 | 212 |
168 } // namespace blink | 213 } // namespace blink |
169 | 214 |
170 #endif // SerializedScriptValue_h | 215 #endif // SerializedScriptValue_h |
OLD | NEW |