Skip to content

Commit

Permalink
[shared storage] Add 'SharedStorageModifierMethod' interface to PA wo…
Browse files Browse the repository at this point in the history
…rklet

Introduce `SharedStorageModifierMethod` interface to PA worklet, that
is inherited by individual methods.

This is the proposed hierarchy, as part of the Web Locks integration
proposal (e.g., This allows the IDL parameter to be written as
sequence<SharedStorageModifierMethod>, and also allows the Blink class
to store mojom object in the base class):
- WICG/shared-storage#199
- WICG/shared-storage#205

Bug: 373899210
Change-Id: I2c83574bf9b87ab7c0cb9e6e4c656e742415b49f
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/6072666
Reviewed-by: Maks Orlovich <morlovich@chromium.org>
Reviewed-by: Cammie Smith Barnes <cammie@chromium.org>
Commit-Queue: Cammie Smith Barnes <cammie@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1393929}
  • Loading branch information
yaoxiachromium authored and Chromium LUCI CQ committed Dec 9, 2024
1 parent 6ad9f2c commit 4e9b666
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 14 deletions.
81 changes: 81 additions & 0 deletions content/services/auction_worklet/bidder_worklet_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10863,6 +10863,87 @@ class BidderWorkletSharedStorageAPIEnabledTest : public BidderWorkletTest {
base::test::ScopedFeatureList feature_list_;
};

TEST_F(BidderWorkletSharedStorageAPIEnabledTest, ModifierMethodTypeHierarchy) {
auction_worklet::TestAuctionSharedStorageHost test_shared_storage_host;
mojo::Receiver<auction_worklet::mojom::AuctionSharedStorageHost> receiver(
&test_shared_storage_host);
shared_storage_hosts_[0] = receiver.BindNewPipeAndPassRemote();

RunGenerateBidWithJavascriptExpectingResult(
CreateGenerateBidScript(
R"({ad: "ad", bid:1, render:"https://response.test/" })",
/*extra_code=*/R"(
function getTypeHierarchy(obj) {
let prototype = Object.getPrototypeOf(obj);
const hierarchy = [];

while (prototype) {
hierarchy.push(prototype.constructor.name);
prototype = Object.getPrototypeOf(prototype);
}

return hierarchy.join('->');
}

let set_method_hierarchy =
getTypeHierarchy(new SharedStorageSetMethod('a', 'b'));

if (set_method_hierarchy !==
'SharedStorageSetMethod->SharedStorageModifierMethod->Object') {
throw 'Invalid set_method_hierarchy: ' + set_method_hierarchy;
}

let append_method_hierarchy =
getTypeHierarchy(new SharedStorageAppendMethod('a', 'b'));

if (append_method_hierarchy !==
'SharedStorageAppendMethod->SharedStorageModifierMethod->Object') {
throw 'Invalid append_method_hierarchy: ' + append_method_hierarchy;
}

let delete_method_hierarchy =
getTypeHierarchy(new SharedStorageDeleteMethod('a'));

if (delete_method_hierarchy !==
'SharedStorageDeleteMethod->SharedStorageModifierMethod->Object') {
throw 'Invalid delete_method_hierarchy: ' + delete_method_hierarchy;
}

let clear_method_hierarchy =
getTypeHierarchy(new SharedStorageClearMethod());

if (clear_method_hierarchy !==
'SharedStorageClearMethod->SharedStorageModifierMethod->Object') {
throw 'Invalid clear_method_hierarchy: ' + clear_method_hierarchy;
}
)"),
/*expected_bids=*/
mojom::BidderWorkletBid::New(
auction_worklet::mojom::BidRole::kUnenforcedKAnon, "\"ad\"", 1,
/*bid_currency=*/std::nullopt,
/*ad_cost=*/std::nullopt,
blink::AdDescriptor(GURL("https://response.test/")),
/*selected_buyer_and_seller_reporting_id=*/std::nullopt,
/*ad_component_descriptors=*/std::nullopt,
/*modeling_signals=*/std::nullopt, base::TimeDelta()),
/*expected_data_version=*/std::nullopt,
/*expected_errors=*/{},
/*expected_debug_loss_report_url=*/std::nullopt,
/*expected_debug_win_report_url=*/std::nullopt,
/*expected_set_priority=*/std::nullopt,
/*expected_update_priority_signals_overrides=*/{},
/*expected_pa_requests=*/{});

v8_helpers_[0]->v8_runner()->PostTask(
FROM_HERE, base::BindOnce(
[](scoped_refptr<AuctionV8Helper> v8_helper) {
v8_helper->isolate()->RequestGarbageCollectionForTesting(
v8::Isolate::kFullGarbageCollection);
},
v8_helpers_[0]));
task_environment_.RunUntilIdle();
}

