popular_sites_impl.h 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. // Copyright 2016 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef COMPONENTS_NTP_TILES_POPULAR_SITES_IMPL_H_
  5. #define COMPONENTS_NTP_TILES_POPULAR_SITES_IMPL_H_
  6. #include <map>
  7. #include <memory>
  8. #include <string>
  9. #include "base/callback.h"
  10. #include "base/memory/raw_ptr.h"
  11. #include "base/memory/weak_ptr.h"
  12. #include "components/ntp_tiles/popular_sites.h"
  13. #include "services/data_decoder/public/cpp/data_decoder.h"
  14. #include "url/gurl.h"
  15. namespace network {
  16. class SimpleURLLoader;
  17. class SharedURLLoaderFactory;
  18. }
  19. namespace user_prefs {
  20. class PrefRegistrySyncable;
  21. }
  22. namespace variations {
  23. class VariationsService;
  24. }
  25. class PrefService;
  26. class TemplateURLService;
  27. namespace ntp_tiles {
  28. // Actual (non-test) implementation of the PopularSites interface. Caches the
  29. // downloaded file on disk to avoid re-downloading on every startup.
  30. class PopularSitesImpl : public PopularSites {
  31. public:
  32. PopularSitesImpl(
  33. PrefService* prefs,
  34. const TemplateURLService* template_url_service,
  35. variations::VariationsService* variations_service,
  36. scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory);
  37. PopularSitesImpl(const PopularSitesImpl&) = delete;
  38. PopularSitesImpl& operator=(const PopularSitesImpl&) = delete;
  39. ~PopularSitesImpl() override;
  40. // PopularSites implementation.
  41. bool MaybeStartFetch(bool force_download, FinishedCallback callback) override;
  42. const std::map<SectionType, SitesVector>& sections() const override;
  43. GURL GetLastURLFetched() const override;
  44. GURL GetURLToFetch() override;
  45. std::string GetDirectoryToFetch() override;
  46. std::string GetCountryToFetch() override;
  47. std::string GetVersionToFetch() override;
  48. const base::ListValue* GetCachedJson() override;
  49. // Register preferences used by this class.
  50. static void RegisterProfilePrefs(
  51. user_prefs::PrefRegistrySyncable* user_prefs);
  52. private:
  53. // Fetch the popular sites at the given URL, overwriting any cache in prefs
  54. // that already exists.
  55. void FetchPopularSites();
  56. // Called once SimpleURLLoader completes the network request.
  57. void OnSimpleLoaderComplete(std::unique_ptr<std::string> response_body);
  58. void OnJsonParsed(data_decoder::DataDecoder::ValueOrError result);
  59. void OnDownloadFailed();
  60. // Parameters set from constructor.
  61. const raw_ptr<PrefService> prefs_;
  62. const raw_ptr<const TemplateURLService> template_url_service_;
  63. const raw_ptr<variations::VariationsService> variations_;
  64. scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory_;
  65. // Set by MaybeStartFetch() and called after fetch completes.
  66. FinishedCallback callback_;
  67. std::unique_ptr<network::SimpleURLLoader> simple_url_loader_;
  68. bool is_fallback_;
  69. std::map<SectionType, SitesVector> sections_;
  70. GURL pending_url_;
  71. int version_in_pending_url_;
  72. base::WeakPtrFactory<PopularSitesImpl> weak_ptr_factory_{this};
  73. };
  74. } // namespace ntp_tiles
  75. #endif // COMPONENTS_NTP_TILES_POPULAR_SITES_IMPL_H_