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

ash / birch / birch_weather_provider.h [blame]

// Copyright 2024 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_BIRCH_BIRCH_WEATHER_PROVIDER_H_
#define ASH_BIRCH_BIRCH_WEATHER_PROVIDER_H_

#include <optional>
#include <string>

#include "ash/ash_export.h"
#include "ash/birch/birch_data_provider.h"
#include "ash/public/cpp/ambient/weather_info.h"
#include "base/memory/weak_ptr.h"
#include "base/time/time.h"

namespace gfx {
class ImageSkia;
}

namespace ash {

class BirchModel;

class ASH_EXPORT BirchWeatherProvider : public BirchDataProvider {
 public:
  explicit BirchWeatherProvider(BirchModel* birch_model);
  BirchWeatherProvider(const BirchWeatherProvider&) = delete;
  BirchWeatherProvider& operator=(const BirchWeatherProvider&) = delete;
  ~BirchWeatherProvider() override;

  // Called from birch model to request weather information to be displayed in
  // UI.
  void RequestBirchDataFetch() override;

  void ResetCacheForTest();

 private:
  // Performs the weather fetch via the ambient controller.
  void FetchWeather();

  // Called in response to a weather info request. It initiates icon fetch from
  // the URL provided in the weather info.
  void OnWeatherInfoFetched(const std::optional<WeatherInfo>& weather_info);

  // Callback to weather info icon request. It will update birch model with the
  // fetched weather info (including the downloaded weather icon).
  void OnWeatherConditionIconDownloaded(
      const std::string& condition_icon_url,
      const std::u16string& weather_description,
      float temp_f,
      const gfx::ImageSkia& icon);

  // Adds the weather item to the birch model.
  void AddItemToBirchModel(const std::u16string& weather_description,
                           float temp_f,
                           const std::string& icon_url);

  const raw_ptr<BirchModel> birch_model_;
  bool is_fetching_ = false;

  // Support for caching the last fetch.
  base::Time last_fetch_time_;
  std::optional<WeatherInfo> last_weather_info_;

  base::WeakPtrFactory<BirchWeatherProvider> weak_factory_{this};
};

}  // namespace ash

#endif  // ASH_BIRCH_BIRCH_WEATHER_PROVIDER_H_