chromium/content/services/auction_worklet/trusted_signals_kvv2_helper.cc

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

#include "content/services/auction_worklet/trusted_signals_kvv2_helper.h"

#include <sys/types.h>

#include <algorithm>
#include <cstddef>
#include <cstdint>
#include <memory>
#include <numeric>
#include <optional>
#include <string>
#include <string_view>
#include <vector>

#include "base/check.h"
#include "base/containers/span.h"
#include "base/containers/span_reader.h"
#include "base/containers/span_writer.h"
#include "base/json/json_reader.h"
#include "base/memory/scoped_refptr.h"
#include "base/numerics/safe_conversions.h"
#include "base/strings/strcat.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/stringprintf.h"
#include "base/time/time.h"
#include "base/types/expected.h"
#include "base/types/optional_ref.h"
#include "components/cbor/reader.h"
#include "components/cbor/values.h"
#include "components/cbor/writer.h"
#include "content/common/features.h"
#include "content/services/auction_worklet/auction_v8_helper.h"
#include "content/services/auction_worklet/trusted_signals.h"
#include "content/services/auction_worklet/trusted_signals_request_manager.h"
#include "third_party/zlib/google/compression_utils.h"
#include "url/origin.h"

namespace auction_worklet {

namespace {

// Constants for POST request body.
constexpr std::array<std::string_view, 2> kAcceptCompression =;
constexpr size_t kCompressionFormatSize =;  // bytes
constexpr size_t kCborStringLengthSize =;   // bytes
constexpr size_t kOhttpHeaderSize =;       // bytes
constexpr char kTagInterestGroupName[] =;
constexpr char kTagKey[] =;
constexpr char kTagRenderUrls[] =;
constexpr char kTagAdComponentRenderUrls[] =;

// Add hardcoded `acceptCompression` to request body.
void AddPostRequestConstants(cbor::Value::MapValue& request_map_value) {}

quiche::ObliviousHttpRequest CreateOHttpRequest(
    mojom::TrustedSignalsPublicKeyPtr public_key,
    cbor::Value::MapValue request_map_value) {}

// Creates a single entry for the "arguments" array of a partition, with a
// single tag and a variable number of data values.
cbor::Value MakeArgument(std::string_view tag,
                         const std::set<std::string>& data) {}

// Parse CBOR value to a CompressionGroupResult and a compression group ID.
// Or return ErrorInfo in case of any failure and `compression_group_id_out`
// will not be changed.
base::expected<CompressionGroupResult,
               TrustedSignalsKVv2ResponseParser::ErrorInfo>
ParseCompressionGroup(
    const cbor::Value& group,
    auction_worklet::mojom::TrustedSignalsCompressionScheme compression_scheme,
    int& compression_group_id_out) {}

// Extract compression schema and cbor string from response body base on
// `kCompressionFormatSize` and `kCborStringLengthSize`. Return ErrorInfo
// in case of any failure.
base::expected<
    std::pair<auction_worklet::mojom::TrustedSignalsCompressionScheme,
              std::vector<uint8_t>>,
    TrustedSignalsKVv2ResponseParser::ErrorInfo>
ExtractCompressionSchemaAndCborStringFromResponseBody(
    base::span<const uint8_t> response_body) {}

// Parse a CBOR ArrayValue to a map. `key_group_outputs` should be the value of
// the `keyGroupOutput` field in the partition. Each entry of the array is
// expected to have the following form:
//
// {
//   "tags": [ <tag> ],
//   "keyValues: {<keyValueMap>}
// }
//
// The returned map has keys of <tag> with values of {<keyValueMap>}.
//
// If any value in the array is not in the expected format, including
// cases with multiple tags or keyValueMaps, or if any item is of the
// wrong type, the call fails with an error.
base::expected<std::map<std::string, const cbor::Value::MapValue*>,
               TrustedSignalsKVv2ResponseParser::ErrorInfo>
ParseKeyGroupOutputsToMap(const cbor::Value::ArrayValue& key_group_outputs) {}

// When we have a <tag> - <keyValue map> in `keyGroupOutputs` map, the <keyValue
// map> is like this:
//
// {
//   "keyA" : {"value" : "<JSON valueA>"},
//   "keyB" : {"value" : "<JSON valueB>"}
// }
//
// The input pair is the result of using a key, such as "keyA," to find an
// entry in the above map. This method aims to use the iterator to retrieve the
// found value map and get the JSON-format string, such as ""valueForA"" or
// "["value1ForB","value2ForB"]".
base::expected<std::string_view, TrustedSignalsKVv2ResponseParser::ErrorInfo>
GetKeyValueDataString(
    const std::pair<cbor::Value, cbor::Value>& key_value_pair) {}

// Retrieve the json string corresponding to each `key` from `keys` in
// `key_group_output_map` with specified tag, and serialize it to
// `AuctionV8Helper::SerializedValue` as the value. Insert this into a
// map with the `key` as the key. Return `ErrorInfo` in case of any failure.
base::expected<std::map<std::string, AuctionV8Helper::SerializedValue>,
               TrustedSignalsKVv2ResponseParser::ErrorInfo>
SerializeKeyGroupOutputsMap(
    AuctionV8Helper* v8_helper,
    const std::map<std::string, const cbor::Value::MapValue*>&
        key_group_outputs_map,
    const std::set<std::string>& keys,
    const char* tag) {}

// Extract or decompress the content CBOR value from compression group result.
// Return a CBOR value with array type, or `ErrorInfo` in case of any parsing
// failure.
base::expected<cbor::Value, TrustedSignalsKVv2ResponseParser::ErrorInfo>
GetContentFromCompressionGroup(const CompressionGroupResult& group_result) {}

// Extract the "keyGroupOutputs" field data from a partition CBOR value. Also,
// assign the partition ID and data version to `id_out` and `data_version_out`,
// respectively. Return `ErrorInfo` in case of any parsing failure.
base::expected<const cbor::Value::ArrayValue*,
               TrustedSignalsKVv2ResponseParser::ErrorInfo>
GetKeyGroupOutputsFromPartition(const cbor::Value& partition_value,
                                int& id_out,
                                std::optional<uint32_t>& data_version_out) {}

}  // namespace

TrustedSignalsKVv2RequestHelper::TrustedSignalsKVv2RequestHelper(
    std::string post_request_body,
    quiche::ObliviousHttpRequest::Context context)
    :{}

TrustedSignalsKVv2RequestHelper::TrustedSignalsKVv2RequestHelper(
    TrustedSignalsKVv2RequestHelper&&) = default;

TrustedSignalsKVv2RequestHelper& TrustedSignalsKVv2RequestHelper::operator=(
    TrustedSignalsKVv2RequestHelper&&) = default;

TrustedSignalsKVv2RequestHelper::~TrustedSignalsKVv2RequestHelper() = default;

std::string TrustedSignalsKVv2RequestHelper::TakePostRequestBody() {}

quiche::ObliviousHttpRequest::Context
TrustedSignalsKVv2RequestHelper::TakeOHttpRequestContext() {}

TrustedSignalsKVv2RequestHelperBuilder ::
    ~TrustedSignalsKVv2RequestHelperBuilder() = default;

TrustedSignalsKVv2RequestHelperBuilder::TrustedSignalsKVv2RequestHelperBuilder(
    std::string hostname,
    GURL trusted_signals_url,
    std::optional<int> experiment_group_id)
    :{}

std::unique_ptr<TrustedSignalsKVv2RequestHelper>
TrustedSignalsKVv2RequestHelperBuilder::Build(
    mojom::TrustedSignalsPublicKeyPtr public_key) {}

TrustedSignalsKVv2RequestHelperBuilder::Partition::Partition() = default;

TrustedSignalsKVv2RequestHelperBuilder::Partition::Partition(
    int partition_id,
    const std::string& interest_group_name,
    const std::set<std::string>& bidding_keys,
    const std::string& hostname,
    const std::optional<int>& experiment_group_id,
    std::pair<std::string, std::string> trusted_bidding_signals_slot_size_param)
    :{}

TrustedSignalsKVv2RequestHelperBuilder::Partition::Partition(
    int partition_id,
    const std::string& render_url,
    const std::set<std::string>& ad_component_render_urls,
    const std::string& hostname,
    const std::optional<int>& experiment_group_id)
    :{}

TrustedSignalsKVv2RequestHelperBuilder::Partition::Partition(Partition&&) =
    default;

TrustedSignalsKVv2RequestHelperBuilder::Partition::~Partition() = default;

TrustedSignalsKVv2RequestHelperBuilder::Partition&
TrustedSignalsKVv2RequestHelperBuilder::Partition::operator=(Partition&&) =
    default;

TrustedBiddingSignalsKVv2RequestHelperBuilder::
    TrustedBiddingSignalsKVv2RequestHelperBuilder(
        const std::string& hostname,
        const GURL& trusted_signals_url,
        std::optional<int> experiment_group_id,
        const std::string& trusted_bidding_signals_slot_size_param)
    :{}

TrustedBiddingSignalsKVv2RequestHelperBuilder::
    ~TrustedBiddingSignalsKVv2RequestHelperBuilder() = default;

TrustedSignalsKVv2RequestHelperBuilder::IsolationIndex
TrustedBiddingSignalsKVv2RequestHelperBuilder::AddTrustedSignalsRequest(
    const std::string& interest_group_name,
    const std::set<std::string>& bidding_keys,
    const url::Origin& interest_group_join_origin,
    const blink::mojom::InterestGroup::ExecutionMode execution_mode) {}

cbor::Value::MapValue
TrustedBiddingSignalsKVv2RequestHelperBuilder::BuildMapForPartition(
    const Partition& partition,
    int partition_id,
    int compression_group_id) {}

TrustedScoringSignalsKVv2RequestHelperBuilder::
    TrustedScoringSignalsKVv2RequestHelperBuilder(
        const std::string& hostname,
        const GURL& trusted_signals_url,
        std::optional<int> experiment_group_id)
    :{}

TrustedScoringSignalsKVv2RequestHelperBuilder::
    ~TrustedScoringSignalsKVv2RequestHelperBuilder() = default;

TrustedSignalsKVv2RequestHelperBuilder::IsolationIndex
TrustedScoringSignalsKVv2RequestHelperBuilder::AddTrustedSignalsRequest(
    const GURL& render_url,
    const std::set<std::string>& ad_component_render_urls,
    const url::Origin& owner_origin,
    const url::Origin& interest_group_join_origin) {}

cbor::Value::MapValue
TrustedScoringSignalsKVv2RequestHelperBuilder::BuildMapForPartition(
    const Partition& partition,
    int partition_id,
    int compression_group_id) {}

CompressionGroupResult::CompressionGroupResult() = default;

CompressionGroupResult::CompressionGroupResult(
    auction_worklet::mojom::TrustedSignalsCompressionScheme scheme,
    std::vector<uint8_t> content,
    base::TimeDelta ttl)
    :{}

CompressionGroupResult::CompressionGroupResult(CompressionGroupResult&&) =
    default;

CompressionGroupResult& CompressionGroupResult::operator=(
    CompressionGroupResult&&) = default;

CompressionGroupResult::~CompressionGroupResult() = default;

TrustedSignalsKVv2ResponseParser::SignalsFetchResult
TrustedSignalsKVv2ResponseParser::ParseResponseToSignalsFetchResult(
    const std::string& body_string,
    quiche::ObliviousHttpRequest::Context& context) {}

TrustedSignalsKVv2ResponseParser::TrustedSignalsResultMap
TrustedSignalsKVv2ResponseParser::ParseBiddingSignalsFetchResultToResultMap(
    AuctionV8Helper* v8_helper,
    const std::set<std::string>& interest_group_names,
    const std::set<std::string>& keys,
    const TrustedSignalsKVv2ResponseParser::CompressionGroupResultMap&
        compression_group_result_map) {}

TrustedSignalsKVv2ResponseParser::TrustedSignalsResultMap
TrustedSignalsKVv2ResponseParser::ParseScoringSignalsFetchResultToResultMap(
    AuctionV8Helper* v8_helper,
    const std::set<std::string>& render_urls,
    const std::set<std::string>& ad_component_render_urls,
    const TrustedSignalsKVv2ResponseParser::CompressionGroupResultMap&
        compression_group_result_map) {}

}  // namespace auction_worklet