Beispiel #1
0
void Bridge::send(const DOMArrayBuffer& binaryData, unsigned byteOffset, unsigned byteLength)
{
    ASSERT(m_peer);
    // ArrayBuffer isn't thread-safe, hence the content of ArrayBuffer is copied into Vector<char>.
    OwnPtr<Vector<char>> data = adoptPtr(new Vector<char>(byteLength));
    if (binaryData.byteLength())
        memcpy(data->data(), static_cast<const char*>(binaryData.data()) + byteOffset, byteLength);

    m_loaderProxy->postTaskToLoader(createCrossThreadTask(&Peer::sendBinaryAsCharVector, m_peer.get(), data.release()));
}
PushMessageData* PushMessageData::create(const ArrayBufferOrArrayBufferViewOrUSVString& messageData)
{
    if (messageData.isArrayBuffer() || messageData.isArrayBufferView()) {
        DOMArrayBuffer* buffer = messageData.isArrayBufferView()
            ? messageData.getAsArrayBufferView()->buffer()
            : messageData.getAsArrayBuffer();

        return new PushMessageData(static_cast<const char*>(buffer->data()), buffer->byteLength());
    }

    if (messageData.isUSVString()) {
        CString encodedString = UTF8Encoding().encode(messageData.getAsUSVString(), WTF::EntitiesForUnencodables);
        return new PushMessageData(encodedString.data(), encodedString.length());
    }

    DCHECK(messageData.isNull());
    return nullptr;
}
Beispiel #3
0
// static
void Blob::populateBlobData(BlobData* blobData, const HeapVector<ArrayBufferOrArrayBufferViewOrBlobOrUSVString>& parts, bool normalizeLineEndingsToNative)
{
    for (const auto& item : parts) {
        if (item.isArrayBuffer()) {
            DOMArrayBuffer* arrayBuffer = item.getAsArrayBuffer();
            blobData->appendBytes(arrayBuffer->data(), arrayBuffer->byteLength());
        } else if (item.isArrayBufferView()) {
            DOMArrayBufferView* arrayBufferView = item.getAsArrayBufferView();
            blobData->appendBytes(arrayBufferView->baseAddress(), arrayBufferView->byteLength());
        } else if (item.isBlob()) {
            item.getAsBlob()->appendTo(*blobData);
        } else if (item.isUSVString()) {
            blobData->appendText(item.getAsUSVString(), normalizeLineEndingsToNative);
        } else {
            NOTREACHED();
        }
    }
}
RequestInit::RequestInit(ExecutionContext* context, const Dictionary& options, ExceptionState& exceptionState)
{
    DictionaryHelper::get(options, "method", method);
    DictionaryHelper::get(options, "headers", headers);
    if (!headers) {
        Vector<Vector<String>> headersVector;
        if (DictionaryHelper::get(options, "headers", headersVector, exceptionState))
            headers = Headers::create(headersVector, exceptionState);
        else
            DictionaryHelper::get(options, "headers", headersDictionary);
    }
    DictionaryHelper::get(options, "mode", mode);
    DictionaryHelper::get(options, "credentials", credentials);

    v8::Local<v8::Value> body;
    if (!DictionaryHelper::get(options, "body", body) || body->IsUndefined() || body->IsNull())
        return;
    OwnPtr<BlobData> blobData = BlobData::create();
    v8::Isolate* isolate = toIsolate(context);
    if (body->IsArrayBuffer()) {
        DOMArrayBuffer* arrayBuffer = V8ArrayBuffer::toImpl(v8::Local<v8::Object>::Cast(body));
        ASSERT(arrayBuffer);
        blobData->appendBytes(arrayBuffer->data(), arrayBuffer->byteLength());
    } else if (body->IsArrayBufferView()) {
        DOMArrayBufferView* arrayBufferView = V8ArrayBufferView::toImpl(v8::Local<v8::Object>::Cast(body));
        ASSERT(arrayBufferView);
        blobData->appendBytes(arrayBufferView->baseAddress(), arrayBufferView->byteLength());
    } else if (V8Blob::hasInstance(body, isolate)) {
        Blob* blob = V8Blob::toImpl(v8::Local<v8::Object>::Cast(body));
        ASSERT(blob);
        blob->appendTo(*blobData);
        blobData->setContentType(blob->type());
    } else if (V8FormData::hasInstance(body, isolate)) {
        DOMFormData* domFormData = V8FormData::toImpl(v8::Local<v8::Object>::Cast(body));
        ASSERT(domFormData);
        RefPtr<FormData> httpBody = domFormData->createMultiPartFormData();
        for (size_t i = 0; i < httpBody->elements().size(); ++i) {
            const FormDataElement& element = httpBody->elements()[i];
            switch (element.m_type) {
            case FormDataElement::data: {
                blobData->appendBytes(element.m_data.data(), element.m_data.size());
                break;
            }
            case FormDataElement::encodedFile:
                blobData->appendFile(element.m_filename, element.m_fileStart, element.m_fileLength, element.m_expectedFileModificationTime);
                break;
            case FormDataElement::encodedBlob:
                if (element.m_optionalBlobDataHandle)
                    blobData->appendBlob(element.m_optionalBlobDataHandle, 0, element.m_optionalBlobDataHandle->size());
                break;
            case FormDataElement::encodedFileSystemURL:
                blobData->appendFileSystemURL(element.m_fileSystemURL, element.m_fileStart, element.m_fileLength, element.m_expectedFileModificationTime);
                break;
            default:
                ASSERT_NOT_REACHED();
            }
        }
        blobData->setContentType(AtomicString("multipart/form-data; boundary=", AtomicString::ConstructFromLiteral) + httpBody->boundary().data());
    } else if (body->IsString()) {
        String stringValue(toUSVString(isolate, body, exceptionState));
        blobData->appendText(stringValue, false);
        blobData->setContentType("text/plain;charset=UTF-8");
    } else {
        return;
    }
    const long long blobSize = blobData->length();
    bodyBlobHandle = BlobDataHandle::create(blobData.release(), blobSize);
}