1 // Copyright 2014 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #include "config.h" 6 #include "RequestInit.h" 7 8 #include "bindings/core/v8/Dictionary.h" 9 #include "bindings/core/v8/V8Binding.h" 10 #include "bindings/core/v8/V8Blob.h" 11 #include "bindings/core/v8/V8FormData.h" 12 #include "bindings/core/v8/custom/V8ArrayBufferCustom.h" 13 #include "bindings/core/v8/custom/V8ArrayBufferViewCustom.h" 14 #include "core/fileapi/Blob.h" 15 #include "modules/serviceworkers/Headers.h" 16 #include "platform/blob/BlobData.h" 17 #include "platform/network/FormData.h" 18 #include "wtf/ArrayBuffer.h" 19 20 namespace blink { 21 22 RequestInit::RequestInit(ExecutionContext* context, const Dictionary& options, ExceptionState& exceptionState) 23 { 24 DictionaryHelper::get(options, "method", method); 25 DictionaryHelper::get(options, "headers", headers); 26 if (!headers) { 27 DictionaryHelper::get(options, "headers", headersDictionary); 28 } 29 DictionaryHelper::get(options, "mode", mode); 30 DictionaryHelper::get(options, "credentials", credentials); 31 32 v8::Local<v8::Value> body; 33 if (!DictionaryHelper::get(options, "body", body) || body->IsUndefined() || body->IsNull()) 34 return; 35 OwnPtr<BlobData> blobData = BlobData::create(); 36 v8::Isolate* isolate = toIsolate(context); 37 if (body->IsArrayBuffer()) { 38 ArrayBuffer* arrayBuffer = V8ArrayBuffer::toImpl(v8::Handle<v8::Object>::Cast(body)); 39 ASSERT(arrayBuffer); 40 blobData->appendArrayBuffer(arrayBuffer); 41 } else if (body->IsArrayBufferView()) { 42 ArrayBufferView* arrayBufferView = V8ArrayBufferView::toImpl(v8::Handle<v8::Object>::Cast(body)); 43 ASSERT(arrayBufferView); 44 blobData->appendArrayBufferView(arrayBufferView); 45 } else if (V8Blob::hasInstance(body, isolate)) { 46 Blob* blob = V8Blob::toImpl(v8::Handle<v8::Object>::Cast(body)); 47 ASSERT(blob); 48 blob->appendTo(*blobData); 49 blobData->setContentType(blob->type()); 50 } else if (V8FormData::hasInstance(body, isolate)) { 51 DOMFormData* domFormData = V8FormData::toImpl(v8::Handle<v8::Object>::Cast(body)); 52 ASSERT(domFormData); 53 RefPtr<FormData> httpBody = domFormData->createMultiPartFormData(); 54 for (size_t i = 0; i < httpBody->elements().size(); ++i) { 55 const FormDataElement& element = httpBody->elements()[i]; 56 switch (element.m_type) { 57 case FormDataElement::data: { 58 blobData->appendBytes(element.m_data.data(), element.m_data.size()); 59 break; 60 } 61 case FormDataElement::encodedFile: 62 blobData->appendFile(element.m_filename, element.m_fileStart, element.m_fileLength, element.m_expectedFileModificationTime); 63 break; 64 case FormDataElement::encodedBlob: 65 if (element.m_optionalBlobDataHandle) 66 blobData->appendBlob(element.m_optionalBlobDataHandle, 0, element.m_optionalBlobDataHandle->size()); 67 break; 68 case FormDataElement::encodedFileSystemURL: 69 blobData->appendFileSystemURL(element.m_fileSystemURL, element.m_fileStart, element.m_fileLength, element.m_expectedFileModificationTime); 70 break; 71 default: 72 ASSERT_NOT_REACHED(); 73 } 74 } 75 blobData->setContentType(AtomicString("multipart/form-data; boundary=", AtomicString::ConstructFromLiteral) + httpBody->boundary().data()); 76 } else if (body->IsString()) { 77 String stringValue(toScalarValueString(body, exceptionState)); 78 blobData->appendText(stringValue, false); 79 blobData->setContentType("text/plain;charset=UTF-8"); 80 } else { 81 return; 82 } 83 const long long blobSize = blobData->length(); 84 bodyBlobHandle = BlobDataHandle::create(blobData.release(), blobSize); 85 } 86 87 } 88