TEST_F(BidderWorkletSharedStorageAPIEnabledTest,
CreateSharedStorageModifierMethod_IDLError) {
std::vector<std::pair<std::string, std::string>> methods_and_errors = {
Expand Down
53 changes: 39 additions & 14 deletions content/services/auction_worklet/shared_storage_bindings.cc
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ constexpr char kNotAConstructorError[] =
"The shared storage method object constructor cannot be called as a "
"function";

constexpr char kSharedStorageSetMethodName[] = "SharedStorageSetMethod";
constexpr char kSharedStorageAppendMethodName[] = "SharedStorageAppendMethod";
constexpr char kSharedStorageDeleteMethodName[] = "SharedStorageDeleteMethod";
constexpr char kSharedStorageClearMethodName[] = "SharedStorageClearMethod";

network::mojom::SharedStorageModifierMethodWithOptionsPtr
CreateMojomSetMethodFromParameters(
AuctionV8Helper* v8_helper,
Expand Down Expand Up @@ -353,16 +358,24 @@ void SharedStorageBindings::AttachToContext(v8::Local<v8::Context> context) {

// These modifier methods are part of the Web Locks integration launch.
if (base::FeatureList::IsEnabled(blink::features::kSharedStorageWebLocks)) {
v8::Local<v8::FunctionTemplate> base_modifier_method_template =
v8::FunctionTemplate::New(v8_helper_->isolate());
base_modifier_method_template->SetClassName(
v8_helper_->CreateStringFromLiteral("SharedStorageModifierMethod"));

v8::Local<v8::FunctionTemplate> set_method_ctor_template =
v8::FunctionTemplate::New(v8_helper_->isolate(),
&SharedStorageBindings::SetMethodConstructor,
v8_this);
set_method_ctor_template->InstanceTemplate()->SetInternalFieldCount(1);
set_method_ctor_template->Inherit(base_modifier_method_template);
set_method_ctor_template->SetClassName(
v8_helper_->CreateStringFromLiteral(kSharedStorageSetMethodName));
v8::Local<v8::Function> set_method_ctor =
set_method_ctor_template->GetFunction(context).ToLocalChecked();
context->Global()
->Set(context,
v8_helper_->CreateStringFromLiteral("SharedStorageSetMethod"),
v8_helper_->CreateStringFromLiteral(kSharedStorageSetMethodName),
set_method_ctor)
.Check();

Expand All @@ -371,38 +384,50 @@ void SharedStorageBindings::AttachToContext(v8::Local<v8::Context> context) {
v8_helper_->isolate(),
&SharedStorageBindings::AppendMethodConstructor, v8_this);
append_method_ctor_template->InstanceTemplate()->SetInternalFieldCount(1);
append_method_ctor_template->Inherit(base_modifier_method_template);
append_method_ctor_template->SetClassName(
v8_helper_->CreateStringFromLiteral(kSharedStorageAppendMethodName));
v8::Local<v8::Function> append_method_ctor =
append_method_ctor_template->GetFunction(context).ToLocalChecked();
context->Global()
->Set(context,
v8_helper_->CreateStringFromLiteral("SharedStorageAppendMethod"),
append_method_ctor)
->Set(
context,
v8_helper_->CreateStringFromLiteral(kSharedStorageAppendMethodName),
append_method_ctor)
.Check();

v8::Local<v8::FunctionTemplate> delete_method_ctor_template =
v8::FunctionTemplate::New(
v8_helper_->isolate(),
&SharedStorageBindings::DeleteMethodConstructor, v8_this);
delete_method_ctor_template->InstanceTemplate()->SetInternalFieldCount(1);
delete_method_ctor_template->Inherit(base_modifier_method_template);
delete_method_ctor_template->SetClassName(
v8_helper_->CreateStringFromLiteral(kSharedStorageDeleteMethodName));
v8::Local<v8::Function> delete_method_ctor =
delete_method_ctor_template->GetFunction(context).ToLocalChecked();
context->Global()
->Set(context,
v8_helper_->CreateStringFromLiteral("SharedStorageDeleteMethod"),
delete_method_ctor)
->Set(
context,
v8_helper_->CreateStringFromLiteral(kSharedStorageDeleteMethodName),
delete_method_ctor)
.Check();

v8::Local<v8::FunctionTemplate> clear_method_ctor_template =
v8::FunctionTemplate::New(
v8_helper_->isolate(),
&SharedStorageBindings::ClearMethodConstructor, v8_this);
clear_method_ctor_template->InstanceTemplate()->SetInternalFieldCount(1);
clear_method_ctor_template->Inherit(base_modifier_method_template);
clear_method_ctor_template->SetClassName(
v8_helper_->CreateStringFromLiteral(kSharedStorageClearMethodName));
v8::Local<v8::Function> clear_method_ctor =
clear_method_ctor_template->GetFunction(context).ToLocalChecked();
context->Global()
->Set(context,
v8_helper_->CreateStringFromLiteral("SharedStorageClearMethod"),
clear_method_ctor)
->Set(
context,
v8_helper_->CreateStringFromLiteral(kSharedStorageClearMethodName),
clear_method_ctor)
.Check();
}
}
Expand Down Expand Up @@ -502,7 +527,7 @@ void SharedStorageBindings::SetMethodConstructor(
network::mojom::SharedStorageModifierMethodWithOptionsPtr mojom_method =
CreateMojomSetMethodFromParameters(
v8_helper, args, bindings->shared_storage_permissions_policy_allowed_,
/*function_name=*/"SharedStorageSetMethod");
/*function_name=*/kSharedStorageSetMethodName);
if (!mojom_method) {
return;
}
Expand All @@ -529,7 +554,7 @@ void SharedStorageBindings::AppendMethodConstructor(
network::mojom::SharedStorageModifierMethodWithOptionsPtr mojom_method =
CreateMojomAppendMethodFromParameters(
v8_helper, args, bindings->shared_storage_permissions_policy_allowed_,
/*function_name=*/"SharedStorageAppendMethod");
/*function_name=*/kSharedStorageAppendMethodName);
if (!mojom_method) {
return;
}
Expand All @@ -556,7 +581,7 @@ void SharedStorageBindings::DeleteMethodConstructor(
network::mojom::SharedStorageModifierMethodWithOptionsPtr mojom_method =
CreateMojomDeleteMethodFromParameters(
v8_helper, args, bindings->shared_storage_permissions_policy_allowed_,
/*function_name=*/"SharedStorageDeleteMethod");
/*function_name=*/kSharedStorageDeleteMethodName);
if (!mojom_method) {
return;
}
Expand All @@ -583,7 +608,7 @@ void SharedStorageBindings::ClearMethodConstructor(
network::mojom::SharedStorageModifierMethodWithOptionsPtr mojom_method =
CreateMojomClearMethodFromParameters(
v8_helper, args, bindings->shared_storage_permissions_policy_allowed_,
/*function_name=*/"SharedStorageClearMethod");
/*function_name=*/kSharedStorageClearMethodName);
if (!mojom_method) {
return;
}
Expand Down

0 comments on commit 4e9b666

Please sign in to comment.