| 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 // Returns the url and dimensions of the original scraped image of a | 92 // Returns the url and dimensions of the original scraped image of a |
| 90 // bookmark |node|. | 93 // bookmark |node|. |
| 91 // Returns true if the out variables are populated, false otherwise. | 94 // Returns true if the out variables are populated, false otherwise. |
| 92 bool GetOriginalImage(const BookmarkNode* node, | 95 bool GetOriginalImage(const bookmarks::BookmarkNode* node, |
| 93 GURL* url, | 96 GURL* url, |
| 94 int* width, | 97 int* width, |
| 95 int* height); | 98 int* height); |
| 96 | 99 |
| 97 // Returns the url and dimensions of the server provided thumbnail image for | 100 // Returns the url and dimensions of the server provided thumbnail image for |
| 98 // a given bookmark |node|. | 101 // a given bookmark |node|. |
| 99 // Returns true if the out variables are populated, false otherwise. | 102 // Returns true if the out variables are populated, false otherwise. |
| 100 bool GetThumbnailImage(const BookmarkNode* node, | 103 bool GetThumbnailImage(const bookmarks::BookmarkNode* node, |
| 101 GURL* url, | 104 GURL* url, |
| 102 int* width, | 105 int* width, |
| 103 int* height); | 106 int* height); |
| 104 | 107 |
| 105 // Returns a brief server provided synopsis of the bookmarked page. | 108 // Returns a brief server provided synopsis of the bookmarked page. |
| 106 // Returns the empty string if the snippet could not be extracted. | 109 // Returns the empty string if the snippet could not be extracted. |
| 107 std::string GetSnippet(const BookmarkNode* node); | 110 std::string GetSnippet(const bookmarks::BookmarkNode* node); |
| 108 | 111 |
| 109 // Sets a custom suffix to be added to the version field when writing meta | 112 // Sets a custom suffix to be added to the version field when writing meta |
| 110 // info fields. | 113 // info fields. |
| 111 void SetVersionSuffix(const std::string& version_suffix); | 114 void SetVersionSuffix(const std::string& version_suffix); |
| 112 | 115 |
| 113 // TODO(rfevang): Add method + enum for accessing/writing flags. | 116 // TODO(rfevang): Add method + enum for accessing/writing flags. |
| 114 | 117 |
| 115 // Used for testing, simulates the process that creates the thumbnails. Will | 118 // Used for testing, simulates the process that creates the thumbnails. Will |
| 116 // remove existing entries for empty urls or set them if the url is not empty. | 119 // remove existing entries for empty urls or set them if the url is not empty. |
| 117 // Expects valid or empty urls. Returns true if the metainfo is successfully | 120 // Expects valid or empty urls. Returns true if the metainfo is successfully |
| 118 // populated. | 121 // populated. |
| 119 // TODO(rfevang): Move this to a testing only utility file. | 122 // TODO(rfevang): Move this to a testing only utility file. |
| 120 bool SetAllImages(const BookmarkNode* node, | 123 bool SetAllImages(const bookmarks::BookmarkNode* node, |
| 121 const GURL& image_url, | 124 const GURL& image_url, |
| 122 int image_width, | 125 int image_width, |
| 123 int image_height, | 126 int image_height, |
| 124 const GURL& thumbnail_url, | 127 const GURL& thumbnail_url, |
| 125 int thumbnail_width, | 128 int thumbnail_width, |
| 126 int thumbnail_height); | 129 int thumbnail_height); |
| 127 | 130 |
| 128 // TODO(rfevang): Ideally nothing should need the underlying bookmark model. | 131 // TODO(rfevang): Ideally nothing should need the underlying bookmark model. |
| 129 // Remove when that is actually the case. | 132 // Remove when that is actually the case. |
| 130 bookmarks::BookmarkModel* bookmark_model() { return bookmark_model_; } | 133 bookmarks::BookmarkModel* bookmark_model() { return bookmark_model_; } |
| 131 | 134 |
| 132 // Returns true if the enhanced bookmark model is done loading. | 135 // Returns true if the enhanced bookmark model is done loading. |
| 133 bool loaded() { return loaded_; } | 136 bool loaded() { return loaded_; } |
| 134 | 137 |
| 135 // Returns the version string to use when setting stars.version. | 138 // Returns the version string to use when setting stars.version. |
| 136 std::string GetVersionString(); | 139 std::string GetVersionString(); |
| 137 | 140 |
| 138 private: | 141 private: |
| 139 FRIEND_TEST_ALL_PREFIXES(::EnhancedBookmarkModelTest, SetMultipleMetaInfo); | 142 FRIEND_TEST_ALL_PREFIXES(::EnhancedBookmarkModelTest, SetMultipleMetaInfo); |
| 140 | 143 |
| 141 typedef std::map<std::string, const BookmarkNode*> IdToNodeMap; | 144 typedef std::map<std::string, const bookmarks::BookmarkNode*> IdToNodeMap; |
| 142 typedef std::map<const BookmarkNode*, std::string> NodeToIdMap; | 145 typedef std::map<const bookmarks::BookmarkNode*, std::string> NodeToIdMap; |
| 143 | 146 |
| 144 // bookmarks::BaseBookmarkModelObserver: | 147 // bookmarks::BaseBookmarkModelObserver: |
| 145 void BookmarkModelChanged() override; | 148 void BookmarkModelChanged() override; |
| 146 void BookmarkModelLoaded(bookmarks::BookmarkModel* model, | 149 void BookmarkModelLoaded(bookmarks::BookmarkModel* model, |
| 147 bool ids_reassigned) override; | 150 bool ids_reassigned) override; |
| 148 void BookmarkNodeAdded(bookmarks::BookmarkModel* model, | 151 void BookmarkNodeAdded(bookmarks::BookmarkModel* model, |
| 149 const BookmarkNode* parent, | 152 const bookmarks::BookmarkNode* parent, |
| 150 int index) override; | 153 int index) override; |
| 151 void BookmarkNodeRemoved(bookmarks::BookmarkModel* model, | 154 void BookmarkNodeRemoved(bookmarks::BookmarkModel* model, |
| 152 const BookmarkNode* parent, | 155 const bookmarks::BookmarkNode* parent, |
| 153 int old_index, | 156 int old_index, |
| 154 const BookmarkNode* node, | 157 const bookmarks::BookmarkNode* node, |
| 155 const std::set<GURL>& removed_urls) override; | 158 const std::set<GURL>& removed_urls) override; |
| 156 void BookmarkNodeChanged(bookmarks::BookmarkModel* model, | 159 void BookmarkNodeChanged(bookmarks::BookmarkModel* model, |
| 157 const BookmarkNode* node) override; | 160 const bookmarks::BookmarkNode* node) override; |
| 158 void OnWillChangeBookmarkMetaInfo(bookmarks::BookmarkModel* model, | 161 void OnWillChangeBookmarkMetaInfo( |
| 159 const BookmarkNode* node) override; | 162 bookmarks::BookmarkModel* model, |
| 163 const bookmarks::BookmarkNode* node) override; |
| 160 void BookmarkMetaInfoChanged(bookmarks::BookmarkModel* model, | 164 void BookmarkMetaInfoChanged(bookmarks::BookmarkModel* model, |
| 161 const BookmarkNode* node) override; | 165 const bookmarks::BookmarkNode* node) override; |
| 162 void BookmarkAllUserNodesRemoved(bookmarks::BookmarkModel* model, | 166 void BookmarkAllUserNodesRemoved(bookmarks::BookmarkModel* model, |
| 163 const std::set<GURL>& removed_urls) override; | 167 const std::set<GURL>& removed_urls) override; |
| 164 | 168 |
| 165 // Initialize the mapping from remote ids to nodes. | 169 // Initialize the mapping from remote ids to nodes. |
| 166 void InitializeIdMap(); | 170 void InitializeIdMap(); |
| 167 | 171 |
| 168 // Adds a node to the id map if it has a (unique) remote id. Must be followed | 172 // Adds a node to the id map if it has a (unique) remote id. Must be followed |
| 169 // by a (Schedule)ResetDuplicateRemoteIds call when done adding nodes. | 173 // by a (Schedule)ResetDuplicateRemoteIds call when done adding nodes. |
| 170 void AddToIdMap(const BookmarkNode* node); | 174 void AddToIdMap(const bookmarks::BookmarkNode* node); |
| 171 | 175 |
| 172 // Recursively removes a node and all its children from the various maps. | 176 // Recursively removes a node and all its children from the various maps. |
| 173 void RemoveNodeFromMaps(const BookmarkNode* node); | 177 void RemoveNodeFromMaps(const bookmarks::BookmarkNode* node); |
| 174 | 178 |
| 175 // If there are nodes that needs to reset their remote ids, schedules | 179 // If there are nodes that needs to reset their remote ids, schedules |
| 176 // ResetDuplicateRemoteIds to be run asynchronously. | 180 // ResetDuplicateRemoteIds to be run asynchronously. |
| 177 void ScheduleResetDuplicateRemoteIds(); | 181 void ScheduleResetDuplicateRemoteIds(); |
| 178 | 182 |
| 179 // Clears out any duplicate remote ids detected by AddToIdMap calls. | 183 // Clears out any duplicate remote ids detected by AddToIdMap calls. |
| 180 void ResetDuplicateRemoteIds(); | 184 void ResetDuplicateRemoteIds(); |
| 181 | 185 |
| 182 // Sets the NEEDS_OFFLINE_PROCESSING flag on the given node. | 186 // Sets the NEEDS_OFFLINE_PROCESSING flag on the given node. |
| 183 void SetNeedsOfflineProcessing(const BookmarkNode* node); | 187 void SetNeedsOfflineProcessing(const bookmarks::BookmarkNode* node); |
| 184 | 188 |
| 185 // Helper method for setting a meta info field on a node. Also updates the | 189 // Helper method for setting a meta info field on a node. Also updates the |
| 186 // version field. | 190 // version field. |
| 187 void SetMetaInfo(const BookmarkNode* node, | 191 void SetMetaInfo(const bookmarks::BookmarkNode* node, |
| 188 const std::string& field, | 192 const std::string& field, |
| 189 const std::string& value); | 193 const std::string& value); |
| 190 | 194 |
| 191 // Helper method for setting multiple meta info fields at once. All the fields | 195 // Helper method for setting multiple meta info fields at once. All the fields |
| 192 // in |meta_info| will be set, but the method will not delete fields not | 196 // in |meta_info| will be set, but the method will not delete fields not |
| 193 // present. | 197 // present. |
| 194 void SetMultipleMetaInfo(const BookmarkNode* node, | 198 void SetMultipleMetaInfo(const bookmarks::BookmarkNode* node, |
| 195 BookmarkNode::MetaInfoMap meta_info); | 199 bookmarks::BookmarkNode::MetaInfoMap meta_info); |
| 196 | 200 |
| 197 bookmarks::BookmarkModel* bookmark_model_; | 201 bookmarks::BookmarkModel* bookmark_model_; |
| 198 bool loaded_; | 202 bool loaded_; |
| 199 | 203 |
| 200 ObserverList<EnhancedBookmarkModelObserver> observers_; | 204 ObserverList<EnhancedBookmarkModelObserver> observers_; |
| 201 | 205 |
| 202 IdToNodeMap id_map_; | 206 IdToNodeMap id_map_; |
| 203 NodeToIdMap nodes_to_reset_; | 207 NodeToIdMap nodes_to_reset_; |
| 204 | 208 |
| 205 // Pending SetNeedsOfflineProcessing calls are stored here, as they may need | 209 // Pending SetNeedsOfflineProcessing calls are stored here, as they may need |
| 206 // to be cancelled if the node is removed. | 210 // to be cancelled if the node is removed. |
| 207 std::map<const BookmarkNode*, linked_ptr<base::CancelableClosure>> | 211 std::map<const bookmarks::BookmarkNode*, linked_ptr<base::CancelableClosure>> |
| 208 set_needs_offline_processing_tasks_; | 212 set_needs_offline_processing_tasks_; |
| 209 | 213 |
| 210 // Caches the remote id of a node before its meta info changes. | 214 // Caches the remote id of a node before its meta info changes. |
| 211 std::string prev_remote_id_; | 215 std::string prev_remote_id_; |
| 212 | 216 |
| 213 std::string version_; | 217 std::string version_; |
| 214 std::string version_suffix_; | 218 std::string version_suffix_; |
| 215 | 219 |
| 216 base::WeakPtrFactory<EnhancedBookmarkModel> weak_ptr_factory_; | 220 base::WeakPtrFactory<EnhancedBookmarkModel> weak_ptr_factory_; |
| 217 }; | 221 }; |
| 218 | 222 |
| 219 } // namespace enhanced_bookmarks | 223 } // namespace enhanced_bookmarks |
| 220 | 224 |
| 221 #endif // COMPONENTS_ENHANCED_BOOKMARKS_ENHANCED_BOOKMARK_MODEL_H_ | 225 #endif // COMPONENTS_ENHANCED_BOOKMARKS_ENHANCED_BOOKMARK_MODEL_H_ |
| OLD | NEW |