Skip to content

Commit

Permalink
Make UTF16ToASCII and UTF16TOUTF8 take a StringPiece
Browse files Browse the repository at this point in the history
This is for consistency with the other UTF APIs. This is annoying in some cases because a WebString can be converted implicitly to a string16, but not a StringPiece, necessitating explicit conversions when this is done. And we can't have both versions because they're ambiguous. But this change will allow greater use of StringPieces without copying.

The parameters to the UTF converters that take StringPieces were converted to by-value given the current guidance in StringPiece.

The main non-search-and-replace change is in aw_render_view_ext.cc which saves two string copies.

NOPRESUBMIT=true
(due to legacy wstrings)

Review URL: https://codereview.chromium.org/1259673002

Cr-Commit-Position: refs/heads/master@{#340306}
  • Loading branch information
brettw authored and Commit bot committed Jul 24, 2015
1 parent 1d8996d commit 717861b
Show file tree
Hide file tree
Showing 33 changed files with 200 additions and 137 deletions.
3 changes: 2 additions & 1 deletion android_webview/renderer/aw_content_renderer_client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,8 @@ void AwContentRendererClient::GetNavigationErrorStrings(
base::string16* error_description) {
if (error_html) {
GURL error_url(failed_request.url());
std::string err = base::UTF16ToUTF8(error.localizedDescription);
std::string err =
base::UTF16ToUTF8(base::StringPiece16(error.localizedDescription));
std::string contents;
if (err.empty()) {
contents = AwResource::GetNoDomainPageContent();
Expand Down
5 changes: 2 additions & 3 deletions android_webview/renderer/aw_render_view_ext.cc
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,8 @@ bool RemovePrefixAndAssignIfMatches(const base::StringPiece& prefix,
url::DecodeURLEscapeSequences(spec.data() + prefix.length(),
spec.length() - prefix.length(),
&output);
std::string decoded_url = base::UTF16ToUTF8(
base::string16(output.data(), output.length()));
dest->assign(decoded_url.begin(), decoded_url.end());
*dest = base::UTF16ToUTF8(
base::StringPiece16(output.data(), output.length()));
return true;
}
return false;
Expand Down
22 changes: 14 additions & 8 deletions base/strings/utf_string_conversions.cc
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ bool UTF8ToWide(const char* src, size_t src_len, std::wstring* output) {
}
}

std::wstring UTF8ToWide(const StringPiece& utf8) {
std::wstring UTF8ToWide(StringPiece utf8) {
if (IsStringASCII(utf8)) {
return std::wstring(utf8.begin(), utf8.end());
}
Expand Down Expand Up @@ -153,7 +153,7 @@ bool UTF8ToUTF16(const char* src, size_t src_len, string16* output) {
}
}

string16 UTF8ToUTF16(const StringPiece& utf8) {
string16 UTF8ToUTF16(StringPiece utf8) {
if (IsStringASCII(utf8)) {
return string16(utf8.begin(), utf8.end());
}
Expand All @@ -176,7 +176,7 @@ bool UTF16ToUTF8(const char16* src, size_t src_len, std::string* output) {
}
}

std::string UTF16ToUTF8(const string16& utf16) {
std::string UTF16ToUTF8(StringPiece16 utf16) {
if (IsStringASCII(utf16)) {
return std::string(utf16.begin(), utf16.end());
}
Expand All @@ -195,26 +195,32 @@ bool UTF8ToUTF16(const char* src, size_t src_len, string16* output) {
return UTF8ToWide(src, src_len, output);
}

string16 UTF8ToUTF16(const StringPiece& utf8) {
string16 UTF8ToUTF16(StringPiece utf8) {
return UTF8ToWide(utf8);
}

bool UTF16ToUTF8(const char16* src, size_t src_len, std::string* output) {
return WideToUTF8(src, src_len, output);
}

std::string UTF16ToUTF8(const string16& utf16) {
return WideToUTF8(utf16);
std::string UTF16ToUTF8(StringPiece16 utf16) {
if (IsStringASCII(utf16))
return std::string(utf16.data(), utf16.data() + utf16.length());

std::string ret;
PrepareForUTF8Output(utf16.data(), utf16.length(), &ret);
ConvertUnicode(utf16.data(), utf16.length(), &ret);
return ret;
}

#endif

string16 ASCIIToUTF16(const StringPiece& ascii) {
string16 ASCIIToUTF16(StringPiece ascii) {
DCHECK(IsStringASCII(ascii)) << ascii;
return string16(ascii.begin(), ascii.end());
}

std::string UTF16ToASCII(const string16& utf16) {
std::string UTF16ToASCII(StringPiece16 utf16) {
DCHECK(IsStringASCII(utf16)) << UTF16ToUTF8(utf16);
return std::string(utf16.begin(), utf16.end());
}
Expand Down
10 changes: 5 additions & 5 deletions base/strings/utf_string_conversions.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ BASE_EXPORT bool WideToUTF8(const wchar_t* src, size_t src_len,
BASE_EXPORT std::string WideToUTF8(const std::wstring& wide);
BASE_EXPORT bool UTF8ToWide(const char* src, size_t src_len,
std::wstring* output);
BASE_EXPORT std::wstring UTF8ToWide(const StringPiece& utf8);
BASE_EXPORT std::wstring UTF8ToWide(StringPiece utf8);

BASE_EXPORT bool WideToUTF16(const wchar_t* src, size_t src_len,
string16* output);
Expand All @@ -34,18 +34,18 @@ BASE_EXPORT bool UTF16ToWide(const char16* src, size_t src_len,
BASE_EXPORT std::wstring UTF16ToWide(const string16& utf16);

BASE_EXPORT bool UTF8ToUTF16(const char* src, size_t src_len, string16* output);
BASE_EXPORT string16 UTF8ToUTF16(const StringPiece& utf8);
BASE_EXPORT string16 UTF8ToUTF16(StringPiece utf8);
BASE_EXPORT bool UTF16ToUTF8(const char16* src, size_t src_len,
std::string* output);
BASE_EXPORT std::string UTF16ToUTF8(const string16& utf16);
BASE_EXPORT std::string UTF16ToUTF8(StringPiece16 utf16);

// This converts an ASCII string, typically a hardcoded constant, to a UTF16
// string.
BASE_EXPORT string16 ASCIIToUTF16(const StringPiece& ascii);
BASE_EXPORT string16 ASCIIToUTF16(StringPiece ascii);

// Converts to 7-bit ASCII by truncating. The result must be known to be ASCII
// beforehand.
BASE_EXPORT std::string UTF16ToASCII(const string16& utf16);
BASE_EXPORT std::string UTF16ToASCII(StringPiece16 utf16);

} // namespace base

Expand Down
3 changes: 2 additions & 1 deletion components/autofill/content/renderer/form_cache.cc
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ namespace {

void LogDeprecationMessages(const WebFormControlElement& element) {
std::string autocomplete_attribute =
base::UTF16ToUTF8(element.getAttribute("autocomplete"));
base::UTF16ToUTF8(base::StringPiece16(
element.getAttribute("autocomplete")));

static const char* const deprecated[] = { "region", "locality" };
for (size_t i = 0; i < arraysize(deprecated); ++i) {
Expand Down
5 changes: 3 additions & 2 deletions components/html_viewer/web_mime_registry_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ namespace html_viewer {
namespace {

std::string ToASCIIOrEmpty(const blink::WebString& string) {
return base::IsStringASCII(string) ? base::UTF16ToASCII(string)
: std::string();
return base::IsStringASCII(string)
? base::UTF16ToASCII(base::StringPiece16(string))
: std::string();
}

} // namespace
Expand Down
4 changes: 2 additions & 2 deletions content/child/browser_font_resource_trusted.cc
Original file line number Diff line number Diff line change
Expand Up @@ -272,8 +272,8 @@ PP_Bool BrowserFontResource_Trusted::Describe(
// While converting the other way in PPFontDescToWebFontDesc we validated
// that the enums can be casted.
WebFontDescription web_desc = font_->fontDescription();
description->face =
StringVar::StringToPPVar(base::UTF16ToUTF8(web_desc.family));
description->face = StringVar::StringToPPVar(base::UTF16ToUTF8(
base::StringPiece16(web_desc.family)));
description->family =
static_cast<PP_BrowserFont_Trusted_Family>(web_desc.genericFamily);
description->size = static_cast<uint32_t>(web_desc.size);
Expand Down
4 changes: 2 additions & 2 deletions content/child/notifications/notification_data_conversions.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ PlatformNotificationData ToPlatformNotificationData(
web_data.direction == WebNotificationData::DirectionLeftToRight
? PlatformNotificationData::NotificationDirectionLeftToRight
: PlatformNotificationData::NotificationDirectionRightToLeft;
platform_data.lang = base::UTF16ToUTF8(web_data.lang);
platform_data.lang = base::UTF16ToUTF8(base::StringPiece16(web_data.lang));
platform_data.body = web_data.body;
platform_data.tag = base::UTF16ToUTF8(web_data.tag);
platform_data.tag = base::UTF16ToUTF8(base::StringPiece16(web_data.tag));
platform_data.icon = GURL(web_data.icon.string());
platform_data.vibration_pattern.assign(web_data.vibrate.begin(),
web_data.vibrate.end());
Expand Down
2 changes: 1 addition & 1 deletion content/child/notifications/notification_manager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ void NotificationManager::getNotifications(
request_id,
service_worker_registration_id,
origin,
base::UTF16ToUTF8(filter_tag)));
base::UTF16ToUTF8(base::StringPiece16(filter_tag))));
}

void NotificationManager::close(blink::WebNotificationDelegate* delegate) {
Expand Down
5 changes: 3 additions & 2 deletions content/child/simple_webmimeregistry_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ namespace content {

//static
std::string SimpleWebMimeRegistryImpl::ToASCIIOrEmpty(const WebString& string) {
return base::IsStringASCII(string) ? base::UTF16ToASCII(string)
: std::string();
return base::IsStringASCII(string)
? base::UTF16ToASCII(base::StringPiece16(string))
: std::string();
}

WebMimeRegistry::SupportsType SimpleWebMimeRegistryImpl::supportsMIMEType(
Expand Down
93 changes: 57 additions & 36 deletions content/renderer/accessibility/blink_ax_tree_source.cc
Original file line number Diff line number Diff line change
Expand Up @@ -224,14 +224,17 @@ void BlinkAXTreeSource::SerializeNode(blink::WebAXObject src,
dst->state = AXStateFromBlink(src);
dst->location = src.boundingBoxRect();
dst->id = src.axID();
std::string name = UTF16ToUTF8(src.deprecatedTitle());
std::string name = UTF16ToUTF8(base::StringPiece16(src.deprecatedTitle()));

std::string value;
if (src.valueDescription().length()) {
dst->AddStringAttribute(ui::AX_ATTR_VALUE,
UTF16ToUTF8(src.valueDescription()));
UTF16ToUTF8(base::StringPiece16(
src.valueDescription())));
} else {
dst->AddStringAttribute(ui::AX_ATTR_VALUE, UTF16ToUTF8(src.stringValue()));
dst->AddStringAttribute(
ui::AX_ATTR_VALUE,
UTF16ToUTF8(base::StringPiece16(src.stringValue())));
}

if (dst->role == ui::AX_ROLE_COLOR_WELL)
Expand All @@ -254,8 +257,9 @@ void BlinkAXTreeSource::SerializeNode(blink::WebAXObject src,
AXInvalidStateFromBlink(src.invalidState()));
}
if (src.invalidState() == blink::WebAXInvalidStateOther) {
dst->AddStringAttribute(ui::AX_ATTR_ARIA_INVALID_VALUE,
UTF16ToUTF8(src.ariaInvalidValue()));
dst->AddStringAttribute(
ui::AX_ATTR_ARIA_INVALID_VALUE,
UTF16ToUTF8(base::StringPiece16(src.ariaInvalidValue())));
}

if (src.textDirection()) {
Expand Down Expand Up @@ -295,14 +299,17 @@ void BlinkAXTreeSource::SerializeNode(blink::WebAXObject src,

if (src.accessKey().length()) {
dst->AddStringAttribute(ui::AX_ATTR_ACCESS_KEY,
UTF16ToUTF8(src.accessKey()));
UTF16ToUTF8(base::StringPiece16(src.accessKey())));
}

if (src.actionVerb().length())
dst->AddStringAttribute(ui::AX_ATTR_ACTION, UTF16ToUTF8(src.actionVerb()));
dst->AddStringAttribute(
ui::AX_ATTR_ACTION,
UTF16ToUTF8(base::StringPiece16(src.actionVerb())));
if (src.ariaAutoComplete().length())
dst->AddStringAttribute(ui::AX_ATTR_AUTO_COMPLETE,
UTF16ToUTF8(src.ariaAutoComplete()));
dst->AddStringAttribute(
ui::AX_ATTR_AUTO_COMPLETE,
UTF16ToUTF8(base::StringPiece16(src.ariaAutoComplete())));
if (src.isAriaReadOnly())
dst->AddBoolAttribute(ui::AX_ATTR_ARIA_READONLY, true);
if (src.isButtonStateMixed())
Expand All @@ -312,22 +319,27 @@ void BlinkAXTreeSource::SerializeNode(blink::WebAXObject src,
if (src.deprecatedAccessibilityDescription().length()) {
dst->AddStringAttribute(
ui::AX_ATTR_DESCRIPTION,
UTF16ToUTF8(src.deprecatedAccessibilityDescription()));
UTF16ToUTF8(base::StringPiece16(
src.deprecatedAccessibilityDescription())));
}
if (src.hasComputedStyle()) {
dst->AddStringAttribute(ui::AX_ATTR_DISPLAY,
UTF16ToUTF8(src.computedStyleDisplay()));
dst->AddStringAttribute(
ui::AX_ATTR_DISPLAY,
UTF16ToUTF8(base::StringPiece16(src.computedStyleDisplay())));
}
if (src.deprecatedHelpText().length())
dst->AddStringAttribute(ui::AX_ATTR_HELP,
UTF16ToUTF8(src.deprecatedHelpText()));
dst->AddStringAttribute(
ui::AX_ATTR_HELP,
UTF16ToUTF8(base::StringPiece16((src.deprecatedHelpText()))));
if (src.deprecatedPlaceholder().length()) {
dst->AddStringAttribute(ui::AX_ATTR_PLACEHOLDER,
UTF16ToUTF8(src.deprecatedPlaceholder()));
dst->AddStringAttribute(
ui::AX_ATTR_PLACEHOLDER,
UTF16ToUTF8(base::StringPiece16(src.deprecatedPlaceholder())));
}
if (src.keyboardShortcut().length()) {
dst->AddStringAttribute(ui::AX_ATTR_SHORTCUT,
UTF16ToUTF8(src.keyboardShortcut()));
dst->AddStringAttribute(
ui::AX_ATTR_SHORTCUT,
UTF16ToUTF8(base::StringPiece16(src.keyboardShortcut())));
}
if (!src.deprecatedTitleUIElement().isDetached()) {
dst->AddIntAttribute(ui::AX_ATTR_TITLE_UI_ELEMENT,
Expand Down Expand Up @@ -377,11 +389,13 @@ void BlinkAXTreeSource::SerializeNode(blink::WebAXObject src,
// a WebElement method that returns the original lower cased tagName.
dst->AddStringAttribute(
ui::AX_ATTR_HTML_TAG,
base::StringToLowerASCII(UTF16ToUTF8(element.tagName())));
base::StringToLowerASCII(UTF16ToUTF8(
base::StringPiece16(element.tagName()))));
for (unsigned i = 0; i < element.attributeCount(); ++i) {
std::string name = base::StringToLowerASCII(UTF16ToUTF8(
element.attributeLocalName(i)));
std::string value = UTF16ToUTF8(element.attributeValue(i));
base::StringPiece16(element.attributeLocalName(i))));
std::string value =
UTF16ToUTF8(base::StringPiece16(element.attributeValue(i)));
dst->html_attributes.push_back(std::make_pair(name, value));
}

Expand All @@ -402,8 +416,9 @@ void BlinkAXTreeSource::SerializeNode(blink::WebAXObject src,

// ARIA role.
if (element.hasAttribute("role")) {
dst->AddStringAttribute(ui::AX_ATTR_ROLE,
UTF16ToUTF8(element.getAttribute("role")));
dst->AddStringAttribute(
ui::AX_ATTR_ROLE,
UTF16ToUTF8(base::StringPiece16(element.getAttribute("role"))));
} else {
std::string role = GetEquivalentAriaRoleString(dst->role);
if (!role.empty())
Expand Down Expand Up @@ -444,19 +459,23 @@ void BlinkAXTreeSource::SerializeNode(blink::WebAXObject src,
if (src.liveRegionBusy())
dst->state |= (1 << ui::AX_STATE_BUSY);
if (!src.liveRegionStatus().isEmpty()) {
dst->AddStringAttribute(ui::AX_ATTR_LIVE_STATUS,
UTF16ToUTF8(src.liveRegionStatus()));
dst->AddStringAttribute(
ui::AX_ATTR_LIVE_STATUS,
UTF16ToUTF8(base::StringPiece16(src.liveRegionStatus())));
}
dst->AddStringAttribute(ui::AX_ATTR_LIVE_RELEVANT,
UTF16ToUTF8(src.liveRegionRelevant()));
dst->AddStringAttribute(
ui::AX_ATTR_LIVE_RELEVANT,
UTF16ToUTF8(base::StringPiece16(src.liveRegionRelevant())));
dst->AddBoolAttribute(ui::AX_ATTR_CONTAINER_LIVE_ATOMIC,
src.containerLiveRegionAtomic());
dst->AddBoolAttribute(ui::AX_ATTR_CONTAINER_LIVE_BUSY,
src.containerLiveRegionBusy());
dst->AddStringAttribute(ui::AX_ATTR_CONTAINER_LIVE_STATUS,
UTF16ToUTF8(src.containerLiveRegionStatus()));
dst->AddStringAttribute(ui::AX_ATTR_CONTAINER_LIVE_RELEVANT,
UTF16ToUTF8(src.containerLiveRegionRelevant()));
dst->AddStringAttribute(
ui::AX_ATTR_CONTAINER_LIVE_STATUS,
UTF16ToUTF8(base::StringPiece16(src.containerLiveRegionStatus())));
dst->AddStringAttribute(
ui::AX_ATTR_CONTAINER_LIVE_RELEVANT,
UTF16ToUTF8(base::StringPiece16(src.containerLiveRegionRelevant())));
}

if (dst->role == ui::AX_ROLE_PROGRESS_INDICATOR ||
Expand All @@ -475,9 +494,10 @@ void BlinkAXTreeSource::SerializeNode(blink::WebAXObject src,
dst->AddStringAttribute(ui::AX_ATTR_HTML_TAG, "#document");
const WebDocument& document = src.document();
if (name.empty())
name = UTF16ToUTF8(document.title());
dst->AddStringAttribute(ui::AX_ATTR_DOC_TITLE,
UTF16ToUTF8(document.title()));
name = UTF16ToUTF8(base::StringPiece16(document.title()));
dst->AddStringAttribute(
ui::AX_ATTR_DOC_TITLE,
UTF16ToUTF8(base::StringPiece16(document.title())));
dst->AddStringAttribute(ui::AX_ATTR_DOC_URL, document.url().spec());
dst->AddStringAttribute(
ui::AX_ATTR_DOC_MIMETYPE,
Expand All @@ -488,8 +508,9 @@ void BlinkAXTreeSource::SerializeNode(blink::WebAXObject src,

const WebDocumentType& doctype = document.doctype();
if (!doctype.isNull()) {
dst->AddStringAttribute(ui::AX_ATTR_DOC_DOCTYPE,
UTF16ToUTF8(doctype.name()));
dst->AddStringAttribute(
ui::AX_ATTR_DOC_DOCTYPE,
UTF16ToUTF8(base::StringPiece16(doctype.name())));
}

}
Expand Down
10 changes: 6 additions & 4 deletions content/renderer/cache_storage/cache_storage_dispatcher.cc
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ ServiceWorkerFetchRequest FetchRequestFromWebRequest(
GetServiceWorkerHeaderMapFromWebRequest(web_request, &headers);

return ServiceWorkerFetchRequest(
web_request.url(), base::UTF16ToASCII(web_request.method()), headers,
web_request.url(),
base::UTF16ToASCII(base::StringPiece16(web_request.method())), headers,
Referrer(web_request.referrerUrl(), web_request.referrerPolicy()),
web_request.isReload());
}
Expand Down Expand Up @@ -82,10 +83,11 @@ ServiceWorkerResponse ResponseFromWebResponse(
DCHECK(web_response.streamURL().isEmpty());
return ServiceWorkerResponse(
web_response.url(), web_response.status(),
base::UTF16ToASCII(web_response.statusText()),
base::UTF16ToASCII(base::StringPiece16(web_response.statusText())),
web_response.responseType(), headers,
base::UTF16ToASCII(web_response.blobUUID()), web_response.blobSize(),
web_response.streamURL(), blink::WebServiceWorkerResponseErrorUnknown);
base::UTF16ToASCII(base::StringPiece16(web_response.blobUUID())),
web_response.blobSize(), web_response.streamURL(),
blink::WebServiceWorkerResponseErrorUnknown);
}

CacheStorageCacheQueryParams QueryParamsFromWebQueryParams(
Expand Down
Loading

0 comments on commit 717861b

Please sign in to comment.