1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
   51
   52
   53
   54
   55
   56
   57
   58
   59
   60
   61
   62
   63
   64
   65
   66
   67
   68
   69
   70
   71
   72
   73
   74
   75
   76
   77
   78
   79
   80
   81
   82
   83
   84
   85
   86
   87
   88
   89
   90
   91
   92
   93
   94
   95
   96
   97
   98
   99
  100
  101
  102
  103
  104
  105
  106
  107
  108
  109
  110
  111
  112
  113
  114
  115
  116
  117
  118
  119
  120
  121
  122
  123
  124
  125

ash / ambient / ambient_weather_controller.h [blame]

// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#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/simple_geolocation_provider.h"
#include "components/prefs/pref_change_registrar.h"

namespace gfx {
class ImageSkia;
}  // namespace gfx

namespace ash {

class AmbientWeatherModel;
struct WeatherInfo;

// Handles fetching weather information from the backdrop server, including the
// weather condition icon image (a sun, a cloud, etc.). Owns the data model that
// caches the current weather info.
class ASH_EXPORT AmbientWeatherController
    : public SimpleGeolocationProvider::Observer,
      public SessionObserver {
 public:
  // Causes AmbientWeatherController to periodically refresh the weather info
  // in the model for as long as this object is alive. The latest weather is
  // also fetched immediately upon construction if there are currently no
  // active `ScopedRefresher`s.
  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(
      SimpleGeolocationProvider* const location_permission_provider);
  AmbientWeatherController(const AmbientWeatherController&) = delete;
  AmbientWeatherController& operator=(const AmbientWeatherController&) = delete;
  ~AmbientWeatherController() override;

  // SimpleGeolocationProvider::Observer:
  void OnGeolocationPermissionChanged(bool enabled) override;

  // SessionObserver:
  void OnActiveUserPrefServiceChanged(PrefService* pref_service) override;

  // Always returns non-null.
  std::unique_ptr<ScopedRefresher> CreateScopedRefresher();

  AmbientWeatherModel* weather_model() { return weather_model_.get(); }

 private:
  friend class AmbientWeatherControllerTest;

  // Triggers a fetch of weather information and a download of the appropriate
  // weather condition icon.
  void FetchWeather();

  void StartDownloadingWeatherConditionIcon(
      const std::optional<WeatherInfo>& weather_info);

  // Invoked upon completion of the weather icon download, |icon| can be a null
  // image if the download attempt from the url failed.
  void OnWeatherConditionIconDownloaded(float temp_f,
                                        bool show_celsius,
                                        const gfx::ImageSkia& icon);

  // Returns true when geolocation permission is allowed for the Ambient
  // Weather, i.e. geolocation access level is set either to "Allowed" or "Only
  // allowed for system".
  bool IsGeolocationUsageAllowed();

  // Returns true when weather has been disabled by policy.
  bool IsWeatherDisabledByPolicy();

  // Deletes the cached weather model.
  void ClearAmbientWeatherModel();

  void OnScopedRefresherDestroyed();

  // Callback used when the pref `kContextualGoogleIntegrationsConfiguration`
  // changes.
  void OnWeatherIntegrationPreferenceChanged(const std::string& pref_name);

  // Called when either geolocation permission or weather policy changes, which
  // determines if fetching weather is allowed.
  void OnPermissionChanged();

  const raw_ptr<SimpleGeolocationProvider> 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};
};

}  // namespace ash

#endif  // ASH_AMBIENT_AMBIENT_WEATHER_CONTROLLER_H_