#include "ash/birch/birch_weather_provider.h"
#include <string>
#include "ash/ambient/ambient_controller.h"
#include "ash/birch/birch_item.h"
#include "ash/birch/birch_model.h"
#include "ash/birch/birch_ranker.h"
#include "ash/constants/ash_pref_names.h"
#include "ash/constants/ash_switches.h"
#include "ash/public/cpp/ambient/ambient_backend_controller.h"
#include "ash/public/cpp/ambient/weather_info.h"
#include "ash/public/cpp/session/session_types.h"
#include "ash/resources/vector_icons/vector_icons.h"
#include "ash/session/session_controller_impl.h"
#include "ash/shell.h"
#include "ash/strings/grit/ash_strings.h"
#include "base/check.h"
#include "base/command_line.h"
#include "base/functional/bind.h"
#include "base/strings/utf_string_conversions.h"
#include "chromeos/ash/components/geolocation/system_location_provider.h"
#include "components/prefs/pref_service.h"
#include "components/user_manager/user_names.h"
#include "ui/base/l10n/l10n_util.h"
#include "url/gurl.h"
namespace ash {
BirchWeatherProvider::BirchWeatherProvider(BirchModel* birch_model)
: birch_model_(birch_model) {}
BirchWeatherProvider::~BirchWeatherProvider() = default;
void BirchWeatherProvider::RequestBirchDataFetch() {
if (base::CommandLine::ForCurrentProcess()->HasSwitch(
ash::switches::kDisableBirchWeatherApiForTesting) &&
!base::CommandLine::ForCurrentProcess()->HasSwitch(
ash::switches::kEnableBirchWeatherApiForTestingOverride)) {
Shell::Get()->birch_model()->SetWeatherItems({});
return;
}
if (!Shell::Get()->session_controller()->IsActiveUserSessionStarted() ||
Shell::Get()->session_controller()->IsActiveAccountManaged()) {
Shell::Get()->birch_model()->SetWeatherItems({});
return;
}
const auto* pref_service =
Shell::Get()->session_controller()->GetLastActiveUserPrefService();
if (!pref_service ||
!base::Contains(pref_service->GetList(
prefs::kContextualGoogleIntegrationsConfiguration),
prefs::kWeatherIntegrationName)) {
Shell::Get()->birch_model()->SetWeatherItems({});
return;
}
if (!SystemLocationProvider::GetInstance()
->IsGeolocationUsageAllowedForSystem()) {
birch_model_->SetWeatherItems({});
return;
}
const UserSession* session =
Shell::Get()->session_controller()->GetUserSession(0);
if (session->user_info.account_id == user_manager::StubAccountId()) {
birch_model_->SetWeatherItems({});
return;
}
BirchRanker ranker(base::Time::Now());
if (!ranker.IsMorning()) {
birch_model_->SetWeatherItems({});
return;
}
if (last_weather_info_.has_value() &&
base::Time::Now() < last_fetch_time_ + base::Minutes(5)) {
OnWeatherInfoFetched(last_weather_info_);
return;
}
if (is_fetching_) {
return;
}
is_fetching_ = true;
if (!birch_model_->birch_client()) {
FetchWeather();
return;
}
birch_model_->birch_client()->WaitForRefreshTokens(base::BindOnce(
&BirchWeatherProvider::FetchWeather, weak_factory_.GetWeakPtr()));
}
void BirchWeatherProvider::FetchWeather() {
Shell::Get()
->ambient_controller()
->ambient_backend_controller()
->FetchWeather("chromeos-system-ui",
base::BindOnce(&BirchWeatherProvider::OnWeatherInfoFetched,
weak_factory_.GetWeakPtr()));
}
void BirchWeatherProvider::OnWeatherInfoFetched(
const std::optional<WeatherInfo>& weather_info) {
is_fetching_ = false;
if (!weather_info || !weather_info->temp_f.has_value() ||
!weather_info->condition_icon_url ||
!weather_info->condition_description ||
weather_info->condition_icon_url->empty()) {
last_weather_info_.reset();
birch_model_->SetWeatherItems({});
return;
}
last_weather_info_ = weather_info;
last_fetch_time_ = base::Time::Now();
AddItemToBirchModel(base::UTF8ToUTF16(*weather_info->condition_description),
*weather_info->temp_f, *weather_info->condition_icon_url);
}
void BirchWeatherProvider::AddItemToBirchModel(
const std::u16string& weather_description,
float temp_f,
const std::string& icon_url) {
std::vector<BirchWeatherItem> items;
items.emplace_back(weather_description, temp_f, GURL(icon_url));
birch_model_->SetWeatherItems(std::move(items));
}
void BirchWeatherProvider::ResetCacheForTest() {
last_weather_info_.reset();
last_fetch_time_ = base::Time();
}
}