1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
   51
   52
   53
   54
   55
   56
   57
   58
   59
   60
   61
   62
   63
   64
   65
   66
   67
   68
   69
   70
   71
   72
   73
   74
   75
   76
   77
   78
   79
   80
   81
   82
   83
   84
   85
   86
   87
   88
   89
   90
   91
   92
   93
   94
   95
   96
   97
   98
   99
  100
  101
  102
  103
  104
  105
  106
  107
  108
  109
  110
  111
  112
  113
  114
  115
  116
  117
  118
  119
  120
  121
  122
  123
  124
  125
  126
  127
  128
  129
  130
  131
  132
  133
  134
  135
  136
  137
  138
  139
  140
  141
  142
  143
  144
  145
  146
  147
  148
  149
  150
  151
  152
  153
  154
  155
  156
  157
  158
  159
  160
  161
  162
  163
  164
  165
  166
  167
  168
  169
  170
  171
  172
  173
  174
  175
  176
  177
  178
  179
  180
  181
  182
  183
  184
  185
  186
  187
  188
  189

content / services / auction_worklet / bidder_lazy_filler.h [blame]

// Copyright 2022 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_BIDDER_LAZY_FILLER_H_
#define CONTENT_SERVICES_AUCTION_WORKLET_BIDDER_LAZY_FILLER_H_

#include <string>
#include <string_view>
#include <vector>

#include "base/functional/callback.h"
#include "base/memory/raw_ptr.h"
#include "base/time/time.h"
#include "content/common/content_export.h"
#include "content/services/auction_worklet/auction_v8_helper.h"
#include "content/services/auction_worklet/context_recycler.h"
#include "content/services/auction_worklet/public/mojom/bidder_worklet.mojom-forward.h"
#include "third_party/blink/public/common/interest_group/interest_group.h"
#include "third_party/blink/public/mojom/interest_group/interest_group_types.mojom-forward.h"
#include "v8/include/v8-forward.h"

class GURL;

namespace auction_worklet {

class AuctionV8Logger;

class CONTENT_EXPORT InterestGroupLazyFiller : public PersistedLazyFiller {
 public:
  // `v8_helper` and `v8_logger` must outlive `this`.
  InterestGroupLazyFiller(AuctionV8Helper* v8_helper,
                          AuctionV8Logger* v8_logger);

  // All arguments must remain valid until Reset() is invoked.
  // `bidding_logic_url` and `bidder_worklet_non_shared_params` must not be
  // null.
  //
  // May be invoked multiple times on the same object, but Reset() must be
  // invoked between calls.
  void ReInitialize(const GURL* bidding_logic_url,
                    const GURL* bidding_wasm_helper_url,
                    const GURL* trusted_bidding_signals_url,
                    const mojom::BidderWorkletNonSharedParams*
                        bidder_worklet_non_shared_params);

  // Returns success/failure.
  //
  // `is_ad_excluded` and `is_ad_component_excluded` are used to filter the
  // `ads` and `adComponents` arrays, respectively.
  bool FillInObject(
      v8::Local<v8::Object> object,
      base::RepeatingCallback<bool(const std::string&)> is_ad_excluded,
      base::RepeatingCallback<bool(const std::string&)>
          is_ad_component_excluded,
      base::RepeatingCallback<bool(
          const std::string& ad_render_url,
          base::optional_ref<const std::string> buyer_reporting_id,
          base::optional_ref<const std::string> buyer_and_seller_reporting_id,
          base::optional_ref<const std::string>
              selected_buyer_and_seller_reporting_id)>
          is_reporting_id_set_excluded);

  void Reset() override;

 private:
  // Converts a vector of blink::InterestGroup::Ads into a v8 object, and writes
  // it to `object's` `name` field. Sets `deprecated_render_url_callback` as the
  // lazy callback for the deprecated `renderUrl` field of each entry. Returns
  // false on failure.
  bool CreateAdVector(
      v8::Local<v8::Object>& object,
      std::string_view name,
      base::RepeatingCallback<bool(const std::string&)> is_ad_excluded,
      base::RepeatingCallback<bool(const std::string&,
                                   base::optional_ref<const std::string>,
                                   base::optional_ref<const std::string>,
                                   base::optional_ref<const std::string>)>
          is_reporting_id_set_excluded,
      const std::vector<blink::InterestGroup::Ad>& ads,
      v8::Local<v8::ObjectTemplate>& lazy_filler_template);

  static void HandleUserBiddingSignals(
      v8::Local<v8::Name> name,
      const v8::PropertyCallbackInfo<v8::Value>& info);

