Skip to content

Commit

Permalink
Code health: remove some deprecated {List,Dict}Value API use from dep…
Browse files Browse the repository at this point in the history
…s of services_unittests

This originally targeted just ListValue::AppendString(), but things
that append Strings to lists tend to also append other things to lists,
and also usually insert things into dictionaries, so the neighboring
code to the use of this API was adjusted to non-deprecated alternatives
as well, to avoid revisiting those spots repeatedly.
(This is done pretty locally, though, so some re-visits will likely still
 happen).

Bug: 1187102

Change-Id: I2a1cb54859d2c3dba766c6ca58ef16abb9b7eac2
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2880740
Reviewed-by: Thomas Guilbert <tguilbert@chromium.org>
Reviewed-by: Florent Castelli <orphis@chromium.org>
Reviewed-by: Reilly Grant <reillyg@chromium.org>
Commit-Queue: Maksim Orlovich <morlovich@chromium.org>
Cr-Commit-Position: refs/heads/master@{#881207}
  • Loading branch information
Maks Orlovich authored and Chromium LUCI CQ committed May 10, 2021
1 parent 587ec19 commit 6dc0673
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 70 deletions.
6 changes: 3 additions & 3 deletions components/device_event_log/device_event_log_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,7 @@ std::string DeviceEventLogImpl::GetAsString(StringOrder order,
GetLogTypes(types, &include_types, &exclude_types);

std::string result;
base::ListValue log_entries;
base::Value log_entries(base::Value::Type::LIST);
if (order == OLDEST_FIRST) {
size_t offset = 0;
if (max_events > 0 && max_events < entries_.size()) {
Expand Down Expand Up @@ -428,7 +428,7 @@ std::string DeviceEventLogImpl::GetAsString(StringOrder order,
if (entry.log_level > max_level)
continue;
if (format_json) {
log_entries.AppendString(LogEntryAsJSON(entry));
log_entries.Append(LogEntryAsJSON(entry));
} else {
result += LogEntryToString(entry, show_time, show_file, show_type,
show_level);
Expand All @@ -444,7 +444,7 @@ std::string DeviceEventLogImpl::GetAsString(StringOrder order,
if (entry.log_level > max_level)
continue;
if (format_json) {
log_entries.AppendString(LogEntryAsJSON(entry));
log_entries.Append(LogEntryAsJSON(entry));
} else {
result += LogEntryToString(entry, show_time, show_file, show_type,
show_level);
Expand Down
39 changes: 19 additions & 20 deletions media/cdm/json_web_key.cc
Original file line number Diff line number Diff line change
Expand Up @@ -297,59 +297,60 @@ void CreateLicenseRequest(const KeyIdList& key_ids,
CdmSessionType session_type,
std::vector<uint8_t>* license) {
// Create the license request.
auto request = std::make_unique<base::DictionaryValue>();
auto list = std::make_unique<base::ListValue>();
base::Value request(base::Value::Type::DICTIONARY);
base::Value list(base::Value::Type::LIST);
for (const auto& key_id : key_ids) {
std::string key_id_string;
base::Base64UrlEncode(
base::StringPiece(reinterpret_cast<const char*>(key_id.data()),
key_id.size()),
base::Base64UrlEncodePolicy::OMIT_PADDING, &key_id_string);

list->AppendString(key_id_string);
list.Append(key_id_string);
}
request->Set(kKeyIdsTag, std::move(list));
request.SetKey(kKeyIdsTag, std::move(list));

switch (session_type) {
case CdmSessionType::kTemporary:
request->SetString(kTypeTag, kTemporarySession);
request.SetStringKey(kTypeTag, kTemporarySession);
break;
case CdmSessionType::kPersistentLicense:
request->SetString(kTypeTag, kPersistentLicenseSession);
request.SetStringKey(kTypeTag, kPersistentLicenseSession);
break;
}

// Serialize the license request as a string.
std::string json;
JSONStringValueSerializer serializer(&json);
serializer.Serialize(*request);
serializer.Serialize(request);

// Convert the serialized license request into std::vector and return it.
std::vector<uint8_t> result(json.begin(), json.end());
license->swap(result);
}

void AddKeyIdsToDictionary(const KeyIdList& key_ids,
base::DictionaryValue* dictionary) {
auto list = std::make_unique<base::ListValue>();
base::Value MakeKeyIdsDictionary(const KeyIdList& key_ids) {
base::Value dictionary(base::Value::Type::DICTIONARY);
base::Value list(base::Value::Type::LIST);
for (const auto& key_id : key_ids) {
std::string key_id_string;
base::Base64UrlEncode(
base::StringPiece(reinterpret_cast<const char*>(key_id.data()),
key_id.size()),
base::Base64UrlEncodePolicy::OMIT_PADDING, &key_id_string);

list->AppendString(key_id_string);
list.Append(key_id_string);
}
dictionary->Set(kKeyIdsTag, std::move(list));
dictionary.SetKey(kKeyIdsTag, std::move(list));
return dictionary;
}

std::vector<uint8_t> SerializeDictionaryToVector(
const base::DictionaryValue* dictionary) {
const base::Value& dictionary) {
// Serialize the dictionary as a string.
std::string json;
JSONStringValueSerializer serializer(&json);
serializer.Serialize(*dictionary);
serializer.Serialize(dictionary);

// Convert the serialized data into std::vector and return it.
return std::vector<uint8_t>(json.begin(), json.end());
Expand All @@ -358,10 +359,9 @@ std::vector<uint8_t> SerializeDictionaryToVector(
void CreateKeyIdsInitData(const KeyIdList& key_ids,
std::vector<uint8_t>* init_data) {
// Create the init_data.
auto dictionary = std::make_unique<base::DictionaryValue>();
AddKeyIdsToDictionary(key_ids, dictionary.get());
auto dictionary = MakeKeyIdsDictionary(key_ids);

auto data = SerializeDictionaryToVector(dictionary.get());
auto data = SerializeDictionaryToVector(dictionary);
init_data->swap(data);
}

Expand All @@ -372,9 +372,8 @@ void CreateKeyIdsInitData(const KeyIdList& key_ids,
// of the octet sequence containing the key ID value.
std::vector<uint8_t> CreateLicenseReleaseMessage(const KeyIdList& key_ids) {
// Create the init_data.
auto dictionary = std::make_unique<base::DictionaryValue>();
AddKeyIdsToDictionary(key_ids, dictionary.get());
return SerializeDictionaryToVector(dictionary.get());
auto dictionary = MakeKeyIdsDictionary(key_ids);
return SerializeDictionaryToVector(dictionary);
}

bool ExtractFirstKeyIdFromLicenseRequest(const std::vector<uint8_t>& license,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include <vector>

#include "base/containers/contains.h"
#include "base/optional.h"
#include "base/types/pass_key.h"
#include "base/values.h"
#include "third_party/blink/public/common/browser_interface_broker_proxy.h"
Expand Down Expand Up @@ -456,66 +457,64 @@ const char* GetTransceiverUpdatedReasonString(
return nullptr;
}

// Builds a DictionaryValue from the StatsReport.
// Builds a dictionary Value from the StatsReport.
// Note:
// The format must be consistent with what webrtc_internals.js expects.
// If you change it here, you must change webrtc_internals.js as well.
std::unique_ptr<base::DictionaryValue> GetDictValueStats(
const StatsReport& report) {
base::Optional<base::Value> GetDictValueStats(const StatsReport& report) {
if (report.values().empty())
return nullptr;
return base::nullopt;

auto values = std::make_unique<base::ListValue>();
base::Value values(base::Value::Type::LIST);

for (const auto& v : report.values()) {
const StatsReport::ValuePtr& value = v.second;
values->AppendString(value->display_name());
values.Append(value->display_name());
switch (value->type()) {
case StatsReport::Value::kInt:
values->AppendInteger(value->int_val());
values.Append(value->int_val());
break;
case StatsReport::Value::kFloat:
values->AppendDouble(value->float_val());
values.Append(value->float_val());
break;
case StatsReport::Value::kString:
values->AppendString(value->string_val());
values.Append(value->string_val());
break;
case StatsReport::Value::kStaticString:
values->AppendString(value->static_string_val());
values.Append(value->static_string_val());
break;
case StatsReport::Value::kBool:
values->AppendBoolean(value->bool_val());
values.Append(value->bool_val());
break;
case StatsReport::Value::kInt64: // int64_t isn't supported, so use
// string.
case StatsReport::Value::kId:
default:
values->AppendString(value->ToString());
values.Append(value->ToString());
break;
}
}

auto dict = std::make_unique<base::DictionaryValue>();
dict->SetDouble("timestamp", report.timestamp());
dict->Set("values", std::move(values));
base::Value dict(base::Value::Type::DICTIONARY);
dict.SetDoubleKey("timestamp", report.timestamp());
dict.SetKey("values", std::move(values));

return dict;
}

// Builds a DictionaryValue from the StatsReport.
// The caller takes the ownership of the returned value.
std::unique_ptr<base::DictionaryValue> GetDictValue(const StatsReport& report) {
std::unique_ptr<base::DictionaryValue> stats = GetDictValueStats(report);
// Builds a dictionary Value from the StatsReport.
base::Optional<base::Value> GetDictValue(const StatsReport& report) {
base::Optional<base::Value> stats = GetDictValueStats(report);
if (!stats)
return nullptr;
return base::nullopt;

// Note:
// The format must be consistent with what webrtc_internals.js expects.
// If you change it here, you must change webrtc_internals.js as well.
auto result = std::make_unique<base::DictionaryValue>();
result->Set("stats", std::move(stats));
result->SetString("id", report.id()->ToString());
result->SetString("type", report.TypeToString());
base::Value result(base::Value::Type::DICTIONARY);
result.SetKey("stats", std::move(stats).value());
result.SetStringKey("id", report.id()->ToString());
result.SetStringKey("type", report.TypeToString());

return result;
}
Expand Down Expand Up @@ -545,9 +544,9 @@ class InternalLegacyStatsObserver : public webrtc::StatsObserver {
void OnComplete(const StatsReports& reports) override {
auto list = std::make_unique<base::ListValue>();
for (const auto* r : reports) {
std::unique_ptr<base::DictionaryValue> report = GetDictValue(*r);
base::Optional<base::Value> report = GetDictValue(*r);
if (report)
list->Append(std::move(report));
list->Append(std::move(report).value());
}

if (!list->empty()) {
Expand Down Expand Up @@ -625,51 +624,47 @@ class InternalStandardStatsObserver : public webrtc::RTCStatsCollectorCallback {
for (const auto& stats : *report) {
// The format of "stats_subdictionary" is:
// {timestamp:<milliseconds>, values: [<key-value pairs>]}
auto stats_subdictionary = std::make_unique<base::DictionaryValue>();
base::Value stats_subdictionary(base::Value::Type::DICTIONARY);
// Timestamp is reported in milliseconds.
stats_subdictionary->SetDouble("timestamp",
stats.timestamp_us() / 1000.0);
stats_subdictionary.SetDoubleKey("timestamp",
stats.timestamp_us() / 1000.0);
// Values are reported as
// "values": ["member1", value, "member2", value...]
auto name_value_pairs = std::make_unique<base::ListValue>();
base::Value name_value_pairs(base::Value::Type::LIST);
for (const auto* member : stats.Members()) {
if (!member->is_defined())
continue;
// Non-standardized / provisional stats which are not exposed
// to Javascript are postfixed with an asterisk.
std::string postfix = member->is_standardized() ? "" : "*";
name_value_pairs->AppendString(member->name() + postfix);
name_value_pairs->Append(MemberToValue(*member));
name_value_pairs.Append(member->name() + postfix);
name_value_pairs.Append(MemberToValue(*member));
}
stats_subdictionary->Set("values", std::move(name_value_pairs));
stats_subdictionary.SetKey("values", std::move(name_value_pairs));

// The format of "stats_dictionary" is:
// {id:<string>, stats:<stats_subdictionary>, type:<string>}
auto stats_dictionary = std::make_unique<base::DictionaryValue>();
stats_dictionary->Set("stats", std::move(stats_subdictionary));
stats_dictionary->SetString("id", stats.id());
stats_dictionary->SetString("type", stats.type());
base::Value stats_dictionary(base::Value::Type::DICTIONARY);
stats_dictionary.SetKey("stats", std::move(stats_subdictionary));
stats_dictionary.SetStringKey("id", stats.id());
stats_dictionary.SetStringKey("type", stats.type());
result_list->Append(std::move(stats_dictionary));
}
return result_list;
}

std::unique_ptr<base::Value> MemberToValue(
const webrtc::RTCStatsMemberInterface& member) {
base::Value MemberToValue(const webrtc::RTCStatsMemberInterface& member) {
switch (member.type()) {
// Types supported by base::Value are passed as the appropriate type.
case webrtc::RTCStatsMemberInterface::Type::kBool:
return std::make_unique<base::Value>(
*member.cast_to<webrtc::RTCStatsMember<bool>>());
return base::Value(*member.cast_to<webrtc::RTCStatsMember<bool>>());
case webrtc::RTCStatsMemberInterface::Type::kInt32:
return std::make_unique<base::Value>(
*member.cast_to<webrtc::RTCStatsMember<int32_t>>());
return base::Value(*member.cast_to<webrtc::RTCStatsMember<int32_t>>());
case webrtc::RTCStatsMemberInterface::Type::kString:
return std::make_unique<base::Value>(
return base::Value(
*member.cast_to<webrtc::RTCStatsMember<std::string>>());
case webrtc::RTCStatsMemberInterface::Type::kDouble:
return std::make_unique<base::Value>(
*member.cast_to<webrtc::RTCStatsMember<double>>());
return base::Value(*member.cast_to<webrtc::RTCStatsMember<double>>());
// Types not supported by base::Value are converted to string.
case webrtc::RTCStatsMemberInterface::Type::kUint32:
case webrtc::RTCStatsMemberInterface::Type::kInt64:
Expand All @@ -682,7 +677,7 @@ class InternalStandardStatsObserver : public webrtc::RTCStatsCollectorCallback {
case webrtc::RTCStatsMemberInterface::Type::kSequenceDouble:
case webrtc::RTCStatsMemberInterface::Type::kSequenceString:
default:
return std::make_unique<base::Value>(member.ValueToString());
return base::Value(member.ValueToString());
}
}

Expand Down

0 comments on commit 6dc0673

Please sign in to comment.