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( ...@@ -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()); DCHECK(result.is_list());
if (!result.is_list())
return base::nullopt;
WeatherInfo weather_info; WeatherInfo weather_info;
const auto& list_result = result.GetList(); const auto& list_result = result.GetList();
const base::Value& condition_icon_url_value = weather_info.condition_icon_url = GetStringValue(
list_result[backdrop::WeatherInfo::kConditionIconUrlFieldNumber - 1]; list_result, backdrop::WeatherInfo::kConditionIconUrlFieldNumber);
if (!condition_icon_url_value.is_none()) weather_info.temp_f =
weather_info.condition_icon_url = condition_icon_url_value.GetString(); GetDoubleValue(list_result, backdrop::WeatherInfo::kTempFFieldNumber);
weather_info.show_celsius =
const base::Value& temp_f_value = GetBoolValue(list_result, backdrop::WeatherInfo::kShowCelsiusFieldNumber)
list_result[backdrop::WeatherInfo::kTempFFieldNumber - 1]; .value_or(false);
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();
return weather_info; return weather_info;
} }
...@@ -388,7 +422,9 @@ void AmbientBackendControllerImpl::FetchWeather(FetchWeatherCallback callback) { ...@@ -388,7 +422,9 @@ void AmbientBackendControllerImpl::FetchWeather(FetchWeatherCallback callback) {
[](FetchWeatherCallback callback, [](FetchWeatherCallback callback,
std::unique_ptr<BackdropURLLoader> backdrop_url_loader, std::unique_ptr<BackdropURLLoader> backdrop_url_loader,
std::unique_ptr<std::string> response) { std::unique_ptr<std::string> response) {
if (response && !response->empty()) { constexpr char kJsonPrefix[] = ")]}'\n";
if (response && response->length() > strlen(kJsonPrefix)) {
auto json_handler = auto json_handler =
[](FetchWeatherCallback callback, [](FetchWeatherCallback callback,
data_decoder::DataDecoder::ValueOrError result) { data_decoder::DataDecoder::ValueOrError result) {
...@@ -400,7 +436,6 @@ void AmbientBackendControllerImpl::FetchWeather(FetchWeatherCallback callback) { ...@@ -400,7 +436,6 @@ void AmbientBackendControllerImpl::FetchWeather(FetchWeatherCallback callback) {
} }
}; };
constexpr char kJsonPrefix[] = ")]}'\n";
data_decoder::DataDecoder::ParseJsonIsolated( data_decoder::DataDecoder::ParseJsonIsolated(
response->substr(strlen(kJsonPrefix)), response->substr(strlen(kJsonPrefix)),
base::BindOnce(json_handler, std::move(callback))); 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