0

[Code health] Avoid Value::GetAsDictionary (in /components/safe_search_api)

This method is deprecated, so port away from it, along with other nearby deprecated base::Value APIs that can be modernized at least somewhat locally.

This CL was uploaded by git cl split.

R=michaelpg@chromium.org

Bug: 1187011
Change-Id: I3997af08ccfe170152120051b7f037ae589849db
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3827381
Auto-Submit: Maks Orlovich <morlovich@chromium.org>
Reviewed-by: Michael Giuffrida <michaelpg@chromium.org>
Commit-Queue: Michael Giuffrida <michaelpg@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1037294}
This commit is contained in:
Maks Orlovich
2022-08-19 22:52:16 +00:00
committed by Chromium LUCI CQ
parent 78e0072dec
commit c3091dec6e

@ -42,30 +42,29 @@ std::string BuildRequestData(const std::string& api_key, const GURL& url) {
// returns true on success. Otherwise, returns false and doesn't set |is_porn|.
bool ParseResponse(const std::string& response, bool* is_porn) {
absl::optional<base::Value> optional_value = base::JSONReader::Read(response);
const base::DictionaryValue* dict = nullptr;
if (!optional_value || !optional_value.value().GetAsDictionary(&dict)) {
if (!optional_value || !optional_value.value().is_dict()) {
DLOG(WARNING) << "ParseResponse failed to parse global dictionary";
return false;
}
const base::ListValue* classifications_list = nullptr;
if (!dict->GetList("classifications", &classifications_list)) {
const base::Value::Dict& dict = optional_value.value().GetDict();
const base::Value::List* classifications_list =
dict.FindList("classifications");
if (!classifications_list) {
DLOG(WARNING) << "ParseResponse failed to parse classifications list";
return false;
}
if (classifications_list->GetListDeprecated().size() != 1) {
if (classifications_list->size() != 1u) {
DLOG(WARNING) << "ParseResponse expected exactly one result";
return false;
}
const base::Value& classification_value =
classifications_list->GetListDeprecated()[0];
const base::Value& classification_value = (*classifications_list)[0];
if (!classification_value.is_dict()) {
DLOG(WARNING) << "ParseResponse failed to parse classification dict";
return false;
}
const base::DictionaryValue& classification_dict =
base::Value::AsDictionaryValue(classification_value);
const base::Value::Dict& classification_dict = classification_value.GetDict();
absl::optional<bool> is_porn_opt =
classification_dict.FindBoolKey("pornography");
classification_dict.FindBool("pornography");
if (is_porn_opt.has_value())
*is_porn = is_porn_opt.value();
return true;