favicon_backend_wrapper.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. // Copyright 2020 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 WEBLAYER_BROWSER_FAVICON_FAVICON_BACKEND_WRAPPER_H_
  5. #define WEBLAYER_BROWSER_FAVICON_FAVICON_BACKEND_WRAPPER_H_
  6. #include <memory>
  7. #include <vector>
  8. #include "base/files/file_path.h"
  9. #include "base/memory/ref_counted.h"
  10. #include "base/memory/ref_counted_delete_on_sequence.h"
  11. #include "base/timer/timer.h"
  12. #include "components/favicon/core/favicon_backend_delegate.h"
  13. #include "components/favicon_base/favicon_types.h"
  14. class GURL;
  15. namespace base {
  16. class FilePath;
  17. class SequencedTaskRunner;
  18. } // namespace base
  19. namespace favicon {
  20. class FaviconBackend;
  21. }
  22. namespace weblayer {
  23. // FaviconBackendWrapper runs on a background task-runner and owns the database
  24. // side of favicons. This class largely delegates to favicon::FaviconBackend.
  25. class FaviconBackendWrapper
  26. : public base::RefCountedDeleteOnSequence<FaviconBackendWrapper>,
  27. public favicon::FaviconBackendDelegate {
  28. public:
  29. explicit FaviconBackendWrapper(
  30. scoped_refptr<base::SequencedTaskRunner> task_runner);
  31. FaviconBackendWrapper(const FaviconBackendWrapper&) = delete;
  32. FaviconBackendWrapper& operator=(const FaviconBackendWrapper&) = delete;
  33. void Init(const base::FilePath& db_path);
  34. void Shutdown();
  35. void DeleteAndRecreateDatabase();
  36. // All of these functions are called by the FaviconServiceImpl. They call
  37. // through to |favicon_backend_|.
  38. std::vector<favicon_base::FaviconRawBitmapResult> GetFaviconsForUrl(
  39. const GURL& page_url,
  40. const favicon_base::IconTypeSet& icon_types,
  41. const std::vector<int>& desired_sizes);
  42. favicon_base::FaviconRawBitmapResult GetLargestFaviconForUrl(
  43. const GURL& page_url,
  44. const std::vector<favicon_base::IconTypeSet>& icon_types_list,
  45. int minimum_size_in_pixels);
  46. void SetFaviconsOutOfDateForPage(const GURL& page_url);
  47. void SetFavicons(const base::flat_set<GURL>& page_urls,
  48. favicon_base::IconType icon_type,
  49. const GURL& icon_url,
  50. const std::vector<SkBitmap>& bitmaps);
  51. void CloneFaviconMappingsForPages(
  52. const GURL& page_url_to_read,
  53. const favicon_base::IconTypeSet& icon_types,
  54. const base::flat_set<GURL>& page_urls_to_write);
  55. std::vector<favicon_base::FaviconRawBitmapResult> GetFavicon(
  56. const GURL& icon_url,
  57. favicon_base::IconType icon_type,
  58. const std::vector<int>& desired_sizes);
  59. std::vector<favicon_base::FaviconRawBitmapResult>
  60. UpdateFaviconMappingsAndFetch(const base::flat_set<GURL>& page_urls,
  61. const GURL& icon_url,
  62. favicon_base::IconType icon_type,
  63. const std::vector<int>& desired_sizes);
  64. void DeleteFaviconMappings(const base::flat_set<GURL>& page_urls,
  65. favicon_base::IconType icon_type);
  66. // favicon::FaviconBackendDelegate:
  67. std::vector<GURL> GetCachedRecentRedirectsForPage(
  68. const GURL& page_url) override;
  69. private:
  70. friend class base::RefCountedDeleteOnSequence<FaviconBackendWrapper>;
  71. friend class base::DeleteHelper<FaviconBackendWrapper>;
  72. friend class FaviconBackendWrapperTest;
  73. ~FaviconBackendWrapper() override;
  74. void ScheduleCommit();
  75. void Commit();
  76. // Called to expire (remove) out of date icons and restart the timer.
  77. void OnExpireTimerFired();
  78. scoped_refptr<base::SequencedTaskRunner> task_runner_;
  79. // Timer used to delay commits for a short amount of time. This done to
  80. // batch commits.
  81. base::OneShotTimer commit_timer_;
  82. // The real implementation of the backend. If there is a problem
  83. // initializing the database this will be null.
  84. std::unique_ptr<favicon::FaviconBackend> favicon_backend_;
  85. // Timer used to remove items from the database that are likely no longer
  86. // needed.
  87. base::OneShotTimer expire_timer_;
  88. base::FilePath db_path_;
  89. };
  90. // These values are here only for tests.
  91. // Amount of time before favicons are removed. That is, any favicons downloaded
  92. // before this amount of time are removed.
  93. constexpr base::TimeDelta kTimeDeltaWhenEntriesAreRemoved = base::Days(30);
  94. // See comment near kMaxNumberOfEntriesToRemoveAtATime for details on this.
  95. constexpr base::TimeDelta kTimeDeltaForRunningExpireWithRemainingWork =
  96. base::Minutes(2);
  97. } // namespace weblayer
  98. #endif // WEBLAYER_BROWSER_FAVICON_FAVICON_BACKEND_WRAPPER_H_