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