Skip to content

Commit

Permalink
FLEDGE: Decouple nested blink::InterestGroup::Size.
Browse files Browse the repository at this point in the history
This CL is a first step to add size fields to the return value of
generateBid().

In https://crrev.com/c/4167800, blink::InterestGroup::Size is
introduced for adding sizes to joinAdInterestGroup(). It is declared
as a nested struct inside blink::InterestGroup.

When working on adding size fields to the return value of
generateBid(), we should reuse this Size struct, instead of
creating a new one, for BidderWorkletBid. The way it being declared
as a nested struct have a few drawbacks:
1. This struct seems to serve InterestGroup only at first glance,
   but it will be used to represent general size info for various
   FLEDGE APIs soon: runAdAuction(), scoreAd(), reportResult() and
   reportWin().
2. Clients that only need the size struct's declaration must include
   interest_group.h entirely.
3. Cannot be forward declared.

This CL decouples the nested blink::InterestGroup::Size, to a
standalone struct blink::AdSize.

Some minor notes:
1. Update Size's operator== to compare units as well.
2. Although the struct traits for Size was defined in the related
   CL, the type was not mapped because no mapping was specified
   the BUILD.gn. This CL added the type mapping. This will later
   be used in the follow-up CL on generateBid().
3. Added unit tests for Size.

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

Bug: http://b/239866637

Related CL on adding sizes to joinAdInterestGroup():
https://crrev.com/c/4167800

Change-Id: I4a4dd47607102599bbeb33d05aa1ca5e6928ec5b
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4296777
Commit-Queue: Xiaochen Zhou <xiaochenzh@chromium.org>
Reviewed-by: Dominic Farolino <dom@chromium.org>
Reviewed-by: Russ Hamilton <behamilton@google.com>
Reviewed-by: Garrett Tanzer <gtanzer@chromium.org>
Reviewed-by: Daniel Cheng <dcheng@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1112228}
  • Loading branch information
xiaochen-z authored and Chromium LUCI CQ committed Mar 2, 2023
1 parent 010ea64 commit c8cdce7
Show file tree
Hide file tree
Showing 31 changed files with 455 additions and 327 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1166,9 +1166,8 @@ TEST_F(AdAuctionServiceImplTest, UpdateAllUpdatableFields) {
interest_group.ads->emplace_back(std::move(ad));
interest_group.ad_sizes.emplace();
interest_group.ad_sizes->emplace(
"size_old", blink::InterestGroup::Size(
640, blink::InterestGroup::Size::LengthUnit::kPixels, 480,
blink::InterestGroup::Size::LengthUnit::kPixels));
"size_old", blink::AdSize(640, blink::AdSize::LengthUnit::kPixels, 480,
blink::AdSize::LengthUnit::kPixels));
interest_group.size_groups.emplace();
std::vector<std::string> size_list = {"size_old"};
interest_group.size_groups->emplace("group_old", size_list);
Expand Down Expand Up @@ -1237,9 +1236,8 @@ TEST_F(AdAuctionServiceImplTest, UpdateAllUpdatableFields) {
ASSERT_TRUE(group.ad_sizes.has_value());
ASSERT_EQ(group.ad_sizes->size(), 1u);
EXPECT_EQ(group.ad_sizes->at("size_new"),
blink::InterestGroup::Size(
300, blink::InterestGroup::Size::LengthUnit::kPixels, 150,
blink::InterestGroup::Size::LengthUnit::kPixels));
blink::AdSize(300, blink::AdSize::LengthUnit::kPixels, 150,
blink::AdSize::LengthUnit::kPixels));
ASSERT_TRUE(group.size_groups.has_value());
ASSERT_EQ(group.size_groups->size(), 1u);
EXPECT_EQ(group.size_groups->at("group_new")[0], "size_new");
Expand Down
28 changes: 12 additions & 16 deletions content/browser/interest_group/interest_group_browsertest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -177,19 +177,18 @@ base::Value::Dict SellerCapabilitiesToDict(
}

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

