0

Use string_view whenever possible in WebString

Separating out ptr/data pairs is both less safe and less ergnomic. If we
keep values in paired up view types as much as possible, there's less
risk of lengths and data getting mixed up.

This also avoids needing separate overloads for all variations on
string. string_view (or optional<u16string_view> for FromUTF16) is a
sink type that can accept all variations.

Change-Id: I36c44b3f6052a62369ee351492dac7e542ec06ef
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4892056
Reviewed-by: Philip Rogers <pdr@chromium.org>
Owners-Override: Daniel Cheng <dcheng@chromium.org>
Reviewed-by: Daniel Cheng <dcheng@chromium.org>
Commit-Queue: David Benjamin <davidben@chromium.org>
Code-Coverage: findit-for-me@appspot.gserviceaccount.com <findit-for-me@appspot.gserviceaccount.com>
Cr-Commit-Position: refs/heads/main@{#1206605}
This commit is contained in:
David Benjamin
2023-10-06 20:30:51 +00:00
committed by Chromium LUCI CQ
parent 7076cb3b8a
commit c7d22adc6d
15 changed files with 100 additions and 126 deletions

@@ -354,8 +354,8 @@ void PasswordGenerationAgentTest::LoadHTMLWithUserGesture(const char* html) {
WebElement PasswordGenerationAgentTest::GetElementById( WebElement PasswordGenerationAgentTest::GetElementById(
base::StringPiece element_id) { base::StringPiece element_id) {
WebDocument document = GetMainFrame()->GetDocument(); WebDocument document = GetMainFrame()->GetDocument();
WebElement element = document.GetElementById( WebElement element =
blink::WebString::FromUTF8(element_id.data(), element_id.size())); document.GetElementById(blink::WebString::FromUTF8(element_id));
CHECK(!element.IsNull()); CHECK(!element.IsNull());
return element; return element;
} }
@@ -435,8 +435,8 @@ void PasswordGenerationAgentTest::ExpectAttribute(
const WebElement& element, const WebElement& element,
base::StringPiece attribute, base::StringPiece attribute,
base::StringPiece expected_value) { base::StringPiece expected_value) {
WebString actual_value = element.GetAttribute( WebString actual_value =
blink::WebString::FromUTF8(attribute.data(), attribute.size())); element.GetAttribute(blink::WebString::FromUTF8(attribute));
ASSERT_FALSE(actual_value.IsNull()); ASSERT_FALSE(actual_value.IsNull());
EXPECT_EQ(expected_value, actual_value.Ascii()); EXPECT_EQ(expected_value, actual_value.Ascii());
} }

@@ -48,7 +48,7 @@ constexpr base::StringPiece kAutocomplete = "autocomplete";
template <const base::StringPiece& string> template <const base::StringPiece& string>
const WebString& GetWebString() { const WebString& GetWebString() {
static const base::NoDestructor<WebString> web_string( static const base::NoDestructor<WebString> web_string(
WebString::FromUTF8(string.data(), string.length())); WebString::FromUTF8(string));
return *web_string; return *web_string;
} }
@@ -77,9 +77,7 @@ void MaybeAppendAriaLabelledByDevtoolsIssue(
base::kWhitespaceUTF16, base::KEEP_WHITESPACE, base::kWhitespaceUTF16, base::KEEP_WHITESPACE,
base::SPLIT_WANT_NONEMPTY), base::SPLIT_WANT_NONEMPTY),
[&](const auto& id) { [&](const auto& id) {
return element.GetDocument() return element.GetDocument().GetElementById(WebString(id)).IsNull();
.GetElementById(WebString(id.data(), id.length()))
.IsNull();
})) { })) {
form_issues.emplace_back( form_issues.emplace_back(
GenericIssueErrorType::kFormAriaLabelledByToNonExistingId, GenericIssueErrorType::kFormAriaLabelledByToNonExistingId,

@@ -181,7 +181,7 @@ constexpr base::StringPiece kValue = "value";
template <const base::StringPiece& string> template <const base::StringPiece& string>
const WebString& GetWebString() { const WebString& GetWebString() {
static const base::NoDestructor<WebString> web_string( static const base::NoDestructor<WebString> web_string(
WebString::FromUTF8(string.data(), string.length())); WebString::FromUTF8(string));
return *web_string; return *web_string;
} }
@@ -1720,8 +1720,7 @@ std::vector<WebElement> GetWebElementsFromIdList(const WebDocument& document,
for (const auto& id : base::SplitStringPiece( for (const auto& id : base::SplitStringPiece(
id_list_utf16, base::kWhitespaceUTF16, base::KEEP_WHITESPACE, id_list_utf16, base::kWhitespaceUTF16, base::KEEP_WHITESPACE,
base::SPLIT_WANT_NONEMPTY)) { base::SPLIT_WANT_NONEMPTY)) {
web_elements.push_back( web_elements.push_back(document.GetElementById(WebString(id)));
document.GetElementById(WebString(id.data(), id.length())));
} }
return web_elements; return web_elements;
} }
@@ -2894,9 +2893,7 @@ void MaybeEmitAriaLabelledByDevtoolsIssue(const WebElement& element,
base::KEEP_WHITESPACE, base::KEEP_WHITESPACE,
base::SPLIT_WANT_NONEMPTY), base::SPLIT_WANT_NONEMPTY),
[&](const auto& id) { [&](const auto& id) {
return element.GetDocument() return element.GetDocument().GetElementById(WebString(id)).IsNull();
.GetElementById(WebString(id.data(), id.length()))
.IsNull();
})) { })) {
element.GetDocument().GetFrame()->AddGenericIssue( element.GetDocument().GetFrame()->AddGenericIssue(
blink::mojom::GenericIssueErrorType::kFormAriaLabelledByToNonExistingId, blink::mojom::GenericIssueErrorType::kFormAriaLabelledByToNonExistingId,

@@ -556,8 +556,8 @@ class PrintRenderFrameHelperTestBase : public content::RenderViewTest {
} }
void OnPrintPagesInFrame(base::StringPiece frame_name) { void OnPrintPagesInFrame(base::StringPiece frame_name) {
blink::WebFrame* frame = GetMainFrame()->FindFrameByName( blink::WebFrame* frame =
blink::WebString::FromUTF8(frame_name.data(), frame_name.size())); GetMainFrame()->FindFrameByName(blink::WebString::FromUTF8(frame_name));
ASSERT_TRUE(frame); ASSERT_TRUE(frame);
content::RenderFrame* render_frame = content::RenderFrame* render_frame =
content::RenderFrame::FromWebFrame(frame->ToWebLocalFrame()); content::RenderFrame::FromWebFrame(frame->ToWebLocalFrame());

@@ -103,9 +103,9 @@ bool CanonicalizeCssSelectors(v8::Local<v8::Context> context,
v8::String::Utf8Value selector(isolate, val.As<v8::String>()); v8::String::Utf8Value selector(isolate, val.As<v8::String>());
// Note: See the TODO in css_natives_handler.cc. // Note: See the TODO in css_natives_handler.cc.
std::string parsed = std::string parsed =
blink::CanonicalizeSelector( blink::CanonicalizeSelector(blink::WebString::FromUTF8(std::string_view(
blink::WebString::FromUTF8(*selector, selector.length()), *selector, selector.length())),
blink::kWebSelectorTypeCompound) blink::kWebSelectorTypeCompound)
.Utf8(); .Utf8();
if (parsed.empty()) { if (parsed.empty()) {
*error = *error =

@@ -255,8 +255,7 @@ blink::WebString UserScriptSet::GetJsSource(const UserScript::Content& file,
base::StrCat({kUserScriptHead, script_content, kUserScriptTail}); base::StrCat({kUserScriptHead, script_content, kUserScriptTail});
source = blink::WebString::FromUTF8(content); source = blink::WebString::FromUTF8(content);
} else { } else {
source = blink::WebString::FromUTF8(script_content.data(), source = blink::WebString::FromUTF8(script_content);
script_content.length());
} }
script_sources_[url] = source; script_sources_[url] = source;
return source; return source;
@@ -270,9 +269,7 @@ blink::WebString UserScriptSet::GetCssSource(const UserScript::Content& file) {
base::StringPiece script_content = file.GetContent(); base::StringPiece script_content = file.GetContent();
return script_sources_ return script_sources_
.insert(std::make_pair( .insert(std::make_pair(url, blink::WebString::FromUTF8(script_content)))
url, blink::WebString::FromUTF8(script_content.data(),
script_content.length())))
.first->second; .first->second;
} }

@@ -346,10 +346,8 @@ class PdfViewWebPluginWithoutInitializeTest
static void AddToPluginParams(base::StringPiece name, static void AddToPluginParams(base::StringPiece name,
base::StringPiece value, base::StringPiece value,
blink::WebPluginParams& params) { blink::WebPluginParams& params) {
params.attribute_names.push_back( params.attribute_names.push_back(blink::WebString::FromUTF8(name));
blink::WebString::FromUTF8(name.data(), name.size())); params.attribute_values.push_back(blink::WebString::FromUTF8(value));
params.attribute_values.push_back(
blink::WebString::FromUTF8(value.data(), value.size()));
} }
void SetUpPlugin(base::StringPiece document_url, void SetUpPlugin(base::StringPiece document_url,

@@ -34,6 +34,7 @@
#include <cstring> #include <cstring>
#include <limits> #include <limits>
#include <string> #include <string>
#include <string_view>
#include "base/memory/scoped_refptr.h" #include "base/memory/scoped_refptr.h"
#include "base/strings/latin1_string_conversions.h" #include "base/strings/latin1_string_conversions.h"
@@ -53,12 +54,10 @@ namespace blink {
// Use either one of static methods to convert ASCII, Latin1, UTF-8 or // Use either one of static methods to convert ASCII, Latin1, UTF-8 or
// UTF-16 string into WebString: // UTF-16 string into WebString:
// //
// * WebString::FromASCII(const std::string& ascii) // * WebString::FromASCII(std::string_view ascii)
// * WebString::FromLatin1(const std::string& latin1) // * WebString::FromLatin1(std::string_view latin1)
// * WebString::FromUTF8(const std::string& utf8) // * WebString::FromUTF8(std::string_view utf8)
// * WebString::FromUTF16(const char16_t* utf16) // * WebString::FromUTF16(absl::optional<std::u16string_view> utf16)
// * WebString::FromUTF16(const std::u16string& utf16)
// * WebString::FromUTF16(const absl::optional<std::u16string>& utf16)
// //
// Similarly, use either of following methods to convert WebString to // Similarly, use either of following methods to convert WebString to
// ASCII, Latin1, UTF-8 or UTF-16: // ASCII, Latin1, UTF-8 or UTF-16:
@@ -98,7 +97,7 @@ class BLINK_PLATFORM_EXPORT WebString {
~WebString(); ~WebString();
WebString(); WebString();
WebString(const WebUChar* data, size_t len); explicit WebString(std::u16string_view s);
WebString(const WebString&); WebString(const WebString&);
WebString(WebString&&); WebString(WebString&&);
@@ -109,9 +108,10 @@ class BLINK_PLATFORM_EXPORT WebString {
void Reset(); void Reset();
bool Equals(const WebString&) const; bool Equals(const WebString&) const;
bool Equals(const char* characters, size_t len) const; bool Equals(std::string_view characters) const;
bool Equals(const char* characters) const { bool Equals(const char* characters) const {
return Equals(characters, characters ? std::strlen(characters) : 0); return Equals(
characters ? std::string_view(characters) : std::string_view());
} }
size_t length() const; size_t length() const;
@@ -124,18 +124,13 @@ class BLINK_PLATFORM_EXPORT WebString {
WebString Substring(size_t pos, WebString Substring(size_t pos,
size_t len = std::numeric_limits<size_t>::max()) const; size_t len = std::numeric_limits<size_t>::max()) const;
static WebString FromUTF8(const char* data, size_t length); static WebString FromUTF8(std::string_view s);
static WebString FromUTF8(const std::string& s) {
return FromUTF8(s.data(), s.length());
}
std::u16string Utf16() const { std::u16string Utf16() const {
return base::Latin1OrUTF16ToUTF16(length(), Data8(), Data16()); return base::Latin1OrUTF16ToUTF16(length(), Data8(), Data16());
} }
static WebString FromUTF16(const char16_t*); static WebString FromUTF16(absl::optional<std::u16string_view>);
static WebString FromUTF16(const std::u16string&);
static WebString FromUTF16(const absl::optional<std::u16string>&);
static absl::optional<std::u16string> ToOptionalString16(const WebString& s) { static absl::optional<std::u16string> ToOptionalString16(const WebString& s) {
return s.IsNull() ? absl::nullopt : absl::make_optional(s.Utf16()); return s.IsNull() ? absl::nullopt : absl::make_optional(s.Utf16());
@@ -143,11 +138,7 @@ class BLINK_PLATFORM_EXPORT WebString {
std::string Latin1() const; std::string Latin1() const;
static WebString FromLatin1(const WebLChar* data, size_t length); static WebString FromLatin1(std::string_view s);
static WebString FromLatin1(const std::string& s) {
return FromLatin1(reinterpret_cast<const WebLChar*>(s.data()), s.length());
}
// This asserts if the string contains non-ascii characters. // This asserts if the string contains non-ascii characters.
// Use this rather than calling base::UTF16ToASCII() which always incurs // Use this rather than calling base::UTF16ToASCII() which always incurs
@@ -158,14 +149,15 @@ class BLINK_PLATFORM_EXPORT WebString {
bool ContainsOnlyASCII() const; bool ContainsOnlyASCII() const;
// Does same as FromLatin1 but asserts if the given string has non-ascii char. // Does same as FromLatin1 but asserts if the given string has non-ascii char.
static WebString FromASCII(const std::string&); static WebString FromASCII(std::string_view);
template <int N> template <int N>
WebString(const char (&data)[N]) : WebString(FromUTF8(data, N - 1)) {} WebString(const char (&data)[N])
: WebString(FromUTF8(std::string_view(data, N - 1))) {}
template <int N> template <int N>
WebString& operator=(const char (&data)[N]) { WebString& operator=(const char (&data)[N]) {
*this = FromUTF8(data, N - 1); *this = FromUTF8(std::string_view(data, N - 1));
return *this; return *this;
} }

@@ -196,9 +196,8 @@ TEST(V8ScriptValueSerializerForModulesTest, RoundTripRTCCertificate) {
// Make a certificate with the existing key above. // Make a certificate with the existing key above.
rtc::scoped_refptr<rtc::RTCCertificate> web_certificate = rtc::scoped_refptr<rtc::RTCCertificate> web_certificate =
certificate_generator->FromPEM( certificate_generator->FromPEM(WebString::FromUTF8(kEcdsaPrivateKey),
WebString::FromUTF8(kEcdsaPrivateKey, sizeof(kEcdsaPrivateKey)), WebString::FromUTF8(kEcdsaCertificate));
WebString::FromUTF8(kEcdsaCertificate, sizeof(kEcdsaCertificate)));
ASSERT_TRUE(web_certificate); ASSERT_TRUE(web_certificate);
RTCCertificate* certificate = RTCCertificate* certificate =
MakeGarbageCollected<RTCCertificate>(std::move(web_certificate)); MakeGarbageCollected<RTCCertificate>(std::move(web_certificate));

@@ -1181,8 +1181,8 @@ TEST_F(WebViewTest, FinishComposingTextDoesNotAssert) {
std::string composition_text("hello"); std::string composition_text("hello");
WebVector<ui::ImeTextSpan> empty_ime_text_spans; WebVector<ui::ImeTextSpan> empty_ime_text_spans;
active_input_method_controller->SetComposition( active_input_method_controller->SetComposition(
WebString::FromUTF8(composition_text.c_str()), empty_ime_text_spans, WebString::FromUTF8(composition_text), empty_ime_text_spans, WebRange(),
WebRange(), 5, 5); 5, 5);
// Do arbitrary change to make layout dirty. // Do arbitrary change to make layout dirty.
Document& document = *web_view->MainFrameImpl()->GetFrame()->GetDocument(); Document& document = *web_view->MainFrameImpl()->GetFrame()->GetDocument();
@@ -1239,8 +1239,8 @@ TEST_F(WebViewTest, FinishComposingTextCursorPositionChange) {
->GetActiveWebInputMethodController(); ->GetActiveWebInputMethodController();
WebVector<ui::ImeTextSpan> empty_ime_text_spans; WebVector<ui::ImeTextSpan> empty_ime_text_spans;
active_input_method_controller->SetComposition( active_input_method_controller->SetComposition(
WebString::FromUTF8(composition_text.c_str()), empty_ime_text_spans, WebString::FromUTF8(composition_text), empty_ime_text_spans, WebRange(),
WebRange(), 3, 3); 3, 3);
WebTextInputInfo info = active_input_method_controller->TextInputInfo(); WebTextInputInfo info = active_input_method_controller->TextInputInfo();
EXPECT_EQ("hello", info.value.Utf8()); EXPECT_EQ("hello", info.value.Utf8());
@@ -1258,8 +1258,8 @@ TEST_F(WebViewTest, FinishComposingTextCursorPositionChange) {
EXPECT_EQ(-1, info.composition_end); EXPECT_EQ(-1, info.composition_end);
active_input_method_controller->SetComposition( active_input_method_controller->SetComposition(
WebString::FromUTF8(composition_text.c_str()), empty_ime_text_spans, WebString::FromUTF8(composition_text), empty_ime_text_spans, WebRange(),
WebRange(), 3, 3); 3, 3);
info = active_input_method_controller->TextInputInfo(); info = active_input_method_controller->TextInputInfo();
EXPECT_EQ("helhellolo", info.value.Utf8()); EXPECT_EQ("helhellolo", info.value.Utf8());
EXPECT_EQ(6, info.selection_start); EXPECT_EQ(6, info.selection_start);
@@ -1305,8 +1305,8 @@ TEST_F(WebViewTest, SetCompositionForNewCaretPositions) {
// Caret is on the left of composing text. // Caret is on the left of composing text.
active_input_method_controller->SetComposition( active_input_method_controller->SetComposition(
WebString::FromUTF8(composition_text.c_str()), empty_ime_text_spans, WebString::FromUTF8(composition_text), empty_ime_text_spans, WebRange(),
WebRange(), 0, 0); 0, 0);
info = active_input_method_controller->TextInputInfo(); info = active_input_method_controller->TextInputInfo();
EXPECT_EQ("helloABCworld", info.value.Utf8()); EXPECT_EQ("helloABCworld", info.value.Utf8());
EXPECT_EQ(5, info.selection_start); EXPECT_EQ(5, info.selection_start);
@@ -1316,8 +1316,8 @@ TEST_F(WebViewTest, SetCompositionForNewCaretPositions) {
// Caret is on the right of composing text. // Caret is on the right of composing text.
active_input_method_controller->SetComposition( active_input_method_controller->SetComposition(
WebString::FromUTF8(composition_text.c_str()), empty_ime_text_spans, WebString::FromUTF8(composition_text), empty_ime_text_spans, WebRange(),
WebRange(), 3, 3); 3, 3);
info = active_input_method_controller->TextInputInfo(); info = active_input_method_controller->TextInputInfo();
EXPECT_EQ("helloABCworld", info.value.Utf8()); EXPECT_EQ("helloABCworld", info.value.Utf8());
EXPECT_EQ(8, info.selection_start); EXPECT_EQ(8, info.selection_start);
@@ -1327,8 +1327,8 @@ TEST_F(WebViewTest, SetCompositionForNewCaretPositions) {
// Caret is between composing text and left boundary. // Caret is between composing text and left boundary.
active_input_method_controller->SetComposition( active_input_method_controller->SetComposition(
WebString::FromUTF8(composition_text.c_str()), empty_ime_text_spans, WebString::FromUTF8(composition_text), empty_ime_text_spans, WebRange(),
WebRange(), -2, -2); -2, -2);
info = active_input_method_controller->TextInputInfo(); info = active_input_method_controller->TextInputInfo();
EXPECT_EQ("helloABCworld", info.value.Utf8()); EXPECT_EQ("helloABCworld", info.value.Utf8());
EXPECT_EQ(3, info.selection_start); EXPECT_EQ(3, info.selection_start);
@@ -1338,8 +1338,8 @@ TEST_F(WebViewTest, SetCompositionForNewCaretPositions) {
// Caret is between composing text and right boundary. // Caret is between composing text and right boundary.
active_input_method_controller->SetComposition( active_input_method_controller->SetComposition(
WebString::FromUTF8(composition_text.c_str()), empty_ime_text_spans, WebString::FromUTF8(composition_text), empty_ime_text_spans, WebRange(),
WebRange(), 5, 5); 5, 5);
info = active_input_method_controller->TextInputInfo(); info = active_input_method_controller->TextInputInfo();
EXPECT_EQ("helloABCworld", info.value.Utf8()); EXPECT_EQ("helloABCworld", info.value.Utf8());
EXPECT_EQ(10, info.selection_start); EXPECT_EQ(10, info.selection_start);
@@ -1349,8 +1349,8 @@ TEST_F(WebViewTest, SetCompositionForNewCaretPositions) {
// Caret is on the left boundary. // Caret is on the left boundary.
active_input_method_controller->SetComposition( active_input_method_controller->SetComposition(
WebString::FromUTF8(composition_text.c_str()), empty_ime_text_spans, WebString::FromUTF8(composition_text), empty_ime_text_spans, WebRange(),
WebRange(), -5, -5); -5, -5);
info = active_input_method_controller->TextInputInfo(); info = active_input_method_controller->TextInputInfo();
EXPECT_EQ("helloABCworld", info.value.Utf8()); EXPECT_EQ("helloABCworld", info.value.Utf8());
EXPECT_EQ(0, info.selection_start); EXPECT_EQ(0, info.selection_start);
@@ -1360,8 +1360,8 @@ TEST_F(WebViewTest, SetCompositionForNewCaretPositions) {
// Caret is on the right boundary. // Caret is on the right boundary.
active_input_method_controller->SetComposition( active_input_method_controller->SetComposition(
WebString::FromUTF8(composition_text.c_str()), empty_ime_text_spans, WebString::FromUTF8(composition_text), empty_ime_text_spans, WebRange(),
WebRange(), 8, 8); 8, 8);
info = active_input_method_controller->TextInputInfo(); info = active_input_method_controller->TextInputInfo();
EXPECT_EQ("helloABCworld", info.value.Utf8()); EXPECT_EQ("helloABCworld", info.value.Utf8());
EXPECT_EQ(13, info.selection_start); EXPECT_EQ(13, info.selection_start);
@@ -1371,8 +1371,8 @@ TEST_F(WebViewTest, SetCompositionForNewCaretPositions) {
// Caret exceeds the left boundary. // Caret exceeds the left boundary.
active_input_method_controller->SetComposition( active_input_method_controller->SetComposition(
WebString::FromUTF8(composition_text.c_str()), empty_ime_text_spans, WebString::FromUTF8(composition_text), empty_ime_text_spans, WebRange(),
WebRange(), -100, -100); -100, -100);
info = active_input_method_controller->TextInputInfo(); info = active_input_method_controller->TextInputInfo();
EXPECT_EQ("helloABCworld", info.value.Utf8()); EXPECT_EQ("helloABCworld", info.value.Utf8());
EXPECT_EQ(0, info.selection_start); EXPECT_EQ(0, info.selection_start);
@@ -1382,8 +1382,8 @@ TEST_F(WebViewTest, SetCompositionForNewCaretPositions) {
// Caret exceeds the right boundary. // Caret exceeds the right boundary.
active_input_method_controller->SetComposition( active_input_method_controller->SetComposition(
WebString::FromUTF8(composition_text.c_str()), empty_ime_text_spans, WebString::FromUTF8(composition_text), empty_ime_text_spans, WebRange(),
WebRange(), 100, 100); 100, 100);
info = active_input_method_controller->TextInputInfo(); info = active_input_method_controller->TextInputInfo();
EXPECT_EQ("helloABCworld", info.value.Utf8()); EXPECT_EQ("helloABCworld", info.value.Utf8());
EXPECT_EQ(13, info.selection_start); EXPECT_EQ(13, info.selection_start);
@@ -1641,8 +1641,8 @@ TEST_F(WebViewTest, InsertNewLinePlacementAfterFinishComposingText) {
std::string composition_text("\n"); std::string composition_text("\n");
active_input_method_controller->CommitText( active_input_method_controller->CommitText(
WebString::FromUTF8(composition_text.c_str()), empty_ime_text_spans, WebString::FromUTF8(composition_text), empty_ime_text_spans, WebRange(),
WebRange(), 0); 0);
info = active_input_method_controller->TextInputInfo(); info = active_input_method_controller->TextInputInfo();
EXPECT_EQ(5, info.selection_start); EXPECT_EQ(5, info.selection_start);
EXPECT_EQ(5, info.selection_end); EXPECT_EQ(5, info.selection_end);
@@ -1759,8 +1759,7 @@ TEST_F(WebViewTest, SetCompositionFromExistingTextInTextArea) {
std::string new_line_text("\n"); std::string new_line_text("\n");
WebVector<ui::ImeTextSpan> empty_ime_text_spans; WebVector<ui::ImeTextSpan> empty_ime_text_spans;
active_input_method_controller->CommitText( active_input_method_controller->CommitText(
WebString::FromUTF8(new_line_text.c_str()), empty_ime_text_spans, WebString::FromUTF8(new_line_text), empty_ime_text_spans, WebRange(), 0);
WebRange(), 0);
WebTextInputInfo info = active_input_method_controller->TextInputInfo(); WebTextInputInfo info = active_input_method_controller->TextInputInfo();
EXPECT_EQ("0123456789abcdefghijklmnopq\nrstuvwxyz", info.value.Utf8()); EXPECT_EQ("0123456789abcdefghijklmnopq\nrstuvwxyz", info.value.Utf8());
@@ -1775,8 +1774,8 @@ TEST_F(WebViewTest, SetCompositionFromExistingTextInTextArea) {
std::string composition_text("yolo"); std::string composition_text("yolo");
active_input_method_controller->CommitText( active_input_method_controller->CommitText(
WebString::FromUTF8(composition_text.c_str()), empty_ime_text_spans, WebString::FromUTF8(composition_text), empty_ime_text_spans, WebRange(),
WebRange(), 0); 0);
info = active_input_method_controller->TextInputInfo(); info = active_input_method_controller->TextInputInfo();
EXPECT_EQ("0123456789abcdefghijklmnopq\nrsyoloxyz", info.value.Utf8()); EXPECT_EQ("0123456789abcdefghijklmnopq\nrsyoloxyz", info.value.Utf8());
EXPECT_EQ(34, info.selection_start); EXPECT_EQ(34, info.selection_start);
@@ -1817,11 +1816,11 @@ TEST_F(WebViewTest, SetEditableSelectionOffsetsKeepsComposition) {
->FrameWidget() ->FrameWidget()
->GetActiveWebInputMethodController(); ->GetActiveWebInputMethodController();
active_input_method_controller->CommitText( active_input_method_controller->CommitText(
WebString::FromUTF8(composition_text_first.c_str()), empty_ime_text_spans, WebString::FromUTF8(composition_text_first), empty_ime_text_spans,
WebRange(), 0); WebRange(), 0);
active_input_method_controller->SetComposition( active_input_method_controller->SetComposition(
WebString::FromUTF8(composition_text_second.c_str()), WebString::FromUTF8(composition_text_second), empty_ime_text_spans,
empty_ime_text_spans, WebRange(), 5, 5); WebRange(), 5, 5);
WebTextInputInfo info = active_input_method_controller->TextInputInfo(); WebTextInputInfo info = active_input_method_controller->TextInputInfo();
EXPECT_EQ("hello world", info.value.Utf8()); EXPECT_EQ("hello world", info.value.Utf8());
@@ -3993,8 +3992,8 @@ TEST_F(WebViewTest, FinishComposingTextDoesntTriggerAutofillTextChange) {
WebVector<ui::ImeTextSpan> empty_ime_text_spans; WebVector<ui::ImeTextSpan> empty_ime_text_spans;
active_input_method_controller->SetComposition( active_input_method_controller->SetComposition(
WebString::FromUTF8(composition_text.c_str()), empty_ime_text_spans, WebString::FromUTF8(composition_text), empty_ime_text_spans, WebRange(),
WebRange(), 0, static_cast<int>(composition_text.length())); 0, static_cast<int>(composition_text.length()));
WebTextInputInfo info = active_input_method_controller->TextInputInfo(); WebTextInputInfo info = active_input_method_controller->TextInputInfo();
EXPECT_EQ(0, info.selection_start); EXPECT_EQ(0, info.selection_start);
@@ -4849,8 +4848,8 @@ TEST_F(WebViewTest, CompositionIsUserGesture) {
EXPECT_EQ(0, client.TextChanges()); EXPECT_EQ(0, client.TextChanges());
EXPECT_TRUE( EXPECT_TRUE(
frame->FrameWidget()->GetActiveWebInputMethodController()->SetComposition( frame->FrameWidget()->GetActiveWebInputMethodController()->SetComposition(
WebString::FromUTF8(std::string("hello").c_str()), WebString::FromUTF8("hello"), WebVector<ui::ImeTextSpan>(),
WebVector<ui::ImeTextSpan>(), WebRange(), 3, 3)); WebRange(), 3, 3));
EXPECT_TRUE(frame->HasTransientUserActivation()); EXPECT_TRUE(frame->HasTransientUserActivation());
EXPECT_EQ(1, client.TextChanges()); EXPECT_EQ(1, client.TextChanges());
EXPECT_TRUE(frame->HasMarkedText()); EXPECT_TRUE(frame->HasMarkedText());
@@ -5280,8 +5279,7 @@ TEST_F(WebViewTest, PasswordFieldEditingIsUserGesture) {
EXPECT_EQ(0, client.TextChanges()); EXPECT_EQ(0, client.TextChanges());
EXPECT_TRUE( EXPECT_TRUE(
frame->FrameWidget()->GetActiveWebInputMethodController()->CommitText( frame->FrameWidget()->GetActiveWebInputMethodController()->CommitText(
WebString::FromUTF8(std::string("hello").c_str()), WebString::FromUTF8("hello"), empty_ime_text_spans, WebRange(), 0));
empty_ime_text_spans, WebRange(), 0));
EXPECT_TRUE(frame->HasTransientUserActivation()); EXPECT_TRUE(frame->HasTransientUserActivation());
EXPECT_EQ(1, client.TextChanges()); EXPECT_EQ(1, client.TextChanges());
frame->SetAutofillClient(nullptr); frame->SetAutofillClient(nullptr);

@@ -79,7 +79,7 @@ class LocalFrameClientImplTest : public testing::Test {
WebString UserAgent() { WebString UserAgent() {
// The test always returns the same user agent . // The test always returns the same user agent .
std::string user_agent = GetLocalFrameClient().UserAgent().Utf8(); std::string user_agent = GetLocalFrameClient().UserAgent().Utf8();
return WebString::FromUTF8(user_agent.c_str(), user_agent.length()); return WebString::FromUTF8(user_agent);
} }
WebLocalFrameImpl* MainFrame() { return helper_.LocalMainFrame(); } WebLocalFrameImpl* MainFrame() { return helper_.LocalMainFrame(); }

@@ -2,6 +2,8 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
#include <string_view>
#include "third_party/blink/public/platform/web_icon_sizes_parser.h" #include "third_party/blink/public/platform/web_icon_sizes_parser.h"
#include "third_party/blink/public/platform/web_string.h" #include "third_party/blink/public/platform/web_string.h"
#include "third_party/blink/renderer/platform/testing/blink_fuzzer_test_support.h" #include "third_party/blink/renderer/platform/testing/blink_fuzzer_test_support.h"
@@ -12,8 +14,8 @@ namespace blink {
// Fuzzer for blink::MHTMLParser. // Fuzzer for blink::MHTMLParser.
int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
static BlinkFuzzerTestSupport test_support = BlinkFuzzerTestSupport(); static BlinkFuzzerTestSupport test_support = BlinkFuzzerTestSupport();
WebString string = WebString string = WebString::FromUTF8(
WebString::FromUTF8(reinterpret_cast<const char*>(data), size); std::string_view(reinterpret_cast<const char*>(data), size));
WebIconSizesParser::ParseIconSizes(string); WebIconSizesParser::ParseIconSizes(string);
return 0; return 0;
} }

@@ -55,10 +55,10 @@ WebString::WebString(WebString&&) = default;
WebString& WebString::operator=(const WebString&) = default; WebString& WebString::operator=(const WebString&) = default;
WebString& WebString::operator=(WebString&&) = default; WebString& WebString::operator=(WebString&&) = default;
WebString::WebString(const WebUChar* data, size_t len) WebString::WebString(std::u16string_view s)
: impl_(StringImpl::Create8BitIfPossible( : impl_(StringImpl::Create8BitIfPossible(
data, s.data(),
base::checked_cast<wtf_size_t>(len))) {} base::checked_cast<wtf_size_t>(s.length()))) {}
void WebString::Reset() { void WebString::Reset() {
impl_ = nullptr; impl_ = nullptr;
@@ -89,30 +89,24 @@ WebString WebString::Substring(size_t pos, size_t len) const {
base::checked_cast<wtf_size_t>(len))); base::checked_cast<wtf_size_t>(len)));
} }
WebString WebString::FromUTF8(const char* data, size_t length) { WebString WebString::FromUTF8(std::string_view s) {
return String::FromUTF8(data, length); return String::FromUTF8(s.data(), s.length());
} }
WebString WebString::FromUTF16(const char16_t* s) { WebString WebString::FromUTF16(absl::optional<std::u16string_view> s) {
return WebString(s, std::char_traits<char16_t>::length(s)); if (!s.has_value()) {
}
WebString WebString::FromUTF16(const std::u16string& s) {
return WebString(s.data(), s.length());
}
WebString WebString::FromUTF16(const absl::optional<std::u16string>& s) {
if (!s.has_value())
return WebString(); return WebString();
return WebString(s->data(), s->length()); }
return WebString(*s);
} }
std::string WebString::Latin1() const { std::string WebString::Latin1() const {
return String(impl_).Latin1(); return String(impl_).Latin1();
} }
WebString WebString::FromLatin1(const WebLChar* data, size_t length) { WebString WebString::FromLatin1(std::string_view s) {
return String(data, base::checked_cast<wtf_size_t>(length)); return String(reinterpret_cast<const WebLChar*>(s.data()),
base::checked_cast<wtf_size_t>(s.length()));
} }
std::string WebString::Ascii() const { std::string WebString::Ascii() const {
@@ -134,7 +128,7 @@ bool WebString::ContainsOnlyASCII() const {
return String(impl_).ContainsOnlyASCIIOrEmpty(); return String(impl_).ContainsOnlyASCIIOrEmpty();
} }
WebString WebString::FromASCII(const std::string& s) { WebString WebString::FromASCII(std::string_view s) {
DCHECK(base::IsStringASCII(s)); DCHECK(base::IsStringASCII(s));
return FromLatin1(s); return FromLatin1(s);
} }
@@ -143,8 +137,9 @@ bool WebString::Equals(const WebString& s) const {
return Equal(impl_.get(), s.impl_.get()); return Equal(impl_.get(), s.impl_.get());
} }
bool WebString::Equals(const char* characters, size_t length) const { bool WebString::Equals(std::string_view characters) const {
return Equal(impl_.get(), characters, base::checked_cast<wtf_size_t>(length)); return Equal(impl_.get(), characters.data(),
base::checked_cast<wtf_size_t>(characters.length()));
} }
bool WebString::operator<(const WebString& other) const { bool WebString::operator<(const WebString& other) const {

@@ -12,17 +12,16 @@ namespace blink {
TEST(WebStringTest, UTF8ConversionRoundTrip) { TEST(WebStringTest, UTF8ConversionRoundTrip) {
// Valid characters. // Valid characters.
for (WebUChar uchar = 0; uchar <= 0xD7FF; ++uchar) { for (WebUChar uchar = 0; uchar <= 0xD7FF; ++uchar) {
WebString utf16_string(&uchar, 1); WebString utf16_string(std::u16string_view(&uchar, 1));
std::string utf8_string(utf16_string.Utf8()); std::string utf8_string(utf16_string.Utf8());
WebString utf16_new_string = WebString utf16_new_string = WebString::FromUTF8(utf8_string);
WebString::FromUTF8(utf8_string.data(), utf8_string.length());
EXPECT_FALSE(utf16_new_string.IsNull()); EXPECT_FALSE(utf16_new_string.IsNull());
EXPECT_TRUE(utf16_string == utf16_new_string); EXPECT_TRUE(utf16_string == utf16_new_string);
} }
// Unpaired surrogates. // Unpaired surrogates.
for (WebUChar uchar = 0xD800; uchar <= 0xDFFF; ++uchar) { for (WebUChar uchar = 0xD800; uchar <= 0xDFFF; ++uchar) {
WebString utf16_string(&uchar, 1); WebString utf16_string(std::u16string_view(&uchar, 1));
// Conversion with Strict mode results in an empty string. // Conversion with Strict mode results in an empty string.
std::string utf8_string( std::string utf8_string(
@@ -32,16 +31,14 @@ TEST(WebStringTest, UTF8ConversionRoundTrip) {
// Unpaired surrogates can't be converted back in Lenient mode. // Unpaired surrogates can't be converted back in Lenient mode.
utf8_string = utf16_string.Utf8(WebString::UTF8ConversionMode::kLenient); utf8_string = utf16_string.Utf8(WebString::UTF8ConversionMode::kLenient);
EXPECT_FALSE(utf8_string.empty()); EXPECT_FALSE(utf8_string.empty());
WebString utf16_new_string = WebString utf16_new_string = WebString::FromUTF8(utf8_string);
WebString::FromUTF8(utf8_string.data(), utf8_string.length());
EXPECT_TRUE(utf16_new_string.IsNull()); EXPECT_TRUE(utf16_new_string.IsNull());
// Round-trip works with StrictReplacingErrorsWithFFFD mode. // Round-trip works with StrictReplacingErrorsWithFFFD mode.
utf8_string = utf16_string.Utf8( utf8_string = utf16_string.Utf8(
WebString::UTF8ConversionMode::kStrictReplacingErrorsWithFFFD); WebString::UTF8ConversionMode::kStrictReplacingErrorsWithFFFD);
EXPECT_FALSE(utf8_string.empty()); EXPECT_FALSE(utf8_string.empty());
utf16_new_string = utf16_new_string = WebString::FromUTF8(utf8_string);
WebString::FromUTF8(utf8_string.data(), utf8_string.length());
EXPECT_FALSE(utf16_new_string.IsNull()); EXPECT_FALSE(utf16_new_string.IsNull());
EXPECT_FALSE(utf16_string == utf16_new_string); EXPECT_FALSE(utf16_string == utf16_new_string);
} }

@@ -135,9 +135,10 @@ TEST(WebVectorTest, EmplaceBackArgumentForwarding) {
WebVector<WebString> vector; WebVector<WebString> vector;
vector.reserve(1); vector.reserve(1);
WebUChar buffer[] = {'H', 'e', 'l', 'l', 'o', ' ', 'b', 'l', 'i', 'n', 'k'}; WebUChar buffer[] = {'H', 'e', 'l', 'l', 'o', ' ', 'b', 'l', 'i', 'n', 'k'};
vector.emplace_back(buffer, std::size(buffer)); std::u16string_view view(buffer, std::size(buffer));
vector.emplace_back(view);
ASSERT_EQ(1U, vector.size()); ASSERT_EQ(1U, vector.size());
EXPECT_EQ(WebString(buffer, std::size(buffer)), vector[0]); EXPECT_EQ(WebString(view), vector[0]);
} }
TEST(WebVectorTest, EmplaceBackElementPlacement) { TEST(WebVectorTest, EmplaceBackElementPlacement) {