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
3 // found in the LICENSE file.
5 #ifndef COMPONENTS_ENHANCED_BOOKMARKS_ENHANCED_BOOKMARK_MODEL_H_
6 #define COMPONENTS_ENHANCED_BOOKMARKS_ENHANCED_BOOKMARK_MODEL_H_
11 #include "base/cancelable_callback.h"
12 #include "base/memory/linked_ptr.h"
13 #include "base/memory/weak_ptr.h"
14 #include "base/observer_list.h"
15 #include "base/strings/string16.h"
16 #include "components/bookmarks/browser/base_bookmark_model_observer.h"
17 #include "components/bookmarks/browser/bookmark_node.h"
18 #include "components/keyed_service/core/keyed_service.h"
31 FORWARD_DECLARE_TEST(EnhancedBookmarkModelTest
, SetMultipleMetaInfo
);
33 namespace enhanced_bookmarks
{
35 class EnhancedBookmarkModelObserver
;
37 // Wrapper around BookmarkModel providing utility functions for enhanced
39 class EnhancedBookmarkModel
: public KeyedService
,
40 public bookmarks::BaseBookmarkModelObserver
{
42 EnhancedBookmarkModel(bookmarks::BookmarkModel
* bookmark_model
,
43 const std::string
& version
);
44 ~EnhancedBookmarkModel() override
;
46 void Shutdown() override
;
48 void AddObserver(EnhancedBookmarkModelObserver
* observer
);
49 void RemoveObserver(EnhancedBookmarkModelObserver
* observer
);
51 // Moves |node| to |new_parent| and inserts it at the given |index|.
52 void Move(const bookmarks::BookmarkNode
* node
,
53 const bookmarks::BookmarkNode
* new_parent
,
56 // Adds a new folder node at the specified position.
57 const bookmarks::BookmarkNode
* AddFolder(
58 const bookmarks::BookmarkNode
* parent
,
60 const base::string16
& title
);
62 // Adds a url at the specified position.
63 const bookmarks::BookmarkNode
* AddURL(const bookmarks::BookmarkNode
* parent
,
65 const base::string16
& title
,
67 const base::Time
& creation_time
);
69 // Returns the remote id for a bookmark |node|.
70 std::string
GetRemoteId(const bookmarks::BookmarkNode
* node
);
72 // Returns the bookmark node corresponding to the given |remote_id|, or NULL
73 // if there is no node with the id.
74 const bookmarks::BookmarkNode
* BookmarkForRemoteId(
75 const std::string
& remote_id
);
77 // Sets the description of a bookmark |node|.
78 void SetDescription(const bookmarks::BookmarkNode
* node
,
79 const std::string
& description
);
81 // Returns the description of a bookmark |node|.
82 std::string
GetDescription(const bookmarks::BookmarkNode
* node
);
84 // Sets the URL of an image representative of a bookmark |node|.
85 // Expects the URL to be valid and not empty.
86 // Returns true if the metainfo is successfully populated.
87 bool SetOriginalImage(const bookmarks::BookmarkNode
* node
,
92 // Removes all image data for the node and sets the user_removed_image flag
93 // so the server won't try to fetch a new image for the node.
94 void RemoveImageData(const bookmarks::BookmarkNode
* node
);
96 // Returns the url and dimensions of the original scraped image of a
98 // Returns true if the out variables are populated, false otherwise.
99 bool GetOriginalImage(const bookmarks::BookmarkNode
* node
,
104 // Returns the url and dimensions of the server provided thumbnail image for
105 // a given bookmark |node|.
106 // Returns true if the out variables are populated, false otherwise.
107 bool GetThumbnailImage(const bookmarks::BookmarkNode
* node
,
112 // Returns a brief server provided synopsis of the bookmarked page.
113 // Returns the empty string if the snippet could not be extracted.
114 std::string
GetSnippet(const bookmarks::BookmarkNode
* node
);
116 // Sets a custom suffix to be added to the version field when writing meta
118 void SetVersionSuffix(const std::string
& version_suffix
);
120 // TODO(rfevang): Add method + enum for accessing/writing flags.
122 // Used for testing, simulates the process that creates the thumbnails. Will
123 // remove existing entries for empty urls or set them if the url is not empty.
124 // Expects valid or empty urls. Returns true if the metainfo is successfully
126 // TODO(rfevang): Move this to a testing only utility file.
127 bool SetAllImages(const bookmarks::BookmarkNode
* node
,
128 const GURL
& image_url
,
131 const GURL
& thumbnail_url
,
133 int thumbnail_height
);
135 // TODO(rfevang): Ideally nothing should need the underlying bookmark model.
136 // Remove when that is actually the case.
137 bookmarks::BookmarkModel
* bookmark_model() { return bookmark_model_
; }
139 // Returns true if the enhanced bookmark model is done loading.
140 bool loaded() { return loaded_
; }
142 // Returns the version string to use when setting stars.version.
143 std::string
GetVersionString();
146 FRIEND_TEST_ALL_PREFIXES(::EnhancedBookmarkModelTest
, SetMultipleMetaInfo
);
148 typedef std::map
<std::string
, const bookmarks::BookmarkNode
*> IdToNodeMap
;
149 typedef std::map
<const bookmarks::BookmarkNode
*, std::string
> NodeToIdMap
;
151 // bookmarks::BaseBookmarkModelObserver:
152 void BookmarkModelChanged() override
;
153 void BookmarkModelLoaded(bookmarks::BookmarkModel
* model
,
154 bool ids_reassigned
) override
;
155 void BookmarkNodeAdded(bookmarks::BookmarkModel
* model
,
156 const bookmarks::BookmarkNode
* parent
,
158 void BookmarkNodeRemoved(bookmarks::BookmarkModel
* model
,
159 const bookmarks::BookmarkNode
* parent
,
161 const bookmarks::BookmarkNode
* node
,
162 const std::set
<GURL
>& removed_urls
) override
;
163 void BookmarkNodeChanged(bookmarks::BookmarkModel
* model
,
164 const bookmarks::BookmarkNode
* node
) override
;
165 void OnWillChangeBookmarkMetaInfo(
166 bookmarks::BookmarkModel
* model
,
167 const bookmarks::BookmarkNode
* node
) override
;
168 void BookmarkMetaInfoChanged(bookmarks::BookmarkModel
* model
,
169 const bookmarks::BookmarkNode
* node
) override
;
170 void BookmarkAllUserNodesRemoved(bookmarks::BookmarkModel
* model
,
171 const std::set
<GURL
>& removed_urls
) override
;
173 // Initialize the mapping from remote ids to nodes.
174 void InitializeIdMap();
176 // Adds a node to the id map if it has a (unique) remote id. Must be followed
177 // by a (Schedule)ResetDuplicateRemoteIds call when done adding nodes.
178 void AddToIdMap(const bookmarks::BookmarkNode
* node
);
180 // Recursively removes a node and all its children from the various maps.
181 void RemoveNodeFromMaps(const bookmarks::BookmarkNode
* node
);
183 // If there are nodes that needs to reset their remote ids, schedules
184 // ResetDuplicateRemoteIds to be run asynchronously.
185 void ScheduleResetDuplicateRemoteIds();
187 // Clears out any duplicate remote ids detected by AddToIdMap calls.
188 void ResetDuplicateRemoteIds();
190 // Helper method for setting a meta info field on a node. Also updates the
192 void SetMetaInfo(const bookmarks::BookmarkNode
* node
,
193 const std::string
& field
,
194 const std::string
& value
);
196 // Helper method for setting multiple meta info fields at once. All the fields
197 // in |meta_info| will be set, but the method will not delete fields not
199 void SetMultipleMetaInfo(const bookmarks::BookmarkNode
* node
,
200 bookmarks::BookmarkNode::MetaInfoMap meta_info
);
202 bookmarks::BookmarkModel
* bookmark_model_
;
205 base::ObserverList
<EnhancedBookmarkModelObserver
> observers_
;
208 NodeToIdMap nodes_to_reset_
;
210 // Caches the remote id of a node before its meta info changes.
211 std::string prev_remote_id_
;
213 std::string version_
;
214 std::string version_suffix_
;
216 base::WeakPtrFactory
<EnhancedBookmarkModel
> weak_ptr_factory_
;
219 } // namespace enhanced_bookmarks
221 #endif // COMPONENTS_ENHANCED_BOOKMARKS_ENHANCED_BOOKMARK_MODEL_H_