Commit 8b17e77f authored by Xiaohui Chen's avatar Xiaohui Chen Committed by Commit Bot

ambient: fix fetch weather crash

Add more data type check for backend response.

Bug: b:169854546
Test: run locally and start ambient mode with celsius settings
Change-Id: If2cd60b363943fe6f3728a9e614a8a1f35431b7f
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2444178Reviewed-by: default avatarJeroen Dhollander <jeroendh@chromium.org>
Reviewed-by: default avatarTao Wu <wutao@chromium.org>
Commit-Queue: Xiaohui Chen <xiaohuic@chromium.org>
Cr-Commit-Position: refs/heads/master@{#813303}
parent 14a08d58
......@@ -146,26 +146,60 @@ AmbientModeTopicType ToAmbientModeTopicType(
}
}
WeatherInfo ToWeatherInfo(const base::Value& result) {
base::Optional<std::string> GetStringValue(base::Value::ConstListView values,
size_t field_number) {
if (values.empty() || values.size() < field_number)
return base::nullopt;
const base::Value& v = values[field_number - 1];
if (!v.is_string())
return base::nullopt;
return v.GetString();
}
base::Optional<double> GetDoubleValue(base::Value::ConstListView values,
size_t field_number) {
if (values.empty() || values.size() < field_number)
return base::nullopt;
const base::Value& v = values[field_number - 1];
if (!v.is_double() && !v.is_int())
return base::nullopt;
return v.GetDouble();
}
base::Optional<bool> GetBoolValue(base::Value::ConstListView values,
size_t field_number) {
if (values.empty() || values.size() < field_number)
return base::nullopt;
const base::Value& v = values[field_number - 1];
if (v.is_bool())
return v.GetBool();
if (v.is_int())
return v.GetInt() > 0;
return base::nullopt;
}
base::Optional<WeatherInfo> ToWeatherInfo(const base::Value& result) {
DCHECK(result.is_list());
if (!result.is_list())
return base::nullopt;
WeatherInfo weather_info;
const auto& list_result = result.GetList();
const base::Value& condition_icon_url_value =
list_result[backdrop::WeatherInfo::kConditionIconUrlFieldNumber - 1];
if (!condition_icon_url_value.is_none())
weather_info.condition_icon_url = condition_icon_url_value.GetString();
const base::Value& temp_f_value =
list_result[backdrop::WeatherInfo::kTempFFieldNumber - 1];
if (!temp_f_value.is_none())
weather_info.temp_f = temp_f_value.GetDouble();
const base::Value& show_celsius_value =
list_result[backdrop::WeatherInfo::kShowCelsiusFieldNumber - 1];
if (!show_celsius_value.is_none())
weather_info.show_celsius = show_celsius_value.GetBool();
weather_info.condition_icon_url = GetStringValue(
list_result, backdrop::WeatherInfo::kConditionIconUrlFieldNumber);
weather_info.temp_f =
GetDoubleValue(list_result, backdrop::WeatherInfo::kTempFFieldNumber);
weather_info.show_celsius =
GetBoolValue(list_result, backdrop::WeatherInfo::kShowCelsiusFieldNumber)
.value_or(false);
return weather_info;
}
......@@ -388,7 +422,9 @@ void AmbientBackendControllerImpl::FetchWeather(FetchWeatherCallback callback) {
[](FetchWeatherCallback callback,
std::unique_ptr<BackdropURLLoader> backdrop_url_loader,
std::unique_ptr<std::string> response) {
if (response && !response->empty()) {
constexpr char kJsonPrefix[] = ")]}'\n";
if (response && response->length() > strlen(kJsonPrefix)) {
auto json_handler =
[](FetchWeatherCallback callback,
data_decoder::DataDecoder::ValueOrError result) {
......@@ -400,7 +436,6 @@ void AmbientBackendControllerImpl::FetchWeather(FetchWeatherCallback callback) {
}
};
constexpr char kJsonPrefix[] = ")]}'\n";
data_decoder::DataDecoder::ParseJsonIsolated(
response->substr(strlen(kJsonPrefix)),
base::BindOnce(json_handler, std::move(callback)));
......
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