  static void HandleBiddingLogicUrl(
      v8::Local<v8::Name> name,
      const v8::PropertyCallbackInfo<v8::Value>& info);
  // Handles "biddingLogicUrl", which is deprecated.
  // TODO(crbug.com/40266734): Remove this method.
  static void HandleDeprecatedBiddingLogicUrl(
      v8::Local<v8::Name> name,
      const v8::PropertyCallbackInfo<v8::Value>& info);

  static void HandleBiddingWasmHelperUrl(
      v8::Local<v8::Name> name,
      const v8::PropertyCallbackInfo<v8::Value>& info);
  // Handles "BiddingWasmHelperUrl", which is deprecated.
  // TODO(crbug.com/40266734): Remove this method.
  static void HandleDeprecatedBiddingWasmHelperUrl(
      v8::Local<v8::Name> name,
      const v8::PropertyCallbackInfo<v8::Value>& info);

  static void HandleUpdateUrl(v8::Local<v8::Name> name,
                              const v8::PropertyCallbackInfo<v8::Value>& info);
  // Handles "updateUrl", which is deprecated.
  // TODO(crbug.com/40266734): Remove this method.
  static void HandleDeprecatedUpdateUrl(
      v8::Local<v8::Name> name,
      const v8::PropertyCallbackInfo<v8::Value>& info);
  // Handles "dailyUpdateUrl", which is deprecated.
  // TODO(crbug.com/40258629): Remove this method.
  static void HandleDeprecatedDailyUpdateUrl(
      v8::Local<v8::Name> name,
      const v8::PropertyCallbackInfo<v8::Value>& info);

  static void HandleTrustedBiddingSignalsUrl(
      v8::Local<v8::Name> name,
      const v8::PropertyCallbackInfo<v8::Value>& info);
  // Handles "trustedBiddingSignalsUrl", which is deprecated.
  // TODO(crbug.com/40266734): Remove this method.
  static void HandleDeprecatedTrustedBiddingSignalsUrl(
      v8::Local<v8::Name> name,
      const v8::PropertyCallbackInfo<v8::Value>& info);

  static void HandleTrustedBiddingSignalsKeys(
      v8::Local<v8::Name> name,
      const v8::PropertyCallbackInfo<v8::Value>& info);
  static void HandlePriorityVector(
      v8::Local<v8::Name> name,
      const v8::PropertyCallbackInfo<v8::Value>& info);
  // TODO(crbug.com/41490104): This field is deprecated in favor of
  // "enableBiddingSignalsPrioritization". Remove this function when it's
  // safe to remove the field.
  static void HandleUseBiddingSignalsPrioritization(
      v8::Local<v8::Name> name,
      const v8::PropertyCallbackInfo<v8::Value>& info);

  // Handles "renderUrl" for the ads and ad components arrays, which is
  // deprecated.
  // TODO(crbug.com/40266734): Remove this method.
  static void HandleDeprecatedAdsRenderUrl(
      v8::Local<v8::Name> name,
      const v8::PropertyCallbackInfo<v8::Value>& info);

  raw_ptr<const GURL> bidding_logic_url_ = nullptr;
  raw_ptr<const GURL> bidding_wasm_helper_url_ = nullptr;
  raw_ptr<const GURL> trusted_bidding_signals_url_ = nullptr;
  raw_ptr<const mojom::BidderWorkletNonSharedParams>
      bidder_worklet_non_shared_params_ = nullptr;
  const raw_ptr<AuctionV8Logger> v8_logger_;
};

// TODO(crbug.com/40270420): Clean up support for deprecated seconds-based
// version after API users migrate.
enum class PrevWinsType { kSeconds, kMilliseconds };

class CONTENT_EXPORT BiddingBrowserSignalsLazyFiller
    : public PersistedLazyFiller {
 public:
  explicit BiddingBrowserSignalsLazyFiller(AuctionV8Helper* v8_helper);

  void ReInitialize(blink::mojom::BiddingBrowserSignals* bidder_browser_signals,
                    base::Time auction_start_time);

  // Returns success/failure.
  bool FillInObject(v8::Local<v8::Object> object);

  void Reset() override;

 private:
  static void HandlePrevWins(v8::Local<v8::Name> name,
                             const v8::PropertyCallbackInfo<v8::Value>& info);
  static void HandlePrevWinsMs(v8::Local<v8::Name> name,
                               const v8::PropertyCallbackInfo<v8::Value>& info);
  static void HandlePrevWinsInternal(
      v8::Local<v8::Name> name,
      const v8::PropertyCallbackInfo<v8::Value>& info,
      PrevWinsType prev_wins_type);

  raw_ptr<blink::mojom::BiddingBrowserSignals> bidder_browser_signals_ =
      nullptr;
  base::Time auction_start_time_;
};

}  // namespace auction_worklet

#endif  // CONTENT_SERVICES_AUCTION_WORKLET_BIDDER_LAZY_FILLER_H_