base::Value::Dict InterestGroupSizeToDict(
const blink::InterestGroup::Size& size) {
base::Value::Dict InterestGroupSizeToDict(const blink::AdSize& size) {
base::Value::Dict output;
output.Set("width", base::NumberToString(size.width) +
InterestGroupSizeUnitToString(size.width_units));
Expand All @@ -199,7 +198,7 @@ base::Value::Dict InterestGroupSizeToDict(
}

base::Value::Dict AdSizesToDict(
const base::flat_map<std::string, blink::InterestGroup::Size>& map) {
const base::flat_map<std::string, blink::AdSize>& map) {
base::Value::Dict dict;
for (const auto& [size_name, size] : map) {
dict.Set(size_name, InterestGroupSizeToDict(size));
Expand Down Expand Up @@ -1606,7 +1605,7 @@ interestGroupBuyers: [$1]
RenderFrameHostImpl* render_frame_host) {
while (expected_ad_component_urls.size() <
blink::kMaxAdAuctionAdComponents) {
expected_ad_component_urls.emplace_back(GURL(url::kAboutBlankURL));
expected_ad_component_urls.emplace_back(url::kAboutBlankURL);
}

absl::optional<std::vector<GURL>> all_component_urls =
Expand Down Expand Up @@ -2695,10 +2694,8 @@ IN_PROC_BROWSER_TEST_F(InterestGroupBrowserTest,
/*ads=*/absl::nullopt,
/*ad_components=*/absl::nullopt,
/*ad_sizes=*/
{{{"size_1",
blink::InterestGroup::Size(
150, blink::InterestGroup::Size::LengthUnit::kPixels, 75,
blink::InterestGroup::Size::LengthUnit::kPixels)}}},
{{{"size_1", blink::AdSize(150, blink::AdSize::LengthUnit::kPixels,
75, blink::AdSize::LengthUnit::kPixels)}}},
/*size_groups=*/{{{"group_1", {"size_1"}}}})));

WaitForAccessObserved({});
Expand All @@ -2708,9 +2705,8 @@ IN_PROC_BROWSER_TEST_F(InterestGroupBrowserTest,
const blink::InterestGroup& group = groups[0].interest_group;
EXPECT_EQ(group.ad_sizes->size(), 1u);
ASSERT_EQ(group.ad_sizes->at("size_1"),
blink::InterestGroup::Size(
150, blink::InterestGroup::Size::LengthUnit::kPixels, 75,
blink::InterestGroup::Size::LengthUnit::kPixels));
blink::AdSize(150, blink::AdSize::LengthUnit::kPixels, 75,
blink::AdSize::LengthUnit::kPixels));
EXPECT_EQ(group.size_groups->size(), 1u);
ASSERT_EQ(group.size_groups->at("group_1").size(), 1u);
ASSERT_EQ(group.size_groups->at("group_1").at(0), "size_1");
Expand Down Expand Up @@ -4685,8 +4681,8 @@ IN_PROC_BROWSER_TEST_F(InterestGroupBrowserTest,
disabled_domain.host(),
"/interest_group/bidding_logic_stop_bidding_after_win.js");
disabled_group.ads.emplace();
disabled_group.ads->emplace_back(blink::InterestGroup::Ad(
GURL("https://stop_bidding_after_win.com/render"), absl::nullopt));
disabled_group.ads->emplace_back(
GURL("https://stop_bidding_after_win.com/render"), absl::nullopt);
manager_->JoinInterestGroup(std::move(disabled_group), disabled_domain);
ASSERT_EQ(1, GetJoinCount(disabled_origin, "candy"));

Expand Down
21 changes: 10 additions & 11 deletions content/browser/interest_group/interest_group_storage.cc
Original file line number Diff line number Diff line change
Expand Up @@ -200,8 +200,8 @@ DeserializeInterestGroupAdVector(const std::string& serialized_ads) {
}

std::string Serialize(
const absl::optional<
base::flat_map<std::string, blink::InterestGroup::Size>>& ad_sizes) {
const absl::optional<base::flat_map<std::string, blink::AdSize>>&
ad_sizes) {
if (!ad_sizes) {
return std::string();
}
Expand All @@ -219,13 +219,13 @@ std::string Serialize(
}
return Serialize(base::Value(std::move(dict)));
}
absl::optional<base::flat_map<std::string, blink::InterestGroup::Size>>
absl::optional<base::flat_map<std::string, blink::AdSize>>
DeserializeStringSizeMap(const std::string& serialized_sizes) {
std::unique_ptr<base::Value> dict = DeserializeValue(serialized_sizes);
if (!dict || !dict->is_dict()) {
return absl::nullopt;
}
std::vector<std::pair<std::string, blink::InterestGroup::Size>> result;
std::vector<std::pair<std::string, blink::AdSize>> result;
for (std::pair<const std::string&, base::Value&> entry : dict->GetDict()) {
std::unique_ptr<base::Value> ads_size =
DeserializeValue(entry.second.GetString());
Expand All @@ -239,13 +239,12 @@ DeserializeStringSizeMap(const std::string& serialized_sizes) {
return absl::nullopt;
}
result.emplace_back(entry.first,
blink::InterestGroup::Size(
width_val->GetDouble(),
static_cast<blink::InterestGroup::Size::LengthUnit>(
width_units_val->GetInt()),
height_val->GetDouble(),
static_cast<blink::InterestGroup::Size::LengthUnit>(
height_units_val->GetInt())));
blink::AdSize(width_val->GetDouble(),
static_cast<blink::AdSize::LengthUnit>(
width_units_val->GetInt()),
height_val->GetDouble(),
static_cast<blink::AdSize::LengthUnit>(
height_units_val->GetInt())));
}
return result;
}
Expand Down
18 changes: 7 additions & 11 deletions content/browser/interest_group/interest_group_storage_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -125,13 +125,10 @@ class InterestGroupStorageTest : public testing::Test {
blink::InterestGroup::Ad(
GURL("https://full.example.com/adcomponent2"), "metadata2c")},
/*ad_sizes=*/
{{{"size_1", blink::InterestGroup::Size(
300, blink::InterestGroup::Size::LengthUnit::kPixels,
150, blink::InterestGroup::Size::LengthUnit::kPixels)},
{"size_2",
blink::InterestGroup::Size(
640, blink::InterestGroup::Size::LengthUnit::kPixels, 480,
blink::InterestGroup::Size::LengthUnit::kPixels)}}},
{{{"size_1", blink::AdSize(300, blink::AdSize::LengthUnit::kPixels, 150,
blink::AdSize::LengthUnit::kPixels)},
{"size_2", blink::AdSize(640, blink::AdSize::LengthUnit::kPixels, 480,
blink::AdSize::LengthUnit::kPixels)}}},
/*size_groups=*/
{{{"group_1", std::vector<std::string>{"size_1"}},
{"group_2", std::vector<std::string>{"size_1", "size_2"}}}});
Expand Down Expand Up @@ -167,11 +164,10 @@ class InterestGroupStorageTest : public testing::Test {
update.trusted_bidding_signals_keys =
std::vector<std::string>{"a", "b2", "c", "d"};
update.ads = full.ads;
update.ads->emplace_back(blink::InterestGroup::Ad(
GURL("https://full.example.com/ad3"), "metadata3"));
update.ads->emplace_back(GURL("https://full.example.com/ad3"), "metadata3");
update.ad_components = full.ad_components;
update.ad_components->emplace_back(blink::InterestGroup::Ad(
GURL("https://full.example.com/adcomponent3"), "metadata3c"));
update.ad_components->emplace_back(
GURL("https://full.example.com/adcomponent3"), "metadata3c");
storage->UpdateInterestGroup(blink::InterestGroupKey(full.owner, full.name),
update);

Expand Down
3 changes: 1 addition & 2 deletions content/browser/interest_group/interest_group_update.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,7 @@ struct CONTENT_EXPORT InterestGroupUpdate {
absl::optional<GURL> trusted_bidding_signals_url;
absl::optional<std::vector<std::string>> trusted_bidding_signals_keys;
absl::optional<std::vector<blink::InterestGroup::Ad>> ads, ad_components;
absl::optional<base::flat_map<std::string, blink::InterestGroup::Size>>
ad_sizes;
absl::optional<base::flat_map<std::string, blink::AdSize>> ad_sizes;
absl::optional<base::flat_map<std::string, std::vector<std::string>>>
size_groups;
};
Expand Down
15 changes: 6 additions & 9 deletions content/browser/interest_group/interest_group_update_manager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@
#include "services/network/public/cpp/shared_url_loader_factory.h"
#include "services/network/public/cpp/simple_url_loader.h"
#include "services/network/public/mojom/client_security_state.mojom.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/interest_group_utils.h"
#include "third_party/blink/public/mojom/interest_group/interest_group_types.mojom.h"
#include "url/gurl.h"
#include "url/origin.h"
Expand Down Expand Up @@ -319,7 +319,7 @@ constexpr net::NetworkTrafficAnnotationTag kTrafficAnnotation =
if (!maybe_sizes) {
return true;
}
base::flat_map<std::string, blink::InterestGroup::Size> size_map;
base::flat_map<std::string, blink::AdSize> size_map;
for (std::pair<const std::string&, const base::Value&> pair : *maybe_sizes) {
const base::Value::Dict* maybe_size = pair.second.GetIfDict();
if (!maybe_size) {
Expand All @@ -331,14 +331,11 @@ constexpr net::NetworkTrafficAnnotationTag kTrafficAnnotation =
return false;
}

auto [width_val, width_units] =
blink::ParseInterestGroupSizeString(*width_str);
auto [height_val, height_units] =
blink::ParseInterestGroupSizeString(*height_str);
auto [width_val, width_units] = blink::ParseAdSizeString(*width_str);
auto [height_val, height_units] = blink::ParseAdSizeString(*height_str);

size_map.emplace(pair.first,
blink::InterestGroup::Size(width_val, width_units,
height_val, height_units));
size_map.emplace(pair.first, blink::AdSize(width_val, width_units,
height_val, height_units));
}
interest_group_update.ad_sizes.emplace(size_map);
return true;
Expand Down
8 changes: 6 additions & 2 deletions third_party/blink/common/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -183,11 +183,13 @@ source_set("common") {
"input/web_mouse_wheel_event.cc",
"input/web_pointer_event.cc",
"input/web_touch_event.cc",
"interest_group/ad_display_size.cc",
"interest_group/ad_display_size_mojom_traits.cc",
"interest_group/ad_display_size_utils.cc",
"interest_group/auction_config.cc",
"interest_group/auction_config_mojom_traits.cc",
"interest_group/interest_group.cc",
"interest_group/interest_group_mojom_traits.cc",
"interest_group/interest_group_utils.cc",
"link_to_text/link_to_text_mojom_traits.cc",
"loader/inter_process_time_ticks_converter.cc",
"loader/loader_constants.cc",
Expand Down Expand Up @@ -385,9 +387,11 @@ source_set("common_unittests_sources") {
"indexeddb/indexeddb_key_unittest.cc",
"input/synthetic_web_input_event_builders_unittest.cc",
"input/web_input_event_unittest.cc",
"interest_group/ad_display_size_mojom_traits_test.cc",
"interest_group/ad_display_size_unittest.cc",
"interest_group/ad_display_size_utils_unittest.cc",
"interest_group/auction_config_mojom_traits_test.cc",
"interest_group/interest_group_mojom_traits_test.cc",
"interest_group/interest_group_utils_unittest.cc",
"loader/inter_process_time_ticks_converter_unittest.cc",
"loader/mime_sniffing_throttle_unittest.cc",
"loader/referrer_utils_unittest.cc",
Expand Down
42 changes: 42 additions & 0 deletions third_party/blink/common/interest_group/ad_display_size.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// 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.

#include <tuple>

#include "third_party/blink/public/common/interest_group/ad_display_size.h"

namespace blink {

AdSize::AdSize() = default;

AdSize::AdSize(double width,
LengthUnit width_units,
double height,
LengthUnit height_units)
: width(width),
width_units(width_units),
height(height),
height_units(height_units) {}

AdSize::AdSize(const AdSize&) = default;

AdSize::AdSize(AdSize&&) = default;

AdSize& AdSize::operator=(const AdSize&) = default;

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

bool AdSize::operator==(const AdSize& other) const {
return std::tie(width, width_units, height, height_units) ==
std::tie(other.width, other.width_units, other.height,
other.height_units);
}

bool AdSize::operator!=(const AdSize& other) const {
return !(*this == other);
}

AdSize::~AdSize() = default;

} // namespace blink
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// 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.

#include "third_party/blink/public/common/interest_group/ad_display_size_mojom_traits.h"

namespace mojo {

bool StructTraits<blink::mojom::AdSizeDataView, blink::AdSize>::Read(
blink::mojom::AdSizeDataView data,
blink::AdSize* out) {
if (!data.ReadWidthUnits(&out->width_units) ||
!data.ReadHeightUnits(&out->height_units)) {
return false;
}
out->width = data.width();
out->height = data.height();
return true;
}

} // namespace mojo
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// 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.

#include "third_party/blink/public/common/interest_group/ad_display_size_mojom_traits.h"

#include "mojo/public/cpp/test_support/test_utils.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/public/common/interest_group/ad_display_size.h"
#include "third_party/blink/public/mojom/interest_group/ad_display_size.mojom.h"

namespace blink {

TEST(AdDisplaySizeStructTraitsTest, SerializeAndDeserializeAdSize) {
blink::AdSize ad_size(300, blink::AdSize::LengthUnit::kPixels, 150,
blink::AdSize::LengthUnit::kPixels);

blink::AdSize ad_size_clone;
ASSERT_TRUE(mojo::test::SerializeAndDeserialize<blink::mojom::AdSize>(
ad_size, ad_size_clone));
EXPECT_EQ(ad_size, ad_size_clone);
}

} // namespace blink
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// 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.

#include "third_party/blink/public/common/interest_group/ad_display_size.h"

#include "testing/gtest/include/gtest/gtest.h"
#include "url/gurl.h"

namespace blink {

namespace {

const GURL kUrl1("https://origin1.test/url1");
const GURL kUrl2("https://origin1.test/url2");

} // namespace

TEST(AdSizeTest, OperatorCompare) {
// AdSizes with different units.
AdSize ad_size_in_pixels(100, AdSize::LengthUnit::kPixels, 100,
AdSize::LengthUnit::kPixels);
AdSize ad_size_in_screenwidth(100, AdSize::LengthUnit::kScreenWidth, 100,
AdSize::LengthUnit::kScreenWidth);
AdSize ad_size_in_mix_units(100, AdSize::LengthUnit::kPixels, 100,
AdSize::LengthUnit::kScreenWidth);

EXPECT_FALSE(ad_size_in_pixels == ad_size_in_screenwidth);
EXPECT_TRUE(ad_size_in_pixels != ad_size_in_screenwidth);
EXPECT_FALSE(ad_size_in_pixels == ad_size_in_mix_units);
EXPECT_TRUE(ad_size_in_pixels != ad_size_in_mix_units);
EXPECT_FALSE(ad_size_in_screenwidth == ad_size_in_mix_units);
EXPECT_TRUE(ad_size_in_screenwidth != ad_size_in_mix_units);

// AdSizes with different numeric values.
AdSize ad_size_in_pixels_small(5, AdSize::LengthUnit::kPixels, 5,
AdSize::LengthUnit::kPixels);

EXPECT_FALSE(ad_size_in_pixels == ad_size_in_pixels_small);
EXPECT_TRUE(ad_size_in_pixels != ad_size_in_pixels_small);

// AdSizes with the same numeric values and units.
AdSize ad_size_in_pixels_clone = ad_size_in_pixels;

EXPECT_TRUE(ad_size_in_pixels == ad_size_in_pixels_clone);
EXPECT_FALSE(ad_size_in_pixels != ad_size_in_pixels_clone);
}

} // namespace blink
Loading

0 comments on commit c8cdce7

Please sign in to comment.