OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef COMPONENTS_ENHANCED_BOOKMARKS_ENHANCED_BOOKMARK_MODEL_H_ | 5 #ifndef COMPONENTS_ENHANCED_BOOKMARKS_ENHANCED_BOOKMARK_MODEL_H_ |
6 #define COMPONENTS_ENHANCED_BOOKMARKS_ENHANCED_BOOKMARK_MODEL_H_ | 6 #define COMPONENTS_ENHANCED_BOOKMARKS_ENHANCED_BOOKMARK_MODEL_H_ |
7 | 7 |
| 8 #include <map> |
8 #include <string> | 9 #include <string> |
9 | 10 |
| 11 #include "base/memory/weak_ptr.h" |
| 12 #include "base/observer_list.h" |
10 #include "base/strings/string16.h" | 13 #include "base/strings/string16.h" |
| 14 #include "components/bookmarks/browser/base_bookmark_model_observer.h" |
11 #include "components/bookmarks/browser/bookmark_node.h" | 15 #include "components/bookmarks/browser/bookmark_node.h" |
12 #include "components/keyed_service/core/keyed_service.h" | 16 #include "components/keyed_service/core/keyed_service.h" |
13 | 17 |
14 namespace base { | 18 namespace base { |
15 class Time; | 19 class Time; |
16 } // namespace base | 20 } // namespace base |
17 | 21 |
18 class BookmarkModel; | 22 class BookmarkModel; |
| 23 class BookmarkNode; |
19 class GURL; | 24 class GURL; |
20 | 25 |
| 26 FORWARD_DECLARE_TEST(EnhancedBookmarkModelTest, SetMultipleMetaInfo); |
| 27 |
21 namespace enhanced_bookmarks { | 28 namespace enhanced_bookmarks { |
| 29 |
| 30 class EnhancedBookmarkModelObserver; |
| 31 |
22 // Wrapper around BookmarkModel providing utility functions for enhanced | 32 // Wrapper around BookmarkModel providing utility functions for enhanced |
23 // bookmarks. | 33 // bookmarks. |
24 class EnhancedBookmarkModel : public KeyedService { | 34 class EnhancedBookmarkModel : public KeyedService, |
| 35 public BaseBookmarkModelObserver { |
25 public: | 36 public: |
26 EnhancedBookmarkModel(BookmarkModel* bookmark_model, | 37 EnhancedBookmarkModel(BookmarkModel* bookmark_model, |
27 const std::string& version); | 38 const std::string& version); |
28 virtual ~EnhancedBookmarkModel(); | 39 virtual ~EnhancedBookmarkModel(); |
29 | 40 |
| 41 virtual void ShutDown(); |
| 42 |
| 43 void AddObserver(EnhancedBookmarkModelObserver* observer); |
| 44 void RemoveObserver(EnhancedBookmarkModelObserver* observer); |
| 45 |
30 // Moves |node| to |new_parent| and inserts it at the given |index|. | 46 // Moves |node| to |new_parent| and inserts it at the given |index|. |
31 void Move(const BookmarkNode* node, | 47 void Move(const BookmarkNode* node, |
32 const BookmarkNode* new_parent, | 48 const BookmarkNode* new_parent, |
33 int index); | 49 int index); |
34 | 50 |
35 // Adds a new folder node at the specified position. | 51 // Adds a new folder node at the specified position. |
36 const BookmarkNode* AddFolder(const BookmarkNode* parent, | 52 const BookmarkNode* AddFolder(const BookmarkNode* parent, |
37 int index, | 53 int index, |
38 const base::string16& title); | 54 const base::string16& title); |
39 | 55 |
40 // Adds a url at the specified position. | 56 // Adds a url at the specified position. |
41 const BookmarkNode* AddURL(const BookmarkNode* parent, | 57 const BookmarkNode* AddURL(const BookmarkNode* parent, |
42 int index, | 58 int index, |
43 const base::string16& title, | 59 const base::string16& title, |
44 const GURL& url, | 60 const GURL& url, |
45 const base::Time& creation_time); | 61 const base::Time& creation_time); |
46 | 62 |
47 // Returns the remote id for a bookmark |node|. | 63 // Returns the remote id for a bookmark |node|. |
48 std::string GetRemoteId(const BookmarkNode* node); | 64 std::string GetRemoteId(const BookmarkNode* node); |
49 | 65 |
| 66 // Returns the bookmark node corresponding to the given |remote_id|, or NULL |
| 67 // if there is no node with the id. |
| 68 const BookmarkNode* BookmarkForRemoteId(const std::string& remote_id); |
| 69 |
50 // Sets the description of a bookmark |node|. | 70 // Sets the description of a bookmark |node|. |
51 void SetDescription(const BookmarkNode* node, const std::string& description); | 71 void SetDescription(const BookmarkNode* node, const std::string& description); |
52 | 72 |
53 // Returns the description of a bookmark |node|. | 73 // Returns the description of a bookmark |node|. |
54 std::string GetDescription(const BookmarkNode* node); | 74 std::string GetDescription(const BookmarkNode* node); |
55 | 75 |
56 // Sets the URL of an image representative of a bookmark |node|. | 76 // Sets the URL of an image representative of a bookmark |node|. |
57 // Expects the URL to be valid and not empty. | 77 // Expects the URL to be valid and not empty. |
58 // Returns true if the metainfo is successfully populated. | 78 // Returns true if the metainfo is successfully populated. |
59 bool SetOriginalImage(const BookmarkNode* node, | 79 bool SetOriginalImage(const BookmarkNode* node, |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
97 int image_width, | 117 int image_width, |
98 int image_height, | 118 int image_height, |
99 const GURL& thumbnail_url, | 119 const GURL& thumbnail_url, |
100 int thumbnail_width, | 120 int thumbnail_width, |
101 int thumbnail_height); | 121 int thumbnail_height); |
102 | 122 |
103 // TODO(rfevang): Ideally nothing should need the underlying bookmark model. | 123 // TODO(rfevang): Ideally nothing should need the underlying bookmark model. |
104 // Remove when that is actually the case. | 124 // Remove when that is actually the case. |
105 BookmarkModel* bookmark_model() { return bookmark_model_; } | 125 BookmarkModel* bookmark_model() { return bookmark_model_; } |
106 | 126 |
| 127 // Returns true if the enhanced bookmark model is done loading. |
| 128 bool loaded() { return loaded_; } |
| 129 |
107 private: | 130 private: |
108 // Generates and sets a remote id for the given bookmark |node|. | 131 FRIEND_TEST_ALL_PREFIXES(::EnhancedBookmarkModelTest, SetMultipleMetaInfo); |
109 // Returns the id set. | 132 |
110 std::string SetRemoteId(const BookmarkNode* node); | 133 typedef std::map<std::string, const BookmarkNode*> IdToNodeMap; |
| 134 typedef std::map<const BookmarkNode*, std::string> NodeToIdMap; |
| 135 |
| 136 // BaseBookmarkModelObserver: |
| 137 virtual void BookmarkModelChanged() OVERRIDE; |
| 138 virtual void BookmarkModelLoaded(BookmarkModel* model, |
| 139 bool ids_reassigned) OVERRIDE; |
| 140 virtual void BookmarkNodeAdded(BookmarkModel* model, |
| 141 const BookmarkNode* parent, |
| 142 int index) OVERRIDE; |
| 143 virtual void BookmarkNodeRemoved(BookmarkModel* model, |
| 144 const BookmarkNode* parent, |
| 145 int old_index, |
| 146 const BookmarkNode* node, |
| 147 const std::set<GURL>& removed_urls) OVERRIDE; |
| 148 virtual void OnWillChangeBookmarkMetaInfo(BookmarkModel* model, |
| 149 const BookmarkNode* node) OVERRIDE; |
| 150 virtual void BookmarkMetaInfoChanged(BookmarkModel* model, |
| 151 const BookmarkNode* node) OVERRIDE; |
| 152 virtual void BookmarkAllUserNodesRemoved( |
| 153 BookmarkModel* model, |
| 154 const std::set<GURL>& removed_urls) OVERRIDE; |
| 155 |
| 156 // Initialize the mapping from remote ids to nodes. |
| 157 void InitializeIdMap(); |
| 158 |
| 159 // Adds a node to the id map if it has a (unique) remote id. Must be followed |
| 160 // by a (Schedule)ResetDuplicateRemoteIds call when done adding nodes. |
| 161 void AddToIdMap(const BookmarkNode* node); |
| 162 |
| 163 // If there are nodes that needs to reset their remote ids, schedules |
| 164 // ResetDuplicateRemoteIds to be run asynchronously. |
| 165 void ScheduleResetDuplicateRemoteIds(); |
| 166 |
| 167 // Clears out any duplicate remote ids detected by AddToIdMap calls. |
| 168 void ResetDuplicateRemoteIds(); |
111 | 169 |
112 // Helper method for setting a meta info field on a node. Also updates the | 170 // Helper method for setting a meta info field on a node. Also updates the |
113 // version and userEdits fields. | 171 // version field. |
114 void SetMetaInfo(const BookmarkNode* node, | 172 void SetMetaInfo(const BookmarkNode* node, |
115 const std::string& field, | 173 const std::string& field, |
116 const std::string& value, | 174 const std::string& value); |
117 bool user_edit); | 175 |
| 176 // Helper method for setting multiple meta info fields at once. All the fields |
| 177 // in |meta_info| will be set, but the method will not delete fields not |
| 178 // present. |
| 179 void SetMultipleMetaInfo(const BookmarkNode* node, |
| 180 BookmarkNode::MetaInfoMap meta_info); |
118 | 181 |
119 // Returns the version string to use when setting stars.version. | 182 // Returns the version string to use when setting stars.version. |
120 std::string GetVersionString(); | 183 std::string GetVersionString(); |
121 | 184 |
122 BookmarkModel* bookmark_model_; | 185 BookmarkModel* bookmark_model_; |
| 186 bool loaded_; |
| 187 |
| 188 ObserverList<EnhancedBookmarkModelObserver> observers_; |
| 189 |
| 190 base::WeakPtrFactory<EnhancedBookmarkModel> weak_ptr_factory_; |
| 191 |
| 192 IdToNodeMap id_map_; |
| 193 NodeToIdMap nodes_to_reset_; |
| 194 |
| 195 // Caches the remote id of a node before its meta info changes. |
| 196 std::string prev_remote_id_; |
| 197 |
123 std::string version_; | 198 std::string version_; |
124 std::string version_suffix_; | 199 std::string version_suffix_; |
125 }; | 200 }; |
126 | 201 |
127 } // namespace enhanced_bookmarks | 202 } // namespace enhanced_bookmarks |
128 | 203 |
129 #endif // COMPONENTS_ENHANCED_BOOKMARKS_ENHANCED_BOOKMARK_MODEL_H_ | 204 #endif // COMPONENTS_ENHANCED_BOOKMARKS_ENHANCED_BOOKMARK_MODEL_H_ |
OLD | NEW |