Skip to content

Commit

Permalink
Eliminate remaining users of DictionaryValue::GetDouble
Browse files Browse the repository at this point in the history
Bug: 1187035
Change-Id: I185b58fbd7f4fe26dc209b7b50374ace33225875
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3205356
Reviewed-by: Dirk Pranke <[email protected]>
Reviewed-by: Sylvain Defresne <[email protected]>
Reviewed-by: Lei Zhang <[email protected]>
Reviewed-by: Yuwei Huang <[email protected]>
Reviewed-by: Stephen White <[email protected]>
Reviewed-by: Yuchen Liu <[email protected]>
Reviewed-by: Reilly Grant <[email protected]>
Reviewed-by: Scott Violet <[email protected]>
Reviewed-by: Yutaka Hirano <[email protected]>
Commit-Queue: Anders Hartvoll Ruud <[email protected]>
Cr-Commit-Position: refs/heads/main@{#931940}
  • Loading branch information
andruud authored and Chromium LUCI CQ committed Oct 15, 2021
1 parent 4ddb173 commit 87021e7
Show file tree
Hide file tree
Showing 16 changed files with 74 additions and 62 deletions.
20 changes: 15 additions & 5 deletions chromecast/browser/extensions/api/tts/tts_extension_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -185,25 +185,35 @@ ExtensionFunction::ResponseAction TtsSpeakFunction::Run() {

double rate = blink::mojom::kSpeechSynthesisDoublePrefNotSet;
if (options->HasKey(constants::kRateKey)) {
EXTENSION_FUNCTION_VALIDATE(options->GetDouble(constants::kRateKey, &rate));
absl::optional<double> rate_option =
options->FindDoubleKey(constants::kRateKey);
EXTENSION_FUNCTION_VALIDATE(rate_option);
if (rate_option)
rate = *rate_option;
if (rate < 0.1 || rate > 10.0) {
return RespondNow(Error(constants::kErrorInvalidRate));
}
}

double pitch = blink::mojom::kSpeechSynthesisDoublePrefNotSet;
if (options->HasKey(constants::kPitchKey)) {
EXTENSION_FUNCTION_VALIDATE(
options->GetDouble(constants::kPitchKey, &pitch));
absl::optional<double> pitch_option =
options->FindDoubleKey(constants::kPitchKey);
EXTENSION_FUNCTION_VALIDATE(pitch_option);
if (pitch_option)
pitch = *pitch_option;
if (pitch < 0.0 || pitch > 2.0) {
return RespondNow(Error(constants::kErrorInvalidPitch));
}
}

double volume = blink::mojom::kSpeechSynthesisDoublePrefNotSet;
if (options->HasKey(constants::kVolumeKey)) {
EXTENSION_FUNCTION_VALIDATE(
options->GetDouble(constants::kVolumeKey, &volume));
absl::optional<double> volume_option =
options->FindDoubleKey(constants::kVolumeKey);
EXTENSION_FUNCTION_VALIDATE(volume_option);
if (volume_option)
volume = *volume_option;
if (volume < 0.0 || volume > 1.0) {
return RespondNow(Error(constants::kErrorInvalidVolume));
}
Expand Down
9 changes: 4 additions & 5 deletions chromecast/crash/linux/synchronized_minidump_manager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,13 @@ base::Time GetRatelimitPeriodStart(base::Value* metadata) {
base::DictionaryValue* ratelimit_params = GetRatelimitParams(metadata);
RCHECK(ratelimit_params, base::Time());

double seconds = 0.0;
RCHECK(
ratelimit_params->GetDouble(kLockfileRatelimitPeriodStartKey, &seconds),
base::Time());
absl::optional<double> seconds =
ratelimit_params->FindDoubleKey(kLockfileRatelimitPeriodStartKey);
RCHECK(seconds, base::Time());

// Return value of 0 indicates "not initialized", so we need to explicitly
// check for it and return time_t = 0 equivalent.
return seconds ? base::Time::FromDoubleT(seconds) : base::Time::UnixEpoch();
return *seconds ? base::Time::FromDoubleT(*seconds) : base::Time::UnixEpoch();
}

// Sets the time of the current ratelimit period's start in |metadata| to
Expand Down
9 changes: 5 additions & 4 deletions chromecast/media/cma/backend/volume_control.cc
Original file line number Diff line number Diff line change
Expand Up @@ -201,19 +201,20 @@ class VolumeControlInternal : public SystemVolumeControl::Delegate {
saved_volumes_writer_ = std::make_unique<base::ImportantFileWriter>(
storage_path_, thread_.task_runner(), base::Seconds(1));

double dbfs;
for (auto type : {AudioContentType::kMedia, AudioContentType::kAlarm,
AudioContentType::kCommunication}) {
CHECK(stored_values_.GetDouble(ContentTypeToDbFSPath(type), &dbfs));
volumes_[type] = VolumeControl::DbFSToVolume(dbfs);
absl::optional<double> dbfs =
stored_values_.FindDoubleKey(ContentTypeToDbFSPath(type));
CHECK(dbfs);
volumes_[type] = VolumeControl::DbFSToVolume(*dbfs);
volume_multipliers_[type] = 1.0f;

#if BUILDFLAG(SYSTEM_OWNS_VOLUME)
// ALSA owns volume; our internal mixer should not apply any scaling
// multiplier.
mixer_->SetVolume(type, 1.0f);
#else
mixer_->SetVolume(type, DbFsToScale(dbfs));
mixer_->SetVolume(type, DbFsToScale(*dbfs));
#endif

// Note that mute state is not persisted across reboots.
Expand Down
24 changes: 12 additions & 12 deletions chromecast/media/cma/backend/volume_map.cc
Original file line number Diff line number Diff line change
Expand Up @@ -70,18 +70,18 @@ void VolumeMap::LoadVolumeMap(std::unique_ptr<base::Value> cast_audio_config) {
const base::DictionaryValue* volume_map_entry;
CHECK(volume_map_list->GetDictionary(i, &volume_map_entry));

double level;
CHECK(volume_map_entry->GetDouble(kKeyLevel, &level));
CHECK_GE(level, 0.0);
CHECK_LE(level, 1.0);
CHECK_GT(level, prev_level);
prev_level = level;

double db;
CHECK(volume_map_entry->GetDouble(kKeyDb, &db));
CHECK_LE(db, 0.0);

new_map.push_back({static_cast<float>(level), static_cast<float>(db)});
absl::optional<double> level = volume_map_entry->FindDoubleKey(kKeyLevel);
CHECK(level);
CHECK_GE(*level, 0.0);
CHECK_LE(*level, 1.0);
CHECK_GT(*level, prev_level);
prev_level = *level;

absl::optional<double> db = volume_map_entry->FindDoubleKey(kKeyDb);
CHECK(db);
CHECK_LE(*db, 0.0);

new_map.push_back({static_cast<float>(*level), static_cast<float>(*db)});
}

if (new_map.empty()) {
Expand Down
4 changes: 3 additions & 1 deletion extensions/browser/api/diagnostics/diagnostics_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,10 @@ bool ParseResult(const std::string& status, std::string* ip, double* latency) {
if (!iterator.value().GetAsDictionary(&info))
return false;

if (!info->GetDouble("avg", latency))
absl::optional<double> avg = info->FindDoubleKey("avg");
if (!avg)
return false;
*latency = *avg;

*ip = iterator.key();
return true;
Expand Down
3 changes: 2 additions & 1 deletion extensions/browser/guest_view/web_view/web_view_guest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1212,7 +1212,8 @@ void WebViewGuest::ApplyAttributes(const base::DictionaryValue& params) {
SetAllowScaling(*allow_scaling);

// Check for a pending zoom from before the first navigation.
params.GetDouble(webview::kInitialZoomFactor, &pending_zoom_factor_);
pending_zoom_factor_ = params.FindDoubleKey(webview::kInitialZoomFactor)
.value_or(pending_zoom_factor_);

bool is_pending_new_window = false;
WebViewGuest* opener = GetOpener();
Expand Down
3 changes: 2 additions & 1 deletion ios/chrome/browser/notification_promo.cc
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,8 @@ void NotificationPromo::InitFromPrefs() {
if (!ntp_promo)
return;

ntp_promo->GetDouble(kPrefPromoFirstViewTime, &first_view_time_);
first_view_time_ = ntp_promo->FindDoubleKey(kPrefPromoFirstViewTime)
.value_or(first_view_time_);
ntp_promo->GetInteger(kPrefPromoViews, &views_);
closed_ = ntp_promo->FindBoolPath(kPrefPromoClosed).value_or(closed_);
}
Expand Down
4 changes: 1 addition & 3 deletions ios/web/js_messaging/web_view_js_utils_unittest.mm
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,7 @@
dictionary->GetDictionary("Key2", &inner_dictionary);
EXPECT_NE(nullptr, inner_dictionary);

double value3;
inner_dictionary->GetDouble("Key3", &value3);
EXPECT_EQ(42, value3);
EXPECT_EQ(42, *inner_dictionary->FindDoubleKey("Key3"));
}

// Tests that ValueResultFromWKResult converts NSArray to properly
Expand Down
13 changes: 7 additions & 6 deletions ios/web/public/test/web_view_interaction_test_util.mm
Original file line number Diff line number Diff line change
Expand Up @@ -215,17 +215,18 @@ CGRect GetBoundingRectOfElement(web::WebState* web_state,
if (!found)
return CGRectNull;

double left, top, width, height;
if (!(rect->GetDouble("left", &left) && rect->GetDouble("top", &top) &&
rect->GetDouble("width", &width) &&
rect->GetDouble("height", &height))) {
absl::optional<double> left = rect->FindDoubleKey("left");
absl::optional<double> top = rect->FindDoubleKey("top");
absl::optional<double> width = rect->FindDoubleKey("width");
absl::optional<double> height = rect->FindDoubleKey("height");
if (!(left && top && width && height))
return CGRectNull;
}

CGFloat scale = [[web_state->GetWebViewProxy() scrollViewProxy] zoomScale];

CGRect elementFrame =
CGRectMake(left * scale, top * scale, width * scale, height * scale);
CGRectMake(left.value() * scale, top.value() * scale,
width.value() * scale, height.value() * scale);
UIEdgeInsets contentInset =
web_state->GetWebViewProxy().scrollViewProxy.contentInset;
elementFrame =
Expand Down
7 changes: 3 additions & 4 deletions jingle/glue/utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -91,16 +91,15 @@ bool DeserializeP2PCandidate(const std::string& candidate_str,
std::string protocol;
std::string username;
std::string password;
double preference = 0;
absl::optional<double> preference = dic_value->FindDoubleKey("preference");
int generation = 0;

if (!dic_value->GetString("ip", &ip) ||
!dic_value->GetInteger("port", &port) ||
!dic_value->GetString("type", &type) ||
!dic_value->GetString("protocol", &protocol) ||
!dic_value->GetString("username", &username) ||
!dic_value->GetString("password", &password) ||
!dic_value->GetDouble("preference", &preference) ||
!dic_value->GetString("password", &password) || !preference ||
!dic_value->GetInteger("generation", &generation)) {
return false;
}
Expand All @@ -110,7 +109,7 @@ bool DeserializeP2PCandidate(const std::string& candidate_str,
candidate->set_protocol(protocol);
candidate->set_username(username);
candidate->set_password(password);
candidate->set_preference(static_cast<float>(preference));
candidate->set_preference(static_cast<float>(*preference));
candidate->set_generation(generation);

return true;
Expand Down
7 changes: 2 additions & 5 deletions net/cert/crl_set.cc
Original file line number Diff line number Diff line change
Expand Up @@ -236,11 +236,8 @@ bool CRLSet::Parse(base::StringPiece data, scoped_refptr<CRLSet>* out_crl_set) {
if (!header_dict->GetInteger("Sequence", &sequence))
return false;

double not_after;
if (!header_dict->GetDouble("NotAfter", &not_after)) {
// NotAfter is optional for now.
not_after = 0;
}
// NotAfter is optional for now.
double not_after = header_dict->FindDoubleKey("NotAfter").value_or(0);
if (not_after < 0)
return false;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,9 +219,9 @@ class NetworkErrorLoggingServiceTest : public ::testing::TestWithParam<bool> {
void ExpectDictDoubleValue(double expected_value,
const base::DictionaryValue& value,
const std::string& key) {
double double_value = 0.0;
EXPECT_TRUE(value.GetDouble(key, &double_value)) << key;
EXPECT_DOUBLE_EQ(expected_value, double_value) << key;
absl::optional<double> double_value = value.FindDoubleKey(key);
EXPECT_TRUE(double_value.has_value()) << key;
EXPECT_DOUBLE_EQ(expected_value, double_value.value_or(0.0)) << key;
}

TEST_P(NetworkErrorLoggingServiceTest, CreateService) {
Expand Down
8 changes: 4 additions & 4 deletions remoting/protocol/pairing_registry.cc
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,14 @@ PairingRegistry::Pairing PairingRegistry::Pairing::Create(
PairingRegistry::Pairing PairingRegistry::Pairing::CreateFromValue(
const base::DictionaryValue& pairing) {
std::string client_name, client_id;
double created_time_value;
if (pairing.GetDouble(kCreatedTimeKey, &created_time_value) &&
pairing.GetString(kClientNameKey, &client_name) &&
absl::optional<double> created_time_value =
pairing.FindDoubleKey(kCreatedTimeKey);
if (created_time_value && pairing.GetString(kClientNameKey, &client_name) &&
pairing.GetString(kClientIdKey, &client_id)) {
// The shared secret is optional.
std::string shared_secret;
pairing.GetString(kSharedSecretKey, &shared_secret);
base::Time created_time = base::Time::FromJsTime(created_time_value);
base::Time created_time = base::Time::FromJsTime(*created_time_value);
return Pairing(created_time, client_name, client_id, shared_secret);
}

Expand Down
6 changes: 5 additions & 1 deletion services/preferences/public/cpp/dictionary_value_update.cc
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,11 @@ bool DictionaryValueUpdate::GetInteger(base::StringPiece path,

bool DictionaryValueUpdate::GetDouble(base::StringPiece path,
double* out_value) const {
return value_->GetDouble(path, out_value);
if (absl::optional<double> value = value_->FindDoubleKey(path)) {
*out_value = *value;
return true;
}
return false;
}

bool DictionaryValueUpdate::GetString(base::StringPiece path,
Expand Down
7 changes: 1 addition & 6 deletions skia/ext/benchmarking_canvas.cc
Original file line number Diff line number Diff line change
Expand Up @@ -398,12 +398,7 @@ double BenchmarkingCanvas::GetTime(size_t index) {
const base::DictionaryValue* op;
if (!op_records_.GetDictionary(index, &op))
return 0;

double t;
if (!op->GetDouble("cmd_time", &t))
return 0;

return t;
return op->FindDoubleKey("cmd_time").value_or(0);
}

void BenchmarkingCanvas::willSave() {
Expand Down
6 changes: 5 additions & 1 deletion tools/json_schema_compiler/test/simple_api_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,11 @@ TEST(JsonSchemaCompilerSimpleTest, OnTestTypeFiredCreate) {
simple_api::TestType some_test_type;
std::unique_ptr<base::DictionaryValue> expected =
CreateTestTypeDictionary();
ASSERT_TRUE(expected->GetDouble("number", &some_test_type.number));

absl::optional<double> number_value = expected->FindDoubleKey("number");
ASSERT_TRUE(*number_value);
some_test_type.number = *number_value;

ASSERT_TRUE(expected->GetString("string", &some_test_type.string));
ASSERT_TRUE(expected->GetInteger("integer", &some_test_type.integer));
ASSERT_TRUE(expected->GetBoolean("boolean", &some_test_type.boolean));
Expand Down

0 comments on commit 87021e7

Please sign in to comment.