chromium/third_party/blink/renderer/bindings/core/v8/script_promise_resolver.h

// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef THIRD_PARTY_BLINK_RENDERER_BINDINGS_CORE_V8_SCRIPT_PROMISE_RESOLVER_H_
#define THIRD_PARTY_BLINK_RENDERER_BINDINGS_CORE_V8_SCRIPT_PROMISE_RESOLVER_H_

#include "base/dcheck_is_on.h"
#include "base/hash/hash.h"
#include "third_party/blink/renderer/bindings/core/v8/script_promise.h"
#include "third_party/blink/renderer/bindings/core/v8/to_v8_traits.h"
#include "third_party/blink/renderer/bindings/core/v8/v8_binding_for_core.h"
#include "third_party/blink/renderer/core/core_export.h"
#include "third_party/blink/renderer/platform/bindings/dictionary_base.h"
#include "third_party/blink/renderer/platform/bindings/exception_context.h"
#include "third_party/blink/renderer/platform/bindings/scoped_persistent.h"
#include "third_party/blink/renderer/platform/bindings/script_forbidden_scope.h"
#include "third_party/blink/renderer/platform/bindings/script_state.h"
#include "third_party/blink/renderer/platform/bindings/union_base.h"
#include "third_party/blink/renderer/platform/heap/disallow_new_wrapper.h"
#include "third_party/blink/renderer/platform/heap/garbage_collected.h"
#include "third_party/blink/renderer/platform/heap/prefinalizer.h"
#include "third_party/blink/renderer/platform/scheduler/public/post_cancellable_task.h"
#include "third_party/blink/renderer/platform/wtf/functional.h"
#include "v8/include/v8.h"

#if DCHECK_IS_ON()
#include "base/debug/stack_trace.h"
#endif

namespace blink {

template <typename IDLResolvedType>
class ScriptPromiseResolver;

// This class wraps v8::Promise::Resolver for easier use in blink.
//
// A ScriptPromiseResolver must be templated with its IDLResolveType. This
// is the type of the promise specified in the relevant IDL file. For types
// that are part of the IDL language, find the corresponding type in idl_types.h
// (`bool` becomes `IDLBoolean`, `any` becomes `IDLAny`, etc). For anything
// defined in an IDL file (interfaces, dictionaries, unions, enums),  template
// with the C++ object type. If the type is nullable (indicated by a "?" suffix
// in the IDL file), wrap the type in IDLNullable<>. The given idl type will
// determine what values may be passed to `Resolve()` - the IDLResolveType and
// the passed-in type will be forwarded to ToV8Traits<>, so the types must be
// compatible for that purpose. For example, a
// `ScriptPromiseResolver<IDLAny>` may `Resolve()` with  a `ScriptValue` or
// a `v8::Local<v8::Value>`, but a `ScriptPromiseResolver<IDLBoolean>` must
// `Resolve()` with a `bool`.
//
// ScriptPromiseResolverBase is an untyped base class and may not be created
// directly. It should only be used for logic that needs to handle resolvers
// of multiple different IDL types. A ScriptPromiseResolverBase can be rejected
// without being downcasted, because reject types are not specified in the IDL.
// However, it must be downcasted via `DowncastTo<IDLResolveType>()` in order
// to `Resolve()`.
//
// `DowncastTo` will DCHECK in the case of a bad cast. It's not a security issue
// because all ScriptPromiseResolverBase/ScriptPromiseResolver classes have the
// same memory layout and vtable, but it is a correctness issue because the
// promise will be resolved with an incorrect type.
//
// This class retains a ScriptState, so a caller can call resolve or reject from
// outside of a V8 context. When the ScriptState's associated ExecutionContext
// is destroyed, resolve or reject  will be ignored.
//
// There are cases where promises cannot work (e.g., where the thread is being
// terminated). In such cases operations will silently fail.
class CORE_EXPORT ScriptPromiseResolverBase
    : public GarbageCollected<ScriptPromiseResolverBase> {};

template <typename IDLResolvedType>
class ScriptPromiseResolver final : public ScriptPromiseResolverBase {};

}  // namespace blink

#endif  // THIRD_PARTY_BLINK_RENDERER_BINDINGS_CORE_V8_SCRIPT_PROMISE_RESOLVER_H_