#ifndef ASH_AMBIENT_AMBIENT_WEATHER_CONTROLLER_H_
#define ASH_AMBIENT_AMBIENT_WEATHER_CONTROLLER_H_
#include <memory>
#include <optional>
#include "ash/ash_export.h"
#include "ash/public/cpp/session/session_observer.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/weak_ptr.h"
#include "base/timer/timer.h"
#include "chromeos/ash/components/geolocation/system_location_provider.h"
#include "components/prefs/pref_change_registrar.h"
namespace gfx {
class ImageSkia;
}
namespace ash {
class AmbientWeatherModel;
struct WeatherInfo;
class ASH_EXPORT AmbientWeatherController
: public SystemLocationProvider::Observer,
public SessionObserver {
public:
class ScopedRefresher {
public:
ScopedRefresher(const ScopedRefresher&) = delete;
ScopedRefresher& operator=(const ScopedRefresher&) = delete;
~ScopedRefresher();
private:
friend class AmbientWeatherController;
explicit ScopedRefresher(AmbientWeatherController* controller);
const raw_ptr<AmbientWeatherController> controller_;
};
explicit AmbientWeatherController(
SystemLocationProvider* const location_permission_provider);
AmbientWeatherController(const AmbientWeatherController&) = delete;
AmbientWeatherController& operator=(const AmbientWeatherController&) = delete;
~AmbientWeatherController() override;
void OnGeolocationPermissionChanged(bool enabled) override;
void OnActiveUserPrefServiceChanged(PrefService* pref_service) override;
std::unique_ptr<ScopedRefresher> CreateScopedRefresher();
AmbientWeatherModel* weather_model() { return weather_model_.get(); }
private:
friend class AmbientWeatherControllerTest;
void FetchWeather();
void StartDownloadingWeatherConditionIcon(
const std::optional<WeatherInfo>& weather_info);
void OnWeatherConditionIconDownloaded(float temp_f,
bool show_celsius,
const gfx::ImageSkia& icon);
bool IsGeolocationUsageAllowed();
bool IsWeatherDisabledByPolicy();
void ClearAmbientWeatherModel();
void OnScopedRefresherDestroyed();
void OnWeatherIntegrationPreferenceChanged(const std::string& pref_name);
void OnPermissionChanged();
const raw_ptr<SystemLocationProvider> location_permission_provider_ = nullptr;
std::unique_ptr<AmbientWeatherModel> weather_model_;
int num_active_scoped_refreshers_ = 0;
base::RepeatingTimer weather_refresh_timer_;
PrefChangeRegistrar pref_change_registrar_;
ScopedSessionObserver scoped_session_observer_{this};
base::WeakPtrFactory<AmbientWeatherController> weak_factory_{this};
};
}
#endif