send_tab_to_self_bridge.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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_SEND_TAB_TO_SELF_SEND_TAB_TO_SELF_BRIDGE_H_
  5. #define COMPONENTS_SEND_TAB_TO_SELF_SEND_TAB_TO_SELF_BRIDGE_H_
  6. #include <memory>
  7. #include <string>
  8. #include <vector>
  9. #include "base/memory/raw_ptr.h"
  10. #include "base/memory/weak_ptr.h"
  11. #include "base/scoped_observation.h"
  12. #include "base/time/time.h"
  13. #include "components/history/core/browser/history_service.h"
  14. #include "components/history/core/browser/history_service_observer.h"
  15. #include "components/send_tab_to_self/send_tab_to_self_entry.h"
  16. #include "components/send_tab_to_self/send_tab_to_self_model.h"
  17. #include "components/sync/base/model_type.h"
  18. #include "components/sync/model/model_type_store.h"
  19. #include "components/sync/model/model_type_sync_bridge.h"
  20. #include "components/sync_device_info/device_info_tracker.h"
  21. namespace syncer {
  22. class ModelTypeChangeProcessor;
  23. } // namespace syncer
  24. namespace base {
  25. class Clock;
  26. } // namespace base
  27. namespace send_tab_to_self {
  28. struct TargetDeviceInfo;
  29. // Interface for a persistence layer for send tab to self.
  30. // All interface methods have to be called on main thread.
  31. class SendTabToSelfBridge : public syncer::ModelTypeSyncBridge,
  32. public SendTabToSelfModel,
  33. public syncer::DeviceInfoTracker::Observer,
  34. public history::HistoryServiceObserver {
  35. public:
  36. // The caller should ensure that all raw pointers are not null and will
  37. // outlive this object. This is not guaranteed by this class.
  38. SendTabToSelfBridge(
  39. std::unique_ptr<syncer::ModelTypeChangeProcessor> change_processor,
  40. base::Clock* clock,
  41. syncer::OnceModelTypeStoreFactory create_store_callback,
  42. history::HistoryService* history_service,
  43. syncer::DeviceInfoTracker* device_info_tracker);
  44. SendTabToSelfBridge(const SendTabToSelfBridge&) = delete;
  45. SendTabToSelfBridge& operator=(const SendTabToSelfBridge&) = delete;
  46. ~SendTabToSelfBridge() override;
  47. // syncer::ModelTypeSyncBridge overrides.
  48. std::unique_ptr<syncer::MetadataChangeList> CreateMetadataChangeList()
  49. override;
  50. absl::optional<syncer::ModelError> MergeSyncData(
  51. std::unique_ptr<syncer::MetadataChangeList> metadata_change_list,
  52. syncer::EntityChangeList entity_data) override;
  53. absl::optional<syncer::ModelError> ApplySyncChanges(
  54. std::unique_ptr<syncer::MetadataChangeList> metadata_change_list,
  55. syncer::EntityChangeList entity_changes) override;
  56. void GetData(StorageKeyList storage_keys, DataCallback callback) override;
  57. void GetAllDataForDebugging(DataCallback callback) override;
  58. std::string GetClientTag(const syncer::EntityData& entity_data) override;
  59. std::string GetStorageKey(const syncer::EntityData& entity_data) override;
  60. void ApplyStopSyncChanges(std::unique_ptr<syncer::MetadataChangeList>
  61. delete_metadata_change_list) override;
  62. // SendTabToSelfModel overrides.
  63. std::vector<std::string> GetAllGuids() const override;
  64. const SendTabToSelfEntry* GetEntryByGUID(
  65. const std::string& guid) const override;
  66. const SendTabToSelfEntry* AddEntry(
  67. const GURL& url,
  68. const std::string& title,
  69. const std::string& target_device_cache_guid) override;
  70. void DeleteEntry(const std::string& guid) override;
  71. void DismissEntry(const std::string& guid) override;
  72. void MarkEntryOpened(const std::string& guid) override;
  73. bool IsReady() override;
  74. bool HasValidTargetDevice() override;
  75. std::vector<TargetDeviceInfo> GetTargetDeviceInfoSortedList() override;
  76. // history::HistoryServiceObserver:
  77. void OnURLsDeleted(history::HistoryService* history_service,
  78. const history::DeletionInfo& deletion_info) override;
  79. // syncer::DeviceInfoTracker::Observer overrides.
  80. void OnDeviceInfoChange() override;
  81. // For testing only.
  82. static std::unique_ptr<syncer::ModelTypeStore> DestroyAndStealStoreForTest(
  83. std::unique_ptr<SendTabToSelfBridge> bridge);
  84. void SetLocalDeviceNameForTest(const std::string& local_device_name);
  85. private:
  86. using SendTabToSelfEntries =
  87. std::map<std::string, std::unique_ptr<SendTabToSelfEntry>>;
  88. // Notify all observers of any added |new_entries| when they are added the the
  89. // model via sync.
  90. void NotifyRemoteSendTabToSelfEntryAdded(
  91. const std::vector<const SendTabToSelfEntry*>& new_entries);
  92. // Notify all observers when the entries with |guids| have been removed from
  93. // the model via sync or via history deletion.
  94. void NotifyRemoteSendTabToSelfEntryDeleted(
  95. const std::vector<std::string>& guids);
  96. // Notify all observers when any new or existing |opened_entries| have been
  97. // marked as opened in the model via sync.
  98. void NotifyRemoteSendTabToSelfEntryOpened(
  99. const std::vector<const SendTabToSelfEntry*>& opened_entries);
  100. // Notify all observers that the model is loaded;
  101. void NotifySendTabToSelfModelLoaded();
  102. // Methods used as callbacks given to DataTypeStore.
  103. void OnStoreCreated(const absl::optional<syncer::ModelError>& error,
  104. std::unique_ptr<syncer::ModelTypeStore> store);
  105. void OnReadAllData(std::unique_ptr<SendTabToSelfEntries> initial_entries,
  106. std::unique_ptr<std::string> local_device_name,
  107. const absl::optional<syncer::ModelError>& error);
  108. void OnReadAllMetadata(const absl::optional<syncer::ModelError>& error,
  109. std::unique_ptr<syncer::MetadataBatch> metadata_batch);
  110. void OnCommit(const absl::optional<syncer::ModelError>& error);
  111. // Persists the changes in the given aggregators
  112. void Commit(std::unique_ptr<syncer::ModelTypeStore::WriteBatch> batch);
  113. // Returns a specific entry for editing. Returns null if the entry does not
  114. // exist.
  115. SendTabToSelfEntry* GetMutableEntryByGUID(const std::string& guid) const;
  116. // Delete expired entries.
  117. void DoGarbageCollection();
  118. void ComputeTargetDeviceInfoSortedList();
  119. // Remove entry with |guid| from entries, but doesn't call Commit on provided
  120. // |batch|. This allows multiple for deletions without duplicate batch calls.
  121. void DeleteEntryWithBatch(const std::string& guid,
  122. syncer::ModelTypeStore::WriteBatch* batch);
  123. // Delete all of the entries that match the URLs provided.
  124. void DeleteEntries(const std::vector<GURL>& urls);
  125. void DeleteAllEntries();
  126. // |entries_| is keyed by GUIDs.
  127. SendTabToSelfEntries entries_;
  128. // |clock_| isn't owned.
  129. const raw_ptr<const base::Clock> clock_;
  130. // |history_service_| isn't owned.
  131. const raw_ptr<history::HistoryService> history_service_;
  132. // |device_info_tracker_| isn't owned.
  133. const raw_ptr<syncer::DeviceInfoTracker> device_info_tracker_;
  134. // The name of this local device.
  135. std::string local_device_name_;
  136. // In charge of actually persisting changes to disk, or loading previous data.
  137. std::unique_ptr<syncer::ModelTypeStore> store_;
  138. // A pointer to the most recently used entry used for deduplication.
  139. raw_ptr<const SendTabToSelfEntry> mru_entry_;
  140. // The list of target devices, deduplicated and sorted by most recently used.
  141. std::vector<TargetDeviceInfo> target_device_info_sorted_list_;
  142. base::ScopedObservation<history::HistoryService, HistoryServiceObserver>
  143. history_service_observation_{this};
  144. base::ScopedObservation<syncer::DeviceInfoTracker,
  145. syncer::DeviceInfoTracker::Observer>
  146. device_info_tracker_observation_{this};
  147. base::WeakPtrFactory<SendTabToSelfBridge> weak_ptr_factory_{this};
  148. };
  149. } // namespace send_tab_to_self
  150. #endif // COMPONENTS_SEND_TAB_TO_SELF_SEND_TAB_TO_SELF_BRIDGE_H_