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

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

Issue 1409243005: Make ImageBitmap Transferable (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix blank line Created 5 years, 1 month 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 /* 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 20 matching lines...) Expand all
31 #include "config.h" 31 #include "config.h"
32 #include "bindings/core/v8/SerializedScriptValue.h" 32 #include "bindings/core/v8/SerializedScriptValue.h"
33 33
34 #include "bindings/core/v8/DOMDataStore.h" 34 #include "bindings/core/v8/DOMDataStore.h"
35 #include "bindings/core/v8/DOMWrapperWorld.h" 35 #include "bindings/core/v8/DOMWrapperWorld.h"
36 #include "bindings/core/v8/ExceptionState.h" 36 #include "bindings/core/v8/ExceptionState.h"
37 #include "bindings/core/v8/ScriptState.h" 37 #include "bindings/core/v8/ScriptState.h"
38 #include "bindings/core/v8/ScriptValueSerializer.h" 38 #include "bindings/core/v8/ScriptValueSerializer.h"
39 #include "bindings/core/v8/SerializedScriptValueFactory.h" 39 #include "bindings/core/v8/SerializedScriptValueFactory.h"
40 #include "bindings/core/v8/V8ArrayBuffer.h" 40 #include "bindings/core/v8/V8ArrayBuffer.h"
41 #include "bindings/core/v8/V8ImageBitmap.h"
41 #include "bindings/core/v8/V8MessagePort.h" 42 #include "bindings/core/v8/V8MessagePort.h"
42 #include "bindings/core/v8/V8SharedArrayBuffer.h" 43 #include "bindings/core/v8/V8SharedArrayBuffer.h"
43 #include "core/dom/ExceptionCode.h" 44 #include "core/dom/ExceptionCode.h"
44 #include "platform/SharedBuffer.h" 45 #include "platform/SharedBuffer.h"
45 #include "platform/blob/BlobData.h" 46 #include "platform/blob/BlobData.h"
46 #include "platform/heap/Handle.h" 47 #include "platform/heap/Handle.h"
47 #include "wtf/Assertions.h" 48 #include "wtf/Assertions.h"
48 #include "wtf/ByteOrder.h" 49 #include "wtf/ByteOrder.h"
49 #include "wtf/Vector.h" 50 #include "wtf/Vector.h"
50 #include "wtf/text/StringBuffer.h" 51 #include "wtf/text/StringBuffer.h"
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
94 if (!wrapper.IsEmpty()) 95 if (!wrapper.IsEmpty())
95 buffers.append(v8::Local<v8::ArrayBuffer>::Cast(wrapper)); 96 buffers.append(v8::Local<v8::ArrayBuffer>::Cast(wrapper));
96 } 97 }
97 } else { 98 } else {
98 v8::Local<v8::Object> wrapper = DOMWrapperWorld::current(isolate).domDat aStore().get(object, isolate); 99 v8::Local<v8::Object> wrapper = DOMWrapperWorld::current(isolate).domDat aStore().get(object, isolate);
99 if (!wrapper.IsEmpty()) 100 if (!wrapper.IsEmpty())
100 buffers.append(v8::Local<v8::ArrayBuffer>::Cast(wrapper)); 101 buffers.append(v8::Local<v8::ArrayBuffer>::Cast(wrapper));
101 } 102 }
102 } 103 }
103 104
105 PassOwnPtr<ImageBitmapArray> SerializedScriptValue::createImageBitmaps(v8::Isola te* isolate, ImageBitmapArray& imageBitmaps, ExceptionState& exceptionState)
106 {
107 ASSERT(imageBitmaps.size());
108
109 for (size_t i = 0; i < imageBitmaps.size(); i++) {
110 if (imageBitmaps[i]->isNeutered()) {
111 exceptionState.throwDOMException(DataCloneError, "ImageBitmap at ind ex " + String::number(i) + " is already neutered.");
112 return nullptr;
113 }
114 }
115
116 OwnPtr<ImageBitmapArray> contents = adoptPtr(new ImageBitmapArray(imageBitma ps.size()));
117 HashSet<ImageBitmap*> visited;
118 for (size_t i = 0; i < imageBitmaps.size(); i++) {
119 if (visited.contains(imageBitmaps[i].get()))
120 continue;
121 visited.add(imageBitmaps[i].get());
122 contents->at(i) = imageBitmaps[i]->transfer();
123 imageBitmaps[i]->neuter();
124 }
125
126 return contents.release();
127 }
128
104 PassOwnPtr<SerializedScriptValue::ArrayBufferContentsArray> SerializedScriptValu e::createArrayBuffers(v8::Isolate* isolate, ArrayBufferArray& arrayBuffers, Exce ptionState& exceptionState) 129 PassOwnPtr<SerializedScriptValue::ArrayBufferContentsArray> SerializedScriptValu e::createArrayBuffers(v8::Isolate* isolate, ArrayBufferArray& arrayBuffers, Exce ptionState& exceptionState)
105 { 130 {
106 ASSERT(arrayBuffers.size()); 131 ASSERT(arrayBuffers.size());
107 132
108 for (size_t i = 0; i < arrayBuffers.size(); i++) { 133 for (size_t i = 0; i < arrayBuffers.size(); i++) {
109 if (arrayBuffers[i]->isNeutered()) { 134 if (arrayBuffers[i]->isNeutered()) {
110 exceptionState.throwDOMException(DataCloneError, "ArrayBuffer at ind ex " + String::number(i) + " is already neutered."); 135 exceptionState.throwDOMException(DataCloneError, "ArrayBuffer at ind ex " + String::number(i) + " is already neutered.");
111 return nullptr; 136 return nullptr;
112 } 137 }
113 } 138 }
(...skipping 12 matching lines...) Expand all
126 exceptionState.throwDOMException(DataCloneError, "SharedArrayBuf fer at index " + String::number(i) + " could not be transferred."); 151 exceptionState.throwDOMException(DataCloneError, "SharedArrayBuf fer at index " + String::number(i) + " could not be transferred.");
127 return nullptr; 152 return nullptr;
128 } 153 }
129 } else { 154 } else {
130 Vector<v8::Local<v8::ArrayBuffer>, 4> bufferHandles; 155 Vector<v8::Local<v8::ArrayBuffer>, 4> bufferHandles;
131 v8::HandleScope handleScope(isolate); 156 v8::HandleScope handleScope(isolate);
132 acculumateArrayBuffersForAllWorlds(isolate, static_pointer_cast<DOMA rrayBuffer>(arrayBuffers[i]).get(), bufferHandles); 157 acculumateArrayBuffersForAllWorlds(isolate, static_pointer_cast<DOMA rrayBuffer>(arrayBuffers[i]).get(), bufferHandles);
133 bool isNeuterable = true; 158 bool isNeuterable = true;
134 for (size_t j = 0; j < bufferHandles.size(); j++) 159 for (size_t j = 0; j < bufferHandles.size(); j++)
135 isNeuterable &= bufferHandles[j]->IsNeuterable(); 160 isNeuterable &= bufferHandles[j]->IsNeuterable();
136
137 RefPtr<DOMArrayBufferBase> toTransfer = arrayBuffers[i]; 161 RefPtr<DOMArrayBufferBase> toTransfer = arrayBuffers[i];
138 if (!isNeuterable) 162 if (!isNeuterable)
139 toTransfer = DOMArrayBuffer::create(arrayBuffers[i]->buffer()); 163 toTransfer = DOMArrayBuffer::create(arrayBuffers[i]->buffer());
140 bool result = toTransfer->transfer(contents->at(i)); 164 bool result = toTransfer->transfer(contents->at(i));
141 if (!result) { 165 if (!result) {
142 exceptionState.throwDOMException(DataCloneError, "ArrayBuffer at index " + String::number(i) + " could not be transferred."); 166 exceptionState.throwDOMException(DataCloneError, "ArrayBuffer at index " + String::number(i) + " could not be transferred.");
143 return nullptr; 167 return nullptr;
144 } 168 }
145 169
146 if (isNeuterable) 170 if (isNeuterable)
147 for (size_t j = 0; j < bufferHandles.size(); j++) 171 for (size_t j = 0; j < bufferHandles.size(); j++)
148 bufferHandles[j]->Neuter(); 172 bufferHandles[j]->Neuter();
149 } 173 }
150
151 } 174 }
152 175
153 return contents.release(); 176 return contents.release();
154 } 177 }
155 178
156 SerializedScriptValue::SerializedScriptValue(const String& wireData) 179 SerializedScriptValue::SerializedScriptValue(const String& wireData)
157 : m_externallyAllocatedMemory(0) 180 : m_externallyAllocatedMemory(0)
158 { 181 {
159 m_data = wireData.isolatedCopy(); 182 m_data = wireData.isolatedCopy();
160 } 183 }
161 184
162 v8::Local<v8::Value> SerializedScriptValue::deserialize(MessagePortArray* messag ePorts) 185 v8::Local<v8::Value> SerializedScriptValue::deserialize(MessagePortArray* messag ePorts)
163 { 186 {
164 return deserialize(v8::Isolate::GetCurrent(), messagePorts, 0); 187 return deserialize(v8::Isolate::GetCurrent(), messagePorts, 0);
165 } 188 }
166 189
167 v8::Local<v8::Value> SerializedScriptValue::deserialize(v8::Isolate* isolate, Me ssagePortArray* messagePorts, const WebBlobInfoArray* blobInfo) 190 v8::Local<v8::Value> SerializedScriptValue::deserialize(v8::Isolate* isolate, Me ssagePortArray* messagePorts, const WebBlobInfoArray* blobInfo)
168 { 191 {
169 return SerializedScriptValueFactory::instance().deserialize(this, isolate, m essagePorts, blobInfo); 192 return SerializedScriptValueFactory::instance().deserialize(this, isolate, m essagePorts, blobInfo);
170 } 193 }
171 194
172 bool SerializedScriptValue::extractTransferables(v8::Isolate* isolate, v8::Local <v8::Value> value, int argumentIndex, MessagePortArray& ports, ArrayBufferArray& arrayBuffers, ExceptionState& exceptionState) 195 bool SerializedScriptValue::extractTransferables(v8::Isolate* isolate, v8::Local <v8::Value> value, int argumentIndex, MessagePortArray& ports, ArrayBufferArray& arrayBuffers, ImageBitmapArray& imageBitmaps, ExceptionState& exceptionState)
173 { 196 {
174 if (isUndefinedOrNull(value)) { 197 if (isUndefinedOrNull(value)) {
175 ports.resize(0); 198 ports.resize(0);
176 arrayBuffers.resize(0); 199 arrayBuffers.resize(0);
200 imageBitmaps.resize(0);
177 return true; 201 return true;
178 } 202 }
179 203
180 uint32_t length = 0; 204 uint32_t length = 0;
181 if (value->IsArray()) { 205 if (value->IsArray()) {
182 v8::Local<v8::Array> array = v8::Local<v8::Array>::Cast(value); 206 v8::Local<v8::Array> array = v8::Local<v8::Array>::Cast(value);
183 length = array->Length(); 207 length = array->Length();
184 } else if (!toV8Sequence(value, length, isolate, exceptionState)) { 208 } else if (!toV8Sequence(value, length, isolate, exceptionState)) {
185 if (!exceptionState.hadException()) 209 if (!exceptionState.hadException())
186 exceptionState.throwTypeError(ExceptionMessages::notAnArrayTypeArgum entOrValue(argumentIndex + 1)); 210 exceptionState.throwTypeError(ExceptionMessages::notAnArrayTypeArgum entOrValue(argumentIndex + 1));
(...skipping 28 matching lines...) Expand all
215 return false; 239 return false;
216 } 240 }
217 arrayBuffers.append(arrayBuffer.release()); 241 arrayBuffers.append(arrayBuffer.release());
218 } else if (V8SharedArrayBuffer::hasInstance(transferrable, isolate)) { 242 } else if (V8SharedArrayBuffer::hasInstance(transferrable, isolate)) {
219 RefPtr<DOMSharedArrayBuffer> sharedArrayBuffer = V8SharedArrayBuffer ::toImpl(v8::Local<v8::Object>::Cast(transferrable)); 243 RefPtr<DOMSharedArrayBuffer> sharedArrayBuffer = V8SharedArrayBuffer ::toImpl(v8::Local<v8::Object>::Cast(transferrable));
220 if (arrayBuffers.contains(sharedArrayBuffer)) { 244 if (arrayBuffers.contains(sharedArrayBuffer)) {
221 exceptionState.throwDOMException(DataCloneError, "SharedArrayBuf fer at index " + String::number(i) + " is a duplicate of an earlier SharedArrayB uffer."); 245 exceptionState.throwDOMException(DataCloneError, "SharedArrayBuf fer at index " + String::number(i) + " is a duplicate of an earlier SharedArrayB uffer.");
222 return false; 246 return false;
223 } 247 }
224 arrayBuffers.append(sharedArrayBuffer.release()); 248 arrayBuffers.append(sharedArrayBuffer.release());
249 } else if (V8ImageBitmap::hasInstance(transferrable, isolate)) {
250 RefPtr<ImageBitmap> imageBitmap = V8ImageBitmap::toImpl(v8::Local<v8 ::Object>::Cast(transferrable));
251 if (imageBitmaps.contains(imageBitmap)) {
252 exceptionState.throwDOMException(DataCloneError, "ImageBitmap at index " + String::number(i) + " is a duplicate of an earlier ImageBitmap.");
253 return false;
254 }
255 imageBitmaps.append(imageBitmap.release());
225 } else { 256 } else {
226 exceptionState.throwTypeError("Value at index " + String::number(i) + " does not have a transferable type."); 257 exceptionState.throwTypeError("Value at index " + String::number(i) + " does not have a transferable type.");
227 return false; 258 return false;
228 } 259 }
229 } 260 }
230 return true; 261 return true;
231 } 262 }
232 263
233 void SerializedScriptValue::registerMemoryAllocatedWithCurrentScriptContext() 264 void SerializedScriptValue::registerMemoryAllocatedWithCurrentScriptContext()
234 { 265 {
(...skipping 17 matching lines...) Expand all
252 ASSERT(v8::Isolate::GetCurrent()); 283 ASSERT(v8::Isolate::GetCurrent());
253 v8::Isolate::GetCurrent()->AdjustAmountOfExternalAllocatedMemory(-m_exte rnallyAllocatedMemory); 284 v8::Isolate::GetCurrent()->AdjustAmountOfExternalAllocatedMemory(-m_exte rnallyAllocatedMemory);
254 } 285 }
255 } 286 }
256 287
257 void SerializedScriptValue::transferArrayBuffers(v8::Isolate* isolate, ArrayBuff erArray& arrayBuffers, ExceptionState& exceptionState) 288 void SerializedScriptValue::transferArrayBuffers(v8::Isolate* isolate, ArrayBuff erArray& arrayBuffers, ExceptionState& exceptionState)
258 { 289 {
259 m_arrayBufferContentsArray = createArrayBuffers(isolate, arrayBuffers, excep tionState); 290 m_arrayBufferContentsArray = createArrayBuffers(isolate, arrayBuffers, excep tionState);
260 } 291 }
261 292
293 void SerializedScriptValue::transferImageBitmaps(v8::Isolate* isolate, ImageBitm apArray& imageBitmaps, ExceptionState& exceptionState)
294 {
295 m_imageBitmapsArray = createImageBitmaps(isolate, imageBitmaps, exceptionSta te);
296 }
297
262 } // namespace blink 298 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698