| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (C) 2012 Google Inc. All rights reserved. | 2 * Copyright (C) 2013 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 |
| 11 * copyright notice, this list of conditions and the following disclaimer | 11 * copyright notice, this list of conditions and the following disclaimer |
| 12 * in the documentation and/or other materials provided with the | 12 * in the documentation and/or other materials provided with the |
| 13 * distribution. | 13 * distribution. |
| 14 * * Neither the name of Google Inc. nor the names of its | 14 * * Neither the name of Google Inc. nor the names of its |
| 15 * contributors may be used to endorse or promote products derived from | 15 * contributors may be used to endorse or promote products derived from |
| 16 * this software without specific prior written permission. | 16 * this software without specific prior written permission. |
| 17 * | 17 * |
| 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | 19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | 20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | 21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | 22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | 23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 */ | 29 */ |
| 30 | 30 |
| 31 #include "config.h" | 31 #include "config.h" |
| 32 #include "V8BlobCustomHelpers.h" |
| 33 |
| 32 #include "V8Blob.h" | 34 #include "V8Blob.h" |
| 33 | |
| 34 #include "bindings/v8/Dictionary.h" | 35 #include "bindings/v8/Dictionary.h" |
| 35 #include "bindings/v8/ExceptionMessages.h" | |
| 36 #include "bindings/v8/V8Binding.h" | 36 #include "bindings/v8/V8Binding.h" |
| 37 #include "bindings/v8/V8Utilities.h" | 37 #include "bindings/v8/V8Utilities.h" |
| 38 #include "bindings/v8/custom/V8ArrayBufferCustom.h" | 38 #include "bindings/v8/custom/V8ArrayBufferCustom.h" |
| 39 #include "bindings/v8/custom/V8ArrayBufferViewCustom.h" | 39 #include "bindings/v8/custom/V8ArrayBufferViewCustom.h" |
| 40 #include "core/fileapi/BlobBuilder.h" | 40 #include "core/fileapi/BlobBuilder.h" |
| 41 #include "wtf/RefPtr.h" | |
| 42 | 41 |
| 43 namespace WebCore { | 42 namespace WebCore { |
| 44 | 43 |
| 45 void V8Blob::constructorCustom(const v8::FunctionCallbackInfo<v8::Value>& info) | 44 namespace V8BlobCustomHelpers { |
| 45 |
| 46 bool processBlobPropertyBag(v8::Local<v8::Value> propertyBag, v8::Isolate* isola
te, const char* blobClassName, String& contentType, String& endings) |
| 46 { | 47 { |
| 47 if (!info.Length()) { | 48 V8TRYCATCH_RETURN(Dictionary, dictionary, Dictionary(propertyBag, isolate),
false); |
| 48 RefPtr<Blob> blob = Blob::create(); | |
| 49 info.GetReturnValue().Set(toV8(blob.get(), info.Holder(), info.GetIsolat
e())); | |
| 50 return; | |
| 51 } | |
| 52 | 49 |
| 53 uint32_t length = 0; | 50 V8TRYCATCH_RETURN(bool, containsEndings, dictionary.get("endings", endings),
false); |
| 54 if (info[0]->IsArray()) { | 51 if (containsEndings) { |
| 55 length = v8::Local<v8::Array>::Cast(info[0])->Length(); | 52 if (endings != "transparent" && endings != "native") { |
| 56 } else { | 53 throwTypeError(ExceptionMessages::failedToConstruct(blobClassName, "
The \"endings\" property must be either \"transparent\" or \"native\"."), isolat
e); |
| 57 const int sequenceArgumentIndex = 0; | 54 return false; |
| 58 if (toV8Sequence(info[sequenceArgumentIndex], length, info.GetIsolate())
.IsEmpty()) { | |
| 59 throwTypeError(ExceptionMessages::failedToConstruct("Blob", Exceptio
nMessages::notAnArrayTypeArgumentOrValue(sequenceArgumentIndex + 1)), info.GetIs
olate()); | |
| 60 return; | |
| 61 } | 55 } |
| 62 } | 56 } |
| 63 | 57 |
| 64 String type; | 58 V8TRYCATCH_RETURN(bool, containsType, dictionary.get("type", contentType), f
alse); |
| 65 String endings = "transparent"; | 59 if (containsType) { |
| 60 if (!contentType.containsOnlyASCII()) { |
| 61 throwError(v8SyntaxError, ExceptionMessages::failedToConstruct(blobC
lassName, "The \"type\" property must consist of ASCII characters."), isolate); |
| 62 return false; |
| 63 } |
| 64 contentType = contentType.lower(); |
| 65 } |
| 66 return true; |
| 67 } |
| 66 | 68 |
| 67 if (info.Length() > 1) { | 69 bool processBlobParts(v8::Local<v8::Object> blobParts, uint32_t blobPartsLength,
v8::Isolate* isolate, const String& endings, BlobBuilder& blobBuilder) |
| 68 if (!info[1]->IsObject()) { | 70 { |
| 69 throwTypeError(ExceptionMessages::failedToConstruct("Blob", "The 2nd
argument is not of type Object."), info.GetIsolate()); | 71 for (uint32_t i = 0; i < blobPartsLength; ++i) { |
| 70 return; | 72 v8::Local<v8::Value> item = blobParts->Get(v8::Uint32::New(i, isolate)); |
| 71 } | |
| 72 | |
| 73 V8TRYCATCH_VOID(Dictionary, dictionary, Dictionary(info[1], info.GetIsol
ate())); | |
| 74 | |
| 75 V8TRYCATCH_VOID(bool, containsEndings, dictionary.get("endings", endings
)); | |
| 76 if (containsEndings) { | |
| 77 if (endings != "transparent" && endings != "native") { | |
| 78 throwTypeError(ExceptionMessages::failedToConstruct("Blob", "The
2nd argument's \"endings\" property must be either \"transparent\" or \"native\
"."), info.GetIsolate()); | |
| 79 return; | |
| 80 } | |
| 81 } | |
| 82 | |
| 83 V8TRYCATCH_VOID(bool, containsType, dictionary.get("type", type)); | |
| 84 UNUSED_PARAM(containsType); | |
| 85 if (!type.containsOnlyASCII()) { | |
| 86 throwError(v8SyntaxError, ExceptionMessages::failedToConstruct("Blob
", "The 2nd argument's \"type\" property must consist of ASCII characters."), in
fo.GetIsolate()); | |
| 87 return; | |
| 88 } | |
| 89 type = type.lower(); | |
| 90 } | |
| 91 | |
| 92 ASSERT(endings == "transparent" || endings == "native"); | |
| 93 | |
| 94 BlobBuilder blobBuilder; | |
| 95 v8::Local<v8::Object> blobParts = v8::Local<v8::Object>::Cast(info[0]); | |
| 96 for (uint32_t i = 0; i < length; ++i) { | |
| 97 v8::Local<v8::Value> item = blobParts->Get(v8::Uint32::New(i, info.GetIs
olate())); | |
| 98 ASSERT(!item.IsEmpty()); | 73 ASSERT(!item.IsEmpty()); |
| 99 if (V8ArrayBuffer::hasInstance(item, info.GetIsolate(), worldType(info.G
etIsolate()))) { | 74 if (V8ArrayBuffer::hasInstance(item, isolate, worldType(isolate))) { |
| 100 ArrayBuffer* arrayBuffer = V8ArrayBuffer::toNative(v8::Handle<v8::Ob
ject>::Cast(item)); | 75 ArrayBuffer* arrayBuffer = V8ArrayBuffer::toNative(v8::Handle<v8::Ob
ject>::Cast(item)); |
| 101 ASSERT(arrayBuffer); | 76 ASSERT(arrayBuffer); |
| 102 blobBuilder.append(arrayBuffer); | 77 blobBuilder.append(arrayBuffer); |
| 103 } else if (V8ArrayBufferView::hasInstance(item, info.GetIsolate(), world
Type(info.GetIsolate()))) { | 78 } else if (V8ArrayBufferView::hasInstance(item, isolate, worldType(isola
te))) { |
| 104 ArrayBufferView* arrayBufferView = V8ArrayBufferView::toNative(v8::H
andle<v8::Object>::Cast(item)); | 79 ArrayBufferView* arrayBufferView = V8ArrayBufferView::toNative(v8::H
andle<v8::Object>::Cast(item)); |
| 105 ASSERT(arrayBufferView); | 80 ASSERT(arrayBufferView); |
| 106 blobBuilder.append(arrayBufferView); | 81 blobBuilder.append(arrayBufferView); |
| 107 } else if (V8Blob::hasInstance(item, info.GetIsolate(), worldType(info.G
etIsolate()))) { | 82 } else if (V8Blob::hasInstance(item, isolate, worldType(isolate))) { |
| 108 Blob* blob = V8Blob::toNative(v8::Handle<v8::Object>::Cast(item)); | 83 Blob* blob = V8Blob::toNative(v8::Handle<v8::Object>::Cast(item)); |
| 109 ASSERT(blob); | 84 ASSERT(blob); |
| 110 blobBuilder.append(blob); | 85 blobBuilder.append(blob); |
| 111 } else { | 86 } else { |
| 112 V8TRYCATCH_FOR_V8STRINGRESOURCE_VOID(V8StringResource<>, stringValue
, item); | 87 V8TRYCATCH_FOR_V8STRINGRESOURCE_RETURN(V8StringResource<>, stringVal
ue, item, false); |
| 113 blobBuilder.append(stringValue, endings); | 88 blobBuilder.append(stringValue, endings); |
| 114 } | 89 } |
| 115 } | 90 } |
| 116 | 91 return true; |
| 117 RefPtr<Blob> blob = blobBuilder.getBlob(type); | |
| 118 info.GetReturnValue().Set(toV8(blob.get(), info.Holder(), info.GetIsolate())
); | |
| 119 } | 92 } |
| 120 | 93 |
| 94 } // namespace V8BlobCustomHelpers |
| 95 |
| 121 } // namespace WebCore | 96 } // namespace WebCore |
| OLD | NEW |