Skip to content

Commit

Permalink
Add a use counter for multiple-argument calls to `URLSearchParams.pro…
Browse files Browse the repository at this point in the history
…totype.{has,delete}`

WHATWG is considering adding a two-argument overload to
`URLSearchParams.prototype.has` that would allow checking for the
existence of a key-value entry; and similarly a two-argument overload
to `URLSearchParams.prototype.delete` that would allow deleting a
single entry, rather than all entries with the same name
(whatwg/url#735).

It was, however, brought up that this is likely not web compatible,
since authors might have been using code patterns like
`array.every(usp.has, usp)`, which call the `has` function with
multiple arguments. This change therefore adds a use counter to detect
any such calls that might have different behavior with this change..

Change-Id: I863b7c462b0909c3794ab11389aa2021656a4919
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4269830
Reviewed-by: Hayato Ito <hayato@chromium.org>
Reviewed-by: Adam Rice <ricea@chromium.org>
Commit-Queue: Andreu Botella <abotella@igalia.com>
Cr-Commit-Position: refs/heads/main@{#1108831}
  • Loading branch information
andreubotella authored and Chromium LUCI CQ committed Feb 23, 2023
1 parent acf2fcf commit f1dd561
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 6 deletions.
Expand Up @@ -3816,6 +3816,7 @@ enum WebFeature {
kCreateNSResolverWithNonElements2 = 4475,
kGetDisplayMediaWithPreferCurrentTabTrue = 4476,
kFencedFrameConfigAttribute = 4477,
kURLSearchParams_Has_Delete_MultipleArguments = 4478,

// Add new features immediately above this line. Don't change assigned
// numbers of any item, and don't reuse removed slots.
Expand Down
22 changes: 20 additions & 2 deletions third_party/blink/renderer/core/url/url_search_params.cc
Expand Up @@ -7,7 +7,9 @@
#include <algorithm>
#include <utility>

#include "third_party/blink/public/mojom/use_counter/metrics/web_feature.mojom-shared.h"
#include "third_party/blink/renderer/bindings/core/v8/v8_union_usvstring_usvstringsequencesequence_usvstringusvstringrecord.h"
#include "third_party/blink/renderer/core/execution_context/execution_context.h"
#include "third_party/blink/renderer/core/url/dom_url.h"
#include "third_party/blink/renderer/platform/bindings/exception_messages.h"
#include "third_party/blink/renderer/platform/network/form_data_encoder.h"
Expand Down Expand Up @@ -184,7 +186,7 @@ void URLSearchParams::append(const String& name, const String& value) {
RunUpdateSteps();
}

void URLSearchParams::deleteAllWithName(const String& name) {
void URLSearchParams::deleteAllWithName(ExecutionContext*, const String& name) {
for (wtf_size_t i = 0; i < params_.size();) {
if (params_[i].first == name)
params_.EraseAt(i);
Expand All @@ -194,6 +196,14 @@ void URLSearchParams::deleteAllWithName(const String& name) {
RunUpdateSteps();
}

void URLSearchParams::deleteAllWithName(ExecutionContext* execution_context,
const String& name,
const ScriptValue& ignored) {
UseCounter::Count(execution_context,
WebFeature::kURLSearchParams_Has_Delete_MultipleArguments);
deleteAllWithName(execution_context, name);
}

String URLSearchParams::get(const String& name) const {
for (const auto& param : params_) {
if (param.first == name)
Expand All @@ -211,14 +221,22 @@ Vector<String> URLSearchParams::getAll(const String& name) const {
return result;
}

bool URLSearchParams::has(const String& name) const {
bool URLSearchParams::has(ExecutionContext*, const String& name) const {
for (const auto& param : params_) {
if (param.first == name)
return true;
}
return false;
}

bool URLSearchParams::has(ExecutionContext* execution_context,
const String& name,
const ScriptValue& ignored) const {
UseCounter::Count(execution_context,
WebFeature::kURLSearchParams_Has_Delete_MultipleArguments);
return has(execution_context, name);
}

void URLSearchParams::set(const String& name, const String& value) {
bool found_match = false;
for (wtf_size_t i = 0; i < params_.size();) {
Expand Down
8 changes: 6 additions & 2 deletions third_party/blink/renderer/core/url/url_search_params.h
Expand Up @@ -51,10 +51,14 @@ class CORE_EXPORT URLSearchParams final
// URLSearchParams interface methods
String toString() const;
void append(const String& name, const String& value);
void deleteAllWithName(const String&);
void deleteAllWithName(ExecutionContext*, const String&);
void deleteAllWithName(ExecutionContext*,
const String&,
const ScriptValue& ignored);
String get(const String&) const;
Vector<String> getAll(const String&) const;
bool has(const String&) const;
bool has(ExecutionContext*, const String&) const;
bool has(ExecutionContext*, const String&, const ScriptValue& ignored) const;
void set(const String& name, const String& value);
void sort();
void SetInputWithoutUpdate(const String&);
Expand Down
4 changes: 2 additions & 2 deletions third_party/blink/renderer/core/url/url_search_params.idl
Expand Up @@ -9,10 +9,10 @@
] interface URLSearchParams {
[RaisesException] constructor(optional (sequence<sequence<USVString>> or record<USVString, USVString> or USVString) init = "");
void append(USVString name, USVString value);
[ImplementedAs=deleteAllWithName] void delete(USVString name);
[ImplementedAs=deleteAllWithName, CallWith=ExecutionContext] void delete(USVString name, optional any ignored);
USVString? get(USVString name);
sequence<USVString> getAll(USVString name);
boolean has(USVString name);
[CallWith=ExecutionContext] boolean has(USVString name, optional any ignored);
void set(USVString name, USVString value);

void sort();
Expand Down
1 change: 1 addition & 0 deletions tools/metrics/histograms/enums.xml
Expand Up @@ -42357,6 +42357,7 @@ Called by update_use_counter_feature_enum.py.-->
<int value="4475" label="CreateNSResolverWithNonElements2"/>
<int value="4476" label="GetDisplayMediaWithPreferCurrentTabTrue"/>
<int value="4477" label="FencedFrameConfigAttribute"/>
<int value="4478" label="URLSearchParams_Has_Delete_MultipleArguments"/>
</enum>

<enum name="FeaturePolicyAllowlistType">
Expand Down

0 comments on commit f1dd561

Please sign in to comment.