bookmark_model_type_processor.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. // Copyright 2017 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_SYNC_BOOKMARKS_BOOKMARK_MODEL_TYPE_PROCESSOR_H_
  5. #define COMPONENTS_SYNC_BOOKMARKS_BOOKMARK_MODEL_TYPE_PROCESSOR_H_
  6. #include <memory>
  7. #include <string>
  8. #include "base/callback.h"
  9. #include "base/callback_forward.h"
  10. #include "base/memory/raw_ptr.h"
  11. #include "base/memory/weak_ptr.h"
  12. #include "base/sequence_checker.h"
  13. #include "components/sync/engine/model_type_processor.h"
  14. #include "components/sync/model/model_type_controller_delegate.h"
  15. #include "components/sync_bookmarks/synced_bookmark_tracker.h"
  16. class BookmarkUndoService;
  17. namespace bookmarks {
  18. class BookmarkModel;
  19. }
  20. namespace favicon {
  21. class FaviconService;
  22. }
  23. namespace sync_bookmarks {
  24. class BookmarkModelObserverImpl;
  25. class BookmarkModelTypeProcessor : public syncer::ModelTypeProcessor,
  26. public syncer::ModelTypeControllerDelegate {
  27. public:
  28. // |bookmark_undo_service| must not be nullptr and must outlive this object.
  29. explicit BookmarkModelTypeProcessor(
  30. BookmarkUndoService* bookmark_undo_service);
  31. BookmarkModelTypeProcessor(const BookmarkModelTypeProcessor&) = delete;
  32. BookmarkModelTypeProcessor& operator=(const BookmarkModelTypeProcessor&) =
  33. delete;
  34. ~BookmarkModelTypeProcessor() override;
  35. // ModelTypeProcessor implementation.
  36. void ConnectSync(std::unique_ptr<syncer::CommitQueue> worker) override;
  37. void DisconnectSync() override;
  38. void GetLocalChanges(size_t max_entries,
  39. GetLocalChangesCallback callback) override;
  40. void OnCommitCompleted(
  41. const sync_pb::ModelTypeState& type_state,
  42. const syncer::CommitResponseDataList& committed_response_list,
  43. const syncer::FailedCommitResponseDataList& error_response_list) override;
  44. void OnUpdateReceived(const sync_pb::ModelTypeState& type_state,
  45. syncer::UpdateResponseDataList updates) override;
  46. // ModelTypeControllerDelegate implementation.
  47. void OnSyncStarting(const syncer::DataTypeActivationRequest& request,
  48. StartCallback start_callback) override;
  49. void OnSyncStopping(syncer::SyncStopMetadataFate metadata_fate) override;
  50. void GetAllNodesForDebugging(AllNodesCallback callback) override;
  51. void GetTypeEntitiesCountForDebugging(
  52. base::OnceCallback<void(const syncer::TypeEntitiesCount&)> callback)
  53. const override;
  54. void RecordMemoryUsageAndCountsHistograms() override;
  55. // Encodes all sync metadata into a string, representing a state that can be
  56. // restored via ModelReadyToSync() below.
  57. std::string EncodeSyncMetadata() const;
  58. // It mainly decodes a BookmarkModelMetadata proto serialized in
  59. // |metadata_str|, and uses it to fill in the tracker and the model type state
  60. // objects. |model| must not be null and must outlive this object. It is used
  61. // to the retrieve the local node ids, and is stored in the processor to be
  62. // used for further model operations. |schedule_save_closure| is a repeating
  63. // closure used to schedule a save of the bookmark model together with the
  64. // metadata.
  65. void ModelReadyToSync(const std::string& metadata_str,
  66. const base::RepeatingClosure& schedule_save_closure,
  67. bookmarks::BookmarkModel* model);
  68. // Sets the favicon service used when processing remote updates. It must be
  69. // called before the processor is ready to receive remote updates, and hence
  70. // before OnSyncStarting() is called. |favicon_service| must not be null.
  71. void SetFaviconService(favicon::FaviconService* favicon_service);
  72. // Returns the estimate of dynamically allocated memory in bytes.
  73. size_t EstimateMemoryUsage() const;
  74. const SyncedBookmarkTracker* GetTrackerForTest() const;
  75. bool IsConnectedForTest() const;
  76. base::WeakPtr<syncer::ModelTypeControllerDelegate> GetWeakPtr();
  77. private:
  78. SEQUENCE_CHECKER(sequence_checker_);
  79. // If preconditions are met, inform sync that we are ready to connect.
  80. void ConnectIfReady();
  81. // Nudges worker if there are any local entities to be committed. Should only
  82. // be called after initial sync is done and processor is tracking sync
  83. // entities.
  84. void NudgeForCommitIfNeeded();
  85. // Performs the required clean up when bookmark model is being deleted.
  86. void OnBookmarkModelBeingDeleted();
  87. // Process specifically calls to OnUpdateReceived() that correspond to the
  88. // initial merge of bookmarks (e.g. was just enabled).
  89. void OnInitialUpdateReceived(const sync_pb::ModelTypeState& type_state,
  90. syncer::UpdateResponseDataList updates);
  91. // Instantiates the required objects to track metadata and starts observing
  92. // changes from the bookmark model.
  93. void StartTrackingMetadata();
  94. void StopTrackingMetadata();
  95. // Creates a DictionaryValue for local and remote debugging information about
  96. // |node| and appends it to |all_nodes|. It does the same for child nodes
  97. // recursively. |index| is the index of |node| within its parent. |index|
  98. // could computed from |node|, however it's much cheaper to pass from outside
  99. // since we iterate over child nodes already in the calling sites.
  100. void AppendNodeAndChildrenForDebugging(const bookmarks::BookmarkNode* node,
  101. int index,
  102. base::Value::List* all_nodes) const;
  103. // Stores the start callback in between OnSyncStarting() and
  104. // ModelReadyToSync().
  105. StartCallback start_callback_;
  106. // The bookmark model we are processing local changes from and forwarding
  107. // remote changes to. It is set during ModelReadyToSync(), which is called
  108. // during startup, as part of the bookmark-loading process.
  109. raw_ptr<bookmarks::BookmarkModel> bookmark_model_ = nullptr;
  110. // Used to when processing remote updates to apply favicon information. It's
  111. // not set at start up because it's only avialable after the bookmark model
  112. // has been loaded.
  113. raw_ptr<favicon::FaviconService> favicon_service_ = nullptr;
  114. // Used to suspend bookmark undo when processing remote changes.
  115. const raw_ptr<BookmarkUndoService> bookmark_undo_service_;
  116. // The callback used to schedule the persistence of bookmark model as well as
  117. // the metadata to a file during which latest metadata should also be pulled
  118. // via EncodeSyncMetadata. Processor should invoke it upon changes in the
  119. // metadata that don't imply changes in the model itself. Persisting updates
  120. // that imply model changes is the model's responsibility.
  121. base::RepeatingClosure schedule_save_closure_;
  122. // Reference to the CommitQueue.
  123. //
  124. // The interface hides the posting of tasks across threads as well as the
  125. // CommitQueue's implementation. Both of these features are
  126. // useful in tests.
  127. std::unique_ptr<syncer::CommitQueue> worker_;
  128. // Keeps the mapping between server ids and bookmarks nodes together with sync
  129. // metadata. It is constructed and set during ModelReadyToSync(), if the
  130. // loaded bookmarks JSON contained previous sync metadata, or upon completion
  131. // of initial sync, which is called during startup, as part of the
  132. // bookmark-loading process.
  133. std::unique_ptr<SyncedBookmarkTracker> bookmark_tracker_;
  134. // GUID string that identifies the sync client and is received from the sync
  135. // engine.
  136. std::string cache_guid_;
  137. syncer::ModelErrorHandler error_handler_;
  138. std::unique_ptr<BookmarkModelObserverImpl> bookmark_model_observer_;
  139. // WeakPtrFactory for this processor for ModelTypeController.
  140. base::WeakPtrFactory<BookmarkModelTypeProcessor>
  141. weak_ptr_factory_for_controller_{this};
  142. // WeakPtrFactory for this processor which will be sent to sync thread.
  143. base::WeakPtrFactory<BookmarkModelTypeProcessor> weak_ptr_factory_for_worker_{
  144. this};
  145. };
  146. } // namespace sync_bookmarks
  147. #endif // COMPONENTS_SYNC_BOOKMARKS_BOOKMARK_MODEL_TYPE_PROCESSOR_H_