| 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/V8Binding.h" | 36 #include "bindings/v8/V8Binding.h" |
| 36 #include "bindings/v8/V8Utilities.h" | 37 #include "bindings/v8/V8Utilities.h" |
| 37 #include "bindings/v8/custom/V8ArrayBufferCustom.h" | 38 #include "bindings/v8/custom/V8ArrayBufferCustom.h" |
| 38 #include "bindings/v8/custom/V8ArrayBufferViewCustom.h" | 39 #include "bindings/v8/custom/V8ArrayBufferViewCustom.h" |
| 39 #include "core/fileapi/BlobBuilder.h" | 40 #include "core/fileapi/BlobBuilder.h" |
| 40 #include "wtf/RefPtr.h" | |
| 41 | 41 |
| 42 namespace WebCore { | 42 namespace WebCore { |
| 43 | 43 |
| 44 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, String& contentType, String& endings) |
| 45 { | 47 { |
| 46 if (!info.Length()) { | 48 V8TRYCATCH_RETURN(Dictionary, dictionary, Dictionary(propertyBag, isolate),
false); |
| 47 RefPtr<Blob> blob = Blob::create(); | 49 |
| 48 info.GetReturnValue().Set(toV8(blob.get(), info.Holder(), info.GetIsolat
e())); | 50 V8TRYCATCH_RETURN(bool, containsEndings, dictionary.get("endings", endings),
false); |
| 49 return; | 51 if (containsEndings) { |
| 52 if (endings != "transparent" && endings != "native") { |
| 53 throwTypeError("The endings property must be either \"transparent\"
or \"native\"", isolate); |
| 54 return false; |
| 55 } |
| 50 } | 56 } |
| 51 | 57 |
| 52 v8::Local<v8::Value> firstArg = info[0]; | 58 V8TRYCATCH_RETURN(bool, containsType, dictionary.get("type", contentType), f
alse); |
| 53 if (!firstArg->IsArray()) { | 59 if (containsType) { |
| 54 throwTypeError("First argument of the constructor is not of type Array",
info.GetIsolate()); | 60 if (!contentType.containsOnlyASCII()) { |
| 55 return; | 61 throwError(v8SyntaxError, "type must consist of ASCII characters", i
solate); |
| 62 return false; |
| 63 } |
| 64 contentType = contentType.lower(); |
| 56 } | 65 } |
| 66 return true; |
| 67 } |
| 57 | 68 |
| 58 String type; | 69 bool processBlobParts(v8::Local<v8::Value> blobParts, v8::Isolate* isolate, cons
t String& endings, BlobBuilder& blobBuilder) |
| 59 String endings = "transparent"; | 70 { |
| 60 | 71 V8TRYCATCH_RETURN(v8::Local<v8::Array>, partsArray, v8::Local<v8::Array>::Ca
st(blobParts), false); |
| 61 if (info.Length() > 1) { | 72 uint32_t length = partsArray->Length(); |
| 62 if (!info[1]->IsObject()) { | |
| 63 throwTypeError("Second argument of the constructor is not of type Ob
ject", info.GetIsolate()); | |
| 64 return; | |
| 65 } | |
| 66 | |
| 67 V8TRYCATCH_VOID(Dictionary, dictionary, Dictionary(info[1], info.GetIsol
ate())); | |
| 68 | |
| 69 V8TRYCATCH_VOID(bool, containsEndings, dictionary.get("endings", endings
)); | |
| 70 if (containsEndings) { | |
| 71 if (endings != "transparent" && endings != "native") { | |
| 72 throwTypeError("The endings property must be either \"transparen
t\" or \"native\"", info.GetIsolate()); | |
| 73 return; | |
| 74 } | |
| 75 } | |
| 76 | |
| 77 V8TRYCATCH_VOID(bool, containsType, dictionary.get("type", type)); | |
| 78 UNUSED_PARAM(containsType); | |
| 79 if (!type.containsOnlyASCII()) { | |
| 80 throwError(v8SyntaxError, "type must consist of ASCII characters", i
nfo.GetIsolate()); | |
| 81 return; | |
| 82 } | |
| 83 type = type.lower(); | |
| 84 } | |
| 85 | |
| 86 ASSERT(endings == "transparent" || endings == "native"); | |
| 87 | |
| 88 BlobBuilder blobBuilder; | |
| 89 | |
| 90 V8TRYCATCH_VOID(v8::Local<v8::Array>, blobParts, v8::Local<v8::Array>::Cast(
firstArg)); | |
| 91 uint32_t length = blobParts->Length(); | |
| 92 | 73 |
| 93 for (uint32_t i = 0; i < length; ++i) { | 74 for (uint32_t i = 0; i < length; ++i) { |
| 94 v8::Local<v8::Value> item = blobParts->Get(v8::Uint32::New(i, info.GetIs
olate())); | 75 v8::Local<v8::Value> item = partsArray->Get(v8::Uint32::New(i, isolate))
; |
| 95 ASSERT(!item.IsEmpty()); | 76 ASSERT(!item.IsEmpty()); |
| 96 if (V8ArrayBuffer::HasInstance(item, info.GetIsolate(), worldType(info.G
etIsolate()))) { | 77 if (V8ArrayBuffer::HasInstance(item, isolate, worldType(isolate))) { |
| 97 ArrayBuffer* arrayBuffer = V8ArrayBuffer::toNative(v8::Handle<v8::Ob
ject>::Cast(item)); | 78 ArrayBuffer* arrayBuffer = V8ArrayBuffer::toNative(v8::Handle<v8::Ob
ject>::Cast(item)); |
| 98 ASSERT(arrayBuffer); | 79 ASSERT(arrayBuffer); |
| 99 blobBuilder.append(arrayBuffer); | 80 blobBuilder.append(arrayBuffer); |
| 100 } else if (V8ArrayBufferView::HasInstance(item, info.GetIsolate(), world
Type(info.GetIsolate()))) { | 81 } else if (V8ArrayBufferView::HasInstance(item, isolate, worldType(isola
te))) { |
| 101 ArrayBufferView* arrayBufferView = V8ArrayBufferView::toNative(v8::H
andle<v8::Object>::Cast(item)); | 82 ArrayBufferView* arrayBufferView = V8ArrayBufferView::toNative(v8::H
andle<v8::Object>::Cast(item)); |
| 102 ASSERT(arrayBufferView); | 83 ASSERT(arrayBufferView); |
| 103 blobBuilder.append(arrayBufferView); | 84 blobBuilder.append(arrayBufferView); |
| 104 } else if (V8Blob::HasInstance(item, info.GetIsolate(), worldType(info.G
etIsolate()))) { | 85 } else if (V8Blob::HasInstance(item, isolate, worldType(isolate))) { |
| 105 Blob* blob = V8Blob::toNative(v8::Handle<v8::Object>::Cast(item)); | 86 Blob* blob = V8Blob::toNative(v8::Handle<v8::Object>::Cast(item)); |
| 106 ASSERT(blob); | 87 ASSERT(blob); |
| 107 blobBuilder.append(blob); | 88 blobBuilder.append(blob); |
| 108 } else { | 89 } else { |
| 109 V8TRYCATCH_FOR_V8STRINGRESOURCE_VOID(V8StringResource<>, stringValue
, item); | 90 V8TRYCATCH_FOR_V8STRINGRESOURCE_RETURN(V8StringResource<>, stringVal
ue, item, false); |
| 110 blobBuilder.append(stringValue, endings); | 91 blobBuilder.append(stringValue, endings); |
| 111 } | 92 } |
| 112 } | 93 } |
| 113 | 94 return true; |
| 114 RefPtr<Blob> blob = blobBuilder.getBlob(type); | |
| 115 info.GetReturnValue().Set(toV8(blob.get(), info.Holder(), info.GetIsolate())
); | |
| 116 } | 95 } |
| 117 | 96 |
| 97 } // namespace V8BlobCustomHelpers |
| 98 |
| 118 } // namespace WebCore | 99 } // namespace WebCore |
| OLD | NEW |