chromium/content/services/auction_worklet/webidl_compat.h

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

#ifndef CONTENT_SERVICES_AUCTION_WORKLET_WEBIDL_COMPAT_H_
#define CONTENT_SERVICES_AUCTION_WORKLET_WEBIDL_COMPAT_H_

#include <initializer_list>
#include <optional>
#include <string>
#include <string_view>
#include <vector>

#include "base/functional/callback.h"
#include "base/memory/raw_ptr.h"
#include "base/strings/strcat.h"
#include "content/common/content_export.h"
#include "content/services/auction_worklet/auction_v8_helper.h"
#include "third_party/abseil-cpp/absl/types/variant.h"
#include "v8/include/v8-local-handle.h"
#include "v8/include/v8-value.h"

namespace v8 {
class TryCatch;
class Isolate;
}  // namespace v8

namespace auction_worklet {

// Helps distinguish between double and unrestricted double in IDL.
struct CONTENT_EXPORT UnrestrictedDouble {};

// IdlConvert converts v8::Value values to a C++ representation
// following the semantics of WebIDL for certain simpler, non-structured,
// WebIDL types. The IDL type desired is denoted by the output parameter of the
// particular Convert() overload used.
//
// Clients should have AuctionV8Helper::TimeLimitScope active to protect against
// non-termination.
//
// `error_prefix` is prepended to non-exception error messages,
//  in order to identify where the error occurred, e.g. foo.js:100, etc.
//
// `error_subject` names the thing being converted (e.g. field foo,
//  argument bar, etc.)
class CONTENT_EXPORT IdlConvert {};

// Tries to convert to WebIDL Record<DOMString, USVString>.
// `time_limit_scope` must have a non-null time limit.
CONTENT_EXPORT IdlConvert::Status ConvertRecord(
    AuctionV8Helper* v8_helper,
    AuctionV8Helper::TimeLimitScope& time_limit_scope,
    std::string_view error_prefix,
    std::initializer_list<std::string_view> error_subject,
    v8::Local<v8::Value> value,
    std::vector<std::pair<std::string, std::string>>& out);

// DictConverter helps convert a v8::Value that's supposed to be a WebIDL
// dictionary to C++ values one field at a time, following the appropriate
// semantics. Please see the constructor comment for more details.
//
// After construction, you should call GetOptional/GetRequired/
// GetOptionalSequence as appropriate for all the fields in the dictionary in
// lexicographic order. Unlike gin, this will do type conversions.
//
// All the Get... methods return true on success, false on failure.
// In particular, if GetOptional() is called on a field that's missing,
// the out-param is set to nullopt, and true is returned.
//
// In case of failure all further Get... ops will fail, and the state of
// out-param is unpredictable. You can use ErrorMessage() to get the
// description of the first detected error.
//
// The output type is what determines the conversion --- see the various
// `IdlConvert::Convert` overloads, except sequences are handled specially, via
//  GetOptionalSequence.
class CONTENT_EXPORT DictConverter {};

// ArgsConverter helps convert argument lists, one argument at a time.
class CONTENT_EXPORT ArgsConverter {};

}  // namespace auction_worklet

#endif  // CONTENT_SERVICES_AUCTION_WORKLET_WEBIDL_COMPAT_H_