Skip to content

Commit

Permalink
FLEDGE: Size macro substitution of ad auction winning url.
Browse files Browse the repository at this point in the history
This CL implements the macro substitution for sizes. For example,
assume the interest group ad and the bid both specify the ad having
width of 100px and height of 50px:
Before substitution: https://ad&size={%AD_WIDTH%}x{%AD_HEIGHT%}
After substitution:  https://ad&size=100x50

See Turtledove issue: WICG/turtledove#312
See Turtledove PR: WICG/turtledove#417

Change-Id: I92fb2f4f3edd55b4f2374e95ed8bafd5a98fe5a2
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4327282
Commit-Queue: Xiaochen Zhou <xiaochenzh@chromium.org>
Reviewed-by: Garrett Tanzer <gtanzer@chromium.org>
Reviewed-by: Russ Hamilton <behamilton@google.com>
Cr-Commit-Position: refs/heads/main@{#1119375}
  • Loading branch information
xiaochen-z authored and Chromium LUCI CQ committed Mar 20, 2023
1 parent 8f2bfd1 commit 58c86af
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 16 deletions.
45 changes: 43 additions & 2 deletions content/browser/fenced_frame/fenced_frame_url_mapping.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@
#include "base/functional/callback.h"
#include "base/memory/ref_counted.h"
#include "base/strings/strcat.h"
#include "base/strings/string_number_conversions.h"
#include "content/browser/fenced_frame/fenced_frame_reporter.h"
#include "third_party/abseil-cpp/absl/types/optional.h"
#include "third_party/blink/public/common/fenced_frame/fenced_frame_utils.h"
#include "third_party/blink/public/common/interest_group/ad_display_size.h"
#include "ui/display/screen.h"
#include "url/gurl.h"

namespace content {
Expand Down Expand Up @@ -53,6 +55,44 @@ std::string SubstituteMappedStrings(
return base::StrCat(output_vec);
}

double AdSizeToPixels(double size, blink::AdSize::LengthUnit unit) {
switch (unit) {
case blink::AdSize::LengthUnit::kPixels:
return size;
case blink::AdSize::LengthUnit::kScreenWidth: {
double screen_width = display::Screen::GetScreen()
->GetPrimaryDisplay()
.GetSizeInPixel()
.width();
return size / 100.0 * screen_width;
}
case blink::AdSize::LengthUnit::kInvalid:
NOTREACHED_NORETURN();
}
}

// TODO(crbug.com/1420638): Once the representation of size in fenced frame
// config is finalized, change the type of substituted width and height to the
// same.
// Substitute the size macros in ad url with the size from the winning bid.
GURL SubstituteSizeIntoURL(const blink::AdDescriptor& ad_descriptor) {
if (!ad_descriptor.size) {
return ad_descriptor.url;
}

// Convert dimensions to pixels.
int width_in_pixels = static_cast<int>(AdSizeToPixels(
ad_descriptor.size->width, ad_descriptor.size->width_units));
int height_in_pixels = static_cast<int>(AdSizeToPixels(
ad_descriptor.size->height, ad_descriptor.size->height_units));

return GURL(SubstituteMappedStrings(
ad_descriptor.url.spec(),
{std::make_pair("{%AD_WIDTH%}", base::NumberToString(width_in_pixels)),
std::make_pair("{%AD_HEIGHT%}",
base::NumberToString(height_in_pixels))}));
}

} // namespace

FencedFrameURLMapping::FencedFrameURLMapping() = default;
Expand Down Expand Up @@ -160,7 +200,8 @@ FencedFrameURLMapping::AssignFencedFrameURLAndInterestGroupInfo(
// config is finalized, pass the ad size from the winning bid to its fenced
// frame config.
config.urn_uuid_.emplace(urn_uuid);
config.mapped_url_.emplace(ad_descriptor.url, VisibilityToEmbedder::kOpaque,
config.mapped_url_.emplace(SubstituteSizeIntoURL(ad_descriptor),
VisibilityToEmbedder::kOpaque,
VisibilityToContent::kTransparent);
config.deprecated_should_freeze_initial_size_.emplace(
true, VisibilityToEmbedder::kTransparent, VisibilityToContent::kOpaque);
Expand All @@ -177,7 +218,7 @@ FencedFrameURLMapping::AssignFencedFrameURLAndInterestGroupInfo(
// TODO(crbug.com/1420638): Once the representation of size in fenced frame
// config is finalized, pass the ad component size from the winning bid to
// its fenced frame config.
nested_configs.emplace_back(ad_component_descriptor.url);
nested_configs.emplace_back(SubstituteSizeIntoURL(ad_component_descriptor));
}
config.nested_configs_.emplace(std::move(nested_configs),
VisibilityToEmbedder::kOpaque,
Expand Down
73 changes: 59 additions & 14 deletions content/browser/interest_group/interest_group_browsertest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,12 @@
#include "third_party/abseil-cpp/absl/types/optional.h"
#include "third_party/blink/public/common/features.h"
#include "third_party/blink/public/common/interest_group/ad_auction_constants.h"
#include "third_party/blink/public/common/interest_group/ad_display_size_utils.h"
#include "third_party/blink/public/common/interest_group/interest_group.h"
#include "third_party/blink/public/common/interest_group/test_interest_group_builder.h"
#include "third_party/blink/public/mojom/interest_group/ad_auction_service.mojom.h"
#include "third_party/blink/public/mojom/interest_group/interest_group_types.mojom.h"
#include "ui/display/screen.h"
#include "url/gurl.h"
#include "url/origin.h"
#include "url/url_constants.h"
Expand Down Expand Up @@ -178,24 +180,12 @@ base::Value::Dict SellerCapabilitiesToDict(
return dict;
}

std::string InterestGroupSizeUnitToString(
const blink::AdSize::LengthUnit unit) {
if (unit == blink::AdSize::LengthUnit::kPixels) {
return "px";
}
if (unit == blink::AdSize::LengthUnit::kScreenWidth) {
return "sw";
}
// kInvalid and default case
return "";
}

base::Value::Dict InterestGroupSizeToDict(const blink::AdSize& size) {
base::Value::Dict output;
output.Set("width", base::NumberToString(size.width) +
InterestGroupSizeUnitToString(size.width_units));
blink::ConvertAdSizeUnitToString(size.width_units));
output.Set("height", base::NumberToString(size.height) +
InterestGroupSizeUnitToString(size.height_units));
blink::ConvertAdSizeUnitToString(size.height_units));
return output;
}

Expand Down Expand Up @@ -5753,6 +5743,61 @@ IN_PROC_BROWSER_TEST_F(InterestGroupBrowserTest,
RunAuctionAndWaitForURLAndNavigateIframe(auction_config, ad_url);
}

// Runs auction just like test InterestGroupBrowserTest.RunAdAuctionWithWinner,
// but runs with the ads specified with sizes info. The ad url contains size
// macros, which should be substituted with the size from the winning bid.
IN_PROC_BROWSER_TEST_F(InterestGroupBrowserTest,
RunAdAuctionWithSizeWithWinnerMacroSubstitution) {
GURL test_url = https_server_->GetURL("a.test", "/page_with_iframe.html");
ASSERT_TRUE(NavigateToURL(shell(), test_url));
url::Origin test_origin = url::Origin::Create(test_url);
GURL ad_url = https_server_->GetURL(
"c.test", "/echo?render_cars&size={%AD_WIDTH%}x{%AD_HEIGHT%}");

EXPECT_EQ(
kSuccess,
JoinInterestGroupAndVerify(
blink::TestInterestGroupBuilder(
/*owner=*/test_origin,
/*name=*/"cars")
.SetBiddingUrl(https_server_->GetURL(
"a.test", "/interest_group/bidding_logic_with_size.js"))
.SetTrustedBiddingSignalsUrl(https_server_->GetURL(
"a.test", "/interest_group/trusted_bidding_signals.json"))
.SetTrustedBiddingSignalsKeys({{"key1"}})
.SetAds(/*ads=*/{
{{ad_url, /*size_group=*/"group_1",
/*metadata=*/R"({"ad":"metadata","here":[1,2]})"}}})
.SetAdSizes(
{{{"size_1",
blink::AdSize(100, blink::AdSize::LengthUnit::kScreenWidth,
50, blink::AdSize::LengthUnit::kPixels)}}})
.SetSizeGroups({{{"group_1", {"size_1"}}}})
.Build()));

std::string auction_config = JsReplace(
R"({
seller: $1,
decisionLogicUrl: $2,
interestGroupBuyers: [$1],
auctionSignals: {x: 1},
sellerSignals: {yet: 'more', info: 1},
sellerTimeout: 200,
perBuyerSignals: {$1: {even: 'more', x: 4.5}},
perBuyerTimeouts: {$1: 100, '*': 150}
})",
test_origin,
https_server_->GetURL("a.test", "/interest_group/decision_logic.js"));
int screen_width = static_cast<int>(display::Screen::GetScreen()
->GetPrimaryDisplay()
.GetSizeInPixel()
.width());
GURL expected_url = https_server_->GetURL(
"c.test",
base::StringPrintf("/echo?render_cars&size=%ix50", screen_width));
RunAuctionAndWaitForURLAndNavigateIframe(auction_config, expected_url);
}

IN_PROC_BROWSER_TEST_F(InterestGroupBrowserTest,
RunAdAuctionWithWinnerReplacedURN) {
GURL test_url = https_server_->GetURL("a.test", "/page_with_iframe.html");
Expand Down

0 comments on commit 58c86af

Please sign in to comment.