Index: components/enhanced_bookmarks/enhanced_bookmark_model.h |
diff --git a/components/enhanced_bookmarks/enhanced_bookmark_model.h b/components/enhanced_bookmarks/enhanced_bookmark_model.h |
index d89f07f78960f9300b600a5ca1a0d5296c9978af..1a661c055c62a8feed3c9676034176a1685702df 100644 |
--- a/components/enhanced_bookmarks/enhanced_bookmark_model.h |
+++ b/components/enhanced_bookmarks/enhanced_bookmark_model.h |
@@ -5,9 +5,13 @@ |
#ifndef COMPONENTS_ENHANCED_BOOKMARKS_ENHANCED_BOOKMARK_MODEL_H_ |
#define COMPONENTS_ENHANCED_BOOKMARKS_ENHANCED_BOOKMARK_MODEL_H_ |
+#include <map> |
#include <string> |
+#include "base/memory/weak_ptr.h" |
+#include "base/observer_list.h" |
#include "base/strings/string16.h" |
+#include "components/bookmarks/browser/base_bookmark_model_observer.h" |
#include "components/bookmarks/browser/bookmark_node.h" |
#include "components/keyed_service/core/keyed_service.h" |
@@ -16,17 +20,29 @@ class Time; |
} // namespace base |
class BookmarkModel; |
+class BookmarkNode; |
class GURL; |
+FORWARD_DECLARE_TEST(EnhancedBookmarkModelTest, SetMultipleMetaInfo); |
+ |
namespace enhanced_bookmarks { |
+ |
+class EnhancedBookmarkModelObserver; |
+ |
// Wrapper around BookmarkModel providing utility functions for enhanced |
// bookmarks. |
-class EnhancedBookmarkModel : public KeyedService { |
+class EnhancedBookmarkModel : public KeyedService, |
+ public BaseBookmarkModelObserver { |
public: |
EnhancedBookmarkModel(BookmarkModel* bookmark_model, |
const std::string& version); |
virtual ~EnhancedBookmarkModel(); |
+ virtual void ShutDown(); |
+ |
+ void AddObserver(EnhancedBookmarkModelObserver* observer); |
+ void RemoveObserver(EnhancedBookmarkModelObserver* observer); |
+ |
// Moves |node| to |new_parent| and inserts it at the given |index|. |
void Move(const BookmarkNode* node, |
const BookmarkNode* new_parent, |
@@ -47,6 +63,10 @@ class EnhancedBookmarkModel : public KeyedService { |
// Returns the remote id for a bookmark |node|. |
std::string GetRemoteId(const BookmarkNode* node); |
+ // Returns the bookmark node corresponding to the given |remote_id|, or NULL |
+ // if there is no node with the id. |
+ const BookmarkNode* BookmarkForRemoteId(const std::string& remote_id); |
+ |
// Sets the description of a bookmark |node|. |
void SetDescription(const BookmarkNode* node, const std::string& description); |
@@ -104,22 +124,77 @@ class EnhancedBookmarkModel : public KeyedService { |
// Remove when that is actually the case. |
BookmarkModel* bookmark_model() { return bookmark_model_; } |
+ // Returns true if the enhanced bookmark model is done loading. |
+ bool loaded() { return loaded_; } |
+ |
private: |
- // Generates and sets a remote id for the given bookmark |node|. |
- // Returns the id set. |
- std::string SetRemoteId(const BookmarkNode* node); |
+ FRIEND_TEST_ALL_PREFIXES(::EnhancedBookmarkModelTest, SetMultipleMetaInfo); |
+ |
+ typedef std::map<std::string, const BookmarkNode*> IdToNodeMap; |
+ typedef std::map<const BookmarkNode*, std::string> NodeToIdMap; |
+ |
+ // BaseBookmarkModelObserver: |
+ virtual void BookmarkModelChanged() OVERRIDE; |
+ virtual void BookmarkModelLoaded(BookmarkModel* model, |
+ bool ids_reassigned) OVERRIDE; |
+ virtual void BookmarkNodeAdded(BookmarkModel* model, |
+ const BookmarkNode* parent, |
+ int index) OVERRIDE; |
+ virtual void BookmarkNodeRemoved(BookmarkModel* model, |
+ const BookmarkNode* parent, |
+ int old_index, |
+ const BookmarkNode* node, |
+ const std::set<GURL>& removed_urls) OVERRIDE; |
+ virtual void OnWillChangeBookmarkMetaInfo(BookmarkModel* model, |
+ const BookmarkNode* node) OVERRIDE; |
+ virtual void BookmarkMetaInfoChanged(BookmarkModel* model, |
+ const BookmarkNode* node) OVERRIDE; |
+ virtual void BookmarkAllUserNodesRemoved( |
+ BookmarkModel* model, |
+ const std::set<GURL>& removed_urls) OVERRIDE; |
+ |
+ // Initialize the mapping from remote ids to nodes. |
+ void InitializeIdMap(); |
+ |
+ // Adds a node to the id map if it has a (unique) remote id. Must be followed |
+ // by a (Schedule)ResetDuplicateRemoteIds call when done adding nodes. |
+ void AddToIdMap(const BookmarkNode* node); |
+ |
+ // If there are nodes that needs to reset their remote ids, schedules |
+ // ResetDuplicateRemoteIds to be run asynchronously. |
+ void ScheduleResetDuplicateRemoteIds(); |
+ |
+ // Clears out any duplicate remote ids detected by AddToIdMap calls. |
+ void ResetDuplicateRemoteIds(); |
// Helper method for setting a meta info field on a node. Also updates the |
- // version and userEdits fields. |
+ // version field. |
void SetMetaInfo(const BookmarkNode* node, |
const std::string& field, |
- const std::string& value, |
- bool user_edit); |
+ const std::string& value); |
+ |
+ // Helper method for setting multiple meta info fields at once. All the fields |
+ // in |meta_info| will be set, but the method will not delete fields not |
+ // present. |
+ void SetMultipleMetaInfo(const BookmarkNode* node, |
+ BookmarkNode::MetaInfoMap meta_info); |
// Returns the version string to use when setting stars.version. |
std::string GetVersionString(); |
BookmarkModel* bookmark_model_; |
+ bool loaded_; |
+ |
+ ObserverList<EnhancedBookmarkModelObserver> observers_; |
+ |
+ base::WeakPtrFactory<EnhancedBookmarkModel> weak_ptr_factory_; |
+ |
+ IdToNodeMap id_map_; |
+ NodeToIdMap nodes_to_reset_; |
+ |
+ // Caches the remote id of a node before its meta info changes. |
+ std::string prev_remote_id_; |
+ |
std::string version_; |
std::string version_suffix_; |
}; |