void V8ArrayBufferOrArrayBufferViewOrDictionary::toImpl(v8::Isolate* isolate, v8::Local<v8::Value> v8Value, ArrayBufferOrArrayBufferViewOrDictionary& impl, UnionTypeConversionMode conversionMode, ExceptionState& exceptionState)
{
    if (v8Value.IsEmpty())
        return;

    if (conversionMode == UnionTypeConversionMode::Nullable && isUndefinedOrNull(v8Value))
        return;

    if (v8Value->IsArrayBuffer()) {
        TestArrayBuffer* cppValue = V8ArrayBuffer::toImpl(v8::Local<v8::Object>::Cast(v8Value));
        impl.setArrayBuffer(cppValue);
        return;
    }

    if (v8Value->IsArrayBufferView()) {
        TestArrayBufferView* cppValue = V8ArrayBufferView::toImpl(v8::Local<v8::Object>::Cast(v8Value));
        impl.setArrayBufferView(cppValue);
        return;
    }

    if (isUndefinedOrNull(v8Value) || v8Value->IsObject()) {
        Dictionary cppValue = Dictionary(v8Value, isolate, exceptionState);
        if (exceptionState.hadException())
            return;
        impl.setDictionary(cppValue);
        return;
    }

    exceptionState.throwTypeError("The provided value is not of type '(ArrayBuffer or ArrayBufferView or Dictionary)'");
}
예제 #2
0
v8::Local<v8::Value> toV8(const ArrayBufferOrArrayBufferViewOrDictionary& impl, v8::Local<v8::Object> creationContext, v8::Isolate* isolate) {
  switch (impl.m_type) {
    case ArrayBufferOrArrayBufferViewOrDictionary::SpecificTypeNone:
      return v8::Null(isolate);
    case ArrayBufferOrArrayBufferViewOrDictionary::SpecificTypeArrayBuffer:
      return toV8(impl.getAsArrayBuffer(), creationContext, isolate);
    case ArrayBufferOrArrayBufferViewOrDictionary::SpecificTypeArrayBufferView:
      return toV8(impl.getAsArrayBufferView(), creationContext, isolate);
    case ArrayBufferOrArrayBufferViewOrDictionary::SpecificTypeDictionary:
      return impl.getAsDictionary().v8Value();
    default:
      NOTREACHED();
  }
  return v8::Local<v8::Value>();
}
예제 #3
0
ScriptPromise SubtleCrypto::importKey(ScriptState* scriptState, const String& rawFormat, const ArrayBufferOrArrayBufferViewOrDictionary& keyData, const AlgorithmIdentifier& rawAlgorithm, bool extractable, const Vector<String>& rawKeyUsages)
{
    RefPtrWillBeRawPtr<CryptoResultImpl> result = CryptoResultImpl::create(scriptState);
    ScriptPromise promise = result->promise();

    if (!canAccessWebCrypto(scriptState, result.get()))
        return promise;

    WebCryptoKeyFormat format;
    if (!CryptoKey::parseFormat(rawFormat, format, result.get()))
        return promise;

    if (keyData.isDictionary()) {
        if (format != WebCryptoKeyFormatJwk) {
            result->completeWithError(WebCryptoErrorTypeData, "Key data must be a buffer for non-JWK formats");
            return promise;
        }
    } else if (format == WebCryptoKeyFormatJwk) {
        result->completeWithError(WebCryptoErrorTypeData, "Key data must be an object for JWK import");
        return promise;
    }

    WebCryptoKeyUsageMask keyUsages;
    if (!CryptoKey::parseUsageMask(rawKeyUsages, keyUsages, result.get()))
        return promise;

    WebCryptoAlgorithm algorithm;
    if (!parseAlgorithm(rawAlgorithm, WebCryptoOperationImportKey, algorithm, result.get()))
        return promise;

    const unsigned char* ptr = nullptr;
    unsigned len = 0;

    CString jsonUtf8;
    if (keyData.isArrayBuffer()) {
        ptr = static_cast<const unsigned char*>(keyData.getAsArrayBuffer()->data());
        len = keyData.getAsArrayBuffer()->byteLength();
    } else if (keyData.isArrayBufferView()) {
        ptr = static_cast<const unsigned char*>(keyData.getAsArrayBufferView()->baseAddress());
        len = keyData.getAsArrayBufferView()->byteLength();
    } else if (keyData.isDictionary()) {
        if (!copyJwkDictionaryToJson(keyData.getAsDictionary(), jsonUtf8, result.get()))
            return promise;
        ptr = reinterpret_cast<const unsigned char*>(jsonUtf8.data());
        len = jsonUtf8.length();
    }
    histogramAlgorithm(scriptState->executionContext(), algorithm);
    Platform::current()->crypto()->importKey(format, ptr, len, algorithm, extractable, keyUsages, result->result());
    return promise;
}
ArrayBufferOrArrayBufferViewOrDictionary ArrayBufferOrArrayBufferViewOrDictionary::fromDictionary(Dictionary value)
{
    ArrayBufferOrArrayBufferViewOrDictionary container;
    container.setDictionary(value);
    return container;
}
ArrayBufferOrArrayBufferViewOrDictionary ArrayBufferOrArrayBufferViewOrDictionary::fromArrayBufferView(TestArrayBufferView* value)
{
    ArrayBufferOrArrayBufferViewOrDictionary container;
    container.setArrayBufferView(value);
    return container;
}