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

content / browser / interest_group / debuggable_auction_worklet.cc [blame]

// Copyright 2021 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/browser/interest_group/debuggable_auction_worklet.h"

#include "base/strings/strcat.h"
#include "base/trace_event/trace_event.h"
#include "base/uuid.h"
#include "content/browser/interest_group/debuggable_auction_worklet_tracker.h"
#include "content/services/auction_worklet/public/mojom/bidder_worklet.mojom.h"
#include "content/services/auction_worklet/public/mojom/seller_worklet.mojom.h"

namespace content {

std::string DebuggableAuctionWorklet::Title() const {
  if (absl::holds_alternative<auction_worklet::mojom::BidderWorklet*>(
          worklet_)) {
    return base::StrCat({"FLEDGE bidder worklet for ", url_.spec()});
  } else {
    return base::StrCat({"FLEDGE seller worklet for ", url_.spec()});
  }
}

DebuggableAuctionWorklet::WorkletType DebuggableAuctionWorklet::Type() const {
  return absl::holds_alternative<auction_worklet::mojom::BidderWorklet*>(
             worklet_)
             ? WorkletType::kBidder
             : WorkletType::kSeller;
}

void DebuggableAuctionWorklet::ConnectDevToolsAgent(
    mojo::PendingAssociatedReceiver<blink::mojom::DevToolsAgent> agent) {
  if (auction_worklet::mojom::BidderWorklet** bidder_worklet =
          absl::get_if<auction_worklet::mojom::BidderWorklet*>(&worklet_)) {
    (*bidder_worklet)->ConnectDevToolsAgent(std::move(agent), thread_index_);
  } else {
    absl::get<auction_worklet::mojom::SellerWorklet*>(worklet_)
        ->ConnectDevToolsAgent(std::move(agent), thread_index_);
  }
}

DebuggableAuctionWorklet::DebuggableAuctionWorklet(
    RenderFrameHostImpl* owning_frame,
    AuctionProcessManager::ProcessHandle& process_handle,
    const GURL& url,
    auction_worklet::mojom::BidderWorklet* bidder_worklet,
    size_t thread_index)
    : owning_frame_(owning_frame),
      process_handle_(process_handle),
      url_(url),
      unique_id_(base::Uuid::GenerateRandomV4().AsLowercaseString()),
      worklet_(bidder_worklet),
      thread_index_(thread_index) {
  DebuggableAuctionWorkletTracker::GetInstance()->NotifyCreated(
      this, should_pause_on_start_);
  RequestPid();
}

DebuggableAuctionWorklet::DebuggableAuctionWorklet(
    RenderFrameHostImpl* owning_frame,
    AuctionProcessManager::ProcessHandle& process_handle,
    const GURL& url,
    auction_worklet::mojom::SellerWorklet* seller_worklet,
    size_t thread_index)
    : owning_frame_(owning_frame),
      process_handle_(process_handle),
      url_(url),
      unique_id_(base::Uuid::GenerateRandomV4().AsLowercaseString()),
      worklet_(seller_worklet),
      thread_index_(thread_index) {
  DebuggableAuctionWorkletTracker::GetInstance()->NotifyCreated(
      this, should_pause_on_start_);
  RequestPid();
}

DebuggableAuctionWorklet::~DebuggableAuctionWorklet() {
  DebuggableAuctionWorkletTracker::GetInstance()->NotifyDestroyed(this);
  if (pid_.has_value()) {
    TRACE_EVENT_INSTANT1(TRACE_DISABLED_BY_DEFAULT("devtools.timeline"),
                         "AuctionWorkletDoneWithProcess",
                         TRACE_EVENT_SCOPE_THREAD, "data",
                         [&](perfetto::TracedValue trace_context) {
                           TraceProcessData(std::move(trace_context));
                         });
  }
}

std::optional<base::ProcessId> DebuggableAuctionWorklet::GetPid(
    PidCallback callback) {
  return process_handle_->GetPid(std::move(callback));
}

void DebuggableAuctionWorklet::RequestPid() {
  std::optional<base::ProcessId> maybe_pid = process_handle_->GetPid(
      base::BindOnce(&DebuggableAuctionWorklet::OnHavePid,
                     weak_ptr_factory_.GetWeakPtr()));
  if (maybe_pid.has_value())
    OnHavePid(maybe_pid.value());
}

void DebuggableAuctionWorklet::OnHavePid(base::ProcessId process_id) {
  pid_ = process_id;
  // TODO(caseq): move all timeline-specific tracing logic to
  // devtools side (i.e. AuctionWorkletDevToolsAgentHost).
  TRACE_EVENT_INSTANT1(TRACE_DISABLED_BY_DEFAULT("devtools.timeline"),
                       "AuctionWorkletRunningInProcess",
                       TRACE_EVENT_SCOPE_THREAD, "data",
                       [&](perfetto::TracedValue trace_context) {
                         TraceProcessData(std::move(trace_context));
                       });
}

void DebuggableAuctionWorklet::TraceProcessData(
    perfetto::TracedValue trace_context) {
  DCHECK(pid_.has_value());
  auto dict = std::move(trace_context).WriteDictionary();
  dict.Add("target", unique_id_);
  dict.Add("pid", pid_.value());
  dict.Add("host", url_.host());
  switch (Type()) {
    case DebuggableAuctionWorklet::WorkletType::kBidder:
      dict.Add("type", "bidder");
      break;
    case DebuggableAuctionWorklet::WorkletType::kSeller:
      dict.Add("type", "seller");
      break;
  }
}

}  // namespace content