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 <map> |
9 #include <string> | 9 #include <string> |
10 | 10 |
11 #include "base/cancelable_callback.h" | 11 #include "base/cancelable_callback.h" |
12 #include "base/memory/linked_ptr.h" | 12 #include "base/memory/linked_ptr.h" |
13 #include "base/memory/weak_ptr.h" | 13 #include "base/memory/weak_ptr.h" |
14 #include "base/observer_list.h" | 14 #include "base/observer_list.h" |
15 #include "base/strings/string16.h" | 15 #include "base/strings/string16.h" |
16 #include "components/bookmarks/browser/base_bookmark_model_observer.h" | 16 #include "components/bookmarks/browser/base_bookmark_model_observer.h" |
17 #include "components/bookmarks/browser/bookmark_node.h" | 17 #include "components/bookmarks/browser/bookmark_node.h" |
18 #include "components/keyed_service/core/keyed_service.h" | 18 #include "components/keyed_service/core/keyed_service.h" |
19 | 19 |
20 class BookmarkNode; | |
21 class GURL; | 20 class GURL; |
22 | 21 |
23 namespace base { | 22 namespace base { |
24 class Time; | 23 class Time; |
25 } | 24 } |
26 | 25 |
27 namespace bookmarks { | 26 namespace bookmarks { |
28 class BookmarkModel; | 27 class BookmarkModel; |
| 28 class BookmarkNode; |
29 } | 29 } |
30 | 30 |
31 FORWARD_DECLARE_TEST(EnhancedBookmarkModelTest, SetMultipleMetaInfo); | 31 FORWARD_DECLARE_TEST(EnhancedBookmarkModelTest, SetMultipleMetaInfo); |
32 | 32 |
33 namespace enhanced_bookmarks { | 33 namespace enhanced_bookmarks { |
34 | 34 |
35 class EnhancedBookmarkModelObserver; | 35 class EnhancedBookmarkModelObserver; |
36 | 36 |
37 // Wrapper around BookmarkModel providing utility functions for enhanced | 37 // Wrapper around BookmarkModel providing utility functions for enhanced |
38 // bookmarks. | 38 // bookmarks. |
39 class EnhancedBookmarkModel : public KeyedService, | 39 class EnhancedBookmarkModel : public KeyedService, |
40 public bookmarks::BaseBookmarkModelObserver { | 40 public bookmarks::BaseBookmarkModelObserver { |
41 public: | 41 public: |
42 EnhancedBookmarkModel(bookmarks::BookmarkModel* bookmark_model, | 42 EnhancedBookmarkModel(bookmarks::BookmarkModel* bookmark_model, |
43 const std::string& version); | 43 const std::string& version); |
44 ~EnhancedBookmarkModel() override; | 44 ~EnhancedBookmarkModel() override; |
45 | 45 |
46 void Shutdown() override; | 46 void Shutdown() override; |
47 | 47 |
48 void AddObserver(EnhancedBookmarkModelObserver* observer); | 48 void AddObserver(EnhancedBookmarkModelObserver* observer); |
49 void RemoveObserver(EnhancedBookmarkModelObserver* observer); | 49 void RemoveObserver(EnhancedBookmarkModelObserver* observer); |
50 | 50 |
51 // Moves |node| to |new_parent| and inserts it at the given |index|. | 51 // Moves |node| to |new_parent| and inserts it at the given |index|. |
52 void Move(const BookmarkNode* node, | 52 void Move(const bookmarks::BookmarkNode* node, |
53 const BookmarkNode* new_parent, | 53 const bookmarks::BookmarkNode* new_parent, |
54 int index); | 54 int index); |
55 | 55 |
56 // Adds a new folder node at the specified position. | 56 // Adds a new folder node at the specified position. |
57 const BookmarkNode* AddFolder(const BookmarkNode* parent, | 57 const bookmarks::BookmarkNode* AddFolder( |
58 int index, | 58 const bookmarks::BookmarkNode* parent, |
59 const base::string16& title); | 59 int index, |
| 60 const base::string16& title); |
60 | 61 |
61 // Adds a url at the specified position. | 62 // Adds a url at the specified position. |
62 const BookmarkNode* AddURL(const BookmarkNode* parent, | 63 const bookmarks::BookmarkNode* AddURL(const bookmarks::BookmarkNode* parent, |
63 int index, | 64 int index, |
64 const base::string16& title, | 65 const base::string16& title, |
65 const GURL& url, | 66 const GURL& url, |
66 const base::Time& creation_time); | 67 const base::Time& creation_time); |
67 | 68 |
68 // Returns the remote id for a bookmark |node|. | 69 // Returns the remote id for a bookmark |node|. |
69 std::string GetRemoteId(const BookmarkNode* node); | 70 std::string GetRemoteId(const bookmarks::BookmarkNode* node); |
70 | 71 |
71 // Returns the bookmark node corresponding to the given |remote_id|, or NULL | 72 // Returns the bookmark node corresponding to the given |remote_id|, or NULL |
72 // if there is no node with the id. | 73 // if there is no node with the id. |
73 const BookmarkNode* BookmarkForRemoteId(const std::string& remote_id); | 74 const bookmarks::BookmarkNode* BookmarkForRemoteId( |
| 75 const std::string& remote_id); |
74 | 76 |
75 // Sets the description of a bookmark |node|. | 77 // Sets the description of a bookmark |node|. |
76 void SetDescription(const BookmarkNode* node, const std::string& description); | 78 void SetDescription(const bookmarks::BookmarkNode* node, |
| 79 const std::string& description); |
77 | 80 |
78 // Returns the description of a bookmark |node|. | 81 // Returns the description of a bookmark |node|. |
79 std::string GetDescription(const BookmarkNode* node); | 82 std::string GetDescription(const bookmarks::BookmarkNode* node); |
80 | 83 |
81 // Sets the URL of an image representative of a bookmark |node|. | 84 // Sets the URL of an image representative of a bookmark |node|. |
82 // Expects the URL to be valid and not empty. | 85 // Expects the URL to be valid and not empty. |
83 // Returns true if the metainfo is successfully populated. | 86 // Returns true if the metainfo is successfully populated. |
84 bool SetOriginalImage(const BookmarkNode* node, | 87 bool SetOriginalImage(const bookmarks::BookmarkNode* node, |
85 const GURL& url, | 88 const GURL& url, |
86 int width, | 89 int width, |
87 int height); | 90 int height); |
88 | 91 |
89 // Removes all image data for the node and sets the user_removed_image flag | 92 // Removes all image data for the node and sets the user_removed_image flag |
90 // so the server won't try to fetch a new image for the node. | 93 // so the server won't try to fetch a new image for the node. |
91 void RemoveImageData(const BookmarkNode* node); | 94 void RemoveImageData(const bookmarks::BookmarkNode* node); |
92 | 95 |
93 // Returns the url and dimensions of the original scraped image of a | 96 // Returns the url and dimensions of the original scraped image of a |
94 // bookmark |node|. | 97 // bookmark |node|. |
95 // Returns true if the out variables are populated, false otherwise. | 98 // Returns true if the out variables are populated, false otherwise. |
96 bool GetOriginalImage(const BookmarkNode* node, | 99 bool GetOriginalImage(const bookmarks::BookmarkNode* node, |
97 GURL* url, | 100 GURL* url, |
98 int* width, | 101 int* width, |
99 int* height); | 102 int* height); |
100 | 103 |
101 // Returns the url and dimensions of the server provided thumbnail image for | 104 // Returns the url and dimensions of the server provided thumbnail image for |
102 // a given bookmark |node|. | 105 // a given bookmark |node|. |
103 // Returns true if the out variables are populated, false otherwise. | 106 // Returns true if the out variables are populated, false otherwise. |
104 bool GetThumbnailImage(const BookmarkNode* node, | 107 bool GetThumbnailImage(const bookmarks::BookmarkNode* node, |
105 GURL* url, | 108 GURL* url, |
106 int* width, | 109 int* width, |
107 int* height); | 110 int* height); |
108 | 111 |
109 // Returns a brief server provided synopsis of the bookmarked page. | 112 // Returns a brief server provided synopsis of the bookmarked page. |
110 // Returns the empty string if the snippet could not be extracted. | 113 // Returns the empty string if the snippet could not be extracted. |
111 std::string GetSnippet(const BookmarkNode* node); | 114 std::string GetSnippet(const bookmarks::BookmarkNode* node); |
112 | 115 |
113 // Sets a custom suffix to be added to the version field when writing meta | 116 // Sets a custom suffix to be added to the version field when writing meta |
114 // info fields. | 117 // info fields. |
115 void SetVersionSuffix(const std::string& version_suffix); | 118 void SetVersionSuffix(const std::string& version_suffix); |
116 | 119 |
117 // TODO(rfevang): Add method + enum for accessing/writing flags. | 120 // TODO(rfevang): Add method + enum for accessing/writing flags. |
118 | 121 |
119 // Used for testing, simulates the process that creates the thumbnails. Will | 122 // Used for testing, simulates the process that creates the thumbnails. Will |
120 // remove existing entries for empty urls or set them if the url is not empty. | 123 // remove existing entries for empty urls or set them if the url is not empty. |
121 // Expects valid or empty urls. Returns true if the metainfo is successfully | 124 // Expects valid or empty urls. Returns true if the metainfo is successfully |
122 // populated. | 125 // populated. |
123 // TODO(rfevang): Move this to a testing only utility file. | 126 // TODO(rfevang): Move this to a testing only utility file. |
124 bool SetAllImages(const BookmarkNode* node, | 127 bool SetAllImages(const bookmarks::BookmarkNode* node, |
125 const GURL& image_url, | 128 const GURL& image_url, |
126 int image_width, | 129 int image_width, |
127 int image_height, | 130 int image_height, |
128 const GURL& thumbnail_url, | 131 const GURL& thumbnail_url, |
129 int thumbnail_width, | 132 int thumbnail_width, |
130 int thumbnail_height); | 133 int thumbnail_height); |
131 | 134 |
132 // TODO(rfevang): Ideally nothing should need the underlying bookmark model. | 135 // TODO(rfevang): Ideally nothing should need the underlying bookmark model. |
133 // Remove when that is actually the case. | 136 // Remove when that is actually the case. |
134 bookmarks::BookmarkModel* bookmark_model() { return bookmark_model_; } | 137 bookmarks::BookmarkModel* bookmark_model() { return bookmark_model_; } |
135 | 138 |
136 // Returns true if the enhanced bookmark model is done loading. | 139 // Returns true if the enhanced bookmark model is done loading. |
137 bool loaded() { return loaded_; } | 140 bool loaded() { return loaded_; } |
138 | 141 |
139 // Returns the version string to use when setting stars.version. | 142 // Returns the version string to use when setting stars.version. |
140 std::string GetVersionString(); | 143 std::string GetVersionString(); |
141 | 144 |
142 private: | 145 private: |
143 FRIEND_TEST_ALL_PREFIXES(::EnhancedBookmarkModelTest, SetMultipleMetaInfo); | 146 FRIEND_TEST_ALL_PREFIXES(::EnhancedBookmarkModelTest, SetMultipleMetaInfo); |
144 | 147 |
145 typedef std::map<std::string, const BookmarkNode*> IdToNodeMap; | 148 typedef std::map<std::string, const bookmarks::BookmarkNode*> IdToNodeMap; |
146 typedef std::map<const BookmarkNode*, std::string> NodeToIdMap; | 149 typedef std::map<const bookmarks::BookmarkNode*, std::string> NodeToIdMap; |
147 | 150 |
148 // bookmarks::BaseBookmarkModelObserver: | 151 // bookmarks::BaseBookmarkModelObserver: |
149 void BookmarkModelChanged() override; | 152 void BookmarkModelChanged() override; |
150 void BookmarkModelLoaded(bookmarks::BookmarkModel* model, | 153 void BookmarkModelLoaded(bookmarks::BookmarkModel* model, |
151 bool ids_reassigned) override; | 154 bool ids_reassigned) override; |
152 void BookmarkNodeAdded(bookmarks::BookmarkModel* model, | 155 void BookmarkNodeAdded(bookmarks::BookmarkModel* model, |
153 const BookmarkNode* parent, | 156 const bookmarks::BookmarkNode* parent, |
154 int index) override; | 157 int index) override; |
155 void BookmarkNodeRemoved(bookmarks::BookmarkModel* model, | 158 void BookmarkNodeRemoved(bookmarks::BookmarkModel* model, |
156 const BookmarkNode* parent, | 159 const bookmarks::BookmarkNode* parent, |
157 int old_index, | 160 int old_index, |
158 const BookmarkNode* node, | 161 const bookmarks::BookmarkNode* node, |
159 const std::set<GURL>& removed_urls) override; | 162 const std::set<GURL>& removed_urls) override; |
160 void BookmarkNodeChanged(bookmarks::BookmarkModel* model, | 163 void BookmarkNodeChanged(bookmarks::BookmarkModel* model, |
161 const BookmarkNode* node) override; | 164 const bookmarks::BookmarkNode* node) override; |
162 void OnWillChangeBookmarkMetaInfo(bookmarks::BookmarkModel* model, | 165 void OnWillChangeBookmarkMetaInfo( |
163 const BookmarkNode* node) override; | 166 bookmarks::BookmarkModel* model, |
| 167 const bookmarks::BookmarkNode* node) override; |
164 void BookmarkMetaInfoChanged(bookmarks::BookmarkModel* model, | 168 void BookmarkMetaInfoChanged(bookmarks::BookmarkModel* model, |
165 const BookmarkNode* node) override; | 169 const bookmarks::BookmarkNode* node) override; |
166 void BookmarkAllUserNodesRemoved(bookmarks::BookmarkModel* model, | 170 void BookmarkAllUserNodesRemoved(bookmarks::BookmarkModel* model, |
167 const std::set<GURL>& removed_urls) override; | 171 const std::set<GURL>& removed_urls) override; |
168 | 172 |
169 // Initialize the mapping from remote ids to nodes. | 173 // Initialize the mapping from remote ids to nodes. |
170 void InitializeIdMap(); | 174 void InitializeIdMap(); |
171 | 175 |
172 // Adds a node to the id map if it has a (unique) remote id. Must be followed | 176 // Adds a node to the id map if it has a (unique) remote id. Must be followed |
173 // by a (Schedule)ResetDuplicateRemoteIds call when done adding nodes. | 177 // by a (Schedule)ResetDuplicateRemoteIds call when done adding nodes. |
174 void AddToIdMap(const BookmarkNode* node); | 178 void AddToIdMap(const bookmarks::BookmarkNode* node); |
175 | 179 |
176 // Recursively removes a node and all its children from the various maps. | 180 // Recursively removes a node and all its children from the various maps. |
177 void RemoveNodeFromMaps(const BookmarkNode* node); | 181 void RemoveNodeFromMaps(const bookmarks::BookmarkNode* node); |
178 | 182 |
179 // If there are nodes that needs to reset their remote ids, schedules | 183 // If there are nodes that needs to reset their remote ids, schedules |
180 // ResetDuplicateRemoteIds to be run asynchronously. | 184 // ResetDuplicateRemoteIds to be run asynchronously. |
181 void ScheduleResetDuplicateRemoteIds(); | 185 void ScheduleResetDuplicateRemoteIds(); |
182 | 186 |
183 // Clears out any duplicate remote ids detected by AddToIdMap calls. | 187 // Clears out any duplicate remote ids detected by AddToIdMap calls. |
184 void ResetDuplicateRemoteIds(); | 188 void ResetDuplicateRemoteIds(); |
185 | 189 |
186 // Sets the NEEDS_OFFLINE_PROCESSING flag on the given node. | 190 // Sets the NEEDS_OFFLINE_PROCESSING flag on the given node. |
187 void SetNeedsOfflineProcessing(const BookmarkNode* node); | 191 void SetNeedsOfflineProcessing(const bookmarks::BookmarkNode* node); |
188 | 192 |
189 // Helper method for setting a meta info field on a node. Also updates the | 193 // Helper method for setting a meta info field on a node. Also updates the |
190 // version field. | 194 // version field. |
191 void SetMetaInfo(const BookmarkNode* node, | 195 void SetMetaInfo(const bookmarks::BookmarkNode* node, |
192 const std::string& field, | 196 const std::string& field, |
193 const std::string& value); | 197 const std::string& value); |
194 | 198 |
195 // Helper method for setting multiple meta info fields at once. All the fields | 199 // Helper method for setting multiple meta info fields at once. All the fields |
196 // in |meta_info| will be set, but the method will not delete fields not | 200 // in |meta_info| will be set, but the method will not delete fields not |
197 // present. | 201 // present. |
198 void SetMultipleMetaInfo(const BookmarkNode* node, | 202 void SetMultipleMetaInfo(const bookmarks::BookmarkNode* node, |
199 BookmarkNode::MetaInfoMap meta_info); | 203 bookmarks::BookmarkNode::MetaInfoMap meta_info); |
200 | 204 |
201 bookmarks::BookmarkModel* bookmark_model_; | 205 bookmarks::BookmarkModel* bookmark_model_; |
202 bool loaded_; | 206 bool loaded_; |
203 | 207 |
204 ObserverList<EnhancedBookmarkModelObserver> observers_; | 208 ObserverList<EnhancedBookmarkModelObserver> observers_; |
205 | 209 |
206 IdToNodeMap id_map_; | 210 IdToNodeMap id_map_; |
207 NodeToIdMap nodes_to_reset_; | 211 NodeToIdMap nodes_to_reset_; |
208 | 212 |
209 // Pending SetNeedsOfflineProcessing calls are stored here, as they may need | 213 // Pending SetNeedsOfflineProcessing calls are stored here, as they may need |
210 // to be cancelled if the node is removed. | 214 // to be cancelled if the node is removed. |
211 std::map<const BookmarkNode*, linked_ptr<base::CancelableClosure>> | 215 std::map<const bookmarks::BookmarkNode*, linked_ptr<base::CancelableClosure>> |
212 set_needs_offline_processing_tasks_; | 216 set_needs_offline_processing_tasks_; |
213 | 217 |
214 // Caches the remote id of a node before its meta info changes. | 218 // Caches the remote id of a node before its meta info changes. |
215 std::string prev_remote_id_; | 219 std::string prev_remote_id_; |
216 | 220 |
217 std::string version_; | 221 std::string version_; |
218 std::string version_suffix_; | 222 std::string version_suffix_; |
219 | 223 |
220 base::WeakPtrFactory<EnhancedBookmarkModel> weak_ptr_factory_; | 224 base::WeakPtrFactory<EnhancedBookmarkModel> weak_ptr_factory_; |
221 }; | 225 }; |
222 | 226 |
223 } // namespace enhanced_bookmarks | 227 } // namespace enhanced_bookmarks |
224 | 228 |
225 #endif // COMPONENTS_ENHANCED_BOOKMARKS_ENHANCED_BOOKMARK_MODEL_H_ | 229 #endif // COMPONENTS_ENHANCED_BOOKMARKS_ENHANCED_BOOKMARK_MODEL_H_ |
OLD | NEW |