custom_links_manager_impl.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. // Copyright 2018 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_CUSTOM_LINKS_MANAGER_IMPL_H_
  5. #define COMPONENTS_NTP_TILES_CUSTOM_LINKS_MANAGER_IMPL_H_
  6. #include <utility>
  7. #include <vector>
  8. #include "base/memory/raw_ptr.h"
  9. #include "base/memory/weak_ptr.h"
  10. #include "base/scoped_observation.h"
  11. #include "components/history/core/browser/history_service.h"
  12. #include "components/history/core/browser/history_service_observer.h"
  13. #include "components/ntp_tiles/custom_links_manager.h"
  14. #include "components/ntp_tiles/custom_links_store.h"
  15. #include "components/ntp_tiles/most_visited_sites.h"
  16. #include "components/ntp_tiles/ntp_tile.h"
  17. #include "components/prefs/pref_change_registrar.h"
  18. #include "third_party/abseil-cpp/absl/types/optional.h"
  19. class PrefService;
  20. namespace user_prefs {
  21. class PrefRegistrySyncable;
  22. } // namespace user_prefs
  23. namespace ntp_tiles {
  24. // Non-test implementation of the CustomLinksManager interface.
  25. class CustomLinksManagerImpl : public CustomLinksManager,
  26. public history::HistoryServiceObserver {
  27. public:
  28. // Restores the previous state of |current_links_| from prefs.
  29. CustomLinksManagerImpl(PrefService* prefs,
  30. // Can be nullptr in unittests.
  31. history::HistoryService* history_service);
  32. CustomLinksManagerImpl(const CustomLinksManagerImpl&) = delete;
  33. CustomLinksManagerImpl& operator=(const CustomLinksManagerImpl&) = delete;
  34. ~CustomLinksManagerImpl() override;
  35. // CustomLinksManager implementation.
  36. bool Initialize(const NTPTilesVector& tiles) override;
  37. void Uninitialize() override;
  38. bool IsInitialized() const override;
  39. const std::vector<Link>& GetLinks() const override;
  40. bool AddLink(const GURL& url, const std::u16string& title) override;
  41. bool UpdateLink(const GURL& url,
  42. const GURL& new_url,
  43. const std::u16string& new_title) override;
  44. bool ReorderLink(const GURL& url, size_t new_pos) override;
  45. bool DeleteLink(const GURL& url) override;
  46. bool UndoAction() override;
  47. base::CallbackListSubscription RegisterCallbackForOnChanged(
  48. base::RepeatingClosure callback) override;
  49. // Register preferences used by this class.
  50. static void RegisterProfilePrefs(
  51. user_prefs::PrefRegistrySyncable* user_prefs);
  52. private:
  53. void ClearLinks();
  54. // Stores the current list to the profile's preferences. Does not notify
  55. // |OnPreferenceChanged|.
  56. void StoreLinks();
  57. // Checks during instantiation to remove custom shortcut links
  58. // created through preinstalled apps.
  59. void RemoveCustomLinksForPreinstalledApps();
  60. // Returns an iterator into |custom_links_|.
  61. std::vector<Link>::iterator FindLinkWithUrl(const GURL& url);
  62. // history::HistoryServiceObserver implementation.
  63. // Deletes any Most Visited links whose URL is in |deletion_info|. Clears
  64. // |previous_links_|. Does not delete entries expired by HistoryService.
  65. void OnURLsDeleted(history::HistoryService* history_service,
  66. const history::DeletionInfo& deletion_info) override;
  67. void HistoryServiceBeingDeleted(
  68. history::HistoryService* history_service) override;
  69. // Called when the current list of links and/or initialization state in
  70. // PrefService is modified. Saves the new set of links in |current_links_|
  71. // and notifies |closure_list_|.
  72. void OnPreferenceChanged();
  73. const raw_ptr<PrefService> prefs_;
  74. CustomLinksStore store_;
  75. std::vector<Link> current_links_;
  76. // The state of the current list of links before the last action was
  77. // performed.
  78. absl::optional<std::vector<Link>> previous_links_;
  79. // List of closures to be invoked when custom links are updated by outside
  80. // sources.
  81. base::RepeatingClosureList closure_list_;
  82. // Observer for the HistoryService.
  83. base::ScopedObservation<history::HistoryService,
  84. history::HistoryServiceObserver>
  85. history_service_observation_{this};
  86. // Observer for Chrome sync changes to |prefs::kCustomLinksList| and
  87. // |prefs::kCustomLinksInitialized|.
  88. PrefChangeRegistrar pref_change_registrar_;
  89. // Used to ignore notifications from |pref_change_registrar_| that we trigger
  90. // ourselves when updating the preferences.
  91. bool updating_preferences_ = false;
  92. base::WeakPtrFactory<CustomLinksManagerImpl> weak_ptr_factory_{this};
  93. };
  94. } // namespace ntp_tiles
  95. #endif // COMPONENTS_NTP_TILES_CUSTOM_LINKS_MANAGER_IMPL_H_