Commit 84b82d6c authored by Ted Meyer's avatar Ted Meyer Committed by Commit Bot

Use string.substr instead of string.replace to save a copy

Pass by constref and only copy up to the first 1000 characters, rather
than copying it all and inserting a nullptr and ellipsis

Also removed the old definition of kMaxUrlLength from media_log.h and
the old truncate method definition

Bug: 1090752
Change-Id: I6665f010937c802b94da9fd344ab3baf6af88a99
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2299195
Commit-Queue: Ted Meyer <tmathmeyer@chromium.org>
Reviewed-by: default avatarDale Curtis <dalecurtis@chromium.org>
Cr-Commit-Position: refs/heads/master@{#788846}
parent 20be12b3
......@@ -180,16 +180,6 @@ class MEDIA_EXPORT MediaLog {
// Helper methods to create events and their parameters.
std::unique_ptr<MediaLogRecord> CreateRecord(MediaLogRecord::Type type);
enum : size_t {
// Max length of URLs in Created/Load events. Exceeding triggers truncation.
kMaxUrlLength = 1000,
};
// URLs (for Created and Load events) may be of arbitrary length from the
// untrusted renderer. This method truncates to |kMaxUrlLength| before storing
// the event, and sets the last 3 characters to an ellipsis.
static std::string TruncateUrlString(std::string log_string);
// The underlying media log.
scoped_refptr<ParentLogRecord> parent_log_record_;
......
......@@ -41,11 +41,11 @@ std::string MediaLogEventToString(MediaLogEvent level) {
return "";
}
std::string TruncateUrlString(std::string url) {
constexpr size_t kMaxUrlLength = 1000;
std::string TruncateUrlString(const std::string& url) {
if (url.length() > kMaxUrlLength) {
url.resize(kMaxUrlLength);
url.replace(url.end() - 3, url.end(), "...");
// Take substring and _then_ replace, to avoid copying unused data.
return url.substr(0, kMaxUrlLength)
.replace(kMaxUrlLength - 3, kMaxUrlLength, "...");
}
return url;
}
......
......@@ -12,6 +12,9 @@
namespace media {
// Maximum length of urls that we can show.
constexpr size_t kMaxUrlLength = 1000;
// Events are changes in the state of a player, or a user interaction, or any
// other internal representation of a player at a given point in time.
// This list contains both events that are instant, such as play/pause, as
......@@ -68,7 +71,7 @@ enum class MediaLogEvent {
MEDIA_EXPORT std::string MediaLogEventToString(MediaLogEvent level);
// Sometimes URLs can have encoded data that can be exteremly large.
MEDIA_EXPORT std::string TruncateUrlString(std::string url);
MEDIA_EXPORT std::string TruncateUrlString(const std::string& url);
// These events can be triggered with no extra associated data.
MEDIA_LOG_EVENT_TYPELESS(kPlay);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment