ScriptPromise Permissions::query(ScriptState* scriptState, const Dictionary& rawPermission)
{
    WebPermissionClient* client = getClient(scriptState->executionContext());
    if (!client)
        return ScriptPromise::rejectWithDOMException(scriptState, DOMException::create(InvalidStateError, "In its current state, the global scope can't query permissions."));

    TrackExceptionState exceptionState;
    PermissionDescriptor permission = NativeValueTraits<PermissionDescriptor>::nativeValue(scriptState->isolate(), rawPermission.v8Value(), exceptionState);

    if (exceptionState.hadException())
        return ScriptPromise::reject(scriptState, v8::Exception::TypeError(v8String(scriptState->isolate(), exceptionState.message())));

    ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState);
    ScriptPromise promise = resolver->promise();

    WebPermissionType type = getPermissionType(scriptState, rawPermission, permission, exceptionState);
    if (handleNotSupportedPermission(scriptState, rawPermission, resolver, type, exceptionState))
        return promise;

    // If the current origin is a file scheme, it will unlikely return a
    // meaningful value because most APIs are broken on file scheme and no
    // permission prompt will be shown even if the returned permission will most
    // likely be "prompt".
    client->queryPermission(type, KURL(KURL(), scriptState->executionContext()->securityOrigin()->toString()), new PermissionCallback(resolver, type));
    return promise;
}
ScriptPromise Permissions::query(ScriptState* scriptState, const ScriptValue& rawPermission)
{
    WebPermissionClient* client = permissionClient(scriptState->executionContext());
    if (!client)
        return ScriptPromise::rejectWithDOMException(scriptState, DOMException::create(InvalidStateError, "In its current state, the global scope can't query permissions."));

    TrackExceptionState exceptionState;
    PermissionDescriptor permission = NativeValueTraits<PermissionDescriptor>::nativeValue(scriptState->isolate(), rawPermission.v8Value(), exceptionState);


    if (exceptionState.hadException())
        return ScriptPromise::reject(scriptState, v8::Exception::TypeError(v8String(scriptState->isolate(), exceptionState.message())));

    RefPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver::create(scriptState);
    ScriptPromise promise = resolver->promise();

    String name = permission.name();
    WebPermissionType type;
    if (name == "geolocation") {
        type = WebPermissionTypeGeolocation;
    } else if (name == "notifications") {
        type = WebPermissionTypeNotifications;
    } else if (name == "push") {
        PushPermissionDescriptor pushPermission = NativeValueTraits<PushPermissionDescriptor>::nativeValue(scriptState->isolate(), rawPermission.v8Value(), exceptionState);
        // Only "userVisible" push is supported for now.
        if (!pushPermission.userVisible()) {
            resolver->resolve(PermissionStatus::create(scriptState->executionContext(), WebPermissionStatusDenied, WebPermissionTypePush));
            return promise;
        }
        type = WebPermissionTypePushNotifications;
    } else if (name == "midi") {
        MidiPermissionDescriptor midiPermission = NativeValueTraits<MidiPermissionDescriptor>::nativeValue(scriptState->isolate(), rawPermission.v8Value(), exceptionState);
        // Only sysex usage requires a permission for now.
        if (!midiPermission.sysex()) {
            resolver->resolve(PermissionStatus::create(scriptState->executionContext(), WebPermissionStatusGranted, WebPermissionTypeMidi));
            return promise;
        }
        type = WebPermissionTypeMidiSysEx;
    } else {
        ASSERT_NOT_REACHED();
        type = WebPermissionTypeGeolocation;
    }

    // If the current origin is a file scheme, it will unlikely return a
    // meaningful value because most APIs are broken on file scheme and no
    // permission prompt will be shown even if the returned permission will most
    // likely be "prompt".
    client->queryPermission(type, KURL(KURL(), scriptState->executionContext()->securityOrigin()->toString()), new PermissionQueryCallback(resolver, type));
    return promise;
}
ScriptPromise Permissions::query(ScriptState* scriptState, const Dictionary& rawPermission)
{
    WebPermissionClient* client = getClient(scriptState->getExecutionContext());
    if (!client)
        return ScriptPromise::rejectWithDOMException(scriptState, DOMException::create(InvalidStateError, "In its current state, the global scope can't query permissions."));

    ExceptionState exceptionState(ExceptionState::GetterContext,  "query", "Permissions", scriptState->context()->Global(), scriptState->isolate());
    Nullable<WebPermissionType> type = parsePermission(scriptState, rawPermission, exceptionState);
    if (exceptionState.hadException())
        return exceptionState.reject(scriptState);

    ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState);
    ScriptPromise promise = resolver->promise();

    // If the current origin is a file scheme, it will unlikely return a
    // meaningful value because most APIs are broken on file scheme and no
    // permission prompt will be shown even if the returned permission will most
    // likely be "prompt".
    client->queryPermission(type.get(), KURL(KURL(), scriptState->getExecutionContext()->getSecurityOrigin()->toString()), new PermissionCallback(resolver, type.get()));
    return promise;
}